blob: 37da3b1405472294d9efe8a8ea6dd87bbf9e0776 [file] [log] [blame]
Michalis Spyroue9362622018-11-23 17:41:37 +00001/*
Usama Ariff6e475c2019-05-10 12:06:28 +01002 * Copyright (c) 2018-2019 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...>
47 void setup(TensorShape shape, DataType data_type, ElementWiseUnary op)
48 {
49 _op = op;
50 _target = compute_target(shape, data_type);
51 _reference = compute_reference(shape, data_type);
52 }
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 {
77 case DataType::F32:
78 case DataType::F16:
79 {
80 std::uniform_real_distribution<> distribution(-2.0f, 2.0f);
81 library->fill(tensor, distribution, i);
82 break;
83 }
84 case DataType::S32:
85 {
86 std::uniform_int_distribution<int32_t> distribution(-100, 100);
87 library->fill(tensor, distribution, i);
88 break;
89 }
90 default:
91 ARM_COMPUTE_ERROR("DataType for Elementwise Negation Not implemented");
92 }
93 break;
94 }
Usama Arifc255aa72019-05-13 16:26:29 +010095 case ElementWiseUnary::LOG:
96 {
97 std::uniform_real_distribution<> distribution(0.0000001f, 100.0f);
98 library->fill(tensor, distribution, i);
99 break;
100 }
Michalis Spyroue9362622018-11-23 17:41:37 +0000101 default:
102 ARM_COMPUTE_ERROR("Not implemented");
103 }
104 }
105
106 TensorType compute_target(const TensorShape &shape, DataType data_type)
107 {
108 // Create tensors
109 TensorType src = create_tensor<TensorType>(shape, data_type);
110 TensorType dst = create_tensor<TensorType>(shape, data_type);
111
112 // Create and configure function
113 FunctionType elwiseunary_layer;
114
115 elwiseunary_layer.configure(&src, &dst);
116
117 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
118 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
119
120 // Allocate tensors
121 src.allocator()->allocate();
122 dst.allocator()->allocate();
123 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
124 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
125
126 // Fill tensors
Usama Ariff6e475c2019-05-10 12:06:28 +0100127 fill(AccessorType(src), 0, data_type);
Michalis Spyroue9362622018-11-23 17:41:37 +0000128
129 // Compute function
130 elwiseunary_layer.run();
131
132 return dst;
133 }
134
135 SimpleTensor<T> compute_reference(const TensorShape &shape, DataType data_type)
136 {
137 // Create reference
138 SimpleTensor<T> src{ shape, data_type };
139
140 // Fill reference
Usama Ariff6e475c2019-05-10 12:06:28 +0100141 fill(src, 0, data_type);
Michalis Spyroue9362622018-11-23 17:41:37 +0000142
143 return reference::elementwise_unary<T>(src, _op);
144 }
145
146 TensorType _target{};
147 SimpleTensor<T> _reference{};
148 ElementWiseUnary _op{};
149};
150
151template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
152class RsqrtValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>
153{
154public:
155 template <typename...>
156 void setup(const TensorShape &shape, DataType data_type)
157 {
158 ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, ElementWiseUnary::RSQRT);
159 }
160};
161
162template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
163class ExpValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>
164{
165public:
166 template <typename...>
167 void setup(const TensorShape &shape, DataType data_type)
168 {
169 ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, ElementWiseUnary::EXP);
170 }
171};
Usama Ariff6e475c2019-05-10 12:06:28 +0100172
173template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
174class NegValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>
175{
176public:
177 template <typename...>
178 void setup(const TensorShape &shape, DataType data_type)
179 {
180 ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, ElementWiseUnary::NEG);
181 }
182};
Usama Arifc255aa72019-05-13 16:26:29 +0100183
184template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
185class LogValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>
186{
187public:
188 template <typename...>
189 void setup(const TensorShape &shape, DataType data_type)
190 {
191 ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, ElementWiseUnary::LOG);
192 }
193};
Manuel Bottini6ac59922019-05-15 14:06:02 +0100194
195template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
196class AbsValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>
197{
198public:
199 template <typename...>
200 void setup(const TensorShape &shape, DataType data_type)
201 {
202 ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, ElementWiseUnary::ABS);
203 }
204};
Michalis Spyroue9362622018-11-23 17:41:37 +0000205} // namespace validation
206} // namespace test
207} // namespace arm_compute
208#endif /* ARM_COMPUTE_TEST_ELEMENTWISE_UNARY_FIXTURE */