compare_op_npu.cc 7.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/* 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. */

F
From00 已提交
14
#include "paddle/fluid/framework/op_registry.h"
B
baoachun 已提交
15
#include "paddle/fluid/framework/op_version_registry.h"
16 17 18 19 20
#include "paddle/fluid/operators/elementwise/elementwise_op_function.h"

namespace paddle {
namespace operators {

B
baoachun 已提交
21
template <typename DeviceContext, typename T>
22 23 24
class EqualNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
25 26 27
    auto* x = ctx.Input<phi::DenseTensor>("X");
    auto* y = ctx.Input<phi::DenseTensor>("Y");
    auto* out = ctx.Output<phi::DenseTensor>("Out");
28 29
    out->mutable_data<bool>(ctx.GetPlace());

L
Leo Chen 已提交
30
    const auto& runner = NpuOpRunner("Equal", {*x, *y}, {*out}, {});
31 32 33 34 35 36 37
    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();
    runner.Run(stream);
  }
};

B
baoachun 已提交
38 39 40 41
template <typename DeviceContext, typename T>
class NotEqualNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
42 43 44
    auto* x = ctx.Input<phi::DenseTensor>("X");
    auto* y = ctx.Input<phi::DenseTensor>("Y");
    auto* out = ctx.Output<phi::DenseTensor>("Out");
B
baoachun 已提交
45 46 47 48 49 50 51 52 53 54
    out->mutable_data<bool>(ctx.GetPlace());

    const auto& runner = NpuOpRunner("NotEqual", {*x, *y}, {*out}, {});
    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();
    runner.Run(stream);
  }
};

55 56 57 58
template <typename DeviceContext, typename T>
class LessThanNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
59 60 61
    auto* x = ctx.Input<phi::DenseTensor>("X");
    auto* y = ctx.Input<phi::DenseTensor>("Y");
    auto* out = ctx.Output<phi::DenseTensor>("Out");
B
baoachun 已提交
62 63 64
    out->mutable_data<bool>(ctx.GetPlace());

    const auto& runner = NpuOpRunner("Less", {*x, *y}, {*out}, {});
65 66 67 68 69 70 71
    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();
    runner.Run(stream);
  }
};

72 73 74 75
template <typename DeviceContext, typename T>
class LessEqualNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
76 77 78
    auto* x = ctx.Input<phi::DenseTensor>("X");
    auto* y = ctx.Input<phi::DenseTensor>("Y");
    auto* out = ctx.Output<phi::DenseTensor>("Out");
B
baoachun 已提交
79 80 81
    out->mutable_data<bool>(ctx.GetPlace());

    const auto& runner = NpuOpRunner("LessEqual", {*x, *y}, {*out}, {});
82 83 84 85 86 87 88 89 90 91 92
    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();
    runner.Run(stream);
  }
};

template <typename DeviceContext, typename T>
class GreaterThanNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
93 94 95
    auto* x = ctx.Input<phi::DenseTensor>("X");
    auto* y = ctx.Input<phi::DenseTensor>("Y");
    auto* out = ctx.Output<phi::DenseTensor>("Out");
96

B
baoachun 已提交
97 98
    out->mutable_data<bool>(ctx.GetPlace());
    const auto& runner = NpuOpRunner("Greater", {*x, *y}, {*out}, {});
99 100 101 102 103 104 105 106 107 108 109
    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();
    runner.Run(stream);
  }
};

template <typename DeviceContext, typename T>
class GreaterEqualNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
110 111 112
    auto* x = ctx.Input<phi::DenseTensor>("X");
    auto* y = ctx.Input<phi::DenseTensor>("Y");
    auto* out = ctx.Output<phi::DenseTensor>("Out");
113

B
baoachun 已提交
114 115
    out->mutable_data<bool>(ctx.GetPlace());
    const auto& runner = NpuOpRunner("GreaterEqual", {*x, *y}, {*out}, {});
116 117 118 119 120 121 122
    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();
    runner.Run(stream);
  }
};

123 124 125 126 127 128
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
namespace plat = paddle::platform;

B
baoachun 已提交
129
REGISTER_OP_NPU_KERNEL(
130 131
    equal,
    ops::EqualNPUKernel<plat::NPUDeviceContext, plat::float16>,
B
baoachun 已提交
132 133 134 135 136 137 138 139 140 141
    ops::EqualNPUKernel<plat::NPUDeviceContext, float>,
    ops::EqualNPUKernel<plat::NPUDeviceContext, double>,
    ops::EqualNPUKernel<plat::NPUDeviceContext, int8_t>,
    ops::EqualNPUKernel<plat::NPUDeviceContext, uint8_t>,
    ops::EqualNPUKernel<plat::NPUDeviceContext, int16_t>,
    ops::EqualNPUKernel<plat::NPUDeviceContext, int>,
    ops::EqualNPUKernel<plat::NPUDeviceContext, int64_t>,
    ops::EqualNPUKernel<plat::NPUDeviceContext, bool>);

