blob: b5ebac3b4927063ed16fac76f3b684ce48027202 [file] [log] [blame]
Gian Marco Iodice926afe12019-03-19 11:44:13 +00001/*
Gian Marco Iodicef16eed92023-04-19 16:29:26 +01002 * Copyright (c) 2019-2023 Arm Limited.
Gian Marco Iodice926afe12019-03-19 11:44:13 +00003 *
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 */
Georgios Pinitas7891a732021-08-20 21:39:25 +010024#include "src/gpu/cl/kernels/gemm/ClGemmHelpers.h"
Gian Marco Iodice926afe12019-03-19 11:44:13 +000025
Gian Marco Iodiceed5fe692020-07-09 08:41:10 +010026#include "arm_compute/core/CL/CLHelpers.h"
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +010027#include "arm_compute/core/CL/CLKernelLibrary.h"
Gian Marco Iodiceed5fe692020-07-09 08:41:10 +010028#include "arm_compute/core/CL/OpenCL.h"
Gian Marco Iodice839e1982020-10-29 13:36:50 +000029#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +010030
Gian Marco Iodicef16eed92023-04-19 16:29:26 +010031#include <limits>
Gian Marco Iodice926afe12019-03-19 11:44:13 +000032#include <utility>
33
34namespace arm_compute
35{
Georgios Pinitas856f66e2021-04-22 21:13:21 +010036namespace opencl
Gian Marco Iodice926afe12019-03-19 11:44:13 +000037{
Georgios Pinitas856f66e2021-04-22 21:13:21 +010038namespace kernels
39{
40namespace gemm
41{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010042std::pair<GEMMLHSMatrixInfo, GEMMRHSMatrixInfo> configure_lhs_rhs_info(unsigned int m,
43 unsigned int n,
44 unsigned int m0,
45 unsigned int n0,
46 unsigned int k0,
47 unsigned int v0,
48 unsigned int h0,
49 bool lhs_interleave,
50 bool rhs_interleave,
51 bool lhs_transpose,
52 bool rhs_transpose,
53 bool export_to_cl_image)
Gian Marco Iodice926afe12019-03-19 11:44:13 +000054{
Georgios Pinitas261df742021-02-23 00:00:42 +000055 ARM_COMPUTE_ERROR_ON(m0 == 0 || n0 == 0);
Gian Marco Iodicef16eed92023-04-19 16:29:26 +010056 ARM_COMPUTE_ERROR_ON(v0 == 0);
Gian Marco Iodice839e1982020-10-29 13:36:50 +000057 v0 = std::max(std::min(static_cast<int>(m / m0), static_cast<int>(v0)), static_cast<int>(1));
Gian Marco Iodicef16eed92023-04-19 16:29:26 +010058
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010059 if (h0 == 0)
Gian Marco Iodicef16eed92023-04-19 16:29:26 +010060 {
61 // When h0 is 0, we should take the maximum H0 possible
62 h0 = std::max(n / n0, 1U);
63 }
64 else
65 {
66 h0 = std::max(std::min(static_cast<int>(n / n0), static_cast<int>(h0)), static_cast<int>(1));
67 }
Gian Marco Iodice926afe12019-03-19 11:44:13 +000068
Gian Marco Iodiceed5fe692020-07-09 08:41:10 +010069 const GEMMLHSMatrixInfo lhs_info(m0, k0, v0, lhs_transpose, lhs_interleave);
70 const GEMMRHSMatrixInfo rhs_info(n0, k0, h0, rhs_transpose, rhs_interleave, export_to_cl_image);
Gian Marco Iodice926afe12019-03-19 11:44:13 +000071
72 return std::make_pair(lhs_info, rhs_info);
73}
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +010074
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010075std::pair<GEMMLHSMatrixInfo, GEMMRHSMatrixInfo>
76select_lhs_rhs_info(std::pair<GEMMLHSMatrixInfo, GEMMRHSMatrixInfo> info_img,
77 std::pair<GEMMLHSMatrixInfo, GEMMRHSMatrixInfo> info_buf,
78 unsigned int n,
79 unsigned int k,
80 unsigned int b,
81 DataType data_type)
Gian Marco Iodice839e1982020-10-29 13:36:50 +000082{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010083 ARM_COMPUTE_ERROR_ON_MSG(info_buf.second.export_to_cl_image == true,
84 "The fallback GeMM configuration cannot have export_to_cl_image = true");
Gian Marco Iodicef16eed92023-04-19 16:29:26 +010085
Gian Marco Iodice839e1982020-10-29 13:36:50 +000086 const TensorInfo tensor_rhs_info(TensorShape(n, k, b), 1, data_type);
Georgios Pinitas856f66e2021-04-22 21:13:21 +010087 const TensorShape shape = misc::shape_calculator::compute_rhs_reshaped_shape(tensor_rhs_info, info_img.second);
Gian Marco Iodice839e1982020-10-29 13:36:50 +000088 const TensorInfo tensor_reshaped_info(shape, 1, data_type);
89
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010090 if (bool(validate_image2d_support_on_rhs(tensor_reshaped_info, info_img.second)))
Gian Marco Iodice839e1982020-10-29 13:36:50 +000091 {
92 return info_img;
93 }
94 else
95 {
96 return info_buf;
97 }
98}
99
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +0100100void update_padding_for_cl_image(ITensorInfo *tensor)
101{
102 constexpr unsigned int num_floats_per_pixel = 4;
103
104 const unsigned int stride_y_in_elements = tensor->strides_in_bytes()[1] / tensor->element_size();
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100105 const unsigned int pixel_alignment = get_cl_image_pitch_alignment(CLKernelLibrary::get().get_device());
Georgios Pinitas9a671782021-02-25 00:04:08 +0000106
107 ARM_COMPUTE_ERROR_ON_MSG(pixel_alignment == 0, "Cannot retrieve cl_image pitch alignment");
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100108 if (pixel_alignment == 0)
Georgios Pinitas9a671782021-02-25 00:04:08 +0000109 {
110 return;
111 }
112
113 const unsigned int row_pitch_alignment = pixel_alignment * num_floats_per_pixel;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100114 const unsigned int round_up_width =
115 ((stride_y_in_elements + row_pitch_alignment - 1) / row_pitch_alignment) * row_pitch_alignment;
116 const unsigned int padding = round_up_width - stride_y_in_elements;
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +0100117
Manuel Bottinif733e032021-05-19 16:15:36 +0100118 tensor->extend_padding(PaddingSize(0, tensor->padding().right + padding, 0, 0));
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +0100119}
Gian Marco Iodiceed5fe692020-07-09 08:41:10 +0100120
121Status validate_image2d_support_on_rhs(const ITensorInfo &tensor_reshaped_info, const GEMMRHSMatrixInfo &rhs_info)
122{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100123 if (rhs_info.export_to_cl_image)
Gian Marco Iodiceed5fe692020-07-09 08:41:10 +0100124 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100125 ARM_COMPUTE_RETURN_ERROR_ON_MSG(((rhs_info.n0 == 2) || (rhs_info.n0 == 3)) && rhs_info.transpose == false,
126 "Export to cl_image only supported with n0 = 4, 8 or 16");
127 ARM_COMPUTE_RETURN_ERROR_ON_MSG(((rhs_info.k0 == 2) || (rhs_info.k0 == 3)) && rhs_info.transpose == true,
128 "Export to cl_image only supported with k0 = 4, 8 or 16");
SiCong Li5bdde852020-08-26 13:55:15 +0100129 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(&tensor_reshaped_info, DataType::F32, DataType::F16);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100130 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
131 !image2d_from_buffer_supported(CLKernelLibrary::get().get_device()),
132 "The extension cl_khr_image2d_from_buffer is not supported on the target platform");
133 ARM_COMPUTE_RETURN_ERROR_ON_MSG(get_cl_image_pitch_alignment(CLKernelLibrary::get().get_device()) == 0,
134 "Impossible to retrieve the cl_image pitch alignment");
Gian Marco Iodiceed5fe692020-07-09 08:41:10 +0100135
136 // Check the width and height of the output tensor.
137 // Since we cannot create a 3d image from a buffer, the third dimension is collapsed on the second dimension
138 const size_t max_image_w = CLKernelLibrary::get().get_device().getInfo<CL_DEVICE_IMAGE2D_MAX_WIDTH>();
139 const size_t max_image_h = CLKernelLibrary::get().get_device().getInfo<CL_DEVICE_IMAGE2D_MAX_HEIGHT>();
140
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100141 ARM_COMPUTE_RETURN_ERROR_ON_MSG(tensor_reshaped_info.tensor_shape()[0] > max_image_w * 4,
142 "Not supported width for cl_image");
143 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
144 tensor_reshaped_info.tensor_shape()[1] * tensor_reshaped_info.tensor_shape()[2] > max_image_h,
145 "Not supported height for cl_image");
Gian Marco Iodiceed5fe692020-07-09 08:41:10 +0100146 }
147
148 return Status{};
149}
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000150
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100151bool is_mmul_kernel_preferred(const unsigned int m,
152 const unsigned int n,
153 const unsigned int k,
154 const unsigned int b,
155 const DataType data_type,
156 unsigned int &best_m0,
157 unsigned int &best_n0)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000158{
159 ARM_COMPUTE_UNUSED(n, k, b, data_type);
160
161 const unsigned int mmul_k0 = 4;
162 best_m0 = 4;
163 best_n0 = 4;
164
165 const unsigned int ceil_to_multiple_m_m0 = ceil_to_multiple(m, best_m0);
166 const unsigned int m_div_m0 = ceil_to_multiple_m_m0 / best_m0;
167 const unsigned int ceil_to_multiple_m_div_m0_mmul_k0 = ceil_to_multiple(m_div_m0, mmul_k0);
168 const unsigned int gws_y = ceil_to_multiple_m_div_m0_mmul_k0 / mmul_k0;
169
170 return ((k % mmul_k0) == 0) && (gws_y > 4);
171}
Gian Marco Iodicef16eed92023-04-19 16:29:26 +0100172
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100173std::pair<GEMMLHSMatrixInfo, GEMMRHSMatrixInfo>
174find_lhs_rhs_info(const GeMMConfigsMatrix &configs, unsigned int m, unsigned int n, unsigned int k, unsigned int b)
Gian Marco Iodicef16eed92023-04-19 16:29:26 +0100175{
Gian Marco Iodice7a0f1bd2023-04-26 14:55:02 +0100176 size_t min_acc = std::numeric_limits<size_t>::max();
Gian Marco Iodicef16eed92023-04-19 16:29:26 +0100177 size_t min_idx = 0;
178
179 ARM_COMPUTE_ERROR_ON(configs.size() == 0);
180 const size_t num_rows = configs.size();
181 const size_t num_cols = configs[0].size();
182
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100183 ARM_COMPUTE_ERROR_ON_MSG(num_cols != 14U, "The entry should have 14 integer values representing: M, N, K, B, M0, "
184 "N0. K0, V0, H0, INT_LHS, INT_RHS, TRA_LHS, TRA_RHS, IMG_RHS");
Gian Marco Iodicef16eed92023-04-19 16:29:26 +0100185 ARM_COMPUTE_UNUSED(num_cols);
186
Gian Marco Iodice7a0f1bd2023-04-26 14:55:02 +0100187 // Find nearest GeMM workload
188 // Note: the workload does not depend on the K dimension
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100189 for (size_t y = 0; y < num_rows; ++y)
Gian Marco Iodicef16eed92023-04-19 16:29:26 +0100190 {
Gian Marco Iodice7a0f1bd2023-04-26 14:55:02 +0100191 size_t mc0 = static_cast<size_t>(configs[y][0]);
192 size_t nc0 = static_cast<size_t>(configs[y][1]);
193 size_t kc0 = static_cast<size_t>(configs[y][2]);
194 size_t bc0 = static_cast<size_t>(configs[y][3]);
195
196 size_t acc = 0;
Gian Marco Iodicef16eed92023-04-19 16:29:26 +0100197 acc += (m - mc0) * (m - mc0);
198 acc += (n - nc0) * (n - nc0);
Gian Marco Iodice7a0f1bd2023-04-26 14:55:02 +0100199 acc += (k - kc0) * (k - kc0);
200 acc += (b - bc0) * (b - bc0);
Gian Marco Iodicef16eed92023-04-19 16:29:26 +0100201 acc = std::sqrt(acc);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100202 if (acc < min_acc)
Gian Marco Iodicef16eed92023-04-19 16:29:26 +0100203 {
204 min_acc = acc;
205 min_idx = y;
206 }
207 }
208
209 // Get the configuration from the nearest GeMM shape
210 const int m0 = configs[min_idx][4];
211 const int n0 = configs[min_idx][5];
212 const int k0 = configs[min_idx][6];
213 const int v0 = configs[min_idx][7];
214 const int h0 = configs[min_idx][8];
215 const int i_lhs = configs[min_idx][9];
216 const int i_rhs = configs[min_idx][10];
217 const int t_lhs = configs[min_idx][11];
218 const int t_rhs = configs[min_idx][12];
219 const int im_rhs = configs[min_idx][13];
220
221 return configure_lhs_rhs_info(m, n, m0, n0, k0, v0, h0, i_lhs, i_rhs, t_lhs, t_rhs, im_rhs);
222}
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100223} // namespace gemm
224} // namespace kernels
225} // namespace opencl
Gian Marco Iodiceed5fe692020-07-09 08:41:10 +0100226} // namespace arm_compute