blob: 6e00c4604238738b125abddcd7c6809b1f13bf68 [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>
198class ArithmeticSubtractionBroadcastValidationFixture : public ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>
199{
200public:
201 template <typename...>
Georgios Pinitasb2842be2020-06-22 21:27:14 +0100202 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 +0100203 {
204 ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(reference::ArithmeticOperation::SUB, shape0, shape1,
205 data_type0, data_type1, output_data_type, convert_policy,
Georgios Pinitasb2842be2020-06-22 21:27:14 +0100206 QuantizationInfo(), QuantizationInfo(), QuantizationInfo(), ActivationLayerInfo(), in_place);
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000207 }
208};
209
210template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
211class ArithmeticSubtractionBroadcastValidationFloatFixture : public ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>
212{
213public:
214 template <typename...>
Georgios Pinitasb2842be2020-06-22 21:27:14 +0100215 void setup(const TensorShape &shape0, const TensorShape &shape1, DataType data_type0, DataType data_type1, DataType output_data_type, ConvertPolicy convert_policy, ActivationLayerInfo act_info,
216 bool in_place)
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000217 {
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(), act_info, in_place);
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100221 }
222};
223
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100224template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
225class ArithmeticSubtractionValidationFixture : public ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>
226{
227public:
228 template <typename...>
Georgios Pinitasb2842be2020-06-22 21:27:14 +0100229 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 +0100230 {
231 ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(reference::ArithmeticOperation::SUB, shape, shape,
232 data_type0, data_type1, output_data_type, convert_policy,
Georgios Pinitasb2842be2020-06-22 21:27:14 +0100233 QuantizationInfo(), QuantizationInfo(), QuantizationInfo(), ActivationLayerInfo(), in_place);
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000234 }
235};
236
237template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
238class ArithmeticSubtractionValidationFloatFixture : public ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>
239{
240public:
241 template <typename...>
Georgios Pinitasb2842be2020-06-22 21:27:14 +0100242 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 +0000243 {
244 ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(reference::ArithmeticOperation::SUB, shape, shape,
245 data_type0, data_type1, output_data_type, convert_policy,
Georgios Pinitasb2842be2020-06-22 21:27:14 +0100246 QuantizationInfo(), QuantizationInfo(), QuantizationInfo(), act_info, in_place);
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100247 }
248};
249
250template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
251class ArithmeticSubtractionValidationQuantizedFixture : public ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>
252{
253public:
254 template <typename...>
255 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 +0100256 QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out, bool in_place)
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100257
258 {
259 ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(reference::ArithmeticOperation::SUB, shape, shape,
260 data_type0, data_type1, output_data_type,
Georgios Pinitasb2842be2020-06-22 21:27:14 +0100261 convert_policy, qinfo0, qinfo1, qinfo_out, ActivationLayerInfo(), in_place);
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100262 }
263};
Michalis Spyroueae65842020-06-15 20:23:59 +0100264
265template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
266class ArithmeticSubtractionValidationQuantizedBroadcastFixture : public ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>
267{
268public:
269 template <typename...>
270 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 +0100271 ConvertPolicy convert_policy, QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out, bool in_place)
Michalis Spyroueae65842020-06-15 20:23:59 +0100272 {
273 ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(reference::ArithmeticOperation::SUB, shape0, shape1,
274 data_type0, data_type1, output_data_type, convert_policy,
Georgios Pinitasb2842be2020-06-22 21:27:14 +0100275 qinfo0, qinfo1, qinfo_out, ActivationLayerInfo(), in_place);
Michalis Spyroueae65842020-06-15 20:23:59 +0100276 }
277};
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100278} // namespace validation
279} // namespace test
280} // namespace arm_compute
281#endif /* ARM_COMPUTE_TEST_ARITHMETIC_OPERATIONS_FIXTURE */