xref: /aosp_15_r20/external/ComputeLibrary/tests/validation/CL/SoftmaxLayer.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1*c217d954SCole Faust /*
2*c217d954SCole Faust  * Copyright (c) 2017-2021 Arm Limited.
3*c217d954SCole Faust  *
4*c217d954SCole Faust  * SPDX-License-Identifier: MIT
5*c217d954SCole Faust  *
6*c217d954SCole Faust  * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust  * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust  * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust  * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust  * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust  *
13*c217d954SCole Faust  * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust  * copies or substantial portions of the Software.
15*c217d954SCole Faust  *
16*c217d954SCole Faust  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust  * SOFTWARE.
23*c217d954SCole Faust  */
24*c217d954SCole Faust #include "arm_compute/core/Types.h"
25*c217d954SCole Faust #include "arm_compute/runtime/CL/CLTensor.h"
26*c217d954SCole Faust #include "arm_compute/runtime/CL/CLTensorAllocator.h"
27*c217d954SCole Faust #include "arm_compute/runtime/CL/functions/CLSoftmaxLayer.h"
28*c217d954SCole Faust #include "tests/CL/CLAccessor.h"
29*c217d954SCole Faust #include "tests/PaddingCalculator.h"
30*c217d954SCole Faust #include "tests/datasets/ShapeDatasets.h"
31*c217d954SCole Faust #include "tests/framework/Asserts.h"
32*c217d954SCole Faust #include "tests/framework/Macros.h"
33*c217d954SCole Faust #include "tests/framework/datasets/Datasets.h"
34*c217d954SCole Faust #include "tests/validation/Validation.h"
35*c217d954SCole Faust #include "tests/validation/fixtures/SoftmaxLayerFixture.h"
36*c217d954SCole Faust 
37*c217d954SCole Faust #include "arm_compute/runtime/MemoryManagerOnDemand.h"
38*c217d954SCole Faust #include "arm_compute/runtime/PoolManager.h"
39*c217d954SCole Faust #include "arm_compute/runtime/BlobLifetimeManager.h"
40*c217d954SCole Faust #include "arm_compute/runtime/CL/CLBufferAllocator.h"
41*c217d954SCole Faust #include "arm_compute/runtime/BlobMemoryPool.h"
42*c217d954SCole Faust 
43*c217d954SCole Faust namespace arm_compute
44*c217d954SCole Faust {
45*c217d954SCole Faust namespace test
46*c217d954SCole Faust {
47*c217d954SCole Faust namespace validation
48*c217d954SCole Faust {
49*c217d954SCole Faust namespace
50*c217d954SCole Faust {
51*c217d954SCole Faust /** Tolerance for float operations */
52*c217d954SCole Faust RelativeTolerance<half>  tolerance_f16(half(0.2));
53*c217d954SCole Faust RelativeTolerance<float> tolerance_f32(0.001f);
54*c217d954SCole Faust 
55*c217d954SCole Faust /** Tolerance for quantized operations */
56*c217d954SCole Faust constexpr AbsoluteTolerance<uint8_t> tolerance_qasymm8(1);
57*c217d954SCole Faust constexpr AbsoluteTolerance<int8_t>  tolerance_qasymm8_signed(1);
58*c217d954SCole Faust 
59*c217d954SCole Faust /** CNN data types */
60*c217d954SCole Faust const auto CNNDataTypes = framework::dataset::make("DataType",
61*c217d954SCole Faust {
62*c217d954SCole Faust     DataType::QASYMM8,
63*c217d954SCole Faust     DataType::F16,
64*c217d954SCole Faust     DataType::F32,
65*c217d954SCole Faust });
66*c217d954SCole Faust } // namespace
67*c217d954SCole Faust 
68*c217d954SCole Faust TEST_SUITE(CL)
TEST_SUITE(SoftmaxLayer)69*c217d954SCole Faust TEST_SUITE(SoftmaxLayer)
70*c217d954SCole Faust 
71*c217d954SCole Faust TEST_CASE(SimpleMemoryManaged, framework::DatasetMode::ALL)
72*c217d954SCole Faust {
73*c217d954SCole Faust     // The purpose of this test is to test if the function can
74*c217d954SCole Faust     // run correctly even with the given memory manager from its caller
75*c217d954SCole Faust     // (Similar scenario when the library is integrated into other software)
76*c217d954SCole Faust     // especially when working with workspace() method of
77*c217d954SCole Faust     // @ref arm_compute::opencl::ClSoftmax.
78*c217d954SCole Faust     const auto shape = TensorShape{4,2};    // Random shape, not important
79*c217d954SCole Faust     constexpr auto dt = DataType::F32;      // Random data type, not important
80*c217d954SCole Faust 
81*c217d954SCole Faust     // Create a memory manager
82*c217d954SCole Faust     auto lm = std::make_shared<BlobLifetimeManager>();
83*c217d954SCole Faust     auto pm = std::make_shared<arm_compute::PoolManager>();
84*c217d954SCole Faust     auto alloc = std::make_unique<CLBufferAllocator>();
85*c217d954SCole Faust     auto mm = std::make_shared<MemoryManagerOnDemand>(lm, pm);
86*c217d954SCole Faust 
87*c217d954SCole Faust     auto src = create_tensor<CLTensor>(shape, dt);
88*c217d954SCole Faust     auto dst = create_tensor<CLTensor>(shape, dt);
89*c217d954SCole Faust     src.allocator()->allocate();
90*c217d954SCole Faust     dst.allocator()->allocate();
91*c217d954SCole Faust 
92*c217d954SCole Faust     // Create the function with the memory manager
93*c217d954SCole Faust     CLSoftmaxLayer smx(mm);
94*c217d954SCole Faust     smx.configure(&src, &dst);
95*c217d954SCole Faust 
96*c217d954SCole Faust     // Populate the memory, acquire() will happen in run()
97*c217d954SCole Faust     mm->populate(*alloc.get(), 1);
98*c217d954SCole Faust 
99*c217d954SCole Faust     std::vector<float> input_vals{0.0f, 1.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f,};
100*c217d954SCole Faust     library->fill_static_values(CLAccessor(src), input_vals);
101*c217d954SCole Faust 
102*c217d954SCole Faust     smx.run();
103*c217d954SCole Faust 
104*c217d954SCole Faust     // Compute reference to compare
105*c217d954SCole Faust     SimpleTensor<float> ref_src{shape, dt};
106*c217d954SCole Faust     library->fill_static_values(ref_src, input_vals);
107*c217d954SCole Faust     auto ref_dst = reference::softmax_layer<float>(ref_src, 1., 0, false);
108*c217d954SCole Faust 
109*c217d954SCole Faust     validate(CLAccessor(dst), ref_dst);
110*c217d954SCole Faust }
111*c217d954SCole Faust 
112*c217d954SCole Faust // *INDENT-OFF*
113*c217d954SCole Faust // clang-format off
114*c217d954SCole Faust DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip(
115*c217d954SCole Faust                framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U), 1, DataType::F32),    // Mismatching data types
116*c217d954SCole Faust                                                        TensorInfo(TensorShape(27U, 13U), 1, DataType::F32),    // Mismatching shapes
117*c217d954SCole Faust                                                        TensorInfo(TensorShape(27U, 13U), 1, DataType::QASYMM8, // Invalid output quantization info
118*c217d954SCole Faust                                                                   QuantizationInfo(1.f/256, 12)),
119*c217d954SCole Faust                                                        TensorInfo(TensorShape(32U, 13U), 1, DataType::F32),
120*c217d954SCole Faust                                                        TensorInfo(TensorShape(32U, 13U), 1, DataType::QASYMM8,
121*c217d954SCole Faust                                                                   QuantizationInfo(1.f/256, 12)),
122*c217d954SCole Faust                                                        TensorInfo(TensorShape(32U, 13U), 1, DataType::QASYMM8_SIGNED,
123*c217d954SCole Faust                                                                   QuantizationInfo(1.f/256, 12)),
124*c217d954SCole Faust                                                        TensorInfo(TensorShape(32U, 13U), 1, DataType::QASYMM8_SIGNED, // Invalid axis high
125*c217d954SCole Faust                                                                   QuantizationInfo(1.f/256, 12)),
126*c217d954SCole Faust                                                        TensorInfo(TensorShape(32U, 13U), 1, DataType::QASYMM8_SIGNED, // Invalid axis low
127*c217d954SCole Faust                                                                   QuantizationInfo(1.f/256, 12))
128*c217d954SCole Faust                                                       }),
129*c217d954SCole Faust                framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U), 1, DataType::F16),
130*c217d954SCole Faust                                                        TensorInfo(TensorShape(27U, 11U), 1, DataType::F32),
131*c217d954SCole Faust                                                        TensorInfo(TensorShape(27U, 13U), 1, DataType::QASYMM8,
132*c217d954SCole Faust                                                                   QuantizationInfo(1.f/256, 12)),
133*c217d954SCole Faust                                                        TensorInfo(TensorShape(32U, 13U), 1, DataType::F32),
134*c217d954SCole Faust                                                        TensorInfo(TensorShape(32U, 13U), 1, DataType::QASYMM8,
135*c217d954SCole Faust                                                                   QuantizationInfo(1.f/256, 0)),
136*c217d954SCole Faust                                                        TensorInfo(TensorShape(32U, 13U), 1, DataType::QASYMM8_SIGNED,
137*c217d954SCole Faust                                                                   QuantizationInfo(1.f/256, -128)),
138*c217d954SCole Faust                                                        TensorInfo(TensorShape(32U, 13U), 1, DataType::QASYMM8_SIGNED,
139*c217d954SCole Faust                                                                   QuantizationInfo(1.f/256, -128)),
140*c217d954SCole Faust                                                        TensorInfo(TensorShape(32U, 13U), 1, DataType::QASYMM8_SIGNED,
141*c217d954SCole Faust                                                                   QuantizationInfo(1.f/256, -128)),
142*c217d954SCole Faust                                                      })),
143*c217d954SCole Faust                framework::dataset::make("beta", { 1.0,
144*c217d954SCole Faust                                                   2.0,
145*c217d954SCole Faust                                                   1.0,
146*c217d954SCole Faust                                                   2.0,
147*c217d954SCole Faust                                                   1.0,
148*c217d954SCole Faust                                                   2.0,
149*c217d954SCole Faust                                                   1.0,
150*c217d954SCole Faust                                                   2.0,
151*c217d954SCole Faust                                                 })),
152*c217d954SCole Faust                framework::dataset::make("axis", {
153*c217d954SCole Faust                                                   0,
154*c217d954SCole Faust                                                   0,
155*c217d954SCole Faust                                                   0,
156*c217d954SCole Faust                                                   1,
157*c217d954SCole Faust                                                   0,
158*c217d954SCole Faust                                                   -1,
159*c217d954SCole Faust                                                   2,
160*c217d954SCole Faust                                                   -3,
161*c217d954SCole Faust                                                 })),
162*c217d954SCole Faust                framework::dataset::make("Expected", { false, false, false, true, true, true, false, false })),
163*c217d954SCole Faust                input_info, output_info, beta, axis, expected)
164*c217d954SCole Faust {
165*c217d954SCole Faust     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);
166*c217d954SCole Faust }
167*c217d954SCole Faust // clang-format on
168*c217d954SCole Faust // *INDENT-ON*
169*c217d954SCole Faust 
170*c217d954SCole Faust template <typename T>
171*c217d954SCole Faust using CLSoftmaxLayerFixture = SoftmaxValidationFixture<CLTensor, CLAccessor, CLSoftmaxLayer, T>;
172*c217d954SCole Faust 
173*c217d954SCole Faust TEST_SUITE(Float)
TEST_SUITE(FP16)174*c217d954SCole Faust TEST_SUITE(FP16)
175*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(RunSmall, CLSoftmaxLayerFixture<half>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SoftmaxLayerSmallShapes(),
176*c217d954SCole Faust                                                                                                                    framework::dataset::make("DataType", DataType::F16)),
177*c217d954SCole Faust                                                                                                            framework::dataset::make("Beta", { 1.0f, 2.0f })),
178*c217d954SCole Faust                                                                                                    framework::dataset::make("Axis", { 0, -1 })))
179*c217d954SCole Faust {
180*c217d954SCole Faust     // Validate output
181*c217d954SCole Faust     validate(CLAccessor(_target), _reference, tolerance_f16);
182*c217d954SCole Faust }
183*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(RunLarge, CLSoftmaxLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::SoftmaxLayerLargeShapes(),
184*c217d954SCole Faust                                                                                                                        framework::dataset::make("DataType", DataType::F16)),
185*c217d954SCole Faust                                                                                                                framework::dataset::make("Beta", { 1.0f, 2.0f })),
186*c217d954SCole Faust                                                                                                        framework::dataset::make("Axis", { 0 })))
187*c217d954SCole Faust {
188*c217d954SCole Faust     // Validate output
189*c217d954SCole Faust     validate(CLAccessor(_target), _reference, tolerance_f16);
190*c217d954SCole Faust }
191*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(Run4D, CLSoftmaxLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::SoftmaxLayer4DShapes(),
192*c217d954SCole Faust                                                                                                                     framework::dataset::make("DataType", DataType::F16)),
193*c217d954SCole Faust                                                                                                             framework::dataset::make("Beta", { 1.0f, 2.0f })),
194*c217d954SCole Faust                                                                                                     framework::dataset::make("Axis", { 0, -1, 2 })))
195*c217d954SCole Faust {
196*c217d954SCole Faust     // Validate output
197*c217d954SCole Faust     validate(CLAccessor(_target), _reference, tolerance_f16);
198*c217d954SCole Faust }
199*c217d954SCole Faust TEST_SUITE_END()
200*c217d954SCole Faust 
TEST_SUITE(FP32)201*c217d954SCole Faust TEST_SUITE(FP32)
202*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(RunSmall, CLSoftmaxLayerFixture<float>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SoftmaxLayerSmallShapes(),
203*c217d954SCole Faust                                                                                                                     framework::dataset::make("DataType", DataType::F32)),
204*c217d954SCole Faust                                                                                                             framework::dataset::make("Beta", { 1.0f, 2.0f })),
205*c217d954SCole Faust                                                                                                     framework::dataset::make("Axis", { 0, 1 })))
206*c217d954SCole Faust {
207*c217d954SCole Faust     // Validate output
208*c217d954SCole Faust     validate(CLAccessor(_target), _reference, tolerance_f32);
209*c217d954SCole Faust }
210*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(RunLarge, CLSoftmaxLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::SoftmaxLayerLargeShapes(),
211*c217d954SCole Faust                                                                                                                         framework::dataset::make("DataType", DataType::F32)),
212*c217d954SCole Faust                                                                                                                 framework::dataset::make("Beta", { 1.0f, 2.0f })),
213*c217d954SCole Faust                                                                                                         framework::dataset::make("Axis", { 0 })))
214*c217d954SCole Faust {
215*c217d954SCole Faust     // Validate output
216*c217d954SCole Faust     validate(CLAccessor(_target), _reference, tolerance_f32);
217*c217d954SCole Faust }
218*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(Run4D, CLSoftmaxLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::SoftmaxLayer4DShapes(),
219*c217d954SCole Faust                                                                                                                      framework::dataset::make("DataType", DataType::F32)),
220*c217d954SCole Faust                                                                                                              framework::dataset::make("Beta", { 1.0f, 2.0f })),
221*c217d954SCole Faust                                                                                                      framework::dataset::make("Axis", { 0, -2, 3 })))
222*c217d954SCole Faust {
223*c217d954SCole Faust     // Validate output
224*c217d954SCole Faust     validate(CLAccessor(_target), _reference, tolerance_f32);
225*c217d954SCole Faust }
226*c217d954SCole Faust TEST_SUITE_END()
227*c217d954SCole Faust TEST_SUITE_END()
228*c217d954SCole Faust 
229*c217d954SCole Faust template <typename T>
230*c217d954SCole Faust using CLSoftmaxLayerQuantizedFixture = SoftmaxValidationQuantizedFixture<CLTensor, CLAccessor, CLSoftmaxLayer, T>;
231*c217d954SCole Faust 
232*c217d954SCole Faust TEST_SUITE(Quantized)
TEST_SUITE(QASYMM8)233*c217d954SCole Faust TEST_SUITE(QASYMM8)
234*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(RunSmall, CLSoftmaxLayerQuantizedFixture<uint8_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SoftmaxLayerSmallShapes(),
235*c217d954SCole Faust                                                                                                                        framework::dataset::make("DataType", DataType::QASYMM8)),
236*c217d954SCole Faust                                                                                                                        combine(framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, -10) }),
237*c217d954SCole Faust                                                                                                                                framework::dataset::make("Beta", { 1.0f, 2.f }))),
238*c217d954SCole Faust                                                                                                                framework::dataset::make("Axis", { 0, 1 })))
239*c217d954SCole Faust {
240*c217d954SCole Faust     // Validate output
241*c217d954SCole Faust     validate(CLAccessor(_target), _reference, tolerance_qasymm8);
242*c217d954SCole Faust }
243*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(RunLarge, CLSoftmaxLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::SoftmaxLayerLargeShapes(),
244*c217d954SCole Faust                                                                                                                    framework::dataset::make("DataType", DataType::QASYMM8)),
245*c217d954SCole Faust                                                                                                                    combine(framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, -10) }),
246*c217d954SCole Faust                                                                                                                            framework::dataset::make("Beta", { 1.0f, 2.0f }))),
247*c217d954SCole Faust                                                                                                                    framework::dataset::make("Axis", { 0 })))
248*c217d954SCole Faust {
249*c217d954SCole Faust     // Validate output
250*c217d954SCole Faust     validate(CLAccessor(_target), _reference, tolerance_qasymm8);
251*c217d954SCole Faust }
252*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(Run4D, CLSoftmaxLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::SoftmaxLayer4DShapes(),
253*c217d954SCole Faust                                                                                                                         framework::dataset::make("DataType", DataType::QASYMM8)),
254*c217d954SCole Faust                                                                                                                         combine(framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, -10) }),
255*c217d954SCole Faust                                                                                                                                 framework::dataset::make("Beta", { 1.0f, 2.0f }))),
256*c217d954SCole Faust                                                                                                                 framework::dataset::make("Axis", { 0, -4, 1 })))
257*c217d954SCole Faust {
258*c217d954SCole Faust     // Validate output
259*c217d954SCole Faust     validate(CLAccessor(_target), _reference, tolerance_qasymm8);
260*c217d954SCole Faust }
261*c217d954SCole Faust 
262*c217d954SCole Faust TEST_SUITE_END() // QASYMM8
263*c217d954SCole Faust 
TEST_SUITE(QASYMM8_SIGNED)264*c217d954SCole Faust TEST_SUITE(QASYMM8_SIGNED)
265*c217d954SCole Faust 
266*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(RunSmall, CLSoftmaxLayerQuantizedFixture<int8_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SoftmaxLayerSmallShapes(),
267*c217d954SCole Faust                                                                                                                       framework::dataset::make("DataType", DataType::QASYMM8_SIGNED)),
268*c217d954SCole Faust                                                                                                                       combine(framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, -10) }),
269*c217d954SCole Faust                                                                                                                               framework::dataset::make("Beta", { 1.0f, 2.f }))),
270*c217d954SCole Faust                                                                                                               framework::dataset::make("Axis", { 0, 1 })))
271*c217d954SCole Faust {
272*c217d954SCole Faust     // Validate output
273*c217d954SCole Faust     validate(CLAccessor(_target), _reference, tolerance_qasymm8_signed);
274*c217d954SCole Faust }
275*c217d954SCole Faust 
276*c217d954SCole Faust TEST_SUITE_END() // QASYMM8_SIGNED
277*c217d954SCole Faust TEST_SUITE_END() // Quantized
278*c217d954SCole Faust TEST_SUITE_END() // SoftmaxLayer
279*c217d954SCole Faust TEST_SUITE_END() // CL
280*c217d954SCole Faust } // namespace validation
281*c217d954SCole Faust } // namespace test
282*c217d954SCole Faust } // namespace arm_compute
283