blob: 9ba7bd3ef7a42c609466d194dc6099a486cdd095 [file] [log] [blame]
Georgios Pinitascbf39c62018-09-10 15:07:45 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Georgios Pinitascbf39c62018-09-10 15:07:45 +01003 *
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_ARITHMETIC_OPERATIONS_FIXTURE
25#define ARM_COMPUTE_TEST_ARITHMETIC_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"
Giorgio Arena8b2a7d32020-02-11 17:21:31 +000035#include "tests/validation/reference/ActivationLayer.h"
Georgios Pinitascbf39c62018-09-10 15:07:45 +010036#include "tests/validation/reference/ArithmeticOperations.h"
37
38namespace arm_compute
39{
40namespace test
41{
42namespace validation
43{
44template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
45class ArithmeticOperationGenericFixture : public framework::Fixture
46{
47public:
48 template <typename...>
49 void setup(reference::ArithmeticOperation op, const TensorShape &shape0, const TensorShape &shape1,
50 DataType data_type0, DataType data_type1, DataType output_data_type, ConvertPolicy convert_policy,
Georgios Pinitasb2842be2020-06-22 21:27:14 +010051 QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out, ActivationLayerInfo act_info, bool in_place)
Georgios Pinitascbf39c62018-09-10 15:07:45 +010052 {
53 _op = op;
Giorgio Arena8b2a7d32020-02-11 17:21:31 +000054 _act_info = act_info;
Georgios Pinitasb2842be2020-06-22 21:27:14 +010055 _in_place = in_place;
Georgios Pinitascbf39c62018-09-10 15:07:45 +010056 _target = compute_target(shape0, shape1, data_type0, data_type1, output_data_type, convert_policy, qinfo0, qinfo1, qinfo_out);
57 _reference = compute_reference(shape0, shape1, data_type0, data_type1, output_data_type, convert_policy, qinfo0, qinfo1, qinfo_out);
58 }
59
60protected:
61 template <typename U>
62 void fill(U &&tensor, int i)
63 {
64 library->fill_tensor_uniform(tensor, i);
65 }
66
67 TensorType compute_target(const TensorShape &shape0, const TensorShape &shape1, DataType data_type0, DataType data_type1, DataType output_data_type, ConvertPolicy convert_policy,
68 QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out)
69 {
70 // Create tensors
Georgios Pinitasb2842be2020-06-22 21:27:14 +010071 TensorType ref_src1 = create_tensor<TensorType>(shape0, data_type0, 1, qinfo0);
72 TensorType ref_src2 = create_tensor<TensorType>(shape1, data_type1, 1, qinfo1);
73 TensorType dst = create_tensor<TensorType>(TensorShape::broadcast_shape(shape0, shape1), output_data_type, 1, qinfo_out);
74 TensorType *dst_to_use = _in_place ? &ref_src1 : &dst;
Georgios Pinitascbf39c62018-09-10 15:07:45 +010075
76 // Create and configure function
77 FunctionType arith_op;
Georgios Pinitasb2842be2020-06-22 21:27:14 +010078 arith_op.configure(&ref_src1, &ref_src2, dst_to_use, convert_policy, _act_info);
Georgios Pinitascbf39c62018-09-10 15:07:45 +010079
80 ARM_COMPUTE_EXPECT(ref_src1.info()->is_resizable(), framework::LogLevel::ERRORS);
81 ARM_COMPUTE_EXPECT(ref_src2.info()->is_resizable(), framework::LogLevel::ERRORS);
Georgios Pinitasb2842be2020-06-22 21:27:14 +010082 ARM_COMPUTE_EXPECT(dst_to_use->info()->is_resizable(), framework::LogLevel::ERRORS);
Georgios Pinitascbf39c62018-09-10 15:07:45 +010083
84 // Allocate tensors
85 ref_src1.allocator()->allocate();
86 ref_src2.allocator()->allocate();
Georgios Pinitasb2842be2020-06-22 21:27:14 +010087 dst_to_use->allocator()->allocate();
Georgios Pinitascbf39c62018-09-10 15:07:45 +010088
89 ARM_COMPUTE_EXPECT(!ref_src1.info()->is_resizable(), framework::LogLevel::ERRORS);
90 ARM_COMPUTE_EXPECT(!ref_src2.info()->is_resizable(), framework::LogLevel::ERRORS);
Georgios Pinitasb2842be2020-06-22 21:27:14 +010091 ARM_COMPUTE_EXPECT(!dst_to_use->info()->is_resizable(), framework::LogLevel::ERRORS);
Georgios Pinitascbf39c62018-09-10 15:07:45 +010092
93 // Fill tensors
94 fill(AccessorType(ref_src1), 0);
95 fill(AccessorType(ref_src2), 1);
96
97 // Compute function
98 arith_op.run();
99
Georgios Pinitasb2842be2020-06-22 21:27:14 +0100100 if(_in_place)
101 {
102 return ref_src1;
103 }
Michele Di Giorgio19023832020-06-17 16:08:10 +0000104 return dst;
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100105 }
106
107 SimpleTensor<T> compute_reference(const TensorShape &shape0, const TensorShape &shape1,
108 DataType data_type0, DataType data_type1, DataType output_data_type, ConvertPolicy convert_policy,
109 QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out)
110 {
Georgios Pinitasb2842be2020-06-22 21:27:14 +0100111 // current in-place implementation only supports same metadata of input and output tensors.
112 // By ignoring output quantization information here, we can make test cases implementation much simpler.
113 QuantizationInfo output_qinfo = _in_place ? qinfo0 : qinfo_out;
114
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100115 // Create reference
116 SimpleTensor<T> ref_src1{ shape0, data_type0, 1, qinfo0 };
117 SimpleTensor<T> ref_src2{ shape1, data_type1, 1, qinfo1 };
Georgios Pinitasb2842be2020-06-22 21:27:14 +0100118 SimpleTensor<T> ref_dst{ TensorShape::broadcast_shape(shape0, shape1), output_data_type, 1, output_qinfo };
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100119
120 // Fill reference
121 fill(ref_src1, 0);
122 fill(ref_src2, 1);
123
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000124 auto result = reference::arithmetic_operation<T>(_op, ref_src1, ref_src2, ref_dst, convert_policy);
Georgios Pinitasb2842be2020-06-22 21:27:14 +0100125 return _act_info.enabled() ? reference::activation_layer(result, _act_info, output_qinfo) : result;
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100126 }
127
128 TensorType _target{};
129 SimpleTensor<T> _reference{};
130 reference::ArithmeticOperation _op{ reference::ArithmeticOperation::ADD };
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000131 ActivationLayerInfo _act_info{};
Georgios Pinitasb2842be2020-06-22 21:27:14 +0100132 bool _in_place{};
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100133};
134
135template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
136class ArithmeticAdditionBroadcastValidationFixture : public ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>
137{
138public:
139 template <typename...>
Michele Di Giorgio19023832020-06-17 16:08:10 +0000140 void setup(const TensorShape &shape0, const TensorShape &shape1, DataType data_type0, DataType data_type1, DataType output_data_type, ConvertPolicy convert_policy)
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100141 {
142 ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(reference::ArithmeticOperation::ADD, shape0, shape1, data_type0, data_type1,
Georgios Pinitasb2842be2020-06-22 21:27:14 +0100143 output_data_type, convert_policy, QuantizationInfo(), QuantizationInfo(), QuantizationInfo(), ActivationLayerInfo(), false);
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100144 }
145};
146
147template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
148class ArithmeticAdditionValidationFixture : public ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>
149{
150public:
151 template <typename...>
Michele Di Giorgio19023832020-06-17 16:08:10 +0000152 void setup(const TensorShape &shape, DataType data_type0, DataType data_type1, DataType output_data_type, ConvertPolicy convert_policy)
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100153 {
154 ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(reference::ArithmeticOperation::ADD, shape, shape, data_type0, data_type1,
Georgios Pinitasb2842be2020-06-22 21:27:14 +0100155 output_data_type, convert_policy, QuantizationInfo(), QuantizationInfo(), QuantizationInfo(), ActivationLayerInfo(), false);
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000156 }
157};
158
159template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
160class ArithmeticAdditionBroadcastValidationFloatFixture : public ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>
161{
162public:
163 template <typename...>
164 void setup(const TensorShape &shape0, const TensorShape &shape1, DataType data_type0, DataType data_type1, DataType output_data_type, ConvertPolicy convert_policy, ActivationLayerInfo act_info)
165 {
166 ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(reference::ArithmeticOperation::ADD, shape0, shape1, data_type0, data_type1,
Georgios Pinitasb2842be2020-06-22 21:27:14 +0100167 output_data_type, convert_policy, QuantizationInfo(), QuantizationInfo(), QuantizationInfo(), act_info, false);
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000168 }
169};
170
171template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
172class ArithmeticAdditionValidationFloatFixture : public ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>
173{
174public:
175 template <typename...>
176 void setup(const TensorShape &shape, DataType data_type0, DataType data_type1, DataType output_data_type, ConvertPolicy convert_policy, ActivationLayerInfo act_info)
177 {
178 ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(reference::ArithmeticOperation::ADD, shape, shape, data_type0, data_type1,
Georgios Pinitasb2842be2020-06-22 21:27:14 +0100179 output_data_type, convert_policy, QuantizationInfo(), QuantizationInfo(), QuantizationInfo(), act_info, false);
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100180 }
181};
182
183template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
184class ArithmeticAdditionValidationQuantizedFixture : public ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>
185{
186public:
187 template <typename...>
188 void setup(const TensorShape &shape, DataType data_type0, DataType data_type1, DataType output_data_type, ConvertPolicy convert_policy,
189 QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out)
190
191 {
192 ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(reference::ArithmeticOperation::ADD, shape, shape, data_type0, data_type1,
Georgios Pinitasb2842be2020-06-22 21:27:14 +0100193 output_data_type, convert_policy, qinfo0, qinfo1, qinfo_out, ActivationLayerInfo(), false);
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100194 }
195};
196
197template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Michalis Spyrou2232a202020-07-13 15:15:33 +0100198class ArithmeticAdditionValidationQuantizedBroadcastFixture : public ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>
199{
200public:
201 template <typename...>
202 void setup(const TensorShape &shape0, const TensorShape &shape1, DataType data_type0, DataType data_type1, DataType output_data_type,
203 ConvertPolicy convert_policy, QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out)
204 {
205 ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(reference::ArithmeticOperation::ADD, shape0, shape1,
206 data_type0, data_type1, output_data_type, convert_policy,
207 qinfo0, qinfo1, qinfo_out, ActivationLayerInfo(), false);
208 }
209};
210
211template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100212class ArithmeticSubtractionBroadcastValidationFixture : public ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>
213{
214public:
215 template <typename...>
Georgios Pinitasb2842be2020-06-22 21:27:14 +0100216 void setup(const TensorShape &shape0, const TensorShape &shape1, DataType data_type0, DataType data_type1, DataType output_data_type, ConvertPolicy convert_policy, bool in_place)
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100217 {
218 ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(reference::ArithmeticOperation::SUB, shape0, shape1,
219 data_type0, data_type1, output_data_type, convert_policy,
Georgios Pinitasb2842be2020-06-22 21:27:14 +0100220 QuantizationInfo(), QuantizationInfo(), QuantizationInfo(), ActivationLayerInfo(), in_place);
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000221 }
222};
223
224template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
225class ArithmeticSubtractionBroadcastValidationFloatFixture : public ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>
226{
227public:
228 template <typename...>
Georgios Pinitasb2842be2020-06-22 21:27:14 +0100229 void setup(const TensorShape &shape0, const TensorShape &shape1, DataType data_type0, DataType data_type1, DataType output_data_type, ConvertPolicy convert_policy, ActivationLayerInfo act_info,
230 bool in_place)
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000231 {
232 ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(reference::ArithmeticOperation::SUB, shape0, shape1,
233 data_type0, data_type1, output_data_type, convert_policy,
Georgios Pinitasb2842be2020-06-22 21:27:14 +0100234 QuantizationInfo(), QuantizationInfo(), QuantizationInfo(), act_info, in_place);
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100235 }
236};
237
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100238template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
239class ArithmeticSubtractionValidationFixture : public ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>
240{
241public:
242 template <typename...>
Georgios Pinitasb2842be2020-06-22 21:27:14 +0100243 void setup(const TensorShape &shape, DataType data_type0, DataType data_type1, DataType output_data_type, ConvertPolicy convert_policy, bool in_place)
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100244 {
245 ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(reference::ArithmeticOperation::SUB, shape, shape,
246 data_type0, data_type1, output_data_type, convert_policy,
Georgios Pinitasb2842be2020-06-22 21:27:14 +0100247 QuantizationInfo(), QuantizationInfo(), QuantizationInfo(), ActivationLayerInfo(), in_place);
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000248 }
249};
250
251template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
252class ArithmeticSubtractionValidationFloatFixture : public ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>
253{
254public:
255 template <typename...>
Georgios Pinitasb2842be2020-06-22 21:27:14 +0100256 void setup(const TensorShape &shape, DataType data_type0, DataType data_type1, DataType output_data_type, ConvertPolicy convert_policy, ActivationLayerInfo act_info, bool in_place)
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000257 {
258 ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(reference::ArithmeticOperation::SUB, shape, shape,
259 data_type0, data_type1, output_data_type, convert_policy,
Georgios Pinitasb2842be2020-06-22 21:27:14 +0100260 QuantizationInfo(), QuantizationInfo(), QuantizationInfo(), act_info, in_place);
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100261 }
262};
263
264template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
265class ArithmeticSubtractionValidationQuantizedFixture : public ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>
266{
267public:
268 template <typename...>
269 void setup(const TensorShape &shape, DataType data_type0, DataType data_type1, DataType output_data_type, ConvertPolicy convert_policy,
Georgios Pinitasb2842be2020-06-22 21:27:14 +0100270 QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out, bool in_place)
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100271
272 {
273 ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(reference::ArithmeticOperation::SUB, shape, shape,
274 data_type0, data_type1, output_data_type,
Georgios Pinitasb2842be2020-06-22 21:27:14 +0100275 convert_policy, qinfo0, qinfo1, qinfo_out, ActivationLayerInfo(), in_place);
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100276 }
277};
Michalis Spyroueae65842020-06-15 20:23:59 +0100278
279template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
280class ArithmeticSubtractionValidationQuantizedBroadcastFixture : public ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>
281{
282public:
283 template <typename...>
284 void setup(const TensorShape &shape0, const TensorShape &shape1, DataType data_type0, DataType data_type1, DataType output_data_type,
Georgios Pinitasb2842be2020-06-22 21:27:14 +0100285 ConvertPolicy convert_policy, QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out, bool in_place)
Michalis Spyroueae65842020-06-15 20:23:59 +0100286 {
287 ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(reference::ArithmeticOperation::SUB, shape0, shape1,
288 data_type0, data_type1, output_data_type, convert_policy,
Georgios Pinitasb2842be2020-06-22 21:27:14 +0100289 qinfo0, qinfo1, qinfo_out, ActivationLayerInfo(), in_place);
Michalis Spyroueae65842020-06-15 20:23:59 +0100290 }
291};
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100292} // namespace validation
293} // namespace test
294} // namespace arm_compute
295#endif /* ARM_COMPUTE_TEST_ARITHMETIC_OPERATIONS_FIXTURE */