blob: 76bf846e745213d0c3be161dc8f78401ba28f210 [file] [log] [blame]
SiCong Lia8d80582023-05-19 14:23:37 +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/ClMatMulNativeMMULKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/ICLTensor.h"
28#include "arm_compute/core/ITensorPack.h"
29#include "arm_compute/core/KernelDescriptors.h"
30#include "arm_compute/core/TensorInfo.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000031#include "arm_compute/core/utils/helpers/AdjustVecSize.h"
SiCong Lia8d80582023-05-19 14:23:37 +010032#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010033#include "arm_compute/core/utils/StringUtils.h"
SiCong Lia8d80582023-05-19 14:23:37 +010034
35#include "src/common/utils/Log.h"
36#include "src/core/helpers/AutoConfiguration.h"
Gunes Bayire87fa662023-09-07 12:20:33 +010037#include "src/gpu/cl/kernels/helpers/MatMulKernelHelpers.h"
SiCong Lia8d80582023-05-19 14:23:37 +010038#include "support/Cast.h"
39#include "support/StringSupport.h"
40
41namespace arm_compute
42{
43namespace opencl
44{
45namespace kernels
46{
47namespace
48{
49// Block size dimensions for the MMUL extension
50constexpr int mmul_m0 = 4;
51constexpr int mmul_n0 = 4;
52constexpr int mmul_k0 = 4;
53
SiCong Lia8d80582023-05-19 14:23:37 +010054Status validate_matmul_kernel_info(const MatMulKernelInfo &matmul_kernel_info)
55{
56 const bool adj_lhs = matmul_kernel_info.adj_lhs;
Mohammed Suhail Munshi8e2dede2023-06-27 14:25:58 +010057 const int m0 = matmul_kernel_info.m0;
58 const int n0 = matmul_kernel_info.n0;
59 const int k0 = matmul_kernel_info.k0;
SiCong Lia8d80582023-05-19 14:23:37 +010060
SiCong Lia8d80582023-05-19 14:23:37 +010061 // Validate M0
62 ARM_COMPUTE_RETURN_ERROR_ON_MSG(m0 < 1, "Only positive integers are supported for M0");
63
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010064 if (adj_lhs)
Gunes Bayir00474e92023-06-19 21:33:51 +010065 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010066 ARM_COMPUTE_RETURN_ERROR_ON_MSG((m0 != 1) && (m0 != 2) && (m0 != 3) && (m0 != 4) && (m0 != 8) && (m0 != 16),
67 "Only 1,2,3,4,8,16 are supported for M0 for Lhs transposed");
Gunes Bayir00474e92023-06-19 21:33:51 +010068 }
69
SiCong Lia8d80582023-05-19 14:23:37 +010070 // Validate N0
71 ARM_COMPUTE_RETURN_ERROR_ON_MSG(n0 < 1, "Only positive integers are supported for N0");
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010072 ARM_COMPUTE_RETURN_ERROR_ON_MSG((n0 != 1) && (n0 != 2) && (n0 != 3) && (n0 != 4) && (n0 != 8) && (n0 != 16),
73 "Only 1,2,3,4,8,16 are supported for N0");
SiCong Lia8d80582023-05-19 14:23:37 +010074
75 // Validate K0
76 ARM_COMPUTE_RETURN_ERROR_ON_MSG((k0 != 1), "Only 1 is supported for k0");
77
78 return Status{};
79}
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010080} // namespace
SiCong Lia8d80582023-05-19 14:23:37 +010081ClMatMulNativeMMULKernel::ClMatMulNativeMMULKernel()
82{
83 _type = CLKernelType::GEMM;
84}
85
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010086Status ClMatMulNativeMMULKernel::validate(const ITensorInfo *lhs,
87 const ITensorInfo *rhs,
88 const ITensorInfo *bias,
89 const ITensorInfo *dst,
90 const MatMulKernelInfo &matmul_kernel_info)
SiCong Lia8d80582023-05-19 14:23:37 +010091{
92 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lhs, rhs, dst);
93 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(lhs, 1, DataType::F32, DataType::F16);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010094 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!arm_matrix_multiply_supported(CLKernelLibrary::get().get_device()),
95 "The extension cl_arm_matrix_multiply is not supported on the target platform");
SiCong Lia8d80582023-05-19 14:23:37 +010096 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lhs, rhs);
97 ARM_COMPUTE_RETURN_ON_ERROR(validate_matmul_kernel_info(matmul_kernel_info));
SiCong Lia8d80582023-05-19 14:23:37 +010098
Gunes Bayire87fa662023-09-07 12:20:33 +010099 const TensorShape &lhs_shape = lhs->tensor_shape();
100 ARM_COMPUTE_RETURN_ON_ERROR(validate_matmul_input_shapes(lhs_shape, rhs->tensor_shape(), matmul_kernel_info));
101
102 const size_t lhs_k = matmul_kernel_info.adj_lhs ? lhs_shape.y() : lhs_shape.x();
103 ARM_COMPUTE_RETURN_ERROR_ON_MSG_VAR((lhs_k % mmul_k0) != 0, "K dimension must be a multiple of %d", mmul_k0);
104
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100105 const TensorShape expected_output_shape =
106 misc::shape_calculator::compute_matmul_shape(lhs_shape, rhs->tensor_shape(), matmul_kernel_info);
Mohammed Suhail Munshi8e2dede2023-06-27 14:25:58 +0100107
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100108 if (dst->total_size() != 0)
SiCong Lia8d80582023-05-19 14:23:37 +0100109 {
Mohammed Suhail Munshi8e2dede2023-06-27 14:25:58 +0100110 const TensorInfo tensor_info_dst = dst->clone()->set_tensor_shape(expected_output_shape);
SiCong Lia8d80582023-05-19 14:23:37 +0100111 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(dst, &tensor_info_dst);
112 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lhs, dst);
113 }
114
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100115 if (bias != nullptr)
Mohammed Suhail Munshi8e2dede2023-06-27 14:25:58 +0100116 {
117 ARM_COMPUTE_RETURN_ERROR_ON_MSG((bias->num_dimensions() > 1), "Multi dimensional bias is unsupported.");
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100118 ARM_COMPUTE_RETURN_ERROR_ON_MSG(bias->dimension(0) != expected_output_shape[0],
119 "First dimension of bias and output tensors must match.");
Mohammed Suhail Munshi8e2dede2023-06-27 14:25:58 +0100120 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lhs, bias);
121 }
122
SiCong Lia8d80582023-05-19 14:23:37 +0100123 return Status{};
124}
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100125void ClMatMulNativeMMULKernel::configure(const ClCompileContext &compile_context,
126 ITensorInfo *lhs,
127 ITensorInfo *rhs,
128 ITensorInfo *bias,
129 ITensorInfo *dst,
130 const MatMulKernelInfo &matmul_kernel_info)
SiCong Lia8d80582023-05-19 14:23:37 +0100131{
132 ARM_COMPUTE_ERROR_ON_NULLPTR(lhs, rhs, dst);
Mohammed Suhail Munshi8e2dede2023-06-27 14:25:58 +0100133 ARM_COMPUTE_LOG_PARAMS(lhs, rhs, bias, dst, matmul_kernel_info);
134 ARM_COMPUTE_ERROR_THROW_ON(validate(lhs, rhs, bias, dst, matmul_kernel_info));
SiCong Lia8d80582023-05-19 14:23:37 +0100135
136 // dst tensor auto initialization if not yet initialized
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100137 auto_init_if_empty(*dst, lhs->clone()->set_tensor_shape(misc::shape_calculator::compute_matmul_shape(
138 lhs->tensor_shape(), rhs->tensor_shape(), matmul_kernel_info)));
SiCong Lia8d80582023-05-19 14:23:37 +0100139
140 const int m = dst->dimension(1);
141 const int n = dst->dimension(0);
Ramy Elgammalc9525962023-05-19 14:23:37 +0100142 const int k = matmul_kernel_info.adj_lhs ? lhs->tensor_shape().y() : lhs->tensor_shape().x();
143
144 _m = m;
145 _n = n;
146 _k = k;
SiCong Lia8d80582023-05-19 14:23:37 +0100147
Gunes Bayire87fa662023-09-07 12:20:33 +0100148 const int m0 = std::min(matmul_kernel_info.m0, m);
149 const int n0 = adjust_vec_size(matmul_kernel_info.n0, n);
SiCong Lia8d80582023-05-19 14:23:37 +0100150
151 // Configure kernel window
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100152 const auto win_config =
153 validate_and_configure_window_for_mmul_kernels(lhs, rhs, dst, matmul_kernel_info, mmul_m0, mmul_n0);
SiCong Lia8d80582023-05-19 14:23:37 +0100154 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
155 IClKernel::configure_internal(win_config.second);
156
157 // 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.
158 const unsigned int m0_leftover = m % m0;
159 const unsigned int n0_leftover = n % n0;
160
161 CLBuildOptions build_opts;
162 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(lhs->data_type()));
163 build_opts.add_option_if(lhs->data_type() == DataType::F16, "-DHALF_PRECISION");
164 build_opts.add_option("-DM0=" + support::cpp11::to_string(m0));
165 build_opts.add_option("-DN0=" + support::cpp11::to_string(n0));
SiCong Lia8d80582023-05-19 14:23:37 +0100166 build_opts.add_option("-DM0_LEFTOVER=" + support::cpp11::to_string(m0_leftover));
167 build_opts.add_option("-DN0_LEFTOVER=" + support::cpp11::to_string(n0_leftover));
168 build_opts.add_option("-DMMUL_M0=" + support::cpp11::to_string(mmul_m0));
169 build_opts.add_option("-DMMUL_N0=" + support::cpp11::to_string(mmul_n0));
170 build_opts.add_option("-DMMUL_K0=" + support::cpp11::to_string(mmul_k0));
Mohammed Suhail Munshi8e2dede2023-06-27 14:25:58 +0100171 build_opts.add_option_if(bias != nullptr, "-DBIAS");
SiCong Lia8d80582023-05-19 14:23:37 +0100172
Ramy Elgammalc9525962023-05-19 14:23:37 +0100173 std::string kernel_name("mat_mul_native_mmul");
174 kernel_name += matmul_kernel_info.adj_lhs ? "_t" : "_nt";
175 kernel_name += matmul_kernel_info.adj_rhs ? "_t" : "_nt";
SiCong Lia8d80582023-05-19 14:23:37 +0100176
177 // A macro guard to compile ONLY the kernel of interest
178 build_opts.add_option("-D" + upper_string(kernel_name));
179
180 // Create kernel
181 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
182
183 // Set config_id for enabling LWS tuning
184 _config_id = kernel_name;
185 _config_id += "_";
186 _config_id += lower_string(string_from_data_type(lhs->data_type()));
187 _config_id += "_";
188 _config_id += support::cpp11::to_string(k);
189 _config_id += "_";
190 _config_id += support::cpp11::to_string(dst->dimension(2));
191 _config_id += "_";
192 _config_id += support::cpp11::to_string(m0);
193 _config_id += "_";
194 _config_id += support::cpp11::to_string(n0);
195 _config_id += "_";
196 _config_id += support::cpp11::to_string(matmul_kernel_info.k0);
197}
198
199void ClMatMulNativeMMULKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
200{
201 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
202 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
203
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100204 const ICLTensor *lhs =
205 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
206 const ICLTensor *rhs =
207 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
208 const ICLTensor *bias = utils::cast::polymorphic_downcast<const ICLTensor *>(
209 tensors.get_const_tensor(TensorType::ACL_SRC_2)); // nullptr if bias is not present
210 ICLTensor *dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
SiCong Lia8d80582023-05-19 14:23:37 +0100211 ARM_COMPUTE_ERROR_ON_NULLPTR(lhs, rhs, dst);
Mohammed Suhail Munshi8e2dede2023-06-27 14:25:58 +0100212 ARM_COMPUTE_LOG_PARAMS(lhs, rhs, bias, dst);
SiCong Lia8d80582023-05-19 14:23:37 +0100213 unsigned int idx = 0;
214
215 add_3d_tensor_nhw_argument(idx, lhs);
216 add_3d_tensor_nhw_argument(idx, rhs);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100217 if (bias != nullptr)
Mohammed Suhail Munshi8e2dede2023-06-27 14:25:58 +0100218 {
219 add_3d_tensor_nhw_argument(idx, bias);
220 }
SiCong Lia8d80582023-05-19 14:23:37 +0100221 add_3d_tensor_nhw_argument(idx, dst);
222
223 // Pass m and n at runtime as signed ints, to ensure results of any subtractions they could be operand in, would still be signed.
224 _kernel.setArg<cl_int>(idx++, _m);
225 _kernel.setArg<cl_int>(idx++, _n);
Ramy Elgammalc9525962023-05-19 14:23:37 +0100226 _kernel.setArg<cl_int>(idx++, _k);
SiCong Lia8d80582023-05-19 14:23:37 +0100227
228 // LWS_x should be multiple of 16 at least. (32, 2) has been chosen to have more work-items on a single core
229 // LWS also enforces the order of execution of the work items which improves cache utilization
230 enqueue(queue, *this, window, cl::NDRange(32, 2), false);
231}
232
233} // namespace kernels
234} // namespace opencl
235} // namespace arm_compute