Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 1 | /* |
Gian Marco Iodice | 10e88a7 | 2021-11-29 12:49:19 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2019-2022 Arm Limited. |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 3 | * |
| 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 Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 24 | #include "src/gpu/cl/kernels/ClGemmMatrixMultiplyReshapedOnlyRhsKernel.h" |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/CL/ICLTensor.h" |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 28 | #include "src/core/CL/CLUtils.h" |
| 29 | #include "src/core/CL/CLValidate.h" |
SiCongLi | 3177861 | 2021-11-12 17:33:45 +0000 | [diff] [blame] | 30 | #include "src/core/experimental/PostOpUtils.h" |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 31 | #include "src/core/helpers/AutoConfiguration.h" |
| 32 | #include "src/core/helpers/WindowHelpers.h" |
| 33 | #include "src/core/utils/helpers/float_ops.h" |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 34 | #include "src/gpu/cl/kernels/gemm/ClGemmHelpers.h" |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 35 | #include "support/Cast.h" |
| 36 | #include "support/StringSupport.h" |
| 37 | |
| 38 | namespace arm_compute |
| 39 | { |
| 40 | namespace opencl |
| 41 | { |
| 42 | namespace kernels |
| 43 | { |
| 44 | namespace |
| 45 | { |
| 46 | using ElementsProcessed = Steps; |
| 47 | |
SiCongLi | afa1972 | 2021-10-24 19:12:33 +0100 | [diff] [blame] | 48 | const auto post_op_utils = experimental::PostOpCLKernelUtils( |
| 49 | { |
| 50 | // PostOp sequence -> {Kernel Postfix, PostOp Slots} |
| 51 | { {}, { "", {} } }, |
| 52 | { { experimental::PostOpType::Activation }, { "", { 1 } } }, |
ramelg01 | 6049eda | 2021-10-29 10:52:53 +0100 | [diff] [blame] | 53 | |
SiCongLi | afa1972 | 2021-10-24 19:12:33 +0100 | [diff] [blame] | 54 | { { experimental::PostOpType::Eltwise_Add }, { "_post_act_eltwise_op_act", { 2 } } }, |
ramelg01 | 6049eda | 2021-10-29 10:52:53 +0100 | [diff] [blame] | 55 | { { experimental::PostOpType::Eltwise_PRelu }, { "_post_act_eltwise_op_act", { 2 } } }, |
| 56 | |
SiCongLi | afa1972 | 2021-10-24 19:12:33 +0100 | [diff] [blame] | 57 | { { experimental::PostOpType::Activation, experimental::PostOpType::Eltwise_Add }, { "_post_act_eltwise_op_act", { 1, 2 } } }, |
ramelg01 | 6049eda | 2021-10-29 10:52:53 +0100 | [diff] [blame] | 58 | { { experimental::PostOpType::Activation, experimental::PostOpType::Eltwise_PRelu }, { "_post_act_eltwise_op_act", { 1, 2 } } }, |
| 59 | |
SiCongLi | afa1972 | 2021-10-24 19:12:33 +0100 | [diff] [blame] | 60 | { { experimental::PostOpType::Eltwise_Add, experimental::PostOpType::Activation }, { "_post_act_eltwise_op_act", { 2, 3 } } }, |
ramelg01 | 6049eda | 2021-10-29 10:52:53 +0100 | [diff] [blame] | 61 | { { 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 } } } |
SiCongLi | afa1972 | 2021-10-24 19:12:33 +0100 | [diff] [blame] | 65 | }); |
| 66 | |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 67 | Status 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 Iodice | 10e88a7 | 2021-11-29 12:49:19 +0000 | [diff] [blame^] | 86 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.has_pad_y, "Tensors cannot have padding along the Y direction"); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 87 | ARM_COMPUTE_RETURN_ON_ERROR(gemm::validate_image2d_support_on_rhs(*src1, rhs_info)); |
SiCongLi | afa1972 | 2021-10-24 19:12:33 +0100 | [diff] [blame] | 88 | 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 Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 89 | |
| 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); |
SiCongLi | afa1972 | 2021-10-24 19:12:33 +0100 | [diff] [blame] | 134 | 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 Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | return Status{}; |
| 138 | } |
| 139 | |
Giorgio Arena | 951d520 | 2021-09-08 13:26:06 +0100 | [diff] [blame] | 140 | Window 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 Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 142 | { |
Giorgio Arena | 951d520 | 2021-09-08 13:26:06 +0100 | [diff] [blame] | 143 | ARM_COMPUTE_UNUSED(src0, src1, src2); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 144 | 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 Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 146 | bool reinterpret_output_as_3d = gemm_info.depth_output_gemm3d != 0; |
| 147 | |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 148 | 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 Arena | 951d520 | 2021-09-08 13:26:06 +0100 | [diff] [blame] | 163 | Window win = calculate_max_window(tmp_info, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y)); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 164 | |
| 165 | // Collapse along the Z direction |
| 166 | // This collapse needs to be here in order to tune the Z dimension of LWS |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 167 | const unsigned int dimension_to_collapse = std::min(static_cast<unsigned int>(dst->num_dimensions()), 2u); |
Giorgio Arena | 951d520 | 2021-09-08 13:26:06 +0100 | [diff] [blame] | 168 | Window collapsed = win.collapse(win, dimension_to_collapse); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 169 | |
Giorgio Arena | 951d520 | 2021-09-08 13:26:06 +0100 | [diff] [blame] | 170 | return collapsed; |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 171 | } |
| 172 | } // namespace |
| 173 | |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 174 | ClGemmMatrixMultiplyReshapedOnlyRhsKernel::ClGemmMatrixMultiplyReshapedOnlyRhsKernel() |
| 175 | { |
| 176 | _type = CLKernelType::GEMM; |
| 177 | } |
| 178 | |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 179 | void ClGemmMatrixMultiplyReshapedOnlyRhsKernel::configure(const CLCompileContext &compile_context, |
Giorgio Arena | 951d520 | 2021-09-08 13:26:06 +0100 | [diff] [blame] | 180 | const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *src2, ITensorInfo *dst, float alpha, float beta, |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 181 | const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, const GEMMKernelInfo &gemm_info) |
| 182 | { |
| 183 | ARM_COMPUTE_ERROR_ON_NULLPTR(src0, src1, dst); |
| 184 | |
SiCongLi | afa1972 | 2021-10-24 19:12:33 +0100 | [diff] [blame] | 185 | // 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 Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 188 | 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; |
SiCongLi | afa1972 | 2021-10-24 19:12:33 +0100 | [diff] [blame] | 195 | _num_post_op_args = gemm_info.post_ops.total_num_arguments(); |
Giorgio Arena | 951d520 | 2021-09-08 13:26:06 +0100 | [diff] [blame] | 196 | |
| 197 | auto padding_info = get_padding_info({ src0, src1, src2, dst }); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 198 | |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 199 | // 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 Arena | 951d520 | 2021-09-08 13:26:06 +0100 | [diff] [blame] | 206 | 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 Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 209 | |
| 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 Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 215 | // 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; |
ramelg01 | 9cca592 | 2021-11-11 10:05:00 +0000 | [diff] [blame] | 222 | _m = internal_m; |
| 223 | _n = gemm_info.n; |
| 224 | _k = gemm_info.k; |
Gian Marco Iodice | 10e88a7 | 2021-11-29 12:49:19 +0000 | [diff] [blame^] | 225 | |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 226 | // Create build options |
| 227 | CLBuildOptions build_opts; |
| 228 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(src0->data_type())); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 229 | 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 Iodice | 10e88a7 | 2021-11-29 12:49:19 +0000 | [diff] [blame^] | 235 | 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 | |
SiCongLi | afa1972 | 2021-10-24 19:12:33 +0100 | [diff] [blame] | 245 | // 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 Iodice | 10e88a7 | 2021-11-29 12:49:19 +0000 | [diff] [blame^] | 252 | 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())); |
SiCongLi | afa1972 | 2021-10-24 19:12:33 +0100 | [diff] [blame] | 256 | } |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 257 | |
| 258 | std::string kernel_name("gemm_mm_reshaped_only_rhs_"); |
| 259 | kernel_name += rhs_info.transpose ? "t" : "nt"; |
Gian Marco Iodice | 10e88a7 | 2021-11-29 12:49:19 +0000 | [diff] [blame^] | 260 | kernel_name += _export_to_cl_image ? "_texture" : ""; |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 261 | |
ramelg01 | 9cca592 | 2021-11-11 10:05:00 +0000 | [diff] [blame] | 262 | // A macro guard to compile ONLY the kernel of interest |
| 263 | build_opts.add_option("-D" + upper_string(kernel_name)); |
| 264 | |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 265 | // 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 Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 271 | _config_id += (_add_bias ? "add_bias_" : ""); |
| 272 | _config_id += (gemm_info.broadcast_bias ? "broadcast_bias_" : ""); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 273 | _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 | |
| 297 | Status 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 Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 301 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src0, src1, src2, dst, alpha, beta, lhs_info, rhs_info, gemm_info)); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 302 | return Status{}; |
| 303 | } |
| 304 | |
| 305 | void 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 Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 324 | 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 Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 330 | 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 Iodice | 10e88a7 | 2021-11-29 12:49:19 +0000 | [diff] [blame^] | 353 | add_3d_tensor_nhw_argument(idx, src0); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 354 | |
| 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 Iodice | 10e88a7 | 2021-11-29 12:49:19 +0000 | [diff] [blame^] | 360 | add_3d_tensor_nhw_argument(idx, src1); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 361 | |
| 362 | // Bias buffer (_add_bias == true) |
Gian Marco Iodice | 10e88a7 | 2021-11-29 12:49:19 +0000 | [diff] [blame^] | 363 | if(_add_bias) |
| 364 | { |
| 365 | add_3d_tensor_nhw_argument(idx, src2); |
| 366 | } |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 367 | |
SiCongLi | afa1972 | 2021-10-24 19:12:33 +0100 | [diff] [blame] | 368 | // 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 Iodice | 10e88a7 | 2021-11-29 12:49:19 +0000 | [diff] [blame^] | 372 | add_3d_tensor_nhw_argument(idx, post_op_arg); |
SiCongLi | afa1972 | 2021-10-24 19:12:33 +0100 | [diff] [blame] | 373 | } |
| 374 | |
Gian Marco Iodice | 10e88a7 | 2021-11-29 12:49:19 +0000 | [diff] [blame^] | 375 | // dst buffer |
| 376 | add_3d_tensor_nhw_argument(idx, dst); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 377 | |
ramelg01 | 9cca592 | 2021-11-11 10:05:00 +0000 | [diff] [blame] | 378 | // 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 Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 383 | 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 |