blob: 9a2a4890f3ed93ba0eccecc20a7d32ace1b9acf3 [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"
44
45namespace arm_compute
46{
47namespace opencl
48{
49namespace kernels
50{
51namespace
52{
53using ElementsProcessed = Steps;
54
55// Block size dimensions for the MMUL extension
56constexpr int mmul_m0 = 4;
57constexpr int mmul_n0 = 4;
58constexpr int mmul_k0 = 4;
59
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010060Status validate_arguments(const ITensorInfo *src0,
61 const ITensorInfo *src1,
62 const ITensorInfo *src2,
63 const ITensorInfo *dst,
64 float alpha,
65 float beta,
66 const GEMMLHSMatrixInfo &lhs_info,
Gunes Bayir4bfc70e2021-12-10 16:17:56 +000067 const GEMMRHSMatrixInfo &rhs_info,
68 const GEMMKernelInfo &gemm_info)
69{
70 ARM_COMPUTE_UNUSED(alpha);
71 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src0, src1, dst);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010072 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!arm_matrix_multiply_supported(CLKernelLibrary::get().get_device()),
73 "The extension cl_arm_matrix_multiply is not supported on the target platform");
Gunes Bayir4bfc70e2021-12-10 16:17:56 +000074 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src0, 1, DataType::F16, DataType::F32);
75 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src0, src1);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010076 ARM_COMPUTE_RETURN_ERROR_ON_MSG(src0->num_dimensions() > 4,
77 "The number of dimensions for the LHS matrix must be <= 4");
78 ARM_COMPUTE_RETURN_ERROR_ON_MSG(src1->num_dimensions() > 3,
79 "The number of dimensions for the RHS matrix must be <= 3");
Gunes Bayir4bfc70e2021-12-10 16:17:56 +000080 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 +010081 ARM_COMPUTE_RETURN_ERROR_ON_MSG(rhs_info.n0 != 1 && rhs_info.n0 != 2 && rhs_info.n0 != 3 && rhs_info.n0 != 4 &&
82 rhs_info.n0 != 8 && rhs_info.n0 != 16,
83 "Only 1,2,3,4,8, and 16 are supported for n0");
Gunes Bayir4bfc70e2021-12-10 16:17:56 +000084 ARM_COMPUTE_RETURN_ERROR_ON_MSG((rhs_info.k0 != 1 || lhs_info.k0 != 1), "Only 1 is supported for k0");
85 ARM_COMPUTE_RETURN_ERROR_ON_MSG((rhs_info.h0 != 4), "Only 4 is supported for h0");
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010086 ARM_COMPUTE_RETURN_ERROR_ON_MSG(rhs_info.interleave != true,
87 "Only true is supported for interleave with mmul extension enabled");
88 ARM_COMPUTE_RETURN_ERROR_ON_MSG(rhs_info.transpose != false,
89 "Only false is supported for transpose with mmul extension enabled");
Gunes Bayir4bfc70e2021-12-10 16:17:56 +000090 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.fp_mixed_precision, "Mixed precision not supported");
91 ARM_COMPUTE_RETURN_ON_ERROR(gemm::validate_image2d_support_on_rhs(*src1, rhs_info));
92
93 const unsigned int m = gemm_info.m;
94 const unsigned int n = gemm_info.n;
95 const unsigned int k = gemm_info.k;
96
97 ARM_COMPUTE_UNUSED(m);
98 ARM_COMPUTE_UNUSED(n);
99 ARM_COMPUTE_UNUSED(k);
100
101 ARM_COMPUTE_RETURN_ERROR_ON(src0->dimension(0) != k);
102
103 // Validate the reinterpreted-as-3D-case
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100104 if (gemm_info.depth_output_gemm3d != 0)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000105 {
106 ARM_COMPUTE_RETURN_ERROR_ON(src0->dimension(1) * src0->dimension(2) != m);
107 }
108 else
109 {
110 ARM_COMPUTE_RETURN_ERROR_ON(src0->dimension(1) != m);
111 }
112
113 // Validate the gemm-batched case
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100114 if (src1->num_dimensions() > 2)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000115 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100116 if (gemm_info.depth_output_gemm3d != 0)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000117 {
118 ARM_COMPUTE_RETURN_ERROR_ON(src0->dimension(3) != src1->dimension(2));
119 }
120 else
121 {
122 ARM_COMPUTE_RETURN_ERROR_ON(src0->dimension(2) != src1->dimension(2));
123 }
124 }
125
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100126 if (src2 != nullptr && !(helpers::float_ops::is_zero(beta)))
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000127 {
128 const unsigned int src2_dim0 = src2->dimension(0);
129 const unsigned int src2_dim1 = src2->dimension(1);
130
131 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src2, src1);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100132 if (gemm_info.broadcast_bias)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000133 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100134 ARM_COMPUTE_RETURN_ERROR_ON_MSG((src2_dim1 != 1 || src2_dim0 != n),
135 "Incorrect dimension of bias matrix which is to be broadcasted");
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000136 }
137 else
138 {
139 ARM_COMPUTE_RETURN_ERROR_ON_MSG((src2_dim0 != n || src2_dim1 != m), "Incorrect dimension of bias matrix");
140 }
141 }
142
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100143 TensorShape tensor_shape1{src1->tensor_shape()};
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000144 tensor_shape1.set(0, n);
145 tensor_shape1.set(1, k);
146
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100147 const TensorInfo tensor_info1 = src1->clone()->set_tensor_shape(tensor_shape1);
148 const TensorInfo tensor_info_reshaped1 =
149 src1->clone()->set_tensor_shape(misc::shape_calculator::compute_rhs_reshaped_shape(tensor_info1, rhs_info));
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000150
151 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(src1, &tensor_info_reshaped1);
152
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100153 if (dst->total_size() != 0)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000154 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100155 const TensorInfo tensor_info_dst =
156 dst->clone()->set_tensor_shape(misc::shape_calculator::compute_mm_shape(*src0, *src1, gemm_info));
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000157 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(dst, &tensor_info_dst);
158 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src0, dst);
159 }
160
161 return Status{};
162}
163
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100164std::pair<Status, Window> validate_and_configure_window(ITensorInfo *src0,
165 ITensorInfo *src1,
166 ITensorInfo *src2,
167 ITensorInfo *dst,
168 const GEMMLHSMatrixInfo &lhs_info,
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000169 const GEMMRHSMatrixInfo &rhs_info,
170 const GEMMKernelInfo &gemm_info)
171{
172 ARM_COMPUTE_UNUSED(src0, src1, src2);
173 bool reinterpret_output_as_3d = gemm_info.depth_output_gemm3d != 0;
174
175 // dst tensor auto initialization if not yet initialized
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100176 auto_init_if_empty(
177 *dst, src0->clone()->set_tensor_shape(misc::shape_calculator::compute_mm_shape(*src0, *src1, gemm_info)));
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000178
179 TensorInfo tmp_info(*dst);
180
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100181 if (reinterpret_output_as_3d)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000182 {
183 // Since the dst tensor has to be reinterpreted as 3D and the execute window is based on a 2D GEMM,
184 // the window needs to be constructed on the 2D collapsed version of the tensor
185 TensorShape tmp_shape(dst->tensor_shape());
186 tmp_shape.collapse(2U, 1U);
187 tmp_info.set_tensor_shape(tmp_shape);
188 }
189
190 Window win = calculate_max_window(tmp_info, Steps(1, 1));
191
192 // Collapse along the Z direction
193 // This collapse needs to be here in order to tune the Z dimension of LWS
194 const unsigned int dimension_to_collapse = std::min(static_cast<unsigned int>(dst->num_dimensions()), 2u);
195 Window collapsed = win.collapse(win, dimension_to_collapse);
196
197 // Reconfigure window size, one arm_matrix_multiply kernel needs 16 threads to finish.
198 Window::Dimension x_dimension = collapsed.x();
199 Window::Dimension y_dimension = collapsed.y();
200
201 // Make M and N multiple of M0 and N0 respectively
202 const unsigned int ceil_to_multiple_n_n0 = ceil_to_multiple(x_dimension.end(), rhs_info.n0);
203 const unsigned int ceil_to_multiple_m_m0 = ceil_to_multiple(y_dimension.end(), lhs_info.m0);
204
205 // Divide M and N by M0 and N0 respectively
206 const unsigned int n_div_n0 = ceil_to_multiple_n_n0 / rhs_info.n0;
207 const unsigned int m_div_m0 = ceil_to_multiple_m_m0 / lhs_info.m0;
208
209 // Make n_div_n0 and m_div_m0 multiple of mmul_n0 and mmul_k0 respectively
210 const unsigned int ceil_to_multiple_n_div_n0_mmul_n0 = ceil_to_multiple(n_div_n0, mmul_n0);
211 const unsigned int ceil_to_multiple_m_div_m0_mmul_k0 = ceil_to_multiple(m_div_m0, mmul_k0);
212
213 // Ensure x_dimension is multiple of MMUL block size (mmul_n0 * mmul_k0)
214 x_dimension.set_end(ceil_to_multiple_n_div_n0_mmul_n0 * mmul_k0);
215 y_dimension.set_end(ceil_to_multiple_m_div_m0_mmul_k0 / mmul_k0);
216
217 collapsed.set(Window::DimX, x_dimension);
218 collapsed.set(Window::DimY, y_dimension);
219
220 return std::make_pair(Status{}, collapsed);
221}
222} // namespace
223
224ClGemmMatrixMultiplyReshapedOnlyRhsMMULKernel::ClGemmMatrixMultiplyReshapedOnlyRhsMMULKernel()
225{
226 _type = CLKernelType::GEMM;
227}
228
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100229void ClGemmMatrixMultiplyReshapedOnlyRhsMMULKernel::configure(const CLCompileContext &compile_context,
230 ITensorInfo *src0,
231 ITensorInfo *src1,
232 ITensorInfo *src2,
233 ITensorInfo *dst,
234 float alpha,
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000235 float beta,
236 const GEMMLHSMatrixInfo &lhs_info,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100237 const GEMMRHSMatrixInfo &rhs_info,
238 const GEMMKernelInfo &gemm_info)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000239{
240 ARM_COMPUTE_ERROR_ON_NULLPTR(src0, src1, dst);
241
242 // dst tensor auto initialization if not yet initialized
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100243 auto_init_if_empty(
244 *dst, src0->clone()->set_tensor_shape(misc::shape_calculator::compute_mm_shape(*src0, *src1, gemm_info)));
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000245
246 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src0, src1, src2, dst, alpha, beta, lhs_info, rhs_info, gemm_info));
247
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100248 auto padding_info = get_padding_info({src0, src1, src2, dst});
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000249 _add_bias = src2 != nullptr;
250 _export_to_cl_image = rhs_info.export_to_cl_image;
251
252 // Configure kernel window
253 auto win_config = validate_and_configure_window(src0, src1, src2, dst, lhs_info, rhs_info, gemm_info);
254 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
255
256 IClKernel::configure_internal(win_config.second);
257
258 _m = gemm_info.m;
259 _n = gemm_info.n;
260 _k = gemm_info.k;
261
262 const unsigned int m0_leftover = _m % lhs_info.m0;
263 const unsigned int n0_leftover = _n % rhs_info.n0;
264
265 // Create build options
266 CLBuildOptions build_opts;
267 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(src0->data_type()));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100268 build_opts.add_option_if(!(helpers::float_ops::is_one(alpha)),
269 "-DALPHA=" + float_to_string_with_full_precision(alpha));
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000270 build_opts.add_option_if(src2 != nullptr, "-DBETA=" + float_to_string_with_full_precision(beta));
271 build_opts.add_option_if(helpers::float_ops::is_one(beta), "-DUNIT_BETA");
272 build_opts.add_option_if(gemm_info.broadcast_bias, "-DBROADCAST_BIAS");
273 build_opts.add_option_if(src0->data_type() == DataType::F16, "-DHALF_PRECISION");
274 build_opts.add_option("-DM0=" + support::cpp11::to_string(lhs_info.m0));
275 build_opts.add_option("-DN0=" + support::cpp11::to_string(rhs_info.n0));
276 build_opts.add_option("-DK0=" + support::cpp11::to_string(rhs_info.k0));
277 build_opts.add_option("-DM0_LEFTOVER=" + support::cpp11::to_string(m0_leftover));
278 build_opts.add_option("-DN0_LEFTOVER=" + support::cpp11::to_string(n0_leftover));
279 build_opts.add_option("-DMMUL_M0=" + support::cpp11::to_string(mmul_m0));
280 build_opts.add_option("-DMMUL_N0=" + support::cpp11::to_string(mmul_n0));
281 build_opts.add_option("-DMMUL_K0=" + support::cpp11::to_string(mmul_k0));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100282 build_opts.add_option("-DACTIVATION_TYPE=" +
283 lower_string(string_from_activation_func(gemm_info.activation_info.activation())));
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000284 build_opts.add_option("-DA_VAL=" + float_to_string_with_full_precision(gemm_info.activation_info.a()));
285 build_opts.add_option("-DB_VAL=" + float_to_string_with_full_precision(gemm_info.activation_info.b()));
286
287 std::string kernel_name("gemm_mm_reshaped_only_rhs_nt_mmul");
288 kernel_name += rhs_info.export_to_cl_image ? "_texture" : "";
289
290 // A macro guard to compile ONLY the kernel of interest
291 build_opts.add_option("-D" + upper_string(kernel_name));
292
293 // Create kernel
294 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
295
296 // Set config_id for enabling LWS tuning
297 _config_id = kernel_name;
298 _config_id += "_";
299 _config_id += (_add_bias ? "add_bias_" : "");
300 _config_id += (gemm_info.broadcast_bias ? "broadcast_bias_" : "");
301 _config_id += (gemm_info.activation_info.enabled() ? "fused_activation_" : "");
302 _config_id += lower_string(string_from_data_type(src0->data_type()));
303 _config_id += "_";
304 _config_id += support::cpp11::to_string(_m);
305 _config_id += "_";
306 _config_id += support::cpp11::to_string(_n);
307 _config_id += "_";
308 _config_id += support::cpp11::to_string(_k);
309 _config_id += "_";
310 _config_id += support::cpp11::to_string(lhs_info.m0);
311 _config_id += "_";
312 _config_id += support::cpp11::to_string(rhs_info.n0);
313
314 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
315}
316
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100317Status ClGemmMatrixMultiplyReshapedOnlyRhsMMULKernel::validate(const ITensorInfo *src0,
318 const ITensorInfo *src1,
319 const ITensorInfo *src2,
320 const ITensorInfo *dst,
321 float alpha,
322 float beta,
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000323 const GEMMLHSMatrixInfo &lhs_info,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100324 const GEMMRHSMatrixInfo &rhs_info,
325 const GEMMKernelInfo &gemm_info)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000326{
327 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 +0100328 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(src0->clone().get(), src1->clone().get(),
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000329 src2 != nullptr ? src2->clone().get() : nullptr,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100330 dst->clone().get(), lhs_info, rhs_info, gemm_info)
331 .first);
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000332
333 return Status{};
334}
335
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100336void ClGemmMatrixMultiplyReshapedOnlyRhsMMULKernel::run_op(ITensorPack &tensors,
337 const Window &window,
338 cl::CommandQueue &queue)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000339{
340 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
341 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
342
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100343 const auto src0 =
344 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
345 const auto src1 =
346 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
347 const auto src2 =
348 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_2));
349 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000350
351 ARM_COMPUTE_ERROR_ON_NULLPTR(src0, src1, dst);
352 ARM_COMPUTE_ERROR_ON(_add_bias && src2 == nullptr);
353
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100354 if (src1->info()->num_dimensions() < 3)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000355 {
356 // The stride_z for matrix B must be zero if we do not slice
357 ARM_COMPUTE_ERROR_ON(src1->info()->strides_in_bytes()[3] != 0);
358 }
359
360 cl::Image2D src1_image2d;
361
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100362 if (_export_to_cl_image)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000363 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100364 const TensorShape shape2d(src1->info()->dimension(0) / 4,
365 src1->info()->dimension(1) * src1->info()->dimension(2));
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000366 const size_t image_row_pitch = src1->info()->strides_in_bytes()[1];
367
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100368 src1_image2d = create_image2d_from_buffer(CLKernelLibrary::get().context(), src1->cl_buffer(), shape2d,
369 src1->info()->data_type(), image_row_pitch, CLImage2DType::ReadOnly);
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000370 }
371
372 Window slice = window.first_slice_window_3D();
373
374 do
375 {
376 unsigned int idx = 0;
377
378 add_3d_tensor_nhw_argument(idx, src0);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100379 if (_export_to_cl_image)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000380 {
381 _kernel.setArg(idx++, src1_image2d);
382 }
383 add_3d_tensor_nhw_argument(idx, src1);
384
385 // Bias buffer (_add_bias == true)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100386 if (_add_bias)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000387 {
388 add_3d_tensor_nhw_argument(idx, src2);
389 }
390 // dst buffer
391 add_3d_tensor_nhw_argument(idx, dst);
392
393 // 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.
394 _kernel.setArg<cl_int>(idx++, _m);
395 _kernel.setArg<cl_int>(idx++, _n);
396 _kernel.setArg<cl_int>(idx++, _k);
397
398 // LWS_x should be multiple of 16 at least. (32, 2) has been chosen to have more work-items on a single core
399 // LWS also enforces the order of execution of the workitems which improves cache utilization
400 enqueue(queue, *this, slice, cl::NDRange(32, 2), false);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100401 } while (window.slide_window_slice_3D(slice));
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000402}
403} // namespace kernels
404} // namespace opencl
405} // namespace arm_compute