xref: /aosp_15_r20/external/ComputeLibrary/tests/validation/CL/GEMMReshapeRHSMatrix.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1*c217d954SCole Faust /*
2*c217d954SCole Faust  * Copyright (c) 2018-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/core/utils/misc/ShapeCalculator.h"
26*c217d954SCole Faust #include "arm_compute/runtime/CL/CLTensor.h"
27*c217d954SCole Faust #include "arm_compute/runtime/CL/CLTensorAllocator.h"
28*c217d954SCole Faust #include "src/gpu/cl/kernels/ClGemmReshapeRhsMatrixKernel.h"
29*c217d954SCole Faust #include "tests/CL/CLAccessor.h"
30*c217d954SCole Faust #include "tests/CL/Helper.h"
31*c217d954SCole Faust #include "tests/PaddingCalculator.h"
32*c217d954SCole Faust #include "tests/datasets/ShapeDatasets.h"
33*c217d954SCole Faust #include "tests/framework/Asserts.h"
34*c217d954SCole Faust #include "tests/framework/Macros.h"
35*c217d954SCole Faust #include "tests/framework/datasets/Datasets.h"
36*c217d954SCole Faust #include "tests/validation/Validation.h"
37*c217d954SCole Faust #include "tests/validation/fixtures/GEMMReshapeRHSMatrixFixture.h"
38*c217d954SCole Faust 
39*c217d954SCole Faust namespace arm_compute
40*c217d954SCole Faust {
41*c217d954SCole Faust namespace test
42*c217d954SCole Faust {
43*c217d954SCole Faust namespace validation
44*c217d954SCole Faust {
45*c217d954SCole Faust namespace
46*c217d954SCole Faust {
47*c217d954SCole Faust // *INDENT-OFF*
48*c217d954SCole Faust // clang-format off
49*c217d954SCole Faust /** Batch size values to test */
50*c217d954SCole Faust const auto b_values = framework::dataset::make("batchsize", 1, 3);
51*c217d954SCole Faust 
52*c217d954SCole Faust /** N0 values to test */
53*c217d954SCole Faust const auto n0_values_nt_s32 = framework::dataset::make("N0", { 1, 2, 3 });
54*c217d954SCole Faust const auto n0_values_nt_s16 = framework::dataset::make("N0", { 4, 8 });
55*c217d954SCole Faust const auto n0_values_nt_s8 = framework::dataset::make("N0", { 16 });
56*c217d954SCole Faust const auto n0_values_t_s32 = framework::dataset::make("N0", { 4, 8 });
57*c217d954SCole Faust const auto n0_values_t_s16 = framework::dataset::make("N0", { 16 });
58*c217d954SCole Faust const auto n0_values_t_s8 = framework::dataset::make("N0", { 2, 3 });
59*c217d954SCole Faust 
60*c217d954SCole Faust /** K0 values to test */
61*c217d954SCole Faust const auto k0_values_nt_s32 = framework::dataset::make("K0", { 1, 2 });
62*c217d954SCole Faust const auto k0_values_nt_s16 = framework::dataset::make("K0", { 16 });
63*c217d954SCole Faust const auto k0_values_nt_s8 = framework::dataset::make("K0", { 3,4 });
64*c217d954SCole Faust const auto k0_values_t_s32 = framework::dataset::make("K0", { 2, 3 });
65*c217d954SCole Faust const auto k0_values_t_s16 = framework::dataset::make("K0", { 4, 8 });
66*c217d954SCole Faust const auto k0_values_t_s8 = framework::dataset::make("K0", { 16 });
67*c217d954SCole Faust 
68*c217d954SCole Faust /** H0 values to test */
69*c217d954SCole Faust const auto h0_values = framework::dataset::make("H0", 1, 4);
70*c217d954SCole Faust 
71*c217d954SCole Faust /** Interleave values to test */
72*c217d954SCole Faust const auto i_values = framework::dataset::make("interleave", { true, false });
73*c217d954SCole Faust } // namespace
74*c217d954SCole Faust 
75*c217d954SCole Faust using namespace arm_compute::misc::shape_calculator;
76*c217d954SCole Faust using namespace arm_compute::opencl::kernels;
77*c217d954SCole Faust 
78*c217d954SCole Faust // Initialize the output tensor with zero and fill the border with zero
79*c217d954SCole Faust using CLGEMMReshapeRHSMatrix = CLSynthetizeOperatorInitOutputWithZeroAndWithZeroConstantBorder<ClGemmReshapeRhsMatrixKernel, 16>;
80*c217d954SCole Faust 
81*c217d954SCole Faust template <typename T>
82*c217d954SCole Faust using CLGEMMReshapeRHSMatrixFixture = GEMMReshapeRHSMatrixValidationFixture<CLTensor, CLAccessor, CLGEMMReshapeRHSMatrix, T>;
83*c217d954SCole Faust 
84*c217d954SCole Faust TEST_SUITE(CL)
TEST_SUITE(GEMMReshapeRHSMatrix)85*c217d954SCole Faust TEST_SUITE(GEMMReshapeRHSMatrix)
86*c217d954SCole Faust 
87*c217d954SCole Faust // *INDENT-OFF*
88*c217d954SCole Faust // clang-format off
89*c217d954SCole Faust DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip(zip(
90*c217d954SCole Faust                framework::dataset::make("InputInfo", { TensorInfo(TensorShape(32U, 16U, 2U), 1, DataType::F32),
91*c217d954SCole Faust                                                        TensorInfo(TensorShape(32U, 16U, 2U), 1, DataType::F32),    // Mismatching data types
92*c217d954SCole Faust                                                        TensorInfo(TensorShape(32U, 16U, 2U), 1, DataType::F32),    // Wrong n0 value
93*c217d954SCole Faust                                                        TensorInfo(TensorShape(32U, 16U, 2U), 1, DataType::F32),    // Wrong k0 value
94*c217d954SCole Faust                                                        TensorInfo(TensorShape(32U, 16U, 2U), 1, DataType::F32),    // Wrong h0 value
95*c217d954SCole Faust                                                        TensorInfo(TensorShape(32U, 16U, 2U), 1, DataType::F32),    // n0 > 16
96*c217d954SCole Faust                                                        TensorInfo(TensorShape(32U, 16U, 2U), 1, DataType::F32),    // k0 > 16
97*c217d954SCole Faust                                                        TensorInfo(TensorShape(32U, 16U, 2U), 1, DataType::F32),    // k0 == 1 && transpose
98*c217d954SCole Faust                                                      }),
99*c217d954SCole Faust                framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(64U, 2U, 2U), 1, DataType::F32),
100*c217d954SCole Faust                                                        TensorInfo(TensorShape(32U, 2U, 2U), 1, DataType::F16),
101*c217d954SCole Faust                                                        TensorInfo(TensorShape(32U, 2U, 2U), 1, DataType::F32),
102*c217d954SCole Faust                                                        TensorInfo(TensorShape(32U, 2U, 2U), 1, DataType::F32),
103*c217d954SCole Faust                                                        TensorInfo(TensorShape(32U, 2U, 2U), 1, DataType::F32),
104*c217d954SCole Faust                                                        TensorInfo(TensorShape(32U, 2U, 2U), 1, DataType::F32),
105*c217d954SCole Faust                                                        TensorInfo(TensorShape(32U, 2U, 2U), 1, DataType::F32),
106*c217d954SCole Faust                                                        TensorInfo(TensorShape(32U, 2U, 2U), 1, DataType::F32),
107*c217d954SCole Faust                                                      })),
108*c217d954SCole Faust                 framework::dataset::make("N0",{ 4, 0, 4, 4, 4, 17, 4, 4 })),
109*c217d954SCole Faust                 framework::dataset::make("K0",{ 4, 4, 0, 4, 4, 4, 17, 1 })),
110*c217d954SCole Faust                 framework::dataset::make("H0",{ 4, 4, 4, 0, 4, 4, 4, 4 })),
111*c217d954SCole Faust                framework::dataset::make("Expected", { false, false, false, false, false, false, false})),
112*c217d954SCole Faust                input_info, output_info, n0, k0, h0, expected)
113*c217d954SCole Faust {
114*c217d954SCole Faust     GEMMRHSMatrixInfo rhs_info;
115*c217d954SCole Faust     rhs_info.n0 = n0;
116*c217d954SCole Faust     rhs_info.k0 = k0;
117*c217d954SCole Faust     rhs_info.h0 = h0;
118*c217d954SCole Faust     rhs_info.transpose = true;
119*c217d954SCole Faust     rhs_info.interleave = true;
120*c217d954SCole Faust 
121*c217d954SCole Faust     bool has_error = bool(ClGemmReshapeRhsMatrixKernel::validate(&input_info.clone()->set_is_resizable(false), (output_info.total_size() == 0) ? nullptr : &output_info.clone()->set_is_resizable(false), rhs_info));
122*c217d954SCole Faust     ARM_COMPUTE_EXPECT(has_error == expected, framework::LogLevel::ERRORS);
123*c217d954SCole Faust }
124*c217d954SCole Faust 
125*c217d954SCole Faust DATA_TEST_CASE(ValidatePadding, framework::DatasetMode::ALL, combine(combine(combine(combine(
126*c217d954SCole Faust                framework::dataset::make("InputShape", { TensorShape(32U, 16U, 1U),
127*c217d954SCole Faust                                                         TensorShape(32U, 16U, 2U)
128*c217d954SCole Faust                                                      }),
129*c217d954SCole Faust                 framework::dataset::make("N0",{ 4 })),
130*c217d954SCole Faust                 framework::dataset::make("K0",{ 4, 8, 16 })),
131*c217d954SCole Faust                 framework::dataset::make("H0",{ 1, 2, 4 })),
132*c217d954SCole Faust                 framework::dataset::make("DataType",{ DataType::F32, DataType::F16 })),
133*c217d954SCole Faust                input_shape, n0, k0, h0, data_type)
134*c217d954SCole Faust {
135*c217d954SCole Faust     CLTensor input;
136*c217d954SCole Faust     CLTensor output;
137*c217d954SCole Faust 
138*c217d954SCole Faust     input.info()->init(input_shape, 1, data_type);
139*c217d954SCole Faust 
140*c217d954SCole Faust     unsigned int padding = 0;
141*c217d954SCole Faust 
142*c217d954SCole Faust     GEMMRHSMatrixInfo rhs_info;
143*c217d954SCole Faust     rhs_info.n0 = n0;
144*c217d954SCole Faust     rhs_info.k0 = k0;
145*c217d954SCole Faust     rhs_info.h0 = h0;
146*c217d954SCole Faust     rhs_info.transpose = true;
147*c217d954SCole Faust     rhs_info.interleave = true;
148*c217d954SCole Faust     rhs_info.export_to_cl_image = image2d_from_buffer_supported(CLKernelLibrary::get().get_device()) && (get_cl_image_pitch_alignment(CLKernelLibrary::get().get_device()) != 0);
149*c217d954SCole Faust 
150*c217d954SCole Faust     if(rhs_info.export_to_cl_image)
151*c217d954SCole Faust     {
152*c217d954SCole Faust         TensorShape output_shape = compute_rhs_reshaped_shape(*input.info(), rhs_info);
153*c217d954SCole Faust         constexpr unsigned int num_floats_per_pixel = 4;
154*c217d954SCole Faust 
155*c217d954SCole Faust         const unsigned int pixel_aligment      = get_cl_image_pitch_alignment(CLKernelLibrary::get().get_device());
156*c217d954SCole Faust         const unsigned int row_pitch_alignment = pixel_aligment * num_floats_per_pixel;
157*c217d954SCole Faust         const unsigned int round_up_width      = ((output_shape[0] + row_pitch_alignment - 1) / row_pitch_alignment) * row_pitch_alignment;
158*c217d954SCole Faust 
159*c217d954SCole Faust         padding = round_up_width - output_shape[0];
160*c217d954SCole Faust     }
161*c217d954SCole Faust 
162*c217d954SCole Faust     ClGemmReshapeRhsMatrixKernel kernel;
163*c217d954SCole Faust 
164*c217d954SCole Faust     kernel.configure(CLKernelLibrary::get().get_compile_context(), input.info(), output.info(), rhs_info);
165*c217d954SCole Faust 
166*c217d954SCole Faust     ARM_COMPUTE_EXPECT((output.info()->padding().right == padding), framework::LogLevel::ERRORS);
167*c217d954SCole Faust }
168*c217d954SCole Faust // clang-format on
169*c217d954SCole Faust // *INDENT-ON*
170*c217d954SCole Faust 
171*c217d954SCole Faust // Run S32 tests only for transpose = false
172*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(S32_NT, CLGEMMReshapeRHSMatrixFixture<int>, framework::DatasetMode::ALL,
173*c217d954SCole Faust                        combine(combine(combine(combine(combine(combine(combine(datasets::SmallGEMMReshape2DShapes(),
174*c217d954SCole Faust                                                                                b_values),
175*c217d954SCole Faust                                                                        framework::dataset::make("DataType", DataType::S32)),
176*c217d954SCole Faust                                                                n0_values_nt_s32),
177*c217d954SCole Faust                                                        k0_values_nt_s32),
178*c217d954SCole Faust                                                h0_values),
179*c217d954SCole Faust                                        i_values),
180*c217d954SCole Faust                                framework::dataset::make("transpose", false)))
181*c217d954SCole Faust {
182*c217d954SCole Faust     // Validate output
183*c217d954SCole Faust     validate(CLAccessor(_target), _reference);
184*c217d954SCole Faust }
185*c217d954SCole Faust 
186*c217d954SCole Faust // Run S32 tests only for transpose = true
187*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(S32_T, CLGEMMReshapeRHSMatrixFixture<int>, framework::DatasetMode::ALL,
188*c217d954SCole Faust                        combine(combine(combine(combine(combine(combine(combine(datasets::SmallGEMMReshape2DShapes(),
189*c217d954SCole Faust                                                                                b_values),
190*c217d954SCole Faust                                                                        framework::dataset::make("DataType", DataType::S32)),
191*c217d954SCole Faust                                                                n0_values_t_s32),
192*c217d954SCole Faust                                                        k0_values_t_s32),
193*c217d954SCole Faust                                                h0_values),
194*c217d954SCole Faust                                        i_values),
195*c217d954SCole Faust                                framework::dataset::make("transpose", true)))
196*c217d954SCole Faust {
197*c217d954SCole Faust     // Validate output
198*c217d954SCole Faust     validate(CLAccessor(_target), _reference);
199*c217d954SCole Faust }
200*c217d954SCole Faust 
201*c217d954SCole Faust // Run S16 tests only for transpose = false
202*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(S16_NT, CLGEMMReshapeRHSMatrixFixture<short>, framework::DatasetMode::ALL,
203*c217d954SCole Faust                        combine(combine(combine(combine(combine(combine(combine(datasets::SmallGEMMReshape2DShapes(),
204*c217d954SCole Faust                                                                                b_values),
205*c217d954SCole Faust                                                                        framework::dataset::make("DataType", DataType::S16)),
206*c217d954SCole Faust                                                                n0_values_nt_s16),
207*c217d954SCole Faust                                                        k0_values_nt_s16),
208*c217d954SCole Faust                                                h0_values),
209*c217d954SCole Faust                                        i_values),
210*c217d954SCole Faust                                framework::dataset::make("transpose", false)))
211*c217d954SCole Faust {
212*c217d954SCole Faust     // Validate output
213*c217d954SCole Faust     validate(CLAccessor(_target), _reference);
214*c217d954SCole Faust }
215*c217d954SCole Faust 
216*c217d954SCole Faust // Run S16 tests only for transpose = true
217*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(S16_T, CLGEMMReshapeRHSMatrixFixture<short>, framework::DatasetMode::ALL,
218*c217d954SCole Faust                        combine(combine(combine(combine(combine(combine(combine(datasets::SmallGEMMReshape2DShapes(),
219*c217d954SCole Faust                                                                                b_values),
220*c217d954SCole Faust                                                                        framework::dataset::make("DataType", DataType::S16)),
221*c217d954SCole Faust                                                                n0_values_t_s16),
222*c217d954SCole Faust                                                        k0_values_t_s16),
223*c217d954SCole Faust                                                h0_values),
224*c217d954SCole Faust                                        i_values),
225*c217d954SCole Faust                                framework::dataset::make("transpose", true)))
226*c217d954SCole Faust {
227*c217d954SCole Faust     // Validate output
228*c217d954SCole Faust     validate(CLAccessor(_target), _reference);
229*c217d954SCole Faust }
230*c217d954SCole Faust 
231*c217d954SCole Faust // Run S8 tests only for transpose = false
232*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(S8_NT, CLGEMMReshapeRHSMatrixFixture<char>, framework::DatasetMode::ALL,
233*c217d954SCole Faust                        combine(combine(combine(combine(combine(combine(combine(datasets::SmallGEMMReshape2DShapes(),
234*c217d954SCole Faust                                                                                b_values),
235*c217d954SCole Faust                                                                        framework::dataset::make("DataType", DataType::S8)),
236*c217d954SCole Faust                                                                n0_values_nt_s8),
237*c217d954SCole Faust                                                        k0_values_nt_s8),
238*c217d954SCole Faust                                                h0_values),
239*c217d954SCole Faust                                        i_values),
240*c217d954SCole Faust                                framework::dataset::make("transpose", false)))
241*c217d954SCole Faust {
242*c217d954SCole Faust     // Validate output
243*c217d954SCole Faust     validate(CLAccessor(_target), _reference);
244*c217d954SCole Faust }
245*c217d954SCole Faust 
246*c217d954SCole Faust // Run S8 tests only for transpose = true
247*c217d954SCole Faust FIXTURE_DATA_TEST_CASE(S8_T, CLGEMMReshapeRHSMatrixFixture<char>, framework::DatasetMode::ALL,
248*c217d954SCole Faust                        combine(combine(combine(combine(combine(combine(combine(datasets::SmallGEMMReshape2DShapes(),
249*c217d954SCole Faust                                                                                b_values),
250*c217d954SCole Faust                                                                        framework::dataset::make("DataType", DataType::S8)),
251*c217d954SCole Faust                                                                n0_values_t_s8),
252*c217d954SCole Faust                                                        k0_values_t_s8),
253*c217d954SCole Faust                                                h0_values),
254*c217d954SCole Faust                                        i_values),
255*c217d954SCole Faust                                framework::dataset::make("transpose", true)))
256*c217d954SCole Faust {
257*c217d954SCole Faust     // Validate output
258*c217d954SCole Faust     validate(CLAccessor(_target), _reference);
259*c217d954SCole Faust }
260*c217d954SCole Faust 
261*c217d954SCole Faust TEST_SUITE_END() // GEMMReshapeRHSMatrix
262*c217d954SCole Faust TEST_SUITE_END() // CL
263*c217d954SCole Faust } // namespace validation
264*c217d954SCole Faust } // namespace test
265*c217d954SCole Faust } // namespace arm_compute
266