blob: d9f26b7368fd51a77d75caf05d1e568e296eee9c [file] [log] [blame]
Moritz Pflanzer572ade72017-07-21 17:36:33 +01001/*
Georgios Pinitas4b3fba12019-06-04 17:31:46 +01002 * Copyright (c) 2017-2019 ARM Limited.
Moritz Pflanzer572ade72017-07-21 17:36:33 +01003 *
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_ACTIVATION_LAYER_FIXTURE
25#define ARM_COMPUTE_TEST_ACTIVATION_LAYER_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
Moritz Pflanzer572ade72017-07-21 17:36:33 +010029#include "tests/AssetsLibrary.h"
30#include "tests/Globals.h"
31#include "tests/IAccessor.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010032#include "tests/framework/Asserts.h"
33#include "tests/framework/Fixture.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010034#include "tests/validation/Helpers.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000035#include "tests/validation/reference/ActivationLayer.h"
Moritz Pflanzer572ade72017-07-21 17:36:33 +010036
37#include <random>
38
39namespace arm_compute
40{
41namespace test
42{
43namespace validation
44{
45template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Michel Iwaniec66cc12f2017-12-07 17:26:40 +000046class ActivationValidationGenericFixture : public framework::Fixture
Moritz Pflanzer572ade72017-07-21 17:36:33 +010047{
48public:
49 template <typename...>
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010050 void setup(TensorShape shape, bool in_place, ActivationLayerInfo::ActivationFunction function, float alpha_beta, DataType data_type, QuantizationInfo quantization_info)
Moritz Pflanzer572ade72017-07-21 17:36:33 +010051 {
Moritz Pflanzer572ade72017-07-21 17:36:33 +010052 ActivationLayerInfo info(function, alpha_beta, alpha_beta);
53
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010054 _in_place = in_place;
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010055 _data_type = data_type;
giuros01c9573f32019-06-20 10:30:17 +010056 _output_quantization_info = calculate_output_quantization_info(_data_type, info, quantization_info);
57 _input_quantization_info = in_place ? _output_quantization_info : quantization_info;
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010058
giuros01c9573f32019-06-20 10:30:17 +010059 _function = function;
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010060 _target = compute_target(shape, info);
61 _reference = compute_reference(shape, info);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010062 }
63
64protected:
65 template <typename U>
66 void fill(U &&tensor)
67 {
68 if(is_data_type_float(_data_type))
69 {
70 float min_bound = 0;
71 float max_bound = 0;
72 std::tie(min_bound, max_bound) = get_activation_layer_test_bounds<T>(_function, _data_type);
73 std::uniform_real_distribution<> distribution(min_bound, max_bound);
74 library->fill(tensor, distribution, 0);
75 }
Manuel Bottini30dbeef2019-06-26 16:23:03 +010076 else if(is_data_type_quantized(tensor.data_type()))
Michel Iwaniec66cc12f2017-12-07 17:26:40 +000077 {
78 library->fill_tensor_uniform(tensor, 0);
79 }
Moritz Pflanzer572ade72017-07-21 17:36:33 +010080 else
81 {
82 int min_bound = 0;
83 int max_bound = 0;
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010084 std::tie(min_bound, max_bound) = get_activation_layer_test_bounds<T>(_function, _data_type);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010085 std::uniform_int_distribution<> distribution(min_bound, max_bound);
86 library->fill(tensor, distribution, 0);
87 }
88 }
89
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010090 TensorType compute_target(const TensorShape &shape, ActivationLayerInfo info)
Moritz Pflanzer572ade72017-07-21 17:36:33 +010091 {
92 // Create tensors
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010093 TensorType src = create_tensor<TensorType>(shape, _data_type, 1, _input_quantization_info);
94 TensorType dst = create_tensor<TensorType>(shape, _data_type, 1, _output_quantization_info);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010095
96 // Create and configure function
97 FunctionType act_layer;
98
Manuel Bottini30dbeef2019-06-26 16:23:03 +010099 TensorType *dst_ptr = _in_place ? nullptr : &dst;
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100100
101 act_layer.configure(&src, dst_ptr, info);
102
103 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
104 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
105
106 // Allocate tensors
107 src.allocator()->allocate();
108 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
109
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100110 if(!_in_place)
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100111 {
112 dst.allocator()->allocate();
113 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
114 }
115
116 // Fill tensors
117 fill(AccessorType(src));
118
119 // Compute function
120 act_layer.run();
121
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100122 if(_in_place)
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100123 {
124 return src;
125 }
126 else
127 {
128 return dst;
129 }
130 }
131
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100132 SimpleTensor<T> compute_reference(const TensorShape &shape, ActivationLayerInfo info)
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100133 {
134 // Create reference
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100135 SimpleTensor<T> src{ shape, _data_type, 1, _input_quantization_info };
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100136
137 // Fill reference
138 fill(src);
139
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100140 return reference::activation_layer<T>(src, info, _output_quantization_info);
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100141 }
142
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100143private:
giuros01c9573f32019-06-20 10:30:17 +0100144 QuantizationInfo calculate_output_quantization_info(DataType dt, const ActivationLayerInfo &act_info, const QuantizationInfo &default_qinfo)
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100145 {
giuros01c9573f32019-06-20 10:30:17 +0100146 auto qasymm8_max = float(std::numeric_limits<uint8_t>::max()) + 1.f;
147 auto qsymm16_max = float(std::numeric_limits<int16_t>::max()) + 1.f;
148
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100149 switch(act_info.activation())
150 {
151 case ActivationLayerInfo::ActivationFunction::TANH:
giuros01c9573f32019-06-20 10:30:17 +0100152 if(dt == DataType::QSYMM16)
153 {
154 return QuantizationInfo(1.f / qsymm16_max, 0);
155 }
156 else if(dt == DataType::QASYMM8)
157 {
158 return QuantizationInfo(1.f / (0.5 * qasymm8_max), int(0.5 * qasymm8_max));
159 }
160 else
161 {
162 return default_qinfo;
163 }
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100164 case ActivationLayerInfo::ActivationFunction::LOGISTIC:
giuros01c9573f32019-06-20 10:30:17 +0100165 if(dt == DataType::QSYMM16)
166 {
167 return QuantizationInfo(1.f / qsymm16_max, 0);
168 }
169 else if(dt == DataType::QASYMM8)
170 {
171 return QuantizationInfo(1.f / qasymm8_max, 0);
172 }
173 else
174 {
175 return default_qinfo;
176 }
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100177 default:
178 return default_qinfo;
179 }
180 }
181
182protected:
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100183 TensorType _target{};
184 SimpleTensor<T> _reference{};
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100185 bool _in_place{};
186 QuantizationInfo _input_quantization_info{};
187 QuantizationInfo _output_quantization_info{};
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100188 DataType _data_type{};
189 ActivationLayerInfo::ActivationFunction _function{};
190};
191
192template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Michel Iwaniec66cc12f2017-12-07 17:26:40 +0000193class ActivationValidationFixture : public ActivationValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100194{
195public:
196 template <typename...>
197 void setup(TensorShape shape, bool in_place, ActivationLayerInfo::ActivationFunction function, float alpha_beta, DataType data_type)
198 {
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100199 ActivationValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, in_place, function, alpha_beta, data_type, QuantizationInfo());
Michel Iwaniec66cc12f2017-12-07 17:26:40 +0000200 }
201};
202
203template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
204class ActivationValidationQuantizedFixture : public ActivationValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
205{
206public:
207 template <typename...>
208 void setup(TensorShape shape, bool in_place, ActivationLayerInfo::ActivationFunction function, float alpha_beta, DataType data_type, QuantizationInfo quantization_info)
209 {
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100210 ActivationValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, in_place, function, alpha_beta, data_type, quantization_info);
Michel Iwaniec66cc12f2017-12-07 17:26:40 +0000211 }
212};
213
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100214} // namespace validation
215} // namespace test
216} // namespace arm_compute
217#endif /* ARM_COMPUTE_TEST_ACTIVATION_LAYER_FIXTURE */