blob: 389d44814daffc5b939c361e8ff4164ce8399b54 [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)
164// Testing for fixed point position [1,6) as reciprocal limits the maximum fixed point position to 5
165BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit"))
166BOOST_DATA_TEST_CASE(RunSmall, SmallShapes() * CNNFixedPointDataTypes() * boost::unit_test::data::xrange(1, 6),
167 shape, dt, fixed_point_position)
168{
169 // Compute function
170 Tensor dst = compute_softmax_layer(shape, dt, fixed_point_position);
171
172 // Compute reference
173 RawTensor ref_dst = Reference::compute_reference_softmax_layer(shape, dt, fixed_point_position);
174
175 // Validate output
176 validate(NEAccessor(dst), ref_dst, tolerance_fixed_point);
177}
178
179BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly"))
180BOOST_DATA_TEST_CASE(RunLarge, LargeShapes() * CNNFixedPointDataTypes() * boost::unit_test::data::xrange(1, 6),
181 shape, dt, fixed_point_position)
182{
183 // Compute function
184 Tensor dst = compute_softmax_layer(shape, dt, fixed_point_position);
185
186 // Compute reference
187 RawTensor ref_dst = Reference::compute_reference_softmax_layer(shape, dt, fixed_point_position);
188
189 // Validate output
190 validate(NEAccessor(dst), ref_dst, tolerance_fixed_point);
191}
192BOOST_AUTO_TEST_SUITE_END()
193
194BOOST_AUTO_TEST_SUITE_END()
195BOOST_AUTO_TEST_SUITE_END()
196#endif