tril_triu_op_npu.cc 3.2 KB
Newer Older
F
furnace 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/* 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
13
limitations under the License. */
F
furnace 已提交
14

15
#include "paddle/fluid/framework/op_registry.h"
16
#include "paddle/fluid/platform/device/npu/npu_op_runner.h"
F
furnace 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

namespace paddle {
namespace operators {

template <typename DeviceContext, typename T>
class TrilTriuNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* x = ctx.Input<Tensor>("X");
    auto* out = ctx.Output<Tensor>("Out");
    int diagonal = ctx.Attr<int>("diagonal");
    bool lower = ctx.Attr<bool>("lower");

    out->mutable_data<T>(ctx.GetPlace());

    std::string op_type = lower ? "Tril" : "Triu";

    framework::NPUAttributeMap attr_input = {{"diagonal", diagonal}};

F
furnace 已提交
36 37
    const auto& dev_ctx =
        ctx.template device_context<paddle::platform::NPUDeviceContext>();
F
furnace 已提交
38

F
furnace 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    auto op_func_tril = [](const std::vector<Tensor>& inputs,
                           const std::vector<Tensor>& outputs,
                           const NPUAttributeMap& attrs,
                           const platform::NPUDeviceContext& dev_ctx) {
      const auto& runner = NpuOpRunner("Tril", inputs, outputs, attrs);
      runner.Run(dev_ctx.stream());
    };

    auto op_func_triu = [](const std::vector<Tensor>& inputs,
                           const std::vector<Tensor>& outputs,
                           const NPUAttributeMap& attrs,
                           const platform::NPUDeviceContext& dev_ctx) {
      const auto& runner = NpuOpRunner("Triu", inputs, outputs, attrs);
      runner.Run(dev_ctx.stream());
    };

55 56
    if (framework::TransToProtoVarType(x->dtype()) ==
        framework::proto::VarType::BOOL) {
F
furnace 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
      if (lower) {
        NpuOpRunner::TypeAdapter({*x}, {*out}, attr_input, dev_ctx,
                                 op_func_tril,
                                 {framework::proto::VarType::UINT8},
                                 {framework::proto::VarType::UINT8});
      } else {
        NpuOpRunner::TypeAdapter({*x}, {*out}, attr_input, dev_ctx,
                                 op_func_triu,
                                 {framework::proto::VarType::UINT8},
                                 {framework::proto::VarType::UINT8});
      }
    } else {
      const auto& runner = NpuOpRunner(op_type, {*x}, {*out}, attr_input);
      runner.Run(dev_ctx.stream());
    }
F
furnace 已提交
72 73 74 75 76 77 78 79 80 81
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
namespace plat = paddle::platform;
REGISTER_OP_NPU_KERNEL(
    tril_triu, ops::TrilTriuNPUKernel<plat::NPUDeviceContext, float>,
F
furnace 已提交
82 83
    ops::TrilTriuNPUKernel<plat::NPUDeviceContext, int>,
    ops::TrilTriuNPUKernel<plat::NPUDeviceContext, bool>,
F
furnace 已提交
84
    ops::TrilTriuNPUKernel<plat::NPUDeviceContext, plat::float16>);