/* 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 #include #include "paddle/fluid/framework/op_registry.h" namespace paddle { namespace operators { // NOTE(Ruibiao): the Ascend TopKV2 operator used in this kernel // may lead to large accuracy error for float32 data template class TopkV2NPUKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& context) const override { auto* input = context.Input("X"); auto* k_tensor = context.Input("K"); auto* out = context.Output("Out"); auto* indices = context.Output("Indices"); // type: INT64 int32_t k = static_cast(context.Attr("k")); int axis = static_cast(context.Attr("axis")); const bool sorted = static_cast(context.Attr("sorted")); const bool largest = static_cast(context.Attr("largest")); if (axis < 0) { axis += input->dims().size(); } if (k_tensor != nullptr) { std::vector v_tmp(1); paddle::framework::TensorToVector( *k_tensor, context.template device_context(), &v_tmp); k = static_cast(v_tmp[0]); } framework::DDim output_dims = input->dims(); output_dims[axis] = k; out->Resize(output_dims); indices->Resize(output_dims); out->mutable_data(context.GetPlace()); indices->mutable_data(context.GetPlace()); phi::DenseTensor indices_int32(phi::DataType::INT32); indices_int32.Resize(output_dims); indices_int32.mutable_data(context.GetPlace()); auto npu_stream = context.template device_context() .stream(); NpuOpRunner npu_op_runner_topkv2; npu_op_runner_topkv2.SetType("TopKV2") .AddInput(*input) .AddInput(std::vector{k}) .AddOutput(*out) .AddOutput(indices_int32) .AddAttr("sorted", sorted) .AddAttr("dim", axis) .AddAttr("largest", largest) .Run(npu_stream); // Cast 'indices_int32' to 'indices', from INT32 to INT64 auto dst_dtype = ConvertToNpuDtype(framework::TransToProtoVarType(indices->type())); const auto& npu_op_runner_cast = NpuOpRunner("Cast", {indices_int32}, {*indices}, {{"dst_type", static_cast(dst_dtype)}}); npu_op_runner_cast.Run(npu_stream); } }; } // namespace operators } // namespace paddle namespace ops = paddle::operators; namespace plat = paddle::platform; REGISTER_OP_NPU_KERNEL(top_k_v2, ops::TopkV2NPUKernel, ops::TopkV2NPUKernel, ops::TopkV2NPUKernel, ops::TopkV2NPUKernel, ops::TopkV2NPUKernel);