xref: /aosp_15_r20/external/ComputeLibrary/tests/validation/dynamic_fusion/gpu/cl/DirectConv2d.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2022-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 
25 #include "tests/AssetsLibrary.h"
26 #include "tests/CL/CLAccessor.h"
27 #include "tests/framework/Fixture.h"
28 #include "tests/framework/Macros.h"
29 #include "tests/framework/datasets/Datasets.h"
30 #include "tests/validation/Validation.h"
31 #include "tests/validation/reference/ConvolutionLayer.h"
32 
33 #include "tests/datasets/SmallConvolutionLayerDataset.h"
34 #include "tests/validation/fixtures/dynamic_fusion/gpu/cl/DirectConv2dFixture.h"
35 
36 namespace arm_compute
37 {
38 namespace test
39 {
40 namespace validation
41 {
42 namespace
43 {
44 /** Tolerances from tests/validation/CL/DirectConvolutionLayer.cpp
45  */
46 RelativeTolerance<float>            tolerance_f32(0.05f);                 /**< Tolerance value for comparing reference's output against implementation's output for DataType::F32 */
47 RelativeTolerance<half_float::half> tolerance_f16(half_float::half(0.2)); /**< Tolerance value for comparing reference's output against implementation's output for DataType::F16 */
48 constexpr float                     abs_tolerance_f32(0.0001f);           /**< Absolute tolerance for FP32 tests*/
49 constexpr float                     tolerance_num = 0.07f;                /**< Tolerance number */
50 } // namespace
51 
52 TEST_SUITE(CL)
53 TEST_SUITE(DYNAMIC_FUSION)
54 /** Synced with tests/validation/CL/ConvolutionLayer.cpp
55  *
56  * Difference                       | Why the difference
57  * f32 tolerance here is smaller    | To use the same tolerance as that of DirectConv2d; lowering tolerance is safe
58  * No quantized tests               | Not supported yet
59  * No grouped CNN tests             | Not supported yet
60  * No mixed layout tests            | Not needed; only NHWC is supported
61  * No activation/post op tests      | Not needed in fusion
62  * No ValidateConvolutionMethod     | Only a single method (direct conv2d) is supported
63  * No ReshapeWeights = true tests   | Not applicable yet. This parameter only concerns gemm-based conv2d
64  * No RunSmallWithPadding tests     | Padding is removed
65  *
66  */
67 TEST_SUITE(CONV2D)
68 
69 template <typename T>
70 using DynamicFusionGpuConv2dFixture = DynamicFusionGpuConv2dValidationFixture<CLTensor, CLAccessor, GpuConv2d, T>;
71 TEST_SUITE(FP32)
72 FIXTURE_DATA_TEST_CASE(RunSmall, DynamicFusionGpuConv2dFixture<float>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallConvolutionLayerDataset(),
73                                                                                                                     framework::dataset::make("DataType", DataType::F32)),
74                                                                                                                     framework::dataset::make("DataLayout", { DataLayout::NHWC })),
75                                                                                                             framework::dataset::make("QuantizationInfo", QuantizationInfo())))
76 {
77     // Validate output
78     validate(CLAccessor(_target), _reference, tolerance_f32);
79 }
80 TEST_SUITE_END() // FP32
81 
TEST_SUITE(FP16)82 TEST_SUITE(FP16)
83 FIXTURE_DATA_TEST_CASE(RunSmall, DynamicFusionGpuConv2dFixture<half>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallConvolutionLayerDataset(),
84                                                                                                                    framework::dataset::make("DataType", DataType::F16)),
85                                                                                                                    framework::dataset::make("DataLayout", { DataLayout::NHWC })),
86                                                                                                            framework::dataset::make("QuantizationInfo", QuantizationInfo())))
87 {
88     // Validate output
89     validate(CLAccessor(_target), _reference, tolerance_f16, tolerance_num);
90 }
91 TEST_SUITE_END() // FP16
92 
93 // Tests for specific conv2d methods
94 /** Synced with tests/validation/CL/DirectConvolutionLayer.cpp
95  *
96  * Difference                       | Why the difference
97  * No quantized tests               | Not supported yet
98  * No Invalid output size test      | Not applicable. Output is removed from the interface
99  * No mixed layout/NCHW tests       | Not needed; only NHWC is supported
100  * No activation tests              | Not needed in fusion
101  */
TEST_SUITE(DIRECT_CONV2D)102 TEST_SUITE(DIRECT_CONV2D)
103 
104 // *INDENT-OFF*
105 // clang-format off
106 DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip(
107                framework::dataset::make("InputInfo", { TensorInfo(TensorShape(2U, 27U, 13U), 1, DataType::F32, DataLayout::NHWC),       // Invalid: Mismatching data type input/weights
108                                                        TensorInfo(TensorShape(2U, 27U, 13U), 1, DataType::F32, DataLayout::NHWC),       // Invalid: Mismatching input feature maps
109                                                        TensorInfo(TensorShape(2U, 27U, 13U), 1, DataType::F32, DataLayout::NHWC),       // Invalid weights dimensions
110                                                        TensorInfo(TensorShape(2U, 27U, 13U), 1, DataType::F32, DataLayout::NHWC),       // Unsupported biases size
111                                                        TensorInfo(TensorShape(2U, 27U, 13U), 1, DataType::F32, DataLayout::NHWC),       // Unsupported biases dimensions
112                                                        TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32, DataLayout::NCHW),       // Unsupported data layout: NCHW
113                                                        TensorInfo(TensorShape(2U, 32U, 16U), 1, DataType::QASYMM8, DataLayout::NHWC),   // Unsupported data type: quantized
114                                                        TensorInfo(TensorShape(2U, 32U, 16U), 1, DataType::F32, DataLayout::NHWC),
115                                                        TensorInfo(TensorShape(2U, 27U, 13U), 1, DataType::F32, DataLayout::NHWC),       // Arbitrary weight sizes for NHWC are supported
116                                                        TensorInfo(TensorShape(2U, 27U, 13U), 1, DataType::F32, DataLayout::NHWC),       // Non-rectangular weights dimensions for NHWC are supported
117                                                        TensorInfo(TensorShape(2U, 27U, 13U), 1, DataType::F32, DataLayout::NHWC),       // Strides > 2 for any kernel sizes for NHWC are supported
118                                                      }),
119                framework::dataset::make("WeightsInfo",{ TensorInfo(TensorShape(2U, 3U, 3U, 4U), 1, DataType::F16, DataLayout::NHWC),
120                                                         TensorInfo(TensorShape(3U, 3U, 3U, 4U), 1, DataType::F32, DataLayout::NHWC),
121                                                         TensorInfo(TensorShape(2U, 3U, 3U, 4U, 3U), 1, DataType::F32, DataLayout::NHWC),
122                                                         TensorInfo(TensorShape(2U, 3U, 3U, 4U), 1, DataType::F32, DataLayout::NHWC),
123                                                         TensorInfo(TensorShape(2U, 3U, 3U, 4U), 1, DataType::F32, DataLayout::NHWC),
124                                                         TensorInfo(TensorShape(3U, 3U, 2U, 4U), 1, DataType::F32, DataLayout::NCHW),
125                                                         TensorInfo(TensorShape(2U, 1U, 1U, 4U), 1, DataType::QASYMM8, DataLayout::NHWC),
126                                                         TensorInfo(TensorShape(2U, 1U, 1U, 4U), 1, DataType::F32, DataLayout::NHWC),
127                                                         TensorInfo(TensorShape(2U, 13U, 13U, 4U), 1, DataType::F32, DataLayout::NHWC),
128                                                         TensorInfo(TensorShape(2U, 5U, 3U, 4U), 1, DataType::F32, DataLayout::NHWC),
129                                                         TensorInfo(TensorShape(2U, 3U, 3U, 4U), 1, DataType::F32, DataLayout::NHWC),
130                                                      })),
131                framework::dataset::make("BiasesInfo",{ TensorInfo(TensorShape(4U), 1, DataType::F32, DataLayout::NHWC),
132                                                        TensorInfo(TensorShape(4U), 1, DataType::F32, DataLayout::NHWC),
133                                                        TensorInfo(TensorShape(4U), 1, DataType::F32, DataLayout::NHWC),
134                                                        TensorInfo(TensorShape(3U), 1, DataType::F32, DataLayout::NHWC),
135                                                        TensorInfo(TensorShape(4U, 2U), 1, DataType::F32, DataLayout::NHWC),
136                                                        TensorInfo(TensorShape(25U), 1, DataType::F32, DataLayout::NCHW),
137                                                        TensorInfo(TensorShape(4U), 1, DataType::QASYMM8, DataLayout::NHWC),
138                                                        TensorInfo(TensorShape(4U), 1, DataType::F32, DataLayout::NHWC),
139                                                        TensorInfo(TensorShape(4U), 1, DataType::F32, DataLayout::NHWC),
140                                                        TensorInfo(TensorShape(4U), 1, DataType::F32, DataLayout::NHWC),
141                                                        TensorInfo(TensorShape(4U), 1, DataType::F32, DataLayout::NHWC),
142                                                      })),
143                framework::dataset::make("Conv2dAttributes",  {
144                                                         Conv2dAttributes().stride({1, 1}).pad({0, 0, 0, 0}),
145                                                         Conv2dAttributes().stride({1, 1}).pad({0, 0, 0, 0}),
146                                                         Conv2dAttributes().stride({1, 1}).pad({0, 0, 0, 0}),
147                                                         Conv2dAttributes().stride({1, 1}).pad({0, 0, 0, 0}),
148                                                         Conv2dAttributes().stride({1, 1}).pad({0, 0, 0, 0}),
149                                                         Conv2dAttributes().stride({1, 1}).pad({0, 0, 0, 0}),
150                                                         Conv2dAttributes().stride({1, 1}).pad({0, 0, 0, 0}),
151                                                         Conv2dAttributes().stride({1, 1}).pad({0, 0, 0, 0}),
152                                                         Conv2dAttributes().stride({1, 1}).pad({0, 0, 0, 0}),
153                                                         Conv2dAttributes().stride({1, 1}).pad({0, 0, 0, 0}),
154                                                         Conv2dAttributes().stride({3, 3}).pad({0, 0, 0, 0}),
155                                                       })),
156                framework::dataset::make("Expected", { false, false, false, false, false, false, false, true, true, true, true })),
157                input_info, weights_info, biases_info, conv2d_attrs, expected)
158 {
159     auto              cl_compile_ctx = CLKernelLibrary::get().get_compile_context();
160     auto              gpu_ctx        = GpuWorkloadContext{ &cl_compile_ctx };
161     GpuWorkloadSketch sketch{ &gpu_ctx };
162 
163     const TensorInfo sketch_input_info   = sketch.create_tensor_info(input_info);
164     const TensorInfo sketch_weights_info = sketch.create_tensor_info(weights_info);
165     const TensorInfo sketch_biases_info  = sketch.create_tensor_info(biases_info);
166     bool is_valid = bool(GpuConv2d::validate_op(sketch, &sketch_input_info, &sketch_weights_info, &sketch_biases_info, conv2d_attrs));
167     ARM_COMPUTE_EXPECT(is_valid == expected, framework::LogLevel::ERRORS);
168 }
169 template <typename T>
170 using DynamicFusionGpuDirectConv2dFixture = DynamicFusionDirectConv2dValidationFixture<CLTensor, CLAccessor, GpuConv2d, T>;
171 
172 TEST_SUITE(FP16)
173 FIXTURE_DATA_TEST_CASE(RunSmall, DynamicFusionGpuDirectConv2dFixture<half>, framework::DatasetMode::PRECOMMIT,
174                combine(combine(combine(zip(zip(zip(zip(zip(
175                framework::dataset::make("InputShape", { TensorShape(27U, 13U, 23U),
176                                                         TensorShape(19U, 5U, 16U, 4U),
177                                                         TensorShape(13U, 5U, 17U, 2U),
178                                                         TensorShape(32U, 37U, 13U) } ),
179                framework::dataset::make("StrideX", { 1, 3, 1, 1 })),
180                framework::dataset::make("StrideY", { 1, 3, 2, 1 })),
181                framework::dataset::make("PadX", { 1, 3, 0, 4 })),
182                framework::dataset::make("PadY", { 1, 3, 0, 4 })),
183                framework::dataset::make("KernelSize", { 3, 8, 1, 9 })),
184                framework::dataset::make("NumKernels", { 17, 3, 1, 19 })),
185                framework::dataset::make("DataType",  DataType::F16)),
186                framework::dataset::make("DataLayout", DataLayout::NHWC)))
187 {
188     validate(CLAccessor(_target), _reference, tolerance_f16, tolerance_num);
189 }
190 
191 FIXTURE_DATA_TEST_CASE(RunLarge, DynamicFusionGpuDirectConv2dFixture<half>, framework::DatasetMode::NIGHTLY,
192                combine(combine(combine(zip(zip(zip(zip(zip(
193                framework::dataset::make("InputShape", { TensorShape(800U, 800U, 3U) } ),
194                framework::dataset::make("StrideX", { 1 })),
195                framework::dataset::make("StrideY", { 1 })),
196                framework::dataset::make("PadX", { 1 })),
197                framework::dataset::make("PadY", { 1 })),
198                framework::dataset::make("KernelSize", { 9 })),
199                framework::dataset::make("NumKernels", { 3 })),
200                framework::dataset::make("DataType",  DataType::F16)),
201                framework::dataset::make("DataLayout", DataLayout::NHWC)))
202 {
203     validate(CLAccessor(_target), _reference, tolerance_f16, tolerance_num);
204 }
205 
206 TEST_SUITE_END() // FP16
207 
TEST_SUITE(FP32)208 TEST_SUITE(FP32)
209 FIXTURE_DATA_TEST_CASE(RunSmall, DynamicFusionGpuDirectConv2dFixture<float>, framework::DatasetMode::PRECOMMIT,
210                combine(combine(combine(zip(zip(zip(zip(zip(
211                framework::dataset::make("InputShape", { TensorShape(27U, 13U, 23U),
212                                                         TensorShape(19U, 5U, 16U, 4U),
213                                                         TensorShape(13U, 5U, 17U, 2U),
214                                                         TensorShape(32U, 37U, 13U) } ),
215                framework::dataset::make("StrideX", { 1, 3, 1, 1 })),
216                framework::dataset::make("StrideY", { 1, 3, 2, 1 })),
217                framework::dataset::make("PadX", { 1, 3, 0, 4 })),
218                framework::dataset::make("PadY", { 1, 3, 0, 4 })),
219                framework::dataset::make("KernelSize", { 3, 8, 1, 9 })),
220                framework::dataset::make("NumKernels", { 17, 3, 1, 19 })),
221                framework::dataset::make("DataType",  DataType::F32)),
222                framework::dataset::make("DataLayout", DataLayout::NHWC)))
223 {
224     validate(CLAccessor(_target), _reference, tolerance_f32, 0.0, abs_tolerance_f32);
225 }
226 
227 FIXTURE_DATA_TEST_CASE(RunLarge, DynamicFusionGpuDirectConv2dFixture<float>, framework::DatasetMode::NIGHTLY,
228                combine(combine(combine(zip(zip(zip(zip(zip(
229                framework::dataset::make("InputShape", { TensorShape(800U, 800U, 3U) } ),
230                framework::dataset::make("StrideX", { 1 })),
231                framework::dataset::make("StrideY", { 1 })),
232                framework::dataset::make("PadX", { 1 })),
233                framework::dataset::make("PadY", { 1 })),
234                framework::dataset::make("KernelSize", { 9 })),
235                framework::dataset::make("NumKernels", { 3 })),
236                framework::dataset::make("DataType",  DataType::F32)),
237                framework::dataset::make("DataLayout", DataLayout::NHWC)))
238 {
239     validate(CLAccessor(_target), _reference, tolerance_f32, 0.0, abs_tolerance_f32);
240 }
241 // clang-format on
242 // *INDENT-ON*
243 
244 TEST_SUITE_END() // FP32
245 TEST_SUITE_END() // DIRECT_CONV2D
246 TEST_SUITE_END() // CONV2D
247 TEST_SUITE_END() // DYNAMIC_FUSION
248 TEST_SUITE_END() // CL
249 } // namespace validation
250 } // namespace test
251 } // namespace arm_compute
252