blob: b1d54fd940c706298863c680e3d862bc69c78e24 [file] [log] [blame]
Michalis Spyroue9362622018-11-23 17:41:37 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 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 {
58 switch(_op)
59 {
60 case ElementWiseUnary::EXP:
61 {
62 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
63 library->fill(tensor, distribution, i);
64 break;
65 }
66 case ElementWiseUnary::RSQRT:
67 {
68 std::uniform_real_distribution<> distribution(1.0f, 2.0f);
69 library->fill(tensor, distribution, i);
70 break;
71 }
Manuel Bottini6ac59922019-05-15 14:06:02 +010072 case ElementWiseUnary::ABS:
Usama Ariff6e475c2019-05-10 12:06:28 +010073 case ElementWiseUnary::NEG:
74 {
75 switch(data_type)
76 {
Usama Ariff6e475c2019-05-10 12:06:28 +010077 case DataType::F16:
78 {
Giorgio Arena6aeb2172020-12-15 15:45:43 +000079 arm_compute::utils::uniform_real_distribution_fp16 distribution{ half(-2.0f), half(2.0f) };
80 library->fill(tensor, distribution, i);
81 break;
82 }
83 case DataType::F32:
84 {
85 std::uniform_real_distribution<float> distribution(-2.0f, 2.0f);
Usama Ariff6e475c2019-05-10 12:06:28 +010086 library->fill(tensor, distribution, i);
87 break;
88 }
89 case DataType::S32:
90 {
91 std::uniform_int_distribution<int32_t> distribution(-100, 100);
92 library->fill(tensor, distribution, i);
93 break;
94 }
95 default:
96 ARM_COMPUTE_ERROR("DataType for Elementwise Negation Not implemented");
97 }
98 break;
99 }
Usama Arifc255aa72019-05-13 16:26:29 +0100100 case ElementWiseUnary::LOG:
101 {
102 std::uniform_real_distribution<> distribution(0.0000001f, 100.0f);
103 library->fill(tensor, distribution, i);
104 break;
105 }
Michalis Spyrou0af44182019-05-17 14:04:47 +0100106 case ElementWiseUnary::SIN:
107 {
Manuel Bottinied753262019-05-15 15:30:47 +0100108 std::uniform_real_distribution<> distribution(-100.00f, 100.00f);
Michalis Spyrou0af44182019-05-17 14:04:47 +0100109 library->fill(tensor, distribution, i);
110 break;
111 }
Usama Arif0a5a57a2019-05-23 14:20:33 +0100112 case ElementWiseUnary::ROUND:
113 {
114 std::uniform_real_distribution<> distribution(100.0f, -100.0f);
115 library->fill(tensor, distribution, i);
116 break;
117 }
Michalis Spyroue9362622018-11-23 17:41:37 +0000118 default:
119 ARM_COMPUTE_ERROR("Not implemented");
120 }
121 }
122
Manuel Bottini80feed52020-06-03 13:20:41 +0100123 TensorType compute_target(const TensorShape &shape, DataType data_type, bool in_place)
Michalis Spyroue9362622018-11-23 17:41:37 +0000124 {
125 // Create tensors
126 TensorType src = create_tensor<TensorType>(shape, data_type);
127 TensorType dst = create_tensor<TensorType>(shape, data_type);
128
Manuel Bottini80feed52020-06-03 13:20:41 +0100129 TensorType *actual_dst = in_place ? &src : &dst;
130
Michalis Spyroue9362622018-11-23 17:41:37 +0000131 // Create and configure function
132 FunctionType elwiseunary_layer;
Manuel Bottini80feed52020-06-03 13:20:41 +0100133 elwiseunary_layer.configure(&src, actual_dst);
Michalis Spyroue9362622018-11-23 17:41:37 +0000134
135 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
Michalis Spyroue9362622018-11-23 17:41:37 +0000136 src.allocator()->allocate();
Michalis Spyroue9362622018-11-23 17:41:37 +0000137 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
Manuel Bottini80feed52020-06-03 13:20:41 +0100138 if(!in_place)
139 {
140 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
141 dst.allocator()->allocate();
142 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
143 }
Michalis Spyroue9362622018-11-23 17:41:37 +0000144
145 // Fill tensors
Usama Ariff6e475c2019-05-10 12:06:28 +0100146 fill(AccessorType(src), 0, data_type);
Michalis Spyroue9362622018-11-23 17:41:37 +0000147
148 // Compute function
149 elwiseunary_layer.run();
150
Manuel Bottini80feed52020-06-03 13:20:41 +0100151 if(in_place)
152 {
153 return src;
154 }
155 else
156 {
157 return dst;
158 }
Michalis Spyroue9362622018-11-23 17:41:37 +0000159 }
160
161 SimpleTensor<T> compute_reference(const TensorShape &shape, DataType data_type)
162 {
163 // Create reference
164 SimpleTensor<T> src{ shape, data_type };
165
166 // Fill reference
Usama Ariff6e475c2019-05-10 12:06:28 +0100167 fill(src, 0, data_type);
Michalis Spyroue9362622018-11-23 17:41:37 +0000168
169 return reference::elementwise_unary<T>(src, _op);
170 }
171
172 TensorType _target{};
173 SimpleTensor<T> _reference{};
174 ElementWiseUnary _op{};
175};
176
177template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
178class RsqrtValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>
179{
180public:
181 template <typename...>
182 void setup(const TensorShape &shape, DataType data_type)
183 {
Manuel Bottini80feed52020-06-03 13:20:41 +0100184 ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, false, ElementWiseUnary::RSQRT);
Michalis Spyroue9362622018-11-23 17:41:37 +0000185 }
186};
187
188template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
189class ExpValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>
190{
191public:
192 template <typename...>
193 void setup(const TensorShape &shape, DataType data_type)
194 {
Manuel Bottini80feed52020-06-03 13:20:41 +0100195 ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, false, ElementWiseUnary::EXP);
Michalis Spyroue9362622018-11-23 17:41:37 +0000196 }
197};
Usama Ariff6e475c2019-05-10 12:06:28 +0100198
199template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
200class NegValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>
201{
202public:
203 template <typename...>
204 void setup(const TensorShape &shape, DataType data_type)
205 {
Manuel Bottini80feed52020-06-03 13:20:41 +0100206 ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, false, ElementWiseUnary::NEG);
207 }
208};
209
210template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
211class NegValidationInPlaceFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>
212{
213public:
214 template <typename...>
215 void setup(const TensorShape &shape, DataType data_type, bool in_place)
216 {
217 ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, in_place, ElementWiseUnary::NEG);
Usama Ariff6e475c2019-05-10 12:06:28 +0100218 }
219};
Usama Arifc255aa72019-05-13 16:26:29 +0100220
221template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
222class LogValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>
223{
224public:
225 template <typename...>
226 void setup(const TensorShape &shape, DataType data_type)
227 {
Manuel Bottini80feed52020-06-03 13:20:41 +0100228 ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, false, ElementWiseUnary::LOG);
Usama Arifc255aa72019-05-13 16:26:29 +0100229 }
230};
Manuel Bottini6ac59922019-05-15 14:06:02 +0100231
232template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
233class AbsValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>
234{
235public:
236 template <typename...>
237 void setup(const TensorShape &shape, DataType data_type)
238 {
Manuel Bottini80feed52020-06-03 13:20:41 +0100239 ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, false, ElementWiseUnary::ABS);
Manuel Bottini6ac59922019-05-15 14:06:02 +0100240 }
241};
Michalis Spyrou0af44182019-05-17 14:04:47 +0100242
243template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
244class SinValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>
245{
246public:
247 template <typename...>
248 void setup(const TensorShape &shape, DataType data_type)
249 {
Manuel Bottini80feed52020-06-03 13:20:41 +0100250 ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, false, ElementWiseUnary::SIN);
Michalis Spyrou0af44182019-05-17 14:04:47 +0100251 }
252};
Usama Arif0a5a57a2019-05-23 14:20:33 +0100253
254template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
255class RoundValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>
256{
257public:
258 template <typename...>
259 void setup(const TensorShape &shape, DataType data_type)
260 {
Manuel Bottini80feed52020-06-03 13:20:41 +0100261 ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, false, ElementWiseUnary::ROUND);
Usama Arif0a5a57a2019-05-23 14:20:33 +0100262 }
263};
Michalis Spyroue9362622018-11-23 17:41:37 +0000264} // namespace validation
265} // namespace test
266} // namespace arm_compute
267#endif /* ARM_COMPUTE_TEST_ELEMENTWISE_UNARY_FIXTURE */