blob: 1b19f1ec5bb3669e2a257b383c1f32b4ce6f9368 [file] [log] [blame]
Georgios Pinitas856f66e2021-04-22 21:13:21 +01001/*
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +00002 * Copyright (c) 2019-2023 Arm Limited.
Georgios Pinitas856f66e2021-04-22 21:13:21 +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 */
Georgios Pinitas7891a732021-08-20 21:39:25 +010024#include "src/gpu/cl/kernels/ClGemmMatrixMultiplyReshapedOnlyRhsKernel.h"
Georgios Pinitas856f66e2021-04-22 21:13:21 +010025
26#include "arm_compute/core/CL/ICLTensor.h"
Jakub Sujak0d27b2e2023-08-24 14:01:20 +010027#include "arm_compute/core/utils/ActivationFunctionUtils.h"
Jakub Sujak0d27b2e2023-08-24 14:01:20 +010028#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010029#include "arm_compute/core/utils/StringUtils.h"
30
Georgios Pinitas856f66e2021-04-22 21:13:21 +010031#include "src/core/CL/CLUtils.h"
32#include "src/core/CL/CLValidate.h"
Georgios Pinitas856f66e2021-04-22 21:13:21 +010033#include "src/core/helpers/AutoConfiguration.h"
34#include "src/core/helpers/WindowHelpers.h"
35#include "src/core/utils/helpers/float_ops.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010036#include "src/gpu/cl/kernels/gemm/ClGemmHelpers.h"
Georgios Pinitas856f66e2021-04-22 21:13:21 +010037#include "support/Cast.h"
38#include "support/StringSupport.h"
39
40namespace arm_compute
41{
42namespace opencl
43{
44namespace kernels
45{
46namespace
47{
48using ElementsProcessed = Steps;
49
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010050Status validate_arguments(const ITensorInfo *src0,
51 const ITensorInfo *src1,
52 const ITensorInfo *src2,
53 const ITensorInfo *dst,
54 float alpha,
55 float beta,
56 const GEMMLHSMatrixInfo &lhs_info,
57 const GEMMRHSMatrixInfo &rhs_info,
58 const GEMMKernelInfo &gemm_info)
Georgios Pinitas856f66e2021-04-22 21:13:21 +010059{
60 ARM_COMPUTE_UNUSED(alpha);
61 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src0, src1, dst);
62 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src0);
63 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src0, 1, DataType::F16, DataType::F32);
64 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src0, src1);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010065 ARM_COMPUTE_RETURN_ERROR_ON_MSG(src0->num_dimensions() > 4,
66 "The number of dimensions for the LHS matrix must be <= 4");
67 ARM_COMPUTE_RETURN_ERROR_ON_MSG(src1->num_dimensions() > 3,
68 "The number of dimensions for the RHS matrix must be <= 3");
Georgios Pinitas856f66e2021-04-22 21:13:21 +010069 ARM_COMPUTE_RETURN_ERROR_ON_MSG(lhs_info.m0 < 1 || lhs_info.m0 > 8, "Only 1,2,3,4,5,6,7,8 are supported for m0");
70 ARM_COMPUTE_RETURN_ERROR_ON(rhs_info.k0 > 16 || rhs_info.k0 < 2);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010071 ARM_COMPUTE_RETURN_ERROR_ON_MSG(((rhs_info.k0 & (rhs_info.k0 - 1)) && rhs_info.k0 != 3),
72 "Only 2,3,4,8,16 are supported for k0");
Georgios Pinitas856f66e2021-04-22 21:13:21 +010073 ARM_COMPUTE_RETURN_ERROR_ON(rhs_info.n0 > 16 || rhs_info.n0 < 2);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010074 ARM_COMPUTE_RETURN_ERROR_ON_MSG(((rhs_info.n0 & (rhs_info.n0 - 1)) && rhs_info.n0 != 3),
75 "Only 2,3,4,8,16 are supported for n0");
76 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
77 (gemm_info.reinterpret_input_as_3d || gemm_info.depth_output_gemm3d != 0) && (src2 != nullptr) &&
78 (!gemm_info.broadcast_bias),
79 "Bias addition only supported with broadcast mode in case the input or dst has to be reinterpreted as 3D");
Georgios Pinitas856f66e2021-04-22 21:13:21 +010080 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.fp_mixed_precision, "Mixed precision not supported");
81 ARM_COMPUTE_RETURN_ON_ERROR(gemm::validate_image2d_support_on_rhs(*src1, rhs_info));
82
83 const unsigned int m = gemm_info.m;
84 const unsigned int n = gemm_info.n;
85 const unsigned int k = gemm_info.k;
86
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010087 TensorShape tensor_shape1{src1->tensor_shape()};
Georgios Pinitas856f66e2021-04-22 21:13:21 +010088 tensor_shape1.set(0, n);
89 tensor_shape1.set(1, k);
90
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010091 if (src2 != nullptr && !(helpers::float_ops::is_zero(beta)))
Georgios Pinitas856f66e2021-04-22 21:13:21 +010092 {
93 const unsigned int src2_dim0 = src2->dimension(0);
94 const unsigned int src2_dim1 = src2->dimension(1);
95
96 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src2, src0);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010097 if (gemm_info.broadcast_bias)
Georgios Pinitas856f66e2021-04-22 21:13:21 +010098 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010099 ARM_COMPUTE_RETURN_ERROR_ON_MSG((src2_dim1 != 1 || src2_dim0 != n),
100 "Incorrect dimension of bias matrix which is to be broadcasted");
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100101 }
102 else
103 {
104 ARM_COMPUTE_RETURN_ERROR_ON_MSG((src2_dim0 != n || src2_dim1 != m), "Incorrect dimension of bias matrix");
105 }
106 }
107
108 const TensorInfo tensor_info1 = src1->clone()->set_tensor_shape(tensor_shape1);
109
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100110 const TensorInfo tensor_info_reshaped1 =
111 src1->clone()->set_tensor_shape(misc::shape_calculator::compute_rhs_reshaped_shape(tensor_info1, rhs_info));
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100112
113 ARM_COMPUTE_RETURN_ERROR_ON(src0->dimension(0) != k);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100114 if (gemm_info.reinterpret_input_as_3d)
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100115 {
116 ARM_COMPUTE_RETURN_ERROR_ON(src0->dimension(1) * src0->dimension(2) != m);
117 }
118 else
119 {
120 ARM_COMPUTE_RETURN_ERROR_ON(src0->dimension(1) != m);
121 }
122 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(src1, &tensor_info_reshaped1);
123
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100124 if (dst->total_size() != 0)
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100125 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100126 const TensorInfo tensor_info_dst =
127 dst->clone()->set_tensor_shape(misc::shape_calculator::compute_mm_shape(*src0, *src1, gemm_info));
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100128 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(dst, &tensor_info_dst);
129 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src0, dst);
130 }
131
132 return Status{};
133}
134
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100135Window validate_and_configure_window(ITensorInfo *src0,
136 ITensorInfo *src1,
137 ITensorInfo *src2,
138 ITensorInfo *dst,
139 const GEMMLHSMatrixInfo &lhs_info,
140 const GEMMRHSMatrixInfo &rhs_info,
141 const GEMMKernelInfo &gemm_info,
142 ElementsProcessed &num_elements_processed)
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100143{
Giorgio Arena951d5202021-09-08 13:26:06 +0100144 ARM_COMPUTE_UNUSED(src0, src1, src2);
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100145 unsigned int &num_elems_processed_per_iteration_x = num_elements_processed[0];
146 unsigned int &num_elems_processed_per_iteration_y = num_elements_processed[1];
Ramy Elgammal451c3092022-02-01 23:01:27 +0000147 bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d;
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100148 bool reinterpret_output_as_3d = gemm_info.depth_output_gemm3d != 0;
149
Ramy Elgammal451c3092022-02-01 23:01:27 +0000150 // In case both input and dst have to be reinterpreted as 3D tensors,
151 // force reinterpret_input_as_3d and reinterpret_output_as_3d to be false.
152 // This approach should only be used when the input/dst tensors have pad on the y direction
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100153 if ((reinterpret_input_as_3d == reinterpret_output_as_3d) && gemm_info.has_pad_y)
Ramy Elgammal451c3092022-02-01 23:01:27 +0000154 {
155 reinterpret_output_as_3d = false;
156 }
157
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100158 TensorInfo tmp_info(*dst);
159
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100160 if (reinterpret_output_as_3d)
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100161 {
162 // Since the dst tensor has to be reinterpreted as 3D and the execute window is based on a 2D GEMM,
163 // the window needs to be constructed on the 2D collapsed version of the tensor
164 TensorShape tmp_shape(dst->tensor_shape());
165 tmp_shape.collapse(2U, 1U);
166 tmp_info.set_tensor_shape(tmp_shape);
167 }
168
169 // Configure kernel window
170 num_elems_processed_per_iteration_x = rhs_info.n0;
171 num_elems_processed_per_iteration_y = lhs_info.m0;
172
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100173 Window win =
174 calculate_max_window(tmp_info, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100175
176 // Collapse along the Z direction
177 // This collapse needs to be here in order to tune the Z dimension of LWS
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100178 const unsigned int dimension_to_collapse = std::min(static_cast<unsigned int>(dst->num_dimensions()), 2u);
Giorgio Arena951d5202021-09-08 13:26:06 +0100179 Window collapsed = win.collapse(win, dimension_to_collapse);
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100180
Giorgio Arena951d5202021-09-08 13:26:06 +0100181 return collapsed;
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100182}
183} // namespace
184
Giorgio Arena4a95bba2021-06-28 11:00:27 +0100185ClGemmMatrixMultiplyReshapedOnlyRhsKernel::ClGemmMatrixMultiplyReshapedOnlyRhsKernel()
186{
187 _type = CLKernelType::GEMM;
188}
189
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100190void ClGemmMatrixMultiplyReshapedOnlyRhsKernel::configure(const CLCompileContext &compile_context,
191 const ITensorInfo *src0,
192 const ITensorInfo *src1,
193 const ITensorInfo *src2,
194 ITensorInfo *dst,
195 float alpha,
196 float beta,
197 const GEMMLHSMatrixInfo &lhs_info,
198 const GEMMRHSMatrixInfo &rhs_info,
199 const GEMMKernelInfo &gemm_info)
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100200{
201 ARM_COMPUTE_ERROR_ON_NULLPTR(src0, src1, dst);
202
SiCongLiafa19722021-10-24 19:12:33 +0100203 // dst tensor auto initialization if not yet initialized
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100204 auto_init_if_empty(
205 *dst, src0->clone()->set_tensor_shape(misc::shape_calculator::compute_mm_shape(*src0, *src1, gemm_info)));
SiCongLiafa19722021-10-24 19:12:33 +0100206
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100207 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src0, src1, src2, dst, alpha, beta, lhs_info, rhs_info, gemm_info));
208
209 _reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d;
210 _reinterpret_output_as_3d = gemm_info.depth_output_gemm3d != 0;
211 _use_dummy_work_items = preferred_dummy_work_items_support(CLKernelLibrary::get().get_device());
212 _add_bias = src2 != nullptr;
213 _export_to_cl_image = rhs_info.export_to_cl_image;
Ramy Elgammal451c3092022-02-01 23:01:27 +0000214 _has_pad_y = gemm_info.has_pad_y;
Giorgio Arena951d5202021-09-08 13:26:06 +0100215
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100216 auto padding_info = get_padding_info({src0, src1, src2, dst});
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100217
Ramy Elgammal451c3092022-02-01 23:01:27 +0000218 // In case both input and dst have to be reinterpreted as 3D tensors,
219 // force reinterpret_input_as_3d and reinterpret_output_as_3d to be false.
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100220 if ((_reinterpret_input_as_3d == _reinterpret_output_as_3d) && _has_pad_y)
Ramy Elgammal451c3092022-02-01 23:01:27 +0000221 {
222 _reinterpret_input_as_3d = false;
223 _reinterpret_output_as_3d = false;
224 }
225
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100226 // Check if we need to slide the matrix B
227 const unsigned int num_dimensions_src0 = src0->num_dimensions();
228 _slide_matrix_b = (src1->num_dimensions() >= num_dimensions_src0);
229
230 ElementsProcessed num_elements_processed{};
231
232 // Configure kernel window
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100233 Window win = validate_and_configure_window(src0->clone().get(), src1->clone().get(),
234 (src2 != nullptr) ? src2->clone().get() : nullptr, dst->clone().get(),
235 lhs_info, rhs_info, gemm_info, num_elements_processed);
Giorgio Arena951d5202021-09-08 13:26:06 +0100236 ICLKernel::configure_internal(win);
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100237
238 // If _reinterpret_input_as_3d = reinterpret_output_as_3d = true,
239 // we will dispatch a batched-GEMM to reduce the complexity of the address calculation within the OpenCL kernel.
240 // This means that the actual m used by the kernel is given by dst->dimension(1) and not by gemm_info.m
241 const unsigned int internal_m = _reinterpret_output_as_3d ? gemm_info.m : dst->dimension(1);
242
Ramy Elgammal451c3092022-02-01 23:01:27 +0000243 // These variables are used only if gemm_info.has_pad_y == true
244 const unsigned int h_gemm_3d = _reinterpret_output_as_3d ? dst->dimension(1) : src0->dimension(1);
245 const unsigned int d_gemm_3d = _reinterpret_output_as_3d ? dst->dimension(2) : src0->dimension(2);
246
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100247 // Shrink M0 to be always <= M (internal_m) to prevent out-of-bounds reads.
248 // NOTE: This might have implications on heuristics and performance
249 const unsigned int internal_m0 = std::min(internal_m, lhs_info.m0);
250
251 // Calculate partial (store instead of load) M0 and partial N0 for the partial blocks at the end of a row/column if any. This is to avoid padding.
252 const unsigned int partial_store_m0 = internal_m % internal_m0;
253 const unsigned int partial_store_n0 = gemm_info.n % rhs_info.n0;
ramelg019cca5922021-11-11 10:05:00 +0000254 _m = internal_m;
255 _n = gemm_info.n;
256 _k = gemm_info.k;
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100257 // Create build options
258 CLBuildOptions build_opts;
259 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(src0->data_type()));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100260 build_opts.add_option_if(!(helpers::float_ops::is_one(alpha)),
261 "-DALPHA=" + float_to_string_with_full_precision(alpha));
Gian Marco Iodice10e88a72021-11-29 12:49:19 +0000262 build_opts.add_option_if(src2 != nullptr, "-DBETA=" + float_to_string_with_full_precision(beta));
263 build_opts.add_option_if(helpers::float_ops::is_one(beta), "-DUNIT_BETA");
264 build_opts.add_option_if(gemm_info.broadcast_bias, "-DBROADCAST_BIAS");
265 build_opts.add_option_if(!_slide_matrix_b, "-DMATRIX_B_DEPTH=" + support::cpp11::to_string(src1->dimension(2)));
266 build_opts.add_option_if(rhs_info.interleave, "-DRHS_INTERLEAVE");
Ramy Elgammal451c3092022-02-01 23:01:27 +0000267 build_opts.add_option_if(_use_dummy_work_items, "-DDUMMY_WORK_ITEMS");
268 build_opts.add_option_if(rhs_info.export_to_cl_image, "-DOPENCL_IMAGE_SUPPORT");
269 build_opts.add_option("-DRHS_HEIGHT=" + support::cpp11::to_string(src1->dimension(1)));
270 build_opts.add_option("-DM0=" + support::cpp11::to_string(internal_m0));
271 build_opts.add_option("-DN0=" + support::cpp11::to_string(rhs_info.n0));
272 build_opts.add_option("-DK0=" + support::cpp11::to_string(rhs_info.k0));
273 build_opts.add_option("-DH0=" + support::cpp11::to_string(rhs_info.h0));
274 build_opts.add_option("-DPARTIAL_STORE_M0=" + support::cpp11::to_string(partial_store_m0));
275 build_opts.add_option("-DPARTIAL_STORE_N0=" + support::cpp11::to_string(partial_store_n0));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100276 if (_has_pad_y)
Ramy Elgammal451c3092022-02-01 23:01:27 +0000277 {
278 build_opts.add_option_if(_reinterpret_input_as_3d, "-DREINTERPRET_INPUT_AS_3D");
279 build_opts.add_option_if(_reinterpret_output_as_3d, "-DREINTERPRET_OUTPUT_AS_3D");
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100280 build_opts.add_option_if(_reinterpret_input_as_3d || _reinterpret_output_as_3d,
281 "-DHEIGHT_GEMM3D=" + support::cpp11::to_string(h_gemm_3d));
282 build_opts.add_option_if(_reinterpret_input_as_3d || _reinterpret_output_as_3d,
283 "-DDEPTH_GEMM3D=" + support::cpp11::to_string(d_gemm_3d));
Ramy Elgammal451c3092022-02-01 23:01:27 +0000284 }
Jakub Sujak0d27b2e2023-08-24 14:01:20 +0100285
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100286 build_opts.add_option_if(gemm_info.activation_info.enabled(),
287 "-DACTIVATION_TYPE=" +
288 lower_string(string_from_activation_func(gemm_info.activation_info.activation())));
289 build_opts.add_option_if(gemm_info.activation_info.enabled(),
290 "-DA_VAL=" + float_to_string_with_full_precision(gemm_info.activation_info.a()));
291 build_opts.add_option_if(gemm_info.activation_info.enabled(),
292 "-DB_VAL=" + float_to_string_with_full_precision(gemm_info.activation_info.b()));
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100293
294 std::string kernel_name("gemm_mm_reshaped_only_rhs_");
295 kernel_name += rhs_info.transpose ? "t" : "nt";
Ramy Elgammal451c3092022-02-01 23:01:27 +0000296 kernel_name += rhs_info.export_to_cl_image ? "_texture" : "";
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100297
ramelg019cca5922021-11-11 10:05:00 +0000298 // A macro guard to compile ONLY the kernel of interest
299 build_opts.add_option("-D" + upper_string(kernel_name));
300
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100301 // Create kernel
302 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
303
304 // Set config_id for enabling LWS tuning
305 _config_id = kernel_name;
306 _config_id += "_";
Ramy Elgammal451c3092022-02-01 23:01:27 +0000307 _config_id += (_has_pad_y ? "" : "no_pad_y_");
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100308 _config_id += (_add_bias ? "add_bias_" : "");
309 _config_id += (gemm_info.broadcast_bias ? "broadcast_bias_" : "");
Ramy Elgammal451c3092022-02-01 23:01:27 +0000310 _config_id += (_reinterpret_input_as_3d ? "3di_" : "");
311 _config_id += (_reinterpret_output_as_3d ? "3do_" : "");
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100312 _config_id += (gemm_info.activation_info.enabled() ? "fused_activation_" : "");
313 _config_id += lower_string(string_from_data_type(src0->data_type()));
314 _config_id += "_";
315 _config_id += support::cpp11::to_string(dst->dimension(1));
316 _config_id += "_";
317 _config_id += support::cpp11::to_string(dst->dimension(0));
318 _config_id += "_";
319 _config_id += support::cpp11::to_string(gemm_info.k);
320 _config_id += "_";
321 _config_id += support::cpp11::to_string(dst->dimension(2));
322 _config_id += "_";
323 _config_id += support::cpp11::to_string(lhs_info.m0);
324 _config_id += "_";
325 _config_id += support::cpp11::to_string(rhs_info.n0);
326 _config_id += "_";
327 _config_id += support::cpp11::to_string(rhs_info.k0);
328 _config_id += "_";
329 _config_id += support::cpp11::to_string(rhs_info.h0);
330 _config_id += "_";
331 _config_id += support::cpp11::to_string(rhs_info.interleave);
332
333 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
334}
335
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100336Status ClGemmMatrixMultiplyReshapedOnlyRhsKernel::validate(const ITensorInfo *src0,
337 const ITensorInfo *src1,
338 const ITensorInfo *src2,
339 const ITensorInfo *dst,
340 float alpha,
341 float beta,
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100342 const GEMMLHSMatrixInfo &lhs_info,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100343 const GEMMRHSMatrixInfo &rhs_info,
344 const GEMMKernelInfo &gemm_info)
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100345{
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100346 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src0, src1, src2, dst, alpha, beta, lhs_info, rhs_info, gemm_info));
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100347 return Status{};
348}
349
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100350void ClGemmMatrixMultiplyReshapedOnlyRhsKernel::run_op(ITensorPack &tensors,
351 const Window &window,
352 cl::CommandQueue &queue)
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100353{
354 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
355 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
356
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100357 const auto src0 =
358 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
359 const auto src1 =
360 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
361 const auto src2 =
362 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_2));
363 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100364
365 ARM_COMPUTE_ERROR_ON_NULLPTR(src0, src1, dst);
366 ARM_COMPUTE_ERROR_ON(_add_bias && src2 == nullptr);
367
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100368 if (src1->info()->num_dimensions() < 3)
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100369 {
370 // The stride_z for matrix B must be zero if we do not slice
371 ARM_COMPUTE_ERROR_ON(src1->info()->strides_in_bytes()[3] != 0);
372 }
373
Ramy Elgammal451c3092022-02-01 23:01:27 +0000374 const size_t lhs_idx_batch_size = _reinterpret_input_as_3d && !_has_pad_y ? 3u : 2u;
375 const size_t rhs_idx_batch_size = 2u;
376 const size_t bia_idx_batch_size = 2u;
377 const size_t out_idx_batch_size = _reinterpret_output_as_3d && !_has_pad_y ? 3u : 2u;
378
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100379 Window slice = window.first_slice_window_3D();
380 Window slice_matrix_b = slice;
381
382 slice_matrix_b.set(Window::DimX, Window::Dimension(0, 1, 1));
383 slice_matrix_b.set(Window::DimY, Window::Dimension(0, 1, 1));
384
Ramy Elgammal451c3092022-02-01 23:01:27 +0000385 // Get cross plane pads
386 const unsigned int total_cross_plane_pad_lhs = src0->info()->padding().top + src0->info()->padding().bottom;
387 const unsigned int total_cross_plane_pad_out = dst->info()->padding().top + dst->info()->padding().bottom;
388
389 // The execution should fail if we try to run with has_pad_y = false but we have padding in either the LHS or DST tensor
390 ARM_COMPUTE_ERROR_ON(!_has_pad_y && ((total_cross_plane_pad_lhs != 0) || (total_cross_plane_pad_out != 0)));
391
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100392 cl::Image2D src1_image2d;
393
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100394 if (_export_to_cl_image)
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100395 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100396 const TensorShape shape2d(src1->info()->dimension(0) / 4,
397 src1->info()->dimension(1) * src1->info()->dimension(2));
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100398 const size_t image_row_pitch = src1->info()->strides_in_bytes()[1];
399
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100400 src1_image2d = create_image2d_from_buffer(CLKernelLibrary::get().context(), src1->cl_buffer(), shape2d,
401 src1->info()->data_type(), image_row_pitch, CLImage2DType::ReadOnly);
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100402 }
403
404 do
405 {
406 Window slice_b = slice;
407 // Don't slice matrix B along the z dimension if matrix B has just 2 dimensions and matrix A more than 2
408 // This scenario can happen when the matrix multiplication is used to perform a convolution operation
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100409 if (!_slide_matrix_b)
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100410 {
411 slice_b = slice_matrix_b;
412 }
413
414 unsigned int idx = 0;
415
416 // LHS buffer
Ramy Elgammal451c3092022-02-01 23:01:27 +0000417 add_2D_tensor_argument(idx, src0, slice);
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100418
419 // RHS buffer or RHS OpenCL image (_export_to_cl_image == true)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100420 if (_export_to_cl_image)
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100421 {
422 _kernel.setArg(idx++, src1_image2d);
423 }
Ramy Elgammal451c3092022-02-01 23:01:27 +0000424 else
425 {
426 add_2D_tensor_argument(idx, src1, slice_b);
427 }
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100428
429 // Bias buffer (_add_bias == true)
Ramy Elgammal451c3092022-02-01 23:01:27 +0000430 add_2D_tensor_argument_if(_add_bias, idx, src2, slice);
431
432 // dst buffer
433 add_2D_tensor_argument(idx, dst, slice);
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100434
Ramy Elgammal451c3092022-02-01 23:01:27 +0000435 // LHS stride_z
436 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(src0->info()->strides_in_bytes()[lhs_idx_batch_size]));
437
438 // RHS stride_z (not used if _export_to_cl_image == true)
439 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(src1->info()->strides_in_bytes()[rhs_idx_batch_size]));
440
441 // Bias stride_z (if _add_bias == true)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100442 if (_add_bias)
Ramy Elgammal451c3092022-02-01 23:01:27 +0000443 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100444 _kernel.setArg<cl_uint>(idx++,
445 static_cast<unsigned int>(src2->info()->strides_in_bytes()[bia_idx_batch_size]));
Ramy Elgammal451c3092022-02-01 23:01:27 +0000446 }
447
448 // dst stride_z
449 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(dst->info()->strides_in_bytes()[out_idx_batch_size]));
Ramy Elgammal451c3092022-02-01 23:01:27 +0000450
451 // Cross-plan padding (if _reinterpret_input_as_3d = true)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100452 if (_reinterpret_input_as_3d && _has_pad_y)
Ramy Elgammal451c3092022-02-01 23:01:27 +0000453 {
454 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(total_cross_plane_pad_lhs));
455 }
456
457 // Cross-plan padding (if reinterpret_output_as_3d = true)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100458 if (_reinterpret_output_as_3d && _has_pad_y)
Ramy Elgammal451c3092022-02-01 23:01:27 +0000459 {
460 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(total_cross_plane_pad_out));
461 }
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100462
ramelg019cca5922021-11-11 10:05:00 +0000463 // Pass m, n and k at runtime as signed ints, to ensure results of any subractions they could be operand in, would still be signed.
464 _kernel.setArg<cl_int>(idx++, _m);
465 _kernel.setArg<cl_int>(idx++, _n);
466 _kernel.setArg<cl_int>(idx++, _k);
467
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100468 enqueue(queue, *this, slice, lws_hint(), _use_dummy_work_items);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100469 } while (window.slide_window_slice_3D(slice));
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100470}
471} // namespace kernels
472} // namespace opencl
473} // namespace arm_compute