communication_op.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. _DSV41_PREFILL_TIMING = __import__("os").environ.get("DSV41_PREFILL_TIMING", "0") == "1"
  8. def tensor_model_parallel_all_reduce(input_: torch.Tensor) -> torch.Tensor:
  9. """All-reduce the input tensor across model parallel group."""
  10. if _DSV41_PREFILL_TIMING and input_.dim() >= 2 and input_.shape[0] >= 1024:
  11. # Stage 7: phase "allreduce" of the prefill budget.
  12. from vllm.model_executor.kernels.attention.dsa.candidate_blocks import _PrefillTimer
  13. with _PrefillTimer("allreduce", int(input_.shape[0])):
  14. return get_tp_group().all_reduce(input_)
  15. return get_tp_group().all_reduce(input_)
  16. def tensor_model_parallel_all_gather(
  17. input_: torch.Tensor, dim: int = -1
  18. ) -> torch.Tensor:
  19. """All-gather the input tensor across model parallel group."""
  20. return get_tp_group().all_gather(input_, dim)
  21. def tensor_model_parallel_reduce_scatter(
  22. input_: torch.Tensor, dim: int = -1
  23. ) -> torch.Tensor:
  24. """Reduce-Scatter the input tensor across model parallel group."""
  25. return get_tp_group().reduce_scatter(input_, dim)
  26. def tensor_model_parallel_gather(
  27. input_: torch.Tensor, dst: int = 0, dim: int = -1
  28. ) -> torch.Tensor | None:
  29. """Gather the input tensor across model parallel group."""
  30. return get_tp_group().gather(input_, dst, dim)
  31. def broadcast_tensor_dict(
  32. tensor_dict: dict[Any, torch.Tensor | Any] | None = None, src: int = 0
  33. ):
  34. if not torch.distributed.is_initialized():
  35. return tensor_dict
  36. return get_tp_group().broadcast_tensor_dict(tensor_dict, src)