blob: fd23aa992447f702f5f6dd0efbf02f10a74c801a [file] [log] [blame]
Georgios Pinitas856f66e2021-04-22 21:13:21 +01001/*
Matthew Bentham314d3e22023-06-23 10:53:52 +00002 * Copyright (c) 2019-2021, 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/ClGemmMatrixMultiplyNativeKernel.h"
Georgios Pinitas856f66e2021-04-22 21:13:21 +010025
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/ICLTensor.h"
29#include "arm_compute/core/CL/OpenCL.h"
30#include "arm_compute/core/Helpers.h"
31#include "arm_compute/core/TensorInfo.h"
Jakub Sujak0d27b2e2023-08-24 14:01:20 +010032#include "arm_compute/core/utils/ActivationFunctionUtils.h"
Jakub Sujak0d27b2e2023-08-24 14:01:20 +010033#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010034#include "arm_compute/core/utils/StringUtils.h"
35#include "arm_compute/core/Validate.h"
36
Georgios Pinitas856f66e2021-04-22 21:13:21 +010037#include "src/core/AccessWindowStatic.h"
SiCongLiafa19722021-10-24 19:12:33 +010038#include "src/core/CL/CLUtils.h"
Georgios Pinitas856f66e2021-04-22 21:13:21 +010039#include "src/core/helpers/AutoConfiguration.h"
40#include "src/core/helpers/WindowHelpers.h"
41#include "src/core/utils/helpers/float_ops.h"
42#include "support/Cast.h"
43#include "support/StringSupport.h"
44
45namespace arm_compute
46{
47namespace opencl
48{
49namespace kernels
50{
51namespace
52{
53using ElementsProcessed = Steps;
54
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010055Status validate_arguments(const ITensorInfo *src0,
56 const ITensorInfo *src1,
57 const ITensorInfo *src2,
58 const ITensorInfo *dst,
59 float alpha,
60 float beta,
61 const GEMMLHSMatrixInfo &lhs_info,
Georgios Pinitas856f66e2021-04-22 21:13:21 +010062 const GEMMRHSMatrixInfo &rhs_info,
63 const GEMMKernelInfo &gemm_info)
64{
65 ARM_COMPUTE_UNUSED(alpha);
66 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src0, src1, dst);
Gian Marco Iodicec9cecc02021-10-15 10:23:24 +010067 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src0, 1, DataType::F32, DataType::F16);
Georgios Pinitas856f66e2021-04-22 21:13:21 +010068 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src0, src1);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010069 ARM_COMPUTE_RETURN_ERROR_ON_MSG(src0->num_dimensions() > 4,
70 "The number of dimensions for the LHS matrix must be <= 4");
71 ARM_COMPUTE_RETURN_ERROR_ON_MSG(src1->num_dimensions() > 3,
72 "The number of dimensions for the RHS matrix must be <= 3");
73 ARM_COMPUTE_RETURN_ERROR_ON_MSG(((rhs_info.k0 & (rhs_info.k0 - 1)) && rhs_info.k0 != 3),
74 "Only 2,3,4,8,16 are supported for k0");
Georgios Pinitas856f66e2021-04-22 21:13:21 +010075 ARM_COMPUTE_RETURN_ERROR_ON(rhs_info.k0 > 16);
76 ARM_COMPUTE_RETURN_ERROR_ON(lhs_info.m0 < 1 || lhs_info.m0 > 8);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010077 ARM_COMPUTE_RETURN_ERROR_ON_MSG(((rhs_info.n0 & (rhs_info.n0 - 1)) && rhs_info.n0 != 3),
78 "Only 2,3,4,8,16 are supported for n0");
79 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
80 (gemm_info.reinterpret_input_as_3d || gemm_info.depth_output_gemm3d != 0) && (src2 != nullptr) &&
81 (!gemm_info.broadcast_bias),
82 "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 +010083 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.fp_mixed_precision, "Mixed precision not supported");
84 ARM_COMPUTE_RETURN_ERROR_ON_MSG(rhs_info.export_to_cl_image, "Export to CLImage not supported for GEMM native");
85
86 const unsigned int m = gemm_info.m;
87 const unsigned int n = gemm_info.n;
88 const unsigned int k = gemm_info.k;
89
90 ARM_COMPUTE_UNUSED(m);
91 ARM_COMPUTE_UNUSED(n);
92 ARM_COMPUTE_UNUSED(k);
93
94 ARM_COMPUTE_RETURN_ERROR_ON(src0->dimension(0) != k);
95 ARM_COMPUTE_RETURN_ERROR_ON(src1->dimension(0) != n);
96 ARM_COMPUTE_RETURN_ERROR_ON(src1->dimension(1) != k);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010097 if (gemm_info.reinterpret_input_as_3d)
Georgios Pinitas856f66e2021-04-22 21:13:21 +010098 {
99 ARM_COMPUTE_RETURN_ERROR_ON(src0->dimension(1) * src0->dimension(2) != m);
100 }
101 else
102 {
103 ARM_COMPUTE_RETURN_ERROR_ON(src0->dimension(1) != m);
104 }
105
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100106 if (src2 != nullptr && !(helpers::float_ops::is_zero(beta)))
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100107 {
108 const unsigned int src2_dim0 = src2->dimension(0);
109 const unsigned int src2_dim1 = src2->dimension(1);
110
111 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src2, src1);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100112 if (gemm_info.broadcast_bias)
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100113 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100114 ARM_COMPUTE_RETURN_ERROR_ON_MSG((src2_dim1 != 1 || src2_dim0 != n),
115 "Incorrect dimension of bias matrix which is to be broadcasted");
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100116 }
117 else
118 {
119 ARM_COMPUTE_RETURN_ERROR_ON_MSG((src2_dim0 != n || src2_dim1 != m), "Incorrect dimension of bias matrix");
120 }
121 }
122
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100123 if (dst->total_size() != 0)
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100124 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100125 const TensorInfo tensor_info_dst =
126 dst->clone()->set_tensor_shape(misc::shape_calculator::compute_mm_shape(*src0, *src1, gemm_info));
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100127 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(dst, &tensor_info_dst);
128 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src0, dst);
129 }
130
131 return Status{};
132}
133
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100134std::pair<Status, Window> validate_and_configure_window(ITensorInfo *src0,
135 ITensorInfo *src1,
136 ITensorInfo *src2,
137 ITensorInfo *dst,
138 const GEMMLHSMatrixInfo &lhs_info,
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100139 const GEMMRHSMatrixInfo &rhs_info,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100140 const GEMMKernelInfo &gemm_info,
141 ElementsProcessed &num_elements_processed)
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100142{
143 unsigned int &num_elems_processed_per_iteration_x = num_elements_processed[0];
144 unsigned int &num_elems_processed_per_iteration_y = num_elements_processed[1];
145 bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d;
146 bool reinterpret_output_as_3d = gemm_info.depth_output_gemm3d != 0;
147
SiCongLi71cbd282021-11-03 12:17:06 +0000148 Window win{};
149 Window win_out{};
150 bool window_changed = false;
151
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100152 // In case both input and dst have to be reinterpreted as 3D tensors,
153 // force reinterpret_input_as_3d and reinterpret_output_as_3d to be false.
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100154 if (reinterpret_input_as_3d == reinterpret_output_as_3d)
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100155 {
156 reinterpret_output_as_3d = false;
157 }
158
SiCongLi71cbd282021-11-03 12:17:06 +0000159 // dst tensor auto initialization if not yet initialized
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100160 auto_init_if_empty(
161 *dst, src0->clone()->set_tensor_shape(misc::shape_calculator::compute_mm_shape(*src0, *src1, gemm_info)));
SiCongLi71cbd282021-11-03 12:17:06 +0000162
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100163 TensorInfo tmp_info(*dst);
164
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100165 if (reinterpret_output_as_3d)
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100166 {
167 // Since the dst tensor has to be reinterpreted as 3D and the execute window is based on a 2D GEMM,
168 // the window needs to be constructed on the 2D collapsed version of the tensor
169 TensorShape tmp_shape(dst->tensor_shape());
170 tmp_shape.collapse(2U, 1U);
171 tmp_info.set_tensor_shape(tmp_shape);
172 }
173
174 // Configure kernel window
175 num_elems_processed_per_iteration_x = rhs_info.n0;
176 num_elems_processed_per_iteration_y = lhs_info.m0;
177
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100178 win =
179 calculate_max_window(tmp_info, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
180 win_out =
181 calculate_max_window(*dst, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
SiCongLi71cbd282021-11-03 12:17:06 +0000182
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100183 AccessWindowStatic src0_access(src0, 0, 0, src0->dimension(0), src0->dimension(1));
184 AccessWindowStatic src1_access(
185 src1, 0, 0, ceil_to_multiple(src1->dimension(0), num_elems_processed_per_iteration_x), src1->dimension(1));
186 AccessWindowStatic dst_access(dst, 0, 0, dst->dimension(0), dst->dimension(1));
SiCongLi71cbd282021-11-03 12:17:06 +0000187
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100188 if (src2 != nullptr)
SiCongLi71cbd282021-11-03 12:17:06 +0000189 {
190 const int bias_processed_per_iteration_x = num_elems_processed_per_iteration_x;
191
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100192 AccessWindowStatic src2_access(src2, 0, 0, ceil_to_multiple(src2->dimension(0), bias_processed_per_iteration_x),
SiCongLi71cbd282021-11-03 12:17:06 +0000193 src2->dimension(1));
194
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100195 window_changed = update_window_and_padding(win, src0_access, src1_access,
196 src2_access) || // window used by the execute_window_loop
197 update_window_and_padding(
198 win_out, dst_access); // window used to update the padding requirements of dst tensor
SiCongLi71cbd282021-11-03 12:17:06 +0000199 }
200 else
201 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100202 window_changed =
203 update_window_and_padding(win, src0_access, src1_access) || // window used by the execute_window_loop
204 update_window_and_padding(win_out,
205 dst_access); // window used to update the padding requirements of dst tensor
SiCongLi71cbd282021-11-03 12:17:06 +0000206 }
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100207
208 // Collapse along the Z direction
209 // This collapse needs to be here in order to tune the Z dimension of LWS
SiCongLi71cbd282021-11-03 12:17:06 +0000210 Window collapsed = win;
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100211 const unsigned int dimension_to_collapse = std::min(static_cast<unsigned int>(dst->num_dimensions()), 2u);
SiCongLi71cbd282021-11-03 12:17:06 +0000212 collapsed = win.collapse(win, dimension_to_collapse);
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100213
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100214 Status err =
215 (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
SiCongLi71cbd282021-11-03 12:17:06 +0000216 return std::make_pair(err, collapsed);
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100217}
218} // namespace
219
Giorgio Arena4a95bba2021-06-28 11:00:27 +0100220ClGemmMatrixMultiplyNativeKernel::ClGemmMatrixMultiplyNativeKernel()
221{
222 _type = CLKernelType::GEMM;
223}
224
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100225void ClGemmMatrixMultiplyNativeKernel::configure(const CLCompileContext &compile_context,
226 ITensorInfo *src0,
227 ITensorInfo *src1,
228 ITensorInfo *src2,
229 ITensorInfo *dst,
230 float alpha,
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100231 float beta,
232 const GEMMLHSMatrixInfo &lhs_info,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100233 const GEMMRHSMatrixInfo &rhs_info,
234 const GEMMKernelInfo &gemm_info)
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100235{
236 ARM_COMPUTE_ERROR_ON_NULLPTR(src0, src1, dst);
237
SiCongLi48717a32021-10-28 18:42:51 +0100238 // dst tensor auto initialization if not yet initialized
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100239 auto_init_if_empty(
240 *dst, src0->clone()->set_tensor_shape(misc::shape_calculator::compute_mm_shape(*src0, *src1, gemm_info)));
SiCongLi48717a32021-10-28 18:42:51 +0100241
SiCongLiafa19722021-10-24 19:12:33 +0100242 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src0, src1, src2, dst, alpha, beta, lhs_info, rhs_info, gemm_info));
243
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100244 auto padding_info = get_padding_info({src0, dst});
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100245 _reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d;
246 _reinterpret_output_as_3d = gemm_info.depth_output_gemm3d != 0;
247 _use_dummy_work_items = preferred_dummy_work_items_support(CLKernelLibrary::get().get_device());
248 _add_bias = src2 != nullptr;
249
250 // In case both input and dst have to be reinterpreted as 3D tensors,
251 // force reinterpret_input_as_3d and reinterpret_output_as_3d to be false.
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100252 if (_reinterpret_input_as_3d == _reinterpret_output_as_3d)
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100253 {
254 _reinterpret_input_as_3d = false;
255 _reinterpret_output_as_3d = false;
256 }
257
258 // Check if we need to slide the matrix B
259 const unsigned int num_dimensions_src0 = src0->num_dimensions();
260 _slide_matrix_b = (src1->num_dimensions() >= num_dimensions_src0);
261
262 ElementsProcessed num_elements_processed{};
263
264 // Configure kernel window
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100265 auto win_config = validate_and_configure_window(src0, src1, src2 != nullptr ? src2 : nullptr, dst, lhs_info,
266 rhs_info, gemm_info, num_elements_processed);
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100267 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
268 IClKernel::configure_internal(win_config.second);
269
270 // If _reinterpret_input_as_3d = _reinterpret_output_as_3d = true,
271 // we will dispatch a batched-GEMM to reduce the complexity of the address calculation within the OpenCL kernel.
272 // This means that the actual m used by the kernel is given by dst->dimension(1) and not by gemm_info.m
273 const unsigned int internal_m = _reinterpret_output_as_3d ? gemm_info.m : dst->dimension(1);
274
275 const unsigned int h_gemm_3d = _reinterpret_output_as_3d ? dst->dimension(1) : src0->dimension(1);
276 const unsigned int d_gemm_3d = _reinterpret_output_as_3d ? dst->dimension(2) : src0->dimension(2);
277
278 // 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.
279 const unsigned int partial_store_m0 = internal_m % lhs_info.m0;
280 const unsigned int partial_store_n0 = gemm_info.n % rhs_info.n0;
281
282 // Shrink M0 to be always <= M (internal_m) to prevent out-of-bounds reads.
283 // NOTE: This might have implications on heuristics and performance
284 const unsigned int internal_m0 = std::min(internal_m, lhs_info.m0);
ramelg019cca5922021-11-11 10:05:00 +0000285 _m = internal_m;
286 _n = gemm_info.n;
287 _k = gemm_info.k;
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100288
289 // Create build options
290 CLBuildOptions build_opts;
291 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(src0->data_type()));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100292 build_opts.add_option_if(!(helpers::float_ops::is_one(alpha)),
293 "-DALPHA=" + float_to_string_with_full_precision(alpha));
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100294 build_opts.add_option_if(src2 != nullptr, "-DBETA=" + float_to_string_with_full_precision(beta));
295 build_opts.add_option_if(helpers::float_ops::is_one(beta), "-DUNIT_BETA");
296 build_opts.add_option_if(gemm_info.broadcast_bias, "-DBROADCAST_BIAS");
297 build_opts.add_option_if(_reinterpret_input_as_3d, "-DREINTERPRET_INPUT_AS_3D");
298 build_opts.add_option_if(_reinterpret_output_as_3d, "-DREINTERPRET_OUTPUT_AS_3D");
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100299 build_opts.add_option_if(_reinterpret_input_as_3d || _reinterpret_output_as_3d,
300 "-DHEIGHT_GEMM3D=" + support::cpp11::to_string(h_gemm_3d));
301 build_opts.add_option_if(_reinterpret_input_as_3d || _reinterpret_output_as_3d,
302 "-DDEPTH_GEMM3D=" + support::cpp11::to_string(d_gemm_3d));
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100303 build_opts.add_option_if(!_slide_matrix_b, "-DMATRIX_B_DEPTH=" + support::cpp11::to_string(src1->dimension(2)));
304 build_opts.add_option_if(_use_dummy_work_items, "-DDUMMY_WORK_ITEMS");
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100305 build_opts.add_option("-DM0=" + support::cpp11::to_string(internal_m0));
306 build_opts.add_option("-DN0=" + support::cpp11::to_string(rhs_info.n0));
307 build_opts.add_option("-DK0=" + support::cpp11::to_string(rhs_info.k0));
308 build_opts.add_option("-DPARTIAL_STORE_M0=" + support::cpp11::to_string(partial_store_m0));
309 build_opts.add_option("-DPARTIAL_STORE_N0=" + support::cpp11::to_string(partial_store_n0));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100310 build_opts.add_option_if(gemm_info.activation_info.enabled(),
311 "-DACTIVATION_TYPE=" +
312 lower_string(string_from_activation_func(gemm_info.activation_info.activation())));
313 build_opts.add_option_if(gemm_info.activation_info.enabled(),
314 "-DA_VAL=" + float_to_string_with_full_precision(gemm_info.activation_info.a()));
315 build_opts.add_option_if(gemm_info.activation_info.enabled(),
316 "-DB_VAL=" + float_to_string_with_full_precision(gemm_info.activation_info.b()));
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100317
318 std::string kernel_name("gemm_mm_native");
319
ramelg019cca5922021-11-11 10:05:00 +0000320 // A macro guard to compile ONLY the kernel of interest
321 build_opts.add_option("-D" + upper_string(kernel_name));
322
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100323 // Create kernel
324 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
325
326 // Set config_id for enabling LWS tuning
327 _config_id = kernel_name;
328 _config_id += "_";
329 _config_id += (_add_bias ? "add_bias_" : "");
330 _config_id += (gemm_info.broadcast_bias ? "broadcast_bias_" : "");
331 _config_id += (_reinterpret_input_as_3d ? "3di_" : "");
332 _config_id += (_reinterpret_output_as_3d ? "3do_" : "");
333 _config_id += (gemm_info.activation_info.enabled() ? "fused_activation_" : "");
334 _config_id += lower_string(string_from_data_type(src0->data_type()));
335 _config_id += "_";
336 _config_id += support::cpp11::to_string(dst->dimension(1));
337 _config_id += "_";
338 _config_id += support::cpp11::to_string(dst->dimension(0));
339 _config_id += "_";
340 _config_id += support::cpp11::to_string(gemm_info.k);
341 _config_id += "_";
342 _config_id += support::cpp11::to_string(dst->dimension(2));
343 _config_id += "_";
344 _config_id += support::cpp11::to_string(lhs_info.m0);
345 _config_id += "_";
346 _config_id += support::cpp11::to_string(rhs_info.n0);
347 _config_id += "_";
348 _config_id += support::cpp11::to_string(rhs_info.k0);
349
350 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
351}
352
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100353Status ClGemmMatrixMultiplyNativeKernel::validate(const ITensorInfo *src0,
354 const ITensorInfo *src1,
355 const ITensorInfo *src2,
356 const ITensorInfo *dst,
357 float alpha,
358 float beta,
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100359 const GEMMLHSMatrixInfo &lhs_info,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100360 const GEMMRHSMatrixInfo &rhs_info,
361 const GEMMKernelInfo &gemm_info)
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100362{
363 ElementsProcessed num_elements_processed{};
364 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src0, src1, src2, dst, alpha, beta, lhs_info, rhs_info, gemm_info));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100365 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(src0->clone().get(), src1->clone().get(),
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100366 src2 != nullptr ? src2->clone().get() : nullptr,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100367 dst->clone().get(), lhs_info, rhs_info, gemm_info,
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100368 num_elements_processed)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100369 .first);
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100370
371 return Status{};
372}
373
374void ClGemmMatrixMultiplyNativeKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
375{
376 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
377 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
378
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100379 const auto src0 =
380 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
381 const auto src1 =
382 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
383 const auto src2 =
384 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_2));
385 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100386
387 ARM_COMPUTE_ERROR_ON_NULLPTR(src0, src1, dst);
388 ARM_COMPUTE_ERROR_ON(_add_bias && src2 == nullptr);
389
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100390 if (src1->info()->num_dimensions() < 3)
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100391 {
392 // The stride_z for matrix B must be zero if we do not slice
393 ARM_COMPUTE_ERROR_ON(src1->info()->strides_in_bytes()[3] != 0);
394 }
395
396 Window slice = window.first_slice_window_3D();
397 Window slice_matrix_b = slice;
398
399 slice_matrix_b.set(Window::DimX, Window::Dimension(0, 1, 1));
400 slice_matrix_b.set(Window::DimY, Window::Dimension(0, 1, 1));
401
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100402 if (_reinterpret_input_as_3d)
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100403 {
404 // Pass bottom paddings to the kernel if the input has to be reinterpreted as 3D tensor
405 unsigned int idx0;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100406 if (_add_bias)
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100407 {
Jakub Sujak0d27b2e2023-08-24 14:01:20 +0100408 idx0 = 4 * num_arguments_per_2D_tensor() + 7;
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100409 }
410 else
411 {
Jakub Sujak0d27b2e2023-08-24 14:01:20 +0100412 idx0 = 3 * num_arguments_per_2D_tensor() + 6;
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100413 }
414 const unsigned int total_cross_plane_pad = src0->info()->padding().top + src0->info()->padding().bottom;
415 _kernel.setArg<cl_uint>(idx0, static_cast<unsigned int>(total_cross_plane_pad));
416 }
417
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100418 if (_reinterpret_output_as_3d)
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100419 {
420 // Pass bottom paddings to the kernel if the dst has to be reinterpreted as 3D tensor
421 unsigned int idx0;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100422 if (_add_bias)
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100423 {
Jakub Sujak0d27b2e2023-08-24 14:01:20 +0100424 idx0 = 4 * num_arguments_per_2D_tensor() + 7 + (_reinterpret_input_as_3d ? 1 : 0);
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100425 }
426 else
427 {
Jakub Sujak0d27b2e2023-08-24 14:01:20 +0100428 idx0 = 3 * num_arguments_per_2D_tensor() + 6 + (_reinterpret_input_as_3d ? 1 : 0);
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100429 }
430 const unsigned int total_cross_plane_pad = dst->info()->padding().top + dst->info()->padding().bottom;
431 _kernel.setArg<cl_uint>(idx0, static_cast<unsigned int>(total_cross_plane_pad));
432 }
433
434 do
435 {
436 Window slice_b = slice;
437 // Don't slice matrix B along the z dimension if matrix B has just 2 dimensions and matrix A more than 2
438 // This scenario can happen when the matrix multiplication is used to perform a convolution operation
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100439 if (!_slide_matrix_b)
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100440 {
441 slice_b = slice_matrix_b;
442 }
443
444 unsigned int idx = 0;
445 add_2D_tensor_argument(idx, src0, slice);
446 add_2D_tensor_argument(idx, src1, slice_b);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100447 if (_add_bias)
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100448 {
449 add_2D_tensor_argument(idx, src2, slice);
450 }
451 add_2D_tensor_argument(idx, dst, slice);
Jakub Sujak0d27b2e2023-08-24 14:01:20 +0100452
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100453 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(src0->info()->strides_in_bytes()[2]));
454 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(src1->info()->strides_in_bytes()[2]));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100455 if (_add_bias)
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100456 {
457 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(src2->info()->strides_in_bytes()[2]));
458 }
459 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(dst->info()->strides_in_bytes()[2]));
ramelg019cca5922021-11-11 10:05:00 +0000460
461 // Pass m, n and k at runtime
462 _kernel.setArg<cl_int>(idx++, _m);
463 _kernel.setArg<cl_int>(idx++, _n);
464 _kernel.setArg<cl_int>(idx++, _k);
465
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100466 enqueue(queue, *this, slice, lws_hint(), _use_dummy_work_items);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100467 } while (window.slide_window_slice_3D(slice));
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100468}
469} // namespace kernels
470} // namespace opencl
471} // namespace arm_compute