blob: eb47b7f666c302bf4ee6c7b197d15f598b97d130 [file] [log] [blame]
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +01001/*
Sang-Hoon Parkd0c9cb82021-03-11 15:29:19 +00002 * Copyright (c) 2017-2021 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"
25#include "arm_compute/runtime/CL/CLTensor.h"
26#include "arm_compute/runtime/CL/CLTensorAllocator.h"
27#include "arm_compute/runtime/CL/functions/CLSoftmaxLayer.h"
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010028#include "tests/CL/CLAccessor.h"
29#include "tests/PaddingCalculator.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010030#include "tests/datasets/ShapeDatasets.h"
31#include "tests/framework/Asserts.h"
32#include "tests/framework/Macros.h"
33#include "tests/framework/datasets/Datasets.h"
34#include "tests/validation/Validation.h"
35#include "tests/validation/fixtures/SoftmaxLayerFixture.h"
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010036
Sang-Hoon Parkd0c9cb82021-03-11 15:29:19 +000037#include "arm_compute/runtime/MemoryManagerOnDemand.h"
38#include "arm_compute/runtime/PoolManager.h"
39#include "arm_compute/runtime/BlobLifetimeManager.h"
40#include "arm_compute/runtime/CL/CLBufferAllocator.h"
41#include "arm_compute/runtime/BlobMemoryPool.h"
42
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010043namespace arm_compute
44{
45namespace test
46{
47namespace validation
48{
49namespace
50{
51/** Tolerance for float operations */
Georgios Pinitas583137c2017-08-31 18:12:42 +010052RelativeTolerance<half> tolerance_f16(half(0.2));
53RelativeTolerance<float> tolerance_f32(0.001f);
steniu013e05e4e2017-08-25 17:18:01 +010054
Chunosovf450caa2017-11-08 16:09:35 +070055/** Tolerance for quantized operations */
56constexpr AbsoluteTolerance<uint8_t> tolerance_qasymm8(1);
Sang-Hoon Park0779fec2019-11-13 17:08:12 +000057constexpr AbsoluteTolerance<int8_t> tolerance_qasymm8_signed(1);
58
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010059/** CNN data types */
60const auto CNNDataTypes = framework::dataset::make("DataType",
61{
Chunosovf450caa2017-11-08 16:09:35 +070062 DataType::QASYMM8,
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010063 DataType::F16,
64 DataType::F32,
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010065});
66} // namespace
67
68TEST_SUITE(CL)
69TEST_SUITE(SoftmaxLayer)
70
Sang-Hoon Parkd0c9cb82021-03-11 15:29:19 +000071TEST_CASE(SimpleMemoryManaged, framework::DatasetMode::ALL)
72{
73 // The purpose of this test is to test if the function can
74 // run correctly even with the given memory manager from its caller
75 // (Similar scenario when the library is integrated into other software)
76 // especially when working with workspace() method of
77 // @ref arm_compute::opencl::ClSoftmax.
78 const auto shape = TensorShape{4,2}; // Random shape, not important
79 constexpr auto dt = DataType::F32; // Random data type, not important
80
81 // Create a memory manager
82 auto lm = std::make_shared<BlobLifetimeManager>();
83 auto pm = std::make_shared<arm_compute::PoolManager>();
84 auto alloc = std::make_unique<CLBufferAllocator>();
85 auto mm = std::make_shared<MemoryManagerOnDemand>(lm, pm);
86
87 auto src = create_tensor<CLTensor>(shape, dt);
88 auto dst = create_tensor<CLTensor>(shape, dt);
89 src.allocator()->allocate();
90 dst.allocator()->allocate();
91
92 // Create the function with the memory manager
93 CLSoftmaxLayer smx(mm);
94 smx.configure(&src, &dst);
95
96 // Populate the memory, acquire() will happen in run()
97 mm->populate(*alloc.get(), 1);
98
99 std::vector<float> input_vals{0.0f, 1.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f,};
100 library->fill_static_values(CLAccessor(src), input_vals);
101
102 smx.run();
103
104 // Compute reference to compare
105 SimpleTensor<float> ref_src{shape, dt};
106 library->fill_static_values(ref_src, input_vals);
107 auto ref_dst = reference::softmax_layer<float>(ref_src, 1., 0, false);
108
109 validate(CLAccessor(dst), ref_dst);
110}
111
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000112// *INDENT-OFF*
113// clang-format off
SiCong Lid004a7a2020-05-28 15:26:41 +0100114DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip(
Georgios Pinitas17d6d3c2018-07-02 17:52:40 +0100115 framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U), 1, DataType::F32), // Mismatching data types
116 TensorInfo(TensorShape(27U, 13U), 1, DataType::F32), // Mismatching shapes
117 TensorInfo(TensorShape(27U, 13U), 1, DataType::QASYMM8, // Invalid output quantization info
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000118 QuantizationInfo(1.f/256, 12)),
Georgios Pinitas17d6d3c2018-07-02 17:52:40 +0100119 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,
SiCong Lid004a7a2020-05-28 15:26:41 +0100123 QuantizationInfo(1.f/256, 12)),
124 TensorInfo(TensorShape(32U, 13U), 1, DataType::QASYMM8_SIGNED, // Invalid axis high
125 QuantizationInfo(1.f/256, 12)),
126 TensorInfo(TensorShape(32U, 13U), 1, DataType::QASYMM8_SIGNED, // Invalid axis low
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000127 QuantizationInfo(1.f/256, 12))
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000128 }),
Georgios Pinitas17d6d3c2018-07-02 17:52:40 +0100129 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U), 1, DataType::F16),
130 TensorInfo(TensorShape(27U, 11U), 1, DataType::F32),
131 TensorInfo(TensorShape(27U, 13U), 1, DataType::QASYMM8,
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000132 QuantizationInfo(1.f/256, 12)),
Georgios Pinitas17d6d3c2018-07-02 17:52:40 +0100133 TensorInfo(TensorShape(32U, 13U), 1, DataType::F32),
134 TensorInfo(TensorShape(32U, 13U), 1, DataType::QASYMM8,
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000135 QuantizationInfo(1.f/256, 0)),
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000136 TensorInfo(TensorShape(32U, 13U), 1, DataType::QASYMM8_SIGNED,
137 QuantizationInfo(1.f/256, -128)),
SiCong Li96209c72020-08-21 12:28:30 +0100138 TensorInfo(TensorShape(32U, 13U), 1, DataType::QASYMM8_SIGNED,
SiCong Lid004a7a2020-05-28 15:26:41 +0100139 QuantizationInfo(1.f/256, -128)),
SiCong Li96209c72020-08-21 12:28:30 +0100140 TensorInfo(TensorShape(32U, 13U), 1, DataType::QASYMM8_SIGNED,
SiCong Lid004a7a2020-05-28 15:26:41 +0100141 QuantizationInfo(1.f/256, -128)),
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000142 })),
SiCong Lid004a7a2020-05-28 15:26:41 +0100143 framework::dataset::make("beta", { 1.0,
144 2.0,
145 1.0,
146 2.0,
147 1.0,
148 2.0,
149 1.0,
150 2.0,
SiCong Lid004a7a2020-05-28 15:26:41 +0100151 })),
morgolock9c7fed82020-08-05 12:30:56 +0100152 framework::dataset::make("axis", {
SiCong Lid004a7a2020-05-28 15:26:41 +0100153 0,
154 0,
155 0,
SiCong Li96209c72020-08-21 12:28:30 +0100156 1,
morgolock9c7fed82020-08-05 12:30:56 +0100157 0,
SiCong Lid004a7a2020-05-28 15:26:41 +0100158 -1,
SiCong Li96209c72020-08-21 12:28:30 +0100159 2,
160 -3,
SiCong Lid004a7a2020-05-28 15:26:41 +0100161 })),
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000162 framework::dataset::make("Expected", { false, false, false, true, true, true, false, false })),
morgolock9c7fed82020-08-05 12:30:56 +0100163 input_info, output_info, beta, axis, expected)
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000164{
morgolock9c7fed82020-08-05 12:30:56 +0100165 ARM_COMPUTE_EXPECT(bool(CLSoftmaxLayer::validate(&input_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false), beta, axis)) == expected, framework::LogLevel::ERRORS);
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000166}
167// clang-format on
168// *INDENT-ON*
169
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100170template <typename T>
171using CLSoftmaxLayerFixture = SoftmaxValidationFixture<CLTensor, CLAccessor, CLSoftmaxLayer, T>;
172
173TEST_SUITE(Float)
174TEST_SUITE(FP16)
giuros01efbf6c82018-09-03 09:53:53 +0100175FIXTURE_DATA_TEST_CASE(RunSmall, CLSoftmaxLayerFixture<half>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SoftmaxLayerSmallShapes(),
176 framework::dataset::make("DataType", DataType::F16)),
177 framework::dataset::make("Beta", { 1.0f, 2.0f })),
SiCong Li96209c72020-08-21 12:28:30 +0100178 framework::dataset::make("Axis", { 0, -1 })))
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100179{
180 // Validate output
181 validate(CLAccessor(_target), _reference, tolerance_f16);
182}
giuros01efbf6c82018-09-03 09:53:53 +0100183FIXTURE_DATA_TEST_CASE(RunLarge, CLSoftmaxLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::SoftmaxLayerLargeShapes(),
184 framework::dataset::make("DataType", DataType::F16)),
185 framework::dataset::make("Beta", { 1.0f, 2.0f })),
morgolock9c7fed82020-08-05 12:30:56 +0100186 framework::dataset::make("Axis", { 0 })))
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100187{
188 // Validate output
189 validate(CLAccessor(_target), _reference, tolerance_f16);
190}
giuros01efbf6c82018-09-03 09:53:53 +0100191FIXTURE_DATA_TEST_CASE(Run4D, CLSoftmaxLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::SoftmaxLayer4DShapes(),
192 framework::dataset::make("DataType", DataType::F16)),
193 framework::dataset::make("Beta", { 1.0f, 2.0f })),
SiCong Li96209c72020-08-21 12:28:30 +0100194 framework::dataset::make("Axis", { 0, -1, 2 })))
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100195{
196 // Validate output
197 validate(CLAccessor(_target), _reference, tolerance_f16);
198}
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100199TEST_SUITE_END()
200
201TEST_SUITE(FP32)
giuros01efbf6c82018-09-03 09:53:53 +0100202FIXTURE_DATA_TEST_CASE(RunSmall, CLSoftmaxLayerFixture<float>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SoftmaxLayerSmallShapes(),
203 framework::dataset::make("DataType", DataType::F32)),
204 framework::dataset::make("Beta", { 1.0f, 2.0f })),
SiCong Li96209c72020-08-21 12:28:30 +0100205 framework::dataset::make("Axis", { 0, 1 })))
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100206{
207 // Validate output
208 validate(CLAccessor(_target), _reference, tolerance_f32);
209}
giuros01efbf6c82018-09-03 09:53:53 +0100210FIXTURE_DATA_TEST_CASE(RunLarge, CLSoftmaxLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::SoftmaxLayerLargeShapes(),
211 framework::dataset::make("DataType", DataType::F32)),
212 framework::dataset::make("Beta", { 1.0f, 2.0f })),
morgolock9c7fed82020-08-05 12:30:56 +0100213 framework::dataset::make("Axis", { 0 })))
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100214{
215 // Validate output
216 validate(CLAccessor(_target), _reference, tolerance_f32);
217}
giuros01efbf6c82018-09-03 09:53:53 +0100218FIXTURE_DATA_TEST_CASE(Run4D, CLSoftmaxLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::SoftmaxLayer4DShapes(),
219 framework::dataset::make("DataType", DataType::F32)),
220 framework::dataset::make("Beta", { 1.0f, 2.0f })),
SiCong Li96209c72020-08-21 12:28:30 +0100221 framework::dataset::make("Axis", { 0, -2, 3 })))
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100222{
223 // Validate output
224 validate(CLAccessor(_target), _reference, tolerance_f32);
225}
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100226TEST_SUITE_END()
227TEST_SUITE_END()
228
229template <typename T>
Chunosovf450caa2017-11-08 16:09:35 +0700230using CLSoftmaxLayerQuantizedFixture = SoftmaxValidationQuantizedFixture<CLTensor, CLAccessor, CLSoftmaxLayer, T>;
231
232TEST_SUITE(Quantized)
233TEST_SUITE(QASYMM8)
giuros01efbf6c82018-09-03 09:53:53 +0100234FIXTURE_DATA_TEST_CASE(RunSmall, CLSoftmaxLayerQuantizedFixture<uint8_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SoftmaxLayerSmallShapes(),
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100235 framework::dataset::make("DataType", DataType::QASYMM8)),
giuros01efbf6c82018-09-03 09:53:53 +0100236 combine(framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, -10) }),
237 framework::dataset::make("Beta", { 1.0f, 2.f }))),
SiCong Li96209c72020-08-21 12:28:30 +0100238 framework::dataset::make("Axis", { 0, 1 })))
Chunosovf450caa2017-11-08 16:09:35 +0700239{
240 // Validate output
241 validate(CLAccessor(_target), _reference, tolerance_qasymm8);
242}
giuros01efbf6c82018-09-03 09:53:53 +0100243FIXTURE_DATA_TEST_CASE(RunLarge, CLSoftmaxLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::SoftmaxLayerLargeShapes(),
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100244 framework::dataset::make("DataType", DataType::QASYMM8)),
245 combine(framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, -10) }),
giuros01efbf6c82018-09-03 09:53:53 +0100246 framework::dataset::make("Beta", { 1.0f, 2.0f }))),
morgolock9c7fed82020-08-05 12:30:56 +0100247 framework::dataset::make("Axis", { 0 })))
Chunosovf450caa2017-11-08 16:09:35 +0700248{
249 // Validate output
250 validate(CLAccessor(_target), _reference, tolerance_qasymm8);
251}
giuros01efbf6c82018-09-03 09:53:53 +0100252FIXTURE_DATA_TEST_CASE(Run4D, CLSoftmaxLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::SoftmaxLayer4DShapes(),
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100253 framework::dataset::make("DataType", DataType::QASYMM8)),
giuros01efbf6c82018-09-03 09:53:53 +0100254 combine(framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, -10) }),
255 framework::dataset::make("Beta", { 1.0f, 2.0f }))),
SiCong Li96209c72020-08-21 12:28:30 +0100256 framework::dataset::make("Axis", { 0, -4, 1 })))
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100257{
258 // Validate output
259 validate(CLAccessor(_target), _reference, tolerance_qasymm8);
260}
261
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000262TEST_SUITE_END() // QASYMM8
Chunosovf450caa2017-11-08 16:09:35 +0700263
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000264TEST_SUITE(QASYMM8_SIGNED)
265
266FIXTURE_DATA_TEST_CASE(RunSmall, CLSoftmaxLayerQuantizedFixture<int8_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SoftmaxLayerSmallShapes(),
267 framework::dataset::make("DataType", DataType::QASYMM8_SIGNED)),
268 combine(framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, -10) }),
269 framework::dataset::make("Beta", { 1.0f, 2.f }))),
SiCong Li96209c72020-08-21 12:28:30 +0100270 framework::dataset::make("Axis", { 0, 1 })))
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000271{
272 // Validate output
Sang-Hoon Park77d3d242020-08-10 22:50:17 +0100273 validate(CLAccessor(_target), _reference, tolerance_qasymm8_signed);
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000274}
275
276TEST_SUITE_END() // QASYMM8_SIGNED
277TEST_SUITE_END() // Quantized
278TEST_SUITE_END() // SoftmaxLayer
279TEST_SUITE_END() // CL
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100280} // namespace validation
281} // namespace test
282} // namespace arm_compute