blob: ba131630a3d57e40422d6b37244c0695696ace38 [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 }
Usama Ariff6e475c2019-05-10 12:06:28 +010072 case ElementWiseUnary::NEG:
73 {
74 switch(data_type)
75 {
76 case DataType::F32:
77 case DataType::F16:
78 {
79 std::uniform_real_distribution<> distribution(-2.0f, 2.0f);
80 library->fill(tensor, distribution, i);
81 break;
82 }
83 case DataType::S32:
84 {
85 std::uniform_int_distribution<int32_t> distribution(-100, 100);
86 library->fill(tensor, distribution, i);
87 break;
88 }
89 default:
90 ARM_COMPUTE_ERROR("DataType for Elementwise Negation Not implemented");
91 }
92 break;
93 }
Michalis Spyroue9362622018-11-23 17:41:37 +000094 default:
95 ARM_COMPUTE_ERROR("Not implemented");
96 }
97 }
98
99 TensorType compute_target(const TensorShape &shape, DataType data_type)
100 {
101 // Create tensors
102 TensorType src = create_tensor<TensorType>(shape, data_type);
103 TensorType dst = create_tensor<TensorType>(shape, data_type);
104
105 // Create and configure function
106 FunctionType elwiseunary_layer;
107
108 elwiseunary_layer.configure(&src, &dst);
109
110 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
111 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
112
113 // Allocate tensors
114 src.allocator()->allocate();
115 dst.allocator()->allocate();
116 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
117 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
118
119 // Fill tensors
Usama Ariff6e475c2019-05-10 12:06:28 +0100120 fill(AccessorType(src), 0, data_type);
Michalis Spyroue9362622018-11-23 17:41:37 +0000121
122 // Compute function
123 elwiseunary_layer.run();
124
125 return dst;
126 }
127
128 SimpleTensor<T> compute_reference(const TensorShape &shape, DataType data_type)
129 {
130 // Create reference
131 SimpleTensor<T> src{ shape, data_type };
132
133 // Fill reference
Usama Ariff6e475c2019-05-10 12:06:28 +0100134 fill(src, 0, data_type);
Michalis Spyroue9362622018-11-23 17:41:37 +0000135
136 return reference::elementwise_unary<T>(src, _op);
137 }
138
139 TensorType _target{};
140 SimpleTensor<T> _reference{};
141 ElementWiseUnary _op{};
142};
143
144template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
145class RsqrtValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>
146{
147public:
148 template <typename...>
149 void setup(const TensorShape &shape, DataType data_type)
150 {
151 ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, ElementWiseUnary::RSQRT);
152 }
153};
154
155template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
156class ExpValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>
157{
158public:
159 template <typename...>
160 void setup(const TensorShape &shape, DataType data_type)
161 {
162 ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, ElementWiseUnary::EXP);
163 }
164};
Usama Ariff6e475c2019-05-10 12:06:28 +0100165
166template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
167class NegValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>
168{
169public:
170 template <typename...>
171 void setup(const TensorShape &shape, DataType data_type)
172 {
173 ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, ElementWiseUnary::NEG);
174 }
175};
Michalis Spyroue9362622018-11-23 17:41:37 +0000176} // namespace validation
177} // namespace test
178} // namespace arm_compute
179#endif /* ARM_COMPUTE_TEST_ELEMENTWISE_UNARY_FIXTURE */