top_k_v2_op_npu.cc 3.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* 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 <string>
#include <vector>
17

18
#include "paddle/fluid/framework/op_registry.h"
19 20 21 22 23 24 25 26 27

namespace paddle {
namespace operators {
// NOTE(Ruibiao): the Ascend TopKV2 operator used in this kernel
// may lead to large accuracy error for float32 data
template <typename T>
class TopkV2NPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
28 29 30 31
    auto* input = context.Input<phi::DenseTensor>("X");
    auto* k_tensor = context.Input<phi::DenseTensor>("K");
    auto* out = context.Output<phi::DenseTensor>("Out");
    auto* indices = context.Output<phi::DenseTensor>("Indices");  // type: INT64
32 33 34 35 36 37 38 39 40 41 42 43

    int32_t k = static_cast<int32_t>(context.Attr<int>("k"));
    int axis = static_cast<int>(context.Attr<int>("axis"));
    const bool sorted = static_cast<bool>(context.Attr<bool>("sorted"));
    const bool largest = static_cast<bool>(context.Attr<bool>("largest"));

    if (axis < 0) {
      axis += input->dims().size();
    }

    if (k_tensor != nullptr) {
      std::vector<int> v_tmp(1);
44
      paddle::framework::TensorToVector(
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
          *k_tensor,
          context.template device_context<paddle::platform::NPUDeviceContext>(),
          &v_tmp);
      k = static_cast<int32_t>(v_tmp[0]);
    }

    framework::DDim output_dims = input->dims();
    output_dims[axis] = k;

    out->Resize(output_dims);
    indices->Resize(output_dims);

    out->mutable_data<T>(context.GetPlace());
    indices->mutable_data<int64_t>(context.GetPlace());

60
    phi::DenseTensor indices_int32(phi::DataType::INT32);
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    indices_int32.Resize(output_dims);
    indices_int32.mutable_data<int32_t>(context.GetPlace());

    auto npu_stream =
        context.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();

    NpuOpRunner npu_op_runner_topkv2;
    npu_op_runner_topkv2.SetType("TopKV2")
        .AddInput(*input)
        .AddInput(std::vector<int32_t>{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
80 81
    auto dst_dtype =
        ConvertToNpuDtype(framework::TransToProtoVarType(indices->type()));
82
    const auto& npu_op_runner_cast =
83 84 85
        NpuOpRunner("Cast",
                    {indices_int32},
                    {*indices},
86 87 88 89 90 91 92 93
                    {{"dst_type", static_cast<int>(dst_dtype)}});
    npu_op_runner_cast.Run(npu_stream);
  }
};
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
94
namespace plat = paddle::platform;
95 96
REGISTER_OP_NPU_KERNEL(top_k_v2,
                       ops::TopkV2NPUKernel<float>,
97
                       ops::TopkV2NPUKernel<plat::float16>,
98 99 100
                       ops::TopkV2NPUKernel<double>,
                       ops::TopkV2NPUKernel<int32_t>,
                       ops::TopkV2NPUKernel<int64_t>);