compare_compute.cc 10.6 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Copyright (c) 2019 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 "lite/kernels/arm/compare_compute.h"
#include <vector>
#include "lite/api/paddle_place.h"
18
#include "lite/backends/arm/math/funcs.h"
Y
Yan Chunwei 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 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
#include "lite/core/op_registry.h"
#include "lite/core/type_system.h"

namespace paddle {
namespace lite {
namespace kernels {
namespace arm {

#define COMPARE_FUNCTOR(name, op)                                           \
  template <typename T>                                                     \
  struct _##name##Functor {                                                 \
    inline bool operator()(const T &a, const T &b) const { return a op b; } \
  };

COMPARE_FUNCTOR(Equal, ==);
COMPARE_FUNCTOR(NotEqual, !=);
COMPARE_FUNCTOR(LessThan, <);
COMPARE_FUNCTOR(LessEqual, <=);
COMPARE_FUNCTOR(GreaterThan, >);
COMPARE_FUNCTOR(GreaterEqual, >=);

template <>
struct _EqualFunctor<float> {
  inline bool operator()(const float &a, const float &b) const {
    // It is safe to cast a and b to double.
    return fabs(static_cast<double>(a - b)) < 1e-8;
  }
};

template <>
struct _NotEqualFunctor<float> {
  inline bool operator()(const float &a, const float &b) const {
    return !_EqualFunctor<float>()(a, b);
  }
};

inline void get_mid_dims(const lite::DDim &x_dims,
                         const lite::DDim &y_dims,
                         const int axis,
                         int *pre,
                         int *n,
                         int *post) {
  *pre = 1;
  *n = 1;
  *post = 1;
  for (int i = 0; i < axis; ++i) {
    (*pre) *= x_dims[i];
  }

  for (int i = 0; i < y_dims.size(); ++i) {
    (*n) *= y_dims[i];
  }

  for (int i = axis + y_dims.size(); i < x_dims.size(); ++i) {
    (*post) *= x_dims[i];
  }
}

template <template <typename T> class Functor>
void CompareCompute<Functor>::Run() {
  auto &param = this->Param<operators::CompareParam>();

  using CompareFunctor = Functor<float>;

  const size_t x_size = param.X->numel();
  const size_t y_size = param.Y->numel();
  auto x_dims = param.X->dims();
  auto y_dims = param.Y->dims();
  bool *z = param.Out->template mutable_data<bool>();
T
TianXiaogang 已提交
88
  const auto *x = param.X->template data<float>();
Y
Yan Chunwei 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
  const auto *y = param.Y->template data<float>();
  auto axis = param.axis;
  bool force_cpu = param.force_cpu;
  if (x_size == y_size) {
    for (int i = 0; i < x_size; ++i) {
      z[i] = CompareFunctor()(x[i], y[i]);
    }
  } else {
    int axis = (param.axis == -1 ? x_dims.size() - y_dims.size() : param.axis);
    int outer_num, mid_num, inner_num;
    get_mid_dims(x_dims, y_dims, axis, &outer_num, &mid_num, &inner_num);
    for (int outer_id = 0; outer_id < outer_num; ++outer_id) {
      for (int mid_id = 0; mid_id < mid_num; ++mid_id) {
        auto y_data = y[mid_id];
        for (int inner_id = 0; inner_id < inner_num; ++inner_id) {
          int index = (outer_id * mid_num + mid_id) * inner_num + inner_id;
          z[index] = CompareFunctor()(x[index], y_data);
          // z[index] = x[index] < y_data;
        }
      }
    }
  }
}

J
juncaipeng 已提交
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 146 147 148
template <template <typename T> class Functor>
void CompareCompute_int32<Functor>::Run() {
  auto &param = this->Param<operators::CompareParam>();

  using CompareFunctor = Functor<int>;

  const size_t x_size = param.X->numel();
  const size_t y_size = param.Y->numel();
  auto x_dims = param.X->dims();
  auto y_dims = param.Y->dims();
  bool *z = param.Out->template mutable_data<bool>();
  const auto *x = param.X->template data<int>();
  const auto *y = param.Y->template data<int>();
  auto axis = param.axis;
  bool force_cpu = param.force_cpu;
  if (x_size == y_size) {
    for (int i = 0; i < x_size; ++i) {
      z[i] = CompareFunctor()(x[i], y[i]);
    }
  } else {
    int axis = (param.axis == -1 ? x_dims.size() - y_dims.size() : param.axis);
    int outer_num, mid_num, inner_num;
    get_mid_dims(x_dims, y_dims, axis, &outer_num, &mid_num, &inner_num);
    for (int outer_id = 0; outer_id < outer_num; ++outer_id) {
      for (int mid_id = 0; mid_id < mid_num; ++mid_id) {
        auto y_data = y[mid_id];
        for (int inner_id = 0; inner_id < inner_num; ++inner_id) {
          int index = (outer_id * mid_num + mid_id) * inner_num + inner_id;
          z[index] = CompareFunctor()(x[index], y_data);
          // z[index] = x[index] < y_data;
        }
      }
    }
  }
}

X
xiaogang 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
template <template <typename T> class Functor>
void CompareCompute_int64<Functor>::Run() {
  auto &param = this->Param<operators::CompareParam>();

  using CompareFunctor = Functor<int64_t>;

  const size_t x_size = param.X->numel();
  const size_t y_size = param.Y->numel();
  auto x_dims = param.X->dims();
  auto y_dims = param.Y->dims();
  bool *z = param.Out->template mutable_data<bool>();
  const auto *x = param.X->template data<int64_t>();
  const auto *y = param.Y->template data<int64_t>();
  auto axis = param.axis;
  bool force_cpu = param.force_cpu;
  if (x_size == y_size) {
    for (int i = 0; i < x_size; ++i) {
      z[i] = CompareFunctor()(x[i], y[i]);
    }
  } else {
    int axis = (param.axis == -1 ? x_dims.size() - y_dims.size() : param.axis);
    int outer_num, mid_num, inner_num;
    get_mid_dims(x_dims, y_dims, axis, &outer_num, &mid_num, &inner_num);
    for (int outer_id = 0; outer_id < outer_num; ++outer_id) {
      for (int mid_id = 0; mid_id < mid_num; ++mid_id) {
        auto y_data = y[mid_id];
        for (int inner_id = 0; inner_id < inner_num; ++inner_id) {
          int index = (outer_id * mid_num + mid_id) * inner_num + inner_id;
          z[index] = CompareFunctor()(x[index], y_data);
        }
      }
    }
  }
}

Y
Yan Chunwei 已提交
184 185 186 187 188
}  // namespace arm
}  // namespace kernels
}  // namespace lite
}  // namespace paddle

