|
| 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/topk.h" |
| 16 | +#include "glog/logging.h" |
| 17 | +#include "paddle/phi/infermeta/spmd_rules/spmd_rule_macro_define.h" |
| 18 | +#include "paddle/phi/infermeta/spmd_rules/utils.h" |
| 19 | + |
| 20 | +namespace phi { |
| 21 | +namespace distributed { |
| 22 | + |
| 23 | +SpmdInfo TopkInferSpmd( |
| 24 | + const DistMetaTensor& x, int k, int axis, bool largest, bool sorted) { |
| 25 | + // Verify input args |
| 26 | + EXTRACT_SHAPE_AND_DIST_ATTR(x); |
| 27 | + axis = axis < 0 ? x_ndim + axis : axis; |
| 28 | + PADDLE_ENFORCE_EQ( |
| 29 | + 0 <= axis && axis < x_ndim, |
| 30 | + true, |
| 31 | + phi::errors::InvalidArgument( |
| 32 | + "The axis of topk should be in range [0, %d), but got %d.", |
| 33 | + x_ndim, |
| 34 | + axis)); |
| 35 | + |
| 36 | + // Create destination dist attrs |
| 37 | + TensorDistAttr x_dist_attr_dst = CopyTensorDistAttrForOutput(x_dist_attr_src); |
| 38 | + TensorDistAttr out_dist_attr_dst = |
| 39 | + CopyTensorDistAttrForOutput(x_dist_attr_src); |
| 40 | + TensorDistAttr indices_dist_attr_dst = |
| 41 | + CopyTensorDistAttrForOutput(x_dist_attr_src); |
| 42 | + |
| 43 | + // Infer dims_mapping |
| 44 | + std::vector<int64_t> x_dims_mapping_dst = x_dims_mapping_src; |
| 45 | + x_dims_mapping_dst[axis] = -1; |
| 46 | + std::vector<int64_t> out_dims_mapping_dst = x_dims_mapping_dst; |
| 47 | + std::vector<int64_t> indices_dims_mapping_dst = x_dims_mapping_dst; |
| 48 | + |
| 49 | + // Set the dims mapping for outputs |
| 50 | + out_dist_attr_dst.set_dims_mapping(out_dims_mapping_dst); |
| 51 | + indices_dist_attr_dst.set_dims_mapping(indices_dims_mapping_dst); |
| 52 | + |
| 53 | + // Update the dims mapping for inputs |
| 54 | + x_dist_attr_dst.set_dims_mapping(x_dims_mapping_dst); |
| 55 | + VLOG(4) << "TopkInferSpmd: Done."; |
| 56 | + LOG_SPMD_INPUT(x); |
| 57 | + LOG_SPMD_OUTPUT(out_dist_attr_dst); |
| 58 | + LOG_SPMD_OUTPUT(indices_dist_attr_dst); |
| 59 | + |
| 60 | + return {{x_dist_attr_dst}, {out_dist_attr_dst, indices_dist_attr_dst}}; |
| 61 | +} |
| 62 | + |
| 63 | +SpmdInfo TopkGradInferSpmd(const DistMetaTensor& x, |
| 64 | + const DistMetaTensor& indices, |
| 65 | + const DistMetaTensor& out_grad, |
| 66 | + int k, |
| 67 | + int axis, |
| 68 | + bool largest, |
| 69 | + bool sorted) { |
| 70 | + // Verify input args |
| 71 | + EXTRACT_SHAPE_AND_DIST_ATTR(x); |
| 72 | + EXTRACT_SHAPE_AND_DIST_ATTR(indices); |
| 73 | + EXTRACT_SHAPE_AND_DIST_ATTR(out_grad); |
| 74 | + PADDLE_ENFORCE_EQ(indices_ndim, |
| 75 | + out_grad_ndim, |
| 76 | + common::errors::InvalidArgument( |
| 77 | + "TopKGrad: The rank of Indices [%d] and OutGrad [%d] " |
| 78 | + "must be the same.", |
| 79 | + indices_ndim, |
| 80 | + out_grad_ndim)); |
| 81 | + PADDLE_ENFORCE_EQ(x_ndim, |
| 82 | + indices_ndim, |
| 83 | + common::errors::InvalidArgument( |
| 84 | + "TopKGrad: The rank of Input [%d] and Indices [%d] " |
| 85 | + "must be the same.", |
| 86 | + x_ndim, |
| 87 | + indices_ndim)); |
| 88 | + axis = axis < 0 ? x_ndim + axis : axis; |
| 89 | + PADDLE_ENFORCE_EQ( |
| 90 | + 0 <= axis && axis < x_ndim, |
| 91 | + true, |
| 92 | + phi::errors::InvalidArgument( |
| 93 | + "The axis of topk_grad should be in range [0, %d), but got %d.", |
| 94 | + x_ndim, |
| 95 | + axis)); |
| 96 | + // Build einsum notation |
| 97 | + std::string alphabet = "abcdefghijlopqrstuvwxyz"; |
| 98 | + std::string x_axes = alphabet.substr(0, x_ndim - 1); |
| 99 | + std::string indices_axes = x_axes; |
| 100 | + std::string out_grad_axes = x_axes; |
| 101 | + |
| 102 | + // Merge sharding |
| 103 | + std::pair<std::string, std::vector<int64_t>> indices_pair( |
| 104 | + indices_axes, indices_dims_mapping_src); |
| 105 | + std::pair<std::string, std::vector<int64_t>> out_grad_pair( |
| 106 | + out_grad_axes, out_grad_dims_mapping_src); |
| 107 | + std::pair<std::string, std::vector<int64_t>> x_pair(x_axes, |
| 108 | + x_dims_mapping_src); |
| 109 | + auto axis_to_dim_map = |
| 110 | + ShardingMergeForTensors({x_pair, indices_pair, out_grad_pair}); |
| 111 | + |
| 112 | + // Infer dims mapping |
| 113 | + std::vector<int64_t> x_grad_dims_mapping_dst = |
| 114 | + GetDimsMappingForAxes(x_axes, axis_to_dim_map); |
| 115 | + x_grad_dims_mapping_dst.insert(x_grad_dims_mapping_dst.begin() + axis, -1); |
| 116 | + std::vector<int64_t> x_dims_mapping_dst = x_grad_dims_mapping_dst; |
| 117 | + std::vector<int64_t> indices_dims_mapping_dst = x_grad_dims_mapping_dst; |
| 118 | + std::vector<int64_t> out_grad_dims_mapping_dst = x_grad_dims_mapping_dst; |
| 119 | + |
| 120 | + // Set the dims mapping |
| 121 | + TensorDistAttr x_grad_dist_attr_dst = |
| 122 | + CopyTensorDistAttrForOutput(out_grad_dist_attr_src); |
| 123 | + TensorDistAttr x_dist_attr_dst = |
| 124 | + CopyTensorDistAttrForOutput(out_grad_dist_attr_src); |
| 125 | + TensorDistAttr indices_dist_attr_dst = |
| 126 | + CopyTensorDistAttrForOutput(out_grad_dist_attr_src); |
| 127 | + TensorDistAttr out_grad_dist_attr_dst = |
| 128 | + CopyTensorDistAttrForOutput(out_grad_dist_attr_src); |
| 129 | + |
| 130 | + x_grad_dist_attr_dst.set_dims_mapping(x_grad_dims_mapping_dst); |
| 131 | + x_dist_attr_dst.set_dims_mapping(x_dims_mapping_dst); |
| 132 | + indices_dist_attr_dst.set_dims_mapping(indices_dims_mapping_dst); |
| 133 | + out_grad_dist_attr_dst.set_dims_mapping(out_grad_dims_mapping_dst); |
| 134 | + |
| 135 | + VLOG(4) << "TopkGradInferSpmd: Done."; |
| 136 | + LOG_SPMD_INPUT(x); |
| 137 | + LOG_SPMD_INPUT(indices); |
| 138 | + LOG_SPMD_INPUT(out_grad); |
| 139 | + LOG_SPMD_OUTPUT(x_grad_dist_attr_dst); |
| 140 | + |
| 141 | + return {{x_dist_attr_dst, indices_dist_attr_dst, out_grad_dist_attr_dst}, |
| 142 | + {x_grad_dist_attr_dst}}; |
| 143 | +} |
| 144 | +SpmdInfo TopkInferSpmdDynamic(const DistMetaTensor& x, |
| 145 | + const Scalar& k, |
| 146 | + int axis, |
| 147 | + bool largest, |
| 148 | + bool sorted) { |
| 149 | + return TopkInferSpmd(x, k.to<int>(), axis, largest, sorted); |
| 150 | +} |
| 151 | + |
| 152 | +SpmdInfo TopkGradInferSpmdDynamic(const DistMetaTensor& x, |
| 153 | + const DistMetaTensor& indices, |
| 154 | + const DistMetaTensor& out_grad, |
| 155 | + const Scalar& k, |
| 156 | + int axis, |
| 157 | + bool largest, |
| 158 | + bool sorted) { |
| 159 | + return TopkGradInferSpmd( |
| 160 | + x, indices, out_grad, k.to<int>(), axis, largest, sorted); |
| 161 | +} |
| 162 | + |
| 163 | +} // namespace distributed |
| 164 | +} // namespace phi |
0 commit comments