|  | 
|  | 1 | +# | 
|  | 2 | +# Copyright (c) 2025 Huawei Technologies Co., Ltd. All Rights Reserved. | 
|  | 3 | +# This file is a part of the vllm-ascend project. | 
|  | 4 | +# | 
|  | 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 6 | +# you may not use this file except in compliance with the License. | 
|  | 7 | +# You may obtain a copy of the License at | 
|  | 8 | +# | 
|  | 9 | +#     http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 10 | +# | 
|  | 11 | +# Unless required by applicable law or agreed to in writing, software | 
|  | 12 | +# distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 14 | +# See the License for the specific language governing permissions and | 
|  | 15 | +# limitations under the License. | 
|  | 16 | +import pytest | 
|  | 17 | + | 
|  | 18 | +from tests.conftest import VllmRunner | 
|  | 19 | +from vllm_ascend.ascend_config import clear_ascend_config, get_ascend_config | 
|  | 20 | + | 
|  | 21 | + | 
|  | 22 | +def _clean_up_ascend_config(func): | 
|  | 23 | + | 
|  | 24 | +    def wrapper(*args, **kwargs): | 
|  | 25 | +        clear_ascend_config() | 
|  | 26 | +        func(*args, **kwargs) | 
|  | 27 | +        clear_ascend_config() | 
|  | 28 | + | 
|  | 29 | +    return wrapper | 
|  | 30 | + | 
|  | 31 | + | 
|  | 32 | +@_clean_up_ascend_config | 
|  | 33 | +def test_run_without_ascend_config(): | 
|  | 34 | +    with VllmRunner("facebook/opt-125m"): | 
|  | 35 | +        ascend_config = get_ascend_config() | 
|  | 36 | + | 
|  | 37 | +        assert not ascend_config.torchair_graph_config.enabled | 
|  | 38 | +        assert not ascend_config.torchair_graph_config.use_cached_graph | 
|  | 39 | +        assert ascend_config.torchair_graph_config.graph_batch_sizes == [] | 
|  | 40 | +        assert not ascend_config.torchair_graph_config.graph_batch_sizes_init | 
|  | 41 | +        assert not ascend_config.ascend_scheduler_config.enabled | 
|  | 42 | +        assert ascend_config.expert_tensor_parallel_size == 1 | 
|  | 43 | + | 
|  | 44 | + | 
|  | 45 | +@_clean_up_ascend_config | 
|  | 46 | +def test_run_with_ascend_config(): | 
|  | 47 | +    input_additional_config = { | 
|  | 48 | +        "torchair_graph_config": { | 
|  | 49 | +            # torchair graph only works with deepseek. The e2e test should be added | 
|  | 50 | +            # in multicard test with deepseek models. | 
|  | 51 | +            "enabled": False, | 
|  | 52 | +            "use_cached_graph": True, | 
|  | 53 | +            "graph_batch_sizes": [1, 2, 4, 8], | 
|  | 54 | +            "graph_batch_sizes_init": False, | 
|  | 55 | +        }, | 
|  | 56 | +        "ascend_scheduler_config": { | 
|  | 57 | +            "enabled": True, | 
|  | 58 | +            "enable_chunked_prefill": True, | 
|  | 59 | +        }, | 
|  | 60 | +        "expert_tensor_parallel_size": 1 | 
|  | 61 | +    } | 
|  | 62 | +    with VllmRunner("facebook/opt-125m", | 
|  | 63 | +                    additional_config=input_additional_config): | 
|  | 64 | +        ascend_config = get_ascend_config() | 
|  | 65 | + | 
|  | 66 | +        assert ascend_config.is_initialized | 
|  | 67 | +        assert not ascend_config.torchair_graph_config.enabled | 
|  | 68 | +        assert ascend_config.torchair_graph_config.use_cached_graph | 
|  | 69 | +        assert ascend_config.torchair_graph_config.graph_batch_sizes == [ | 
|  | 70 | +            1, 2, 4, 8 | 
|  | 71 | +        ] | 
|  | 72 | +        assert not ascend_config.torchair_graph_config.graph_batch_sizes_init | 
|  | 73 | +        assert ascend_config.ascend_scheduler_config.enabled | 
|  | 74 | +        assert ascend_config.ascend_scheduler_config.enable_chunked_prefill | 
|  | 75 | +        assert ascend_config.expert_tensor_parallel_size == 1 | 
|  | 76 | + | 
|  | 77 | + | 
|  | 78 | +@_clean_up_ascend_config | 
|  | 79 | +def test_ascend_config_init_error(): | 
|  | 80 | +    # ascend_config should be initialized first | 
|  | 81 | +    with pytest.raises(RuntimeError): | 
|  | 82 | +        _ = get_ascend_config() | 
|  | 83 | + | 
|  | 84 | + | 
|  | 85 | +@_clean_up_ascend_config | 
|  | 86 | +def test_ascend_config_load_error(): | 
|  | 87 | +    # graph_batch_sizes should be list. | 
|  | 88 | +    with pytest.raises(TypeError): | 
|  | 89 | +        input_additional_config_fake_1 = { | 
|  | 90 | +            "torchair_graph_config": { | 
|  | 91 | +                "graph_batch_sizes": "fake_size", | 
|  | 92 | +            }, | 
|  | 93 | +        } | 
|  | 94 | +        with VllmRunner("facebook/opt-125m", | 
|  | 95 | +                        additional_config=input_additional_config_fake_1): | 
|  | 96 | +            pass | 
|  | 97 | + | 
|  | 98 | +    # graph_batch_sizes_init should not be True when graph_batch_sizes is not empty. | 
|  | 99 | +    with pytest.raises(ValueError): | 
|  | 100 | +        input_additional_config_fake_2 = { | 
|  | 101 | +            "torchair_graph_config": { | 
|  | 102 | +                "graph_batch_sizes": [1, 2, 4, 8], | 
|  | 103 | +                "graph_batch_sizes_init": True, | 
|  | 104 | +            }, | 
|  | 105 | +        } | 
|  | 106 | +        with VllmRunner("facebook/opt-125m", | 
|  | 107 | +                        additional_config=input_additional_config_fake_2): | 
|  | 108 | +            pass | 
|  | 109 | + | 
|  | 110 | +    # torchair graph only works with deepseek. | 
|  | 111 | +    with pytest.raises(NotImplementedError): | 
|  | 112 | +        input_additional_config_fake_2 = { | 
|  | 113 | +            "torchair_graph_config": { | 
|  | 114 | +                "enabled": True, | 
|  | 115 | +            }, | 
|  | 116 | +        } | 
|  | 117 | +        with VllmRunner("facebook/opt-125m", | 
|  | 118 | +                        additional_config=input_additional_config_fake_2): | 
|  | 119 | +            pass | 
0 commit comments