xref: /aosp_15_r20/external/ComputeLibrary/tests/validation/dynamic_fusion/gpu/cl/Softmax.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2023 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  */
24 #include "arm_compute/core/Types.h"
25 #include "arm_compute/dynamic_fusion/sketch/gpu/operators/GpuSoftmax.h"
26 
27 #include "tests/CL/CLAccessor.h"
28 #include "tests/datasets/ShapeDatasets.h"
29 #include "tests/framework/Asserts.h"
30 #include "tests/framework/Fixture.h"
31 #include "tests/framework/Macros.h"
32 #include "tests/framework/datasets/Datasets.h"
33 #include "tests/validation/Validation.h"
34 #include "tests/validation/fixtures/dynamic_fusion/operators/SoftmaxFixture.h"
35 
36 using namespace arm_compute::experimental::dynamic_fusion;
37 
38 namespace arm_compute
39 {
40 namespace test
41 {
42 namespace validation
43 {
44 /** Tolerance for float operations */
45 RelativeTolerance<half>  tolerance_f16(half(0.2));
46 RelativeTolerance<float> tolerance_f32(0.001f);
47 
48 TEST_SUITE(CL)
TEST_SUITE(DYNAMIC_FUSION)49 TEST_SUITE(DYNAMIC_FUSION)
50 TEST_SUITE(SOFTMAX)
51 
52 // *INDENT-OFF*
53 // clang-format off
54 DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip(
55                framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U), 1, DataType::F32), // Mismatching data types
56                                                        TensorInfo(TensorShape(27U, 13U), 1, DataType::F32), // Mismatching shapes
57                                                        TensorInfo(TensorShape(32U, 13U), 1, DataType::F32),
58                                                        TensorInfo(TensorShape(32U, 13U), 1, DataType::F32),
59                                                        TensorInfo(TensorShape(32U, 13U), 1, DataType::S32), // Unsupported data type
60                                                        TensorInfo(TensorShape(32U, 13U), 1, DataType::F16),
61                                                        TensorInfo(TensorShape(32U, 13U), 1, DataType::F32),
62                                                        TensorInfo(TensorShape(32U, 13U), 1, DataType::F32),
63                                                        TensorInfo(TensorShape(32U, 13U), 1, DataType::F32),
64                                                        TensorInfo(TensorShape(32U, 13U), 1, DataType::F32),
65 
66                                                       }),
67                framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U), 1, DataType::F16),
68                                                        TensorInfo(TensorShape(27U, 11U), 1, DataType::F32),
69                                                        TensorInfo(TensorShape(32U, 13U), 1, DataType::F32),
70                                                        TensorInfo(TensorShape(32U, 13U), 1, DataType::F32),
71                                                        TensorInfo(TensorShape(32U, 13U), 1, DataType::F32),
72                                                        TensorInfo(TensorShape(32U, 13U), 1, DataType::QASYMM16), // Unsupported data type
73                                                        TensorInfo(TensorShape(32U, 13U), 1, DataType::F32),
74                                                        TensorInfo(TensorShape(32U, 13U), 1, DataType::F32),
75                                                        TensorInfo(TensorShape(32U, 13U), 1, DataType::F32),
76                                                        TensorInfo(TensorShape(32U, 13U), 1, DataType::F32),
77 
78                                                      })),
79                framework::dataset::make("beta", { 1.0,
80                                                   2.0,
81                                                   2.0,
82                                                   1.0,
83                                                   1.0,
84                                                   1.0,
85                                                   1.0,
86                                                   1.0,
87                                                   1.0,
88                                                   1.0,
89                                                 })),
90                framework::dataset::make("axis", {
91                                                   0,
92                                                   0,
93                                                   1,  // Invalid as axis != 0
94                                                   0,
95                                                   0,
96                                                   0,
97                                                   -3, // Invalid as axis != 0
98                                                   2,  // Invalid as axis != 0
99                                                   1,  // Invalid as axis != 0
100                                                   -1, // Invalid as axis != 0
101                                                 })),
102                framework::dataset::make("Expected", { false, false, false, true, false, false, false, false, false, false})),
103                input_info, output_info, beta, axis, expected)
104 {
105     // Create a new workload sketch
106     CLCompileContext   cl_compile_ctx = CLKernelLibrary::get().get_compile_context();
107     GpuWorkloadContext gpu_ctx        = GpuWorkloadContext{ &cl_compile_ctx };
108     GpuWorkloadSketch  sketch{ &gpu_ctx };
109 
110     SoftmaxAttributes softmax_attr{};
111     softmax_attr.axis(axis).beta(beta).is_log_softmax(false);
112     TensorInfo src_info  = sketch.create_tensor_info(input_info);
113     TensorInfo dst_info = sketch.create_tensor_info(output_info);
114     const bool res = static_cast<bool>(GpuSoftmax::validate_op(sketch, &src_info, &dst_info, softmax_attr));
115     ARM_COMPUTE_EXPECT(res == expected, framework::LogLevel::ERRORS);
116 }
117 
118 template <typename T>
119 using DynamicFusionSoftmaxLayerFixture = DynamicFusionSoftmaxValidationFixture<CLTensor, CLAccessor, GpuSoftmax, T>;
120 
121 TEST_SUITE(FLOAT)
TEST_SUITE(FP32)122 TEST_SUITE(FP32)
123 
124 FIXTURE_DATA_TEST_CASE(RunSmall, DynamicFusionSoftmaxLayerFixture<float>, framework::DatasetMode::ALL, combine(combine(combine(combine(datasets::SoftmaxLayerSmallShapes(),
125                                                                                                                    framework::dataset::make("DataType", DataType::F32)),
126                                                                                                            framework::dataset::make("Beta", { 1.0f, 2.0f })),
127                                                                                                    framework::dataset::make("Axis", { 0 })),
128                                                                                                    framework::dataset::make("is_log", {false, true})))
129 {
130     // Validate output
131     validate(CLAccessor(_target), _reference, tolerance_f32);
132 }
133 
134 
135 FIXTURE_DATA_TEST_CASE(RunLarge, DynamicFusionSoftmaxLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(combine(combine(datasets::SoftmaxLayerLargeShapes(),
136                                                                                                                    framework::dataset::make("DataType", DataType::F32)),
137                                                                                                            framework::dataset::make("Beta", { 1.0f, 2.0f })),
138                                                                                                    framework::dataset::make("Axis", { 0 })),
139                                                                                                    framework::dataset::make("is_log", {false, true})))
140 {
141     // Validate output
142     validate(CLAccessor(_target), _reference, tolerance_f32);
143 }
144 
145 
146 FIXTURE_DATA_TEST_CASE(Run4D, DynamicFusionSoftmaxLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(combine(combine(datasets::SoftmaxLayer4DShapes(),
147                                                                                                                    framework::dataset::make("DataType", DataType::F32)),
148                                                                                                            framework::dataset::make("Beta", { 1.0f, 2.0f })),
149                                                                                                    framework::dataset::make("Axis", { 0 })),
150                                                                                                    framework::dataset::make("is_log", {false, true})))
151 {
152     // Validate output
153     validate(CLAccessor(_target), _reference, tolerance_f32);
154 }
155 TEST_SUITE_END() // FP32
TEST_SUITE(FP16)156 TEST_SUITE(FP16)
157 
158 FIXTURE_DATA_TEST_CASE(RunSmall, DynamicFusionSoftmaxLayerFixture<half>, framework::DatasetMode::ALL, combine(combine(combine(combine(datasets::SoftmaxLayerSmallShapes(),
159                                                                                                                    framework::dataset::make("DataType", DataType::F16)),
160                                                                                                            framework::dataset::make("Beta", { 1.0f, 2.0f })),
161                                                                                                    framework::dataset::make("Axis", { 0 })),
162                                                                                                    framework::dataset::make("is_log", {false, true})))
163 {
164     // Validate output
165     validate(CLAccessor(_target), _reference, tolerance_f16);
166 }
167 
168 
169 FIXTURE_DATA_TEST_CASE(RunLarge, DynamicFusionSoftmaxLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(combine(combine(datasets::SoftmaxLayerLargeShapes(),
170                                                                                                                    framework::dataset::make("DataType", DataType::F16)),
171                                                                                                            framework::dataset::make("Beta", { 1.0f, 2.0f })),
172                                                                                                    framework::dataset::make("Axis", { 0 })),
173                                                                                                    framework::dataset::make("is_log", {false, true})))
174 {
175     // Validate output
176     validate(CLAccessor(_target), _reference, tolerance_f16);
177 }
178 
179 
180 FIXTURE_DATA_TEST_CASE(Run4D, DynamicFusionSoftmaxLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(combine(combine(datasets::SoftmaxLayer4DShapes(),
181                                                                                                                    framework::dataset::make("DataType", DataType::F16)),
182                                                                                                            framework::dataset::make("Beta", { 1.0f, 2.0f })),
183                                                                                                    framework::dataset::make("Axis", { 0 })),
184                                                                                                    framework::dataset::make("is_log", {false, true})))
185 {
186     // Validate output
187     validate(CLAccessor(_target), _reference, tolerance_f16);
188 }
189 TEST_SUITE_END() // FP16
190 TEST_SUITE_END() // FLOAT
191 
192 TEST_SUITE_END() // SOFTMAX
193 TEST_SUITE_END() // DYNAMIC_FUSION
194 TEST_SUITE_END() // CL
195 
196 } // namespace validation
197 } // namespace test
198 } // namespace arm_compute
199