blob: 94e3c4e47bcc5a05b363377f7992542334cb0fe0 [file] [log] [blame]
Gunes Bayire87fa662023-09-07 12:20:33 +01001/*
2 * Copyright (c) 2023 Arm Limited.
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 */
24#include "src/gpu/cl/kernels/ClMatMulLowpNativeMMULKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/ICLTensor.h"
28#include "arm_compute/core/Error.h"
29#include "arm_compute/core/ITensorPack.h"
30#include "arm_compute/core/TensorInfo.h"
Gunes Bayira116cd32023-09-13 11:59:34 +010031#include "arm_compute/core/utils/helpers/AdjustVecSize.h"
Gunes Bayire87fa662023-09-07 12:20:33 +010032#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Gunes Bayira116cd32023-09-13 11:59:34 +010033#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010034#include "arm_compute/core/utils/StringUtils.h"
Gunes Bayire87fa662023-09-07 12:20:33 +010035
36#include "src/common/utils/Log.h"
37#include "src/core/helpers/AutoConfiguration.h"
Gunes Bayire87fa662023-09-07 12:20:33 +010038#include "src/gpu/cl/ClCompileContext.h"
39#include "src/gpu/cl/kernels/helpers/MatMulKernelHelpers.h"
40#include "support/Cast.h"
41#include "support/StringSupport.h"
Gunes Bayira116cd32023-09-13 11:59:34 +010042#include "utils/TypePrinter.h"
Gunes Bayire87fa662023-09-07 12:20:33 +010043
44namespace arm_compute
45{
46namespace opencl
47{
48namespace kernels
49{
50namespace
51{
52// Block size dimensions for the MMUL extension
53constexpr int mmul_m0 = 4;
54constexpr int mmul_n0 = 4;
55constexpr int mmul_k0 = 16;
56
57Status validate_matmul_kernel_info(const MatMulKernelInfo &matmul_kernel_info)
58{
Gunes Bayira116cd32023-09-13 11:59:34 +010059 const bool adj_lhs = matmul_kernel_info.adj_lhs;
60 const int m0 = matmul_kernel_info.m0;
61 const int n0 = matmul_kernel_info.n0;
62 const int k0 = matmul_kernel_info.k0;
63
64 // Validate M0
65 ARM_COMPUTE_RETURN_ERROR_ON_MSG(m0 < 1, "Only positive integers are supported for M0");
66
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010067 if (adj_lhs)
Gunes Bayira116cd32023-09-13 11:59:34 +010068 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010069 ARM_COMPUTE_RETURN_ERROR_ON_MSG((m0 != 1) && (m0 != 2) && (m0 != 3) && (m0 != 4) && (m0 != 8) && (m0 != 16),
70 "Only 1,2,3,4,8,16 are supported for M0 for Lhs transposed");
Gunes Bayira116cd32023-09-13 11:59:34 +010071 }
72
73 // Validate N0
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010074 ARM_COMPUTE_RETURN_ERROR_ON_MSG((n0 != 1) && (n0 != 2) && (n0 != 3) && (n0 != 4) && (n0 != 8) && (n0 != 16),
75 "Only 1,2,3,4,8,16 are supported for N0");
Gunes Bayira116cd32023-09-13 11:59:34 +010076
77 // Validate K0
78 ARM_COMPUTE_RETURN_ERROR_ON_MSG((k0 != 4), "Only 4 is supported for k0");
79
Gunes Bayire87fa662023-09-07 12:20:33 +010080 return Status{};
81}
82} // namespace
83
84ClMatMulLowpNativeMMULKernel::ClMatMulLowpNativeMMULKernel()
85{
86 _type = CLKernelType::GEMM;
87}
88
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010089Status ClMatMulLowpNativeMMULKernel::validate(const ITensorInfo *lhs,
90 const ITensorInfo *rhs,
91 const ITensorInfo *bias,
92 const ITensorInfo *dst,
93 const MatMulKernelInfo &matmul_kernel_info,
Gunes Bayire87fa662023-09-07 12:20:33 +010094 const ActivationLayerInfo &act_info)
95{
96 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lhs, rhs, dst);
Gunes Bayira116cd32023-09-13 11:59:34 +010097 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!arm_matrix_multiply_supported(CLKernelLibrary::get().get_device()),
98 "The extension cl_arm_matrix_multiply is not supported on the target platform");
Gunes Bayire87fa662023-09-07 12:20:33 +010099 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(lhs, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED);
100 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lhs, rhs);
101 ARM_COMPUTE_RETURN_ON_ERROR(validate_matmul_kernel_info(matmul_kernel_info));
Gunes Bayire87fa662023-09-07 12:20:33 +0100102
Gunes Bayira116cd32023-09-13 11:59:34 +0100103 const TensorShape &lhs_shape = lhs->tensor_shape();
104 ARM_COMPUTE_RETURN_ON_ERROR(validate_matmul_input_shapes(lhs_shape, rhs->tensor_shape(), matmul_kernel_info));
105
106 const size_t lhs_k = matmul_kernel_info.adj_lhs ? lhs_shape.y() : lhs_shape.x();
107 ARM_COMPUTE_RETURN_ERROR_ON_MSG_VAR((lhs_k % mmul_k0) != 0, "K dimension must be a multiple of %d", mmul_k0);
108
109 ARM_COMPUTE_RETURN_ERROR_ON_MSG((act_info.activation() != ActivationFunction::IDENTITY),
Gunes Bayire87fa662023-09-07 12:20:33 +0100110 "Activation Function specified is unsupported.");
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100111 const TensorShape expected_output_shape =
112 misc::shape_calculator::compute_matmul_shape(lhs_shape, rhs->tensor_shape(), matmul_kernel_info);
Gunes Bayire87fa662023-09-07 12:20:33 +0100113
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100114 if (dst->total_size() != 0)
Gunes Bayire87fa662023-09-07 12:20:33 +0100115 {
116 const TensorInfo tensor_info_output = dst->clone()->set_tensor_shape(expected_output_shape);
117 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(dst, &tensor_info_output);
118 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lhs, dst);
119 }
120
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100121 if (bias != nullptr)
Gunes Bayire87fa662023-09-07 12:20:33 +0100122 {
123 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(bias, 1, DataType::S32);
124 ARM_COMPUTE_RETURN_ERROR_ON(bias->num_dimensions() > 1);
125 ARM_COMPUTE_RETURN_ERROR_ON(expected_output_shape[0] != bias->dimension(0));
126 }
127
128 return Status{};
129}
130
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100131void ClMatMulLowpNativeMMULKernel::configure(const ClCompileContext &compile_context,
132 ITensorInfo *lhs,
133 ITensorInfo *rhs,
134 ITensorInfo *bias,
135 ITensorInfo *dst,
136 const MatMulKernelInfo &matmul_kernel_info,
137 const ActivationLayerInfo &act_info)
Gunes Bayire87fa662023-09-07 12:20:33 +0100138{
139 ARM_COMPUTE_ERROR_ON_NULLPTR(lhs, rhs, dst);
140 ARM_COMPUTE_LOG_PARAMS(lhs, rhs, bias, dst, matmul_kernel_info, act_info);
141 ARM_COMPUTE_ERROR_THROW_ON(validate(lhs, rhs, bias, dst, matmul_kernel_info));
142
143 // dst tensor auto initialization if not yet initialized
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100144 auto_init_if_empty(*dst, lhs->clone()->set_tensor_shape(misc::shape_calculator::compute_matmul_shape(
145 lhs->tensor_shape(), rhs->tensor_shape(), matmul_kernel_info)));
Gunes Bayire87fa662023-09-07 12:20:33 +0100146
147 ARM_COMPUTE_UNUSED(compile_context, lhs, rhs, bias, matmul_kernel_info, act_info);
Gunes Bayira116cd32023-09-13 11:59:34 +0100148 CLBuildOptions build_opts;
149
150 const int m = dst->dimension(1);
151 const int n = dst->dimension(0);
152 const int k = matmul_kernel_info.adj_lhs ? lhs->tensor_shape().y() : lhs->tensor_shape().x();
153
154 const int m0 = std::min(matmul_kernel_info.m0, m);
155 const int n0 = adjust_vec_size(matmul_kernel_info.n0, n);
156
157 // Calculate partial (store instead of load) M0 and partial N0 for the partial blocks
158 // at the end of a row/column if any. This is to avoid padding.
159 const unsigned int m0_leftover = m % m0;
160 const unsigned int n0_leftover = n % n0;
Gunes Bayire87fa662023-09-07 12:20:33 +0100161
162 // Configure kernel window
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100163 const auto win_config =
164 validate_and_configure_window_for_mmul_kernels(lhs, rhs, dst, matmul_kernel_info, mmul_m0, mmul_n0);
Gunes Bayire87fa662023-09-07 12:20:33 +0100165 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
166 IClKernel::configure_internal(win_config.second);
167
Gunes Bayira116cd32023-09-13 11:59:34 +0100168 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(lhs->data_type()));
169 build_opts.add_option("-DM=" + support::cpp11::to_string(m));
170 build_opts.add_option("-DN=" + support::cpp11::to_string(n));
171 build_opts.add_option("-DK=" + support::cpp11::to_string(k));
172 build_opts.add_option("-DM0=" + support::cpp11::to_string(m0));
173 build_opts.add_option("-DN0=" + support::cpp11::to_string(n0));
174 build_opts.add_option("-DK0=" + support::cpp11::to_string(matmul_kernel_info.k0));
175 build_opts.add_option("-DM0_LEFTOVER=" + support::cpp11::to_string(m0_leftover));
176 build_opts.add_option("-DN0_LEFTOVER=" + support::cpp11::to_string(n0_leftover));
177 build_opts.add_option("-DMMUL_M0=" + support::cpp11::to_string(mmul_m0));
178 build_opts.add_option("-DMMUL_N0=" + support::cpp11::to_string(mmul_n0));
179 build_opts.add_option("-DMMUL_K0=" + support::cpp11::to_string(mmul_k0));
180 build_opts.add_option_if(bias != nullptr, "-DBIAS");
181
182 const UniformQuantizationInfo lqinfo = lhs->quantization_info().uniform();
183 const UniformQuantizationInfo rqinfo = rhs->quantization_info().uniform();
184 const UniformQuantizationInfo dqinfo = dst->quantization_info().uniform();
185
186 float multiplier = lqinfo.scale * rqinfo.scale / dqinfo.scale;
187 int output_multiplier = 0;
188 int output_shift = 0;
189 arm_compute::quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift);
190
191 build_opts.add_option("-DDST_MULTIPLIER=" + support::cpp11::to_string(output_multiplier));
192 build_opts.add_option("-DDST_SHIFT=" + support::cpp11::to_string(output_shift));
193
194 // Note : Offset is not negated, unlike gemmlowp kernels
195 build_opts.add_option("-DLHS_OFFSET=" + support::cpp11::to_string(lqinfo.offset));
196 build_opts.add_option("-DRHS_OFFSET=" + support::cpp11::to_string(rqinfo.offset));
197 build_opts.add_option("-DDST_OFFSET=" + support::cpp11::to_string(dqinfo.offset));
Gunes Bayire87fa662023-09-07 12:20:33 +0100198
199 std::string kernel_name("mat_mul_native_quantized_mmul");
200 kernel_name += matmul_kernel_info.adj_lhs ? "_t" : "_nt";
201 kernel_name += matmul_kernel_info.adj_rhs ? "_t" : "_nt";
202
203 // A macro guard to compile ONLY the kernel of interest
204 build_opts.add_option("-D" + upper_string(kernel_name));
205
206 // Create kernel
207 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
208
Gunes Bayira116cd32023-09-13 11:59:34 +0100209 // Set config_id for enabling LWS tuning
210 _config_id = kernel_name;
211 _config_id += "_";
212 _config_id += lower_string(string_from_data_type(lhs->data_type()));
213 _config_id += "_";
214 _config_id += support::cpp11::to_string(m);
215 _config_id += "_";
216 _config_id += support::cpp11::to_string(n);
217 _config_id += "_";
218 _config_id += support::cpp11::to_string(k);
219 _config_id += "_";
220 _config_id += support::cpp11::to_string(dst->dimension(2));
221 _config_id += "_";
222 _config_id += support::cpp11::to_string(m0);
223 _config_id += "_";
224 _config_id += support::cpp11::to_string(n0);
Gunes Bayire87fa662023-09-07 12:20:33 +0100225}
226
227void ClMatMulLowpNativeMMULKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
228{
229 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
230 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
231
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100232 const auto *lhs =
233 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
234 const auto *rhs =
235 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
236 const auto *bias = utils::cast::polymorphic_downcast<const ICLTensor *>(
237 tensors.get_const_tensor(TensorType::ACL_SRC_2)); // nullptr if bias is not present
238 auto *dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
Gunes Bayire87fa662023-09-07 12:20:33 +0100239
240 ARM_COMPUTE_ERROR_ON_NULLPTR(lhs, rhs, dst);
241 ARM_COMPUTE_LOG_PARAMS(lhs, rhs, bias, dst);
242
243 unsigned int idx = 0;
244 add_3d_tensor_nhw_argument(idx, lhs);
245 add_3d_tensor_nhw_argument(idx, rhs);
246
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100247 if (bias != nullptr)
Gunes Bayire87fa662023-09-07 12:20:33 +0100248 {
249 add_3d_tensor_nhw_argument(idx, bias);
250 }
251 add_3d_tensor_nhw_argument(idx, dst);
252
253 // LWS_x should be multiple of 16 at least. (32, 2) has been chosen to have more work-items on a single core
254 // LWS also enforces the order of execution of the work items which improves cache utilization
255 enqueue(queue, *this, window, cl::NDRange(32, 2), false);
256}
257
258} // namespace kernels
259} // namespace opencl
260} // namespace arm_compute