2024-06-13 08:27:08 +08:00
|
|
|
from typing import Any, Dict, Optional, Union
|
2024-01-20 03:23:30 +08:00
|
|
|
|
2023-10-03 06:36:09 +08:00
|
|
|
import torch
|
2024-06-13 08:27:08 +08:00
|
|
|
import torch.distributed
|
2023-10-03 06:36:09 +08:00
|
|
|
|
2024-06-13 08:27:08 +08:00
|
|
|
from .parallel_state import get_tp_group
|
2023-10-03 06:36:09 +08:00
|
|
|
|
|
|
|
|
|
2024-01-20 03:23:30 +08:00
|
|
|
def tensor_model_parallel_all_reduce(input_: torch.Tensor) -> torch.Tensor:
|
2024-06-13 08:27:08 +08:00
|
|
|
"""All-reduce the input tensor across model parallel group."""
|
|
|
|
|
return get_tp_group().all_reduce(input_)
|
2023-10-03 06:36:09 +08:00
|
|
|
|
|
|
|
|
|
2024-01-20 03:23:30 +08:00
|
|
|
def tensor_model_parallel_all_gather(input_: torch.Tensor,
|
|
|
|
|
dim: int = -1) -> torch.Tensor:
|
2023-10-03 06:36:09 +08:00
|
|
|
"""All-gather the input tensor across model parallel group."""
|
2024-06-13 08:27:08 +08:00
|
|
|
return get_tp_group().all_gather(input_, dim)
|
2024-01-04 03:30:22 +08:00
|
|
|
|
|
|
|
|
|
2024-01-20 03:23:30 +08:00
|
|
|
def tensor_model_parallel_gather(input_: torch.Tensor,
|
|
|
|
|
dst: int = 0,
|
2024-08-13 13:33:41 +08:00
|
|
|
dim: int = -1) -> Optional[torch.Tensor]:
|
2024-06-13 08:27:08 +08:00
|
|
|
"""Gather the input tensor across model parallel group."""
|
|
|
|
|
return get_tp_group().gather(input_, dst, dim)
|
2024-01-04 03:30:22 +08:00
|
|
|
|
|
|
|
|
|
2024-06-13 08:27:08 +08:00
|
|
|
def broadcast_tensor_dict(tensor_dict: Optional[Dict[Any, Union[torch.Tensor,
|
|
|
|
|
Any]]] = None,
|
|
|
|
|
src: int = 0):
|
|
|
|
|
if not torch.distributed.is_initialized():
|
2024-05-16 15:53:51 +08:00
|
|
|
return tensor_dict
|
2024-06-13 08:27:08 +08:00
|
|
|
return get_tp_group().broadcast_tensor_dict(tensor_dict, src)
|