blob: 9729907630f9925ec8c3ff8db90ef3036a3b325c [file] [log] [blame]
Michalis Spyroue9362622018-11-23 17:41:37 +00001/*
Giorgio Arenab309fc22021-01-05 09:46:16 +00002 * Copyright (c) 2018-2021 Arm Limited.
Michalis Spyroue9362622018-11-23 17:41:37 +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_UNARY_FIXTURE
25#define ARM_COMPUTE_TEST_ELEMENTWISE_UNARY_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/ElementWiseUnary.h"
35
36namespace arm_compute
37{
38namespace test
39{
40namespace validation
41{
42template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
43class ElementWiseUnaryValidationFixture : public framework::Fixture
44{
45public:
46 template <typename...>
Sang-Hoon Park668ccdc2021-02-03 10:32:59 +000047 void setup(TensorShape input_shape, DataType input_data_type, bool in_place, ElementWiseUnary op, bool use_dynamic_shape = false)
Michalis Spyroue9362622018-11-23 17:41:37 +000048 {
Sang-Hoon Park668ccdc2021-02-03 10:32:59 +000049 _op = op;
50 _target = compute_target(input_shape, input_data_type, in_place);
51 _reference = compute_reference(input_shape, input_data_type);
52 _use_dynamic_shape = use_dynamic_shape;
Michalis Spyroue9362622018-11-23 17:41:37 +000053 }
54
55protected:
56 template <typename U>
Usama Ariff6e475c2019-05-10 12:06:28 +010057 void fill(U &&tensor, int i, DataType data_type)
Michalis Spyroue9362622018-11-23 17:41:37 +000058 {
Giorgio Arena4bdd1772020-12-17 16:47:07 +000059 using FloatType = typename std::conditional < std::is_same<T, half>::value || std::is_floating_point<T>::value, T, float >::type;
Giorgio Arena33b103b2021-01-08 10:37:15 +000060 using FloatDistributionType = typename std::conditional<std::is_same<T, half>::value, arm_compute::utils::uniform_real_distribution_16bit<T>, std::uniform_real_distribution<FloatType>>::type;
Giorgio Arena4bdd1772020-12-17 16:47:07 +000061
Michalis Spyroue9362622018-11-23 17:41:37 +000062 switch(_op)
63 {
64 case ElementWiseUnary::EXP:
65 {
Giorgio Arena4bdd1772020-12-17 16:47:07 +000066 FloatDistributionType distribution{ FloatType(-1.0f), FloatType(1.0f) };
Michalis Spyroue9362622018-11-23 17:41:37 +000067 library->fill(tensor, distribution, i);
68 break;
69 }
70 case ElementWiseUnary::RSQRT:
71 {
Giorgio Arena4bdd1772020-12-17 16:47:07 +000072 FloatDistributionType distribution{ FloatType(1.0f), FloatType(2.0f) };
Michalis Spyroue9362622018-11-23 17:41:37 +000073 library->fill(tensor, distribution, i);
74 break;
75 }
Manuel Bottini6ac59922019-05-15 14:06:02 +010076 case ElementWiseUnary::ABS:
Usama Ariff6e475c2019-05-10 12:06:28 +010077 case ElementWiseUnary::NEG:
78 {
79 switch(data_type)
80 {
Usama Ariff6e475c2019-05-10 12:06:28 +010081 case DataType::F16:
82 {
Giorgio Arenaa8e2aeb2021-01-06 11:34:57 +000083 arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ -2.0f, 2.0f };
Giorgio Arena6aeb2172020-12-15 15:45:43 +000084 library->fill(tensor, distribution, i);
85 break;
86 }
87 case DataType::F32:
88 {
Giorgio Arena4bdd1772020-12-17 16:47:07 +000089 FloatDistributionType distribution{ FloatType(-2.0f), FloatType(2.0f) };
Usama Ariff6e475c2019-05-10 12:06:28 +010090 library->fill(tensor, distribution, i);
91 break;
92 }
93 case DataType::S32:
94 {
95 std::uniform_int_distribution<int32_t> distribution(-100, 100);
96 library->fill(tensor, distribution, i);
97 break;
98 }
99 default:
100 ARM_COMPUTE_ERROR("DataType for Elementwise Negation Not implemented");
101 }
102 break;
103 }
Usama Arifc255aa72019-05-13 16:26:29 +0100104 case ElementWiseUnary::LOG:
105 {
Giorgio Arena4bdd1772020-12-17 16:47:07 +0000106 FloatDistributionType distribution{ FloatType(0.0000001f), FloatType(100.0f) };
Usama Arifc255aa72019-05-13 16:26:29 +0100107 library->fill(tensor, distribution, i);
108 break;
109 }
Michalis Spyrou0af44182019-05-17 14:04:47 +0100110 case ElementWiseUnary::SIN:
111 {
Giorgio Arena4bdd1772020-12-17 16:47:07 +0000112 FloatDistributionType distribution{ FloatType(-100.00f), FloatType(100.00f) };
Michalis Spyrou0af44182019-05-17 14:04:47 +0100113 library->fill(tensor, distribution, i);
114 break;
115 }
Usama Arif0a5a57a2019-05-23 14:20:33 +0100116 case ElementWiseUnary::ROUND:
117 {
Giorgio Arena4bdd1772020-12-17 16:47:07 +0000118 FloatDistributionType distribution{ FloatType(100.0f), FloatType(-100.0f) };
Usama Arif0a5a57a2019-05-23 14:20:33 +0100119 library->fill(tensor, distribution, i);
120 break;
121 }
Michalis Spyroue9362622018-11-23 17:41:37 +0000122 default:
123 ARM_COMPUTE_ERROR("Not implemented");
124 }
125 }
126
Manuel Bottini80feed52020-06-03 13:20:41 +0100127 TensorType compute_target(const TensorShape &shape, DataType data_type, bool in_place)
Michalis Spyroue9362622018-11-23 17:41:37 +0000128 {
129 // Create tensors
130 TensorType src = create_tensor<TensorType>(shape, data_type);
131 TensorType dst = create_tensor<TensorType>(shape, data_type);
132
Manuel Bottini80feed52020-06-03 13:20:41 +0100133 TensorType *actual_dst = in_place ? &src : &dst;
134
Sang-Hoon Park668ccdc2021-02-03 10:32:59 +0000135 // if _use_dynamic_shape is true, this fixture will test scenario for dynamic shapes.
136 // - At configure time, all input tensors are marked as dynamic using set_tensor_dynamic()
137 // - After configure, tensors are marked as static for run using set_tensor_static()
138 // - The tensors with static shape are given to run()
139 if(_use_dynamic_shape)
140 {
141 set_tensor_dynamic(src);
142 }
143
Michalis Spyroue9362622018-11-23 17:41:37 +0000144 // Create and configure function
145 FunctionType elwiseunary_layer;
Manuel Bottini80feed52020-06-03 13:20:41 +0100146 elwiseunary_layer.configure(&src, actual_dst);
Michalis Spyroue9362622018-11-23 17:41:37 +0000147
Sang-Hoon Park668ccdc2021-02-03 10:32:59 +0000148 if(_use_dynamic_shape)
149 {
150 set_tensor_static(src);
151 }
152
Michalis Spyroue9362622018-11-23 17:41:37 +0000153 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
Michalis Spyroue9362622018-11-23 17:41:37 +0000154 src.allocator()->allocate();
Michalis Spyroue9362622018-11-23 17:41:37 +0000155 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
Manuel Bottini80feed52020-06-03 13:20:41 +0100156 if(!in_place)
157 {
158 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
159 dst.allocator()->allocate();
160 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
161 }
Michalis Spyroue9362622018-11-23 17:41:37 +0000162
163 // Fill tensors
Usama Ariff6e475c2019-05-10 12:06:28 +0100164 fill(AccessorType(src), 0, data_type);
Michalis Spyroue9362622018-11-23 17:41:37 +0000165
166 // Compute function
167 elwiseunary_layer.run();
168
Manuel Bottini80feed52020-06-03 13:20:41 +0100169 if(in_place)
170 {
171 return src;
172 }
173 else
174 {
175 return dst;
176 }
Michalis Spyroue9362622018-11-23 17:41:37 +0000177 }
178
179 SimpleTensor<T> compute_reference(const TensorShape &shape, DataType data_type)
180 {
181 // Create reference
182 SimpleTensor<T> src{ shape, data_type };
183
184 // Fill reference
Usama Ariff6e475c2019-05-10 12:06:28 +0100185 fill(src, 0, data_type);
Michalis Spyroue9362622018-11-23 17:41:37 +0000186
187 return reference::elementwise_unary<T>(src, _op);
188 }
189
190 TensorType _target{};
191 SimpleTensor<T> _reference{};
192 ElementWiseUnary _op{};
Sang-Hoon Park668ccdc2021-02-03 10:32:59 +0000193 bool _use_dynamic_shape{ false };
Michalis Spyroue9362622018-11-23 17:41:37 +0000194};
195
196template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
197class RsqrtValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>
198{
199public:
200 template <typename...>
201 void setup(const TensorShape &shape, DataType data_type)
202 {
Manuel Bottini80feed52020-06-03 13:20:41 +0100203 ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, false, ElementWiseUnary::RSQRT);
Michalis Spyroue9362622018-11-23 17:41:37 +0000204 }
205};
206
207template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Sang-Hoon Park668ccdc2021-02-03 10:32:59 +0000208class RsqrtDynamicShapeValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>
209{
210public:
211 template <typename...>
212 void setup(const TensorShape &shape, DataType data_type)
213 {
214 ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, false, ElementWiseUnary::RSQRT, true);
215 }
216};
217
218template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Michalis Spyroue9362622018-11-23 17:41:37 +0000219class ExpValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>
220{
221public:
222 template <typename...>
223 void setup(const TensorShape &shape, DataType data_type)
224 {
Manuel Bottini80feed52020-06-03 13:20:41 +0100225 ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, false, ElementWiseUnary::EXP);
Michalis Spyroue9362622018-11-23 17:41:37 +0000226 }
227};
Usama Ariff6e475c2019-05-10 12:06:28 +0100228
229template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
230class NegValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>
231{
232public:
233 template <typename...>
234 void setup(const TensorShape &shape, DataType data_type)
235 {
Manuel Bottini80feed52020-06-03 13:20:41 +0100236 ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, false, ElementWiseUnary::NEG);
237 }
238};
239
240template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
241class NegValidationInPlaceFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>
242{
243public:
244 template <typename...>
245 void setup(const TensorShape &shape, DataType data_type, bool in_place)
246 {
247 ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, in_place, ElementWiseUnary::NEG);
Usama Ariff6e475c2019-05-10 12:06:28 +0100248 }
249};
Usama Arifc255aa72019-05-13 16:26:29 +0100250
251template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
252class LogValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>
253{
254public:
255 template <typename...>
256 void setup(const TensorShape &shape, DataType data_type)
257 {
Manuel Bottini80feed52020-06-03 13:20:41 +0100258 ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, false, ElementWiseUnary::LOG);
Usama Arifc255aa72019-05-13 16:26:29 +0100259 }
260};
Manuel Bottini6ac59922019-05-15 14:06:02 +0100261
262template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
263class AbsValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>
264{
265public:
266 template <typename...>
267 void setup(const TensorShape &shape, DataType data_type)
268 {
Manuel Bottini80feed52020-06-03 13:20:41 +0100269 ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, false, ElementWiseUnary::ABS);
Manuel Bottini6ac59922019-05-15 14:06:02 +0100270 }
271};
Michalis Spyrou0af44182019-05-17 14:04:47 +0100272
273template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
274class SinValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>
275{
276public:
277 template <typename...>
278 void setup(const TensorShape &shape, DataType data_type)
279 {
Manuel Bottini80feed52020-06-03 13:20:41 +0100280 ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, false, ElementWiseUnary::SIN);
Michalis Spyrou0af44182019-05-17 14:04:47 +0100281 }
282};
Usama Arif0a5a57a2019-05-23 14:20:33 +0100283
284template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
285class RoundValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>
286{
287public:
288 template <typename...>
289 void setup(const TensorShape &shape, DataType data_type)
290 {
Manuel Bottini80feed52020-06-03 13:20:41 +0100291 ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, false, ElementWiseUnary::ROUND);
Usama Arif0a5a57a2019-05-23 14:20:33 +0100292 }
293};
Michalis Spyroue9362622018-11-23 17:41:37 +0000294} // namespace validation
295} // namespace test
296} // namespace arm_compute
297#endif /* ARM_COMPUTE_TEST_ELEMENTWISE_UNARY_FIXTURE */