communication_op.py 1.3 KB

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