communication_op.py 1.46 KB
Newer Older
1
2
# SPDX-License-Identifier: Apache-2.0

3
from typing import Any, Optional, Union
4

5
import torch
6
import torch.distributed
7

8
from .parallel_state import get_tp_group
9
10


11
def tensor_model_parallel_all_reduce(input_: torch.Tensor) -> torch.Tensor:
12
13
    """All-reduce the input tensor across model parallel group."""
    return get_tp_group().all_reduce(input_)
14
15


16
17
def tensor_model_parallel_all_gather(input_: torch.Tensor,
                                     dim: int = -1) -> torch.Tensor:
18
    """All-gather the input tensor across model parallel group."""
19
    return get_tp_group().all_gather(input_, dim)
20
21


22
23
24
25
26
27
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)


28
29
def tensor_model_parallel_gather(input_: torch.Tensor,
                                 dst: int = 0,
30
                                 dim: int = -1) -> Optional[torch.Tensor]:
31
32
    """Gather the input tensor across model parallel group."""
    return get_tp_group().gather(input_, dst, dim)
33
34


35
def broadcast_tensor_dict(tensor_dict: Optional[dict[Any, Union[torch.Tensor,
36
37
38
                                                                Any]]] = None,
                          src: int = 0):
    if not torch.distributed.is_initialized():
39
        return tensor_dict
40
    return get_tp_group().broadcast_tensor_dict(tensor_dict, src)