blob: 44c720a40c8015af2f95f1862ee77c14730d7b4c [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"
Matthew Bentham314d3e22023-06-23 10:53:52 +000033#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"
37#include "src/core/helpers/WindowHelpers.h"
38
39#include "support/Cast.h"
40#include "support/StringSupport.h"
41
42namespace arm_compute
43{
44namespace opencl
45{
46namespace kernels
47{
48namespace
49{
50// Block size dimensions for the MMUL extension
51constexpr int mmul_m0 = 4;
52constexpr int mmul_n0 = 4;
53constexpr int mmul_k0 = 4;
54
55inline std::pair<int, int> adjust_m0_n0(int m0, int n0, int m, int n)
56{
57 m0 = std::min(m0, m);
58 n0 = adjust_vec_size(n0, n);
59 return { m0, n0 };
60}
61
62Status validate_matmul_kernel_info(const MatMulKernelInfo &matmul_kernel_info)
63{
64 const bool adj_lhs = matmul_kernel_info.adj_lhs;
Mohammed Suhail Munshi8e2dede2023-06-27 14:25:58 +010065 const int m0 = matmul_kernel_info.m0;
66 const int n0 = matmul_kernel_info.n0;
67 const int k0 = matmul_kernel_info.k0;
SiCong Lia8d80582023-05-19 14:23:37 +010068
SiCong Lia8d80582023-05-19 14:23:37 +010069 // Validate M0
70 ARM_COMPUTE_RETURN_ERROR_ON_MSG(m0 < 1, "Only positive integers are supported for M0");
71
Gunes Bayir00474e92023-06-19 21:33:51 +010072 if(adj_lhs)
73 {
74 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");
75 }
76
SiCong Lia8d80582023-05-19 14:23:37 +010077 // Validate N0
78 ARM_COMPUTE_RETURN_ERROR_ON_MSG(n0 < 1, "Only positive integers are supported for N0");
Gunes Bayir00474e92023-06-19 21:33:51 +010079 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 +010080
81 // Validate K0
82 ARM_COMPUTE_RETURN_ERROR_ON_MSG((k0 != 1), "Only 1 is supported for k0");
83
84 return Status{};
85}
86
87Status validate_input_shapes(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const MatMulKernelInfo &matmul_kernel_info)
88{
Gunes Bayir00474e92023-06-19 21:33:51 +010089 const size_t lhs_k = matmul_kernel_info.adj_lhs ? lhs_shape.y() : lhs_shape.x();
Ramy Elgammalc9525962023-05-19 14:23:37 +010090 const size_t rhs_k = matmul_kernel_info.adj_rhs ? rhs_shape.x() : rhs_shape.y();
SiCong Lia8d80582023-05-19 14:23:37 +010091
92 ARM_COMPUTE_RETURN_ERROR_ON_MSG(lhs_k != rhs_k, "K dimension in Lhs and Rhs matrices must match.");
93 ARM_COMPUTE_RETURN_ERROR_ON_MSG_VAR((lhs_k % mmul_k0) != 0, "K dimension must be a multiple of %d", mmul_k0);
94 ARM_COMPUTE_RETURN_ERROR_ON_MSG(lhs_shape.total_size() == 0, "Lhs tensor can't be empty");
95 ARM_COMPUTE_RETURN_ERROR_ON_MSG(rhs_shape.total_size() == 0, "Rhs tensor can't be empty");
96
97 constexpr size_t batch_dim_start = 2;
98 for(size_t i = batch_dim_start; i < Coordinates::num_max_dimensions; ++i)
99 {
100 ARM_COMPUTE_RETURN_ERROR_ON_MSG(lhs_shape[i] != rhs_shape[i], "Batch dimension broadcasting is not supported");
101 }
102
103 return Status{};
104}
105
106std::pair<Status, Window> validate_and_configure_window(ITensorInfo *lhs, ITensorInfo *rhs, ITensorInfo *dst, const MatMulKernelInfo &matmul_kernel_info)
107{
108 ARM_COMPUTE_UNUSED(lhs, rhs);
109
110 const Window win = calculate_max_window(*dst, Steps(1, 1));
111
112 // Collapse along the Z direction
113 // This collapse needs to be here in order to tune the Z dimension of LWS
114 Window collapsed = win.collapse(win, Window::DimZ);
115
116 // Reconfigure window size, one arm_matrix_multiply call needs 16 threads to finish.
117 Window::Dimension x_dimension = collapsed.x();
118 Window::Dimension y_dimension = collapsed.y();
119
120 const int m = dst->dimension(1);
121 const int n = dst->dimension(0);
122
123 int m0{};
124 int n0{};
125 std::tie(m0, n0) = adjust_m0_n0(matmul_kernel_info.m0, matmul_kernel_info.n0, m, n);
126
127 // Make M and N multiple of M0 and N0 respectively
128 const unsigned int ceil_to_multiple_n_n0 = ceil_to_multiple(n, n0);
129 const unsigned int ceil_to_multiple_m_m0 = ceil_to_multiple(m, m0);
130
131 // Divide M and N by M0 and N0 respectively
132 const unsigned int n_div_n0 = ceil_to_multiple_n_n0 / n0;
133 const unsigned int m_div_m0 = ceil_to_multiple_m_m0 / m0;
134
135 // Make n_div_n0 and m_div_m0 multiple of mmul_n0 and mmul_m0 respectively
136 const unsigned int ceil_to_multiple_n_div_n0_mmul_n0 = ceil_to_multiple(n_div_n0, mmul_n0);
137 const unsigned int ceil_to_multiple_m_div_m0_mmul_m0 = ceil_to_multiple(m_div_m0, mmul_m0);
138
139 // Ensure x_dimension is multiple of MMUL block size (mmul_m0 * mmul_n0)
140 x_dimension.set_end(ceil_to_multiple_n_div_n0_mmul_n0 * mmul_m0);
141 y_dimension.set_end(ceil_to_multiple_m_div_m0_mmul_m0 / mmul_m0);
142
143 collapsed.set(Window::DimX, x_dimension);
144 collapsed.set(Window::DimY, y_dimension);
145
146 return std::make_pair(Status{}, collapsed);
147}
148}
149ClMatMulNativeMMULKernel::ClMatMulNativeMMULKernel()
150{
151 _type = CLKernelType::GEMM;
152}
153
Mohammed Suhail Munshi8e2dede2023-06-27 14:25:58 +0100154Status 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 +0100155{
156 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lhs, rhs, dst);
157 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(lhs, 1, DataType::F32, DataType::F16);
158 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");
159 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lhs, rhs);
160 ARM_COMPUTE_RETURN_ON_ERROR(validate_matmul_kernel_info(matmul_kernel_info));
161 ARM_COMPUTE_RETURN_ON_ERROR(validate_input_shapes(lhs->tensor_shape(), rhs->tensor_shape(), matmul_kernel_info));
162
Mohammed Suhail Munshi8e2dede2023-06-27 14:25:58 +0100163 const TensorShape expected_output_shape = misc::shape_calculator::compute_matmul_shape(lhs->tensor_shape(), rhs->tensor_shape(), matmul_kernel_info);
164
SiCong Lia8d80582023-05-19 14:23:37 +0100165 if(dst->total_size() != 0)
166 {
Mohammed Suhail Munshi8e2dede2023-06-27 14:25:58 +0100167 const TensorInfo tensor_info_dst = dst->clone()->set_tensor_shape(expected_output_shape);
SiCong Lia8d80582023-05-19 14:23:37 +0100168 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(dst, &tensor_info_dst);
169 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lhs, dst);
170 }
171
Mohammed Suhail Munshi8e2dede2023-06-27 14:25:58 +0100172 if(bias != nullptr)
173 {
174 ARM_COMPUTE_RETURN_ERROR_ON_MSG((bias->num_dimensions() > 1), "Multi dimensional bias is unsupported.");
175 ARM_COMPUTE_RETURN_ERROR_ON_MSG(bias->dimension(0) != expected_output_shape[0], "First dimension of bias and output tensors must match.");
176 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lhs, bias);
177 }
178
SiCong Lia8d80582023-05-19 14:23:37 +0100179 return Status{};
180}
Mohammed Suhail Munshi8e2dede2023-06-27 14:25:58 +0100181void 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 +0100182{
183 ARM_COMPUTE_ERROR_ON_NULLPTR(lhs, rhs, dst);
Mohammed Suhail Munshi8e2dede2023-06-27 14:25:58 +0100184 ARM_COMPUTE_LOG_PARAMS(lhs, rhs, bias, dst, matmul_kernel_info);
185 ARM_COMPUTE_ERROR_THROW_ON(validate(lhs, rhs, bias, dst, matmul_kernel_info));
SiCong Lia8d80582023-05-19 14:23:37 +0100186
187 // dst tensor auto initialization if not yet initialized
188 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)));
189
190 const int m = dst->dimension(1);
191 const int n = dst->dimension(0);
Ramy Elgammalc9525962023-05-19 14:23:37 +0100192 const int k = matmul_kernel_info.adj_lhs ? lhs->tensor_shape().y() : lhs->tensor_shape().x();
193
194 _m = m;
195 _n = n;
196 _k = k;
SiCong Lia8d80582023-05-19 14:23:37 +0100197
198 int m0{};
199 int n0{};
200 std::tie(m0, n0) = adjust_m0_n0(matmul_kernel_info.m0, matmul_kernel_info.n0, m, n);
201
202 // Configure kernel window
203 const auto win_config = validate_and_configure_window(lhs, rhs, dst, matmul_kernel_info);
204 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
205 IClKernel::configure_internal(win_config.second);
206
207 // 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.
208 const unsigned int m0_leftover = m % m0;
209 const unsigned int n0_leftover = n % n0;
210
211 CLBuildOptions build_opts;
212 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(lhs->data_type()));
213 build_opts.add_option_if(lhs->data_type() == DataType::F16, "-DHALF_PRECISION");
214 build_opts.add_option("-DM0=" + support::cpp11::to_string(m0));
215 build_opts.add_option("-DN0=" + support::cpp11::to_string(n0));
SiCong Lia8d80582023-05-19 14:23:37 +0100216 build_opts.add_option("-DM0_LEFTOVER=" + support::cpp11::to_string(m0_leftover));
217 build_opts.add_option("-DN0_LEFTOVER=" + support::cpp11::to_string(n0_leftover));
218 build_opts.add_option("-DMMUL_M0=" + support::cpp11::to_string(mmul_m0));
219 build_opts.add_option("-DMMUL_N0=" + support::cpp11::to_string(mmul_n0));
220 build_opts.add_option("-DMMUL_K0=" + support::cpp11::to_string(mmul_k0));
Mohammed Suhail Munshi8e2dede2023-06-27 14:25:58 +0100221 build_opts.add_option_if(bias != nullptr, "-DBIAS");
SiCong Lia8d80582023-05-19 14:23:37 +0100222
Ramy Elgammalc9525962023-05-19 14:23:37 +0100223 std::string kernel_name("mat_mul_native_mmul");
224 kernel_name += matmul_kernel_info.adj_lhs ? "_t" : "_nt";
225 kernel_name += matmul_kernel_info.adj_rhs ? "_t" : "_nt";
SiCong Lia8d80582023-05-19 14:23:37 +0100226
227 // A macro guard to compile ONLY the kernel of interest
228 build_opts.add_option("-D" + upper_string(kernel_name));
229
230 // Create kernel
231 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
232
233 // Set config_id for enabling LWS tuning
234 _config_id = kernel_name;
235 _config_id += "_";
236 _config_id += lower_string(string_from_data_type(lhs->data_type()));
237 _config_id += "_";
238 _config_id += support::cpp11::to_string(k);
239 _config_id += "_";
240 _config_id += support::cpp11::to_string(dst->dimension(2));
241 _config_id += "_";
242 _config_id += support::cpp11::to_string(m0);
243 _config_id += "_";
244 _config_id += support::cpp11::to_string(n0);
245 _config_id += "_";
246 _config_id += support::cpp11::to_string(matmul_kernel_info.k0);
247}
248
249void ClMatMulNativeMMULKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
250{
251 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
252 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
253
Mohammed Suhail Munshi8e2dede2023-06-27 14:25:58 +0100254 const ICLTensor *lhs = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
255 const ICLTensor *rhs = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
256 const ICLTensor *bias = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_2)); // nullptr if bias is not present
257 ICLTensor *dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
SiCong Lia8d80582023-05-19 14:23:37 +0100258 ARM_COMPUTE_ERROR_ON_NULLPTR(lhs, rhs, dst);
Mohammed Suhail Munshi8e2dede2023-06-27 14:25:58 +0100259 ARM_COMPUTE_LOG_PARAMS(lhs, rhs, bias, dst);
SiCong Lia8d80582023-05-19 14:23:37 +0100260 unsigned int idx = 0;
261
262 add_3d_tensor_nhw_argument(idx, lhs);
263 add_3d_tensor_nhw_argument(idx, rhs);
Mohammed Suhail Munshi8e2dede2023-06-27 14:25:58 +0100264 if(bias != nullptr)
265 {
266 add_3d_tensor_nhw_argument(idx, bias);
267 }
SiCong Lia8d80582023-05-19 14:23:37 +0100268 add_3d_tensor_nhw_argument(idx, dst);
269
270 // Pass m and n at runtime as signed ints, to ensure results of any subtractions they could be operand in, would still be signed.
271 _kernel.setArg<cl_int>(idx++, _m);
272 _kernel.setArg<cl_int>(idx++, _n);
Ramy Elgammalc9525962023-05-19 14:23:37 +0100273 _kernel.setArg<cl_int>(idx++, _k);
SiCong Lia8d80582023-05-19 14:23:37 +0100274
275 // LWS_x should be multiple of 16 at least. (32, 2) has been chosen to have more work-items on a single core
276 // LWS also enforces the order of execution of the work items which improves cache utilization
277 enqueue(queue, *this, window, cl::NDRange(32, 2), false);
278}
279
280} // namespace kernels
281} // namespace opencl
282} // namespace arm_compute