blob: 71dfcdc4e24546fab0573284f75da6b5d1fe5eb3 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
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#include "Globals.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025#include "NEON/NEAccessor.h"
Moritz Pflanzer5b512292017-06-21 15:54:07 +010026#include "PaddingCalculator.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "TensorLibrary.h"
28#include "TypePrinter.h"
29#include "Utils.h"
30#include "validation/Datasets.h"
31#include "validation/Helpers.h"
32#include "validation/Reference.h"
33#include "validation/Validation.h"
34
35#include "arm_compute/core/Helpers.h"
36#include "arm_compute/core/Types.h"
37#include "arm_compute/runtime/NEON/functions/NEActivationLayer.h"
38#include "arm_compute/runtime/Tensor.h"
39#include "arm_compute/runtime/TensorAllocator.h"
40
41#include "boost_wrapper.h"
42
43#include <random>
44#include <string>
45#include <tuple>
46
47using namespace arm_compute;
48using namespace arm_compute::test;
49using namespace arm_compute::test::neon;
50using namespace arm_compute::test::validation;
51
52namespace
53{
54/** Define tolerance of the activation layer
55 *
56 * @param[in] activation The activation function used.
57 * @param[in] fixed_point_position Number of bits for the fractional part..
58 *
59 * @return Tolerance depending on the activation function.
60 */
61float activation_layer_tolerance(ActivationLayerInfo::ActivationFunction activation, int fixed_point_position = 0)
62{
63 switch(activation)
64 {
65 case ActivationLayerInfo::ActivationFunction::LOGISTIC:
66 case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
67 case ActivationLayerInfo::ActivationFunction::SQRT:
68 case ActivationLayerInfo::ActivationFunction::TANH:
69 return (fixed_point_position != 0) ? 5.f : 0.00001f;
70 break;
71 default:
72 return 0.f;
73 }
74}
75
76/** Compute Neon activation layer function.
77 *
Moritz Pflanzer443c8b92017-06-27 12:36:21 +010078 * @param[in] in_place Compute the activation layer in-place.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010079 * @param[in] shape Shape of the input and output tensors.
80 * @param[in] dt Shape Data type of tensors.
81 * @param[in] act_info Activation layer information.
Moritz Pflanzer443c8b92017-06-27 12:36:21 +010082 * @param[in] fixed_point_position (Optional) Number of bits for the fractional part of fixed point numbers.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010083 *
84 * @return Computed output tensor.
85 */
Moritz Pflanzer443c8b92017-06-27 12:36:21 +010086Tensor compute_activation_layer(bool in_place, const TensorShape &shape, DataType dt, ActivationLayerInfo act_info, int fixed_point_position = 0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010087{
88 // Create tensors
Moritz Pflanzer94450f12017-06-30 12:48:43 +010089 Tensor src = create_tensor<Tensor>(shape, dt, 1, fixed_point_position);
90 Tensor dst = create_tensor<Tensor>(shape, dt, 1, fixed_point_position);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010091
92 // Create and configure function
93 NEActivationLayer act_layer;
Moritz Pflanzer443c8b92017-06-27 12:36:21 +010094
95 if(in_place)
96 {
97 act_layer.configure(&src, nullptr, act_info);
98 }
99 else
100 {
101 act_layer.configure(&src, &dst, act_info);
102 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103
104 // Allocate tensors
105 src.allocator()->allocate();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100106 BOOST_TEST(!src.info()->is_resizable());
Moritz Pflanzer443c8b92017-06-27 12:36:21 +0100107
108 if(!in_place)
109 {
110 dst.allocator()->allocate();
111 BOOST_TEST(!dst.info()->is_resizable());
112 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113
114 // Fill tensors
115 if(dt == DataType::F32)
116 {
117 float min_bound = 0;
118 float max_bound = 0;
119 std::tie(min_bound, max_bound) = get_activation_layer_test_bounds<float>(act_info.activation());
120 std::uniform_real_distribution<> distribution(min_bound, max_bound);
121 library->fill(NEAccessor(src), distribution, 0);
122 }
123 else
124 {
125 int min_bound = 0;
126 int max_bound = 0;
127 std::tie(min_bound, max_bound) = get_activation_layer_test_bounds<int8_t>(act_info.activation(), fixed_point_position);
128 std::uniform_int_distribution<> distribution(min_bound, max_bound);
129 library->fill(NEAccessor(src), distribution, 0);
130 }
131
132 // Compute function
133 act_layer.run();
134
Moritz Pflanzer443c8b92017-06-27 12:36:21 +0100135 if(in_place)
136 {
137 return src;
138 }
139 else
140 {
141 return dst;
142 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100143}
144} // namespace
145
146#ifndef DOXYGEN_SKIP_THIS
147BOOST_AUTO_TEST_SUITE(NEON)
148BOOST_AUTO_TEST_SUITE(ActivationLayer)
149
150BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit") * boost::unit_test::label("nightly"))
Moritz Pflanzer443c8b92017-06-27 12:36:21 +0100151BOOST_DATA_TEST_CASE(Configuration, boost::unit_test::data::make({ false, true }) * (SmallShapes() + LargeShapes()) * CNNDataTypes(), in_place, shape, dt)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152{
153 // Set fixed point position data type allowed
Moritz Pflanzer443c8b92017-06-27 12:36:21 +0100154 const int fixed_point_position = (arm_compute::is_data_type_fixed_point(dt)) ? 3 : 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100155
156 // Create tensors
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100157 Tensor src = create_tensor<Tensor>(shape, dt, 1, fixed_point_position);
158 Tensor dst = create_tensor<Tensor>(shape, dt, 1, fixed_point_position);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100159
160 BOOST_TEST(src.info()->is_resizable());
161 BOOST_TEST(dst.info()->is_resizable());
162
163 // Create and configure function
164 NEActivationLayer act_layer;
Moritz Pflanzer443c8b92017-06-27 12:36:21 +0100165
166 if(in_place)
167 {
168 act_layer.configure(&src, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS));
169 }
170 else
171 {
172 act_layer.configure(&src, &dst, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS));
173 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100174
175 // Validate valid region
176 const ValidRegion valid_region = shape_to_valid_region(shape);
177 validate(src.info()->valid_region(), valid_region);
Moritz Pflanzer443c8b92017-06-27 12:36:21 +0100178
179 if(!in_place)
180 {
181 validate(dst.info()->valid_region(), valid_region);
182 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100183
184 // Validate padding
Moritz Pflanzer2509fba2017-06-23 14:15:03 +0100185 const PaddingSize padding = PaddingCalculator(shape.x(), 16).required_padding();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100186 validate(src.info()->padding(), padding);
Moritz Pflanzer443c8b92017-06-27 12:36:21 +0100187
188 if(!in_place)
189 {
190 validate(dst.info()->padding(), padding);
191 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100192}
193
194BOOST_AUTO_TEST_SUITE(Float)
195BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit"))
Georgios Pinitasccc65d42017-06-27 17:39:11 +0100196BOOST_DATA_TEST_CASE(RunSmall, boost::unit_test::data::make({ false, true }) * SmallShapes() * CNNFloatDataTypes() * ActivationFunctions() * boost::unit_test::data::make({ 0.5f, 1.f }),
197 in_place, shape, dt, act_function, alpha_beta)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100198{
199 // Create activation layer info
Georgios Pinitasccc65d42017-06-27 17:39:11 +0100200 ActivationLayerInfo act_info(act_function, alpha_beta, alpha_beta);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100201
202 // Compute function
Moritz Pflanzer443c8b92017-06-27 12:36:21 +0100203 Tensor dst = compute_activation_layer(in_place, shape, dt, act_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100204
205 // Compute reference
206 RawTensor ref_dst = Reference::compute_reference_activation_layer(shape, dt, act_info);
207
208 // Validate output
209 validate(NEAccessor(dst), ref_dst, activation_layer_tolerance(act_function));
210}
211
212BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly"))
Georgios Pinitasccc65d42017-06-27 17:39:11 +0100213BOOST_DATA_TEST_CASE(RunLarge, boost::unit_test::data::make({ false, true }) * LargeShapes() * CNNFloatDataTypes() * ActivationFunctions() * boost::unit_test::data::make({ 0.5f, 1.f }),
214 in_place, shape, dt, act_function, alpha_beta)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100215{
216 // Create activation layer info
Georgios Pinitasccc65d42017-06-27 17:39:11 +0100217 ActivationLayerInfo act_info(act_function, alpha_beta, alpha_beta);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100218
219 // Compute function
Moritz Pflanzer443c8b92017-06-27 12:36:21 +0100220 Tensor dst = compute_activation_layer(in_place, shape, dt, act_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100221
222 // Compute reference
223 RawTensor ref_dst = Reference::compute_reference_activation_layer(shape, dt, act_info);
224
225 // Validate output
226 validate(NEAccessor(dst), ref_dst, activation_layer_tolerance(act_function));
227}
228BOOST_AUTO_TEST_SUITE_END()
229
230/** @note We test for fixed point precision [3,5] because [1,2] and [6,7] ranges
231 * cause overflowing issues in most of the transcendentals functions.
232 */
233BOOST_AUTO_TEST_SUITE(Quantized)
Georgios Pinitasccc65d42017-06-27 17:39:11 +0100234BOOST_AUTO_TEST_SUITE(QS8)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100235BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit"))
Georgios Pinitasccc65d42017-06-27 17:39:11 +0100236BOOST_DATA_TEST_CASE(RunSmall, boost::unit_test::data::make({ false, true }) * SmallShapes() * ActivationFunctions() * boost::unit_test::data::xrange(3, 6, 1) * boost::unit_test::data::make({ 0.5f, 1.f }),
237 in_place, shape, act_function, fixed_point_position, alpha_beta)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100238{
239 // Create activation layer info
Georgios Pinitasccc65d42017-06-27 17:39:11 +0100240 ActivationLayerInfo act_info(act_function, alpha_beta, alpha_beta);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100241
242 // Compute function
Moritz Pflanzer443c8b92017-06-27 12:36:21 +0100243 Tensor dst = compute_activation_layer(in_place, shape, DataType::QS8, act_info, fixed_point_position);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100244
245 // Compute reference
246 RawTensor ref_dst = Reference::compute_reference_activation_layer(shape, DataType::QS8, act_info, fixed_point_position);
247
248 // Validate output
249 validate(NEAccessor(dst), ref_dst, activation_layer_tolerance(act_function, fixed_point_position));
250}
251BOOST_AUTO_TEST_SUITE_END()
252
Georgios Pinitasccc65d42017-06-27 17:39:11 +0100253BOOST_AUTO_TEST_SUITE(QS16)
254BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit"))
255BOOST_DATA_TEST_CASE(RunSmall, boost::unit_test::data::make({ false, true }) * SmallShapes() * ActivationFunctions() * boost::unit_test::data::xrange(3, 6, 1) * boost::unit_test::data::make({ 0.5f, 1.f }),
256 in_place, shape, act_function, fixed_point_position, alpha_beta)
257{
258 // Create activation layer info
259 ActivationLayerInfo act_info(act_function, alpha_beta, alpha_beta);
260
261 // Compute function
262 Tensor dst = compute_activation_layer(in_place, shape, DataType::QS16, act_info, fixed_point_position);
263
264 // Compute reference
265 RawTensor ref_dst = Reference::compute_reference_activation_layer(shape, DataType::QS16, act_info, fixed_point_position);
266
267 // Validate output
268 validate(NEAccessor(dst), ref_dst, activation_layer_tolerance(act_function, fixed_point_position));
269}
270BOOST_AUTO_TEST_SUITE_END()
271
272BOOST_AUTO_TEST_SUITE_END()
273
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100274BOOST_AUTO_TEST_SUITE_END()
275BOOST_AUTO_TEST_SUITE_END()
Anthony Barbierac69aa12017-07-03 17:39:37 +0100276#endif /* DOXYGEN_SKIP_THIS */