1*c217d954SCole Faust /*
2*c217d954SCole Faust  * Copyright (c) 2019-2023 Arm Limited.
3*c217d954SCole Faust  *
4*c217d954SCole Faust  * SPDX-License-Identifier: MIT
5*c217d954SCole Faust  *
6*c217d954SCole Faust  * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust  * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust  * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust  * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust  * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust  *
13*c217d954SCole Faust  * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust  * copies or substantial portions of the Software.
15*c217d954SCole Faust  *
16*c217d954SCole Faust  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust  * SOFTWARE.
23*c217d954SCole Faust  */
24*c217d954SCole Faust #include "src/gpu/cl/kernels/ClGemmMatrixMultiplyReshapedOnlyRhsKernel.h"
25*c217d954SCole Faust 
26*c217d954SCole Faust #include "arm_compute/core/CL/ICLTensor.h"
27*c217d954SCole Faust #include "arm_compute/core/utils/misc/ShapeCalculator.h"
28*c217d954SCole Faust #include "src/core/CL/CLUtils.h"
29*c217d954SCole Faust #include "src/core/CL/CLValidate.h"
30*c217d954SCole Faust #include "src/core/experimental/PostOpUtils.h"
31*c217d954SCole Faust #include "src/core/helpers/AutoConfiguration.h"
32*c217d954SCole Faust #include "src/core/helpers/WindowHelpers.h"
33*c217d954SCole Faust #include "src/core/utils/helpers/float_ops.h"
34*c217d954SCole Faust #include "src/gpu/cl/kernels/gemm/ClGemmHelpers.h"
35*c217d954SCole Faust #include "support/Cast.h"
36*c217d954SCole Faust #include "support/StringSupport.h"
37*c217d954SCole Faust 
38*c217d954SCole Faust namespace arm_compute
39*c217d954SCole Faust {
40*c217d954SCole Faust namespace opencl
41*c217d954SCole Faust {
42*c217d954SCole Faust namespace kernels
43*c217d954SCole Faust {
44*c217d954SCole Faust namespace
45*c217d954SCole Faust {
46*c217d954SCole Faust using ElementsProcessed = Steps;
47*c217d954SCole Faust 
48*c217d954SCole Faust const auto post_op_utils = experimental::PostOpCLKernelUtils(
49*c217d954SCole Faust {
50*c217d954SCole Faust     //  PostOp sequence                   -> {Kernel Postfix, PostOp Slots}
51*c217d954SCole Faust     { {}, { "", {} } },
52*c217d954SCole Faust     { { experimental::PostOpType::Activation }, { "", { 1 } } },
53*c217d954SCole Faust 
54*c217d954SCole Faust     { { experimental::PostOpType::Eltwise_Add }, { "_post_act_eltwise_op_act", { 2 } } },
55*c217d954SCole Faust     { { experimental::PostOpType::Eltwise_PRelu }, { "_post_act_eltwise_op_act", { 2 } } },
56*c217d954SCole Faust 
57*c217d954SCole Faust     { { experimental::PostOpType::Activation, experimental::PostOpType::Eltwise_Add }, { "_post_act_eltwise_op_act", { 1, 2 } } },
58*c217d954SCole Faust     { { experimental::PostOpType::Activation, experimental::PostOpType::Eltwise_PRelu }, { "_post_act_eltwise_op_act", { 1, 2 } } },
59*c217d954SCole Faust 
60*c217d954SCole Faust     { { experimental::PostOpType::Eltwise_Add, experimental::PostOpType::Activation }, { "_post_act_eltwise_op_act", { 2, 3 } } },
61*c217d954SCole Faust     { { experimental::PostOpType::Eltwise_PRelu, experimental::PostOpType::Activation }, { "_post_act_eltwise_op_act", { 2, 3 } } },
62*c217d954SCole Faust 
63*c217d954SCole Faust     { { experimental::PostOpType::Activation, experimental::PostOpType::Eltwise_Add, experimental::PostOpType::Activation }, { "_post_act_eltwise_op_act", { 1, 2, 3 } } },
64*c217d954SCole Faust     { { experimental::PostOpType::Activation, experimental::PostOpType::Eltwise_PRelu, experimental::PostOpType::Activation }, { "_post_act_eltwise_op_act", { 1, 2, 3 } } }
65*c217d954SCole Faust });
66*c217d954SCole Faust 
validate_arguments(const ITensorInfo * src0,const ITensorInfo * src1,const ITensorInfo * src2,const ITensorInfo * dst,float alpha,float beta,const GEMMLHSMatrixInfo & lhs_info,const GEMMRHSMatrixInfo & rhs_info,const GEMMKernelInfo & gemm_info)67*c217d954SCole Faust Status validate_arguments(const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, float alpha, float beta,
68*c217d954SCole Faust                           const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, const GEMMKernelInfo &gemm_info)
69*c217d954SCole Faust {
70*c217d954SCole Faust     ARM_COMPUTE_UNUSED(alpha);
71*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src0, src1, dst);
72*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src0);
73*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src0, 1, DataType::F16, DataType::F32);
74*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src0, src1);
75*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_MSG(src0->num_dimensions() > 4, "The number of dimensions for the LHS matrix must be <= 4");
76*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_MSG(src1->num_dimensions() > 3, "The number of dimensions for the RHS matrix must be <= 3");
77*c217d954SCole Faust     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*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON(rhs_info.k0 > 16 || rhs_info.k0 < 2);
79*c217d954SCole Faust     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*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON(rhs_info.n0 > 16 || rhs_info.n0 < 2);
81*c217d954SCole Faust     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*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_MSG((gemm_info.reinterpret_input_as_3d || gemm_info.depth_output_gemm3d != 0) && (src2 != nullptr)
83*c217d954SCole Faust                                     && (!gemm_info.broadcast_bias),
84*c217d954SCole Faust                                     "Bias addition only supported with broadcast mode in case the input or dst has to be reinterpreted as 3D");
85*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.fp_mixed_precision, "Mixed precision not supported");
86*c217d954SCole Faust     ARM_COMPUTE_RETURN_ON_ERROR(gemm::validate_image2d_support_on_rhs(*src1, rhs_info));
87*c217d954SCole Faust     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");
88*c217d954SCole Faust 
89*c217d954SCole Faust     const unsigned int m = gemm_info.m;
90*c217d954SCole Faust     const unsigned int n = gemm_info.n;
91*c217d954SCole Faust     const unsigned int k = gemm_info.k;
92*c217d954SCole Faust 
93*c217d954SCole Faust     TensorShape tensor_shape1{ src1->tensor_shape() };
94*c217d954SCole Faust     tensor_shape1.set(0, n);
95*c217d954SCole Faust     tensor_shape1.set(1, k);
96*c217d954SCole Faust 
97*c217d954SCole Faust     if(src2 != nullptr && !(helpers::float_ops::is_zero(beta)))
98*c217d954SCole Faust     {
99*c217d954SCole Faust         const unsigned int src2_dim0 = src2->dimension(0);
100*c217d954SCole Faust         const unsigned int src2_dim1 = src2->dimension(1);
101*c217d954SCole Faust 
102*c217d954SCole Faust         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src2, src0);
103*c217d954SCole Faust         if(gemm_info.broadcast_bias)
104*c217d954SCole Faust         {
105*c217d954SCole Faust             ARM_COMPUTE_RETURN_ERROR_ON_MSG((src2_dim1 != 1 || src2_dim0 != n), "Incorrect dimension of bias matrix which is to be broadcasted");
106*c217d954SCole Faust         }
107*c217d954SCole Faust         else
108*c217d954SCole Faust         {
109*c217d954SCole Faust             ARM_COMPUTE_RETURN_ERROR_ON_MSG((src2_dim0 != n || src2_dim1 != m), "Incorrect dimension of bias matrix");
110*c217d954SCole Faust         }
111*c217d954SCole Faust     }
112*c217d954SCole Faust 
113*c217d954SCole Faust     const TensorInfo tensor_info1 = src1->clone()->set_tensor_shape(tensor_shape1);
114*c217d954SCole Faust 
115*c217d954SCole Faust     const TensorInfo tensor_info_reshaped1 = src1->clone()->set_tensor_shape(misc::shape_calculator::compute_rhs_reshaped_shape(tensor_info1, rhs_info));
116*c217d954SCole Faust 
117*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON(src0->dimension(0) != k);
118*c217d954SCole Faust     if(gemm_info.reinterpret_input_as_3d)
119*c217d954SCole Faust     {
120*c217d954SCole Faust         ARM_COMPUTE_RETURN_ERROR_ON(src0->dimension(1) * src0->dimension(2) != m);
121*c217d954SCole Faust     }
122*c217d954SCole Faust     else
123*c217d954SCole Faust     {
124*c217d954SCole Faust         ARM_COMPUTE_RETURN_ERROR_ON(src0->dimension(1) != m);
125*c217d954SCole Faust     }
126*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(src1, &tensor_info_reshaped1);
127*c217d954SCole Faust 
128*c217d954SCole Faust     if(dst->total_size() != 0)
129*c217d954SCole Faust     {
130*c217d954SCole Faust         const TensorInfo tensor_info_dst = dst->clone()->set_tensor_shape(misc::shape_calculator::compute_mm_shape(*src0, *src1, gemm_info));
131*c217d954SCole Faust         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(dst, &tensor_info_dst);
132*c217d954SCole Faust         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src0, dst);
133*c217d954SCole Faust         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");
134*c217d954SCole Faust     }
135*c217d954SCole Faust 
136*c217d954SCole Faust     return Status{};
137*c217d954SCole Faust }
138*c217d954SCole Faust 
validate_and_configure_window(ITensorInfo * src0,ITensorInfo * src1,ITensorInfo * src2,ITensorInfo * dst,const GEMMLHSMatrixInfo & lhs_info,const GEMMRHSMatrixInfo & rhs_info,const GEMMKernelInfo & gemm_info,ElementsProcessed & num_elements_processed)139*c217d954SCole Faust Window validate_and_configure_window(ITensorInfo *src0, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst, const GEMMLHSMatrixInfo &lhs_info,
140*c217d954SCole Faust                                      const GEMMRHSMatrixInfo &rhs_info, const GEMMKernelInfo &gemm_info, ElementsProcessed &num_elements_processed)
141*c217d954SCole Faust {
142*c217d954SCole Faust     ARM_COMPUTE_UNUSED(src0, src1, src2);
143*c217d954SCole Faust     unsigned int &num_elems_processed_per_iteration_x = num_elements_processed[0];
144*c217d954SCole Faust     unsigned int &num_elems_processed_per_iteration_y = num_elements_processed[1];
145*c217d954SCole Faust     bool          reinterpret_input_as_3d             = gemm_info.reinterpret_input_as_3d;
146*c217d954SCole Faust     bool          reinterpret_output_as_3d            = gemm_info.depth_output_gemm3d != 0;
147*c217d954SCole Faust 
148*c217d954SCole Faust     // In case both input and dst have to be reinterpreted as 3D tensors,
149*c217d954SCole Faust     // force reinterpret_input_as_3d and reinterpret_output_as_3d to be false.
150*c217d954SCole Faust     // This approach should only be used when the input/dst tensors have pad on the y direction
151*c217d954SCole Faust     if((reinterpret_input_as_3d == reinterpret_output_as_3d) && gemm_info.has_pad_y)
152*c217d954SCole Faust     {
153*c217d954SCole Faust         reinterpret_output_as_3d = false;
154*c217d954SCole Faust     }
155*c217d954SCole Faust 
156*c217d954SCole Faust     TensorInfo tmp_info(*dst);
157*c217d954SCole Faust 
158*c217d954SCole Faust     if(reinterpret_output_as_3d)
159*c217d954SCole Faust     {
160*c217d954SCole Faust         // Since the dst tensor has to be reinterpreted as 3D and the execute window is based on a 2D GEMM,
161*c217d954SCole Faust         // the window needs to be constructed on the 2D collapsed version of the tensor
162*c217d954SCole Faust         TensorShape tmp_shape(dst->tensor_shape());
163*c217d954SCole Faust         tmp_shape.collapse(2U, 1U);
164*c217d954SCole Faust         tmp_info.set_tensor_shape(tmp_shape);
165*c217d954SCole Faust     }
166*c217d954SCole Faust 
167*c217d954SCole Faust     // Configure kernel window
168*c217d954SCole Faust     num_elems_processed_per_iteration_x = rhs_info.n0;
169*c217d954SCole Faust     num_elems_processed_per_iteration_y = lhs_info.m0;
170*c217d954SCole Faust 
171*c217d954SCole Faust     Window win = calculate_max_window(tmp_info, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
172*c217d954SCole Faust 
173*c217d954SCole Faust     // Collapse along the Z direction
174*c217d954SCole Faust     // This collapse needs to be here in order to tune the Z dimension of LWS
175*c217d954SCole Faust     const unsigned int dimension_to_collapse = std::min(static_cast<unsigned int>(dst->num_dimensions()), 2u);
176*c217d954SCole Faust     Window             collapsed             = win.collapse(win, dimension_to_collapse);
177*c217d954SCole Faust 
178*c217d954SCole Faust     return collapsed;
179*c217d954SCole Faust }
180*c217d954SCole Faust } // namespace
181*c217d954SCole Faust 
ClGemmMatrixMultiplyReshapedOnlyRhsKernel()182*c217d954SCole Faust ClGemmMatrixMultiplyReshapedOnlyRhsKernel::ClGemmMatrixMultiplyReshapedOnlyRhsKernel()
183*c217d954SCole Faust {
184*c217d954SCole Faust     _type = CLKernelType::GEMM;
185*c217d954SCole Faust }
186*c217d954SCole Faust 
configure(const CLCompileContext & compile_context,const ITensorInfo * src0,const ITensorInfo * src1,const ITensorInfo * src2,ITensorInfo * dst,float alpha,float beta,const GEMMLHSMatrixInfo & lhs_info,const GEMMRHSMatrixInfo & rhs_info,const GEMMKernelInfo & gemm_info)187*c217d954SCole Faust void ClGemmMatrixMultiplyReshapedOnlyRhsKernel::configure(const CLCompileContext &compile_context,
188*c217d954SCole Faust                                                           const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *src2, ITensorInfo *dst, float alpha, float beta,
189*c217d954SCole Faust                                                           const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, const GEMMKernelInfo &gemm_info)
190*c217d954SCole Faust {
191*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON_NULLPTR(src0, src1, dst);
192*c217d954SCole Faust 
193*c217d954SCole Faust     // dst tensor auto initialization if not yet initialized
194*c217d954SCole Faust     auto_init_if_empty(*dst, src0->clone()->set_tensor_shape(misc::shape_calculator::compute_mm_shape(*src0, *src1, gemm_info)));
195*c217d954SCole Faust 
196*c217d954SCole Faust     ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src0, src1, src2, dst, alpha, beta, lhs_info, rhs_info, gemm_info));
197*c217d954SCole Faust 
198*c217d954SCole Faust     _reinterpret_input_as_3d  = gemm_info.reinterpret_input_as_3d;
199*c217d954SCole Faust     _reinterpret_output_as_3d = gemm_info.depth_output_gemm3d != 0;
200*c217d954SCole Faust     _use_dummy_work_items     = preferred_dummy_work_items_support(CLKernelLibrary::get().get_device());
201*c217d954SCole Faust     _add_bias                 = src2 != nullptr;
202*c217d954SCole Faust     _export_to_cl_image       = rhs_info.export_to_cl_image;
203*c217d954SCole Faust     _has_pad_y                = gemm_info.has_pad_y;
204*c217d954SCole Faust     _num_post_op_args         = gemm_info.post_ops.total_num_arguments();
205*c217d954SCole Faust 
206*c217d954SCole Faust     auto padding_info = get_padding_info({ src0, src1, src2, dst });
207*c217d954SCole Faust 
208*c217d954SCole Faust     // In case both input and dst have to be reinterpreted as 3D tensors,
209*c217d954SCole Faust     // force reinterpret_input_as_3d and reinterpret_output_as_3d to be false.
210*c217d954SCole Faust     if((_reinterpret_input_as_3d == _reinterpret_output_as_3d) && _has_pad_y)
211*c217d954SCole Faust     {
212*c217d954SCole Faust         _reinterpret_input_as_3d  = false;
213*c217d954SCole Faust         _reinterpret_output_as_3d = false;
214*c217d954SCole Faust     }
215*c217d954SCole Faust 
216*c217d954SCole Faust     // Check if we need to slide the matrix B
217*c217d954SCole Faust     const unsigned int num_dimensions_src0 = src0->num_dimensions();
218*c217d954SCole Faust     _slide_matrix_b                        = (src1->num_dimensions() >= num_dimensions_src0);
219*c217d954SCole Faust 
220*c217d954SCole Faust     ElementsProcessed num_elements_processed{};
221*c217d954SCole Faust 
222*c217d954SCole Faust     // Configure kernel window
223*c217d954SCole Faust     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,
224*c217d954SCole Faust                                                num_elements_processed);
225*c217d954SCole Faust     ICLKernel::configure_internal(win);
226*c217d954SCole Faust 
227*c217d954SCole Faust     // If _reinterpret_input_as_3d = reinterpret_output_as_3d = true,
228*c217d954SCole Faust     // we will dispatch a batched-GEMM to reduce the complexity of the address calculation within the OpenCL kernel.
229*c217d954SCole Faust     // This means that the actual m used by the kernel is given by dst->dimension(1) and not by gemm_info.m
230*c217d954SCole Faust     const unsigned int internal_m = _reinterpret_output_as_3d ? gemm_info.m : dst->dimension(1);
231*c217d954SCole Faust 
232*c217d954SCole Faust     // These variables are used only if gemm_info.has_pad_y == true
233*c217d954SCole Faust     const unsigned int h_gemm_3d = _reinterpret_output_as_3d ? dst->dimension(1) : src0->dimension(1);
234*c217d954SCole Faust     const unsigned int d_gemm_3d = _reinterpret_output_as_3d ? dst->dimension(2) : src0->dimension(2);
235*c217d954SCole Faust 
236*c217d954SCole Faust     // Shrink M0 to be always <= M (internal_m) to prevent out-of-bounds reads.
237*c217d954SCole Faust     // NOTE: This might have implications on heuristics and performance
238*c217d954SCole Faust     const unsigned int internal_m0 = std::min(internal_m, lhs_info.m0);
239*c217d954SCole Faust 
240*c217d954SCole Faust     // 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.
241*c217d954SCole Faust     const unsigned int partial_store_m0 = internal_m % internal_m0;
242*c217d954SCole Faust     const unsigned int partial_store_n0 = gemm_info.n % rhs_info.n0;
243*c217d954SCole Faust     _m                                  = internal_m;
244*c217d954SCole Faust     _n                                  = gemm_info.n;
245*c217d954SCole Faust     _k                                  = gemm_info.k;
246*c217d954SCole Faust     // Create build options
247*c217d954SCole Faust     CLBuildOptions build_opts;
248*c217d954SCole Faust     build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(src0->data_type()));
249*c217d954SCole Faust     build_opts.add_option_if(!(helpers::float_ops::is_one(alpha)), "-DALPHA=" + float_to_string_with_full_precision(alpha));
250*c217d954SCole Faust     build_opts.add_option_if(src2 != nullptr, "-DBETA=" + float_to_string_with_full_precision(beta));
251*c217d954SCole Faust     build_opts.add_option_if(helpers::float_ops::is_one(beta), "-DUNIT_BETA");
252*c217d954SCole Faust     build_opts.add_option_if(gemm_info.broadcast_bias, "-DBROADCAST_BIAS");
253*c217d954SCole Faust     build_opts.add_option_if(!_slide_matrix_b, "-DMATRIX_B_DEPTH=" + support::cpp11::to_string(src1->dimension(2)));
254*c217d954SCole Faust     build_opts.add_option_if(rhs_info.interleave, "-DRHS_INTERLEAVE");
255*c217d954SCole Faust     build_opts.add_option_if(_use_dummy_work_items, "-DDUMMY_WORK_ITEMS");
256*c217d954SCole Faust     build_opts.add_option_if(rhs_info.export_to_cl_image, "-DOPENCL_IMAGE_SUPPORT");
257*c217d954SCole Faust     build_opts.add_option("-DRHS_HEIGHT=" + support::cpp11::to_string(src1->dimension(1)));
258*c217d954SCole Faust     build_opts.add_option("-DM0=" + support::cpp11::to_string(internal_m0));
259*c217d954SCole Faust     build_opts.add_option("-DN0=" + support::cpp11::to_string(rhs_info.n0));
260*c217d954SCole Faust     build_opts.add_option("-DK0=" + support::cpp11::to_string(rhs_info.k0));
261*c217d954SCole Faust     build_opts.add_option("-DH0=" + support::cpp11::to_string(rhs_info.h0));
262*c217d954SCole Faust     build_opts.add_option("-DPARTIAL_STORE_M0=" + support::cpp11::to_string(partial_store_m0));
263*c217d954SCole Faust     build_opts.add_option("-DPARTIAL_STORE_N0=" + support::cpp11::to_string(partial_store_n0));
264*c217d954SCole Faust     if(_has_pad_y)
265*c217d954SCole Faust     {
266*c217d954SCole Faust         build_opts.add_option_if(_reinterpret_input_as_3d, "-DREINTERPRET_INPUT_AS_3D");
267*c217d954SCole Faust         build_opts.add_option_if(_reinterpret_output_as_3d, "-DREINTERPRET_OUTPUT_AS_3D");
268*c217d954SCole Faust         build_opts.add_option_if(_reinterpret_input_as_3d || _reinterpret_output_as_3d, "-DHEIGHT_GEMM3D=" + support::cpp11::to_string(h_gemm_3d));
269*c217d954SCole Faust         build_opts.add_option_if(_reinterpret_input_as_3d || _reinterpret_output_as_3d, "-DDEPTH_GEMM3D=" + support::cpp11::to_string(d_gemm_3d));
270*c217d954SCole Faust     }
271*c217d954SCole Faust     // If post_ops are used, then we disable the use of gemm_info.activation_info
272*c217d954SCole Faust     if(gemm_info.post_ops.size() > 0)
273*c217d954SCole Faust     {
274*c217d954SCole Faust         post_op_utils.set_post_ops_cl_build_options(build_opts, gemm_info.post_ops);
275*c217d954SCole Faust     }
276*c217d954SCole Faust     else
277*c217d954SCole Faust     {
278*c217d954SCole Faust         build_opts.add_option_if(gemm_info.activation_info.enabled(), "-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(gemm_info.activation_info.activation())));
279*c217d954SCole Faust         build_opts.add_option_if(gemm_info.activation_info.enabled(), "-DA_VAL=" + float_to_string_with_full_precision(gemm_info.activation_info.a()));
280*c217d954SCole Faust         build_opts.add_option_if(gemm_info.activation_info.enabled(), "-DB_VAL=" + float_to_string_with_full_precision(gemm_info.activation_info.b()));
281*c217d954SCole Faust     }
282*c217d954SCole Faust 
283*c217d954SCole Faust     std::string kernel_name("gemm_mm_reshaped_only_rhs_");
284*c217d954SCole Faust     kernel_name += rhs_info.transpose ? "t" : "nt";
285*c217d954SCole Faust     kernel_name += rhs_info.export_to_cl_image ? "_texture" : "";
286*c217d954SCole Faust     post_op_utils.set_post_ops_cl_kernel_name(kernel_name, gemm_info.post_ops);
287*c217d954SCole Faust 
288*c217d954SCole Faust     // A macro guard to compile ONLY the kernel of interest
289*c217d954SCole Faust     build_opts.add_option("-D" + upper_string(kernel_name));
290*c217d954SCole Faust 
291*c217d954SCole Faust     // Create kernel
292*c217d954SCole Faust     _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
293*c217d954SCole Faust 
294*c217d954SCole Faust     // Set config_id for enabling LWS tuning
295*c217d954SCole Faust     _config_id = kernel_name;
296*c217d954SCole Faust     _config_id += "_";
297*c217d954SCole Faust     _config_id += (_has_pad_y ? "" : "no_pad_y_");
298*c217d954SCole Faust     _config_id += (_add_bias ? "add_bias_" : "");
299*c217d954SCole Faust     _config_id += (gemm_info.broadcast_bias ? "broadcast_bias_" : "");
300*c217d954SCole Faust     _config_id += (_reinterpret_input_as_3d ? "3di_" : "");
301*c217d954SCole Faust     _config_id += (_reinterpret_output_as_3d ? "3do_" : "");
302*c217d954SCole Faust     _config_id += (gemm_info.activation_info.enabled() ? "fused_activation_" : "");
303*c217d954SCole Faust     _config_id += lower_string(string_from_data_type(src0->data_type()));
304*c217d954SCole Faust     _config_id += "_";
305*c217d954SCole Faust     _config_id += support::cpp11::to_string(dst->dimension(1));
306*c217d954SCole Faust     _config_id += "_";
307*c217d954SCole Faust     _config_id += support::cpp11::to_string(dst->dimension(0));
308*c217d954SCole Faust     _config_id += "_";
309*c217d954SCole Faust     _config_id += support::cpp11::to_string(gemm_info.k);
310*c217d954SCole Faust     _config_id += "_";
311*c217d954SCole Faust     _config_id += support::cpp11::to_string(dst->dimension(2));
312*c217d954SCole Faust     _config_id += "_";
313*c217d954SCole Faust     _config_id += support::cpp11::to_string(lhs_info.m0);
314*c217d954SCole Faust     _config_id += "_";
315*c217d954SCole Faust     _config_id += support::cpp11::to_string(rhs_info.n0);
316*c217d954SCole Faust     _config_id += "_";
317*c217d954SCole Faust     _config_id += support::cpp11::to_string(rhs_info.k0);
318*c217d954SCole Faust     _config_id += "_";
319*c217d954SCole Faust     _config_id += support::cpp11::to_string(rhs_info.h0);
320*c217d954SCole Faust     _config_id += "_";
321*c217d954SCole Faust     _config_id += support::cpp11::to_string(rhs_info.interleave);
322*c217d954SCole Faust 
323*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
324*c217d954SCole Faust }
325*c217d954SCole Faust 
validate(const ITensorInfo * src0,const ITensorInfo * src1,const ITensorInfo * src2,const ITensorInfo * dst,float alpha,float beta,const GEMMLHSMatrixInfo & lhs_info,const GEMMRHSMatrixInfo & rhs_info,const GEMMKernelInfo & gemm_info)326*c217d954SCole Faust Status ClGemmMatrixMultiplyReshapedOnlyRhsKernel::validate(const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, float alpha, float beta,
327*c217d954SCole Faust                                                            const GEMMLHSMatrixInfo &lhs_info,
328*c217d954SCole Faust                                                            const GEMMRHSMatrixInfo &rhs_info, const GEMMKernelInfo &gemm_info)
329*c217d954SCole Faust {
330*c217d954SCole Faust     ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src0, src1, src2, dst, alpha, beta, lhs_info, rhs_info, gemm_info));
331*c217d954SCole Faust     return Status{};
332*c217d954SCole Faust }
333*c217d954SCole Faust 
run_op(ITensorPack & tensors,const Window & window,cl::CommandQueue & queue)334*c217d954SCole Faust void ClGemmMatrixMultiplyReshapedOnlyRhsKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
335*c217d954SCole Faust {
336*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
337*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
338*c217d954SCole Faust 
339*c217d954SCole Faust     const auto src0 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
340*c217d954SCole Faust     const auto src1 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
341*c217d954SCole Faust     const auto src2 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_2));
342*c217d954SCole Faust     auto       dst  = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
343*c217d954SCole Faust 
344*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON_NULLPTR(src0, src1, dst);
345*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON(_add_bias && src2 == nullptr);
346*c217d954SCole Faust 
347*c217d954SCole Faust     if(src1->info()->num_dimensions() < 3)
348*c217d954SCole Faust     {
349*c217d954SCole Faust         // The stride_z for matrix B must be zero if we do not slice
350*c217d954SCole Faust         ARM_COMPUTE_ERROR_ON(src1->info()->strides_in_bytes()[3] != 0);
351*c217d954SCole Faust     }
352*c217d954SCole Faust 
353*c217d954SCole Faust     const size_t lhs_idx_batch_size = _reinterpret_input_as_3d && !_has_pad_y ? 3u : 2u;
354*c217d954SCole Faust     const size_t rhs_idx_batch_size = 2u;
355*c217d954SCole Faust     const size_t bia_idx_batch_size = 2u;
356*c217d954SCole Faust     const size_t out_idx_batch_size = _reinterpret_output_as_3d && !_has_pad_y ? 3u : 2u;
357*c217d954SCole Faust 
358*c217d954SCole Faust     Window slice          = window.first_slice_window_3D();
359*c217d954SCole Faust     Window slice_matrix_b = slice;
360*c217d954SCole Faust 
361*c217d954SCole Faust     slice_matrix_b.set(Window::DimX, Window::Dimension(0, 1, 1));
362*c217d954SCole Faust     slice_matrix_b.set(Window::DimY, Window::Dimension(0, 1, 1));
363*c217d954SCole Faust 
364*c217d954SCole Faust     // Get cross plane pads
365*c217d954SCole Faust     const unsigned int total_cross_plane_pad_lhs = src0->info()->padding().top + src0->info()->padding().bottom;
366*c217d954SCole Faust     const unsigned int total_cross_plane_pad_out = dst->info()->padding().top + dst->info()->padding().bottom;
367*c217d954SCole Faust 
368*c217d954SCole Faust     // The execution should fail if we try to run with has_pad_y = false but we have padding in either the LHS or DST tensor
369*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON(!_has_pad_y && ((total_cross_plane_pad_lhs != 0) || (total_cross_plane_pad_out != 0)));
370*c217d954SCole Faust 
371*c217d954SCole Faust     cl::Image2D src1_image2d;
372*c217d954SCole Faust 
373*c217d954SCole Faust     if(_export_to_cl_image)
374*c217d954SCole Faust     {
375*c217d954SCole Faust         const TensorShape shape2d(src1->info()->dimension(0) / 4, src1->info()->dimension(1) * src1->info()->dimension(2));
376*c217d954SCole Faust         const size_t      image_row_pitch = src1->info()->strides_in_bytes()[1];
377*c217d954SCole Faust 
378*c217d954SCole Faust         src1_image2d = create_image2d_from_buffer(CLKernelLibrary::get().context(), src1->cl_buffer(), shape2d, src1->info()->data_type(), image_row_pitch, CLImage2DType::ReadOnly);
379*c217d954SCole Faust     }
380*c217d954SCole Faust 
381*c217d954SCole Faust     do
382*c217d954SCole Faust     {
383*c217d954SCole Faust         Window slice_b = slice;
384*c217d954SCole Faust         // Don't slice matrix B along the z dimension if matrix B has just 2 dimensions and matrix A more than 2
385*c217d954SCole Faust         // This scenario can happen when the matrix multiplication is used to perform a convolution operation
386*c217d954SCole Faust         if(!_slide_matrix_b)
387*c217d954SCole Faust         {
388*c217d954SCole Faust             slice_b = slice_matrix_b;
389*c217d954SCole Faust         }
390*c217d954SCole Faust 
391*c217d954SCole Faust         unsigned int idx = 0;
392*c217d954SCole Faust 
393*c217d954SCole Faust         // LHS buffer
394*c217d954SCole Faust         add_2D_tensor_argument(idx, src0, slice);
395*c217d954SCole Faust 
396*c217d954SCole Faust         // RHS buffer or RHS OpenCL image (_export_to_cl_image == true)
397*c217d954SCole Faust         if(_export_to_cl_image)
398*c217d954SCole Faust         {
399*c217d954SCole Faust             _kernel.setArg(idx++, src1_image2d);
400*c217d954SCole Faust         }
401*c217d954SCole Faust         else
402*c217d954SCole Faust         {
403*c217d954SCole Faust             add_2D_tensor_argument(idx, src1, slice_b);
404*c217d954SCole Faust         }
405*c217d954SCole Faust 
406*c217d954SCole Faust         // Bias buffer (_add_bias == true)
407*c217d954SCole Faust         add_2D_tensor_argument_if(_add_bias, idx, src2, slice);
408*c217d954SCole Faust 
409*c217d954SCole Faust         // dst buffer
410*c217d954SCole Faust         add_2D_tensor_argument(idx, dst, slice);
411*c217d954SCole Faust 
412*c217d954SCole Faust         // post op argument buffers
413*c217d954SCole Faust         for(size_t i = 0; i < _num_post_op_args; ++i)
414*c217d954SCole Faust         {
415*c217d954SCole Faust             const auto post_op_arg = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(experimental::get_post_op_arg_type(i)));
416*c217d954SCole Faust             add_2D_tensor_argument(idx, post_op_arg, slice);
417*c217d954SCole Faust         }
418*c217d954SCole Faust 
419*c217d954SCole Faust         // LHS stride_z
420*c217d954SCole Faust         _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(src0->info()->strides_in_bytes()[lhs_idx_batch_size]));
421*c217d954SCole Faust 
422*c217d954SCole Faust         // RHS stride_z (not used if _export_to_cl_image == true)
423*c217d954SCole Faust         _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(src1->info()->strides_in_bytes()[rhs_idx_batch_size]));
424*c217d954SCole Faust 
425*c217d954SCole Faust         // Bias stride_z (if _add_bias == true)
426*c217d954SCole Faust         if(_add_bias)
427*c217d954SCole Faust         {
428*c217d954SCole Faust             _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(src2->info()->strides_in_bytes()[bia_idx_batch_size]));
429*c217d954SCole Faust         }
430*c217d954SCole Faust 
431*c217d954SCole Faust         // dst stride_z
432*c217d954SCole Faust         _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(dst->info()->strides_in_bytes()[out_idx_batch_size]));
433*c217d954SCole Faust         // post op argument stride_z
434*c217d954SCole Faust         for(size_t i = 0; i < _num_post_op_args; ++i)
435*c217d954SCole Faust         {
436*c217d954SCole Faust             const auto post_op_arg = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(experimental::get_post_op_arg_type(i)));
437*c217d954SCole Faust             _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(post_op_arg->info()->strides_in_bytes()[2]));
438*c217d954SCole Faust         }
439*c217d954SCole Faust 
440*c217d954SCole Faust         // Cross-plan padding (if _reinterpret_input_as_3d = true)
441*c217d954SCole Faust         if(_reinterpret_input_as_3d && _has_pad_y)
442*c217d954SCole Faust         {
443*c217d954SCole Faust             _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(total_cross_plane_pad_lhs));
444*c217d954SCole Faust         }
445*c217d954SCole Faust 
446*c217d954SCole Faust         // Cross-plan padding (if reinterpret_output_as_3d = true)
447*c217d954SCole Faust         if(_reinterpret_output_as_3d && _has_pad_y)
448*c217d954SCole Faust         {
449*c217d954SCole Faust             _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(total_cross_plane_pad_out));
450*c217d954SCole Faust         }
451*c217d954SCole Faust 
452*c217d954SCole Faust         // 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.
453*c217d954SCole Faust         _kernel.setArg<cl_int>(idx++, _m);
454*c217d954SCole Faust         _kernel.setArg<cl_int>(idx++, _n);
455*c217d954SCole Faust         _kernel.setArg<cl_int>(idx++, _k);
456*c217d954SCole Faust 
457*c217d954SCole Faust         enqueue(queue, *this, slice, lws_hint(), _use_dummy_work_items);
458*c217d954SCole Faust     }
459*c217d954SCole Faust     while(window.slide_window_slice_3D(slice));
460*c217d954SCole Faust }
461*c217d954SCole Faust } // namespace kernels
462*c217d954SCole Faust } // namespace opencl
463*c217d954SCole Faust } // namespace arm_compute
464