logical_compute_test.cc 4.1 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// 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"

namespace paddle {
namespace lite {

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
struct _logical_and_func {
  inline bool operator()(const bool& a, const bool& b) const { return a && b; }
};

struct _logical_or_func {
  inline bool operator()(const bool& a, const bool& b) const { return a || b; }
};

struct _logical_xor_func {
  inline bool operator()(const bool& a, const bool& b) const {
    return (a || b) && !(a && b);
  }
};

struct _logical_not_func {
  inline bool operator()(const bool& a, const bool& b) const { return !a; }
};

template <class Functor>
class LogicalTester : public arena::TestCase {
Y
Yan Chunwei 已提交
43
 protected:
44 45 46 47 48
  std::string op_type_ = "logical_xor";
  std::string x_ = "x";
  std::string y_ = "y";
  std::string out_ = "out";
  DDim dims_{{2, 3, 4, 5}};
Y
Yan Chunwei 已提交
49 50

 public:
51 52 53 54
  LogicalTester(const Place& place,
                const std::string& alias,
                const std::string& op_type)
      : TestCase(place, alias), op_type_(op_type) {}
Y
Yan Chunwei 已提交
55 56

  void RunBaseline(Scope* scope) override {
57 58 59 60 61 62 63 64 65 66
    auto* x = scope->FindTensor(x_);
    const bool* x_data = x->data<bool>();
    const Tensor* y = nullptr;
    const bool* y_data = nullptr;
    if (op_type_ != "logical_not") {
      y = scope->FindTensor(y_);
      y_data = y->data<bool>();
    }

    auto* out = scope->NewTensor(out_);
Y
Yan Chunwei 已提交
67 68 69
    out->Resize(dims_);
    bool* out_data = out->mutable_data<bool>();
    for (int i = 0; i < dims_.production(); i++) {
70 71
      bool y_tmp = (y_data == nullptr) ? true : y_data[i];
      out_data[i] = Functor()(x_data[i], y_tmp);
Y
Yan Chunwei 已提交
72 73 74 75
    }
  }

  void PrepareOpDesc(cpp::OpDesc* op_desc) {
76 77 78 79 80 81
    op_desc->SetType(op_type_);
    op_desc->SetInput("X", {x_});
    if (op_type_ != "logical_not") {
      op_desc->SetInput("Y", {y_});
    }
    op_desc->SetOutput("Out", {out_});
Y
Yan Chunwei 已提交
82 83 84
  }

  void PrepareData() override {
85 86 87
    bool* dx = new bool[dims_.production()];
    for (int64_t i = 0; i < dims_.production(); i++) {
      dx[i] = (i % 3 == 0);
Y
Yan Chunwei 已提交
88
    }
89 90
    SetCommonTensor(x_, dims_, dx);
    delete dx;
Y
Yan Chunwei 已提交
91

92 93 94 95 96 97 98 99
    if (op_type_ != "logical_not") {
      bool* dy = new bool[dims_.production()];
      for (int64_t i = 0; i < dims_.production(); i++) {
        dy[i] = (i % 2 == 0);
      }
      SetCommonTensor(y_, dims_, dy);
      delete dy;
    }
Y
Yan Chunwei 已提交
100 101 102
  }
};

103 104 105 106 107
void TestLogical(Place place, float abs_error) {
  std::unique_ptr<arena::TestCase> logical_and_tester(
      new LogicalTester<_logical_and_func>(place, "def", "logical_and"));
  arena::Arena arena_and(std::move(logical_and_tester), place, abs_error);
  arena_and.TestPrecision();
Y
Yan Chunwei 已提交
108

109 110 111 112
  std::unique_ptr<arena::TestCase> logical_or_tester(
      new LogicalTester<_logical_or_func>(place, "def", "logical_or"));
  arena::Arena arena_or(std::move(logical_or_tester), place, abs_error);
  arena_or.TestPrecision();
Y
Yan Chunwei 已提交
113

114 115 116 117
  std::unique_ptr<arena::TestCase> logical_xor_tester(
      new LogicalTester<_logical_xor_func>(place, "def", "logical_xor"));
  arena::Arena arena_xor(std::move(logical_xor_tester), place, abs_error);
  arena_xor.TestPrecision();
Y
Yan Chunwei 已提交
118

119 120 121 122
  std::unique_ptr<arena::TestCase> logical_not_tester(
      new LogicalTester<_logical_not_func>(place, "def", "logical_not"));
  arena::Arena arena_not(std::move(logical_not_tester), place, abs_error);
  arena_not.TestPrecision();
Y
Yan Chunwei 已提交
123
}
124

Y
Yan Chunwei 已提交
125
TEST(Logical, precision) {
126 127 128 129 130 131
  Place place;
  float abs_error = 1e-5;
#if defined(LITE_WITH_ARM)
  place = TARGET(kHost);
#else
  return;
Y
Yan Chunwei 已提交
132
#endif
133 134

  TestLogical(place, abs_error);
Y
Yan Chunwei 已提交
135 136 137 138
}

}  // namespace lite
}  // namespace paddle