|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +import torch |
| 3 | + |
| 4 | + |
| 5 | +def stateless_init_process_group(master_address, master_port, rank, world_size, |
| 6 | + device): |
| 7 | + """ |
| 8 | + vLLM provides `StatelessProcessGroup` to create a process group |
| 9 | + without considering the global process group in torch.distributed. |
| 10 | + It is recommended to create `StatelessProcessGroup`, and then initialize |
| 11 | + the data-plane communication (NCCL) between external (train processes) |
| 12 | + and vLLM workers. |
| 13 | + """ |
| 14 | + from vllm.distributed.device_communicators.pynccl import PyNcclCommunicator |
| 15 | + from vllm.distributed.utils import StatelessProcessGroup |
| 16 | + pg = StatelessProcessGroup.create(host=master_address, |
| 17 | + port=master_port, |
| 18 | + rank=rank, |
| 19 | + world_size=world_size) |
| 20 | + pynccl = PyNcclCommunicator(pg, device=device) |
| 21 | + return pynccl |
| 22 | + |
| 23 | + |
| 24 | +class WorkerExtension: |
| 25 | + """ |
| 26 | + The class for vLLM's worker to inherit from. |
| 27 | + By defining an extension class, the code can work no matter what is |
| 28 | + the underlying worker class. This way, the code can be compatible |
| 29 | + with both vLLM V0 and V1. |
| 30 | + NOTE: we define this class in a separate module, and the main module |
| 31 | + should pass the full qualified name as `worker_extension_cls` argument. |
| 32 | + """ |
| 33 | + |
| 34 | + def init_weight_update_group(self, master_address, master_port, |
| 35 | + rank_offset, world_size): |
| 36 | + from vllm.distributed.parallel_state import get_world_group |
| 37 | + rank = get_world_group().rank + rank_offset |
| 38 | + self.model_update_group = stateless_init_process_group( |
| 39 | + master_address, |
| 40 | + master_port, |
| 41 | + rank, |
| 42 | + world_size, |
| 43 | + self.device, |
| 44 | + ) |
| 45 | + |
| 46 | + def update_weight(self, name, dtype, shape): |
| 47 | + weight = torch.empty(shape, dtype=dtype, device="cuda") |
| 48 | + self.model_update_group.broadcast(weight, |
| 49 | + src=0, |
| 50 | + stream=torch.cuda.current_stream()) |
| 51 | + |
| 52 | + self.model_runner.model.load_weights(weights=[(name, weight)]) |
| 53 | + |
| 54 | + del weight |
| 55 | + |
| 56 | + def check_weights_changed(self): |
| 57 | + """ |
| 58 | + Check if the weights are updated to 0. |
| 59 | + """ |
| 60 | + weights_updated = True |
| 61 | + for name, p in self.model_runner.model.named_parameters(): |
| 62 | + weights_updated = weights_updated and torch.allclose( |
| 63 | + p, torch.zeros_like(p)) |
| 64 | + return weights_updated |
| 65 | + |
| 66 | + |
| 67 | +class ColocateWorkerExtension: |
| 68 | + """ |
| 69 | + The class for vLLM's worker to inherit from, in the colocate setting. |
| 70 | + By defining an extension class, the code can work no matter what is |
| 71 | + the underlying worker class. This way, the code can be compatible |
| 72 | + with both vLLM V0 and V1. |
| 73 | + NOTE: we define this class in a separate module, and the main module |
| 74 | + should pass the full qualified name as `worker_extension_cls` argument. |
| 75 | + """ |
| 76 | + |
| 77 | + def report_device_id(self) -> str: |
| 78 | + from vllm.platforms import current_platform |
| 79 | + self.device_uuid = current_platform.get_device_uuid(self.device.index) |
| 80 | + return self.device_uuid |
| 81 | + |
| 82 | + def update_weights_from_ipc_handles(self, ipc_handles): |
| 83 | + handles = ipc_handles[self.device_uuid] |
| 84 | + device_id = self.device.index |
| 85 | + weights = [] |
| 86 | + for name, handle in handles.items(): |
| 87 | + func, args = handle |
| 88 | + list_args = list(args) |
| 89 | + # the key is to change device id to the current device id |
| 90 | + # in case two processes have different CUDA_VISIBLE_DEVICES |
| 91 | + list_args[6] = device_id |
| 92 | + tensor = func(*list_args) |
| 93 | + weights.append((name, tensor)) |
| 94 | + self.model_runner.model.load_weights(weights=weights) |
| 95 | + torch.cuda.synchronize() |
| 96 | + |
| 97 | + def check_weights_changed(self): |
| 98 | + """ |
| 99 | + Check if the weights are updated to 0. |
| 100 | + """ |
| 101 | + weights_updated = True |
| 102 | + for name, p in self.model_runner.model.named_parameters(): |
| 103 | + weights_updated = weights_updated and torch.allclose( |
| 104 | + p, torch.zeros_like(p)) |
| 105 | + return weights_updated |
0 commit comments