blob: 5ee929f6b9f6a4fdffa5275f8c764319a23c0a62 [file] [log] [blame]
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +01001/*
Michalis Spyrou80943252019-01-10 17:19:50 +00002 * Copyright (c) 2017-2019 ARM Limited.
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +01003 *
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
Chunosovf450caa2017-11-08 16:09:35 +070050/** Tolerance for quantized operations */
51constexpr AbsoluteTolerance<uint8_t> tolerance_qasymm8(1);
Sang-Hoon Park0779fec2019-11-13 17:08:12 +000052constexpr AbsoluteTolerance<int8_t> tolerance_qasymm8_signed(1);
53
54/*
55 The following tolerance number is used as a workaround for the mismatches
56 caused by float computation in reference (and NEON) kernel
57 and integer computations in OpenCL kernel.
58 COMPMID-2958 is created to investigate this.
59*/
60constexpr float tolerance_number_qasymm8_signed = 0.05f;
Chunosovf450caa2017-11-08 16:09:35 +070061
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010062/** CNN data types */
63const auto CNNDataTypes = framework::dataset::make("DataType",
64{
Chunosovf450caa2017-11-08 16:09:35 +070065 DataType::QASYMM8,
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010066 DataType::F16,
67 DataType::F32,
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010068});
69} // namespace
70
71TEST_SUITE(CL)
72TEST_SUITE(SoftmaxLayer)
73
Michalis Spyrou80943252019-01-10 17:19:50 +000074DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(datasets::SoftmaxLayerSmallShapes(), CNNDataTypes), shape, data_type)
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010075{
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010076 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 +010077
78 // Create tensors
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010079 CLTensor src = create_tensor<CLTensor>(shape, data_type, 1, quantization_info);
80 CLTensor dst = create_tensor<CLTensor>(shape, data_type, 1, QuantizationInfo(1.f / 256.f, 0));
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010081
82 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
83 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
84
85 // Create and configure function
86 CLSoftmaxLayer smx_layer;
87 smx_layer.configure(&src, &dst);
88
89 // Validate valid region
90 const ValidRegion valid_region = shape_to_valid_region(shape);
91 validate(src.info()->valid_region(), valid_region);
92 validate(dst.info()->valid_region(), valid_region);
93
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010094 // CLLogits1DMaxShiftExpSumKernel configures the paddings only in the 2D case
95 if(shape.num_dimensions() <= 2)
96 {
97 // Get reduction kernel info
98 CLLogits1DMaxShiftExpSumKernel::ParallelReductionInfo reduction_info = CLLogits1DMaxShiftExpSumKernel::is_parallel_reduction(shape.x());
Chunosovd6afedc2017-11-06 22:09:45 +070099
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100100 // Validate src padding for 2D softmax
101 const PaddingSize padding_src = PaddingCalculator(shape.x(), std::get<1>(reduction_info)).required_padding();
102 validate(src.info()->padding(), padding_src);
Chunosovd6afedc2017-11-06 22:09:45 +0700103
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100104 // Validate dst padding for 2D softmax
105 const PaddingSize padding_dst = PaddingCalculator(shape.x(), 16).required_padding();
106 validate(dst.info()->padding(), padding_dst);
107 }
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100108}
109
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000110// *INDENT-OFF*
111// clang-format off
112DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(
Georgios Pinitas17d6d3c2018-07-02 17:52:40 +0100113 framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U), 1, DataType::F32), // Mismatching data types
114 TensorInfo(TensorShape(27U, 13U), 1, DataType::F32), // Mismatching shapes
115 TensorInfo(TensorShape(27U, 13U), 1, DataType::QASYMM8, // Invalid output quantization info
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000116 QuantizationInfo(1.f/256, 12)),
Georgios Pinitas17d6d3c2018-07-02 17:52:40 +0100117 TensorInfo(TensorShape(27U, 13U), 1, DataType::F32), // Window shrink
118 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32),// Invalid input dimensionality
119 TensorInfo(TensorShape(32U, 13U), 1, DataType::F32),
120 TensorInfo(TensorShape(32U, 13U), 1, DataType::QASYMM8,
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000121 QuantizationInfo(1.f/256, 12)),
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000122 TensorInfo(TensorShape(32U, 13U), 1, DataType::QASYMM8_SIGNED,
123 QuantizationInfo(1.f/256, 12))
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000124 }),
Georgios Pinitas17d6d3c2018-07-02 17:52:40 +0100125 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U), 1, DataType::F16),
126 TensorInfo(TensorShape(27U, 11U), 1, DataType::F32),
127 TensorInfo(TensorShape(27U, 13U), 1, DataType::QASYMM8,
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000128 QuantizationInfo(1.f/256, 12)),
Georgios Pinitas17d6d3c2018-07-02 17:52:40 +0100129 TensorInfo(TensorShape(27U, 13U), 1, DataType::F32),
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000130 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32),
Georgios Pinitas17d6d3c2018-07-02 17:52:40 +0100131 TensorInfo(TensorShape(32U, 13U), 1, DataType::F32),
132 TensorInfo(TensorShape(32U, 13U), 1, DataType::QASYMM8,
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000133 QuantizationInfo(1.f/256, 0)),
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000134 TensorInfo(TensorShape(32U, 13U), 1, DataType::QASYMM8_SIGNED,
135 QuantizationInfo(1.f/256, -128)),
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000136 })),
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000137 framework::dataset::make("Expected", { false, false, false, false, false, true, true, true })),
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000138 input_info, output_info, expected)
139{
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000140 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 +0000141}
142// clang-format on
143// *INDENT-ON*
144
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100145template <typename T>
146using CLSoftmaxLayerFixture = SoftmaxValidationFixture<CLTensor, CLAccessor, CLSoftmaxLayer, T>;
147
148TEST_SUITE(Float)
149TEST_SUITE(FP16)
giuros01efbf6c82018-09-03 09:53:53 +0100150FIXTURE_DATA_TEST_CASE(RunSmall, CLSoftmaxLayerFixture<half>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SoftmaxLayerSmallShapes(),
151 framework::dataset::make("DataType", DataType::F16)),
152 framework::dataset::make("Beta", { 1.0f, 2.0f })),
153 framework::dataset::make("Axis", { 1, 2 })))
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100154{
155 // Validate output
156 validate(CLAccessor(_target), _reference, tolerance_f16);
157}
giuros01efbf6c82018-09-03 09:53:53 +0100158FIXTURE_DATA_TEST_CASE(RunLarge, CLSoftmaxLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::SoftmaxLayerLargeShapes(),
159 framework::dataset::make("DataType", DataType::F16)),
160 framework::dataset::make("Beta", { 1.0f, 2.0f })),
161 framework::dataset::make("Axis", { 1, 2 })))
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100162{
163 // Validate output
164 validate(CLAccessor(_target), _reference, tolerance_f16);
165}
giuros01efbf6c82018-09-03 09:53:53 +0100166FIXTURE_DATA_TEST_CASE(Run4D, CLSoftmaxLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::SoftmaxLayer4DShapes(),
167 framework::dataset::make("DataType", DataType::F16)),
168 framework::dataset::make("Beta", { 1.0f, 2.0f })),
169 framework::dataset::make("Axis", { 1, 2, 3 })))
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100170{
171 // Validate output
172 validate(CLAccessor(_target), _reference, tolerance_f16);
173}
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100174TEST_SUITE_END()
175
176TEST_SUITE(FP32)
giuros01efbf6c82018-09-03 09:53:53 +0100177FIXTURE_DATA_TEST_CASE(RunSmall, CLSoftmaxLayerFixture<float>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SoftmaxLayerSmallShapes(),
178 framework::dataset::make("DataType", DataType::F32)),
179 framework::dataset::make("Beta", { 1.0f, 2.0f })),
180 framework::dataset::make("Axis", { 1, 2 })))
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100181{
182 // Validate output
183 validate(CLAccessor(_target), _reference, tolerance_f32);
184}
giuros01efbf6c82018-09-03 09:53:53 +0100185FIXTURE_DATA_TEST_CASE(RunLarge, CLSoftmaxLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::SoftmaxLayerLargeShapes(),
186 framework::dataset::make("DataType", DataType::F32)),
187 framework::dataset::make("Beta", { 1.0f, 2.0f })),
188 framework::dataset::make("Axis", { 1, 2 })))
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100189{
190 // Validate output
191 validate(CLAccessor(_target), _reference, tolerance_f32);
192}
giuros01efbf6c82018-09-03 09:53:53 +0100193FIXTURE_DATA_TEST_CASE(Run4D, CLSoftmaxLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::SoftmaxLayer4DShapes(),
194 framework::dataset::make("DataType", DataType::F32)),
195 framework::dataset::make("Beta", { 1.0f, 2.0f })),
196 framework::dataset::make("Axis", { 1, 2, 3 })))
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100197{
198 // Validate output
199 validate(CLAccessor(_target), _reference, tolerance_f32);
200}
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100201TEST_SUITE_END()
202TEST_SUITE_END()
203
204template <typename T>
Chunosovf450caa2017-11-08 16:09:35 +0700205using CLSoftmaxLayerQuantizedFixture = SoftmaxValidationQuantizedFixture<CLTensor, CLAccessor, CLSoftmaxLayer, T>;
206
207TEST_SUITE(Quantized)
208TEST_SUITE(QASYMM8)
giuros01efbf6c82018-09-03 09:53:53 +0100209FIXTURE_DATA_TEST_CASE(RunSmall, CLSoftmaxLayerQuantizedFixture<uint8_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SoftmaxLayerSmallShapes(),
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100210 framework::dataset::make("DataType", DataType::QASYMM8)),
giuros01efbf6c82018-09-03 09:53:53 +0100211 combine(framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, -10) }),
212 framework::dataset::make("Beta", { 1.0f, 2.f }))),
213 framework::dataset::make("Axis", { 1, 2 })))
Chunosovf450caa2017-11-08 16:09:35 +0700214{
215 // Validate output
216 validate(CLAccessor(_target), _reference, tolerance_qasymm8);
217}
giuros01efbf6c82018-09-03 09:53:53 +0100218FIXTURE_DATA_TEST_CASE(RunLarge, CLSoftmaxLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::SoftmaxLayerLargeShapes(),
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100219 framework::dataset::make("DataType", DataType::QASYMM8)),
220 combine(framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, -10) }),
giuros01efbf6c82018-09-03 09:53:53 +0100221 framework::dataset::make("Beta", { 1.0f, 2.0f }))),
222 framework::dataset::make("Axis", { 1 })))
Chunosovf450caa2017-11-08 16:09:35 +0700223{
224 // Validate output
225 validate(CLAccessor(_target), _reference, tolerance_qasymm8);
226}
giuros01efbf6c82018-09-03 09:53:53 +0100227FIXTURE_DATA_TEST_CASE(Run4D, CLSoftmaxLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::SoftmaxLayer4DShapes(),
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100228 framework::dataset::make("DataType", DataType::QASYMM8)),
giuros01efbf6c82018-09-03 09:53:53 +0100229 combine(framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, -10) }),
230 framework::dataset::make("Beta", { 1.0f, 2.0f }))),
231 framework::dataset::make("Axis", { 1, 2, 3 })))
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100232{
233 // Validate output
234 validate(CLAccessor(_target), _reference, tolerance_qasymm8);
235}
236
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000237TEST_SUITE_END() // QASYMM8
Chunosovf450caa2017-11-08 16:09:35 +0700238
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000239TEST_SUITE(QASYMM8_SIGNED)
240
241FIXTURE_DATA_TEST_CASE(RunSmall, CLSoftmaxLayerQuantizedFixture<int8_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SoftmaxLayerSmallShapes(),
242 framework::dataset::make("DataType", DataType::QASYMM8_SIGNED)),
243 combine(framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, -10) }),
244 framework::dataset::make("Beta", { 1.0f, 2.f }))),
245 framework::dataset::make("Axis", { 1, 2 })))
246{
247 // Validate output
248 validate(CLAccessor(_target), _reference, tolerance_qasymm8_signed, tolerance_number_qasymm8_signed);
249}
250
251TEST_SUITE_END() // QASYMM8_SIGNED
252TEST_SUITE_END() // Quantized
253TEST_SUITE_END() // SoftmaxLayer
254TEST_SUITE_END() // CL
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100255} // namespace validation
256} // namespace test
257} // namespace arm_compute