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