/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #include "paddle/fluid/operators/index_select_op.h" #include "paddle/fluid/operators/npu_op_runner.h" namespace paddle { namespace operators { template class IndexSelectNPUKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { auto* x = ctx.Input("X"); auto* index = ctx.Input("Index"); auto dim = ctx.Attr("dim"); auto* out = ctx.Output("Out"); out->mutable_data(ctx.GetPlace()); auto stream = ctx.template device_context() .stream(); NpuOpRunner runner; runner.SetType("GatherV2") .AddInput(*x) .AddInput(*index) .AddInput(std::vector{dim}) .AddOutput(*out); runner.Run(stream); } }; template class IndexSelectGradNPUKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { auto* x_grad = ctx.Output(framework::GradVarName("X")); auto* index = ctx.Input("Index"); auto* out_grad = ctx.Input(framework::GradVarName("Out")); auto stream = ctx.template device_context() .stream(); auto x_dims = x_grad->dims(); auto out_dims = out_grad->dims(); int dim = ctx.Attr("dim"); if (dim < 0) { dim += out_dims.size(); } Tensor casted_index; if (index->type() != framework::proto::VarType::INT32) { casted_index.mutable_data(index->dims(), ctx.GetPlace()); const auto& cast_runner = NpuOpRunner("Cast", {*index}, {casted_index}, {{"dst_type", ACL_INT32}}); cast_runner.Run(stream); } else { casted_index.ShareDataWith(*index); } if (dim == 0) { x_grad->mutable_data(ctx.GetPlace()); const auto& zeros_runner = NpuOpRunner("ZerosLike", {*x_grad}, {*x_grad}); zeros_runner.Run(stream); NpuOpRunner runner; runner.SetType("UnsortedSegmentSum") .AddInput(*out_grad) .AddInput(casted_index) .AddInput(std::vector{x_dims[dim]}) .AddOutput(*x_grad); runner.Run(stream); } else { Tensor transed_out_grad; std::vector in_trans_perm; in_trans_perm.push_back(dim); for (int i = 0; i < out_dims.size(); ++i) { if (i == dim) continue; in_trans_perm.push_back(i); } framework::DDim transed_out_dims(out_dims); for (size_t i = 0; i < in_trans_perm.size(); ++i) { transed_out_dims[i] = out_dims[in_trans_perm[i]]; } transed_out_grad.mutable_data(transed_out_dims, ctx.GetPlace()); framework::NPUAttributeMap in_trans_attr = {{"perm", in_trans_perm}}; const auto& in_trans_runner = NpuOpRunner( "TransposeD", {*out_grad}, {transed_out_grad}, in_trans_attr); in_trans_runner.Run(stream); Tensor sum_out; framework::DDim sum_dims(x_dims); sum_dims[0] = x_dims[dim]; auto idx = 1; for (int i = 0; i < x_dims.size(); ++i) { if (i == dim) continue; sum_dims[idx++] = x_dims[i]; } sum_out.mutable_data(sum_dims, ctx.GetPlace()); const auto& zeros_runner = NpuOpRunner("ZerosLike", {sum_out}, {sum_out}); zeros_runner.Run(stream); NpuOpRunner runner; runner.SetType("UnsortedSegmentSum") .AddInput(transed_out_grad) .AddInput(casted_index) .AddInput(std::vector{x_dims[dim]}) .AddOutput(sum_out); runner.Run(stream); std::vector out_trans_perm; for (int i = 1; i < 1 + dim; ++i) { out_trans_perm.push_back(i); } out_trans_perm.push_back(0); for (int i = 1 + dim; i < x_dims.size(); ++i) { out_trans_perm.push_back(i); } framework::NPUAttributeMap out_trans_attr = {{"perm", out_trans_perm}}; x_grad->mutable_data(ctx.GetPlace()); const auto& out_trans_runner = NpuOpRunner("TransposeD", {sum_out}, {*x_grad}, out_trans_attr); out_trans_runner.Run(stream); } } }; } // namespace operators } // namespace paddle namespace ops = paddle::operators; REGISTER_OP_NPU_KERNEL( index_select, ops::IndexSelectNPUKernel, ops::IndexSelectNPUKernel, ops::IndexSelectNPUKernel); REGISTER_OP_NPU_KERNEL( index_select_grad, ops::IndexSelectGradNPUKernel, ops::IndexSelectGradNPUKernel, ops::IndexSelectGradNPUKernel);