blob: 2680070e50787b9fcc484fbe4abcc8fe4e2494f2 [file] [log] [blame]
Georgios Pinitase5f8fd62017-06-23 18:03:44 +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 "CL/CLAccessor.h"
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010025#include "Globals.h"
26#include "PaddingCalculator.h"
27#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/CL/CLTensor.h"
37#include "arm_compute/runtime/CL/CLTensorAllocator.h"
38#include "arm_compute/runtime/CL/functions/CLSoftmaxLayer.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::cl;
48using namespace arm_compute::test::validation;
49
50namespace
51{
Georgios Pinitas09796752017-07-10 16:05:21 +010052const float tolerance = 0.000001f; /** Tolerance for float operations */
53const float tolerance_qs8 = 2.f; /** Tolerance for QS8 fixed point operations */
54const float tolerance_qs16 = 2.f; /** Tolerance for QS16 fixed point operations */
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010055
56/** Compute OpenCL softmax layer function.
57 *
58 * @param[in] shape Shape of the input and output tensors.
59 * @param[in] dt Shape Data type of tensors.
60 * @param[in] fixed_point_position (Optional) Number of bits for the fractional part of fixed point numbers.
61 *
62 * @return Computed output tensor.
63 */
64CLTensor compute_softmax_layer(const TensorShape &shape, DataType dt, int fixed_point_position = 0)
65{
66 // Create tensors
Moritz Pflanzer94450f12017-06-30 12:48:43 +010067 CLTensor src = create_tensor<CLTensor>(shape, dt, 1, fixed_point_position);
68 CLTensor dst = create_tensor<CLTensor>(shape, dt, 1, fixed_point_position);
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010069
70 // Create and configure function
71 CLSoftmaxLayer smx_layer;
72 smx_layer.configure(&src, &dst);
73
74 // Allocate tensors
75 src.allocator()->allocate();
76 dst.allocator()->allocate();
77
78 BOOST_TEST(!src.info()->is_resizable());
79 BOOST_TEST(!dst.info()->is_resizable());
80
81 // Fill tensors
82 if(arm_compute::is_data_type_float(dt))
83 {
84 std::uniform_real_distribution<> distribution(-1000.f, 1000.f);
85 library->fill(CLAccessor(src), distribution, 0);
86 }
87 else
88 {
89 int one_fixed = 1 << fixed_point_position;
90 std::uniform_int_distribution<> distribution(-one_fixed, one_fixed);
91 library->fill(CLAccessor(src), distribution, 0);
92 }
93
94 // Compute function
95 smx_layer.run();
96
97 return dst;
98}
99} // namespace
100
101#ifndef DOXYGEN_SKIP_THIS
102BOOST_AUTO_TEST_SUITE(CL)
103BOOST_AUTO_TEST_SUITE(SoftmaxLayer)
104
105BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit") * boost::unit_test::label("nightly"))
106BOOST_DATA_TEST_CASE(Configuration, (SmallShapes() + LargeShapes()) * CNNDataTypes(), shape, dt)
107{
108 // Set fixed point position data type allowed
109 int fixed_point_position = (arm_compute::is_data_type_fixed_point(dt)) ? 3 : 0;
110
111 // Create tensors
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100112 CLTensor src = create_tensor<CLTensor>(shape, dt, 1, fixed_point_position);
113 CLTensor dst = create_tensor<CLTensor>(shape, dt, 1, fixed_point_position);
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100114
115 BOOST_TEST(src.info()->is_resizable());
116 BOOST_TEST(dst.info()->is_resizable());
117
118 // Create and configure function
119 CLSoftmaxLayer smx_layer;
120 smx_layer.configure(&src, &dst);
121
122 // Validate valid region
123 const ValidRegion valid_region = shape_to_valid_region(shape);
124 validate(src.info()->valid_region(), valid_region);
125 validate(dst.info()->valid_region(), valid_region);
126
127 // Validate padding
128 const PaddingSize padding = PaddingCalculator(shape.x(), 16).required_padding();
129 validate(src.info()->padding(), padding);
130 validate(dst.info()->padding(), padding);
131}
132
133BOOST_AUTO_TEST_SUITE(Float)
134BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit"))
135BOOST_DATA_TEST_CASE(RunSmall, SmallShapes() * CNNFloatDataTypes(), shape, dt)
136{
137 // Compute function
138 CLTensor dst = compute_softmax_layer(shape, dt);
139
140 // Compute reference
141 RawTensor ref_dst = Reference::compute_reference_softmax_layer(shape, dt);
142
143 // Validate output
144 validate(CLAccessor(dst), ref_dst, tolerance);
145}
146
147BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly"))
148BOOST_DATA_TEST_CASE(RunLarge, LargeShapes() * CNNFloatDataTypes(), shape, dt)
149{
150 // Compute function
151 CLTensor dst = compute_softmax_layer(shape, dt);
152
153 // Compute reference
154 RawTensor ref_dst = Reference::compute_reference_softmax_layer(shape, dt);
155
156 // Validate output
157 validate(CLAccessor(dst), ref_dst, tolerance);
158}
159BOOST_AUTO_TEST_SUITE_END()
160
161BOOST_AUTO_TEST_SUITE(Quantized)
Georgios Pinitas09796752017-07-10 16:05:21 +0100162BOOST_AUTO_TEST_SUITE(QS8)
163// Testing for fixed point position [1,6) as reciprocal limits the maximum fixed point position to 5
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100164BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit"))
Georgios Pinitas09796752017-07-10 16:05:21 +0100165BOOST_DATA_TEST_CASE(RunSmall, SmallShapes() * boost::unit_test::data::xrange(1, 6),
166 shape, fixed_point_position)
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100167{
168 // Compute function
Georgios Pinitas09796752017-07-10 16:05:21 +0100169 CLTensor dst = compute_softmax_layer(shape, DataType::QS8, fixed_point_position);
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100170
171 // Compute reference
Georgios Pinitas09796752017-07-10 16:05:21 +0100172 RawTensor ref_dst = Reference::compute_reference_softmax_layer(shape, DataType::QS8, fixed_point_position);
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100173
174 // Validate output
Georgios Pinitas09796752017-07-10 16:05:21 +0100175 validate(CLAccessor(dst), ref_dst, tolerance_qs8);
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100176}
177
178BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly"))
Georgios Pinitas09796752017-07-10 16:05:21 +0100179BOOST_DATA_TEST_CASE(RunLarge, LargeShapes() * boost::unit_test::data::xrange(1, 6),
180 shape, fixed_point_position)
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100181{
182 // Compute function
Georgios Pinitas09796752017-07-10 16:05:21 +0100183 CLTensor dst = compute_softmax_layer(shape, DataType::QS8, fixed_point_position);
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100184
185 // Compute reference
Georgios Pinitas09796752017-07-10 16:05:21 +0100186 RawTensor ref_dst = Reference::compute_reference_softmax_layer(shape, DataType::QS8, fixed_point_position);
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100187
188 // Validate output
Georgios Pinitas09796752017-07-10 16:05:21 +0100189 validate(CLAccessor(dst), ref_dst, tolerance_qs8);
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100190}
191BOOST_AUTO_TEST_SUITE_END()
192
Georgios Pinitas09796752017-07-10 16:05:21 +0100193BOOST_AUTO_TEST_SUITE(QS16)
194// Testing for fixed point position [1,14) as reciprocal limits the maximum fixed point position to 14
195BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit"))
196BOOST_DATA_TEST_CASE(RunSmall, SmallShapes() * boost::unit_test::data::xrange(1, 14),
197 shape, fixed_point_position)
198{
199 // Compute function
200 CLTensor dst = compute_softmax_layer(shape, DataType::QS16, fixed_point_position);
201
202 // Compute reference
203 RawTensor ref_dst = Reference::compute_reference_softmax_layer(shape, DataType::QS16, fixed_point_position);
204
205 // Validate output
206 validate(CLAccessor(dst), ref_dst, tolerance_qs16);
207}
208
209BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly"))
210BOOST_DATA_TEST_CASE(RunLarge, LargeShapes() * boost::unit_test::data::xrange(1, 14),
211 shape, fixed_point_position)
212{
213 // Compute function
214 CLTensor dst = compute_softmax_layer(shape, DataType::QS16, fixed_point_position);
215
216 // Compute reference
217 RawTensor ref_dst = Reference::compute_reference_softmax_layer(shape, DataType::QS16, fixed_point_position);
218
219 // Validate output
220 validate(CLAccessor(dst), ref_dst, tolerance_qs16);
221}
222BOOST_AUTO_TEST_SUITE_END()
223BOOST_AUTO_TEST_SUITE_END()
224
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100225BOOST_AUTO_TEST_SUITE_END()
226BOOST_AUTO_TEST_SUITE_END()
Anthony Barbierac69aa12017-07-03 17:39:37 +0100227#endif /* DOXYGEN_SKIP_THIS */