|
| 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 | +# |
| 17 | + |
| 18 | +from typing import Optional, Union |
| 19 | + |
| 20 | +import torch |
| 21 | +import torch.distributed as dist |
| 22 | +import torch_npu # noqa: F401 |
| 23 | +from torch.distributed import ProcessGroup, ReduceOp |
| 24 | +from vllm.distributed.utils import StatelessProcessGroup |
| 25 | +from vllm.logger import logger |
| 26 | + |
| 27 | +from vllm_ascend.distributed.device_communicators.pyhccl_wrapper import ( |
| 28 | + HCCLLibrary, aclrtStream_t, buffer_type, hcclComm_t, hcclDataTypeEnum, |
| 29 | + hcclRedOpTypeEnum, hcclUniqueId) |
| 30 | +from vllm_ascend.utils import current_stream |
| 31 | + |
| 32 | + |
| 33 | +class PyHcclCommunicator: |
| 34 | + |
| 35 | + def __init__( |
| 36 | + self, |
| 37 | + group: Union[ProcessGroup, StatelessProcessGroup], |
| 38 | + device: Union[int, str, torch.device], |
| 39 | + library_path: Optional[str] = None, |
| 40 | + ): |
| 41 | + """ |
| 42 | + Args: |
| 43 | + group: the process group to work on. If None, it will use the |
| 44 | + default process group. |
| 45 | + device: the device to bind the PyHcclCommunicator to. If None, |
| 46 | + it will be bind to f"npu:{local_rank}". |
| 47 | + library_path: the path to the HCCL library. If None, it will |
| 48 | + use the default library path. |
| 49 | + It is the caller's responsibility to make sure each communicator |
| 50 | + is bind to a unique device. |
| 51 | + """ |
| 52 | + |
| 53 | + if not isinstance(group, StatelessProcessGroup): |
| 54 | + assert dist.is_initialized() |
| 55 | + assert dist.get_backend(group) != dist.Backend.HCCL, ( |
| 56 | + "PyHcclCommunicator should be attached to a non-HCCL group.") |
| 57 | + # note: this rank is the rank in the group |
| 58 | + self.rank = dist.get_rank(group) |
| 59 | + self.world_size = dist.get_world_size(group) |
| 60 | + else: |
| 61 | + self.rank = group.rank |
| 62 | + self.world_size = group.world_size |
| 63 | + |
| 64 | + self.group = group |
| 65 | + |
| 66 | + # if world_size == 1, no need to create communicator |
| 67 | + if self.world_size == 1: |
| 68 | + self.available = False |
| 69 | + self.disabled = True |
| 70 | + return |
| 71 | + |
| 72 | + try: |
| 73 | + self.hccl = HCCLLibrary(library_path) |
| 74 | + except Exception: |
| 75 | + # disable because of missing HCCL library |
| 76 | + # e.g. in a non-NPU environment |
| 77 | + self.available = False |
| 78 | + self.disabled = True |
| 79 | + return |
| 80 | + |
| 81 | + self.available = True |
| 82 | + self.disabled = False |
| 83 | + |
| 84 | + logger.info("vLLM is using pyhccl") |
| 85 | + |
| 86 | + if isinstance(device, int): |
| 87 | + device = torch.device(f"npu:{device}") |
| 88 | + elif isinstance(device, str): |
| 89 | + device = torch.device(device) |
| 90 | + # now `device` is a `torch.device` object |
| 91 | + assert isinstance(device, torch.device) |
| 92 | + self.device = device |
| 93 | + |
| 94 | + if self.rank == 0: |
| 95 | + # get the unique id from HCCL |
| 96 | + with torch.npu.device(device): |
| 97 | + self.unique_id = self.hccl.hcclGetUniqueId() |
| 98 | + else: |
| 99 | + # construct an empty unique id |
| 100 | + self.unique_id = hcclUniqueId() |
| 101 | + |
| 102 | + if not isinstance(group, StatelessProcessGroup): |
| 103 | + tensor = torch.ByteTensor(list(self.unique_id.internal)) |
| 104 | + ranks = dist.get_process_group_ranks(group) |
| 105 | + # arg `src` in `broadcast` is the global rank |
| 106 | + dist.broadcast(tensor, src=ranks[0], group=group) |
| 107 | + byte_list = tensor.tolist() |
| 108 | + for i, byte in enumerate(byte_list): |
| 109 | + self.unique_id.internal[i] = byte |
| 110 | + else: |
| 111 | + self.unique_id = group.broadcast_obj(self.unique_id, src=0) |
| 112 | + |
| 113 | + # hccl communicator and stream will use this device |
| 114 | + # `torch.npu.device` is a context manager that changes the |
| 115 | + # current npu device to the specified one |
| 116 | + with torch.npu.device(device): |
| 117 | + self.comm: hcclComm_t = self.hccl.hcclCommInitRank( |
| 118 | + self.world_size, self.unique_id, self.rank) |
| 119 | + |
| 120 | + stream = current_stream() |
| 121 | + # A small all_reduce for warmup. |
| 122 | + data = torch.zeros(1, device=device) |
| 123 | + self.all_reduce(data) |
| 124 | + stream.synchronize() |
| 125 | + del data |
| 126 | + |
| 127 | + def all_reduce(self, |
| 128 | + in_tensor: torch.Tensor, |
| 129 | + op: ReduceOp = ReduceOp.SUM, |
| 130 | + stream=None) -> torch.Tensor: |
| 131 | + if self.disabled: |
| 132 | + return None |
| 133 | + # hccl communicator created on a specific device |
| 134 | + # will only work on tensors on the same device |
| 135 | + # otherwise it will cause "illegal memory access" |
| 136 | + assert in_tensor.device == self.device, ( |
| 137 | + f"this hccl communicator is created to work on {self.device}, " |
| 138 | + f"but the input tensor is on {in_tensor.device}") |
| 139 | + |
| 140 | + out_tensor = torch.empty_like(in_tensor) |
| 141 | + |
| 142 | + if stream is None: |
| 143 | + stream = current_stream() |
| 144 | + self.hccl.hcclAllReduce(buffer_type(in_tensor.data_ptr()), |
| 145 | + buffer_type(out_tensor.data_ptr()), |
| 146 | + in_tensor.numel(), |
| 147 | + hcclDataTypeEnum.from_torch(in_tensor.dtype), |
| 148 | + hcclRedOpTypeEnum.from_torch(op), self.comm, |
| 149 | + aclrtStream_t(stream.npu_stream)) |
| 150 | + return out_tensor |
| 151 | + |
| 152 | + def broadcast(self, tensor: torch.Tensor, src: int, stream=None): |
| 153 | + if self.disabled: |
| 154 | + return |
| 155 | + assert tensor.device == self.device, ( |
| 156 | + f"this hccl communicator is created to work on {self.device}, " |
| 157 | + f"but the input tensor is on {tensor.device}") |
| 158 | + if stream is None: |
| 159 | + stream = current_stream() |
| 160 | + if src == self.rank: |
| 161 | + buffer = buffer_type(tensor.data_ptr()) |
| 162 | + else: |
| 163 | + buffer = buffer_type(tensor.data_ptr()) |
| 164 | + self.hccl.hcclBroadcast(buffer, tensor.numel(), |
| 165 | + hcclDataTypeEnum.from_torch(tensor.dtype), src, |
| 166 | + self.comm, aclrtStream_t(stream.npu_stream)) |
0 commit comments