REGISTER_OP_NPU_KERNEL(
142 143
    not_equal,
    ops::NotEqualNPUKernel<plat::NPUDeviceContext, plat::float16>,
B
baoachun 已提交
144 145 146 147 148 149 150
    ops::NotEqualNPUKernel<plat::NPUDeviceContext, float>,
    ops::NotEqualNPUKernel<plat::NPUDeviceContext, double>,
    ops::NotEqualNPUKernel<plat::NPUDeviceContext, int8_t>,
    ops::NotEqualNPUKernel<plat::NPUDeviceContext, uint8_t>,
    ops::NotEqualNPUKernel<plat::NPUDeviceContext, int16_t>,
    ops::NotEqualNPUKernel<plat::NPUDeviceContext, int>,
    ops::NotEqualNPUKernel<plat::NPUDeviceContext, int64_t>);
151 152

REGISTER_OP_NPU_KERNEL(
153 154
    less_than,
    ops::LessThanNPUKernel<plat::NPUDeviceContext, plat::float16>,
B
baoachun 已提交
155 156 157 158 159 160 161
    ops::LessThanNPUKernel<plat::NPUDeviceContext, float>,
    ops::LessThanNPUKernel<plat::NPUDeviceContext, double>,
    ops::LessThanNPUKernel<plat::NPUDeviceContext, int8_t>,
    ops::LessThanNPUKernel<plat::NPUDeviceContext, uint8_t>,
    ops::LessThanNPUKernel<plat::NPUDeviceContext, int16_t>,
    ops::LessThanNPUKernel<plat::NPUDeviceContext, int>,
    ops::LessThanNPUKernel<plat::NPUDeviceContext, int64_t>);
162

163
REGISTER_OP_NPU_KERNEL(
164 165
    less_equal,
    ops::LessEqualNPUKernel<plat::NPUDeviceContext, plat::float16>,
B
baoachun 已提交
166 167 168 169 170 171 172
    ops::LessEqualNPUKernel<plat::NPUDeviceContext, float>,
    ops::LessEqualNPUKernel<plat::NPUDeviceContext, double>,
    ops::LessEqualNPUKernel<plat::NPUDeviceContext, int8_t>,
    ops::LessEqualNPUKernel<plat::NPUDeviceContext, uint8_t>,
    ops::LessEqualNPUKernel<plat::NPUDeviceContext, int16_t>,
    ops::LessEqualNPUKernel<plat::NPUDeviceContext, int>,
    ops::LessEqualNPUKernel<plat::NPUDeviceContext, int64_t>);
173 174 175

REGISTER_OP_NPU_KERNEL(
    greater_than,
B
baoachun 已提交
176 177 178 179 180 181 182 183
    ops::GreaterThanNPUKernel<plat::NPUDeviceContext, plat::float16>,
    ops::GreaterThanNPUKernel<plat::NPUDeviceContext, float>,
    ops::GreaterThanNPUKernel<plat::NPUDeviceContext, double>,
    ops::GreaterThanNPUKernel<plat::NPUDeviceContext, int8_t>,
    ops::GreaterThanNPUKernel<plat::NPUDeviceContext, uint8_t>,
    ops::GreaterThanNPUKernel<plat::NPUDeviceContext, int16_t>,
    ops::GreaterThanNPUKernel<plat::NPUDeviceContext, int>,
    ops::GreaterThanNPUKernel<plat::NPUDeviceContext, int64_t>);
184 185 186

REGISTER_OP_NPU_KERNEL(
    greater_equal,
B
baoachun 已提交
187 188 189 190 191 192 193
    ops::GreaterEqualNPUKernel<plat::NPUDeviceContext, plat::float16>,
    ops::GreaterEqualNPUKernel<plat::NPUDeviceContext, float>,
    ops::GreaterEqualNPUKernel<plat::NPUDeviceContext, double>,
    ops::GreaterEqualNPUKernel<plat::NPUDeviceContext, int8_t>,
    ops::GreaterEqualNPUKernel<plat::NPUDeviceContext, uint8_t>,
    ops::GreaterEqualNPUKernel<plat::NPUDeviceContext, int>,
    ops::GreaterEqualNPUKernel<plat::NPUDeviceContext, int64_t>);