blob: a4826accfa16149bd419d7418771e7ac5dd27285 [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"
25#include "NEON/Helper.h"
26#include "NEON/NEAccessor.h"
Moritz Pflanzer5b512292017-06-21 15:54:07 +010027#include "PaddingCalculator.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "TensorLibrary.h"
29#include "TypePrinter.h"
30#include "Utils.h"
31#include "validation/Datasets.h"
32#include "validation/Helpers.h"
33#include "validation/Reference.h"
34#include "validation/Validation.h"
35
36#include "arm_compute/core/Helpers.h"
37#include "arm_compute/core/Types.h"
38#include "arm_compute/runtime/NEON/functions/NEActivationLayer.h"
39#include "arm_compute/runtime/Tensor.h"
40#include "arm_compute/runtime/TensorAllocator.h"
41
42#include "boost_wrapper.h"
43
44#include <random>
45#include <string>
46#include <tuple>
47
48using namespace arm_compute;
49using namespace arm_compute::test;
50using namespace arm_compute::test::neon;
51using namespace arm_compute::test::validation;
52
53namespace
54{
55/** Define tolerance of the activation layer
56 *
57 * @param[in] activation The activation function used.
58 * @param[in] fixed_point_position Number of bits for the fractional part..
59 *
60 * @return Tolerance depending on the activation function.
61 */
62float activation_layer_tolerance(ActivationLayerInfo::ActivationFunction activation, int fixed_point_position = 0)
63{
64 switch(activation)
65 {
66 case ActivationLayerInfo::ActivationFunction::LOGISTIC:
67 case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
68 case ActivationLayerInfo::ActivationFunction::SQRT:
69 case ActivationLayerInfo::ActivationFunction::TANH:
70 return (fixed_point_position != 0) ? 5.f : 0.00001f;
71 break;
72 default:
73 return 0.f;
74 }
75}
76
77/** Compute Neon activation layer function.
78 *
79 * @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.
82 * @param[in] fixed_point_position Number of bits for the fractional part of fixed point numbers.
83 *
84 * @return Computed output tensor.
85 */
86Tensor compute_activation_layer(const TensorShape &shape, DataType dt, ActivationLayerInfo act_info, int fixed_point_position = 0)
87{
88 // Create tensors
89 Tensor src = create_tensor(shape, dt, 1, fixed_point_position);
90 Tensor dst = create_tensor(shape, dt, 1, fixed_point_position);
91
92 // Create and configure function
93 NEActivationLayer act_layer;
94 act_layer.configure(&src, &dst, act_info);
95
96 // Allocate tensors
97 src.allocator()->allocate();
98 dst.allocator()->allocate();
99
100 BOOST_TEST(!src.info()->is_resizable());
101 BOOST_TEST(!dst.info()->is_resizable());
102
103 // Fill tensors
104 if(dt == DataType::F32)
105 {
106 float min_bound = 0;
107 float max_bound = 0;
108 std::tie(min_bound, max_bound) = get_activation_layer_test_bounds<float>(act_info.activation());
109 std::uniform_real_distribution<> distribution(min_bound, max_bound);
110 library->fill(NEAccessor(src), distribution, 0);
111 }
112 else
113 {
114 int min_bound = 0;
115 int max_bound = 0;
116 std::tie(min_bound, max_bound) = get_activation_layer_test_bounds<int8_t>(act_info.activation(), fixed_point_position);
117 std::uniform_int_distribution<> distribution(min_bound, max_bound);
118 library->fill(NEAccessor(src), distribution, 0);
119 }
120
121 // Compute function
122 act_layer.run();
123
124 return dst;
125}
126} // namespace
127
128#ifndef DOXYGEN_SKIP_THIS
129BOOST_AUTO_TEST_SUITE(NEON)
130BOOST_AUTO_TEST_SUITE(ActivationLayer)
131
132BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit") * boost::unit_test::label("nightly"))
133BOOST_DATA_TEST_CASE(Configuration, (SmallShapes() + LargeShapes()) * CNNDataTypes(), shape, dt)
134{
135 // Set fixed point position data type allowed
136 int fixed_point_position = (arm_compute::is_data_type_fixed_point(dt)) ? 3 : 0;
137
138 // Create tensors
139 Tensor src = create_tensor(shape, dt, 1, fixed_point_position);
140 Tensor dst = create_tensor(shape, dt, 1, fixed_point_position);
141
142 BOOST_TEST(src.info()->is_resizable());
143 BOOST_TEST(dst.info()->is_resizable());
144
145 // Create and configure function
146 NEActivationLayer act_layer;
147 act_layer.configure(&src, &dst, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS));
148
149 // Validate valid region
150 const ValidRegion valid_region = shape_to_valid_region(shape);
151 validate(src.info()->valid_region(), valid_region);
152 validate(dst.info()->valid_region(), valid_region);
153
154 // Validate padding
Moritz Pflanzer2509fba2017-06-23 14:15:03 +0100155 const PaddingSize padding = PaddingCalculator(shape.x(), 16).required_padding();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100156 validate(src.info()->padding(), padding);
157 validate(dst.info()->padding(), padding);
158}
159
160BOOST_AUTO_TEST_SUITE(Float)
161BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit"))
162BOOST_DATA_TEST_CASE(RunSmall, SmallShapes() * CNNFloatDataTypes() * ActivationFunctions(), shape, dt, act_function)
163{
164 // Create activation layer info
165 ActivationLayerInfo act_info(act_function, 1.f, 1.f);
166
167 // Compute function
168 Tensor dst = compute_activation_layer(shape, dt, act_info);
169
170 // Compute reference
171 RawTensor ref_dst = Reference::compute_reference_activation_layer(shape, dt, act_info);
172
173 // Validate output
174 validate(NEAccessor(dst), ref_dst, activation_layer_tolerance(act_function));
175}
176
177BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly"))
178BOOST_DATA_TEST_CASE(RunLarge, LargeShapes() * CNNFloatDataTypes() * ActivationFunctions(), shape, dt, act_function)
179{
180 // Create activation layer info
181 ActivationLayerInfo act_info(act_function, 1.f, 1.f);
182
183 // Compute function
184 Tensor dst = compute_activation_layer(shape, dt, act_info);
185
186 // Compute reference
187 RawTensor ref_dst = Reference::compute_reference_activation_layer(shape, dt, act_info);
188
189 // Validate output
190 validate(NEAccessor(dst), ref_dst, activation_layer_tolerance(act_function));
191}
192BOOST_AUTO_TEST_SUITE_END()
193
194/** @note We test for fixed point precision [3,5] because [1,2] and [6,7] ranges
195 * cause overflowing issues in most of the transcendentals functions.
196 */
197BOOST_AUTO_TEST_SUITE(Quantized)
198BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit"))
199BOOST_DATA_TEST_CASE(RunSmall, SmallShapes() * ActivationFunctions() * boost::unit_test::data::xrange(3, 6, 1),
200 shape, act_function, fixed_point_position)
201{
202 // Create activation layer info
203 ActivationLayerInfo act_info(act_function, 1.f, 1.f);
204
205 // Compute function
206 Tensor dst = compute_activation_layer(shape, DataType::QS8, act_info, fixed_point_position);
207
208 // Compute reference
209 RawTensor ref_dst = Reference::compute_reference_activation_layer(shape, DataType::QS8, act_info, fixed_point_position);
210
211 // Validate output
212 validate(NEAccessor(dst), ref_dst, activation_layer_tolerance(act_function, fixed_point_position));
213}
214BOOST_AUTO_TEST_SUITE_END()
215
216BOOST_AUTO_TEST_SUITE_END()
217BOOST_AUTO_TEST_SUITE_END()
218#endif