|
| 1 | +# Copyright (c) 2025 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 | + |
| 16 | +import paddle.distributed as dist |
| 17 | +from paddle.distributed.auto_parallel.intermediate.tensor_parallel import ( |
| 18 | + PrepareLayerInput, |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +def layer_input_parallel_row_hook(process_mesh): |
| 23 | + def hook(layer, inputs, output=None): |
| 24 | + res_inputs = [] |
| 25 | + for input in inputs: |
| 26 | + if not input.is_dist(): |
| 27 | + x = dist.shard_tensor(input, process_mesh, [dist.Shard(0), dist.Replicate()]) |
| 28 | + res_inputs.append(dist.reshard(x, process_mesh, [dist.Shard(0), dist.Replicate()])) |
| 29 | + else: |
| 30 | + res_inputs.append(dist.reshard(input, process_mesh, [dist.Shard(0), dist.Replicate()])) |
| 31 | + return tuple(res_inputs) |
| 32 | + |
| 33 | + return hook |
| 34 | + |
| 35 | + |
| 36 | +def layer_input_parallel_row_and_col_hook(process_mesh): |
| 37 | + def hook(layer, inputs, output=None): |
| 38 | + res_inputs = [] |
| 39 | + for input in inputs: |
| 40 | + if not input.is_dist(): |
| 41 | + x = dist.shard_tensor(input, process_mesh, [dist.Shard(0), dist.Shard(1)]) |
| 42 | + res_inputs.append(dist.reshard(x, process_mesh, [dist.Shard(0), dist.Shard(1)])) |
| 43 | + else: |
| 44 | + res_inputs.append(dist.reshard(input, process_mesh, [dist.Shard(0), dist.Shard(1)])) |
| 45 | + return tuple(res_inputs) |
| 46 | + |
| 47 | + return hook |
| 48 | + |
| 49 | + |
| 50 | +def layer_input_replicate_hook(process_mesh): |
| 51 | + def hook(layer, inputs, output=None): |
| 52 | + res_inputs = [] |
| 53 | + for input in inputs: |
| 54 | + if not input.is_dist(): |
| 55 | + x = dist.shard_tensor(input, process_mesh, [dist.Replicate(), dist.Replicate()]) |
| 56 | + res_inputs.append(dist.reshard(x, process_mesh, [dist.Replicate(), dist.Replicate()])) |
| 57 | + else: |
| 58 | + res_inputs.append(dist.reshard(input, process_mesh, [dist.Replicate(), dist.Replicate()])) |
| 59 | + return tuple(res_inputs) |
| 60 | + |
| 61 | + return hook |
| 62 | + |
| 63 | + |
| 64 | +def auto_dist_config(self, prefix=""): |
| 65 | + if prefix != "": |
| 66 | + assert prefix.endswith(".") |
| 67 | + config = { |
| 68 | + "sp_config": { |
| 69 | + "parallelize_plan": { |
| 70 | + f"{prefix}llama.embed_tokens": [ |
| 71 | + dist.ColWiseParallel(), |
| 72 | + dist.SequenceParallelBegin(), |
| 73 | + ], |
| 74 | + f"{prefix}llama.reshard_row": PrepareLayerInput(layer_input_parallel_row_hook), |
| 75 | + f"{prefix}llama.reshard_row_and_col": PrepareLayerInput(layer_input_parallel_row_and_col_hook), |
| 76 | + f"{prefix}llama.global_layer.reshard_replicate": PrepareLayerInput(layer_input_replicate_hook), |
| 77 | + f"{prefix}llama.layers.*.self_attn.qkv_proj": dist.ColWiseParallel(), |
| 78 | + f"{prefix}llama.layers.*.self_attn.q_proj": dist.ColWiseParallel(), |
| 79 | + f"{prefix}llama.layers.*.self_attn.k_proj": dist.ColWiseParallel(), |
| 80 | + f"{prefix}llama.layers.*.self_attn.v_proj": dist.ColWiseParallel(), |
| 81 | + f"{prefix}llama.layers.*.self_attn.o_proj": dist.RowWiseParallel(), |
| 82 | + f"{prefix}llama.layers.*.self_attn": dist.SequenceParallelDisable(), |
| 83 | + f"{prefix}llama.layers.*.mlp.gate_proj": dist.ColWiseParallel(), |
| 84 | + f"{prefix}llama.layers.*.mlp.up_proj": dist.ColWiseParallel(), |
| 85 | + f"{prefix}llama.layers.*.mlp.gate_up_fused_proj": dist.ColWiseParallel(), |
| 86 | + f"{prefix}llama.layers.*.mlp.down_proj": dist.RowWiseParallel(), |
| 87 | + f"{prefix}llama.layers.*.mlp": dist.SequenceParallelDisable(need_transpose=False), |
| 88 | + f"{prefix}lm_head.weight": dist.ColWiseParallel(), |
| 89 | + f"{prefix}lm_head": dist.SequenceParallelEnd(), |
| 90 | + } |
| 91 | + }, |
| 92 | + "mp_config": { |
| 93 | + "parallelize_plan": { |
| 94 | + f"{prefix}llama.embed_tokens": dist.ColWiseParallel(gather_output=True), |
| 95 | + f"{prefix}llama.reshard_row": PrepareLayerInput(layer_input_parallel_row_hook), |
| 96 | + f"{prefix}llama.reshard_row_and_col": PrepareLayerInput(layer_input_parallel_row_and_col_hook), |
| 97 | + f"{prefix}llama.global_layer.reshard_replicate": PrepareLayerInput(layer_input_replicate_hook), |
| 98 | + f"{prefix}llama.layers.*.self_attn.qkv_proj": dist.ColWiseParallel(), |
| 99 | + f"{prefix}llama.layers.*.self_attn.q_proj": dist.ColWiseParallel(), |
| 100 | + f"{prefix}llama.layers.*.self_attn.k_proj": dist.ColWiseParallel(), |
| 101 | + f"{prefix}llama.layers.*.self_attn.v_proj": dist.ColWiseParallel(), |
| 102 | + f"{prefix}llama.layers.*.self_attn.o_proj": dist.RowWiseParallel(), |
| 103 | + f"{prefix}llama.layers.*.mlp.gate_proj": dist.ColWiseParallel(), |
| 104 | + f"{prefix}llama.layers.*.mlp.up_proj": dist.ColWiseParallel(), |
| 105 | + f"{prefix}llama.layers.*.mlp.gate_up_fused_proj": dist.ColWiseParallel(), |
| 106 | + f"{prefix}llama.layers.*.mlp.down_proj": dist.RowWiseParallel(), |
| 107 | + f"{prefix}lm_head.weight": dist.ColWiseParallel(), |
| 108 | + } |
| 109 | + }, |
| 110 | + "pp_config": {"split_spec": f"{prefix}llama.layers", "global_spec": f"{prefix}llama.global_layer"}, |
| 111 | + } |
| 112 | + |
| 113 | + return config |
0 commit comments