blob: 546a61e2645691c97ed57ac1b33aeddc1e2ab17c [file] [log] [blame]
Georgios Pinitas856f66e2021-04-22 21:13:21 +01001/*
Gian Marco Iodice10e88a72021-11-29 12:49:19 +00002 * Copyright (c) 2019-2022 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"
Georgios Pinitas856f66e2021-04-22 21:13:21 +010027#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Georgios Pinitas856f66e2021-04-22 21:13:21 +010028#include "src/core/CL/CLUtils.h"
29#include "src/core/CL/CLValidate.h"
SiCongLi31778612021-11-12 17:33:45 +000030#include "src/core/experimental/PostOpUtils.h"
Georgios Pinitas856f66e2021-04-22 21:13:21 +010031#include "src/core/helpers/AutoConfiguration.h"
32#include "src/core/helpers/WindowHelpers.h"
33#include "src/core/utils/helpers/float_ops.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010034#include "src/gpu/cl/kernels/gemm/ClGemmHelpers.h"
Georgios Pinitas856f66e2021-04-22 21:13:21 +010035#include "support/Cast.h"
36#include "support/StringSupport.h"
37
38namespace arm_compute
39{
40namespace opencl
41{
42namespace kernels
43{
44namespace
45{
46using ElementsProcessed = Steps;
47
SiCongLiafa19722021-10-24 19:12:33 +010048const auto post_op_utils = experimental::PostOpCLKernelUtils(
49{
50 // PostOp sequence -> {Kernel Postfix, PostOp Slots}
51 { {}, { "", {} } },
52 { { experimental::PostOpType::Activation }, { "", { 1 } } },
ramelg016049eda2021-10-29 10:52:53 +010053
SiCongLiafa19722021-10-24 19:12:33 +010054 { { experimental::PostOpType::Eltwise_Add }, { "_post_act_eltwise_op_act", { 2 } } },
ramelg016049eda2021-10-29 10:52:53 +010055 { { experimental::PostOpType::Eltwise_PRelu }, { "_post_act_eltwise_op_act", { 2 } } },
56
SiCongLiafa19722021-10-24 19:12:33 +010057 { { experimental::PostOpType::Activation, experimental::PostOpType::Eltwise_Add }, { "_post_act_eltwise_op_act", { 1, 2 } } },
ramelg016049eda2021-10-29 10:52:53 +010058 { { experimental::PostOpType::Activation, experimental::PostOpType::Eltwise_PRelu }, { "_post_act_eltwise_op_act", { 1, 2 } } },
59
SiCongLiafa19722021-10-24 19:12:33 +010060 { { experimental::PostOpType::Eltwise_Add, experimental::PostOpType::Activation }, { "_post_act_eltwise_op_act", { 2, 3 } } },
ramelg016049eda2021-10-29 10:52:53 +010061 { { experimental::PostOpType::Eltwise_PRelu, experimental::PostOpType::Activation }, { "_post_act_eltwise_op_act", { 2, 3 } } },
62
63 { { experimental::PostOpType::Activation, experimental::PostOpType::Eltwise_Add, experimental::PostOpType::Activation }, { "_post_act_eltwise_op_act", { 1, 2, 3 } } },
64 { { experimental::PostOpType::Activation, experimental::PostOpType::Eltwise_PRelu, experimental::PostOpType::Activation }, { "_post_act_eltwise_op_act", { 1, 2, 3 } } }
SiCongLiafa19722021-10-24 19:12:33 +010065});
66
Georgios Pinitas856f66e2021-04-22 21:13:21 +010067Status validate_arguments(const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, float alpha, float beta,
68 const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, const GEMMKernelInfo &gemm_info)
69{
70 ARM_COMPUTE_UNUSED(alpha);
71 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src0, src1, dst);
72 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src0);
73 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);
75 ARM_COMPUTE_RETURN_ERROR_ON_MSG(src0->num_dimensions() > 4, "The number of dimensions for the LHS matrix must be <= 4");
76 ARM_COMPUTE_RETURN_ERROR_ON_MSG(src1->num_dimensions() > 3, "The number of dimensions for the RHS matrix must be <= 3");
77 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");
78 ARM_COMPUTE_RETURN_ERROR_ON(rhs_info.k0 > 16 || rhs_info.k0 < 2);
79 ARM_COMPUTE_RETURN_ERROR_ON_MSG(((rhs_info.k0 & (rhs_info.k0 - 1)) && rhs_info.k0 != 3), "Only 2,3,4,8,16 are supported for k0");
80 ARM_COMPUTE_RETURN_ERROR_ON(rhs_info.n0 > 16 || rhs_info.n0 < 2);
81 ARM_COMPUTE_RETURN_ERROR_ON_MSG(((rhs_info.n0 & (rhs_info.n0 - 1)) && rhs_info.n0 != 3), "Only 2,3,4,8,16 are supported for n0");
82 ARM_COMPUTE_RETURN_ERROR_ON_MSG((gemm_info.reinterpret_input_as_3d || gemm_info.depth_output_gemm3d != 0) && (src2 != nullptr)
83 && (!gemm_info.broadcast_bias),
84 "Bias addition only supported with broadcast mode in case the input or dst has to be reinterpreted as 3D");
85 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.fp_mixed_precision, "Mixed precision not supported");
Gian Marco Iodice10e88a72021-11-29 12:49:19 +000086 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.has_pad_y, "Tensors cannot have padding along the Y direction");
Georgios Pinitas856f66e2021-04-22 21:13:21 +010087 ARM_COMPUTE_RETURN_ON_ERROR(gemm::validate_image2d_support_on_rhs(*src1, rhs_info));
SiCongLiafa19722021-10-24 19:12:33 +010088 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!post_op_utils.is_post_op_sequence_supported(gemm_info.post_ops), "The sequence of Post Ops is not supported");
Georgios Pinitas856f66e2021-04-22 21:13:21 +010089
90 const unsigned int m = gemm_info.m;
91 const unsigned int n = gemm_info.n;
92 const unsigned int k = gemm_info.k;
93
94 TensorShape tensor_shape1{ src1->tensor_shape() };
95 tensor_shape1.set(0, n);
96 tensor_shape1.set(1, k);
97
98 if(src2 != nullptr && !(helpers::float_ops::is_zero(beta)))
99 {
100 const unsigned int src2_dim0 = src2->dimension(0);
101 const unsigned int src2_dim1 = src2->dimension(1);
102
103 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src2, src0);
104 if(gemm_info.broadcast_bias)
105 {
106 ARM_COMPUTE_RETURN_ERROR_ON_MSG((src2_dim1 != 1 || src2_dim0 != n), "Incorrect dimension of bias matrix which is to be broadcasted");
107 }
108 else
109 {
110 ARM_COMPUTE_RETURN_ERROR_ON_MSG((src2_dim0 != n || src2_dim1 != m), "Incorrect dimension of bias matrix");
111 }
112 }
113
114 const TensorInfo tensor_info1 = src1->clone()->set_tensor_shape(tensor_shape1);
115
116 const TensorInfo tensor_info_reshaped1 = src1->clone()->set_tensor_shape(misc::shape_calculator::compute_rhs_reshaped_shape(tensor_info1, rhs_info));
117
118 ARM_COMPUTE_RETURN_ERROR_ON(src0->dimension(0) != k);
119 if(gemm_info.reinterpret_input_as_3d)
120 {
121 ARM_COMPUTE_RETURN_ERROR_ON(src0->dimension(1) * src0->dimension(2) != m);
122 }
123 else
124 {
125 ARM_COMPUTE_RETURN_ERROR_ON(src0->dimension(1) != m);
126 }
127 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(src1, &tensor_info_reshaped1);
128
129 if(dst->total_size() != 0)
130 {
131 const TensorInfo tensor_info_dst = dst->clone()->set_tensor_shape(misc::shape_calculator::compute_mm_shape(*src0, *src1, gemm_info));
132 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(dst, &tensor_info_dst);
133 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src0, dst);
SiCongLiafa19722021-10-24 19:12:33 +0100134 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!post_op_utils.are_post_op_shapes_compliant(dst, gemm_info.post_ops), "The Post Op shapes are not compliant");
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100135 }
136
137 return Status{};
138}
139
Giorgio Arena951d5202021-09-08 13:26:06 +0100140Window validate_and_configure_window(ITensorInfo *src0, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst, const GEMMLHSMatrixInfo &lhs_info,
141 const GEMMRHSMatrixInfo &rhs_info, const GEMMKernelInfo &gemm_info, ElementsProcessed &num_elements_processed)
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100142{
Giorgio Arena951d5202021-09-08 13:26:06 +0100143 ARM_COMPUTE_UNUSED(src0, src1, src2);
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100144 unsigned int &num_elems_processed_per_iteration_x = num_elements_processed[0];
145 unsigned int &num_elems_processed_per_iteration_y = num_elements_processed[1];
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100146 bool reinterpret_output_as_3d = gemm_info.depth_output_gemm3d != 0;
147
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100148 TensorInfo tmp_info(*dst);
149
150 if(reinterpret_output_as_3d)
151 {
152 // Since the dst tensor has to be reinterpreted as 3D and the execute window is based on a 2D GEMM,
153 // the window needs to be constructed on the 2D collapsed version of the tensor
154 TensorShape tmp_shape(dst->tensor_shape());
155 tmp_shape.collapse(2U, 1U);
156 tmp_info.set_tensor_shape(tmp_shape);
157 }
158
159 // Configure kernel window
160 num_elems_processed_per_iteration_x = rhs_info.n0;
161 num_elems_processed_per_iteration_y = lhs_info.m0;
162
Giorgio Arena951d5202021-09-08 13:26:06 +0100163 Window win = 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 +0100164
165 // Collapse along the Z direction
166 // This collapse needs to be here in order to tune the Z dimension of LWS
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100167 const unsigned int dimension_to_collapse = std::min(static_cast<unsigned int>(dst->num_dimensions()), 2u);
Giorgio Arena951d5202021-09-08 13:26:06 +0100168 Window collapsed = win.collapse(win, dimension_to_collapse);
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100169
Giorgio Arena951d5202021-09-08 13:26:06 +0100170 return collapsed;
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100171}
172} // namespace
173
Giorgio Arena4a95bba2021-06-28 11:00:27 +0100174ClGemmMatrixMultiplyReshapedOnlyRhsKernel::ClGemmMatrixMultiplyReshapedOnlyRhsKernel()
175{
176 _type = CLKernelType::GEMM;
177}
178
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100179void ClGemmMatrixMultiplyReshapedOnlyRhsKernel::configure(const CLCompileContext &compile_context,
Giorgio Arena951d5202021-09-08 13:26:06 +0100180 const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *src2, ITensorInfo *dst, float alpha, float beta,
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100181 const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, const GEMMKernelInfo &gemm_info)
182{
183 ARM_COMPUTE_ERROR_ON_NULLPTR(src0, src1, dst);
184
SiCongLiafa19722021-10-24 19:12:33 +0100185 // dst tensor auto initialization if not yet initialized
186 auto_init_if_empty(*dst, src0->clone()->set_tensor_shape(misc::shape_calculator::compute_mm_shape(*src0, *src1, gemm_info)));
187
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100188 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src0, src1, src2, dst, alpha, beta, lhs_info, rhs_info, gemm_info));
189
190 _reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d;
191 _reinterpret_output_as_3d = gemm_info.depth_output_gemm3d != 0;
192 _use_dummy_work_items = preferred_dummy_work_items_support(CLKernelLibrary::get().get_device());
193 _add_bias = src2 != nullptr;
194 _export_to_cl_image = rhs_info.export_to_cl_image;
SiCongLiafa19722021-10-24 19:12:33 +0100195 _num_post_op_args = gemm_info.post_ops.total_num_arguments();
Giorgio Arena951d5202021-09-08 13:26:06 +0100196
197 auto padding_info = get_padding_info({ src0, src1, src2, dst });
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100198
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100199 // Check if we need to slide the matrix B
200 const unsigned int num_dimensions_src0 = src0->num_dimensions();
201 _slide_matrix_b = (src1->num_dimensions() >= num_dimensions_src0);
202
203 ElementsProcessed num_elements_processed{};
204
205 // Configure kernel window
Giorgio Arena951d5202021-09-08 13:26:06 +0100206 Window win = validate_and_configure_window(src0->clone().get(), src1->clone().get(), (src2 != nullptr) ? src2->clone().get() : nullptr, dst->clone().get(), lhs_info, rhs_info, gemm_info,
207 num_elements_processed);
208 ICLKernel::configure_internal(win);
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100209
210 // If _reinterpret_input_as_3d = reinterpret_output_as_3d = true,
211 // we will dispatch a batched-GEMM to reduce the complexity of the address calculation within the OpenCL kernel.
212 // This means that the actual m used by the kernel is given by dst->dimension(1) and not by gemm_info.m
213 const unsigned int internal_m = _reinterpret_output_as_3d ? gemm_info.m : dst->dimension(1);
214
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100215 // Shrink M0 to be always <= M (internal_m) to prevent out-of-bounds reads.
216 // NOTE: This might have implications on heuristics and performance
217 const unsigned int internal_m0 = std::min(internal_m, lhs_info.m0);
218
219 // 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.
220 const unsigned int partial_store_m0 = internal_m % internal_m0;
221 const unsigned int partial_store_n0 = gemm_info.n % rhs_info.n0;
ramelg019cca5922021-11-11 10:05:00 +0000222 _m = internal_m;
223 _n = gemm_info.n;
224 _k = gemm_info.k;
Gian Marco Iodice10e88a72021-11-29 12:49:19 +0000225
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100226 // Create build options
227 CLBuildOptions build_opts;
228 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(src0->data_type()));
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100229 build_opts.add_option("-DM0=" + support::cpp11::to_string(internal_m0));
230 build_opts.add_option("-DN0=" + support::cpp11::to_string(rhs_info.n0));
231 build_opts.add_option("-DK0=" + support::cpp11::to_string(rhs_info.k0));
232 build_opts.add_option("-DH0=" + support::cpp11::to_string(rhs_info.h0));
233 build_opts.add_option("-DPARTIAL_STORE_M0=" + support::cpp11::to_string(partial_store_m0));
234 build_opts.add_option("-DPARTIAL_STORE_N0=" + support::cpp11::to_string(partial_store_n0));
Gian Marco Iodice10e88a72021-11-29 12:49:19 +0000235 build_opts.add_option_if(_use_dummy_work_items, "-DDUMMY_WORK_ITEMS");
236 build_opts.add_option_if(!(helpers::float_ops::is_one(alpha)), "-DALPHA=" + float_to_string_with_full_precision(alpha));
237 build_opts.add_option_if(src2 != nullptr, "-DBETA=" + float_to_string_with_full_precision(beta));
238 build_opts.add_option_if(helpers::float_ops::is_one(beta), "-DUNIT_BETA");
239 build_opts.add_option_if(gemm_info.broadcast_bias, "-DBROADCAST_BIAS");
240 build_opts.add_option_if(!_slide_matrix_b, "-DMATRIX_B_DEPTH=" + support::cpp11::to_string(src1->dimension(2)));
241 build_opts.add_option_if(rhs_info.interleave, "-DRHS_INTERLEAVE");
242 build_opts.add_option_if(gemm_info.k % rhs_info.k0, "-DRUN_LEFTOVER_K0");
243 build_opts.add_option_if((gemm_info.k % rhs_info.k0) && rhs_info.transpose, "-DPARTIAL_K=" + support::cpp11::to_string(gemm_info.k % rhs_info.k0));
244
SiCongLiafa19722021-10-24 19:12:33 +0100245 // If post_ops are used, then we disable the use of gemm_info.activation_info
246 if(gemm_info.post_ops.size() > 0)
247 {
248 post_op_utils.set_post_ops_cl_build_options(build_opts, gemm_info.post_ops);
249 }
250 else
251 {
Gian Marco Iodice10e88a72021-11-29 12:49:19 +0000252 build_opts.add_option_if(gemm_info.activation_info.enabled(), "-DPOST_OP1");
253 build_opts.add_option_if(gemm_info.activation_info.enabled(), "-DP1_ACTIVATION_TYPE=" + lower_string(string_from_activation_func(gemm_info.activation_info.activation())));
254 build_opts.add_option_if(gemm_info.activation_info.enabled(), "-DP1_ACTIVATION_A_VAL=" + float_to_string_with_full_precision(gemm_info.activation_info.a()));
255 build_opts.add_option_if(gemm_info.activation_info.enabled(), "-DP1_ACTIVATION_B_VAL=" + float_to_string_with_full_precision(gemm_info.activation_info.b()));
SiCongLiafa19722021-10-24 19:12:33 +0100256 }
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100257
258 std::string kernel_name("gemm_mm_reshaped_only_rhs_");
259 kernel_name += rhs_info.transpose ? "t" : "nt";
Gian Marco Iodice10e88a72021-11-29 12:49:19 +0000260 kernel_name += _export_to_cl_image ? "_texture" : "";
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100261
ramelg019cca5922021-11-11 10:05:00 +0000262 // A macro guard to compile ONLY the kernel of interest
263 build_opts.add_option("-D" + upper_string(kernel_name));
264
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100265 // Create kernel
266 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
267
268 // Set config_id for enabling LWS tuning
269 _config_id = kernel_name;
270 _config_id += "_";
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100271 _config_id += (_add_bias ? "add_bias_" : "");
272 _config_id += (gemm_info.broadcast_bias ? "broadcast_bias_" : "");
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100273 _config_id += (gemm_info.activation_info.enabled() ? "fused_activation_" : "");
274 _config_id += lower_string(string_from_data_type(src0->data_type()));
275 _config_id += "_";
276 _config_id += support::cpp11::to_string(dst->dimension(1));
277 _config_id += "_";
278 _config_id += support::cpp11::to_string(dst->dimension(0));
279 _config_id += "_";
280 _config_id += support::cpp11::to_string(gemm_info.k);
281 _config_id += "_";
282 _config_id += support::cpp11::to_string(dst->dimension(2));
283 _config_id += "_";
284 _config_id += support::cpp11::to_string(lhs_info.m0);
285 _config_id += "_";
286 _config_id += support::cpp11::to_string(rhs_info.n0);
287 _config_id += "_";
288 _config_id += support::cpp11::to_string(rhs_info.k0);
289 _config_id += "_";
290 _config_id += support::cpp11::to_string(rhs_info.h0);
291 _config_id += "_";
292 _config_id += support::cpp11::to_string(rhs_info.interleave);
293
294 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
295}
296
297Status ClGemmMatrixMultiplyReshapedOnlyRhsKernel::validate(const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, float alpha, float beta,
298 const GEMMLHSMatrixInfo &lhs_info,
299 const GEMMRHSMatrixInfo &rhs_info, const GEMMKernelInfo &gemm_info)
300{
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100301 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 +0100302 return Status{};
303}
304
305void ClGemmMatrixMultiplyReshapedOnlyRhsKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
306{
307 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
308 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
309
310 const auto src0 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
311 const auto src1 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
312 const auto src2 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_2));
313 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
314
315 ARM_COMPUTE_ERROR_ON_NULLPTR(src0, src1, dst);
316 ARM_COMPUTE_ERROR_ON(_add_bias && src2 == nullptr);
317
318 if(src1->info()->num_dimensions() < 3)
319 {
320 // The stride_z for matrix B must be zero if we do not slice
321 ARM_COMPUTE_ERROR_ON(src1->info()->strides_in_bytes()[3] != 0);
322 }
323
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100324 Window slice = window.first_slice_window_3D();
325 Window slice_matrix_b = slice;
326
327 slice_matrix_b.set(Window::DimX, Window::Dimension(0, 1, 1));
328 slice_matrix_b.set(Window::DimY, Window::Dimension(0, 1, 1));
329
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100330 cl::Image2D src1_image2d;
331
332 if(_export_to_cl_image)
333 {
334 const TensorShape shape2d(src1->info()->dimension(0) / 4, src1->info()->dimension(1) * src1->info()->dimension(2));
335 const size_t image_row_pitch = src1->info()->strides_in_bytes()[1];
336
337 src1_image2d = create_image2d_from_buffer(CLKernelLibrary::get().context(), src1->cl_buffer(), shape2d, src1->info()->data_type(), image_row_pitch);
338 }
339
340 do
341 {
342 Window slice_b = slice;
343 // Don't slice matrix B along the z dimension if matrix B has just 2 dimensions and matrix A more than 2
344 // This scenario can happen when the matrix multiplication is used to perform a convolution operation
345 if(!_slide_matrix_b)
346 {
347 slice_b = slice_matrix_b;
348 }
349
350 unsigned int idx = 0;
351
352 // LHS buffer
Gian Marco Iodice10e88a72021-11-29 12:49:19 +0000353 add_3d_tensor_nhw_argument(idx, src0);
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100354
355 // RHS buffer or RHS OpenCL image (_export_to_cl_image == true)
356 if(_export_to_cl_image)
357 {
358 _kernel.setArg(idx++, src1_image2d);
359 }
Gian Marco Iodice10e88a72021-11-29 12:49:19 +0000360 add_3d_tensor_nhw_argument(idx, src1);
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100361
362 // Bias buffer (_add_bias == true)
Gian Marco Iodice10e88a72021-11-29 12:49:19 +0000363 if(_add_bias)
364 {
365 add_3d_tensor_nhw_argument(idx, src2);
366 }
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100367
SiCongLiafa19722021-10-24 19:12:33 +0100368 // post op argument buffers
369 for(size_t i = 0; i < _num_post_op_args; ++i)
370 {
371 const auto post_op_arg = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(experimental::get_post_op_arg_type(i)));
Gian Marco Iodice10e88a72021-11-29 12:49:19 +0000372 add_3d_tensor_nhw_argument(idx, post_op_arg);
SiCongLiafa19722021-10-24 19:12:33 +0100373 }
374
Gian Marco Iodice10e88a72021-11-29 12:49:19 +0000375 // dst buffer
376 add_3d_tensor_nhw_argument(idx, dst);
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100377
ramelg019cca5922021-11-11 10:05:00 +0000378 // 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.
379 _kernel.setArg<cl_int>(idx++, _m);
380 _kernel.setArg<cl_int>(idx++, _n);
381 _kernel.setArg<cl_int>(idx++, _k);
382
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100383 enqueue(queue, *this, slice, lws_hint(), _use_dummy_work_items);
384 }
385 while(window.slide_window_slice_3D(slice));
386}
387} // namespace kernels
388} // namespace opencl
389} // namespace arm_compute