blob: 76f241cedb50920232958afa733389da198dacd8 [file] [log] [blame]
Georgios Pinitascbf39c62018-09-10 15:07:45 +01001/*
Manuel Bottini6a2b6e82019-02-25 13:50:11 +00002 * Copyright (c) 2017-2019 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"
35#include "tests/validation/reference/ArithmeticOperations.h"
36
37namespace arm_compute
38{
39namespace test
40{
41namespace validation
42{
43template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
44class ArithmeticOperationGenericFixture : public framework::Fixture
45{
46public:
47 template <typename...>
48 void setup(reference::ArithmeticOperation op, const TensorShape &shape0, const TensorShape &shape1,
49 DataType data_type0, DataType data_type1, DataType output_data_type, ConvertPolicy convert_policy,
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, convert_policy, qinfo0, qinfo1, qinfo_out);
54 _reference = compute_reference(shape0, shape1, data_type0, data_type1, output_data_type, convert_policy, qinfo0, qinfo1, qinfo_out);
55 }
56
57protected:
58 template <typename U>
59 void fill(U &&tensor, int i)
60 {
61 library->fill_tensor_uniform(tensor, i);
62 }
63
64 TensorType compute_target(const TensorShape &shape0, const TensorShape &shape1, DataType data_type0, DataType data_type1, DataType output_data_type, ConvertPolicy convert_policy,
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 arith_op;
74 arith_op.configure(&ref_src1, &ref_src2, &dst, convert_policy);
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 arith_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, ConvertPolicy convert_policy,
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, convert_policy);
113 }
114
115 TensorType _target{};
116 SimpleTensor<T> _reference{};
117 reference::ArithmeticOperation _op{ reference::ArithmeticOperation::ADD };
118};
119
120template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
121class ArithmeticAdditionBroadcastValidationFixture : public ArithmeticOperationGenericFixture<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, ConvertPolicy convert_policy)
126 {
127 ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(reference::ArithmeticOperation::ADD, shape0, shape1, data_type0, data_type1,
128 output_data_type, convert_policy, QuantizationInfo(), QuantizationInfo(), QuantizationInfo());
129 }
130};
131
132template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
133class ArithmeticAdditionValidationFixture : public ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>
134{
135public:
136 template <typename...>
137 void setup(const TensorShape &shape, DataType data_type0, DataType data_type1, DataType output_data_type, ConvertPolicy convert_policy)
138 {
139 ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(reference::ArithmeticOperation::ADD, shape, shape, data_type0, data_type1,
140 output_data_type, convert_policy, QuantizationInfo(), QuantizationInfo(), QuantizationInfo());
141 }
142};
143
144template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
145class ArithmeticAdditionValidationQuantizedFixture : public ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>
146{
147public:
148 template <typename...>
149 void setup(const TensorShape &shape, DataType data_type0, DataType data_type1, DataType output_data_type, ConvertPolicy convert_policy,
150 QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out)
151
152 {
153 ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(reference::ArithmeticOperation::ADD, shape, shape, data_type0, data_type1,
154 output_data_type, convert_policy, qinfo0, qinfo1, qinfo_out);
155 }
156};
157
158template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
159class ArithmeticSubtractionBroadcastValidationFixture : public ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>
160{
161public:
162 template <typename...>
163 void setup(const TensorShape &shape0, const TensorShape &shape1, DataType data_type0, DataType data_type1, DataType output_data_type, ConvertPolicy convert_policy)
164 {
165 ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(reference::ArithmeticOperation::SUB, shape0, shape1,
166 data_type0, data_type1, output_data_type, convert_policy,
167 QuantizationInfo(), QuantizationInfo(), QuantizationInfo());
168 }
169};
170
Manuel Bottini6a2b6e82019-02-25 13:50:11 +0000171template <typename TensorType, typename AccessorType, typename FunctionType>
172class ArithmeticSubtractionQuantValidationFixture : public ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, qasymm8_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,
177 QuantizationInfo in1_qua_info, QuantizationInfo in2_qua_info, QuantizationInfo out_qua_info)
178 {
179 ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, qasymm8_t>::setup(reference::ArithmeticOperation::SUB, shape, shape,
180 data_type0, data_type1, output_data_type, convert_policy,
181 in1_qua_info, in2_qua_info, out_qua_info);
182 }
183};
184
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100185template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
186class ArithmeticSubtractionValidationFixture : public ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>
187{
188public:
189 template <typename...>
190 void setup(const TensorShape &shape, DataType data_type0, DataType data_type1, DataType output_data_type, ConvertPolicy convert_policy)
191 {
192 ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(reference::ArithmeticOperation::SUB, shape, shape,
193 data_type0, data_type1, output_data_type, convert_policy,
194 QuantizationInfo(), QuantizationInfo(), QuantizationInfo());
195 }
196};
197
198template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
199class ArithmeticSubtractionValidationQuantizedFixture : public ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>
200{
201public:
202 template <typename...>
203 void setup(const TensorShape &shape, DataType data_type0, DataType data_type1, DataType output_data_type, ConvertPolicy convert_policy,
204 QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out)
205
206 {
207 ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(reference::ArithmeticOperation::SUB, shape, shape,
208 data_type0, data_type1, output_data_type,
209 convert_policy, qinfo0, qinfo1, qinfo_out);
210 }
211};
212} // namespace validation
213} // namespace test
214} // namespace arm_compute
215#endif /* ARM_COMPUTE_TEST_ARITHMETIC_OPERATIONS_FIXTURE */