compare_compute_test.cc 8.2 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// 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 <gtest/gtest.h>
#include "lite/api/paddle_use_kernels.h"
#include "lite/api/paddle_use_ops.h"
#include "lite/core/arena/framework.h"
19
#include "lite/tests/utils/fill_data.h"
Y
Yan Chunwei 已提交
20 21 22

namespace paddle {
namespace lite {
23

Y
Yan Chunwei 已提交
24 25
#define COMPARE_FUNCTOR(name, op)                                           \
  template <typename T>                                                     \
26
  struct name##Functor {                                                    \
Y
Yan Chunwei 已提交
27 28 29 30 31 32 33 34 35 36 37
    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 <>
38
struct EqualFunctor<float> {
Y
Yan Chunwei 已提交
39 40 41 42 43 44 45
  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 <>
46
struct NotEqualFunctor<float> {
Y
Yan Chunwei 已提交
47
  inline bool operator()(const float& a, const float& b) const {
48
    return !EqualFunctor<float>()(a, b);
Y
Yan Chunwei 已提交
49 50 51
  }
};

52 53
template <typename T, template <typename U> class Functor>
class CompareComputeTester : public arena::TestCase {
Y
Yan Chunwei 已提交
54
 protected:
55 56 57 58
  std::string x_ = "x";
  std::string y_ = "y";
  std::string out_ = "out";
  std::string op_ = "less_than";
Y
Yan Chunwei 已提交
59 60
  DDim x_dims_{{3, 5, 4, 4}};
  DDim y_dims_{{4}};
61 62
  int axis_ = -1;
  bool force_cpu_ = false;
Y
Yan Chunwei 已提交
63 64

 public:
65 66 67 68 69 70
  CompareComputeTester(const Place& place,
                       const std::string& alias,
                       const std::string op,
                       DDim x_dims,
                       DDim y_dims,
                       int axis = -1)
Y
Yan Chunwei 已提交
71
      : TestCase(place, alias),
72
        op_(op),
Y
Yan Chunwei 已提交
73 74
        x_dims_(x_dims),
        y_dims_(y_dims),
75
        axis_(axis) {}
Y
Yan Chunwei 已提交
76 77

  void RunBaseline(Scope* scope) override {
78
    auto* out = scope->NewTensor(out_);
Y
Yan Chunwei 已提交
79 80 81 82
    CHECK(out);
    out->Resize(x_dims_);
    auto* out_data = out->mutable_data<bool>();
    auto axis = axis_;
83 84 85 86
    auto* x = scope->FindTensor(x_);
    const auto* x_data = x->data<T>();
    auto* y = scope->FindTensor(y_);
    auto* y_data_in = y->data<T>();
Y
Yan Chunwei 已提交
87

88
    using CompareFunc = Functor<T>;
Y
Yan Chunwei 已提交
89 90 91 92 93
    if (x_dims_.size() == y_dims_.size()) {
      for (int i = 0; i < x_dims_.production(); i++) {
        out_data[i] = CompareFunc()(x_data[i], y_data_in[i]);
      }
    } else {
94 95
      auto* y_data =
          reinterpret_cast<T*>(malloc(x_dims_.production() * sizeof(T)));
Y
Yan Chunwei 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

      if (axis < 0) {
        axis = x_dims_.size() - y_dims_.size();
      }
      int batch = 1;
      int channels = 1;
      int num = 1;
      for (int i = 0; i < axis; ++i) {
        batch *= x_dims_[i];
      }
      for (int i = 0; i < y_dims_.size(); ++i) {
        channels *= y_dims_[i];
      }
      for (int i = y_dims_.size() + axis; i < x_dims_.size(); ++i) {
        num *= x_dims_[i];
      }
      int ysize = channels * num;
113
      T* y_data_t = reinterpret_cast<T*>(y_data);
Y
Yan Chunwei 已提交
114 115 116 117
      if (num == 1) {
        for (int i = 0; i < batch; ++i) {
          memcpy(reinterpret_cast<void*>(y_data_t),
                 reinterpret_cast<const void*>(&y_data_in[0]),
118
                 ysize * sizeof(T));
Y
Yan Chunwei 已提交
119 120 121 122 123 124 125 126 127
          y_data_t += ysize;
        }

      } else {
        for (int i = 0; i < channels; i++) {
          for (int j = 0; j < num; j++) {
            y_data_t[i * num + j] = y_data_in[i];
          }
        }
128
        T* tempptr = y_data_t;
Y
Yan Chunwei 已提交
129
        for (int i = 0; i < batch; ++i) {
130
          memcpy(y_data_t, tempptr, ysize * sizeof(T));
Y
Yan Chunwei 已提交
131 132 133 134 135 136 137 138 139 140
          y_data_t += ysize;
        }
      }
      for (int i = 0; i < x_dims_.production(); i++) {
        out_data[i] = CompareFunc()(x_data[i], y_data[i]);
      }
    }
  }

