| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | from typing import Any |
| 5 | |
| 6 | import torch |
| 7 | import torch.distributed |
| 8 | |
| 9 | from .parallel_state import get_tp_group |
| 10 | |
| 11 | |
| 12 | def tensor_model_parallel_all_reduce(input_: torch.Tensor) -> torch.Tensor: |
| 13 | """All-reduce the input tensor across model parallel group.""" |
| 14 | return get_tp_group().all_reduce(input_) |
| 15 | |
| 16 | |
| 17 | def tensor_model_parallel_all_gather( |
| 18 | input_: torch.Tensor, dim: int = -1 |
| 19 | ) -> torch.Tensor: |
| 20 | """All-gather the input tensor across model parallel group.""" |
| 21 | return get_tp_group().all_gather(input_, dim) |
| 22 | |
| 23 | |
| 24 | def tensor_model_parallel_reduce_scatter( |
| 25 | input_: torch.Tensor, dim: int = -1 |
| 26 | ) -> torch.Tensor: |
| 27 | """Reduce-Scatter the input tensor across model parallel group.""" |
| 28 | return get_tp_group().reduce_scatter(input_, dim) |
| 29 | |
| 30 | |
| 31 | def tensor_model_parallel_gather( |
| 32 | input_: torch.Tensor, dst: int = 0, dim: int = -1 |
| 33 | ) -> torch.Tensor | None: |
| 34 | """Gather the input tensor across model parallel group.""" |
| 35 | return get_tp_group().gather(input_, dst, dim) |
| 36 | |
| 37 | |
| 38 | def broadcast_tensor_dict( |
| 39 | tensor_dict: dict[Any, torch.Tensor | Any] | None = None, src: int = 0 |
| 40 | ): |
| 41 | if not torch.distributed.is_initialized(): |
| 42 | return tensor_dict |
| 43 | return get_tp_group().broadcast_tensor_dict(tensor_dict, src) |