communication_op.py 2.05 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3

4
from typing import Any, Optional, Union
5

6
import torch
7
import torch.distributed
8

王敏's avatar
王敏 committed
9
from .parallel_state import get_tp_group, get_ep_group
10
11


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


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


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


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

王敏's avatar
王敏 committed
35
36
37
38
39
40
41
42
43
44
45
def expert_parallel_all_gather(input_: torch.Tensor,
                                     dim: int = -1) -> torch.Tensor:
    """All-gather the input tensor across model parallel group."""
    return get_ep_group().all_gather(input_, dim)

def expert_parallel_gather(input_: torch.Tensor,
                                 dst: int = 0,
                                 dim: int = -1) -> Optional[torch.Tensor]:
    """Gather the input tensor across model parallel group."""
    return get_ep_group().gather(input_, dst, dim)

46

47
def broadcast_tensor_dict(tensor_dict: Optional[dict[Any, Union[torch.Tensor,
48
49
50
                                                                Any]]] = None,
                          src: int = 0):
    if not torch.distributed.is_initialized():
51
        return tensor_dict
52
    return get_tp_group().broadcast_tensor_dict(tensor_dict, src)