MatmulAllReduce
算子基础信息
算子名称 |
MatmulAllReduce |
---|---|
torch_npu api接口 |
torch_npu.npu_mm_all_reduce_base(x1, x2, group, reduce_op, bias, comm_turn) |
支持的torch_npu版本 |
1.11.0, 2.1.0 |
支持的芯片类型 |
Atlas A2 训练系列产品 |
支持的数据类型 |
float16, bfloat16 |
算子IR及torch_npu接口参数
算子IR:
REG_OP(MatmulAllReduce) .INPUT(x1, TensorType({DT_FLOAT16, DT_BF16})) .INPUT(x2, TensorType({DT_FLOAT16, DT_BF16})) .OPTIONAL_INPUT(bias, TensorType({DT_FLOAT16, DT_BF16})) .OUTPUT(y, TensorType({DT_FLOAT16, DT_BF16})) .REQUIRED_ATTR(group, String) .ATTR(reduce_op, String, "sum") .ATTR(is_trans_a, Bool, false) .ATTR(is_trans_b, Bool, false) .ATTR(comm_turn, Int, 0) .OP_END_FACTORY_REG(MatmulAllReduce)
torch_npu接口:
npu_mm_all_reduce_base(Tensor self, Tensor x2, string group, *, str reduce_op='sum', Tensor? bias=None, int comm_turn=0) -> Tensor

torch_npu接口中的问号表示这个输入参数是可选的。
参数说明:
- x1:matmul左矩阵,shape要求输入为两维或者三维。
- x2:matmul右矩阵,shape要求输入为两维。
- bias:偏置,shape要求输入为一维。大小与x2最后一维相同。
- group:标识列组的字符串。
- reduce_op:reduce操作类型,目前仅支持sum。
- comm_turn:通信数据切分数,即总数据量/单次通信量,目前仅支持输入0。
模型中替换代码及算子计算逻辑
模型中替换代码:
import torch.distributed as dist world_size = 8 rank = 8 master_ip = '127.0.0.1' master_port = '50001' m = 64 k = 512 n = 128 input_shape = [m,k] weight_shape = [k,n] torch_npu.npu.set_device(rank) init_method = 'tcp://' init_method += master_ip + ':' + master_port dist.init_process_group(backend="hccl", rank=rank, world_size=world_size, init_method=init_method) if dist.is_available(): from torch.distributed.distributed_c10d import _get_default_group, ReduceOp default_pg = _get_default_group() world_size = torch.distributed.get_world_size(default_pg) if torch.__version__ > '2.0.1': hcomm_info = default_pg._get_backend(torch.device("npu")).get_hccl_comm_name(rank) else: hcomm_info = default_pg.get_hccl_comm_name(rank) weight = torch.randn(weight_shape, dtype=dtype).npu() input = torch.randn(input_shape, dtype=dtype).npu() output = torch.matmul(input, weight) dist.all_reduce(output,op=ReduceOp.SUM)
其中output替换为:
output = torch_npu.npu_mm_all_reduce_base(input, weight, hcomm_info, reduce_op="sum", comm_turn=0)
算子替换的模型中小算子
MatMul/hcom_allReduce
图1 计算图


使用限制
当前仅支持Atlas A2 训练系列产品TP切分场景
已支持模型典型case
GPT3 65B
- case 1:
x1: S = 1 ~ 8192,{S,1024}, BF16/FP16
x2: {1024,8192}, BF16/FP16
bias: {8192}
- case 2:
x1: S = 1 ~ 8192,{S,2732}, BF16/FP16
x2: {2732,8192}, BF16/FP16
bias: {8192}
- case 3:
x1: B = 1 ~ 24,{B,1024}, BF16/FP16
x2: {1024,8192}, BF16/FP16
bias: {8192}
- case 4:
x1: B = 1 ~ 24,{B,2732}, BF16/FP16
x2: {2732,8192}, BF16/FP16
bias: {8192}
父主题: 融合算子替换