blob: 46ce63ed75740b6d6c8881d9efdf57bbb3a63ff3 [file] [log] [blame]
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +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 */
Chunosovd6afedc2017-11-06 22:09:45 +070024#include "arm_compute/core/CL/kernels/CLSoftmaxLayerKernel.h"
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010025#include "arm_compute/core/Types.h"
26#include "arm_compute/runtime/CL/CLTensor.h"
27#include "arm_compute/runtime/CL/CLTensorAllocator.h"
28#include "arm_compute/runtime/CL/functions/CLSoftmaxLayer.h"
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010029#include "tests/CL/CLAccessor.h"
30#include "tests/PaddingCalculator.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010031#include "tests/datasets/ShapeDatasets.h"
32#include "tests/framework/Asserts.h"
33#include "tests/framework/Macros.h"
34#include "tests/framework/datasets/Datasets.h"
35#include "tests/validation/Validation.h"
36#include "tests/validation/fixtures/SoftmaxLayerFixture.h"
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010037
38namespace arm_compute
39{
40namespace test
41{
42namespace validation
43{
44namespace
45{
46/** Tolerance for float operations */
Georgios Pinitas583137c2017-08-31 18:12:42 +010047RelativeTolerance<half> tolerance_f16(half(0.2));
48RelativeTolerance<float> tolerance_f32(0.001f);
steniu013e05e4e2017-08-25 17:18:01 +010049
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010050/** Tolerance for fixed point operations */
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +010051constexpr AbsoluteTolerance<int16_t> tolerance_fixed_point(2);
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010052
Chunosovf450caa2017-11-08 16:09:35 +070053/** Tolerance for quantized operations */
54constexpr AbsoluteTolerance<uint8_t> tolerance_qasymm8(1);
55
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010056/** CNN data types */
57const auto CNNDataTypes = framework::dataset::make("DataType",
58{
Chunosovf450caa2017-11-08 16:09:35 +070059 DataType::QASYMM8,
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010060 DataType::F16,
61 DataType::F32,
62 DataType::QS8,
63 DataType::QS16,
64});
65} // namespace
66
67TEST_SUITE(CL)
68TEST_SUITE(SoftmaxLayer)
69
Chunosovd6afedc2017-11-06 22:09:45 +070070DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(concat(datasets::SoftmaxLayerSmallShapes(), datasets::SoftmaxLayerLargeShapes()), CNNDataTypes), shape, data_type)
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010071{
Chunosovf450caa2017-11-08 16:09:35 +070072 // Set fixed point position and quantization info if is allowed
73 const int fixed_point_position = is_data_type_fixed_point(data_type) ? 3 : 0;
74 const QuantizationInfo quantization_info = is_data_type_quantized_asymmetric(data_type) ? QuantizationInfo(1.f / 255.f, 0) : QuantizationInfo();
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010075
76 // Create tensors
Chunosovf450caa2017-11-08 16:09:35 +070077 CLTensor src = create_tensor<CLTensor>(shape, data_type, 1, fixed_point_position, quantization_info);
78 CLTensor dst = create_tensor<CLTensor>(shape, data_type, 1, fixed_point_position, QuantizationInfo(1.f / 256.f, 0));
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010079
80 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
81 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
82
83 // Create and configure function
84 CLSoftmaxLayer smx_layer;
85 smx_layer.configure(&src, &dst);
86
87 // Validate valid region
88 const ValidRegion valid_region = shape_to_valid_region(shape);
89 validate(src.info()->valid_region(), valid_region);
90 validate(dst.info()->valid_region(), valid_region);
91
Chunosovd6afedc2017-11-06 22:09:45 +070092 // Get reduction kernel info
93 CLLogits1DMaxShiftExpSumKernel::ParallelReductionInfo reduction_info = CLLogits1DMaxShiftExpSumKernel::is_parallel_reduction(shape.x());
94
95 // Validate src padding
Chunosovf450caa2017-11-08 16:09:35 +070096 // Legacy path used only by quantized asymmetric data type TODO(COMPMID-661) : Remove when port to new path
97 if(is_data_type_quantized_asymmetric(data_type))
98 {
99 const PaddingSize padding_src = PaddingCalculator(shape.x(), 16).required_padding();
100 validate(src.info()->padding(), padding_src);
101 }
102 else
103 {
104 const PaddingSize padding_src = PaddingCalculator(shape.x(), std::get<1>(reduction_info)).required_padding();
105 validate(src.info()->padding(), padding_src);
106 }
Chunosovd6afedc2017-11-06 22:09:45 +0700107
108 // Validate dst padding
109 const PaddingSize padding_dst = PaddingCalculator(shape.x(), 16).required_padding();
110 validate(dst.info()->padding(), padding_dst);
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100111}
112
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000113// *INDENT-OFF*
114// clang-format off
115DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(
116 framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching data types
117 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching shapes
118 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QS8, 2), // Mismatching fixed point
119 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QASYMM8, // Invalid output quantization info
120 QuantizationInfo(1.f/256, 12)),
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000121 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Window shrink
122 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
123 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QS8, 3),
124 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8,
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000125 QuantizationInfo(1.f/256, 12)),
126 }),
127 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F16),
128 TensorInfo(TensorShape(27U, 11U, 2U), 1, DataType::F32),
129 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QS8, 3),
130 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QASYMM8,
131 QuantizationInfo(1.f/256, 12)),
132 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32),
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000133 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
134 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QS8, 3),
135 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8,
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000136 QuantizationInfo(1.f/256, 0)),
137 })),
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000138 framework::dataset::make("Expected", { false, false, false, false, false, true, true, true })),
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000139 input_info, output_info, expected)
140{
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000141 ARM_COMPUTE_EXPECT(bool(CLSoftmaxLayer::validate(&input_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false))) == expected, framework::LogLevel::ERRORS);
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000142}
143// clang-format on
144// *INDENT-ON*
145
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100146template <typename T>
147using CLSoftmaxLayerFixture = SoftmaxValidationFixture<CLTensor, CLAccessor, CLSoftmaxLayer, T>;
148
149TEST_SUITE(Float)
150TEST_SUITE(FP16)
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100151FIXTURE_DATA_TEST_CASE(RunSmall, CLSoftmaxLayerFixture<half>, framework::DatasetMode::ALL, combine(combine(datasets::SoftmaxLayerSmallShapes(),
152 framework::dataset::make("DataType", DataType::F16)),
153 framework::dataset::make("Beta", { 1.0f, 2.0f })))
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100154{
155 // Validate output
156 validate(CLAccessor(_target), _reference, tolerance_f16);
157}
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100158FIXTURE_DATA_TEST_CASE(RunLarge, CLSoftmaxLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::SoftmaxLayerLargeShapes(),
159 framework::dataset::make("DataType", DataType::F16)),
160 framework::dataset::make("Beta", { 1.0f, 2.0f })))
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100161{
162 // Validate output
163 validate(CLAccessor(_target), _reference, tolerance_f16);
164}
165TEST_SUITE_END()
166
167TEST_SUITE(FP32)
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100168FIXTURE_DATA_TEST_CASE(RunSmall, CLSoftmaxLayerFixture<float>, framework::DatasetMode::ALL, combine(combine(datasets::SoftmaxLayerSmallShapes(),
169 framework::dataset::make("DataType", DataType::F32)),
170 framework::dataset::make("Beta", { 1.0f, 2.0f })))
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100171{
172 // Validate output
173 validate(CLAccessor(_target), _reference, tolerance_f32);
174}
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100175FIXTURE_DATA_TEST_CASE(RunLarge, CLSoftmaxLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::SoftmaxLayerLargeShapes(),
176 framework::dataset::make("DataType", DataType::F32)),
177 framework::dataset::make("Beta", { 1.0f, 2.0f })))
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100178{
179 // Validate output
180 validate(CLAccessor(_target), _reference, tolerance_f32);
181}
182TEST_SUITE_END()
183TEST_SUITE_END()
184
185template <typename T>
186using CLSoftmaxLayerFixedPointFixture = SoftmaxValidationFixedPointFixture<CLTensor, CLAccessor, CLSoftmaxLayer, T>;
187
Chunosovf450caa2017-11-08 16:09:35 +0700188TEST_SUITE(FixedPoint)
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100189TEST_SUITE(QS8)
190// Testing for fixed point position [1,6) as reciprocal limits the maximum fixed point position to 5
Chunosovf450caa2017-11-08 16:09:35 +0700191FIXTURE_DATA_TEST_CASE(RunSmall, CLSoftmaxLayerFixedPointFixture<int8_t>, framework::DatasetMode::ALL, combine(combine(datasets::SoftmaxLayerSmallShapes(), framework::dataset::make("DataType",
192 DataType::QS8)),
193 framework::dataset::make("FractionalBits", 1, 6)))
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100194{
195 // Validate output
196 validate(CLAccessor(_target), _reference, tolerance_fixed_point);
197}
Chunosovd6afedc2017-11-06 22:09:45 +0700198FIXTURE_DATA_TEST_CASE(RunLarge, CLSoftmaxLayerFixedPointFixture<int8_t>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::SoftmaxLayerLargeShapes(), framework::dataset::make("DataType",
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100199 DataType::QS8)),
200 framework::dataset::make("FractionalBits", 1, 6)))
201{
202 // Validate output
203 validate(CLAccessor(_target), _reference, tolerance_fixed_point);
204}
205TEST_SUITE_END()
206
207TEST_SUITE(QS16)
208// Testing for fixed point position [1,14) as reciprocal limits the maximum fixed point position to 14
Chunosovf450caa2017-11-08 16:09:35 +0700209FIXTURE_DATA_TEST_CASE(RunSmall, CLSoftmaxLayerFixedPointFixture<int16_t>, framework::DatasetMode::ALL, combine(combine(datasets::SoftmaxLayerSmallShapes(),
210 framework::dataset::make("DataType",
211 DataType::QS16)),
212 framework::dataset::make("FractionalBits", 1, 14)))
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100213{
214 // Validate output
215 validate(CLAccessor(_target), _reference, tolerance_fixed_point);
216}
Chunosovd6afedc2017-11-06 22:09:45 +0700217FIXTURE_DATA_TEST_CASE(RunLarge, CLSoftmaxLayerFixedPointFixture<int16_t>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::SoftmaxLayerLargeShapes(),
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100218 framework::dataset::make("DataType",
219 DataType::QS16)),
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100220 framework::dataset::make("FractionalBits", 1, 14)))
221{
222 // Validate output
223 validate(CLAccessor(_target), _reference, tolerance_fixed_point);
224}
225TEST_SUITE_END()
226TEST_SUITE_END()
227
Chunosovf450caa2017-11-08 16:09:35 +0700228template <typename T>
229using CLSoftmaxLayerQuantizedFixture = SoftmaxValidationQuantizedFixture<CLTensor, CLAccessor, CLSoftmaxLayer, T>;
230
231TEST_SUITE(Quantized)
232TEST_SUITE(QASYMM8)
233FIXTURE_DATA_TEST_CASE(RunSmall, CLSoftmaxLayerQuantizedFixture<uint8_t>, framework::DatasetMode::ALL, combine(combine(datasets::SoftmaxLayerSmallShapes(),
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100234 framework::dataset::make("DataType", DataType::QASYMM8)),
235 combine(framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, -10) }),
236 framework::dataset::make("Beta", { 1.0f, 2.f }))))
Chunosovf450caa2017-11-08 16:09:35 +0700237{
238 // Validate output
239 validate(CLAccessor(_target), _reference, tolerance_qasymm8);
240}
241FIXTURE_DATA_TEST_CASE(RunLarge, CLSoftmaxLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::SoftmaxLayerLargeShapes(),
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100242 framework::dataset::make("DataType", DataType::QASYMM8)),
243 combine(framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, -10) }),
244 framework::dataset::make("Beta", { 1.0f, 2.0f }))))
Chunosovf450caa2017-11-08 16:09:35 +0700245{
246 // Validate output
247 validate(CLAccessor(_target), _reference, tolerance_qasymm8);
248}
249TEST_SUITE_END()
250TEST_SUITE_END()
251
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100252TEST_SUITE_END()
253TEST_SUITE_END()
254} // namespace validation
255} // namespace test
256} // namespace arm_compute