blob: 1df0ca0410fccc72f2bfeb419c36d9d1f0900e72 [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 Bayir2ad0a6b2023-09-19 15:37:38 +010080 // Validate ExportToCLImage
81 ARM_COMPUTE_RETURN_ERROR_ON_MSG(matmul_kernel_info.export_rhs_to_cl_image, "Export to CLImage is not supported!");
82
Gunes Bayire87fa662023-09-07 12:20:33 +010083 return Status{};
84}
85} // namespace
86
87ClMatMulLowpNativeMMULKernel::ClMatMulLowpNativeMMULKernel()
88{
89 _type = CLKernelType::GEMM;
90}
91
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010092Status ClMatMulLowpNativeMMULKernel::validate(const ITensorInfo *lhs,
93 const ITensorInfo *rhs,
94 const ITensorInfo *bias,
95 const ITensorInfo *dst,
96 const MatMulKernelInfo &matmul_kernel_info,
Gunes Bayire87fa662023-09-07 12:20:33 +010097 const ActivationLayerInfo &act_info)
98{
99 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lhs, rhs, dst);
Gunes Bayira116cd32023-09-13 11:59:34 +0100100 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!arm_matrix_multiply_supported(CLKernelLibrary::get().get_device()),
101 "The extension cl_arm_matrix_multiply is not supported on the target platform");
Gunes Bayire87fa662023-09-07 12:20:33 +0100102 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(lhs, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED);
103 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lhs, rhs);
104 ARM_COMPUTE_RETURN_ON_ERROR(validate_matmul_kernel_info(matmul_kernel_info));
Gunes Bayire87fa662023-09-07 12:20:33 +0100105
Gunes Bayira116cd32023-09-13 11:59:34 +0100106 const TensorShape &lhs_shape = lhs->tensor_shape();
107 ARM_COMPUTE_RETURN_ON_ERROR(validate_matmul_input_shapes(lhs_shape, rhs->tensor_shape(), matmul_kernel_info));
108
109 const size_t lhs_k = matmul_kernel_info.adj_lhs ? lhs_shape.y() : lhs_shape.x();
110 ARM_COMPUTE_RETURN_ERROR_ON_MSG_VAR((lhs_k % mmul_k0) != 0, "K dimension must be a multiple of %d", mmul_k0);
111
112 ARM_COMPUTE_RETURN_ERROR_ON_MSG((act_info.activation() != ActivationFunction::IDENTITY),
Gunes Bayire87fa662023-09-07 12:20:33 +0100113 "Activation Function specified is unsupported.");
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100114 const TensorShape expected_output_shape =
115 misc::shape_calculator::compute_matmul_shape(lhs_shape, rhs->tensor_shape(), matmul_kernel_info);
Gunes Bayire87fa662023-09-07 12:20:33 +0100116
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100117 if (dst->total_size() != 0)
Gunes Bayire87fa662023-09-07 12:20:33 +0100118 {
119 const TensorInfo tensor_info_output = dst->clone()->set_tensor_shape(expected_output_shape);
120 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(dst, &tensor_info_output);
121 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lhs, dst);
122 }
123
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100124 if (bias != nullptr)
Gunes Bayire87fa662023-09-07 12:20:33 +0100125 {
126 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(bias, 1, DataType::S32);
127 ARM_COMPUTE_RETURN_ERROR_ON(bias->num_dimensions() > 1);
128 ARM_COMPUTE_RETURN_ERROR_ON(expected_output_shape[0] != bias->dimension(0));
129 }
130
131 return Status{};
132}
133
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100134void ClMatMulLowpNativeMMULKernel::configure(const ClCompileContext &compile_context,
135 ITensorInfo *lhs,
136 ITensorInfo *rhs,
137 ITensorInfo *bias,
138 ITensorInfo *dst,
139 const MatMulKernelInfo &matmul_kernel_info,
140 const ActivationLayerInfo &act_info)
Gunes Bayire87fa662023-09-07 12:20:33 +0100141{
142 ARM_COMPUTE_ERROR_ON_NULLPTR(lhs, rhs, dst);
143 ARM_COMPUTE_LOG_PARAMS(lhs, rhs, bias, dst, matmul_kernel_info, act_info);
144 ARM_COMPUTE_ERROR_THROW_ON(validate(lhs, rhs, bias, dst, matmul_kernel_info));
145
146 // dst tensor auto initialization if not yet initialized
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100147 auto_init_if_empty(*dst, lhs->clone()->set_tensor_shape(misc::shape_calculator::compute_matmul_shape(
148 lhs->tensor_shape(), rhs->tensor_shape(), matmul_kernel_info)));
Gunes Bayire87fa662023-09-07 12:20:33 +0100149
150 ARM_COMPUTE_UNUSED(compile_context, lhs, rhs, bias, matmul_kernel_info, act_info);
Gunes Bayira116cd32023-09-13 11:59:34 +0100151 CLBuildOptions build_opts;
152
153 const int m = dst->dimension(1);
154 const int n = dst->dimension(0);
155 const int k = matmul_kernel_info.adj_lhs ? lhs->tensor_shape().y() : lhs->tensor_shape().x();
156
157 const int m0 = std::min(matmul_kernel_info.m0, m);
158 const int n0 = adjust_vec_size(matmul_kernel_info.n0, n);
159
160 // Calculate partial (store instead of load) M0 and partial N0 for the partial blocks
161 // at the end of a row/column if any. This is to avoid padding.
162 const unsigned int m0_leftover = m % m0;
163 const unsigned int n0_leftover = n % n0;
Gunes Bayire87fa662023-09-07 12:20:33 +0100164
165 // Configure kernel window
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100166 const auto win_config =
167 validate_and_configure_window_for_mmul_kernels(lhs, rhs, dst, matmul_kernel_info, mmul_m0, mmul_n0);
Gunes Bayire87fa662023-09-07 12:20:33 +0100168 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
169 IClKernel::configure_internal(win_config.second);
170
Gunes Bayira116cd32023-09-13 11:59:34 +0100171 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(lhs->data_type()));
172 build_opts.add_option("-DM=" + support::cpp11::to_string(m));
173 build_opts.add_option("-DN=" + support::cpp11::to_string(n));
174 build_opts.add_option("-DK=" + support::cpp11::to_string(k));
175 build_opts.add_option("-DM0=" + support::cpp11::to_string(m0));
176 build_opts.add_option("-DN0=" + support::cpp11::to_string(n0));
177 build_opts.add_option("-DK0=" + support::cpp11::to_string(matmul_kernel_info.k0));
178 build_opts.add_option("-DM0_LEFTOVER=" + support::cpp11::to_string(m0_leftover));
179 build_opts.add_option("-DN0_LEFTOVER=" + support::cpp11::to_string(n0_leftover));
180 build_opts.add_option("-DMMUL_M0=" + support::cpp11::to_string(mmul_m0));
181 build_opts.add_option("-DMMUL_N0=" + support::cpp11::to_string(mmul_n0));
182 build_opts.add_option("-DMMUL_K0=" + support::cpp11::to_string(mmul_k0));
183 build_opts.add_option_if(bias != nullptr, "-DBIAS");
184
185 const UniformQuantizationInfo lqinfo = lhs->quantization_info().uniform();
186 const UniformQuantizationInfo rqinfo = rhs->quantization_info().uniform();
187 const UniformQuantizationInfo dqinfo = dst->quantization_info().uniform();
188
189 float multiplier = lqinfo.scale * rqinfo.scale / dqinfo.scale;
190 int output_multiplier = 0;
191 int output_shift = 0;
192 arm_compute::quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift);
193
194 build_opts.add_option("-DDST_MULTIPLIER=" + support::cpp11::to_string(output_multiplier));
195 build_opts.add_option("-DDST_SHIFT=" + support::cpp11::to_string(output_shift));
196
197 // Note : Offset is not negated, unlike gemmlowp kernels
198 build_opts.add_option("-DLHS_OFFSET=" + support::cpp11::to_string(lqinfo.offset));
199 build_opts.add_option("-DRHS_OFFSET=" + support::cpp11::to_string(rqinfo.offset));
200 build_opts.add_option("-DDST_OFFSET=" + support::cpp11::to_string(dqinfo.offset));
Gunes Bayire87fa662023-09-07 12:20:33 +0100201
202 std::string kernel_name("mat_mul_native_quantized_mmul");
203 kernel_name += matmul_kernel_info.adj_lhs ? "_t" : "_nt";
204 kernel_name += matmul_kernel_info.adj_rhs ? "_t" : "_nt";
205
206 // A macro guard to compile ONLY the kernel of interest
207 build_opts.add_option("-D" + upper_string(kernel_name));
208
209 // Create kernel
210 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
211
Gunes Bayira116cd32023-09-13 11:59:34 +0100212 // Set config_id for enabling LWS tuning
213 _config_id = kernel_name;
214 _config_id += "_";
215 _config_id += lower_string(string_from_data_type(lhs->data_type()));
216 _config_id += "_";
217 _config_id += support::cpp11::to_string(m);
218 _config_id += "_";
219 _config_id += support::cpp11::to_string(n);
220 _config_id += "_";
221 _config_id += support::cpp11::to_string(k);
222 _config_id += "_";
223 _config_id += support::cpp11::to_string(dst->dimension(2));
224 _config_id += "_";
225 _config_id += support::cpp11::to_string(m0);
226 _config_id += "_";
227 _config_id += support::cpp11::to_string(n0);
Gunes Bayire87fa662023-09-07 12:20:33 +0100228}
229
230void ClMatMulLowpNativeMMULKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
231{
232 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
233 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
234
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100235 const auto *lhs =
236 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
237 const auto *rhs =
238 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
239 const auto *bias = utils::cast::polymorphic_downcast<const ICLTensor *>(
240 tensors.get_const_tensor(TensorType::ACL_SRC_2)); // nullptr if bias is not present
241 auto *dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
Gunes Bayire87fa662023-09-07 12:20:33 +0100242
243 ARM_COMPUTE_ERROR_ON_NULLPTR(lhs, rhs, dst);
244 ARM_COMPUTE_LOG_PARAMS(lhs, rhs, bias, dst);
245
246 unsigned int idx = 0;
247 add_3d_tensor_nhw_argument(idx, lhs);
248 add_3d_tensor_nhw_argument(idx, rhs);
249
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100250 if (bias != nullptr)
Gunes Bayire87fa662023-09-07 12:20:33 +0100251 {
252 add_3d_tensor_nhw_argument(idx, bias);
253 }
254 add_3d_tensor_nhw_argument(idx, dst);
255
256 // LWS_x should be multiple of 16 at least. (32, 2) has been chosen to have more work-items on a single core
257 // LWS also enforces the order of execution of the work items which improves cache utilization
258 enqueue(queue, *this, window, cl::NDRange(32, 2), false);
259}
260
261} // namespace kernels
262} // namespace opencl
263} // namespace arm_compute