blob: b372bdf3facf0b55e4fa3b94392850a8277bb658 [file] [log] [blame]
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +01001/*
Matthew Bentham314d3e22023-06-23 10:53:52 +00002 * Copyright (c) 2017-2020, 2022-2023 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 */
24#include "arm_compute/core/Types.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000025#include "arm_compute/core/utils/StringUtils.h"
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010026#include "arm_compute/runtime/NEON/functions/NESoftmaxLayer.h"
27#include "arm_compute/runtime/Tensor.h"
28#include "arm_compute/runtime/TensorAllocator.h"
Dana Zlotnik6a2df882022-01-17 09:54:26 +020029#include "src/common/cpuinfo/CpuIsaInfo.h"
30#include "src/cpu/kernels/CpuSoftmaxKernel.h"
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010031#include "tests/NEON/Accessor.h"
32#include "tests/PaddingCalculator.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010033#include "tests/datasets/ShapeDatasets.h"
34#include "tests/framework/Asserts.h"
35#include "tests/framework/Macros.h"
36#include "tests/framework/datasets/Datasets.h"
37#include "tests/validation/Validation.h"
38#include "tests/validation/fixtures/SoftmaxLayerFixture.h"
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010039namespace arm_compute
40{
41namespace test
42{
43namespace validation
44{
45namespace
46{
47/** Tolerance for float operations */
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010048constexpr AbsoluteTolerance<float> tolerance_f32(0.000001f);
Manuel Bottini678d83a2019-01-07 16:05:36 +000049RelativeTolerance<half> tolerance_f16(half(0.2));
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010050
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +000051/** Tolerance for quantized operations */
52constexpr AbsoluteTolerance<uint8_t> tolerance_qasymm8(1);
Sang-Hoon Parkc3a74202019-11-22 16:05:46 +000053constexpr AbsoluteTolerance<int8_t> tolerance_qasymm8_signed(1);
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +000054
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010055/** CNN data types */
56const auto CNNDataTypes = framework::dataset::make("DataType",
57{
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +000058#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010059 DataType::F16,
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +000060#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010061 DataType::F32,
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010062});
63} // namespace
64
65TEST_SUITE(NEON)
66TEST_SUITE(SoftmaxLayer)
Michalis Spyrouafa5d812017-11-30 14:25:57 +000067// *INDENT-OFF*
68// clang-format off
Manuel Bottini678d83a2019-01-07 16:05:36 +000069DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip(
70 framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U), 1, DataType::F32), // Mismatching data types
71 TensorInfo(TensorShape(27U, 13U), 1, DataType::F32), // Mismatching shapes
72 TensorInfo(TensorShape(27U, 13U), 1, DataType::QASYMM8, // Invalid output quantization info
73 QuantizationInfo(1.f/256, 12)),
Manuel Bottini678d83a2019-01-07 16:05:36 +000074 TensorInfo(TensorShape(32U, 13U), 1, DataType::F32),
75 TensorInfo(TensorShape(32U, 13U), 1, DataType::QASYMM8,
76 QuantizationInfo(1.f/256, 12)),
SiCong Li96209c72020-08-21 12:28:30 +010077 TensorInfo(TensorShape(32U, 13U), 1, DataType::F32),
SiCong Lid004a7a2020-05-28 15:26:41 +010078 TensorInfo(TensorShape(32U, 13U), 1, DataType::QASYMM8, //Invalid axis high
79 QuantizationInfo(1.f/256, 12)),
80 TensorInfo(TensorShape(32U, 13U), 1, DataType::QASYMM8, //Invalid axis low
Manuel Bottini678d83a2019-01-07 16:05:36 +000081 QuantizationInfo(1.f/256, 12)),
82 }),
83 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U), 1, DataType::F16),
84 TensorInfo(TensorShape(27U, 11U), 1, DataType::F32),
85 TensorInfo(TensorShape(27U, 13U), 1, DataType::QASYMM8,
86 QuantizationInfo(1.f/256, 12)),
Manuel Bottini678d83a2019-01-07 16:05:36 +000087 TensorInfo(TensorShape(32U, 13U), 1, DataType::F32),
88 TensorInfo(TensorShape(32U, 13U), 1, DataType::QASYMM8,
89 QuantizationInfo(1.f/256, 0)),
SiCong Li96209c72020-08-21 12:28:30 +010090 TensorInfo(TensorShape(32U, 13U), 1, DataType::F32),
Manuel Bottini678d83a2019-01-07 16:05:36 +000091 TensorInfo(TensorShape(32U, 13U), 1, DataType::QASYMM8,
92 QuantizationInfo(1.f/256, 0)),
SiCong Lid004a7a2020-05-28 15:26:41 +010093 TensorInfo(TensorShape(32U, 13U), 1, DataType::QASYMM8,
94 QuantizationInfo(1.f/256, 0)),
Manuel Bottini678d83a2019-01-07 16:05:36 +000095 })),
96 framework::dataset::make("beta", { 1.0,
97 2.0,
98 1.0,
99 2.0,
100 1.0,
SiCong Li96209c72020-08-21 12:28:30 +0100101 1.0,
Manuel Bottini678d83a2019-01-07 16:05:36 +0000102 2.0,
103 1.0,
Manuel Bottini678d83a2019-01-07 16:05:36 +0000104 })),
morgolock9c7fed82020-08-05 12:30:56 +0100105 framework::dataset::make("axis", { 0,
SiCong Lid004a7a2020-05-28 15:26:41 +0100106 0,
107 0,
SiCong Li96209c72020-08-21 12:28:30 +0100108 1,
morgolock9c7fed82020-08-05 12:30:56 +0100109 0,
SiCong Li96209c72020-08-21 12:28:30 +0100110 -1,
SiCong Lid004a7a2020-05-28 15:26:41 +0100111 2,
Sheri Zhang1f567af2020-05-05 11:47:36 +0100112 -3,
Manuel Bottini678d83a2019-01-07 16:05:36 +0000113 })),
SiCong Li96209c72020-08-21 12:28:30 +0100114 framework::dataset::make("Expected", { false, false, false, true, true, true, false, false })),
morgolock9c7fed82020-08-05 12:30:56 +0100115 input_info, output_info, beta, axis, expected)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000116{
morgolock9c7fed82020-08-05 12:30:56 +0100117 ARM_COMPUTE_EXPECT(bool(NESoftmaxLayer::validate(&input_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false), beta, axis)) == expected, framework::LogLevel::ERRORS);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000118}
119// clang-format on
120// *INDENT-ON*
121
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100122template <typename T>
123using NESoftmaxLayerFixture = SoftmaxValidationFixture<Tensor, Accessor, NESoftmaxLayer, T>;
124
Dana Zlotnik6a2df882022-01-17 09:54:26 +0200125DATA_TEST_CASE(KernelSelection_max_logits, framework::DatasetMode::ALL, concat(
126 combine(framework::dataset::make("CpuExt", std::string("NEON")),
127 framework::dataset::make("DataType", { DataType::F32,
128 DataType::F16,
129 DataType::QASYMM8,
130 DataType::QASYMM8_SIGNED
131 })),
132 combine(framework::dataset::make("CpuExt", std::string("SVE")),
133 framework::dataset::make("DataType", { DataType::F32,
134 DataType::F16,
135 DataType::QASYMM8,
136 DataType::QASYMM8_SIGNED
137 }))),
138 cpu_ext, data_type)
139{
140 using namespace cpu::kernels;
141
142 cpuinfo::CpuIsaInfo cpu_isa{};
143 cpu_isa.neon = (cpu_ext == "NEON");
144 cpu_isa.sve = (cpu_ext == "SVE");
145 cpu_isa.fp16 = (data_type == DataType::F16);
146
147 const auto *selected_impl = CpuLogits1DMaxKernel::get_implementation(DataTypeISASelectorData{ data_type, cpu_isa }, cpu::KernelSelectionType::Preferred);
148
149 ARM_COMPUTE_ERROR_ON_NULLPTR(selected_impl);
150
151 std::string expected = lower_string(cpu_ext) + "_" + cpu_impl_dt(data_type) + "_logits_1d_max";
152 std::string actual = selected_impl->name;
153
154 ARM_COMPUTE_EXPECT_EQUAL(expected, actual, framework::LogLevel::ERRORS);
155}
156
157DATA_TEST_CASE(KernelSelection_logits, framework::DatasetMode::ALL, concat(concat(
158 combine(framework::dataset::make("CpuExt", std::string("NEON")),
159 framework::dataset::make("DataType", { DataType::F32,
160 DataType::F16,
161 DataType::QASYMM8,
162 DataType::QASYMM8_SIGNED
163 })),
164 combine(framework::dataset::make("CpuExt", std::string("SVE")),
165 framework::dataset::make("DataType", { DataType::F32,
166 DataType::F16
167 }))),
168 combine(framework::dataset::make("CpuExt", std::string("SVE2")),
169 framework::dataset::make("DataType", { DataType::QASYMM8,
170 DataType::QASYMM8_SIGNED
171 }))),
172 cpu_ext, data_type)
173{
174 using namespace cpu::kernels;
175
176 cpuinfo::CpuIsaInfo cpu_isa{};
177 cpu_isa.neon = (cpu_ext == "NEON");
178 cpu_isa.sve = (cpu_ext == "SVE");
179 cpu_isa.sve2 = (cpu_ext == "SVE2");
180 cpu_isa.fp16 = (data_type == DataType::F16);
181
182 const auto *selected_impl = CpuLogits1DSoftmaxKernel<false>::get_implementation(DataTypeISASelectorData{ data_type, cpu_isa }, cpu::KernelSelectionType::Preferred);
183
184 ARM_COMPUTE_ERROR_ON_NULLPTR(selected_impl);
185
186 std::string expected = lower_string(cpu_ext) + "_" + cpu_impl_dt(data_type) + "_softmax_logits_1d";
187 std::string actual = selected_impl->name;
188
189 ARM_COMPUTE_EXPECT_EQUAL(expected, actual, framework::LogLevel::ERRORS);
190}
191
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100192TEST_SUITE(Float)
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000193#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100194TEST_SUITE(FP16)
Manuel Bottini678d83a2019-01-07 16:05:36 +0000195FIXTURE_DATA_TEST_CASE(RunSmall, NESoftmaxLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::Small4DShapes(),
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100196 framework::dataset::make("DataType", DataType::F16)),
giuros01efbf6c82018-09-03 09:53:53 +0100197 framework::dataset::make("Beta", { 1.0f, 2.0f })),
SiCong Li96209c72020-08-21 12:28:30 +0100198 framework::dataset::make("Axis", { 0, 1 })))
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100199{
200 // Validate output
Manuel Bottini678d83a2019-01-07 16:05:36 +0000201 validate(Accessor(_target), _reference, tolerance_f16);
202}
203FIXTURE_DATA_TEST_CASE(RunSmall4D, NESoftmaxLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::Small4DShapes(),
Manuel Bottini21079dd2019-10-29 17:20:09 +0000204 framework::dataset::make("DataType", DataType::F16)),
Manuel Bottini678d83a2019-01-07 16:05:36 +0000205 framework::dataset::make("Beta", { 1.0f, 2.0f })),
SiCong Li96209c72020-08-21 12:28:30 +0100206 framework::dataset::make("Axis", { 0, 2, -1 })))
Manuel Bottini678d83a2019-01-07 16:05:36 +0000207{
208 // Validate output
Manuel Bottini21079dd2019-10-29 17:20:09 +0000209 validate(Accessor(_target), _reference, tolerance_f16);
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100210}
giuros01efbf6c82018-09-03 09:53:53 +0100211FIXTURE_DATA_TEST_CASE(RunLarge, NESoftmaxLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::SoftmaxLayerLargeShapes(),
212 framework::dataset::make("DataType", DataType::F16)),
213 framework::dataset::make("Beta", { 1.0f, 2.0f })),
morgolock9c7fed82020-08-05 12:30:56 +0100214 framework::dataset::make("Axis", { 0 })))
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100215{
216 // Validate output
Manuel Bottini678d83a2019-01-07 16:05:36 +0000217 validate(Accessor(_target), _reference, tolerance_f16);
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100218}
Manuel Bottini678d83a2019-01-07 16:05:36 +0000219TEST_SUITE_END() //FP16
220#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100221
222TEST_SUITE(FP32)
Manuel Bottini678d83a2019-01-07 16:05:36 +0000223FIXTURE_DATA_TEST_CASE(RunSmall2D, NESoftmaxLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SoftmaxLayerSmallShapes(),
224 framework::dataset::make("DataType", DataType::F32)),
225 framework::dataset::make("Beta", { 1.0f, 2.0f })),
SiCong Li96209c72020-08-21 12:28:30 +0100226 framework::dataset::make("Axis", { 0, -1 })))
Manuel Bottini678d83a2019-01-07 16:05:36 +0000227{
228 // Validate output
229 validate(Accessor(_target), _reference, tolerance_f32);
230}
231FIXTURE_DATA_TEST_CASE(RunSmall4D, NESoftmaxLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::Small4DShapes(),
232 framework::dataset::make("DataType", DataType::F32)),
233 framework::dataset::make("Beta", { 1.0f, 2.0f })),
SiCong Li96209c72020-08-21 12:28:30 +0100234 framework::dataset::make("Axis", { 0, -2, 3 })))
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100235{
236 // Validate output
237 validate(Accessor(_target), _reference, tolerance_f32);
238}
giuros01efbf6c82018-09-03 09:53:53 +0100239FIXTURE_DATA_TEST_CASE(RunLarge, NESoftmaxLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::SoftmaxLayerLargeShapes(),
240 framework::dataset::make("DataType", DataType::F32)),
241 framework::dataset::make("Beta", { 1.0f, 2.0f })),
morgolock9c7fed82020-08-05 12:30:56 +0100242 framework::dataset::make("Axis", { 0 })))
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100243{
244 // Validate output
245 validate(Accessor(_target), _reference, tolerance_f32);
246}
Manuel Bottini678d83a2019-01-07 16:05:36 +0000247TEST_SUITE_END() //FP32
248TEST_SUITE_END() //Float
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100249
250template <typename T>
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000251using NESoftmaxLayerQuantizedFixture = SoftmaxValidationQuantizedFixture<Tensor, Accessor, NESoftmaxLayer, T>;
252
253TEST_SUITE(Quantized)
254TEST_SUITE(QASYMM8)
Manuel Bottini678d83a2019-01-07 16:05:36 +0000255FIXTURE_DATA_TEST_CASE(RunSmall2D, NESoftmaxLayerQuantizedFixture<uint8_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SoftmaxLayerSmallShapes(),
256 framework::dataset::make("DataType", DataType::QASYMM8)),
257 combine(framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, -10) }),
258 framework::dataset::make("Beta", { 1.0f, 2.f }))),
SiCong Li96209c72020-08-21 12:28:30 +0100259 framework::dataset::make("Axis", { 0, -1 })))
Manuel Bottini678d83a2019-01-07 16:05:36 +0000260{
261 // Validate output
262 validate(Accessor(_target), _reference, tolerance_qasymm8);
263}
264FIXTURE_DATA_TEST_CASE(RunSmall4D, NESoftmaxLayerQuantizedFixture<uint8_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::Small4DShapes(),
265 framework::dataset::make("DataType", DataType::QASYMM8)),
266 combine(framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, -10) }),
267 framework::dataset::make("Beta", { 1.0f, 2.f }))),
SiCong Li96209c72020-08-21 12:28:30 +0100268 framework::dataset::make("Axis", { 0, 1, -2 })))
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000269{
270 // Validate output
271 validate(Accessor(_target), _reference, tolerance_qasymm8);
272}
giuros01efbf6c82018-09-03 09:53:53 +0100273FIXTURE_DATA_TEST_CASE(RunLarge, NESoftmaxLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::SoftmaxLayerLargeShapes(),
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000274 framework::dataset::make("DataType", DataType::QASYMM8)),
275 combine(framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, -10) }),
giuros01efbf6c82018-09-03 09:53:53 +0100276 framework::dataset::make("Beta", { 1.0f, 2.0f }))),
morgolock9c7fed82020-08-05 12:30:56 +0100277 framework::dataset::make("Axis", { 0 })))
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000278{
279 // Validate output
280 validate(Accessor(_target), _reference, tolerance_qasymm8);
281}
Manuel Bottini678d83a2019-01-07 16:05:36 +0000282TEST_SUITE_END() //QASYMM8
Sang-Hoon Parkc3a74202019-11-22 16:05:46 +0000283
284TEST_SUITE(QASYMM8_SIGNED)
285FIXTURE_DATA_TEST_CASE(RunSmall2D, NESoftmaxLayerQuantizedFixture<int8_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SoftmaxLayerSmallShapes(),
286 framework::dataset::make("DataType", DataType::QASYMM8_SIGNED)),
287 combine(framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, -10) }),
288 framework::dataset::make("Beta", { 1.0f, 2.f }))),
SiCong Li96209c72020-08-21 12:28:30 +0100289 framework::dataset::make("Axis", { 0, -1 })))
Sang-Hoon Parkc3a74202019-11-22 16:05:46 +0000290{
291 // Validate output
292 validate(Accessor(_target), _reference, tolerance_qasymm8_signed);
293}
294FIXTURE_DATA_TEST_CASE(RunSmall4D, NESoftmaxLayerQuantizedFixture<int8_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::Small4DShapes(),
295 framework::dataset::make("DataType", DataType::QASYMM8_SIGNED)),
296 combine(framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, -10) }),
297 framework::dataset::make("Beta", { 1.0f, 2.f }))),
SiCong Li96209c72020-08-21 12:28:30 +0100298 framework::dataset::make("Axis", { 0, 1, -1 })))
Sang-Hoon Parkc3a74202019-11-22 16:05:46 +0000299{
300 // Validate output
301 validate(Accessor(_target), _reference, tolerance_qasymm8_signed);
302}
303TEST_SUITE_END() //QASYMM8_SIGNED
304
Manuel Bottini678d83a2019-01-07 16:05:36 +0000305TEST_SUITE_END() //Quantized
giuros01efbf6c82018-09-03 09:53:53 +0100306
Manuel Bottini678d83a2019-01-07 16:05:36 +0000307TEST_SUITE_END() //SoftmaxLayer
308TEST_SUITE_END() //NEON
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100309} // namespace validation
310} // namespace test
311} // namespace arm_compute