compare_op_xpu.cc 5.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */
#ifdef PADDLE_WITH_XPU

F
From00 已提交
15
#include "paddle/fluid/framework/op_registry.h"
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#include "paddle/fluid/framework/op_version_registry.h"

namespace paddle {
namespace operators {

template <typename T, typename XPUType>
void XPUCompare(
    const framework::ExecutionContext& ctx,
    std::function<int(xpu::Context*, const XPUType*, const XPUType*, bool*,
                      const std::vector<int>&, const std::vector<int>&)>
        func) {
  auto* x = ctx.Input<framework::Tensor>("X");
  auto* y = ctx.Input<framework::Tensor>("Y");
  auto* z = ctx.Output<framework::Tensor>("Out");

31 32
  auto x_shape = phi::vectorize<int>(x->dims());
  auto y_shape = phi::vectorize<int>(y->dims());
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145

  auto x_data = reinterpret_cast<const XPUType*>(x->data<T>());
  auto y_data = reinterpret_cast<const XPUType*>(y->data<T>());
  auto z_data = z->mutable_data<bool>(ctx.GetPlace());

  auto& dev_ctx =
      ctx.template device_context<paddle::platform::XPUDeviceContext>();

  int ret = func(dev_ctx.x_context(), x_data, y_data, z_data, x_shape, y_shape);
  PADDLE_ENFORCE_EQ(
      ret, xpu::SUCCESS,
      platform::errors::External(
          "XPU kernel compare op occur error[%d %s] in XPUCompare.", ret,
          XPUAPIErrorMsg[ret]));
}

template <typename DeviceContext, typename T>
class EqualXPUKernel : public framework::OpKernel<T> {
  using XPUType = typename XPUTypeTrait<T>::Type;

 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    XPUCompare<T, XPUType>(ctx, xpu::broadcast_equal<XPUType>);
  }
};

template <typename DeviceContext, typename T>
class NotEqualXPUKernel : public framework::OpKernel<T> {
  using XPUType = typename XPUTypeTrait<T>::Type;

 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    XPUCompare<T, XPUType>(ctx, xpu::broadcast_not_equal<XPUType>);
  }
};

template <typename DeviceContext, typename T>
class LessThanXPUKernel : public framework::OpKernel<T> {
  using XPUType = typename XPUTypeTrait<T>::Type;

 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    XPUCompare<T, XPUType>(ctx, xpu::broadcast_less_than<XPUType>);
  }
};

template <typename DeviceContext, typename T>
class LessEqualXPUKernel : public framework::OpKernel<T> {
  using XPUType = typename XPUTypeTrait<T>::Type;

 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    XPUCompare<T, XPUType>(ctx, xpu::broadcast_less_equal<XPUType>);
  }
};

template <typename DeviceContext, typename T>
class GreaterThanXPUKernel : public framework::OpKernel<T> {
  using XPUType = typename XPUTypeTrait<T>::Type;

 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    XPUCompare<T, XPUType>(ctx, xpu::broadcast_greater_than<XPUType>);
  }
};

template <typename DeviceContext, typename T>
class GreaterEqualXPUKernel : public framework::OpKernel<T> {
  using XPUType = typename XPUTypeTrait<T>::Type;

 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    XPUCompare<T, XPUType>(ctx, xpu::broadcast_greater_equal<XPUType>);
  }
};

}  // namespace operators
}  // namespace paddle

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

REGISTER_OP_XPU_KERNEL(equal,
                       ops::EqualXPUKernel<plat::XPUDeviceContext, float>,
                       ops::EqualXPUKernel<plat::XPUDeviceContext, int>,
                       ops::EqualXPUKernel<plat::XPUDeviceContext, int64_t>);

REGISTER_OP_XPU_KERNEL(not_equal,
                       ops::NotEqualXPUKernel<plat::XPUDeviceContext, float>,
                       ops::NotEqualXPUKernel<plat::XPUDeviceContext, int>,
                       ops::NotEqualXPUKernel<plat::XPUDeviceContext, int64_t>);

REGISTER_OP_XPU_KERNEL(less_than,
                       ops::LessThanXPUKernel<plat::XPUDeviceContext, float>,
                       ops::LessThanXPUKernel<plat::XPUDeviceContext, int>,
                       ops::LessThanXPUKernel<plat::XPUDeviceContext, int64_t>);

REGISTER_OP_XPU_KERNEL(
    less_equal, ops::LessEqualXPUKernel<plat::XPUDeviceContext, float>,
    ops::LessEqualXPUKernel<plat::XPUDeviceContext, int>,
    ops::LessEqualXPUKernel<plat::XPUDeviceContext, int64_t>);

REGISTER_OP_XPU_KERNEL(
    greater_than, ops::GreaterThanXPUKernel<plat::XPUDeviceContext, float>,
    ops::GreaterThanXPUKernel<plat::XPUDeviceContext, int>,
    ops::GreaterThanXPUKernel<plat::XPUDeviceContext, int64_t>);

REGISTER_OP_XPU_KERNEL(
    greater_equal, ops::GreaterEqualXPUKernel<plat::XPUDeviceContext, float>,
    ops::GreaterEqualXPUKernel<plat::XPUDeviceContext, int>,
    ops::GreaterEqualXPUKernel<plat::XPUDeviceContext, int64_t>);

#endif