blob: 4630ec08e9aa1be65167c5e94f58dd2bd1ff9b56 [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"
31#include "arm_compute/core/utils/misc/ShapeCalculator.h"
32
33#include "src/common/utils/Log.h"
34#include "src/core/helpers/AutoConfiguration.h"
35#include "src/core/helpers/WindowHelpers.h"
36
37#include "support/Cast.h"
38#include "support/StringSupport.h"
39
40namespace arm_compute
41{
42namespace opencl
43{
44namespace kernels
45{
46namespace
47{
48// Block size dimensions for the MMUL extension
49constexpr int mmul_m0 = 4;
50constexpr int mmul_n0 = 4;
51constexpr int mmul_k0 = 4;
52
53inline std::pair<int, int> adjust_m0_n0(int m0, int n0, int m, int n)
54{
55 m0 = std::min(m0, m);
56 n0 = adjust_vec_size(n0, n);
57 return { m0, n0 };
58}
59
60Status validate_matmul_kernel_info(const MatMulKernelInfo &matmul_kernel_info)
61{
62 const bool adj_lhs = matmul_kernel_info.adj_lhs;
Ramy Elgammalc9525962023-05-19 14:23:37 +010063 const int m0 = matmul_kernel_info.m0;
64 const int n0 = matmul_kernel_info.n0;
65 const int k0 = matmul_kernel_info.k0;
SiCong Lia8d80582023-05-19 14:23:37 +010066
SiCong Lia8d80582023-05-19 14:23:37 +010067 // Validate M0
68 ARM_COMPUTE_RETURN_ERROR_ON_MSG(m0 < 1, "Only positive integers are supported for M0");
69
Gunes Bayir00474e92023-06-19 21:33:51 +010070 if(adj_lhs)
71 {
72 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");
73 }
74
SiCong Lia8d80582023-05-19 14:23:37 +010075 // Validate N0
76 ARM_COMPUTE_RETURN_ERROR_ON_MSG(n0 < 1, "Only positive integers are supported for N0");
Gunes Bayir00474e92023-06-19 21:33:51 +010077 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 +010078
79 // Validate K0
80 ARM_COMPUTE_RETURN_ERROR_ON_MSG((k0 != 1), "Only 1 is supported for k0");
81
82 return Status{};
83}
84
85Status validate_input_shapes(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const MatMulKernelInfo &matmul_kernel_info)
86{
Gunes Bayir00474e92023-06-19 21:33:51 +010087 const size_t lhs_k = matmul_kernel_info.adj_lhs ? lhs_shape.y() : lhs_shape.x();
Ramy Elgammalc9525962023-05-19 14:23:37 +010088 const size_t rhs_k = matmul_kernel_info.adj_rhs ? rhs_shape.x() : rhs_shape.y();
SiCong Lia8d80582023-05-19 14:23:37 +010089
90 ARM_COMPUTE_RETURN_ERROR_ON_MSG(lhs_k != rhs_k, "K dimension in Lhs and Rhs matrices must match.");
91 ARM_COMPUTE_RETURN_ERROR_ON_MSG_VAR((lhs_k % mmul_k0) != 0, "K dimension must be a multiple of %d", mmul_k0);
92 ARM_COMPUTE_RETURN_ERROR_ON_MSG(lhs_shape.total_size() == 0, "Lhs tensor can't be empty");
93 ARM_COMPUTE_RETURN_ERROR_ON_MSG(rhs_shape.total_size() == 0, "Rhs tensor can't be empty");
94
95 constexpr size_t batch_dim_start = 2;
96 for(size_t i = batch_dim_start; i < Coordinates::num_max_dimensions; ++i)
97 {
98 ARM_COMPUTE_RETURN_ERROR_ON_MSG(lhs_shape[i] != rhs_shape[i], "Batch dimension broadcasting is not supported");
99 }
100
101 return Status{};
102}
103
104std::pair<Status, Window> validate_and_configure_window(ITensorInfo *lhs, ITensorInfo *rhs, ITensorInfo *dst, const MatMulKernelInfo &matmul_kernel_info)
105{
106 ARM_COMPUTE_UNUSED(lhs, rhs);
107
108 const Window win = calculate_max_window(*dst, Steps(1, 1));
109
110 // Collapse along the Z direction
111 // This collapse needs to be here in order to tune the Z dimension of LWS
112 Window collapsed = win.collapse(win, Window::DimZ);
113
114 // Reconfigure window size, one arm_matrix_multiply call needs 16 threads to finish.
115 Window::Dimension x_dimension = collapsed.x();
116 Window::Dimension y_dimension = collapsed.y();
117
118 const int m = dst->dimension(1);
119 const int n = dst->dimension(0);
120
121 int m0{};
122 int n0{};
123 std::tie(m0, n0) = adjust_m0_n0(matmul_kernel_info.m0, matmul_kernel_info.n0, m, n);
124
125 // Make M and N multiple of M0 and N0 respectively
126 const unsigned int ceil_to_multiple_n_n0 = ceil_to_multiple(n, n0);
127 const unsigned int ceil_to_multiple_m_m0 = ceil_to_multiple(m, m0);
128
129 // Divide M and N by M0 and N0 respectively
130 const unsigned int n_div_n0 = ceil_to_multiple_n_n0 / n0;
131 const unsigned int m_div_m0 = ceil_to_multiple_m_m0 / m0;
132
133 // Make n_div_n0 and m_div_m0 multiple of mmul_n0 and mmul_m0 respectively
134 const unsigned int ceil_to_multiple_n_div_n0_mmul_n0 = ceil_to_multiple(n_div_n0, mmul_n0);
135 const unsigned int ceil_to_multiple_m_div_m0_mmul_m0 = ceil_to_multiple(m_div_m0, mmul_m0);
136
137 // Ensure x_dimension is multiple of MMUL block size (mmul_m0 * mmul_n0)
138 x_dimension.set_end(ceil_to_multiple_n_div_n0_mmul_n0 * mmul_m0);
139 y_dimension.set_end(ceil_to_multiple_m_div_m0_mmul_m0 / mmul_m0);
140
141 collapsed.set(Window::DimX, x_dimension);
142 collapsed.set(Window::DimY, y_dimension);
143
144 return std::make_pair(Status{}, collapsed);
145}
146}
147ClMatMulNativeMMULKernel::ClMatMulNativeMMULKernel()
148{
149 _type = CLKernelType::GEMM;
150}
151
152Status ClMatMulNativeMMULKernel::validate(const ITensorInfo *lhs, const ITensorInfo *rhs, const ITensorInfo *dst, const MatMulKernelInfo &matmul_kernel_info)
153{
154 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lhs, rhs, dst);
155 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(lhs, 1, DataType::F32, DataType::F16);
156 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");
157 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lhs, rhs);
158 ARM_COMPUTE_RETURN_ON_ERROR(validate_matmul_kernel_info(matmul_kernel_info));
159 ARM_COMPUTE_RETURN_ON_ERROR(validate_input_shapes(lhs->tensor_shape(), rhs->tensor_shape(), matmul_kernel_info));
160
161 if(dst->total_size() != 0)
162 {
163 const TensorInfo tensor_info_dst = dst->clone()->set_tensor_shape(misc::shape_calculator::compute_matmul_shape(lhs->tensor_shape(), rhs->tensor_shape(), matmul_kernel_info));
164 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(dst, &tensor_info_dst);
165 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lhs, dst);
166 }
167
168 return Status{};
169}
170void ClMatMulNativeMMULKernel::configure(const ClCompileContext &compile_context, ITensorInfo *lhs, ITensorInfo *rhs, ITensorInfo *dst, const MatMulKernelInfo &matmul_kernel_info)
171{
172 ARM_COMPUTE_ERROR_ON_NULLPTR(lhs, rhs, dst);
173 ARM_COMPUTE_LOG_PARAMS(lhs, rhs, dst, matmul_kernel_info);
174 ARM_COMPUTE_ERROR_THROW_ON(validate(lhs, rhs, dst, matmul_kernel_info));
175
176 // dst tensor auto initialization if not yet initialized
177 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)));
178
179 const int m = dst->dimension(1);
180 const int n = dst->dimension(0);
Ramy Elgammalc9525962023-05-19 14:23:37 +0100181 const int k = matmul_kernel_info.adj_lhs ? lhs->tensor_shape().y() : lhs->tensor_shape().x();
182
183 _m = m;
184 _n = n;
185 _k = k;
SiCong Lia8d80582023-05-19 14:23:37 +0100186
187 int m0{};
188 int n0{};
189 std::tie(m0, n0) = adjust_m0_n0(matmul_kernel_info.m0, matmul_kernel_info.n0, m, n);
190
191 // Configure kernel window
192 const auto win_config = validate_and_configure_window(lhs, rhs, dst, matmul_kernel_info);
193 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
194 IClKernel::configure_internal(win_config.second);
195
196 // 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.
197 const unsigned int m0_leftover = m % m0;
198 const unsigned int n0_leftover = n % n0;
199
200 CLBuildOptions build_opts;
201 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(lhs->data_type()));
202 build_opts.add_option_if(lhs->data_type() == DataType::F16, "-DHALF_PRECISION");
203 build_opts.add_option("-DM0=" + support::cpp11::to_string(m0));
204 build_opts.add_option("-DN0=" + support::cpp11::to_string(n0));
SiCong Lia8d80582023-05-19 14:23:37 +0100205 build_opts.add_option("-DM0_LEFTOVER=" + support::cpp11::to_string(m0_leftover));
206 build_opts.add_option("-DN0_LEFTOVER=" + support::cpp11::to_string(n0_leftover));
207 build_opts.add_option("-DMMUL_M0=" + support::cpp11::to_string(mmul_m0));
208 build_opts.add_option("-DMMUL_N0=" + support::cpp11::to_string(mmul_n0));
209 build_opts.add_option("-DMMUL_K0=" + support::cpp11::to_string(mmul_k0));
SiCong Lia8d80582023-05-19 14:23:37 +0100210
Ramy Elgammalc9525962023-05-19 14:23:37 +0100211 std::string kernel_name("mat_mul_native_mmul");
212 kernel_name += matmul_kernel_info.adj_lhs ? "_t" : "_nt";
213 kernel_name += matmul_kernel_info.adj_rhs ? "_t" : "_nt";
SiCong Lia8d80582023-05-19 14:23:37 +0100214
215 // A macro guard to compile ONLY the kernel of interest
216 build_opts.add_option("-D" + upper_string(kernel_name));
217
218 // Create kernel
219 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
220
221 // Set config_id for enabling LWS tuning
222 _config_id = kernel_name;
223 _config_id += "_";
224 _config_id += lower_string(string_from_data_type(lhs->data_type()));
225 _config_id += "_";
226 _config_id += support::cpp11::to_string(k);
227 _config_id += "_";
228 _config_id += support::cpp11::to_string(dst->dimension(2));
229 _config_id += "_";
230 _config_id += support::cpp11::to_string(m0);
231 _config_id += "_";
232 _config_id += support::cpp11::to_string(n0);
233 _config_id += "_";
234 _config_id += support::cpp11::to_string(matmul_kernel_info.k0);
235}
236
237void ClMatMulNativeMMULKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
238{
239 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
240 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
241
242 const ICLTensor *lhs = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
243 const ICLTensor *rhs = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
244 ICLTensor *dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
245 ARM_COMPUTE_ERROR_ON_NULLPTR(lhs, rhs, dst);
246 ARM_COMPUTE_LOG_PARAMS(lhs, rhs, dst);
247 unsigned int idx = 0;
248
249 add_3d_tensor_nhw_argument(idx, lhs);
250 add_3d_tensor_nhw_argument(idx, rhs);
251 add_3d_tensor_nhw_argument(idx, dst);
252
253 // Pass m and n at runtime as signed ints, to ensure results of any subtractions they could be operand in, would still be signed.
254 _kernel.setArg<cl_int>(idx++, _m);
255 _kernel.setArg<cl_int>(idx++, _n);
Ramy Elgammalc9525962023-05-19 14:23:37 +0100256 _kernel.setArg<cl_int>(idx++, _k);
SiCong Lia8d80582023-05-19 14:23:37 +0100257
258 // LWS_x should be multiple of 16 at least. (32, 2) has been chosen to have more work-items on a single core
259 // LWS also enforces the order of execution of the work items which improves cache utilization
260 enqueue(queue, *this, window, cl::NDRange(32, 2), false);
261}
262
263} // namespace kernels
264} // namespace opencl
265} // namespace arm_compute