blob: 9ac81af278b5b672ff333a56d3344df97e7b1bb5 [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/Reference.h"
32#include "validation/Validation.h"
33
34#include "arm_compute/core/Helpers.h"
35#include "arm_compute/core/Types.h"
36#include "arm_compute/runtime/NEON/functions/NESoftmaxLayer.h"
37#include "arm_compute/runtime/Tensor.h"
38#include "arm_compute/runtime/TensorAllocator.h"
39
40#include "boost_wrapper.h"
41
42#include <random>
43#include <string>
44
45using namespace arm_compute;
46using namespace arm_compute::test;
47using namespace arm_compute::test::neon;
48using namespace arm_compute::test::validation;
49
50namespace
51{
52/** Tolerance for float operations */
53const float tolerance = 0.000001f;
54/** Tolerance for fixed point operations */
55const float tolerance_fixed_point = 2.f;
56
57/** Compute Neon softmax layer function.
58 *
59 * @param[in] shape Shape of the input and output tensors.
60 * @param[in] dt Shape Data type of tensors.
61 * @param[in] fixed_point_position (Optional) Number of bits for the fractional part of fixed point numbers.
62 *
63 * @return Computed output tensor.
64 */
65Tensor compute_softmax_layer(const TensorShape &shape, DataType dt, int fixed_point_position = 0)
66{
67 // Create tensors
Moritz Pflanzer94450f12017-06-30 12:48:43 +010068 Tensor src = create_tensor<Tensor>(shape, dt, 1, fixed_point_position);
69 Tensor dst = create_tensor<Tensor>(shape, dt, 1, fixed_point_position);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010070
71 // Create and configure function
72 NESoftmaxLayer smx_layer;
73 smx_layer.configure(&src, &dst);
74
75 // Allocate tensors
76 src.allocator()->allocate();
77 dst.allocator()->allocate();
78
79 BOOST_TEST(!src.info()->is_resizable());
80 BOOST_TEST(!dst.info()->is_resizable());
81
82 // Fill tensors
83 if(arm_compute::is_data_type_float(dt))
84 {
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010085 std::uniform_real_distribution<> distribution(-1000.f, 1000.f);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010086 library->fill(NEAccessor(src), distribution, 0);
87 }
88 else
89 {
90 int one_fixed = 1 << fixed_point_position;
91 std::uniform_int_distribution<> distribution(-one_fixed, one_fixed);
92 library->fill(NEAccessor(src), distribution, 0);
93 }
94
95 // Compute function
96 smx_layer.run();
97
98 return dst;
99}
100} // namespace
101
102#ifndef DOXYGEN_SKIP_THIS
103BOOST_AUTO_TEST_SUITE(NEON)
104BOOST_AUTO_TEST_SUITE(SoftmaxLayer)
105
106BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit") * boost::unit_test::label("nightly"))
107BOOST_DATA_TEST_CASE(Configuration, (SmallShapes() + LargeShapes()) * CNNDataTypes(), shape, dt)
108{
109 // Set fixed point position data type allowed
110 int fixed_point_position = (arm_compute::is_data_type_fixed_point(dt)) ? 3 : 0;
111
112 // Create tensors
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100113 Tensor src = create_tensor<Tensor>(shape, dt, 1, fixed_point_position);
114 Tensor dst = create_tensor<Tensor>(shape, dt, 1, fixed_point_position);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115
116 BOOST_TEST(src.info()->is_resizable());
117 BOOST_TEST(dst.info()->is_resizable());
118
119 // Create and configure function
120 NESoftmaxLayer smx_layer;
121 smx_layer.configure(&src, &dst);
122
123 // Validate valid region
124 const ValidRegion valid_region = shape_to_valid_region(shape);
125 validate(src.info()->valid_region(), valid_region);
126 validate(dst.info()->valid_region(), valid_region);
127
128 // Validate padding
Moritz Pflanzer2509fba2017-06-23 14:15:03 +0100129 const int step = 16 / arm_compute::data_size_from_type(dt);
130 const PaddingSize padding = PaddingCalculator(shape.x(), step).required_padding();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100131 validate(src.info()->padding(), padding);
132 validate(dst.info()->padding(), padding);
133}
134
135BOOST_AUTO_TEST_SUITE(Float)
136BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit"))
137BOOST_DATA_TEST_CASE(RunSmall, SmallShapes() * CNNFloatDataTypes(), shape, dt)
138{
139 // Compute function
140 Tensor dst = compute_softmax_layer(shape, dt);
141
142 // Compute reference
143 RawTensor ref_dst = Reference::compute_reference_softmax_layer(shape, dt);
144
145 // Validate output
146 validate(NEAccessor(dst), ref_dst, tolerance);
147}
148
149BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly"))
150BOOST_DATA_TEST_CASE(RunLarge, LargeShapes() * CNNFloatDataTypes(), shape, dt)
151{
152 // Compute function
153 Tensor dst = compute_softmax_layer(shape, dt);
154
155 // Compute reference
156 RawTensor ref_dst = Reference::compute_reference_softmax_layer(shape, dt);
157
158 // Validate output
159 validate(NEAccessor(dst), ref_dst, tolerance);
160}
161BOOST_AUTO_TEST_SUITE_END()
162
163BOOST_AUTO_TEST_SUITE(Quantized)
Georgios Pinitas9247c922017-06-28 18:29:47 +0100164BOOST_AUTO_TEST_SUITE(QS8)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100165// Testing for fixed point position [1,6) as reciprocal limits the maximum fixed point position to 5
166BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit"))
Georgios Pinitas9247c922017-06-28 18:29:47 +0100167BOOST_DATA_TEST_CASE(RunSmall, SmallShapes() * boost::unit_test::data::xrange(1, 6),
168 shape, fixed_point_position)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100169{
170 // Compute function
Georgios Pinitas9247c922017-06-28 18:29:47 +0100171 Tensor dst = compute_softmax_layer(shape, DataType::QS8, fixed_point_position);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100172
173 // Compute reference
Georgios Pinitas9247c922017-06-28 18:29:47 +0100174 RawTensor ref_dst = Reference::compute_reference_softmax_layer(shape, DataType::QS8, fixed_point_position);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100175
176 // Validate output
177 validate(NEAccessor(dst), ref_dst, tolerance_fixed_point);
178}
179
180BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly"))
Georgios Pinitas9247c922017-06-28 18:29:47 +0100181BOOST_DATA_TEST_CASE(RunLarge, LargeShapes() * boost::unit_test::data::xrange(1, 6),
182 shape, fixed_point_position)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100183{
184 // Compute function
Georgios Pinitas9247c922017-06-28 18:29:47 +0100185 Tensor dst = compute_softmax_layer(shape, DataType::QS8, fixed_point_position);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100186
187 // Compute reference
Georgios Pinitas9247c922017-06-28 18:29:47 +0100188 RawTensor ref_dst = Reference::compute_reference_softmax_layer(shape, DataType::QS8, fixed_point_position);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100189
190 // Validate output
191 validate(NEAccessor(dst), ref_dst, tolerance_fixed_point);
192}
193BOOST_AUTO_TEST_SUITE_END()
194
Georgios Pinitas9247c922017-06-28 18:29:47 +0100195BOOST_AUTO_TEST_SUITE(QS16)
196// Testing for fixed point position [1,14) as reciprocal limits the maximum fixed point position to 14
197BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit"))
198BOOST_DATA_TEST_CASE(RunSmall, SmallShapes() * boost::unit_test::data::xrange(1, 14),
199 shape, fixed_point_position)
200{
201 // Compute function
202 Tensor dst = compute_softmax_layer(shape, DataType::QS16, fixed_point_position);
203
204 // Compute reference
205 RawTensor ref_dst = Reference::compute_reference_softmax_layer(shape, DataType::QS16, fixed_point_position);
206
207 // Validate output
208 validate(NEAccessor(dst), ref_dst, tolerance_fixed_point);
209}
210
211BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly"))
212BOOST_DATA_TEST_CASE(RunLarge, LargeShapes() * boost::unit_test::data::xrange(1, 14),
213 shape, fixed_point_position)
214{
215 // Compute function
216 Tensor dst = compute_softmax_layer(shape, DataType::QS16, fixed_point_position);
217
218 // Compute reference
219 RawTensor ref_dst = Reference::compute_reference_softmax_layer(shape, DataType::QS16, fixed_point_position);
220
221 // Validate output
222 validate(NEAccessor(dst), ref_dst, tolerance_fixed_point);
223}
224BOOST_AUTO_TEST_SUITE_END()
225BOOST_AUTO_TEST_SUITE_END()
226
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100227BOOST_AUTO_TEST_SUITE_END()
228BOOST_AUTO_TEST_SUITE_END()
Anthony Barbierac69aa12017-07-03 17:39:37 +0100229#endif /* DOXYGEN_SKIP_THIS */