Repository navigation
[Core] Support data parallel for vllm-v0 engine #339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| # | ||
| # Copyright (c) 2025 Huawei Technologies Co., Ltd. All Rights Reserved. | ||
| # This file is a part of the vllm-ascend project. | ||
| # Adapted from vllm-project/vllm/examples/offline_inference/basic.py | ||
| # Copyright 2023 The vLLM team. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
|
|
||
| import gc | ||
| import os | ||
| from multiprocessing import Process | ||
|
|
||
|
|
||
| def main(dp_size, dp_rank, dp_master_ip, dp_master_port, tp_size): | ||
| os.environ["VLLM_DP_RANK"] = str(dp_rank) | ||
| os.environ["VLLM_DP_SIZE"] = str(dp_size) | ||
| os.environ["VLLM_DP_MASTER_IP"] = dp_master_ip | ||
| os.environ["VLLM_DP_MASTER_PORT"] = str(dp_master_port) | ||
| os.environ["ASCEND_RT_VISIBLE_DEVICES"] = ",".join( | ||
| str(i) for i in range(dp_rank * tp_size, (dp_rank + 1) * tp_size)) | ||
|
|
||
| import torch | ||
| import torch_npu # noqa | ||
| from vllm import LLM, SamplingParams | ||
| from vllm.distributed.parallel_state import ( | ||
| destroy_distributed_environment, destroy_model_parallel) | ||
|
|
||
| import vllm_ascend # noqa | ||
|
|
||
| prompts = [ | ||
| "Hello, my name is", | ||
| "The president of the United States is", | ||
| "The capital of France is", | ||
| "The future of AI is", | ||
| ] * 4 | ||
|
|
||
| promts_per_rank = len(prompts) // dp_size | ||
| start = dp_rank * promts_per_rank | ||
| end = start + promts_per_rank | ||
| prompts = prompts[start:end] | ||
| if len(prompts) == 0: | ||
| prompts = ["Placeholder"] | ||
| print(f"DP rank {dp_rank} needs to process {len(prompts)} prompts") | ||
|
|
||
| sampling_params = SamplingParams(temperature=0.8, | ||
| top_p=0.95, | ||
| max_tokens=4, | ||
| min_tokens=4) | ||
|
|
||
| # Create an LLM. | ||
| llm = LLM(model="deepseek-ai/DeepSeek-V2-Lite-Chat", | ||
| tensor_parallel_size=tp_size, | ||
| enable_expert_parallel=True, | ||
| enforce_eager=True, | ||
| trust_remote_code=True, | ||
| max_model_len=4096) | ||
|
|
||
| outputs = llm.generate(prompts, sampling_params) | ||
| # Print the outputs. | ||
| for output in outputs: | ||
| prompt = output.prompt | ||
| generated_text = output.outputs[0].text | ||
| print(f"DP rank {dp_rank}, Prompt: {prompt!r}, " | ||
| f"Generated text: {generated_text!r}") | ||
|
|
||
| del llm | ||
| destroy_model_parallel() | ||
| destroy_distributed_environment() | ||
| gc.collect() | ||
| torch.npu.empty_cache() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| dp_master_ip = "127.0.0.1" | ||
| dp_master_port = 29500 | ||
| procs = [] | ||
|
|
||
| TP_size = 2 | ||
| DP_size = 2 | ||
| for i in range(DP_size): | ||
| proc = Process(target=main, | ||
| args=(DP_size, i, dp_master_ip, dp_master_port, | ||
| TP_size)) | ||
| proc.start() | ||
| procs.append(proc) | ||
| for proc in procs: | ||
| proc.join() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,5 +22,9 @@ def register(): | |
|
|
||
|
|
||
| def register_model(): | ||
| from . import config # noqa | ||
| from . import forward_context # noqa | ||
| from .distributed import utils # noqa | ||
| from .engine import llm_engine # noqa | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these patches seems no related to model registion, if we have to do so, let's do patch in |
||
| from .models import register_model | ||
| register_model() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| # Copyright (c) 2025 Huawei Technologies Co., Ltd. All Rights Reserved. | ||
| # This file is a part of the vllm-ascend project. | ||
| # Adapted from vllm/tests/kernels/test_moe.py | ||
| # Copyright 2023 The vLLM team. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import torch | ||
| from torch.distributed import ProcessGroup, ReduceOp | ||
| from vllm.config import ParallelConfig | ||
|
|
||
|
|
||
| def stateless_init_dp_group(self) -> "ProcessGroup": | ||
| from vllm.distributed.utils import \ | ||
| stateless_init_torch_distributed_process_group | ||
|
|
||
| dp_group = stateless_init_torch_distributed_process_group( | ||
| self.data_parallel_master_ip, | ||
| self.get_next_dp_init_port(), | ||
| self.data_parallel_rank, | ||
| self.data_parallel_size, | ||
| backend="hccl") | ||
|
|
||
| return dp_group | ||
|
|
||
|
|
||
| def has_unfinished_dp(dp_group: "ProcessGroup", has_unfinished: bool) -> bool: | ||
|
|
||
| tensor = torch.tensor([has_unfinished], dtype=torch.int32, device="npu") | ||
| torch.distributed.all_reduce(tensor, op=ReduceOp.MAX, group=dp_group) | ||
| aggregated_has_unfinished = bool(tensor.item()) | ||
| return aggregated_has_unfinished | ||
|
|
||
|
|
||
| ParallelConfig.stateless_init_dp_group = stateless_init_dp_group | ||
| ParallelConfig.has_unfinished_dp = has_unfinished_dp |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| # Copyright (c) 2025 Huawei Technologies Co., Ltd. All Rights Reserved. | ||
| # This file is a part of the vllm-ascend project. | ||
| # Adapted from vllm/tests/kernels/test_moe.py | ||
| # Copyright 2023 The vLLM team. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import torch | ||
| from torch.distributed import ProcessGroup, is_hccl_available | ||
| from torch.distributed.distributed_c10d import (Backend, PrefixStore, | ||
| _get_default_timeout, | ||
| is_nccl_available) | ||
| from torch.distributed.rendezvous import rendezvous | ||
| from vllm.distributed import utils | ||
|
|
||
|
|
||
| def stateless_init_torch_distributed_process_group( | ||
| host: str, port: int, rank: int, world_size: int, | ||
| backend: str) -> ProcessGroup: | ||
| """ | ||
| A replacement for `torch.distributed.init_process_group` that does not | ||
| pollute the global state. The created ProcessGroup object can be used for | ||
| some operations such as `allreduce`, because it does not depend on the | ||
| global rank. However, some operations such as `broadcast` cannot be used | ||
| because it depends on the global rank. | ||
|
|
||
| This function is useful when we are not sure about the total number of | ||
| processes in the process group. For example, we may have process | ||
| 1, 2, ..., 8 who want to communicate, and process 9 might be the same | ||
| process as process 1, or it might be a different process; process 10 | ||
| might be the same process as process 5, or it might be a different process. | ||
| In this case, how can we reliably form a communication channel within | ||
| process 9 and 10, without affecting the communication channel within | ||
| process 1, 2, ..., 8? | ||
|
|
||
| One possible solution is to figure out if process 9 and 10 are the same | ||
| as process 1 and 5 beforehand, and then form a communication channel | ||
| based on the information, adjusting the ranks and world_size etc. However, | ||
| figuring out the information is not always easy, and it will interfere | ||
| with the main communication channel. | ||
|
|
||
| Our solution is to always form a communication channel with process 1, 2, | ||
| ..., 8, and then use this function to form another communication channel | ||
| with process 9 and 10. This way, regardless of whether process 9 and 10 | ||
| are the same as process 1 and 5, the main communication channel is | ||
| always formed with process 1, 2, ..., 8, and the additional communication | ||
| channel is formed with process 9 and 10. | ||
| """ | ||
| init_method = f"tcp://{host}:{port}" | ||
| backend = Backend(backend) # it is basically string | ||
| timeout = _get_default_timeout(backend) | ||
|
|
||
| store, rank, world_size = next( | ||
| rendezvous(init_method, rank, world_size, timeout=timeout)) | ||
| store.set_timeout(timeout) | ||
|
|
||
| group_rank = rank | ||
| group_size = world_size | ||
|
|
||
| # Use a PrefixStore to avoid accidental overrides of keys used by | ||
| # different systems (e.g. RPC) in case the store is multi-tenant. | ||
| prefix_store = PrefixStore(init_method, store) | ||
|
|
||
| pg_options = ProcessGroup.Options(backend=backend, timeout=timeout) | ||
|
|
||
| pg: ProcessGroup = ProcessGroup( | ||
| prefix_store, | ||
| group_rank, | ||
| group_size, | ||
| pg_options, | ||
| ) | ||
|
|
||
| if backend == "gloo": | ||
| from torch.distributed.distributed_c10d import ProcessGroupGloo | ||
| backend_class = ProcessGroupGloo(prefix_store, | ||
| group_rank, | ||
| group_size, | ||
| timeout=timeout) | ||
| backend_type = ProcessGroup.BackendType.GLOO | ||
| device = torch.device("cpu") | ||
| backend_class._set_sequence_number_for_group() | ||
| pg._register_backend(device, backend_type, backend_class) | ||
|
|
||
| elif backend == "nccl": | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's remove the cuda related code here |
||
| assert is_nccl_available() | ||
| from torch.distributed.distributed_c10d import ProcessGroupNCCL | ||
|
|
||
| backend_options = ProcessGroupNCCL.Options() | ||
| backend_options._timeout = timeout | ||
|
|
||
| backend_class = ProcessGroupNCCL(prefix_store, group_rank, group_size, | ||
| backend_options) | ||
| backend_type = ProcessGroup.BackendType.NCCL | ||
| device = torch.device("cuda") | ||
| backend_class._set_sequence_number_for_group() | ||
| pg._register_backend(device, backend_type, backend_class) | ||
|
|
||
| elif backend == "hccl": | ||
| assert is_hccl_available() | ||
| import torch_npu # noqa | ||
| from torch_npu._C._distributed_c10d import ProcessGroupHCCL | ||
| backend_options = ProcessGroupHCCL.Options() | ||
| backend_options._timeout = timeout | ||
| backend_class = ProcessGroupHCCL(prefix_store, group_rank, group_size, | ||
| backend_options) | ||
| device = torch.device("npu") | ||
| backend_class._set_sequence_number_for_group() | ||
| backend_type = ProcessGroup.BackendType.CUSTOM | ||
| pg._register_backend(device, backend_type, backend_class) | ||
|
|
||
| return pg | ||
|
|
||
|
|
||
| utils.stateless_init_torch_distributed_process_group = stateless_init_torch_distributed_process_group | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # Copyright (c) 2025 Huawei Technologies Co., Ltd. All Rights Reserved. | ||
| # This file is a part of the vllm-ascend project. | ||
| # Adapted from vllm/tests/kernels/test_moe.py | ||
| # Copyright 2023 The vLLM team. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
|
|
||
| def apply_custom_engine_patch(): | ||
| from vllm.config import ParallelConfig | ||
| from vllm.config import VllmConfig as vllm_config # noqa | ||
| from vllm.engine.arg_utils import EngineArgs # noqa | ||
| from vllm.engine.llm_engine import LLMEngine | ||
|
|
||
| original_init = LLMEngine.__init__ | ||
|
|
||
| def new_init(self, *args, **kwargs): | ||
| original_init(self, *args, **kwargs) | ||
| self.dp_enabled = self.parallel_config.data_parallel_size > 1 # noqa | ||
| self.should_execute_dummy_batch = False | ||
| if self.dp_enabled: | ||
| self.dp_group = self.parallel_config.stateless_init_dp_group() | ||
|
|
||
| def new_has_unfinished_requests(self) -> bool: | ||
| has_unfinished = any(scheduler.has_unfinished_seqs() | ||
| for scheduler in self.scheduler) | ||
| if not self.dp_enabled: | ||
| return has_unfinished | ||
| return new_has_unfinished_requests_dp(self, has_unfinished) | ||
|
|
||
| def new_has_unfinished_requests_dp(self, has_unfinished: bool) -> bool: | ||
| aggregated_has_unfinished = ParallelConfig.has_unfinished_dp( | ||
| self.dp_group, has_unfinished) | ||
| if not has_unfinished and aggregated_has_unfinished: | ||
| self.should_execute_dummy_batch = True | ||
| return aggregated_has_unfinished | ||
|
|
||
| LLMEngine.__init__ = new_init | ||
| LLMEngine.has_unfinished_requests = new_has_unfinished_requests | ||
| LLMEngine.has_unfinished_requests_dp = new_has_unfinished_requests_dp | ||
| print("[Success] Custom LLMEngine patch applied!") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need to patch engine? I don't think it's a good idea to patch engine |
||
|
|
||
|
|
||
| apply_custom_engine_patch() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there is no need to import vllm-ascend