Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2020 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 | */ |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 24 | #include "src/core/CL/gemm/reshaped/CLGEMMReshapedKernelConfigurationBifrost.h" |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/GPUTarget.h" |
Gian Marco Iodice | ed5fe69 | 2020-07-09 08:41:10 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/TensorInfo.h" |
| 30 | #include "arm_compute/core/TensorShape.h" |
| 31 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 32 | #include "src/core/CL/gemm/CLGEMMHelpers.h" |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 33 | |
| 34 | #include <map> |
| 35 | #include <utility> |
| 36 | |
| 37 | namespace arm_compute |
| 38 | { |
| 39 | namespace cl_gemm |
| 40 | { |
Gian Marco Iodice | ed5fe69 | 2020-07-09 08:41:10 +0100 | [diff] [blame] | 41 | using namespace arm_compute::misc::shape_calculator; |
| 42 | |
Gian Marco Iodice | 12f2b8c | 2020-02-13 12:27:37 +0000 | [diff] [blame] | 43 | CLGEMMReshapedKernelConfigurationBifrost::CLGEMMReshapedKernelConfigurationBifrost(GPUTarget gpu) |
| 44 | : ICLGEMMKernelConfiguration(gpu) |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 45 | { |
| 46 | } |
| 47 | |
| 48 | std::pair<GEMMLHSMatrixInfo, GEMMRHSMatrixInfo> CLGEMMReshapedKernelConfigurationBifrost::configure(unsigned int m, unsigned int n, unsigned int k, unsigned int b, DataType data_type) |
| 49 | { |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 50 | using ConfigurationFunctionExecutorPtr = std::pair<GEMMLHSMatrixInfo, GEMMRHSMatrixInfo> (CLGEMMReshapedKernelConfigurationBifrost::*)(unsigned int m, unsigned int n, unsigned int k, unsigned int b); |
| 51 | |
| 52 | // Configurations for Mali-G76 |
| 53 | static std::map<DataType, ConfigurationFunctionExecutorPtr> gemm_configs_G76 = |
| 54 | { |
| 55 | { DataType::F32, &CLGEMMReshapedKernelConfigurationBifrost::configure_G76_f32 }, |
Gian Marco Iodice | 05639f6 | 2019-09-24 12:05:06 +0100 | [diff] [blame] | 56 | { DataType::F16, &CLGEMMReshapedKernelConfigurationBifrost::configure_G76_f16 }, |
Gian Marco Iodice | eb65f6d | 2020-04-15 11:42:15 +0100 | [diff] [blame] | 57 | { DataType::QASYMM8, &CLGEMMReshapedKernelConfigurationBifrost::configure_G76_u8 }, |
| 58 | { DataType::QSYMM8, &CLGEMMReshapedKernelConfigurationBifrost::configure_G76_u8 }, |
| 59 | { DataType::QASYMM8_SIGNED, &CLGEMMReshapedKernelConfigurationBifrost::configure_G76_u8 }, |
| 60 | { DataType::QSYMM8_PER_CHANNEL, &CLGEMMReshapedKernelConfigurationBifrost::configure_G76_u8 } |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | // Configurations for Mali-G7x |
| 64 | static std::map<DataType, ConfigurationFunctionExecutorPtr> gemm_configs_G7x = |
| 65 | { |
| 66 | { DataType::F32, &CLGEMMReshapedKernelConfigurationBifrost::configure_G7x_f32 }, |
Gian Marco Iodice | 05639f6 | 2019-09-24 12:05:06 +0100 | [diff] [blame] | 67 | { DataType::F16, &CLGEMMReshapedKernelConfigurationBifrost::configure_G7x_f16 }, |
Gian Marco Iodice | eb65f6d | 2020-04-15 11:42:15 +0100 | [diff] [blame] | 68 | { DataType::QASYMM8, &CLGEMMReshapedKernelConfigurationBifrost::configure_G7x_u8 }, |
| 69 | { DataType::QSYMM8, &CLGEMMReshapedKernelConfigurationBifrost::configure_G7x_u8 }, |
| 70 | { DataType::QASYMM8_SIGNED, &CLGEMMReshapedKernelConfigurationBifrost::configure_G7x_u8 }, |
| 71 | { DataType::QSYMM8_PER_CHANNEL, &CLGEMMReshapedKernelConfigurationBifrost::configure_G7x_u8 } |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | switch(_target) |
| 75 | { |
| 76 | case GPUTarget::G76: |
Gian Marco Iodice | 12f2b8c | 2020-02-13 12:27:37 +0000 | [diff] [blame] | 77 | if(gemm_configs_G76.find(data_type) != gemm_configs_G76.end()) |
Gian Marco Iodice | 0c17aa2 | 2019-09-27 09:23:15 +0100 | [diff] [blame] | 78 | { |
| 79 | return (this->*gemm_configs_G76[data_type])(m, n, k, b); |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | ARM_COMPUTE_ERROR("Not supported data type"); |
| 84 | } |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 85 | default: |
Gian Marco Iodice | 12f2b8c | 2020-02-13 12:27:37 +0000 | [diff] [blame] | 86 | if(gemm_configs_G7x.find(data_type) != gemm_configs_G7x.end()) |
Gian Marco Iodice | 0c17aa2 | 2019-09-27 09:23:15 +0100 | [diff] [blame] | 87 | { |
| 88 | return (this->*gemm_configs_G7x[data_type])(m, n, k, b); |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | ARM_COMPUTE_ERROR("Not supported data type"); |
| 93 | } |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
| 97 | std::pair<GEMMLHSMatrixInfo, GEMMRHSMatrixInfo> CLGEMMReshapedKernelConfigurationBifrost::configure_G7x_f32(unsigned int m, unsigned int n, unsigned int k, unsigned int b) |
| 98 | { |
| 99 | ARM_COMPUTE_UNUSED(k); |
| 100 | ARM_COMPUTE_UNUSED(b); |
| 101 | |
| 102 | if(n <= 4) |
| 103 | { |
| 104 | return configure_lhs_rhs_info(m, n, 4, 2, 8, 16, 16, true, false, false, true); |
| 105 | } |
| 106 | else |
| 107 | { |
| 108 | return configure_lhs_rhs_info(m, n, 5, 4, 4, 2, 16, false, true, false, true); |
| 109 | } |
| 110 | } |
| 111 | |
Gian Marco Iodice | 05639f6 | 2019-09-24 12:05:06 +0100 | [diff] [blame] | 112 | std::pair<GEMMLHSMatrixInfo, GEMMRHSMatrixInfo> CLGEMMReshapedKernelConfigurationBifrost::configure_G7x_f16(unsigned int m, unsigned int n, unsigned int k, unsigned int b) |
| 113 | { |
| 114 | ARM_COMPUTE_UNUSED(k); |
| 115 | ARM_COMPUTE_UNUSED(b); |
| 116 | |
| 117 | if(n <= 4) |
| 118 | { |
| 119 | return configure_lhs_rhs_info(m, n, 4, 2, 8, 8, 2, true, true, true, false); |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | return configure_lhs_rhs_info(m, n, 4, 8, 4, 4, 2, true, true, true, false); |
| 124 | } |
| 125 | } |
| 126 | |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 127 | std::pair<GEMMLHSMatrixInfo, GEMMRHSMatrixInfo> CLGEMMReshapedKernelConfigurationBifrost::configure_G7x_u8(unsigned int m, unsigned int n, unsigned int k, unsigned int b) |
| 128 | { |
| 129 | ARM_COMPUTE_UNUSED(k); |
| 130 | ARM_COMPUTE_UNUSED(b); |
| 131 | |
| 132 | if(dot8_supported(CLKernelLibrary::get().get_device())) |
| 133 | { |
| 134 | if(n <= 4) |
| 135 | { |
| 136 | return configure_lhs_rhs_info(m, n, 4, 2, 16, 2, 2, true, false, false, true); |
| 137 | } |
| 138 | else |
| 139 | { |
| 140 | return configure_lhs_rhs_info(m, n, 4, 4, 16, 2, 2, true, false, false, true); |
| 141 | } |
| 142 | } |
| 143 | else |
| 144 | { |
| 145 | if(n <= 4) |
| 146 | { |
| 147 | return configure_lhs_rhs_info(m, n, 4, 2, 8, 2, 2, true, false, false, true); |
| 148 | } |
| 149 | else |
| 150 | { |
| 151 | return configure_lhs_rhs_info(m, n, 6, 4, 4, 2, 2, true, true, false, true); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | std::pair<GEMMLHSMatrixInfo, GEMMRHSMatrixInfo> CLGEMMReshapedKernelConfigurationBifrost::configure_G76_f32(unsigned int m, unsigned int n, unsigned int k, unsigned int b) |
| 157 | { |
| 158 | ARM_COMPUTE_UNUSED(k); |
| 159 | ARM_COMPUTE_UNUSED(b); |
| 160 | |
Gian Marco Iodice | ed5fe69 | 2020-07-09 08:41:10 +0100 | [diff] [blame] | 161 | GEMMLHSMatrixInfo lhs_info_buf; |
| 162 | GEMMRHSMatrixInfo rhs_info_buf; |
| 163 | GEMMLHSMatrixInfo lhs_info_img; |
| 164 | GEMMRHSMatrixInfo rhs_info_img; |
| 165 | |
| 166 | // Get lhs_info/rhs_info in case of OpenCL buffer |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 167 | if(n <= 4) |
| 168 | { |
Gian Marco Iodice | ed5fe69 | 2020-07-09 08:41:10 +0100 | [diff] [blame] | 169 | std::tie(lhs_info_buf, rhs_info_buf) = configure_lhs_rhs_info(m, n, 4, 2, 8, 16, 16, true, false, false, true); |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 170 | } |
| 171 | else |
| 172 | { |
Gian Marco Iodice | ed5fe69 | 2020-07-09 08:41:10 +0100 | [diff] [blame] | 173 | std::tie(lhs_info_buf, rhs_info_buf) = configure_lhs_rhs_info(m, n, 4, 4, 2, 8, 16, false, false, false, true); |
| 174 | } |
| 175 | |
| 176 | // Get lhs_info/rhs_info in case of OpenCL image |
| 177 | // Condition on the GPU workload |
| 178 | if((m / 4) * (n / 4) >= 2560) |
| 179 | { |
| 180 | // Big workload |
| 181 | std::tie(lhs_info_img, rhs_info_img) = configure_lhs_rhs_info(m, n, 4, 4, 4, 2, 8, true, true, true, false, true); |
| 182 | } |
| 183 | else |
| 184 | { |
| 185 | // Small workload |
| 186 | std::tie(lhs_info_img, rhs_info_img) = configure_lhs_rhs_info(m, n, 2, 4, 4, 1, 1, true, true, true, false, true); |
| 187 | } |
| 188 | |
| 189 | const TensorInfo tensor_rhs_info(TensorShape(n, k, b), 1, DataType::F32); |
| 190 | const TensorShape shape = compute_rhs_reshaped_shape(tensor_rhs_info, rhs_info_img); |
| 191 | const TensorInfo tensor_reshaped_info(shape, 1, DataType::F32); |
| 192 | |
| 193 | // In case of vector by matrix with few work-items, we use the OpenCL buffer rather than the OpenCL image2d |
| 194 | const bool use_cl_image2d = (n <= 4) ? false : true; |
| 195 | |
| 196 | if(bool(validate_image2d_support_on_rhs(tensor_reshaped_info, rhs_info_img)) && use_cl_image2d) |
| 197 | { |
| 198 | return std::make_pair(lhs_info_img, rhs_info_img); |
| 199 | } |
| 200 | else |
| 201 | { |
| 202 | return std::make_pair(lhs_info_buf, rhs_info_buf); |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
Gian Marco Iodice | 05639f6 | 2019-09-24 12:05:06 +0100 | [diff] [blame] | 206 | std::pair<GEMMLHSMatrixInfo, GEMMRHSMatrixInfo> CLGEMMReshapedKernelConfigurationBifrost::configure_G76_f16(unsigned int m, unsigned int n, unsigned int k, unsigned int b) |
| 207 | { |
Gian Marco Iodice | c6af9db | 2020-09-24 13:55:20 +0100 | [diff] [blame] | 208 | const float workload = (static_cast<float>(m) * static_cast<float>(n) * static_cast<float>(b)) / 20.0f; |
Gian Marco Iodice | 839e198 | 2020-10-29 13:36:50 +0000 | [diff] [blame] | 209 | const float r_mk = static_cast<float>(m) / static_cast<float>(k); |
| 210 | const float r_nk = static_cast<float>(n) / static_cast<float>(k); |
Gian Marco Iodice | c6af9db | 2020-09-24 13:55:20 +0100 | [diff] [blame] | 211 | |
Gian Marco Iodice | 839e198 | 2020-10-29 13:36:50 +0000 | [diff] [blame] | 212 | if(workload <= 1422.40f) |
Gian Marco Iodice | 05639f6 | 2019-09-24 12:05:06 +0100 | [diff] [blame] | 213 | { |
Gian Marco Iodice | 839e198 | 2020-10-29 13:36:50 +0000 | [diff] [blame] | 214 | if(r_mk <= 2.45f) |
Gian Marco Iodice | c6af9db | 2020-09-24 13:55:20 +0100 | [diff] [blame] | 215 | { |
Gian Marco Iodice | 839e198 | 2020-10-29 13:36:50 +0000 | [diff] [blame] | 216 | if(workload <= 801.60f) |
Gian Marco Iodice | c6af9db | 2020-09-24 13:55:20 +0100 | [diff] [blame] | 217 | { |
Gian Marco Iodice | 839e198 | 2020-10-29 13:36:50 +0000 | [diff] [blame] | 218 | return configure_lhs_rhs_info(m, n, 2, 4, 4, 1, 2, true, false, true, false, false); |
Gian Marco Iodice | c6af9db | 2020-09-24 13:55:20 +0100 | [diff] [blame] | 219 | } |
| 220 | else |
| 221 | { |
Gian Marco Iodice | 839e198 | 2020-10-29 13:36:50 +0000 | [diff] [blame] | 222 | return configure_lhs_rhs_info(m, n, 4, 2, 4, 2, 2, false, false, true, false, false); |
Gian Marco Iodice | c6af9db | 2020-09-24 13:55:20 +0100 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | else |
| 226 | { |
Gian Marco Iodice | 839e198 | 2020-10-29 13:36:50 +0000 | [diff] [blame] | 227 | if(r_nk <= 0.67f) |
Gian Marco Iodice | c6af9db | 2020-09-24 13:55:20 +0100 | [diff] [blame] | 228 | { |
Gian Marco Iodice | 839e198 | 2020-10-29 13:36:50 +0000 | [diff] [blame] | 229 | return configure_lhs_rhs_info(m, n, 4, 2, 4, 2, 2, false, false, true, false, false); |
Gian Marco Iodice | c6af9db | 2020-09-24 13:55:20 +0100 | [diff] [blame] | 230 | } |
| 231 | else |
| 232 | { |
Gian Marco Iodice | 839e198 | 2020-10-29 13:36:50 +0000 | [diff] [blame] | 233 | return configure_lhs_rhs_info(m, n, 2, 4, 4, 4, 1, false, true, false, true, false); |
Gian Marco Iodice | c6af9db | 2020-09-24 13:55:20 +0100 | [diff] [blame] | 234 | } |
| 235 | } |
Gian Marco Iodice | 05639f6 | 2019-09-24 12:05:06 +0100 | [diff] [blame] | 236 | } |
| 237 | else |
| 238 | { |
Gian Marco Iodice | 839e198 | 2020-10-29 13:36:50 +0000 | [diff] [blame] | 239 | return configure_lhs_rhs_info(m, n, 4, 4, 4, 2, 4, true, true, true, false, false); |
Gian Marco Iodice | 05639f6 | 2019-09-24 12:05:06 +0100 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 243 | std::pair<GEMMLHSMatrixInfo, GEMMRHSMatrixInfo> CLGEMMReshapedKernelConfigurationBifrost::configure_G76_u8(unsigned int m, unsigned int n, unsigned int k, unsigned int b) |
| 244 | { |
| 245 | ARM_COMPUTE_UNUSED(k); |
| 246 | ARM_COMPUTE_UNUSED(b); |
| 247 | |
| 248 | if(n <= 4) |
| 249 | { |
| 250 | return configure_lhs_rhs_info(m, n, 4, 2, 16, 4, 1, false, false, false, true); |
| 251 | } |
| 252 | else |
| 253 | { |
| 254 | return configure_lhs_rhs_info(m, n, 4, 4, 16, 2, 2, false, true, false, true); |
| 255 | } |
| 256 | } |
| 257 | } // namespace cl_gemm |
Gian Marco Iodice | 12f2b8c | 2020-02-13 12:27:37 +0000 | [diff] [blame] | 258 | } // namespace arm_compute |