blob: 8cffef48f671f0589294dbd293c3d9694532597a [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...>
Manuel Bottini80feed52020-06-03 13:20:41 +010047 void setup(TensorShape input_shape, DataType input_data_type, bool in_place, ElementWiseUnary op)
Michalis Spyroue9362622018-11-23 17:41:37 +000048 {
49 _op = op;
Manuel Bottini80feed52020-06-03 13:20:41 +010050 _target = compute_target(input_shape, input_data_type, in_place);
Manuel Bottinied753262019-05-15 15:30:47 +010051 _reference = compute_reference(input_shape, input_data_type);
Michalis Spyroue9362622018-11-23 17:41:37 +000052 }
53
54protected:
55 template <typename U>
Usama Ariff6e475c2019-05-10 12:06:28 +010056 void fill(U &&tensor, int i, DataType data_type)
Michalis Spyroue9362622018-11-23 17:41:37 +000057 {
Giorgio Arena4bdd1772020-12-17 16:47:07 +000058 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 +000059 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 +000060
Michalis Spyroue9362622018-11-23 17:41:37 +000061 switch(_op)
62 {
63 case ElementWiseUnary::EXP:
64 {
Giorgio Arena4bdd1772020-12-17 16:47:07 +000065 FloatDistributionType distribution{ FloatType(-1.0f), FloatType(1.0f) };
Michalis Spyroue9362622018-11-23 17:41:37 +000066 library->fill(tensor, distribution, i);
67 break;
68 }
69 case ElementWiseUnary::RSQRT:
70 {
Giorgio Arena4bdd1772020-12-17 16:47:07 +000071 FloatDistributionType distribution{ FloatType(1.0f), FloatType(2.0f) };
Michalis Spyroue9362622018-11-23 17:41:37 +000072 library->fill(tensor, distribution, i);
73 break;
74 }
Manuel Bottini6ac59922019-05-15 14:06:02 +010075 case ElementWiseUnary::ABS:
Usama Ariff6e475c2019-05-10 12:06:28 +010076 case ElementWiseUnary::NEG:
77 {
78 switch(data_type)
79 {
Usama Ariff6e475c2019-05-10 12:06:28 +010080 case DataType::F16:
81 {
Giorgio Arenaa8e2aeb2021-01-06 11:34:57 +000082 arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ -2.0f, 2.0f };
Giorgio Arena6aeb2172020-12-15 15:45:43 +000083 library->fill(tensor, distribution, i);
84 break;
85 }
86 case DataType::F32:
87 {
Giorgio Arena4bdd1772020-12-17 16:47:07 +000088 FloatDistributionType distribution{ FloatType(-2.0f), FloatType(2.0f) };
Usama Ariff6e475c2019-05-10 12:06:28 +010089 library->fill(tensor, distribution, i);
90 break;
91 }
92 case DataType::S32:
93 {
94 std::uniform_int_distribution<int32_t> distribution(-100, 100);
95 library->fill(tensor, distribution, i);
96 break;
97 }
98 default:
99 ARM_COMPUTE_ERROR("DataType for Elementwise Negation Not implemented");
100 }
101 break;
102 }
Usama Arifc255aa72019-05-13 16:26:29 +0100103 case ElementWiseUnary::LOG:
104 {
Giorgio Arena4bdd1772020-12-17 16:47:07 +0000105 FloatDistributionType distribution{ FloatType(0.0000001f), FloatType(100.0f) };
Usama Arifc255aa72019-05-13 16:26:29 +0100106 library->fill(tensor, distribution, i);
107 break;
108 }
Michalis Spyrou0af44182019-05-17 14:04:47 +0100109 case ElementWiseUnary::SIN:
110 {
Giorgio Arena4bdd1772020-12-17 16:47:07 +0000111 FloatDistributionType distribution{ FloatType(-100.00f), FloatType(100.00f) };
Michalis Spyrou0af44182019-05-17 14:04:47 +0100112 library->fill(tensor, distribution, i);
113 break;
114 }
Usama Arif0a5a57a2019-05-23 14:20:33 +0100115 case ElementWiseUnary::ROUND:
116 {
Giorgio Arena4bdd1772020-12-17 16:47:07 +0000117 FloatDistributionType distribution{ FloatType(100.0f), FloatType(-100.0f) };
Usama Arif0a5a57a2019-05-23 14:20:33 +0100118 library->fill(tensor, distribution, i);
119 break;
120 }
Michalis Spyroue9362622018-11-23 17:41:37 +0000121 default:
122 ARM_COMPUTE_ERROR("Not implemented");
123 }
124 }
125
Manuel Bottini80feed52020-06-03 13:20:41 +0100126 TensorType compute_target(const TensorShape &shape, DataType data_type, bool in_place)
Michalis Spyroue9362622018-11-23 17:41:37 +0000127 {
128 // Create tensors
129 TensorType src = create_tensor<TensorType>(shape, data_type);
130 TensorType dst = create_tensor<TensorType>(shape, data_type);
131
Manuel Bottini80feed52020-06-03 13:20:41 +0100132 TensorType *actual_dst = in_place ? &src : &dst;
133
Michalis Spyroue9362622018-11-23 17:41:37 +0000134 // Create and configure function
135 FunctionType elwiseunary_layer;
Manuel Bottini80feed52020-06-03 13:20:41 +0100136 elwiseunary_layer.configure(&src, actual_dst);
Michalis Spyroue9362622018-11-23 17:41:37 +0000137
138 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
Michalis Spyroue9362622018-11-23 17:41:37 +0000139 src.allocator()->allocate();
Michalis Spyroue9362622018-11-23 17:41:37 +0000140 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
Manuel Bottini80feed52020-06-03 13:20:41 +0100141 if(!in_place)
142 {
143 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
144 dst.allocator()->allocate();
145 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
146 }
Michalis Spyroue9362622018-11-23 17:41:37 +0000147
148 // Fill tensors
Usama Ariff6e475c2019-05-10 12:06:28 +0100149 fill(AccessorType(src), 0, data_type);
Michalis Spyroue9362622018-11-23 17:41:37 +0000150
151 // Compute function
152 elwiseunary_layer.run();
153
Manuel Bottini80feed52020-06-03 13:20:41 +0100154 if(in_place)
155 {
156 return src;
157 }
158 else
159 {
160 return dst;
161 }
Michalis Spyroue9362622018-11-23 17:41:37 +0000162 }
163
164 SimpleTensor<T> compute_reference(const TensorShape &shape, DataType data_type)
165 {
166 // Create reference
167 SimpleTensor<T> src{ shape, data_type };
168
169 // Fill reference
Usama Ariff6e475c2019-05-10 12:06:28 +0100170 fill(src, 0, data_type);
Michalis Spyroue9362622018-11-23 17:41:37 +0000171
172 return reference::elementwise_unary<T>(src, _op);
173 }
174
175 TensorType _target{};
176 SimpleTensor<T> _reference{};
177 ElementWiseUnary _op{};
178};
179
180template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
181class RsqrtValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>
182{
183public:
184 template <typename...>
185 void setup(const TensorShape &shape, DataType data_type)
186 {
Manuel Bottini80feed52020-06-03 13:20:41 +0100187 ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, false, ElementWiseUnary::RSQRT);
Michalis Spyroue9362622018-11-23 17:41:37 +0000188 }
189};
190
191template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
192class ExpValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>
193{
194public:
195 template <typename...>
196 void setup(const TensorShape &shape, DataType data_type)
197 {
Manuel Bottini80feed52020-06-03 13:20:41 +0100198 ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, false, ElementWiseUnary::EXP);
Michalis Spyroue9362622018-11-23 17:41:37 +0000199 }
200};
Usama Ariff6e475c2019-05-10 12:06:28 +0100201
202template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
203class NegValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>
204{
205public:
206 template <typename...>
207 void setup(const TensorShape &shape, DataType data_type)
208 {
Manuel Bottini80feed52020-06-03 13:20:41 +0100209 ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, false, ElementWiseUnary::NEG);
210 }
211};
212
213template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
214class NegValidationInPlaceFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>
215{
216public:
217 template <typename...>
218 void setup(const TensorShape &shape, DataType data_type, bool in_place)
219 {
220 ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, in_place, ElementWiseUnary::NEG);
Usama Ariff6e475c2019-05-10 12:06:28 +0100221 }
222};
Usama Arifc255aa72019-05-13 16:26:29 +0100223
224template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
225class LogValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>
226{
227public:
228 template <typename...>
229 void setup(const TensorShape &shape, DataType data_type)
230 {
Manuel Bottini80feed52020-06-03 13:20:41 +0100231 ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, false, ElementWiseUnary::LOG);
Usama Arifc255aa72019-05-13 16:26:29 +0100232 }
233};
Manuel Bottini6ac59922019-05-15 14:06:02 +0100234
235template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
236class AbsValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>
237{
238public:
239 template <typename...>
240 void setup(const TensorShape &shape, DataType data_type)
241 {
Manuel Bottini80feed52020-06-03 13:20:41 +0100242 ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, false, ElementWiseUnary::ABS);
Manuel Bottini6ac59922019-05-15 14:06:02 +0100243 }
244};
Michalis Spyrou0af44182019-05-17 14:04:47 +0100245
246template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
247class SinValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>
248{
249public:
250 template <typename...>
251 void setup(const TensorShape &shape, DataType data_type)
252 {
Manuel Bottini80feed52020-06-03 13:20:41 +0100253 ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, false, ElementWiseUnary::SIN);
Michalis Spyrou0af44182019-05-17 14:04:47 +0100254 }
255};
Usama Arif0a5a57a2019-05-23 14:20:33 +0100256
257template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
258class RoundValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>
259{
260public:
261 template <typename...>
262 void setup(const TensorShape &shape, DataType data_type)
263 {
Manuel Bottini80feed52020-06-03 13:20:41 +0100264 ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, false, ElementWiseUnary::ROUND);
Usama Arif0a5a57a2019-05-23 14:20:33 +0100265 }
266};
Michalis Spyroue9362622018-11-23 17:41:37 +0000267} // namespace validation
268} // namespace test
269} // namespace arm_compute
270#endif /* ARM_COMPUTE_TEST_ELEMENTWISE_UNARY_FIXTURE */