|
| 1 | +# |
| 2 | +# Copyright (c) 2025 Huawei Technologies Co., Ltd. All Rights Reserved. |
| 3 | +# Copyright 2025 The vLLM team. |
| 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 | +# |
| 17 | + |
| 18 | +import contextlib |
| 19 | +import gc |
| 20 | +import math |
| 21 | +import multiprocessing |
| 22 | +import os |
| 23 | +import sys |
| 24 | +from time import sleep |
| 25 | +from unittest.mock import patch |
| 26 | + |
| 27 | +import pytest |
| 28 | +import torch |
| 29 | +from vllm import LLM, SamplingParams |
| 30 | +from vllm.distributed.parallel_state import ( # noqa E402 |
| 31 | + destroy_distributed_environment, destroy_model_parallel) |
| 32 | + |
| 33 | +MODELS = ["Qwen/Qwen3-0.6B", "vllm-ascend/DeepSeek-V2-Lite-W8A8"] |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.parametrize("model", MODELS) |
| 37 | +@pytest.mark.parametrize("max_tokens", [4]) |
| 38 | +@patch.dict(os.environ, {"ASCEND_RT_VISIBLE_DEVICES": "0,1"}) |
| 39 | +def test_aclgraph_capture_replay_dp2( |
| 40 | + model: str, |
| 41 | + max_tokens: int, |
| 42 | +) -> None: |
| 43 | + # HCCL_OP_EXPANSION_MODE determines how max_num_batch_sizes is computed. |
| 44 | + if 'VLLM_WORKER_MULTIPROC_METHOD' in os.environ: |
| 45 | + del os.environ["VLLM_WORKER_MULTIPROC_METHOD"] |
| 46 | + if 'HCCL_OP_EXPANSION_MODE' in os.environ: |
| 47 | + del os.environ['HCCL_OP_EXPANSION_MODE'] |
| 48 | + os.environ["HCCL_NPU_SOCKET_PORT_RANGE"] = 'auto' |
| 49 | + os.environ["HCCL_HOST_SOCKET_PORT_RANGE"] = 'auto' |
| 50 | + dp_size = 2 |
| 51 | + tp_size = 1 |
| 52 | + replay_counter = multiprocessing.Value("i", 0) |
| 53 | + capture_counter = multiprocessing.Value("i", 0) |
| 54 | + num_hidden_layers_shared = multiprocessing.Value("i", -1) |
| 55 | + num_execute_model_shared = multiprocessing.Value("i", 0) |
| 56 | + dp_master_ip = "127.0.0.1" |
| 57 | + dp_master_port = 11011 |
| 58 | + |
| 59 | + def dp_rank_main(global_dp_rank: int, local_dp_rank: int): |
| 60 | + os.environ["VLLM_DP_RANK"] = str(global_dp_rank) |
| 61 | + os.environ["VLLM_DP_RANK_LOCAL"] = str(local_dp_rank) |
| 62 | + os.environ["VLLM_DP_SIZE"] = str(dp_size) |
| 63 | + os.environ["VLLM_DP_MASTER_IP"] = dp_master_ip |
| 64 | + os.environ["VLLM_DP_MASTER_PORT"] = str(dp_master_port) |
| 65 | + |
| 66 | + original_replay = torch.npu.NPUGraph.replay |
| 67 | + |
| 68 | + def replay_wrapper(self): |
| 69 | + with replay_counter.get_lock(): |
| 70 | + replay_counter.value += 1 |
| 71 | + return original_replay(self) |
| 72 | + |
| 73 | + original_init = torch.npu.NPUGraph.__init__ |
| 74 | + |
| 75 | + def init_wrapper(self, *args, **kwargs): |
| 76 | + with capture_counter.get_lock(): |
| 77 | + capture_counter.value += 1 |
| 78 | + return original_init(self, *args, **kwargs) |
| 79 | + |
| 80 | + with patch.object(torch.npu.NPUGraph, "replay", replay_wrapper), \ |
| 81 | + patch.object(torch.npu.NPUGraph, "__init__", init_wrapper): |
| 82 | + prompts = [ |
| 83 | + "Hello, my name is", "The president of the United States is", |
| 84 | + "The capital of France is", "The future of AI is" |
| 85 | + ] |
| 86 | + chunk_size = len(prompts) // dp_size |
| 87 | + start = global_dp_rank * chunk_size |
| 88 | + end = start + chunk_size if global_dp_rank < dp_size - 1 else len( |
| 89 | + prompts) |
| 90 | + my_prompts = prompts[start:end] |
| 91 | + sampling_params = SamplingParams(max_tokens=max_tokens, |
| 92 | + temperature=0.0) |
| 93 | + |
| 94 | + def trace_calls(frame, event, arg): |
| 95 | + if event == 'call': |
| 96 | + code = frame.f_code |
| 97 | + func_name = code.co_name |
| 98 | + file_name = code.co_filename |
| 99 | + if func_name == 'execute_dummy_batch' and 'worker_v1.py' in file_name: |
| 100 | + with num_execute_model_shared.get_lock(): |
| 101 | + num_execute_model_shared.value += 1 |
| 102 | + return trace_calls |
| 103 | + |
| 104 | + sys.settrace(trace_calls) |
| 105 | + if model == "vllm-ascend/DeepSeek-V2-Lite-W8A8": |
| 106 | + llm = LLM( |
| 107 | + model=model, |
| 108 | + quantization="ascend", |
| 109 | + tensor_parallel_size=tp_size, |
| 110 | + trust_remote_code=True, |
| 111 | + ) |
| 112 | + else: |
| 113 | + llm = LLM( |
| 114 | + model=model, |
| 115 | + tensor_parallel_size=tp_size, |
| 116 | + trust_remote_code=True, |
| 117 | + ) |
| 118 | + num_hidden_layers_shared.value = llm.llm_engine.model_config.hf_config.num_hidden_layers |
| 119 | + _ = llm.generate(my_prompts, sampling_params) |
| 120 | + sys.settrace(None) |
| 121 | + |
| 122 | + # Give engines time to pause their processing loops before exiting. |
| 123 | + sleep(5) |
| 124 | + del llm |
| 125 | + cleanup_env_and_memory() |
| 126 | + |
| 127 | + processes = [] |
| 128 | + for local_dp_rank in range(dp_size): |
| 129 | + global_dp_rank = local_dp_rank |
| 130 | + p = multiprocessing.Process(target=dp_rank_main, |
| 131 | + args=(global_dp_rank, local_dp_rank)) |
| 132 | + p.start() |
| 133 | + processes.append(p) |
| 134 | + |
| 135 | + for p in processes: |
| 136 | + p.join(timeout=900) |
| 137 | + if p.exitcode != 0: |
| 138 | + if p.exitcode is None: |
| 139 | + p.kill() |
| 140 | + raise RuntimeError(f"Process {p.pid} timed out") |
| 141 | + else: |
| 142 | + raise RuntimeError( |
| 143 | + f"Process failed with exit code {p.exitcode}") |
| 144 | + |
| 145 | + actual_capture = capture_counter.value |
| 146 | + actual_replay = replay_counter.value |
| 147 | + num_hidden_layers = num_hidden_layers_shared.value |
| 148 | + num_execute_model = num_execute_model_shared.value |
| 149 | + |
| 150 | + num_acl_graphs = num_hidden_layers + 1 |
| 151 | + num_comm_groups = sum(size > 1 for size in [ |
| 152 | + dp_size, |
| 153 | + tp_size, |
| 154 | + ]) |
| 155 | + max_num_batch_sizes = math.floor( |
| 156 | + (1800 - num_comm_groups * 40) / num_acl_graphs / |
| 157 | + (1 + num_comm_groups * 2)) |
| 158 | + expected_total_capture = max_num_batch_sizes * num_acl_graphs * dp_size |
| 159 | + assert actual_capture == expected_total_capture, ( |
| 160 | + f"capture count mismatch. Expected: {expected_total_capture}, Got: {actual_capture}" |
| 161 | + ) |
| 162 | + |
| 163 | + num_inference_steps = max_tokens + 1 # first token + max_tokens |
| 164 | + expected_total_replay = num_acl_graphs * num_inference_steps * dp_size + num_execute_model * num_acl_graphs |
| 165 | + assert actual_replay == expected_total_replay, ( |
| 166 | + f"Replay count mismatch. Expected: {expected_total_replay}, Got: {actual_replay}" |
| 167 | + ) |
| 168 | + os.environ["VLLM_WORKER_MULTIPROC_METHOD"] = 'spawn' |
| 169 | + del os.environ["HCCL_NPU_SOCKET_PORT_RANGE"] |
| 170 | + del os.environ["HCCL_HOST_SOCKET_PORT_RANGE"] |
| 171 | + |
| 172 | + |
| 173 | +def cleanup_env_and_memory(): |
| 174 | + destroy_model_parallel() |
| 175 | + destroy_distributed_environment() |
| 176 | + with contextlib.suppress(AssertionError): |
| 177 | + torch.distributed.destroy_process_group() |
| 178 | + gc.collect() |
| 179 | + torch.npu.empty_cache() |
| 180 | + torch.npu.reset_peak_memory_stats() |
0 commit comments