Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 1 | /* |
Gian Marco Iodice | f16eed9 | 2023-04-19 16:29:26 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2023 Arm Limited. |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 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 | */ |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 24 | #include "src/gpu/cl/kernels/gemm/ClGemmHelpers.h" |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 25 | |
Gian Marco Iodice | ed5fe69 | 2020-07-09 08:41:10 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/CL/CLHelpers.h" |
Manuel Bottini | c8e6e2c | 2020-07-02 11:27:27 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
Gian Marco Iodice | ed5fe69 | 2020-07-09 08:41:10 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/CL/OpenCL.h" |
Gian Marco Iodice | 839e198 | 2020-10-29 13:36:50 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Manuel Bottini | c8e6e2c | 2020-07-02 11:27:27 +0100 | [diff] [blame] | 30 | |
Gian Marco Iodice | f16eed9 | 2023-04-19 16:29:26 +0100 | [diff] [blame] | 31 | #include <limits> |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 32 | #include <utility> |
| 33 | |
| 34 | namespace arm_compute |
| 35 | { |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 36 | namespace opencl |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 37 | { |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 38 | namespace kernels |
| 39 | { |
| 40 | namespace gemm |
| 41 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 42 | std::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 Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 54 | { |
Georgios Pinitas | 261df74 | 2021-02-23 00:00:42 +0000 | [diff] [blame] | 55 | ARM_COMPUTE_ERROR_ON(m0 == 0 || n0 == 0); |
Gian Marco Iodice | f16eed9 | 2023-04-19 16:29:26 +0100 | [diff] [blame] | 56 | ARM_COMPUTE_ERROR_ON(v0 == 0); |
Gian Marco Iodice | 839e198 | 2020-10-29 13:36:50 +0000 | [diff] [blame] | 57 | v0 = std::max(std::min(static_cast<int>(m / m0), static_cast<int>(v0)), static_cast<int>(1)); |
Gian Marco Iodice | f16eed9 | 2023-04-19 16:29:26 +0100 | [diff] [blame] | 58 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 59 | if (h0 == 0) |
Gian Marco Iodice | f16eed9 | 2023-04-19 16:29:26 +0100 | [diff] [blame] | 60 | { |
| 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 Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 68 | |
Gian Marco Iodice | ed5fe69 | 2020-07-09 08:41:10 +0100 | [diff] [blame] | 69 | 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 Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 71 | |
| 72 | return std::make_pair(lhs_info, rhs_info); |
| 73 | } |
Manuel Bottini | c8e6e2c | 2020-07-02 11:27:27 +0100 | [diff] [blame] | 74 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 75 | std::pair<GEMMLHSMatrixInfo, GEMMRHSMatrixInfo> |
| 76 | select_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 Iodice | 839e198 | 2020-10-29 13:36:50 +0000 | [diff] [blame] | 82 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 83 | 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 Iodice | f16eed9 | 2023-04-19 16:29:26 +0100 | [diff] [blame] | 85 | |
Gian Marco Iodice | 839e198 | 2020-10-29 13:36:50 +0000 | [diff] [blame] | 86 | const TensorInfo tensor_rhs_info(TensorShape(n, k, b), 1, data_type); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 87 | const TensorShape shape = misc::shape_calculator::compute_rhs_reshaped_shape(tensor_rhs_info, info_img.second); |
Gian Marco Iodice | 839e198 | 2020-10-29 13:36:50 +0000 | [diff] [blame] | 88 | const TensorInfo tensor_reshaped_info(shape, 1, data_type); |
| 89 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 90 | if (bool(validate_image2d_support_on_rhs(tensor_reshaped_info, info_img.second))) |
Gian Marco Iodice | 839e198 | 2020-10-29 13:36:50 +0000 | [diff] [blame] | 91 | { |
| 92 | return info_img; |
| 93 | } |
| 94 | else |
| 95 | { |
| 96 | return info_buf; |
| 97 | } |
| 98 | } |
| 99 | |
Manuel Bottini | c8e6e2c | 2020-07-02 11:27:27 +0100 | [diff] [blame] | 100 | void 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 Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 105 | const unsigned int pixel_alignment = get_cl_image_pitch_alignment(CLKernelLibrary::get().get_device()); |
Georgios Pinitas | 9a67178 | 2021-02-25 00:04:08 +0000 | [diff] [blame] | 106 | |
| 107 | ARM_COMPUTE_ERROR_ON_MSG(pixel_alignment == 0, "Cannot retrieve cl_image pitch alignment"); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 108 | if (pixel_alignment == 0) |
Georgios Pinitas | 9a67178 | 2021-02-25 00:04:08 +0000 | [diff] [blame] | 109 | { |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | const unsigned int row_pitch_alignment = pixel_alignment * num_floats_per_pixel; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 114 | 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 Bottini | c8e6e2c | 2020-07-02 11:27:27 +0100 | [diff] [blame] | 117 | |
Manuel Bottini | f733e03 | 2021-05-19 16:15:36 +0100 | [diff] [blame] | 118 | tensor->extend_padding(PaddingSize(0, tensor->padding().right + padding, 0, 0)); |
Manuel Bottini | c8e6e2c | 2020-07-02 11:27:27 +0100 | [diff] [blame] | 119 | } |
Gian Marco Iodice | ed5fe69 | 2020-07-09 08:41:10 +0100 | [diff] [blame] | 120 | |
| 121 | Status validate_image2d_support_on_rhs(const ITensorInfo &tensor_reshaped_info, const GEMMRHSMatrixInfo &rhs_info) |
| 122 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 123 | if (rhs_info.export_to_cl_image) |
Gian Marco Iodice | ed5fe69 | 2020-07-09 08:41:10 +0100 | [diff] [blame] | 124 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 125 | 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 Li | 5bdde85 | 2020-08-26 13:55:15 +0100 | [diff] [blame] | 129 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(&tensor_reshaped_info, DataType::F32, DataType::F16); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 130 | 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 Iodice | ed5fe69 | 2020-07-09 08:41:10 +0100 | [diff] [blame] | 135 | |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 141 | 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 Iodice | ed5fe69 | 2020-07-09 08:41:10 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | return Status{}; |
| 149 | } |
Gunes Bayir | 4bfc70e | 2021-12-10 16:17:56 +0000 | [diff] [blame] | 150 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 151 | bool 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 Bayir | 4bfc70e | 2021-12-10 16:17:56 +0000 | [diff] [blame] | 158 | { |
| 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 Iodice | f16eed9 | 2023-04-19 16:29:26 +0100 | [diff] [blame] | 172 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 173 | std::pair<GEMMLHSMatrixInfo, GEMMRHSMatrixInfo> |
| 174 | find_lhs_rhs_info(const GeMMConfigsMatrix &configs, unsigned int m, unsigned int n, unsigned int k, unsigned int b) |
Gian Marco Iodice | f16eed9 | 2023-04-19 16:29:26 +0100 | [diff] [blame] | 175 | { |
Gian Marco Iodice | 7a0f1bd | 2023-04-26 14:55:02 +0100 | [diff] [blame] | 176 | size_t min_acc = std::numeric_limits<size_t>::max(); |
Gian Marco Iodice | f16eed9 | 2023-04-19 16:29:26 +0100 | [diff] [blame] | 177 | 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 183 | 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 Iodice | f16eed9 | 2023-04-19 16:29:26 +0100 | [diff] [blame] | 185 | ARM_COMPUTE_UNUSED(num_cols); |
| 186 | |
Gian Marco Iodice | 7a0f1bd | 2023-04-26 14:55:02 +0100 | [diff] [blame] | 187 | // Find nearest GeMM workload |
| 188 | // Note: the workload does not depend on the K dimension |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 189 | for (size_t y = 0; y < num_rows; ++y) |
Gian Marco Iodice | f16eed9 | 2023-04-19 16:29:26 +0100 | [diff] [blame] | 190 | { |
Gian Marco Iodice | 7a0f1bd | 2023-04-26 14:55:02 +0100 | [diff] [blame] | 191 | 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 Iodice | f16eed9 | 2023-04-19 16:29:26 +0100 | [diff] [blame] | 197 | acc += (m - mc0) * (m - mc0); |
| 198 | acc += (n - nc0) * (n - nc0); |
Gian Marco Iodice | 7a0f1bd | 2023-04-26 14:55:02 +0100 | [diff] [blame] | 199 | acc += (k - kc0) * (k - kc0); |
| 200 | acc += (b - bc0) * (b - bc0); |
Gian Marco Iodice | f16eed9 | 2023-04-19 16:29:26 +0100 | [diff] [blame] | 201 | acc = std::sqrt(acc); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 202 | if (acc < min_acc) |
Gian Marco Iodice | f16eed9 | 2023-04-19 16:29:26 +0100 | [diff] [blame] | 203 | { |
| 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 Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 223 | } // namespace gemm |
| 224 | } // namespace kernels |
| 225 | } // namespace opencl |
Gian Marco Iodice | ed5fe69 | 2020-07-09 08:41:10 +0100 | [diff] [blame] | 226 | } // namespace arm_compute |