  void PrepareOpDesc(cpp::OpDesc* op_desc) {
141 142 143 144
    op_desc->SetType(op_);
    op_desc->SetInput("X", {x_});
    op_desc->SetInput("Y", {y_});
    op_desc->SetOutput("Out", {out_});
Y
Yan Chunwei 已提交
145 146 147 148 149
    op_desc->SetAttr("axis", axis_);
    op_desc->SetAttr("force_cpu", force_cpu_);
  }

  void PrepareData() override {
150 151 152 153 154 155
    std::vector<T> dx(x_dims_.production());
    std::vector<T> dy(y_dims_.production());
    fill_data_rand<T>(dx.data(), -5, 5, x_dims_.production());
    fill_data_rand<T>(dy.data(), -5, 5, y_dims_.production());
    SetCommonTensor(x_, x_dims_, dx.data());
    SetCommonTensor(y_, y_dims_, dy.data());
Y
Yan Chunwei 已提交
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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

template <typename T>
void TestCompare(Place place,
                 float abs_error,
                 std::string op,
                 std::vector<int64_t> x_dims,
                 std::vector<int64_t> y_dims,
                 int axis) {
  if (typeid(T) == typeid(float)) {
    place.precision = PRECISION(kFloat);
  } else if (typeid(T) == typeid(int32_t)) {
    place.precision = PRECISION(kInt32);
  } else if (typeid(T) == typeid(int64_t)) {
    place.precision = PRECISION(kInt64);
  } else {
    LOG(FATAL) << "unsupported dtype";
  }

  std::unique_ptr<arena::TestCase> tester = nullptr;
  if (op == "equal") {
    tester = static_cast<std::unique_ptr<arena::TestCase>>(
        new CompareComputeTester<T, EqualFunctor>(
            place, "def", op, DDim(x_dims), DDim(y_dims), axis));
  } else if (op == "not_equal") {
    tester = static_cast<std::unique_ptr<arena::TestCase>>(
        new CompareComputeTester<T, NotEqualFunctor>(
            place, "def", op, DDim(x_dims), DDim(y_dims), axis));
  } else if (op == "less_than") {
    tester = static_cast<std::unique_ptr<arena::TestCase>>(
        new CompareComputeTester<T, LessThanFunctor>(
            place, "def", op, DDim(x_dims), DDim(y_dims), axis));
  } else if (op == "less_equal") {
    tester = static_cast<std::unique_ptr<arena::TestCase>>(
        new CompareComputeTester<T, LessEqualFunctor>(
            place, "def", op, DDim(x_dims), DDim(y_dims), axis));
  } else if (op == "greater_than") {
    tester = static_cast<std::unique_ptr<arena::TestCase>>(
        new CompareComputeTester<T, GreaterThanFunctor>(
            place, "def", op, DDim(x_dims), DDim(y_dims), axis));
  } else if (op == "greater_equal") {
    tester = static_cast<std::unique_ptr<arena::TestCase>>(
        new CompareComputeTester<T, GreaterEqualFunctor>(
            place, "def", op, DDim(x_dims), DDim(y_dims), axis));
  } else {
    LOG(FATAL) << "unsupported type";
Y
Yan Chunwei 已提交
203
  }
204 205
  arena::Arena arena(std::move(tester), place, abs_error);
  arena.TestPrecision();
Y
Yan Chunwei 已提交
206
}
207 208 209 210 211 212 213 214 215

#if defined(LITE_WITH_NPU)
TEST(Compare_OP_NPU, precision) {
  Place place{TARGET(kNPU)};
  float abs_error = 1e-2;

  TestCompare<float>(
      place, abs_error, "less_than", {2, 3, 4, 5}, {2, 3, 4, 5}, -1);
  TestCompare<float>(place, abs_error, "less_than", {2, 3, 4}, {2, 3, 4}, 0);
Y
Yan Chunwei 已提交
216
}
217 218
#elif defined(LITE_WITH_ARM)
TEST(Compare_OP_ARM, precision) {
219
  Place place{TARGET(kHost)};
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
  float abs_error = 1e-5;
  for (auto op : std::vector<std::string>{"equal",
                                          "not_equal",
                                          "less_than",
                                          "less_equal",
                                          "greater_than",
                                          "greater_equal"}) {
    TestCompare<float>(place, abs_error, op, {2, 3, 4, 5}, {2, 3, 4, 5}, -1);
    TestCompare<float>(place, abs_error, op, {2, 3, 4}, {2, 3, 4}, 0);
  }

  TestCompare<float>(place, abs_error, "equal", {2, 3, 4}, {3, 4}, 1);
  TestCompare<float>(place, abs_error, "equal", {2, 3, 4, 5}, {3, 4}, 1);
  TestCompare<float>(place, abs_error, "equal", {2, 3, 4}, {4}, 2);
  TestCompare<float>(place, abs_error, "equal", {2, 3, 4, 5}, {5}, 3);

  TestCompare<int32_t>(place, abs_error, "less_than", {3, 4}, {3, 4}, -1);
  TestCompare<int64_t>(place, abs_error, "less_than", {3, 4}, {3, 4}, -1);
}
#endif
Y
Yan Chunwei 已提交
240 241 242

}  // namespace lite
}  // namespace paddle