|
| 1 | +# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | + |
| 17 | +import paddle |
| 18 | + |
| 19 | +try: |
| 20 | + import paddle_custom_device |
| 21 | +except ImportError: |
| 22 | + raise ImportError("Current device does not support MC2!") |
| 23 | + |
| 24 | +from paddle import distributed as dist |
| 25 | +from paddle.autograd import PyLayer |
| 26 | +from paddle.distributed.fleet.utils.sequence_parallel_utils import ( |
| 27 | + ColumnSequenceParallelLinear, |
| 28 | + RowSequenceParallelLinear, |
| 29 | +) |
| 30 | + |
| 31 | +__all_gather_recomputation__ = False |
| 32 | +if int(os.getenv("MC2_Recompute", 0)): |
| 33 | + __all_gather_recomputation__ = True |
| 34 | + |
| 35 | + |
| 36 | +class MC2Column(PyLayer): |
| 37 | + @staticmethod |
| 38 | + def forward(ctx, input_, weight, group): |
| 39 | + ctx.save_for_backward(input_, weight) |
| 40 | + |
| 41 | + rank = dist.get_rank() |
| 42 | + hcomm_info = group.process_group.get_comm_name(rank) |
| 43 | + |
| 44 | + world_size = group.nranks |
| 45 | + output, gather_out = paddle_custom_device.npu.fused_allgather_mm( |
| 46 | + input_, |
| 47 | + weight, |
| 48 | + bias=None, |
| 49 | + hcom=hcomm_info, |
| 50 | + world_size=world_size, |
| 51 | + gather_index=0, |
| 52 | + gather_output=(not __all_gather_recomputation__), |
| 53 | + comm_turn=0, |
| 54 | + ) |
| 55 | + |
| 56 | + ctx.all_gather_output = gather_out |
| 57 | + ctx.world_size = world_size |
| 58 | + ctx.group = group |
| 59 | + return output |
| 60 | + |
| 61 | + @staticmethod |
| 62 | + def backward(ctx, grad_output): |
| 63 | + input_, weight = ctx.saved_tensor() |
| 64 | + |
| 65 | + if __all_gather_recomputation__: |
| 66 | + dim_size = input_.shape |
| 67 | + dim_size[0] = dim_size[0] * ctx.world_size |
| 68 | + all_gather_output = paddle.empty(dim_size, dtype=input_.dtype) |
| 69 | + all_gather_output.stop_gradient = True |
| 70 | + all_gather_work = dist.stream.all_gather(all_gather_output, input_, group=ctx.group, sync_op=False) |
| 71 | + else: |
| 72 | + all_gather_output = ctx.all_gather_output |
| 73 | + |
| 74 | + grad_input = paddle.matmul(grad_output, weight, transpose_y=True) |
| 75 | + sub_grad_input = paddle.empty(input_.shape, dtype=input_.dtype) |
| 76 | + reduce_scatter_work = dist.stream.reduce_scatter(sub_grad_input, grad_input, group=ctx.group, sync_op=False) |
| 77 | + |
| 78 | + if __all_gather_recomputation__: |
| 79 | + all_gather_work.wait() |
| 80 | + |
| 81 | + grad_weight = paddle.matmul(all_gather_output, grad_output, transpose_x=True) |
| 82 | + reduce_scatter_work.wait() |
| 83 | + |
| 84 | + return sub_grad_input, grad_weight |
| 85 | + |
| 86 | + |
| 87 | +class MC2Row(PyLayer): |
| 88 | + @staticmethod |
| 89 | + def forward(ctx, input_, weight, group): |
| 90 | + ctx.save_for_backward(input_, weight) |
| 91 | + |
| 92 | + rank = dist.get_rank() |
| 93 | + hcomm_info = group.process_group.get_comm_name(rank) |
| 94 | + world_size = group.nranks |
| 95 | + |
| 96 | + output = paddle_custom_device.npu.fused_mm_reduce_scatter( |
| 97 | + input_, |
| 98 | + weight, |
| 99 | + bias=None, |
| 100 | + hcom=hcomm_info, |
| 101 | + world_size=world_size, |
| 102 | + reduce_op="sum", |
| 103 | + comm_turn=0, |
| 104 | + ) |
| 105 | + |
| 106 | + ctx.hcomm_info = hcomm_info |
| 107 | + ctx.world_size = world_size |
| 108 | + return output |
| 109 | + |
| 110 | + @staticmethod |
| 111 | + def backward(ctx, grad_output): |
| 112 | + input_, weight = ctx.saved_tensor() |
| 113 | + hcomm_info = ctx.hcomm_info |
| 114 | + world_size = ctx.world_size |
| 115 | + |
| 116 | + grad_input, all_gather_grad_output = paddle_custom_device.npu.fused_allgather_mm( |
| 117 | + grad_output, |
| 118 | + weight.t(), |
| 119 | + bias=None, |
| 120 | + hcom=hcomm_info, |
| 121 | + world_size=world_size, |
| 122 | + gather_index=0, |
| 123 | + gather_output=True, |
| 124 | + comm_turn=0, |
| 125 | + ) |
| 126 | + grad_weight = paddle.matmul(input_, all_gather_grad_output, transpose_x=True) |
| 127 | + |
| 128 | + return grad_input, grad_weight |
| 129 | + |
| 130 | + |
| 131 | +class MC2ColumnSeqParallelLinear(ColumnSequenceParallelLinear): |
| 132 | + def forward(self, x): |
| 133 | + output = MC2Column.apply(x, self.weight, self.model_parallel_group) |
| 134 | + output = output + self.bias if self.bias is not None else output |
| 135 | + return output |
| 136 | + |
| 137 | + |
| 138 | +class MC2RowSeqParallelLinear(RowSequenceParallelLinear): |
| 139 | + def forward(self, x): |
| 140 | + output = MC2Row.apply(x, self.weight, self.model_parallel_group) |
| 141 | + output = output + self.bias if self.bias is not None else output |
| 142 | + return output |
0 commit comments