|
| 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 paddle |
| 16 | + |
| 17 | +from .base_reshard_func import ReshardFunction |
| 18 | + |
| 19 | + |
| 20 | +class GlobaleToSubMeshFunction(ReshardFunction): |
| 21 | + def is_suitable(self, src_dist_attr, dst_dist_attr): |
| 22 | + if 0 in src_dist_attr.dims_mapping or 0 in src_dist_attr.partial_status: |
| 23 | + return False |
| 24 | + in_mesh = src_dist_attr.process_mesh |
| 25 | + out_mesh = dst_dist_attr.process_mesh |
| 26 | + if in_mesh.ndim != out_mesh.ndim + 1: |
| 27 | + return False |
| 28 | + sub_meshes = paddle.base.libpaddle.pir.get_sub_meshes(in_mesh) |
| 29 | + return out_mesh in sub_meshes |
| 30 | + |
| 31 | + def reshard(self, src_dist_attr, dst_dist_attr, src_value, dst_type): |
| 32 | + if src_value.has_one_use(): |
| 33 | + src_value.update_dist_attr(dst_dist_attr) |
| 34 | + prev_op = src_value.get_defining_op() |
| 35 | + op_dist_attr = prev_op.dist_attr |
| 36 | + op_mesh = op_dist_attr.process_mesh |
| 37 | + operands = op_dist_attr.operands() |
| 38 | + results = op_dist_attr.results() |
| 39 | + results[src_value.index()] = dst_dist_attr |
| 40 | + prev_op.dist_attr = ( |
| 41 | + paddle.base.libpaddle.pir.create_op_dist_attribute( |
| 42 | + op_mesh, operands, results |
| 43 | + ) |
| 44 | + ) |
| 45 | + return src_value |
| 46 | + else: |
| 47 | + dst_value = paddle._C_ops.share_data_(src_value) |
| 48 | + share_data_op = dst_value.get_defining_op() |
| 49 | + # set dist type and dist attr |
| 50 | + dst_value.set_type(dst_type) |
| 51 | + share_data_op.dist_attr = ( |
| 52 | + paddle.base.libpaddle.pir.create_op_dist_attribute( |
| 53 | + dst_dist_attr.process_mesh, [src_dist_attr], [dst_dist_attr] |
| 54 | + ) |
| 55 | + ) |
| 56 | + return dst_value |
0 commit comments