blob: d67c595449900819eac27aedec79cc77397f0d66 [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);
Gunes Bayir270576a2023-12-13 09:38:07 +0000102 ARM_COMPUTE_RETURN_ERROR_ON(src0->dimension(1) != m);
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000103
104 // Validate the gemm-batched case
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100105 if (src1->num_dimensions() > 2)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000106 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100107 if (gemm_info.depth_output_gemm3d != 0)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000108 {
109 ARM_COMPUTE_RETURN_ERROR_ON(src0->dimension(3) != src1->dimension(2));
110 }
111 else
112 {
113 ARM_COMPUTE_RETURN_ERROR_ON(src0->dimension(2) != src1->dimension(2));
114 }
115 }
116
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100117 if (src2 != nullptr && !(helpers::float_ops::is_zero(beta)))
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000118 {
119 const unsigned int src2_dim0 = src2->dimension(0);
120 const unsigned int src2_dim1 = src2->dimension(1);
121
122 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src2, src1);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100123 if (gemm_info.broadcast_bias)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000124 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100125 ARM_COMPUTE_RETURN_ERROR_ON_MSG((src2_dim1 != 1 || src2_dim0 != n),
126 "Incorrect dimension of bias matrix which is to be broadcasted");
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000127 }
128 else
129 {
130 ARM_COMPUTE_RETURN_ERROR_ON_MSG((src2_dim0 != n || src2_dim1 != m), "Incorrect dimension of bias matrix");
131 }
132 }
133
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100134 TensorShape tensor_shape1{src1->tensor_shape()};
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000135 tensor_shape1.set(0, n);
136 tensor_shape1.set(1, k);
137
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100138 const TensorInfo tensor_info1 = src1->clone()->set_tensor_shape(tensor_shape1);
139 const TensorInfo tensor_info_reshaped1 =
140 src1->clone()->set_tensor_shape(misc::shape_calculator::compute_rhs_reshaped_shape(tensor_info1, rhs_info));
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000141
142 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(src1, &tensor_info_reshaped1);
143
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100144 if (dst->total_size() != 0)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000145 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100146 const TensorInfo tensor_info_dst =
147 dst->clone()->set_tensor_shape(misc::shape_calculator::compute_mm_shape(*src0, *src1, gemm_info));
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000148 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(dst, &tensor_info_dst);
149 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src0, dst);
150 }
151
152 return Status{};
153}
154
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100155std::pair<Status, Window> validate_and_configure_window(ITensorInfo *src0,
156 ITensorInfo *src1,
157 ITensorInfo *src2,
158 ITensorInfo *dst,
159 const GEMMLHSMatrixInfo &lhs_info,
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000160 const GEMMRHSMatrixInfo &rhs_info,
161 const GEMMKernelInfo &gemm_info)
162{
163 ARM_COMPUTE_UNUSED(src0, src1, src2);
164 bool reinterpret_output_as_3d = gemm_info.depth_output_gemm3d != 0;
165
166 // dst tensor auto initialization if not yet initialized
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100167 auto_init_if_empty(
168 *dst, src0->clone()->set_tensor_shape(misc::shape_calculator::compute_mm_shape(*src0, *src1, gemm_info)));
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000169
170 TensorInfo tmp_info(*dst);
171
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100172 if (reinterpret_output_as_3d)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000173 {
174 // Since the dst tensor has to be reinterpreted as 3D and the execute window is based on a 2D GEMM,
175 // the window needs to be constructed on the 2D collapsed version of the tensor
176 TensorShape tmp_shape(dst->tensor_shape());
177 tmp_shape.collapse(2U, 1U);
178 tmp_info.set_tensor_shape(tmp_shape);
179 }
180
181 Window win = calculate_max_window(tmp_info, Steps(1, 1));
182
183 // Collapse along the Z direction
184 // This collapse needs to be here in order to tune the Z dimension of LWS
185 const unsigned int dimension_to_collapse = std::min(static_cast<unsigned int>(dst->num_dimensions()), 2u);
186 Window collapsed = win.collapse(win, dimension_to_collapse);
187
188 // Reconfigure window size, one arm_matrix_multiply kernel needs 16 threads to finish.
189 Window::Dimension x_dimension = collapsed.x();
190 Window::Dimension y_dimension = collapsed.y();
191
192 // Make M and N multiple of M0 and N0 respectively
193 const unsigned int ceil_to_multiple_n_n0 = ceil_to_multiple(x_dimension.end(), rhs_info.n0);
194 const unsigned int ceil_to_multiple_m_m0 = ceil_to_multiple(y_dimension.end(), lhs_info.m0);
195
196 // Divide M and N by M0 and N0 respectively
197 const unsigned int n_div_n0 = ceil_to_multiple_n_n0 / rhs_info.n0;
198 const unsigned int m_div_m0 = ceil_to_multiple_m_m0 / lhs_info.m0;
199
200 // Make n_div_n0 and m_div_m0 multiple of mmul_n0 and mmul_k0 respectively
201 const unsigned int ceil_to_multiple_n_div_n0_mmul_n0 = ceil_to_multiple(n_div_n0, mmul_n0);
202 const unsigned int ceil_to_multiple_m_div_m0_mmul_k0 = ceil_to_multiple(m_div_m0, mmul_k0);
203
204 // Ensure x_dimension is multiple of MMUL block size (mmul_n0 * mmul_k0)
205 x_dimension.set_end(ceil_to_multiple_n_div_n0_mmul_n0 * mmul_k0);
206 y_dimension.set_end(ceil_to_multiple_m_div_m0_mmul_k0 / mmul_k0);
207
208 collapsed.set(Window::DimX, x_dimension);
209 collapsed.set(Window::DimY, y_dimension);
210
211 return std::make_pair(Status{}, collapsed);
212}
213} // namespace
214
215ClGemmMatrixMultiplyReshapedOnlyRhsMMULKernel::ClGemmMatrixMultiplyReshapedOnlyRhsMMULKernel()
216{
217 _type = CLKernelType::GEMM;
218}
219
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100220void ClGemmMatrixMultiplyReshapedOnlyRhsMMULKernel::configure(const CLCompileContext &compile_context,
221 ITensorInfo *src0,
222 ITensorInfo *src1,
223 ITensorInfo *src2,
224 ITensorInfo *dst,
225 float alpha,
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000226 float beta,
227 const GEMMLHSMatrixInfo &lhs_info,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100228 const GEMMRHSMatrixInfo &rhs_info,
229 const GEMMKernelInfo &gemm_info)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000230{
231 ARM_COMPUTE_ERROR_ON_NULLPTR(src0, src1, dst);
232
233 // dst tensor auto initialization if not yet initialized
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100234 auto_init_if_empty(
235 *dst, src0->clone()->set_tensor_shape(misc::shape_calculator::compute_mm_shape(*src0, *src1, gemm_info)));
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000236
237 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src0, src1, src2, dst, alpha, beta, lhs_info, rhs_info, gemm_info));
238
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100239 auto padding_info = get_padding_info({src0, src1, src2, dst});
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000240 _add_bias = src2 != nullptr;
241 _export_to_cl_image = rhs_info.export_to_cl_image;
242
243 // Configure kernel window
244 auto win_config = validate_and_configure_window(src0, src1, src2, dst, lhs_info, rhs_info, gemm_info);
245 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
246
247 IClKernel::configure_internal(win_config.second);
248
249 _m = gemm_info.m;
250 _n = gemm_info.n;
251 _k = gemm_info.k;
252
253 const unsigned int m0_leftover = _m % lhs_info.m0;
254 const unsigned int n0_leftover = _n % rhs_info.n0;
255
256 // Create build options
257 CLBuildOptions build_opts;
258 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(src0->data_type()));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100259 build_opts.add_option_if(!(helpers::float_ops::is_one(alpha)),
260 "-DALPHA=" + float_to_string_with_full_precision(alpha));
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000261 build_opts.add_option_if(src2 != nullptr, "-DBETA=" + float_to_string_with_full_precision(beta));
262 build_opts.add_option_if(helpers::float_ops::is_one(beta), "-DUNIT_BETA");
263 build_opts.add_option_if(gemm_info.broadcast_bias, "-DBROADCAST_BIAS");
264 build_opts.add_option_if(src0->data_type() == DataType::F16, "-DHALF_PRECISION");
265 build_opts.add_option("-DM0=" + support::cpp11::to_string(lhs_info.m0));
266 build_opts.add_option("-DN0=" + support::cpp11::to_string(rhs_info.n0));
267 build_opts.add_option("-DK0=" + support::cpp11::to_string(rhs_info.k0));
268 build_opts.add_option("-DM0_LEFTOVER=" + support::cpp11::to_string(m0_leftover));
269 build_opts.add_option("-DN0_LEFTOVER=" + support::cpp11::to_string(n0_leftover));
270 build_opts.add_option("-DMMUL_M0=" + support::cpp11::to_string(mmul_m0));
271 build_opts.add_option("-DMMUL_N0=" + support::cpp11::to_string(mmul_n0));
272 build_opts.add_option("-DMMUL_K0=" + support::cpp11::to_string(mmul_k0));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100273 build_opts.add_option("-DACTIVATION_TYPE=" +
274 lower_string(string_from_activation_func(gemm_info.activation_info.activation())));
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000275 build_opts.add_option("-DA_VAL=" + float_to_string_with_full_precision(gemm_info.activation_info.a()));
276 build_opts.add_option("-DB_VAL=" + float_to_string_with_full_precision(gemm_info.activation_info.b()));
277
278 std::string kernel_name("gemm_mm_reshaped_only_rhs_nt_mmul");
279 kernel_name += rhs_info.export_to_cl_image ? "_texture" : "";
280
281 // A macro guard to compile ONLY the kernel of interest
282 build_opts.add_option("-D" + upper_string(kernel_name));
283
284 // Create kernel
285 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
286
287 // Set config_id for enabling LWS tuning
288 _config_id = kernel_name;
289 _config_id += "_";
290 _config_id += (_add_bias ? "add_bias_" : "");
291 _config_id += (gemm_info.broadcast_bias ? "broadcast_bias_" : "");
292 _config_id += (gemm_info.activation_info.enabled() ? "fused_activation_" : "");
293 _config_id += lower_string(string_from_data_type(src0->data_type()));
294 _config_id += "_";
295 _config_id += support::cpp11::to_string(_m);
296 _config_id += "_";
297 _config_id += support::cpp11::to_string(_n);
298 _config_id += "_";
299 _config_id += support::cpp11::to_string(_k);
300 _config_id += "_";
301 _config_id += support::cpp11::to_string(lhs_info.m0);
302 _config_id += "_";
303 _config_id += support::cpp11::to_string(rhs_info.n0);
304
305 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
306}
307
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100308Status ClGemmMatrixMultiplyReshapedOnlyRhsMMULKernel::validate(const ITensorInfo *src0,
309 const ITensorInfo *src1,
310 const ITensorInfo *src2,
311 const ITensorInfo *dst,
312 float alpha,
313 float beta,
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000314 const GEMMLHSMatrixInfo &lhs_info,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100315 const GEMMRHSMatrixInfo &rhs_info,
316 const GEMMKernelInfo &gemm_info)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000317{
318 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 +0100319 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(src0->clone().get(), src1->clone().get(),
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000320 src2 != nullptr ? src2->clone().get() : nullptr,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100321 dst->clone().get(), lhs_info, rhs_info, gemm_info)
322 .first);
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000323
324 return Status{};
325}
326
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100327void ClGemmMatrixMultiplyReshapedOnlyRhsMMULKernel::run_op(ITensorPack &tensors,
328 const Window &window,
329 cl::CommandQueue &queue)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000330{
331 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
332 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
333
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100334 const auto src0 =
335 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
336 const auto src1 =
337 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
338 const auto src2 =
339 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_2));
340 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000341
342 ARM_COMPUTE_ERROR_ON_NULLPTR(src0, src1, dst);
343 ARM_COMPUTE_ERROR_ON(_add_bias && src2 == nullptr);
344
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100345 if (src1->info()->num_dimensions() < 3)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000346 {
347 // The stride_z for matrix B must be zero if we do not slice
348 ARM_COMPUTE_ERROR_ON(src1->info()->strides_in_bytes()[3] != 0);
349 }
350
351 cl::Image2D src1_image2d;
352
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100353 if (_export_to_cl_image)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000354 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100355 const TensorShape shape2d(src1->info()->dimension(0) / 4,
356 src1->info()->dimension(1) * src1->info()->dimension(2));
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000357 const size_t image_row_pitch = src1->info()->strides_in_bytes()[1];
358
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100359 src1_image2d = create_image2d_from_buffer(CLKernelLibrary::get().context(), src1->cl_buffer(), shape2d,
360 src1->info()->data_type(), image_row_pitch, CLImage2DType::ReadOnly);
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000361 }
362
363 Window slice = window.first_slice_window_3D();
364
365 do
366 {
367 unsigned int idx = 0;
368
369 add_3d_tensor_nhw_argument(idx, src0);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100370 if (_export_to_cl_image)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000371 {
372 _kernel.setArg(idx++, src1_image2d);
373 }
374 add_3d_tensor_nhw_argument(idx, src1);
375
376 // Bias buffer (_add_bias == true)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100377 if (_add_bias)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000378 {
379 add_3d_tensor_nhw_argument(idx, src2);
380 }
381 // dst buffer
382 add_3d_tensor_nhw_argument(idx, dst);
383
384 // 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.
385 _kernel.setArg<cl_int>(idx++, _m);
386 _kernel.setArg<cl_int>(idx++, _n);
387 _kernel.setArg<cl_int>(idx++, _k);
388
389 // LWS_x should be multiple of 16 at least. (32, 2) has been chosen to have more work-items on a single core
390 // LWS also enforces the order of execution of the workitems which improves cache utilization
391 enqueue(queue, *this, slice, cl::NDRange(32, 2), false);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100392 } while (window.slide_window_slice_3D(slice));
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000393}
394} // namespace kernels
395} // namespace opencl
396} // namespace arm_compute