blob: 2420ad6a78ecb5b73c366650d00716f6221227b1 [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"
Gunes Bayire87fa662023-09-07 12:20:33 +010031#include "arm_compute/core/utils/StringUtils.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000032#include "arm_compute/core/utils/helpers/AdjustVecSize.h"
SiCong Lia8d80582023-05-19 14:23:37 +010033#include "arm_compute/core/utils/misc/ShapeCalculator.h"
34
35#include "src/common/utils/Log.h"
36#include "src/core/helpers/AutoConfiguration.h"
37#include "src/core/helpers/WindowHelpers.h"
Gunes Bayire87fa662023-09-07 12:20:33 +010038#include "src/gpu/cl/kernels/helpers/MatMulKernelHelpers.h"
SiCong Lia8d80582023-05-19 14:23:37 +010039
40#include "support/Cast.h"
41#include "support/StringSupport.h"
42
43namespace arm_compute
44{
45namespace opencl
46{
47namespace kernels
48{
49namespace
50{
51// Block size dimensions for the MMUL extension
52constexpr int mmul_m0 = 4;
53constexpr int mmul_n0 = 4;
54constexpr int mmul_k0 = 4;
55
SiCong Lia8d80582023-05-19 14:23:37 +010056Status validate_matmul_kernel_info(const MatMulKernelInfo &matmul_kernel_info)
57{
58 const bool adj_lhs = matmul_kernel_info.adj_lhs;
Mohammed Suhail Munshi8e2dede2023-06-27 14:25:58 +010059 const int m0 = matmul_kernel_info.m0;
60 const int n0 = matmul_kernel_info.n0;
61 const int k0 = matmul_kernel_info.k0;
SiCong Lia8d80582023-05-19 14:23:37 +010062
SiCong Lia8d80582023-05-19 14:23:37 +010063 // Validate M0
64 ARM_COMPUTE_RETURN_ERROR_ON_MSG(m0 < 1, "Only positive integers are supported for M0");
65
Gunes Bayir00474e92023-06-19 21:33:51 +010066 if(adj_lhs)
67 {
68 ARM_COMPUTE_RETURN_ERROR_ON_MSG((m0 != 1) && (m0 != 2) && (m0 != 3) && (m0 != 4) && (m0 != 8) && (m0 != 16), "Only 1,2,3,4,8,16 are supported for M0 for Lhs transposed");
69 }
70
SiCong Lia8d80582023-05-19 14:23:37 +010071 // Validate N0
72 ARM_COMPUTE_RETURN_ERROR_ON_MSG(n0 < 1, "Only positive integers are supported for N0");
Gunes Bayir00474e92023-06-19 21:33:51 +010073 ARM_COMPUTE_RETURN_ERROR_ON_MSG((n0 != 1) && (n0 != 2) && (n0 != 3) && (n0 != 4) && (n0 != 8) && (n0 != 16), "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}
SiCong Lia8d80582023-05-19 14:23:37 +010080}
81ClMatMulNativeMMULKernel::ClMatMulNativeMMULKernel()
82{
83 _type = CLKernelType::GEMM;
84}
85
Mohammed Suhail Munshi8e2dede2023-06-27 14:25:58 +010086Status ClMatMulNativeMMULKernel::validate(const ITensorInfo *lhs, const ITensorInfo *rhs, const ITensorInfo *bias, const ITensorInfo *dst, const MatMulKernelInfo &matmul_kernel_info)
SiCong Lia8d80582023-05-19 14:23:37 +010087{
88 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lhs, rhs, dst);
89 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(lhs, 1, DataType::F32, DataType::F16);
90 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!arm_matrix_multiply_supported(CLKernelLibrary::get().get_device()), "The extension cl_arm_matrix_multiply is not supported on the target platform");
91 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lhs, rhs);
92 ARM_COMPUTE_RETURN_ON_ERROR(validate_matmul_kernel_info(matmul_kernel_info));
SiCong Lia8d80582023-05-19 14:23:37 +010093
Gunes Bayire87fa662023-09-07 12:20:33 +010094 const TensorShape &lhs_shape = lhs->tensor_shape();
95 ARM_COMPUTE_RETURN_ON_ERROR(validate_matmul_input_shapes(lhs_shape, rhs->tensor_shape(), matmul_kernel_info));
96
97 const size_t lhs_k = matmul_kernel_info.adj_lhs ? lhs_shape.y() : lhs_shape.x();
98 ARM_COMPUTE_RETURN_ERROR_ON_MSG_VAR((lhs_k % mmul_k0) != 0, "K dimension must be a multiple of %d", mmul_k0);
99
100 const TensorShape expected_output_shape = misc::shape_calculator::compute_matmul_shape(lhs_shape, rhs->tensor_shape(), matmul_kernel_info);
Mohammed Suhail Munshi8e2dede2023-06-27 14:25:58 +0100101
SiCong Lia8d80582023-05-19 14:23:37 +0100102 if(dst->total_size() != 0)
103 {
Mohammed Suhail Munshi8e2dede2023-06-27 14:25:58 +0100104 const TensorInfo tensor_info_dst = dst->clone()->set_tensor_shape(expected_output_shape);
SiCong Lia8d80582023-05-19 14:23:37 +0100105 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(dst, &tensor_info_dst);
106 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lhs, dst);
107 }
108
Mohammed Suhail Munshi8e2dede2023-06-27 14:25:58 +0100109 if(bias != nullptr)
110 {
111 ARM_COMPUTE_RETURN_ERROR_ON_MSG((bias->num_dimensions() > 1), "Multi dimensional bias is unsupported.");
112 ARM_COMPUTE_RETURN_ERROR_ON_MSG(bias->dimension(0) != expected_output_shape[0], "First dimension of bias and output tensors must match.");
113 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lhs, bias);
114 }
115
SiCong Lia8d80582023-05-19 14:23:37 +0100116 return Status{};
117}
Mohammed Suhail Munshi8e2dede2023-06-27 14:25:58 +0100118void ClMatMulNativeMMULKernel::configure(const ClCompileContext &compile_context, ITensorInfo *lhs, ITensorInfo *rhs, ITensorInfo *bias, ITensorInfo *dst, const MatMulKernelInfo &matmul_kernel_info)
SiCong Lia8d80582023-05-19 14:23:37 +0100119{
120 ARM_COMPUTE_ERROR_ON_NULLPTR(lhs, rhs, dst);
Mohammed Suhail Munshi8e2dede2023-06-27 14:25:58 +0100121 ARM_COMPUTE_LOG_PARAMS(lhs, rhs, bias, dst, matmul_kernel_info);
122 ARM_COMPUTE_ERROR_THROW_ON(validate(lhs, rhs, bias, dst, matmul_kernel_info));
SiCong Lia8d80582023-05-19 14:23:37 +0100123
124 // dst tensor auto initialization if not yet initialized
125 auto_init_if_empty(*dst, lhs->clone()->set_tensor_shape(misc::shape_calculator::compute_matmul_shape(lhs->tensor_shape(), rhs->tensor_shape(), matmul_kernel_info)));
126
127 const int m = dst->dimension(1);
128 const int n = dst->dimension(0);
Ramy Elgammalc9525962023-05-19 14:23:37 +0100129 const int k = matmul_kernel_info.adj_lhs ? lhs->tensor_shape().y() : lhs->tensor_shape().x();
130
131 _m = m;
132 _n = n;
133 _k = k;
SiCong Lia8d80582023-05-19 14:23:37 +0100134
Gunes Bayire87fa662023-09-07 12:20:33 +0100135 const int m0 = std::min(matmul_kernel_info.m0, m);
136 const int n0 = adjust_vec_size(matmul_kernel_info.n0, n);
SiCong Lia8d80582023-05-19 14:23:37 +0100137
138 // Configure kernel window
Gunes Bayire87fa662023-09-07 12:20:33 +0100139 const auto win_config = validate_and_configure_window_for_mmul_kernels(lhs, rhs, dst, matmul_kernel_info, mmul_m0,
140 mmul_n0);
141
SiCong Lia8d80582023-05-19 14:23:37 +0100142 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
143 IClKernel::configure_internal(win_config.second);
144
145 // 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.
146 const unsigned int m0_leftover = m % m0;
147 const unsigned int n0_leftover = n % n0;
148
149 CLBuildOptions build_opts;
150 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(lhs->data_type()));
151 build_opts.add_option_if(lhs->data_type() == DataType::F16, "-DHALF_PRECISION");
152 build_opts.add_option("-DM0=" + support::cpp11::to_string(m0));
153 build_opts.add_option("-DN0=" + support::cpp11::to_string(n0));
SiCong Lia8d80582023-05-19 14:23:37 +0100154 build_opts.add_option("-DM0_LEFTOVER=" + support::cpp11::to_string(m0_leftover));
155 build_opts.add_option("-DN0_LEFTOVER=" + support::cpp11::to_string(n0_leftover));
156 build_opts.add_option("-DMMUL_M0=" + support::cpp11::to_string(mmul_m0));
157 build_opts.add_option("-DMMUL_N0=" + support::cpp11::to_string(mmul_n0));
158 build_opts.add_option("-DMMUL_K0=" + support::cpp11::to_string(mmul_k0));
Mohammed Suhail Munshi8e2dede2023-06-27 14:25:58 +0100159 build_opts.add_option_if(bias != nullptr, "-DBIAS");
SiCong Lia8d80582023-05-19 14:23:37 +0100160
Ramy Elgammalc9525962023-05-19 14:23:37 +0100161 std::string kernel_name("mat_mul_native_mmul");
162 kernel_name += matmul_kernel_info.adj_lhs ? "_t" : "_nt";
163 kernel_name += matmul_kernel_info.adj_rhs ? "_t" : "_nt";
SiCong Lia8d80582023-05-19 14:23:37 +0100164
165 // A macro guard to compile ONLY the kernel of interest
166 build_opts.add_option("-D" + upper_string(kernel_name));
167
168 // Create kernel
169 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
170
171 // Set config_id for enabling LWS tuning
172 _config_id = kernel_name;
173 _config_id += "_";
174 _config_id += lower_string(string_from_data_type(lhs->data_type()));
175 _config_id += "_";
176 _config_id += support::cpp11::to_string(k);
177 _config_id += "_";
178 _config_id += support::cpp11::to_string(dst->dimension(2));
179 _config_id += "_";
180 _config_id += support::cpp11::to_string(m0);
181 _config_id += "_";
182 _config_id += support::cpp11::to_string(n0);
183 _config_id += "_";
184 _config_id += support::cpp11::to_string(matmul_kernel_info.k0);
185}
186
187void ClMatMulNativeMMULKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
188{
189 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
190 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
191
Mohammed Suhail Munshi8e2dede2023-06-27 14:25:58 +0100192 const ICLTensor *lhs = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
193 const ICLTensor *rhs = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
194 const ICLTensor *bias = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_2)); // nullptr if bias is not present
195 ICLTensor *dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
SiCong Lia8d80582023-05-19 14:23:37 +0100196 ARM_COMPUTE_ERROR_ON_NULLPTR(lhs, rhs, dst);
Mohammed Suhail Munshi8e2dede2023-06-27 14:25:58 +0100197 ARM_COMPUTE_LOG_PARAMS(lhs, rhs, bias, dst);
SiCong Lia8d80582023-05-19 14:23:37 +0100198 unsigned int idx = 0;
199
200 add_3d_tensor_nhw_argument(idx, lhs);
201 add_3d_tensor_nhw_argument(idx, rhs);
Mohammed Suhail Munshi8e2dede2023-06-27 14:25:58 +0100202 if(bias != nullptr)
203 {
204 add_3d_tensor_nhw_argument(idx, bias);
205 }
SiCong Lia8d80582023-05-19 14:23:37 +0100206 add_3d_tensor_nhw_argument(idx, dst);
207
208 // Pass m and n at runtime as signed ints, to ensure results of any subtractions they could be operand in, would still be signed.
209 _kernel.setArg<cl_int>(idx++, _m);
210 _kernel.setArg<cl_int>(idx++, _n);
Ramy Elgammalc9525962023-05-19 14:23:37 +0100211 _kernel.setArg<cl_int>(idx++, _k);
SiCong Lia8d80582023-05-19 14:23:37 +0100212
213 // LWS_x should be multiple of 16 at least. (32, 2) has been chosen to have more work-items on a single core
214 // LWS also enforces the order of execution of the work items which improves cache utilization
215 enqueue(queue, *this, window, cl::NDRange(32, 2), false);
216}
217
218} // namespace kernels
219} // namespace opencl
220} // namespace arm_compute