Files
enginex-mthreads-vllm/vllm/distributed/communication_op.py

44 lines
1.3 KiB
Python
Raw Permalink Normal View History

2026-01-19 10:38:50 +08:00
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
from typing import Any
2026-01-09 13:34:11 +08:00
import torch
2026-01-19 10:38:50 +08:00
import torch.distributed
2026-01-09 13:34:11 +08:00
2026-01-19 10:38:50 +08:00
from .parallel_state import get_tp_group
2026-01-09 13:34:11 +08:00
def tensor_model_parallel_all_reduce(input_: torch.Tensor) -> torch.Tensor:
2026-01-19 10:38:50 +08:00
"""All-reduce the input tensor across model parallel group."""
return get_tp_group().all_reduce(input_)
2026-01-09 13:34:11 +08:00
2026-01-19 10:38:50 +08:00
def tensor_model_parallel_all_gather(
input_: torch.Tensor, dim: int = -1
) -> torch.Tensor:
2026-01-09 13:34:11 +08:00
"""All-gather the input tensor across model parallel group."""
2026-01-19 10:38:50 +08:00
return get_tp_group().all_gather(input_, dim)
2026-01-09 13:34:11 +08:00
2026-01-19 10:38:50 +08:00
def tensor_model_parallel_reduce_scatter(
input_: torch.Tensor, dim: int = -1
) -> torch.Tensor:
"""Reduce-Scatter the input tensor across model parallel group."""
return get_tp_group().reduce_scatter(input_, dim)
2026-01-09 13:34:11 +08:00
2026-01-19 10:38:50 +08:00
def tensor_model_parallel_gather(
input_: torch.Tensor, dst: int = 0, dim: int = -1
) -> torch.Tensor | None:
"""Gather the input tensor across model parallel group."""
return get_tp_group().gather(input_, dst, dim)
2026-01-09 13:34:11 +08:00
def broadcast_tensor_dict(
2026-01-19 10:38:50 +08:00
tensor_dict: dict[Any, torch.Tensor | Any] | None = None, src: int = 0
):
if not torch.distributed.is_initialized():
2026-01-09 13:34:11 +08:00
return tensor_dict
2026-01-19 10:38:50 +08:00
return get_tp_group().broadcast_tensor_dict(tensor_dict, src)