blob: 66a9e03acef3fe6d8d39f7604916f5cc383b0d55 [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/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/NESoftmaxLayer.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
46using namespace arm_compute;
47using namespace arm_compute::test;
48using namespace arm_compute::test::neon;
49using namespace arm_compute::test::validation;
50
51namespace
52{
53/** Tolerance for float operations */
54const float tolerance = 0.000001f;
55/** Tolerance for fixed point operations */
56const float tolerance_fixed_point = 2.f;
57
58/** Compute Neon softmax layer function.
59 *
60 * @param[in] shape Shape of the input and output tensors.
61 * @param[in] dt Shape Data type of tensors.
62 * @param[in] fixed_point_position (Optional) Number of bits for the fractional part of fixed point numbers.
63 *
64 * @return Computed output tensor.
65 */
66Tensor compute_softmax_layer(const TensorShape &shape, DataType dt, int fixed_point_position = 0)
67{
68 // Create tensors
69 Tensor src = create_tensor(shape, dt, 1, fixed_point_position);
70 Tensor dst = create_tensor(shape, dt, 1, fixed_point_position);
71
72 // Create and configure function
73 NESoftmaxLayer smx_layer;
74 smx_layer.configure(&src, &dst);
75
76 // Allocate tensors
77 src.allocator()->allocate();
78 dst.allocator()->allocate();
79
80 BOOST_TEST(!src.info()->is_resizable());
81 BOOST_TEST(!dst.info()->is_resizable());
82
83 // Fill tensors
84 if(arm_compute::is_data_type_float(dt))
85 {
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010086 std::uniform_real_distribution<> distribution(-1000.f, 1000.f);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010087 library->fill(NEAccessor(src), distribution, 0);
88 }
89 else
90 {
91 int one_fixed = 1 << fixed_point_position;
92 std::uniform_int_distribution<> distribution(-one_fixed, one_fixed);
93 library->fill(NEAccessor(src), distribution, 0);
94 }
95
96 // Compute function
97 smx_layer.run();
98
99 return dst;
100}
101} // namespace
102
103#ifndef DOXYGEN_SKIP_THIS
104BOOST_AUTO_TEST_SUITE(NEON)
105BOOST_AUTO_TEST_SUITE(SoftmaxLayer)
106
107BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit") * boost::unit_test::label("nightly"))
108BOOST_DATA_TEST_CASE(Configuration, (SmallShapes() + LargeShapes()) * CNNDataTypes(), shape, dt)
109{
110 // Set fixed point position data type allowed
111 int fixed_point_position = (arm_compute::is_data_type_fixed_point(dt)) ? 3 : 0;
112
113 // Create tensors
114 Tensor src = create_tensor(shape, dt, 1, fixed_point_position);
115 Tensor dst = create_tensor(shape, dt, 1, fixed_point_position);
116
117 BOOST_TEST(src.info()->is_resizable());
118 BOOST_TEST(dst.info()->is_resizable());
119
120 // Create and configure function
121 NESoftmaxLayer smx_layer;
122 smx_layer.configure(&src, &dst);
123
124 // Validate valid region
125 const ValidRegion valid_region = shape_to_valid_region(shape);
126 validate(src.info()->valid_region(), valid_region);
127 validate(dst.info()->valid_region(), valid_region);
128
129 // Validate padding
Moritz Pflanzer2509fba2017-06-23 14:15:03 +0100130 const int step = 16 / arm_compute::data_size_from_type(dt);
131 const PaddingSize padding = PaddingCalculator(shape.x(), step).required_padding();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100132 validate(src.info()->padding(), padding);
133 validate(dst.info()->padding(), padding);
134}
135
136BOOST_AUTO_TEST_SUITE(Float)
137BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit"))
138BOOST_DATA_TEST_CASE(RunSmall, SmallShapes() * CNNFloatDataTypes(), shape, dt)
139{
140 // Compute function
141 Tensor dst = compute_softmax_layer(shape, dt);
142
143 // Compute reference
144 RawTensor ref_dst = Reference::compute_reference_softmax_layer(shape, dt);
145
146 // Validate output
147 validate(NEAccessor(dst), ref_dst, tolerance);
148}
149
150BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly"))
151BOOST_DATA_TEST_CASE(RunLarge, LargeShapes() * CNNFloatDataTypes(), shape, dt)
152{
153 // Compute function
154 Tensor dst = compute_softmax_layer(shape, dt);
155
156 // Compute reference
157 RawTensor ref_dst = Reference::compute_reference_softmax_layer(shape, dt);
158
159 // Validate output
160 validate(NEAccessor(dst), ref_dst, tolerance);
161}
162BOOST_AUTO_TEST_SUITE_END()
163
164BOOST_AUTO_TEST_SUITE(Quantized)
165// 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"))
167BOOST_DATA_TEST_CASE(RunSmall, SmallShapes() * CNNFixedPointDataTypes() * boost::unit_test::data::xrange(1, 6),
168 shape, dt, fixed_point_position)
169{
170 // Compute function
171 Tensor dst = compute_softmax_layer(shape, dt, fixed_point_position);
172
173 // Compute reference
174 RawTensor ref_dst = Reference::compute_reference_softmax_layer(shape, dt, fixed_point_position);
175
176 // Validate output
177 validate(NEAccessor(dst), ref_dst, tolerance_fixed_point);
178}
179
180BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly"))
181BOOST_DATA_TEST_CASE(RunLarge, LargeShapes() * CNNFixedPointDataTypes() * boost::unit_test::data::xrange(1, 6),
182 shape, dt, fixed_point_position)
183{
184 // Compute function
185 Tensor dst = compute_softmax_layer(shape, dt, fixed_point_position);
186
187 // Compute reference
188 RawTensor ref_dst = Reference::compute_reference_softmax_layer(shape, dt, fixed_point_position);
189
190 // Validate output
191 validate(NEAccessor(dst), ref_dst, tolerance_fixed_point);
192}
193BOOST_AUTO_TEST_SUITE_END()
194
195BOOST_AUTO_TEST_SUITE_END()
196BOOST_AUTO_TEST_SUITE_END()
197#endif