blob: 8b88f6a8fb1abd5dcb16538a875918ffab3b05b1 [file] [log] [blame]
giuros01164a2722018-11-20 18:34:46 +00001/*
Georgios Pinitas587708b2018-12-31 15:43:52 +00002 * Copyright (c) 2018-2019 ARM Limited.
giuros01164a2722018-11-20 18:34:46 +00003 *
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_ELEMENTWISE_OPERATIONS_FIXTURE
25#define ARM_COMPUTE_TEST_ELEMENTWISE_OPERATIONS_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/Helpers.h"
35#include "tests/validation/reference/ElementwiseOperations.h"
36
37namespace arm_compute
38{
39namespace test
40{
41namespace validation
42{
43template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
44class ArithmeticOperationsGenericFixture : public framework::Fixture
45{
46public:
47 template <typename...>
48 void setup(ArithmeticOperation op, const TensorShape &shape0, const TensorShape &shape1,
49 DataType data_type0, DataType data_type1, DataType output_data_type,
50 QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out)
51 {
52 _op = op;
53 _target = compute_target(shape0, shape1, data_type0, data_type1, output_data_type, qinfo0, qinfo1, qinfo_out);
54 _reference = compute_reference(shape0, shape1, data_type0, data_type1, output_data_type, qinfo0, qinfo1, qinfo_out);
55 }
56
57protected:
58 template <typename U>
59 void fill(U &&tensor, int i)
60 {
Georgios Pinitas587708b2018-12-31 15:43:52 +000061 (_op == ArithmeticOperation::DIV) ? library->fill_tensor_uniform_ranged(tensor, i, { std::pair<float, float>(-0.001f, 0.001f) }) : library->fill_tensor_uniform(tensor, i);
giuros01164a2722018-11-20 18:34:46 +000062 }
63
64 TensorType compute_target(const TensorShape &shape0, const TensorShape &shape1, DataType data_type0, DataType data_type1, DataType output_data_type,
65 QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out)
66 {
67 // Create tensors
68 TensorType ref_src1 = create_tensor<TensorType>(shape0, data_type0, 1, qinfo0);
69 TensorType ref_src2 = create_tensor<TensorType>(shape1, data_type1, 1, qinfo1);
70 TensorType dst = create_tensor<TensorType>(TensorShape::broadcast_shape(shape0, shape1), output_data_type, 1, qinfo_out);
71
72 // Create and configure function
73 FunctionType elem_op;
74 elem_op.configure(&ref_src1, &ref_src2, &dst);
75
76 ARM_COMPUTE_EXPECT(ref_src1.info()->is_resizable(), framework::LogLevel::ERRORS);
77 ARM_COMPUTE_EXPECT(ref_src2.info()->is_resizable(), framework::LogLevel::ERRORS);
78 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
79
80 // Allocate tensors
81 ref_src1.allocator()->allocate();
82 ref_src2.allocator()->allocate();
83 dst.allocator()->allocate();
84
85 ARM_COMPUTE_EXPECT(!ref_src1.info()->is_resizable(), framework::LogLevel::ERRORS);
86 ARM_COMPUTE_EXPECT(!ref_src2.info()->is_resizable(), framework::LogLevel::ERRORS);
87 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
88
89 // Fill tensors
90 fill(AccessorType(ref_src1), 0);
91 fill(AccessorType(ref_src2), 1);
92
93 // Compute function
94 elem_op.run();
95
96 return dst;
97 }
98
99 SimpleTensor<T> compute_reference(const TensorShape &shape0, const TensorShape &shape1,
100 DataType data_type0, DataType data_type1, DataType output_data_type,
101 QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out)
102 {
103 // Create reference
104 SimpleTensor<T> ref_src1{ shape0, data_type0, 1, qinfo0 };
105 SimpleTensor<T> ref_src2{ shape1, data_type1, 1, qinfo1 };
106 SimpleTensor<T> ref_dst{ TensorShape::broadcast_shape(shape0, shape1), output_data_type, 1, qinfo_out };
107
108 // Fill reference
109 fill(ref_src1, 0);
110 fill(ref_src2, 1);
111
112 return reference::arithmetic_operation<T>(_op, ref_src1, ref_src2, ref_dst);
113 }
114
115 TensorType _target{};
116 SimpleTensor<T> _reference{};
117 ArithmeticOperation _op{ ArithmeticOperation::ADD };
118};
119
120template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
121class ArithmeticDivisionBroadcastValidationFixture : public ArithmeticOperationsGenericFixture<TensorType, AccessorType, FunctionType, T>
122{
123public:
124 template <typename...>
125 void setup(const TensorShape &shape0, const TensorShape &shape1, DataType data_type0, DataType data_type1, DataType output_data_type)
126 {
127 ArithmeticOperationsGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(ArithmeticOperation::DIV, shape0, shape1,
128 data_type0, data_type1, output_data_type,
129 QuantizationInfo(), QuantizationInfo(), QuantizationInfo());
130 }
131};
132
133template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
134class ArithmeticDivisionValidationFixture : public ArithmeticOperationsGenericFixture<TensorType, AccessorType, FunctionType, T>
135{
136public:
137 template <typename...>
138 void setup(const TensorShape &shape, DataType data_type0, DataType data_type1, DataType output_data_type)
139 {
140 ArithmeticOperationsGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(ArithmeticOperation::DIV, shape, shape,
141 data_type0, data_type1, output_data_type,
142 QuantizationInfo(), QuantizationInfo(), QuantizationInfo());
143 }
144};
145
146template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
147class ArithmeticDivisionValidationQuantizedFixture : public ArithmeticOperationsGenericFixture<TensorType, AccessorType, FunctionType, T>
148{
149public:
150 template <typename...>
151 void setup(const TensorShape &shape, DataType data_type0, DataType data_type1, DataType output_data_type,
152 QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out)
153
154 {
155 ArithmeticOperationsGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(ArithmeticOperation::DIV, shape, shape,
156 data_type0, data_type1, output_data_type,
157 qinfo0, qinfo1, qinfo_out);
158 }
159};
160
161template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
162class ElementwiseMaxBroadcastValidationFixture : public ArithmeticOperationsGenericFixture<TensorType, AccessorType, FunctionType, T>
163{
164public:
165 template <typename...>
166 void setup(const TensorShape &shape0, const TensorShape &shape1, DataType data_type0, DataType data_type1, DataType output_data_type)
167 {
168 ArithmeticOperationsGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(ArithmeticOperation::MAX, shape0, shape1,
169 data_type0, data_type1, output_data_type,
170 QuantizationInfo(), QuantizationInfo(), QuantizationInfo());
171 }
172};
173
174template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
175class ElementwiseMaxValidationFixture : public ArithmeticOperationsGenericFixture<TensorType, AccessorType, FunctionType, T>
176{
177public:
178 template <typename...>
179 void setup(const TensorShape &shape, DataType data_type0, DataType data_type1, DataType output_data_type)
180 {
181 ArithmeticOperationsGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(ArithmeticOperation::MAX, shape, shape,
182 data_type0, data_type1, output_data_type,
183 QuantizationInfo(), QuantizationInfo(), QuantizationInfo());
184 }
185};
186
187template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
188class ElementwiseMaxValidationQuantizedFixture : public ArithmeticOperationsGenericFixture<TensorType, AccessorType, FunctionType, T>
189{
190public:
191 template <typename...>
192 void setup(const TensorShape &shape, DataType data_type0, DataType data_type1, DataType output_data_type,
193 QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out)
194
195 {
196 ArithmeticOperationsGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(ArithmeticOperation::MAX, shape, shape,
197 data_type0, data_type1, output_data_type,
198 qinfo0, qinfo1, qinfo_out);
199 }
200};
201
202template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
giuros0192fd9432018-12-03 17:30:00 +0000203class ElementwiseMaxQuantizedBroadcastValidationFixture : public ArithmeticOperationsGenericFixture<TensorType, AccessorType, FunctionType, T>
204{
205public:
206 template <typename...>
207 void setup(const TensorShape &shape0, const TensorShape &shape1, DataType data_type0, DataType data_type1, DataType output_data_type,
208 QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out)
209
210 {
211 ArithmeticOperationsGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(ArithmeticOperation::MAX, shape0, shape1,
212 data_type0, data_type1, output_data_type,
213 qinfo0, qinfo1, qinfo_out);
214 }
215};
216
217template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
giuros01164a2722018-11-20 18:34:46 +0000218class ElementwiseMinBroadcastValidationFixture : public ArithmeticOperationsGenericFixture<TensorType, AccessorType, FunctionType, T>
219{
220public:
221 template <typename...>
222 void setup(const TensorShape &shape0, const TensorShape &shape1, DataType data_type0, DataType data_type1, DataType output_data_type)
223 {
224 ArithmeticOperationsGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(ArithmeticOperation::MIN, shape0, shape1,
225 data_type0, data_type1, output_data_type,
226 QuantizationInfo(), QuantizationInfo(), QuantizationInfo());
227 }
228};
229
230template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
231class ElementwiseMinValidationFixture : public ArithmeticOperationsGenericFixture<TensorType, AccessorType, FunctionType, T>
232{
233public:
234 template <typename...>
235 void setup(const TensorShape &shape, DataType data_type0, DataType data_type1, DataType output_data_type)
236 {
237 ArithmeticOperationsGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(ArithmeticOperation::MIN, shape, shape,
238 data_type0, data_type1, output_data_type,
239 QuantizationInfo(), QuantizationInfo(), QuantizationInfo());
240 }
241};
242
243template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
244class ElementwiseMinValidationQuantizedFixture : public ArithmeticOperationsGenericFixture<TensorType, AccessorType, FunctionType, T>
245{
246public:
247 template <typename...>
248 void setup(const TensorShape &shape, DataType data_type0, DataType data_type1, DataType output_data_type,
249 QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out)
250
251 {
252 ArithmeticOperationsGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(ArithmeticOperation::MIN, shape, shape,
253 data_type0, data_type1, output_data_type,
254 qinfo0, qinfo1, qinfo_out);
255 }
256};
257
258template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
giuros0192fd9432018-12-03 17:30:00 +0000259class ElementwiseMinQuantizedBroadcastValidationFixture : public ArithmeticOperationsGenericFixture<TensorType, AccessorType, FunctionType, T>
260{
261public:
262 template <typename...>
263 void setup(const TensorShape &shape0, const TensorShape &shape1, DataType data_type0, DataType data_type1, DataType output_data_type,
264 QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out)
265
266 {
267 ArithmeticOperationsGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(ArithmeticOperation::MIN, shape0, shape1,
268 data_type0, data_type1, output_data_type,
269 qinfo0, qinfo1, qinfo_out);
270 }
271};
272
273template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
giuros01164a2722018-11-20 18:34:46 +0000274class ElementwiseSquaredDiffBroadcastValidationFixture : public ArithmeticOperationsGenericFixture<TensorType, AccessorType, FunctionType, T>
275{
276public:
277 template <typename...>
278 void setup(const TensorShape &shape0, const TensorShape &shape1, DataType data_type0, DataType data_type1, DataType output_data_type)
279 {
280 ArithmeticOperationsGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(ArithmeticOperation::SQUARED_DIFF, shape0, shape1,
281 data_type0, data_type1, output_data_type,
282 QuantizationInfo(), QuantizationInfo(), QuantizationInfo());
283 }
284};
285
286template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
287class ElementwiseSquaredDiffValidationFixture : public ArithmeticOperationsGenericFixture<TensorType, AccessorType, FunctionType, T>
288{
289public:
290 template <typename...>
291 void setup(const TensorShape &shape, DataType data_type0, DataType data_type1, DataType output_data_type)
292 {
293 ArithmeticOperationsGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(ArithmeticOperation::SQUARED_DIFF, shape, shape,
294 data_type0, data_type1, output_data_type,
295 QuantizationInfo(), QuantizationInfo(), QuantizationInfo());
296 }
297};
298
299template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
300class ElementwiseSquaredDiffValidationQuantizedFixture : public ArithmeticOperationsGenericFixture<TensorType, AccessorType, FunctionType, T>
301{
302public:
303 template <typename...>
304 void setup(const TensorShape &shape, DataType data_type0, DataType data_type1, DataType output_data_type,
305 QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out)
306
307 {
308 ArithmeticOperationsGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(ArithmeticOperation::SQUARED_DIFF, shape, shape,
309 data_type0, data_type1, output_data_type,
310 qinfo0, qinfo1, qinfo_out);
311 }
312};
giuros0192fd9432018-12-03 17:30:00 +0000313
314template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
315class ElementwiseSquaredDiffQuantizedBroadcastValidationFixture : public ArithmeticOperationsGenericFixture<TensorType, AccessorType, FunctionType, T>
316{
317public:
318 template <typename...>
319 void setup(const TensorShape &shape0, const TensorShape &shape1, DataType data_type0, DataType data_type1, DataType output_data_type,
320 QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out)
321
322 {
323 ArithmeticOperationsGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(ArithmeticOperation::SQUARED_DIFF, shape0, shape1,
324 data_type0, data_type1, output_data_type,
325 qinfo0, qinfo1, qinfo_out);
326 }
327};
giuros01164a2722018-11-20 18:34:46 +0000328} // namespace validation
329} // namespace test
330} // namespace arm_compute
331#endif /* ARM_COMPUTE_TEST_ARITHMETIC_OPERATIONS_FIXTURE */