blob: a4817cf78519e929e068a2893708907714d38694 [file] [log] [blame]
Georgios Pinitas58bce682020-11-13 11:38:58 +00001/*
2 * Copyright (c) 2020 Arm Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#ifndef ARM_COMPUTE_TEST_LOGICAL_FIXTURE
25#define ARM_COMPUTE_TEST_LOGICAL_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
29#include "tests/AssetsLibrary.h"
30#include "tests/Globals.h"
31#include "tests/IAccessor.h"
32#include "tests/framework/Asserts.h"
33#include "tests/framework/Fixture.h"
34#include "tests/validation/reference/Logical.h"
35
36namespace arm_compute
37{
38namespace test
39{
40namespace validation
41{
42template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
43class LogicalOperationValidationFixtureBase : public framework::Fixture
44{
45protected:
46 template <typename U>
47 void fill(U &&tensor, int i)
48 {
49 constexpr uint8_t zero = 0;
50 constexpr uint8_t one = 0x1;
51 constexpr uint8_t mixed = 0xAA;
52 constexpr uint8_t mixed_bitwise_not = ~(0xAA);
53
54 library->fill_static_values(tensor, i == 0 ?
55 std::vector<uint8_t> { zero, one, zero, one, mixed, zero, mixed } :
56 std::vector<uint8_t> { zero, zero, one, one, zero, mixed, mixed_bitwise_not });
57 }
58
59 void allocate_tensor(std::initializer_list<TensorType *> tensors)
60 {
61 for(auto t : tensors)
62 {
63 ARM_COMPUTE_EXPECT(t->info()->is_resizable(), framework::LogLevel::ERRORS);
64 t->allocator()->allocate();
65 ARM_COMPUTE_EXPECT(!t->info()->is_resizable(), framework::LogLevel::ERRORS);
66 }
67 }
68
69 TensorType _target{};
70 SimpleTensor<T> _reference{};
71};
72
73template <typename TensorType, typename AccessorType, typename FunctionType, reference::LogicalBinaryOperation Op, typename T>
74class LogicalBinaryOperationValidationFixture : public LogicalOperationValidationFixtureBase<TensorType, AccessorType, FunctionType, T>
75{
76 using Parent = LogicalOperationValidationFixtureBase<TensorType, AccessorType, FunctionType, T>;
77
78public:
79 template <typename...>
80 void setup(TensorShape shape0, TensorShape shape1)
81 {
82 Parent::_target = compute_target(shape0, shape1);
83 Parent::_reference = compute_reference(shape0, shape1);
84 }
85
86private:
87 TensorType compute_target(const TensorShape &shape0, const TensorShape &shape1)
88 {
89 TensorType src0 = create_tensor<TensorType>(shape0, _data_type);
90 TensorType src1 = create_tensor<TensorType>(shape1, _data_type);
91 TensorType dst = create_tensor<TensorType>(TensorShape::broadcast_shape(shape0, shape1), _data_type);
92
93 FunctionType logical_binary_op;
94
95 logical_binary_op.configure(&src0, &src1, &dst);
96
97 Parent::allocate_tensor({ &src0, &src1, &dst });
98
99 Parent::fill(AccessorType(src0), 0);
100 Parent::fill(AccessorType(src1), 1);
101
102 logical_binary_op.run();
103
104 return dst;
105 }
106
107 SimpleTensor<T> compute_reference(const TensorShape &shape0, const TensorShape &shape1)
108 {
109 // Create reference
110 SimpleTensor<T> src0{ shape0, _data_type };
111 SimpleTensor<T> src1{ shape1, _data_type };
112
113 // Fill reference
114 Parent::fill(src0, 0);
115 Parent::fill(src1, 1);
116
117 switch(Op)
118 {
119 case reference::LogicalBinaryOperation::OR:
120 return reference::logical_or<T>(src0, src1);
121 case reference::LogicalBinaryOperation::AND:
122 return reference::logical_and<T>(src0, src1);
123 case reference::LogicalBinaryOperation::UNKNOWN:
124 /* fall-through */
125 default:
126 ARM_COMPUTE_ASSERT_FAIL("unknown logical binary operator is given");
127 }
128
129 return SimpleTensor<T> {};
130 }
131
132 static constexpr auto _data_type{ DataType::U8 };
133};
134
135template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
136class LogicalNotValidationFixture : public LogicalOperationValidationFixtureBase<TensorType, AccessorType, FunctionType, T>
137{
138 using Parent = LogicalOperationValidationFixtureBase<TensorType, AccessorType, FunctionType, T>;
139
140public:
141 template <typename...>
142 void setup(TensorShape shape, DataType data_type)
143 {
144 Parent::_target = compute_target(shape, data_type);
145 Parent::_reference = compute_reference(shape, data_type);
146 }
147
148private:
149 TensorType compute_target(const TensorShape &shape, DataType data_type)
150 {
151 TensorType src = create_tensor<TensorType>(shape, data_type);
152 TensorType dst = create_tensor<TensorType>(shape, data_type);
153
154 FunctionType logical_not;
155
156 logical_not.configure(&src, &dst);
157
158 Parent::allocate_tensor({ &src, &dst });
159
160 Parent::fill(AccessorType(src), 0);
161
162 logical_not.run();
163
164 return dst;
165 }
166
167 SimpleTensor<T> compute_reference(const TensorShape &shape, DataType data_type)
168 {
169 // Create reference
170 SimpleTensor<T> src{ shape, data_type };
171
172 // Fill reference
173 Parent::fill(src, 0);
174
175 return reference::logical_not<T>(src);
176 }
177};
178} // namespace validation
179} // namespace test
180} // namespace arm_compute
181#endif /* ARM_COMPUTE_TEST_LOGICAL_FIXTURE */