|
| 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 | +#include "paddle/phi/infermeta/spmd_rules/take_along_axis.h" |
| 16 | + |
| 17 | +#include "glog/logging.h" |
| 18 | + |
| 19 | +#include "paddle/phi/infermeta/spmd_rules/spmd_rule_macro_define.h" |
| 20 | +#include "paddle/phi/infermeta/spmd_rules/utils.h" |
| 21 | + |
| 22 | +namespace phi::distributed { |
| 23 | +SpmdInfo TakeAlongAxisInferSpmd(const DistMetaTensor& x, |
| 24 | + const DistMetaTensor& index, |
| 25 | + int axis) { |
| 26 | + /* |
| 27 | + gather computation formula: |
| 28 | +
|
| 29 | + out[i][j][k] = x[index[i][j][k]][j][k] # if dim == 0 |
| 30 | + out[i][j][k] = x[i][index[i][j][k]][k] # if dim == 1 |
| 31 | + out[i][j][k] = x[i][j][index[i][j][k]] # if dim == 2 |
| 32 | + */ |
| 33 | + |
| 34 | + // Deduced spmd rule: |
| 35 | + // x: cannot be sharded on `axis` dim; |
| 36 | + // index: the `axis` dim could be either sharded or not, other dimension |
| 37 | + // should be the same as x; |
| 38 | + // out: same as index; |
| 39 | + // For non-`axis` dim, if the sizes of this dim in x and index are not |
| 40 | + // the same, this dim should not be sharded. |
| 41 | + |
| 42 | + EXTRACT_SHAPE_AND_DIST_ATTR(x); |
| 43 | + EXTRACT_SHAPE_AND_DIST_ATTR(index); |
| 44 | + PADDLE_ENFORCE_EQ(x_ndim, |
| 45 | + index_ndim, |
| 46 | + common::errors::InvalidArgument( |
| 47 | + "x and index must have the same number of dimensions " |
| 48 | + "but received x_ndim [%d], index_ndim [%d]", |
| 49 | + x_ndim, |
| 50 | + index_ndim)); |
| 51 | + |
| 52 | + // Step1: Build Einsum Notation |
| 53 | + // e.g. axis=1, x: a1c, index: abc, out: abc |
| 54 | + std::string alphabet = "abcdefghijklmnopqrstuvwxyz"; |
| 55 | + std::string index_axes = GetBroadcastAxes(index_ndim, index_ndim, alphabet); |
| 56 | + std::string x_axes = index_axes; |
| 57 | + x_axes.replace(axis, 1, "1"); |
| 58 | + for (int i = 0; i < index_ndim; ++i) { |
| 59 | + if (i != axis && x_shape[i] != index_shape[i]) { |
| 60 | + x_axes.replace(i, 1, "1"); |
| 61 | + index_axes.replace(i, 1, "1"); |
| 62 | + } |
| 63 | + } |
| 64 | + std::string out_axes = index_axes; |
| 65 | + |
| 66 | + // Step2: Sharding Propagation |
| 67 | + // Step2.1: Merge input shardings |
| 68 | + std::vector<int64_t> x_dims_mapping(x_dims_mapping_src); |
| 69 | + x_dims_mapping[axis] = -1; |
| 70 | + std::vector<int64_t> index_dims_mapping(index_dims_mapping_src); |
| 71 | + std::unordered_map<std::string, int64_t> axis_to_dim_map = |
| 72 | + ShardingMergeForTensors( |
| 73 | + {{x_axes, x_dims_mapping}, {index_axes, index_dims_mapping}}); |
| 74 | + |
| 75 | + // Step2.2: Infer output dims mapping |
| 76 | + TensorDistAttr x_dist_attr_dst = CopyTensorDistAttrForOutput(x_dist_attr_src); |
| 77 | + x_dist_attr_dst.set_dims_mapping( |
| 78 | + GetDimsMappingForAxes(x_axes, axis_to_dim_map)); |
| 79 | + |
| 80 | + TensorDistAttr index_dist_attr_dst = |
| 81 | + CopyTensorDistAttrForOutput(index_dist_attr_src); |
| 82 | + index_dist_attr_dst.set_dims_mapping( |
| 83 | + GetDimsMappingForAxes(index_axes, axis_to_dim_map)); |
| 84 | + |
| 85 | + TensorDistAttr out_dist_attr = |
| 86 | + CopyTensorDistAttrForOutput(index_dist_attr_src); |
| 87 | + out_dist_attr.set_dims_mapping( |
| 88 | + GetDimsMappingForAxes(out_axes, axis_to_dim_map)); |
| 89 | + |
| 90 | + VLOG(4) << "x_axes: " << x_axes << " index_axes: " << index_axes |
| 91 | + << " out_axes: " << out_axes; |
| 92 | + LOG_SPMD_INPUT(x); |
| 93 | + LOG_SPMD_INPUT(index); |
| 94 | + VLOG(4) << "out"; |
| 95 | + VLOG(4) << "dist_attr: [" << out_dist_attr.to_string() << "]"; |
| 96 | + return {{x_dist_attr_dst, index_dist_attr_dst}, {out_dist_attr}}; |
| 97 | +} |
| 98 | + |
| 99 | +SpmdInfo TakeAlongAxisGradInferSpmd(const DistMetaTensor& x, |
| 100 | + const DistMetaTensor& index, |
| 101 | + const DistMetaTensor& out_grad, |
| 102 | + int axis) { |
| 103 | + EXTRACT_SHAPE_AND_DIST_ATTR(x); |
| 104 | + EXTRACT_SHAPE_AND_DIST_ATTR(index); |
| 105 | + EXTRACT_SHAPE_AND_DIST_ATTR(out_grad); |
| 106 | + |
| 107 | + // Step1: Build Einsum Notation |
| 108 | + // e.g. axis=1, out_grad: abc -> x: a1c, index: abc, x_grad: a1c |
| 109 | + std::string alphabet = "abcdefghijklmnopqrstuvwxyz"; |
| 110 | + std::string out_grad_axes = |
| 111 | + GetBroadcastAxes(out_grad_ndim, out_grad_ndim, alphabet); |
| 112 | + std::string index_axes = out_grad_axes; |
| 113 | + std::string x_axes = index_axes; |
| 114 | + x_axes.replace(axis, 1, "1"); |
| 115 | + for (int i = 0; i < index_ndim; ++i) { |
| 116 | + if (i != axis && x_shape[i] != index_shape[i]) { |
| 117 | + x_axes.replace(i, 1, "1"); |
| 118 | + index_axes.replace(i, 1, "1"); |
| 119 | + out_grad_axes.replace(i, 1, "1"); |
| 120 | + } |
| 121 | + } |
| 122 | + std::string x_grad_axes = x_axes; |
| 123 | + |
| 124 | + // Step2: Sharding Propagation |
| 125 | + // Step2.1: Merge input shardings |
| 126 | + std::vector<int64_t> out_grad_dims_mapping(out_grad_dims_mapping_src); |
| 127 | + std::unordered_map<std::string, int64_t> axis_to_dim_map = |
| 128 | + ShardingMergeForTensors({{out_grad_axes, out_grad_dims_mapping}}); |
| 129 | + |
| 130 | + // step2.2: Infer input dims mapping from merged input dims mapping |
| 131 | + std::vector<int64_t> index_dims_mapping = |
| 132 | + GetDimsMappingForAxes(index_axes, axis_to_dim_map); |
| 133 | + auto index_dist_attr_dst = CopyTensorDistAttrForOutput(index_dist_attr_src); |
| 134 | + index_dist_attr_dst.set_dims_mapping(index_dims_mapping); |
| 135 | + |
| 136 | + auto out_grad_dist_attr_dst = |
| 137 | + CopyTensorDistAttrForOutput(out_grad_dist_attr_src); |
| 138 | + out_grad_dist_attr_dst.set_dims_mapping(index_dims_mapping); |
| 139 | + |
| 140 | + auto x_dist_attr_dst = CopyTensorDistAttrForOutput(x_dist_attr_src); |
| 141 | + x_dist_attr_dst.set_dims_mapping( |
| 142 | + GetDimsMappingForAxes(x_axes, axis_to_dim_map)); |
| 143 | + |
| 144 | + auto x_grad_dist_attr_dst = CopyTensorDistAttrForOutput(x_dist_attr_src); |
| 145 | + x_grad_dist_attr_dst.set_dims_mapping( |
| 146 | + GetDimsMappingForAxes(x_grad_axes, axis_to_dim_map)); |
| 147 | + |
| 148 | + VLOG(4) << "out_grad"; |
| 149 | + VLOG(4) << "dist_attr: [" << out_grad_dist_attr_dst.to_string() << "]"; |
| 150 | + VLOG(4) << "index"; |
| 151 | + VLOG(4) << "dist_attr: [" << index_dist_attr_dst.to_string() << "]"; |
| 152 | + VLOG(4) << "x"; |
| 153 | + VLOG(4) << "dist_attr: [" << x_dist_attr_dst.to_string() << "]"; |
| 154 | + VLOG(4) << "x_grad"; |
| 155 | + VLOG(4) << "dist_attr: [" << x_grad_dist_attr_dst.to_string() << "]"; |
| 156 | + |
| 157 | + return {{x_dist_attr_dst, index_dist_attr_dst, out_grad_dist_attr_dst}, |
| 158 | + {x_grad_dist_attr_dst}}; |
| 159 | +} |
| 160 | +} // namespace phi::distributed |
0 commit comments