189
REGISTER_LITE_KERNEL(equal,
Y
Yan Chunwei 已提交
190 191 192 193
                     kARM,
                     kFloat,
                     kNCHW,
                     paddle::lite::kernels::arm::CompareCompute<
194
                         paddle::lite::kernels::arm::_EqualFunctor>,
Y
Yan Chunwei 已提交
195 196 197 198 199
                     def)
    .BindInput("X", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindInput("Y", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kBool))})
    .Finalize();
200 201

REGISTER_LITE_KERNEL(equal,
X
xiaogang 已提交
202
                     kARM,
203
                     kInt32,
X
xiaogang 已提交
204
                     kNCHW,
205 206
                     paddle::lite::kernels::arm::CompareCompute_int32<
                         paddle::lite::kernels::arm::_EqualFunctor>,
X
xiaogang 已提交
207
                     def)
208 209
    .BindInput("X", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt32))})
    .BindInput("Y", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt32))})
X
xiaogang 已提交
210 211
    .BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kBool))})
    .Finalize();
212 213

REGISTER_LITE_KERNEL(not_equal,
Y
Yan Chunwei 已提交
214 215 216 217
                     kARM,
                     kFloat,
                     kNCHW,
                     paddle::lite::kernels::arm::CompareCompute<
218
                         paddle::lite::kernels::arm::_NotEqualFunctor>,
Y
Yan Chunwei 已提交
219 220 221 222 223
                     def)
    .BindInput("X", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindInput("Y", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kBool))})
    .Finalize();
224 225

REGISTER_LITE_KERNEL(less_than,
Y
Yan Chunwei 已提交
226 227 228 229
                     kARM,
                     kFloat,
                     kNCHW,
                     paddle::lite::kernels::arm::CompareCompute<
230
                         paddle::lite::kernels::arm::_LessThanFunctor>,
Y
Yan Chunwei 已提交
231 232 233 234 235
                     def)
    .BindInput("X", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindInput("Y", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kBool))})
    .Finalize();
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260

REGISTER_LITE_KERNEL(less_than,
                     kARM,
                     kInt32,
                     kNCHW,
                     paddle::lite::kernels::arm::CompareCompute_int32<
                         paddle::lite::kernels::arm::_LessThanFunctor>,
                     def)
    .BindInput("X", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt32))})
    .BindInput("Y", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt32))})
    .BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kBool))})
    .Finalize();

REGISTER_LITE_KERNEL(less_than,
                     kARM,
                     kInt64,
                     kNCHW,
                     paddle::lite::kernels::arm::CompareCompute_int64<
                         paddle::lite::kernels::arm::_LessThanFunctor>,
                     def)
    .BindInput("X", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt64))})
    .BindInput("Y", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt64))})
    .BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kBool))})
    .Finalize();

Y
Yan Chunwei 已提交
261 262 263 264 265 266 267 268 269 270 271
REGISTER_LITE_KERNEL(less_equal,
                     kARM,
                     kFloat,
                     kNCHW,
                     paddle::lite::kernels::arm::CompareCompute<
                         paddle::lite::kernels::arm::_LessEqualFunctor>,
                     def)
    .BindInput("X", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindInput("Y", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kBool))})
    .Finalize();
272

Y
Yan Chunwei 已提交
273 274 275 276 277 278 279 280 281 282 283
REGISTER_LITE_KERNEL(greater_than,
                     kARM,
                     kFloat,
                     kNCHW,
                     paddle::lite::kernels::arm::CompareCompute<
                         paddle::lite::kernels::arm::_GreaterThanFunctor>,
                     def)
    .BindInput("X", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindInput("Y", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kBool))})
    .Finalize();
284

Y
Yan Chunwei 已提交
285 286 287 288 289 290 291 292 293 294 295
REGISTER_LITE_KERNEL(greater_equal,
                     kARM,
                     kFloat,
                     kNCHW,
                     paddle::lite::kernels::arm::CompareCompute<
                         paddle::lite::kernels::arm::_GreaterEqualFunctor>,
                     def)
    .BindInput("X", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindInput("Y", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kBool))})
    .Finalize();