blob: 4ca4b83f9c86b32ef31c081799ae637b3459f36b [file] [log] [blame]
Gunes Bayir4bfc70e2021-12-10 16:17:56 +00001/*
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +00002 * Copyright (c) 2022-2023 Arm Limited.
Gunes Bayir4bfc70e2021-12-10 16:17:56 +00003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "src/gpu/cl/kernels/ClGemmMatrixMultiplyReshapedOnlyRhsMMULKernel.h"
25
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"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010032#include "arm_compute/core/utils/ActivationFunctionUtils.h"
33#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000034#include "arm_compute/core/utils/StringUtils.h"
Gunes Bayir4bfc70e2021-12-10 16:17:56 +000035#include "arm_compute/core/Validate.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010036
Gunes Bayir4bfc70e2021-12-10 16:17:56 +000037#include "src/core/CL/CLUtils.h"
38#include "src/core/helpers/AutoConfiguration.h"
39#include "src/core/helpers/WindowHelpers.h"
40#include "src/core/utils/helpers/float_ops.h"
41#include "src/gpu/cl/kernels/gemm/ClGemmHelpers.h"
42#include "support/Cast.h"
43#include "support/StringSupport.h"
Gunes Bayir4bfc70e2021-12-10 16:17:56 +000044namespace arm_compute
45{
46namespace opencl
47{
48namespace kernels
49{
50namespace
51{
52using ElementsProcessed = Steps;
53
54// Block size dimensions for the MMUL extension
55constexpr int mmul_m0 = 4;
56constexpr int mmul_n0 = 4;
57constexpr int mmul_k0 = 4;
58
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010059Status validate_arguments(const ITensorInfo *src0,
60 const ITensorInfo *src1,
61 const ITensorInfo *src2,
62 const ITensorInfo *dst,
63 float alpha,
64 float beta,
65 const GEMMLHSMatrixInfo &lhs_info,
Gunes Bayir4bfc70e2021-12-10 16:17:56 +000066 const GEMMRHSMatrixInfo &rhs_info,
67 const GEMMKernelInfo &gemm_info)
68{
69 ARM_COMPUTE_UNUSED(alpha);
70 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src0, src1, dst);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010071 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!arm_matrix_multiply_supported(CLKernelLibrary::get().get_device()),
72 "The extension cl_arm_matrix_multiply is not supported on the target platform");
Gunes Bayir4bfc70e2021-12-10 16:17:56 +000073 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src0, 1, DataType::F16, DataType::F32);
74 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src0, src1);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010075 ARM_COMPUTE_RETURN_ERROR_ON_MSG(src0->num_dimensions() > 4,
76 "The number of dimensions for the LHS matrix must be <= 4");
77 ARM_COMPUTE_RETURN_ERROR_ON_MSG(src1->num_dimensions() > 3,
78 "The number of dimensions for the RHS matrix must be <= 3");
Gunes Bayir4bfc70e2021-12-10 16:17:56 +000079 ARM_COMPUTE_RETURN_ERROR_ON_MSG(lhs_info.m0 < 1, "Only values greater than 0 are supported for m0");
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010080 ARM_COMPUTE_RETURN_ERROR_ON_MSG(rhs_info.n0 != 1 && rhs_info.n0 != 2 && rhs_info.n0 != 3 && rhs_info.n0 != 4 &&
81 rhs_info.n0 != 8 && rhs_info.n0 != 16,
82 "Only 1,2,3,4,8, and 16 are supported for n0");
Gunes Bayir4bfc70e2021-12-10 16:17:56 +000083 ARM_COMPUTE_RETURN_ERROR_ON_MSG((rhs_info.k0 != 1 || lhs_info.k0 != 1), "Only 1 is supported for k0");
84 ARM_COMPUTE_RETURN_ERROR_ON_MSG((rhs_info.h0 != 4), "Only 4 is supported for h0");
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010085 ARM_COMPUTE_RETURN_ERROR_ON_MSG(rhs_info.interleave != true,
86 "Only true is supported for interleave with mmul extension enabled");
87 ARM_COMPUTE_RETURN_ERROR_ON_MSG(rhs_info.transpose != false,
88 "Only false is supported for transpose with mmul extension enabled");
Gunes Bayir4bfc70e2021-12-10 16:17:56 +000089 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.fp_mixed_precision, "Mixed precision not supported");
90 ARM_COMPUTE_RETURN_ON_ERROR(gemm::validate_image2d_support_on_rhs(*src1, rhs_info));
91
92 const unsigned int m = gemm_info.m;
93 const unsigned int n = gemm_info.n;
94 const unsigned int k = gemm_info.k;
95
96 ARM_COMPUTE_UNUSED(m);
97 ARM_COMPUTE_UNUSED(n);
98 ARM_COMPUTE_UNUSED(k);
99
100 ARM_COMPUTE_RETURN_ERROR_ON(src0->dimension(0) != k);
Gunes Bayirec0a0572023-12-13 09:38:07 +0000101
102 // Validate the reinterpreted-as-3D-case
Gunes Bayirc310c112023-12-22 15:43:29 +0000103 if (gemm_info.reinterpret_input_as_3d)
Gunes Bayirec0a0572023-12-13 09:38:07 +0000104 {
105 ARM_COMPUTE_RETURN_ERROR_ON(src0->dimension(1) * src0->dimension(2) != m);
106 }
107 else
108 {
109 ARM_COMPUTE_RETURN_ERROR_ON(src0->dimension(1) != m);
110 }
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000111
112 // Validate the gemm-batched case
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100113 if (src1->num_dimensions() > 2)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000114 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100115 if (gemm_info.depth_output_gemm3d != 0)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000116 {
117 ARM_COMPUTE_RETURN_ERROR_ON(src0->dimension(3) != src1->dimension(2));
118 }
119 else
120 {
121 ARM_COMPUTE_RETURN_ERROR_ON(src0->dimension(2) != src1->dimension(2));
122 }
123 }
124
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100125 if (src2 != nullptr && !(helpers::float_ops::is_zero(beta)))
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000126 {
127 const unsigned int src2_dim0 = src2->dimension(0);
128 const unsigned int src2_dim1 = src2->dimension(1);
129
130 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src2, src1);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100131 if (gemm_info.broadcast_bias)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000132 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100133 ARM_COMPUTE_RETURN_ERROR_ON_MSG((src2_dim1 != 1 || src2_dim0 != n),
134 "Incorrect dimension of bias matrix which is to be broadcasted");
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000135 }
136 else
137 {
138 ARM_COMPUTE_RETURN_ERROR_ON_MSG((src2_dim0 != n || src2_dim1 != m), "Incorrect dimension of bias matrix");
139 }
140 }
141
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100142 TensorShape tensor_shape1{src1->tensor_shape()};
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000143 tensor_shape1.set(0, n);
144 tensor_shape1.set(1, k);
145
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100146 const TensorInfo tensor_info1 = src1->clone()->set_tensor_shape(tensor_shape1);
147 const TensorInfo tensor_info_reshaped1 =
148 src1->clone()->set_tensor_shape(misc::shape_calculator::compute_rhs_reshaped_shape(tensor_info1, rhs_info));
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000149
150 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(src1, &tensor_info_reshaped1);
151
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100152 if (dst->total_size() != 0)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000153 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100154 const TensorInfo tensor_info_dst =
155 dst->clone()->set_tensor_shape(misc::shape_calculator::compute_mm_shape(*src0, *src1, gemm_info));
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000156 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(dst, &tensor_info_dst);
157 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src0, dst);
158 }
159
160 return Status{};
161}
162
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100163std::pair<Status, Window> validate_and_configure_window(ITensorInfo *src0,
164 ITensorInfo *src1,
165 ITensorInfo *src2,
166 ITensorInfo *dst,
167 const GEMMLHSMatrixInfo &lhs_info,
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000168 const GEMMRHSMatrixInfo &rhs_info,
169 const GEMMKernelInfo &gemm_info)
170{
171 ARM_COMPUTE_UNUSED(src0, src1, src2);
172 bool reinterpret_output_as_3d = gemm_info.depth_output_gemm3d != 0;
173
174 // dst tensor auto initialization if not yet initialized
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100175 auto_init_if_empty(
176 *dst, src0->clone()->set_tensor_shape(misc::shape_calculator::compute_mm_shape(*src0, *src1, gemm_info)));
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000177
178 TensorInfo tmp_info(*dst);
179
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100180 if (reinterpret_output_as_3d)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000181 {
182 // Since the dst tensor has to be reinterpreted as 3D and the execute window is based on a 2D GEMM,
183 // the window needs to be constructed on the 2D collapsed version of the tensor
184 TensorShape tmp_shape(dst->tensor_shape());
185 tmp_shape.collapse(2U, 1U);
186 tmp_info.set_tensor_shape(tmp_shape);
187 }
188
189 Window win = calculate_max_window(tmp_info, Steps(1, 1));
190
191 // Collapse along the Z direction
192 // This collapse needs to be here in order to tune the Z dimension of LWS
193 const unsigned int dimension_to_collapse = std::min(static_cast<unsigned int>(dst->num_dimensions()), 2u);
194 Window collapsed = win.collapse(win, dimension_to_collapse);
195
196 // Reconfigure window size, one arm_matrix_multiply kernel needs 16 threads to finish.
197 Window::Dimension x_dimension = collapsed.x();
198 Window::Dimension y_dimension = collapsed.y();
199
200 // Make M and N multiple of M0 and N0 respectively
201 const unsigned int ceil_to_multiple_n_n0 = ceil_to_multiple(x_dimension.end(), rhs_info.n0);
202 const unsigned int ceil_to_multiple_m_m0 = ceil_to_multiple(y_dimension.end(), lhs_info.m0);
203
204 // Divide M and N by M0 and N0 respectively
205 const unsigned int n_div_n0 = ceil_to_multiple_n_n0 / rhs_info.n0;
206 const unsigned int m_div_m0 = ceil_to_multiple_m_m0 / lhs_info.m0;
207
208 // Make n_div_n0 and m_div_m0 multiple of mmul_n0 and mmul_k0 respectively
209 const unsigned int ceil_to_multiple_n_div_n0_mmul_n0 = ceil_to_multiple(n_div_n0, mmul_n0);
210 const unsigned int ceil_to_multiple_m_div_m0_mmul_k0 = ceil_to_multiple(m_div_m0, mmul_k0);
211
212 // Ensure x_dimension is multiple of MMUL block size (mmul_n0 * mmul_k0)
213 x_dimension.set_end(ceil_to_multiple_n_div_n0_mmul_n0 * mmul_k0);
214 y_dimension.set_end(ceil_to_multiple_m_div_m0_mmul_k0 / mmul_k0);
215
216 collapsed.set(Window::DimX, x_dimension);
217 collapsed.set(Window::DimY, y_dimension);
218
219 return std::make_pair(Status{}, collapsed);
220}
221} // namespace
222
223ClGemmMatrixMultiplyReshapedOnlyRhsMMULKernel::ClGemmMatrixMultiplyReshapedOnlyRhsMMULKernel()
224{
225 _type = CLKernelType::GEMM;
226}
227
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100228void ClGemmMatrixMultiplyReshapedOnlyRhsMMULKernel::configure(const CLCompileContext &compile_context,
229 ITensorInfo *src0,
230 ITensorInfo *src1,
231 ITensorInfo *src2,
232 ITensorInfo *dst,
233 float alpha,
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000234 float beta,
235 const GEMMLHSMatrixInfo &lhs_info,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100236 const GEMMRHSMatrixInfo &rhs_info,
237 const GEMMKernelInfo &gemm_info)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000238{
239 ARM_COMPUTE_ERROR_ON_NULLPTR(src0, src1, dst);
240
241 // dst tensor auto initialization if not yet initialized
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100242 auto_init_if_empty(
243 *dst, src0->clone()->set_tensor_shape(misc::shape_calculator::compute_mm_shape(*src0, *src1, gemm_info)));
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000244
245 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src0, src1, src2, dst, alpha, beta, lhs_info, rhs_info, gemm_info));
246
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100247 auto padding_info = get_padding_info({src0, src1, src2, dst});
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000248 _add_bias = src2 != nullptr;
249 _export_to_cl_image = rhs_info.export_to_cl_image;
250
251 // Configure kernel window
252 auto win_config = validate_and_configure_window(src0, src1, src2, dst, lhs_info, rhs_info, gemm_info);
253 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
254
255 IClKernel::configure_internal(win_config.second);
256
257 _m = gemm_info.m;
258 _n = gemm_info.n;
259 _k = gemm_info.k;
260
261 const unsigned int m0_leftover = _m % lhs_info.m0;
262 const unsigned int n0_leftover = _n % rhs_info.n0;
263
264 // Create build options
265 CLBuildOptions build_opts;
266 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(src0->data_type()));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100267 build_opts.add_option_if(!(helpers::float_ops::is_one(alpha)),
268 "-DALPHA=" + float_to_string_with_full_precision(alpha));
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000269 build_opts.add_option_if(src2 != nullptr, "-DBETA=" + float_to_string_with_full_precision(beta));
270 build_opts.add_option_if(helpers::float_ops::is_one(beta), "-DUNIT_BETA");
271 build_opts.add_option_if(gemm_info.broadcast_bias, "-DBROADCAST_BIAS");
272 build_opts.add_option_if(src0->data_type() == DataType::F16, "-DHALF_PRECISION");
273 build_opts.add_option("-DM0=" + support::cpp11::to_string(lhs_info.m0));
274 build_opts.add_option("-DN0=" + support::cpp11::to_string(rhs_info.n0));
275 build_opts.add_option("-DK0=" + support::cpp11::to_string(rhs_info.k0));
276 build_opts.add_option("-DM0_LEFTOVER=" + support::cpp11::to_string(m0_leftover));
277 build_opts.add_option("-DN0_LEFTOVER=" + support::cpp11::to_string(n0_leftover));
278 build_opts.add_option("-DMMUL_M0=" + support::cpp11::to_string(mmul_m0));
279 build_opts.add_option("-DMMUL_N0=" + support::cpp11::to_string(mmul_n0));
280 build_opts.add_option("-DMMUL_K0=" + support::cpp11::to_string(mmul_k0));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100281 build_opts.add_option("-DACTIVATION_TYPE=" +
282 lower_string(string_from_activation_func(gemm_info.activation_info.activation())));
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000283 build_opts.add_option("-DA_VAL=" + float_to_string_with_full_precision(gemm_info.activation_info.a()));
284 build_opts.add_option("-DB_VAL=" + float_to_string_with_full_precision(gemm_info.activation_info.b()));
285
Gunes Bayirc310c112023-12-22 15:43:29 +0000286 build_opts.add_option_if(gemm_info.reinterpret_input_as_3d, "-DREINTERPRET_INPUT_AS_3D");
287 build_opts.add_option_if(gemm_info.depth_output_gemm3d != 0, "-DREINTERPRET_OUTPUT_AS_3D");
288 build_opts.add_option_if(src1->num_dimensions() > 2, "-DBATCHED_RHS");
289
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000290 std::string kernel_name("gemm_mm_reshaped_only_rhs_nt_mmul");
Gunes Bayirc310c112023-12-22 15:43:29 +0000291 kernel_name += _export_to_cl_image ? "_texture" : "";
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000292
293 // A macro guard to compile ONLY the kernel of interest
294 build_opts.add_option("-D" + upper_string(kernel_name));
295
296 // Create kernel
297 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
298
299 // Set config_id for enabling LWS tuning
300 _config_id = kernel_name;
301 _config_id += "_";
302 _config_id += (_add_bias ? "add_bias_" : "");
303 _config_id += (gemm_info.broadcast_bias ? "broadcast_bias_" : "");
304 _config_id += (gemm_info.activation_info.enabled() ? "fused_activation_" : "");
305 _config_id += lower_string(string_from_data_type(src0->data_type()));
306 _config_id += "_";
307 _config_id += support::cpp11::to_string(_m);
308 _config_id += "_";
309 _config_id += support::cpp11::to_string(_n);
310 _config_id += "_";
311 _config_id += support::cpp11::to_string(_k);
312 _config_id += "_";
313 _config_id += support::cpp11::to_string(lhs_info.m0);
314 _config_id += "_";
315 _config_id += support::cpp11::to_string(rhs_info.n0);
316
317 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
318}
319
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100320Status ClGemmMatrixMultiplyReshapedOnlyRhsMMULKernel::validate(const ITensorInfo *src0,
321 const ITensorInfo *src1,
322 const ITensorInfo *src2,
323 const ITensorInfo *dst,
324 float alpha,
325 float beta,
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000326 const GEMMLHSMatrixInfo &lhs_info,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100327 const GEMMRHSMatrixInfo &rhs_info,
328 const GEMMKernelInfo &gemm_info)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000329{
330 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 +0100331 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(src0->clone().get(), src1->clone().get(),
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000332 src2 != nullptr ? src2->clone().get() : nullptr,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100333 dst->clone().get(), lhs_info, rhs_info, gemm_info)
334 .first);
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000335
336 return Status{};
337}
338
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100339void ClGemmMatrixMultiplyReshapedOnlyRhsMMULKernel::run_op(ITensorPack &tensors,
340 const Window &window,
341 cl::CommandQueue &queue)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000342{
343 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
344 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
345
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100346 const auto src0 =
347 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
348 const auto src1 =
349 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
350 const auto src2 =
351 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_2));
352 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000353
354 ARM_COMPUTE_ERROR_ON_NULLPTR(src0, src1, dst);
355 ARM_COMPUTE_ERROR_ON(_add_bias && src2 == nullptr);
356
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100357 if (src1->info()->num_dimensions() < 3)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000358 {
359 // The stride_z for matrix B must be zero if we do not slice
360 ARM_COMPUTE_ERROR_ON(src1->info()->strides_in_bytes()[3] != 0);
361 }
362
363 cl::Image2D src1_image2d;
364
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100365 if (_export_to_cl_image)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000366 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100367 const TensorShape shape2d(src1->info()->dimension(0) / 4,
368 src1->info()->dimension(1) * src1->info()->dimension(2));
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000369 const size_t image_row_pitch = src1->info()->strides_in_bytes()[1];
370
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100371 src1_image2d = create_image2d_from_buffer(CLKernelLibrary::get().context(), src1->cl_buffer(), shape2d,
372 src1->info()->data_type(), image_row_pitch, CLImage2DType::ReadOnly);
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000373 }
374
375 Window slice = window.first_slice_window_3D();
376
377 do
378 {
379 unsigned int idx = 0;
380
381 add_3d_tensor_nhw_argument(idx, src0);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100382 if (_export_to_cl_image)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000383 {
384 _kernel.setArg(idx++, src1_image2d);
385 }
386 add_3d_tensor_nhw_argument(idx, src1);
387
388 // Bias buffer (_add_bias == true)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100389 if (_add_bias)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000390 {
391 add_3d_tensor_nhw_argument(idx, src2);
392 }
393 // dst buffer
394 add_3d_tensor_nhw_argument(idx, dst);
395
396 // Pass m, n and k at runtime as signed ints, to ensure results of any subtractions they could be operand in, would still be signed.
397 _kernel.setArg<cl_int>(idx++, _m);
398 _kernel.setArg<cl_int>(idx++, _n);
399 _kernel.setArg<cl_int>(idx++, _k);
400
401 // LWS_x should be multiple of 16 at least. (32, 2) has been chosen to have more work-items on a single core
402 // LWS also enforces the order of execution of the workitems which improves cache utilization
403 enqueue(queue, *this, slice, cl::NDRange(32, 2), false);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100404 } while (window.slide_window_slice_3D(slice));
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000405}
406} // namespace kernels
407} // namespace opencl
408} // namespace arm_compute