Gian Marco Iodice | 12f2b8c | 2020-02-13 12:27:37 +0000 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2020 Arm Limited. |
Gian Marco Iodice | 12f2b8c | 2020-02-13 12:27:37 +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 | */ |
| 24 | #include "arm_compute/core/CL/gemm/reshaped_only_rhs/CLGEMMReshapedOnlyRHSKernelConfigurationValhall.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
| 28 | #include "arm_compute/core/CL/gemm/CLGEMMHelpers.h" |
| 29 | #include "arm_compute/core/GPUTarget.h" |
Gian Marco Iodice | c6eaec3 | 2020-07-20 13:31:05 +0100 | [diff] [blame^] | 30 | #include "arm_compute/core/TensorInfo.h" |
| 31 | #include "arm_compute/core/TensorShape.h" |
| 32 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Gian Marco Iodice | 12f2b8c | 2020-02-13 12:27:37 +0000 | [diff] [blame] | 33 | |
| 34 | #include <map> |
| 35 | #include <utility> |
| 36 | |
| 37 | namespace arm_compute |
| 38 | { |
| 39 | namespace cl_gemm |
| 40 | { |
Gian Marco Iodice | c6eaec3 | 2020-07-20 13:31:05 +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 | CLGEMMReshapedOnlyRHSKernelConfigurationValhall::CLGEMMReshapedOnlyRHSKernelConfigurationValhall(GPUTarget gpu) |
| 44 | : ICLGEMMKernelConfiguration(gpu) |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | std::pair<GEMMLHSMatrixInfo, GEMMRHSMatrixInfo> CLGEMMReshapedOnlyRHSKernelConfigurationValhall::configure(unsigned int m, unsigned int n, unsigned int k, unsigned int b, DataType data_type) |
| 49 | { |
| 50 | using ConfigurationFunctionExecutorPtr = std::pair<GEMMLHSMatrixInfo, GEMMRHSMatrixInfo> (CLGEMMReshapedOnlyRHSKernelConfigurationValhall::*)(unsigned int m, unsigned int n, unsigned int k, |
| 51 | unsigned int b); |
| 52 | |
| 53 | // Configurations for Mali-G77 |
| 54 | static std::map<DataType, ConfigurationFunctionExecutorPtr> gemm_configs_G77 = |
| 55 | { |
| 56 | { DataType::F32, &CLGEMMReshapedOnlyRHSKernelConfigurationValhall::configure_G77_f32 }, |
| 57 | { DataType::F16, &CLGEMMReshapedOnlyRHSKernelConfigurationValhall::configure_G77_f16 }, |
Gian Marco Iodice | eb65f6d | 2020-04-15 11:42:15 +0100 | [diff] [blame] | 58 | { DataType::QASYMM8, &CLGEMMReshapedOnlyRHSKernelConfigurationValhall::configure_G77_u8 }, |
| 59 | { DataType::QSYMM8, &CLGEMMReshapedOnlyRHSKernelConfigurationValhall::configure_G77_u8 }, |
| 60 | { DataType::QASYMM8_SIGNED, &CLGEMMReshapedOnlyRHSKernelConfigurationValhall::configure_G77_u8 }, |
| 61 | { DataType::QSYMM8_PER_CHANNEL, &CLGEMMReshapedOnlyRHSKernelConfigurationValhall::configure_G77_u8 } |
Gian Marco Iodice | 12f2b8c | 2020-02-13 12:27:37 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | switch(_target) |
| 65 | { |
| 66 | case GPUTarget::G77: |
| 67 | default: |
| 68 | if(gemm_configs_G77.find(data_type) != gemm_configs_G77.end()) |
| 69 | { |
| 70 | return (this->*gemm_configs_G77[data_type])(m, n, k, b); |
| 71 | } |
| 72 | else |
| 73 | { |
| 74 | ARM_COMPUTE_ERROR("Not supported data type"); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | std::pair<GEMMLHSMatrixInfo, GEMMRHSMatrixInfo> CLGEMMReshapedOnlyRHSKernelConfigurationValhall::configure_G77_f32(unsigned int m, unsigned int n, unsigned int k, unsigned int b) |
| 80 | { |
| 81 | ARM_COMPUTE_UNUSED(k); |
Gian Marco Iodice | 12f2b8c | 2020-02-13 12:27:37 +0000 | [diff] [blame] | 82 | |
Gian Marco Iodice | c6eaec3 | 2020-07-20 13:31:05 +0100 | [diff] [blame^] | 83 | GEMMLHSMatrixInfo lhs_info_buf; |
| 84 | GEMMRHSMatrixInfo rhs_info_buf; |
| 85 | GEMMLHSMatrixInfo lhs_info_img; |
| 86 | GEMMRHSMatrixInfo rhs_info_img; |
| 87 | |
| 88 | // Get lhs_info/rhs_info in case of OpenCL buffer |
Gian Marco Iodice | 12f2b8c | 2020-02-13 12:27:37 +0000 | [diff] [blame] | 89 | if(m == 1) |
| 90 | { |
Gian Marco Iodice | c6eaec3 | 2020-07-20 13:31:05 +0100 | [diff] [blame^] | 91 | const unsigned int h0 = std::max(n / 4, 1U); |
| 92 | std::tie(lhs_info_buf, rhs_info_buf) = configure_lhs_rhs_info(m, n, 1, 4, 4, 1, h0, false, true, false, true); |
Gian Marco Iodice | 5a4fe19 | 2020-03-16 12:22:37 +0000 | [diff] [blame] | 93 | } |
| 94 | else |
| 95 | { |
Gian Marco Iodice | c6eaec3 | 2020-07-20 13:31:05 +0100 | [diff] [blame^] | 96 | if(m > 256) |
Gian Marco Iodice | 5a4fe19 | 2020-03-16 12:22:37 +0000 | [diff] [blame] | 97 | { |
Gian Marco Iodice | c6eaec3 | 2020-07-20 13:31:05 +0100 | [diff] [blame^] | 98 | const int v0 = std::max(std::min(static_cast<int>(n / 4), static_cast<int>(8)), static_cast<int>(1)); |
| 99 | std::tie(lhs_info_buf, rhs_info_buf) = configure_lhs_rhs_info(m, n, 4, 4, 4, 1, v0, false, true, false, true); |
Gian Marco Iodice | 5a4fe19 | 2020-03-16 12:22:37 +0000 | [diff] [blame] | 100 | } |
| 101 | else |
| 102 | { |
Gian Marco Iodice | c6eaec3 | 2020-07-20 13:31:05 +0100 | [diff] [blame^] | 103 | const int v0 = std::max(std::min(static_cast<int>(n / 4), static_cast<int>(8)), static_cast<int>(1)); |
| 104 | std::tie(lhs_info_buf, rhs_info_buf) = configure_lhs_rhs_info(m, n, 2, 4, 4, 1, v0, false, true, false, true); |
Gian Marco Iodice | 12f2b8c | 2020-02-13 12:27:37 +0000 | [diff] [blame] | 105 | } |
| 106 | } |
Gian Marco Iodice | c6eaec3 | 2020-07-20 13:31:05 +0100 | [diff] [blame^] | 107 | |
| 108 | // Get lhs_info/rhs_info in case of OpenCL image |
| 109 | if(m == 1) |
| 110 | { |
| 111 | std::tie(lhs_info_img, rhs_info_img) = configure_lhs_rhs_info(m, n, 1, 4, 4, 1, 8, true, true, false, false, true); |
| 112 | } |
| 113 | else |
| 114 | { |
| 115 | if((m / 4) * (n / 4) > 4096) |
| 116 | { |
| 117 | const int h0 = std::max(std::min(static_cast<int>(n / 4), static_cast<int>(8)), static_cast<int>(1)); |
| 118 | std::tie(lhs_info_img, rhs_info_img) = configure_lhs_rhs_info(m, n, 4, 4, 4, 1, h0, false, true, false, false, true); |
| 119 | } |
| 120 | else |
| 121 | { |
| 122 | const int h0 = std::max(std::min(static_cast<int>(n / 4), static_cast<int>(8)), static_cast<int>(1)); |
| 123 | std::tie(lhs_info_img, rhs_info_img) = configure_lhs_rhs_info(m, n, 2, 4, 4, 1, h0, false, true, false, false, true); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | const TensorInfo tensor_rhs_info(TensorShape(n, k, b), 1, DataType::F32); |
| 128 | const TensorShape shape = compute_rhs_reshaped_shape(tensor_rhs_info, rhs_info_img); |
| 129 | const TensorInfo tensor_reshaped_info(shape, 1, DataType::F32); |
| 130 | |
| 131 | // In case of small workloads, we use the OpenCL buffer rather than the OpenCL image2d |
| 132 | const bool use_cl_image2d = ((m / lhs_info_img.m0) * (n / rhs_info_img.n0)) * b < 1024 ? false : true; |
| 133 | |
| 134 | if(bool(validate_image2d_support_on_rhs(tensor_reshaped_info, rhs_info_img)) && use_cl_image2d) |
| 135 | { |
| 136 | return std::make_pair(lhs_info_img, rhs_info_img); |
| 137 | } |
| 138 | else |
| 139 | { |
| 140 | return std::make_pair(lhs_info_buf, rhs_info_buf); |
| 141 | } |
Gian Marco Iodice | 12f2b8c | 2020-02-13 12:27:37 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | std::pair<GEMMLHSMatrixInfo, GEMMRHSMatrixInfo> CLGEMMReshapedOnlyRHSKernelConfigurationValhall::configure_G77_f16(unsigned int m, unsigned int n, unsigned int k, unsigned int b) |
| 145 | { |
| 146 | ARM_COMPUTE_UNUSED(k); |
| 147 | ARM_COMPUTE_UNUSED(b); |
| 148 | |
| 149 | if(m == 1) |
| 150 | { |
Gian Marco Iodice | 2cfd3f7 | 2020-05-06 11:27:08 +0100 | [diff] [blame] | 151 | if(n > 2048) |
| 152 | { |
| 153 | const unsigned int h0 = std::max(n / 4, 1U); |
| 154 | return configure_lhs_rhs_info(m, n, 1, 4, 4, 1, h0, false, true, false, true); |
| 155 | } |
| 156 | else |
| 157 | { |
| 158 | const unsigned int h0 = std::max(n / 2, 1U); |
| 159 | return configure_lhs_rhs_info(m, n, 1, 2, 8, 1, h0, false, true, false, true); |
| 160 | } |
Gian Marco Iodice | 12f2b8c | 2020-02-13 12:27:37 +0000 | [diff] [blame] | 161 | } |
Gian Marco Iodice | c6eaec3 | 2020-07-20 13:31:05 +0100 | [diff] [blame^] | 162 | else if(m < 128) |
Gian Marco Iodice | 12f2b8c | 2020-02-13 12:27:37 +0000 | [diff] [blame] | 163 | { |
Gian Marco Iodice | 5a4fe19 | 2020-03-16 12:22:37 +0000 | [diff] [blame] | 164 | const int h0 = std::max(std::min(static_cast<int>(n / 4), static_cast<int>(256)), static_cast<int>(1)); |
Gian Marco Iodice | 2cfd3f7 | 2020-05-06 11:27:08 +0100 | [diff] [blame] | 165 | if(k >= 512) |
Gian Marco Iodice | 5a4fe19 | 2020-03-16 12:22:37 +0000 | [diff] [blame] | 166 | { |
| 167 | return configure_lhs_rhs_info(m, n, 2, 4, 16, 1, h0, false, true, false, false); |
| 168 | } |
| 169 | else |
| 170 | { |
| 171 | return configure_lhs_rhs_info(m, n, 2, 4, 8, 1, h0, false, true, false, false); |
| 172 | } |
Gian Marco Iodice | 12f2b8c | 2020-02-13 12:27:37 +0000 | [diff] [blame] | 173 | } |
Gian Marco Iodice | 939586e | 2020-05-05 15:10:21 +0100 | [diff] [blame] | 174 | else |
| 175 | { |
| 176 | const int h0 = std::max(std::min(static_cast<int>(n / 4), static_cast<int>(256)), static_cast<int>(1)); |
Gian Marco Iodice | 2886c75 | 2020-05-07 10:26:15 +0100 | [diff] [blame] | 177 | if(n >= 64) |
| 178 | { |
| 179 | return configure_lhs_rhs_info(m, n, 4, 4, 4, 1, h0, false, true, false, false); |
| 180 | } |
| 181 | else |
| 182 | { |
| 183 | if(k >= 512) |
| 184 | { |
| 185 | return configure_lhs_rhs_info(m, n, 2, 4, 16, 1, h0, false, true, false, false); |
| 186 | } |
| 187 | else |
| 188 | { |
| 189 | return configure_lhs_rhs_info(m, n, 2, 4, 8, 1, h0, false, true, false, false); |
| 190 | } |
| 191 | } |
Gian Marco Iodice | 939586e | 2020-05-05 15:10:21 +0100 | [diff] [blame] | 192 | } |
Gian Marco Iodice | 12f2b8c | 2020-02-13 12:27:37 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | std::pair<GEMMLHSMatrixInfo, GEMMRHSMatrixInfo> CLGEMMReshapedOnlyRHSKernelConfigurationValhall::configure_G77_u8(unsigned int m, unsigned int n, unsigned int k, unsigned int b) |
| 196 | { |
| 197 | ARM_COMPUTE_UNUSED(k); |
| 198 | ARM_COMPUTE_UNUSED(b); |
| 199 | |
| 200 | if(m == 1) |
| 201 | { |
| 202 | const unsigned int h0 = std::max(n / 2, 1U); |
| 203 | return configure_lhs_rhs_info(m, n, 1, 4, 16, 1, h0, false, true, false, true); |
| 204 | } |
| 205 | else |
| 206 | { |
Gian Marco Iodice | eb65f6d | 2020-04-15 11:42:15 +0100 | [diff] [blame] | 207 | const int h0 = std::max(std::min(static_cast<int>(n / 4), static_cast<int>(256)), static_cast<int>(1)); |
| 208 | if(m >= 28) |
| 209 | { |
| 210 | return configure_lhs_rhs_info(m, n, 4, 4, 16, 1, h0, false, true, false, true); |
| 211 | } |
| 212 | else |
| 213 | { |
| 214 | return configure_lhs_rhs_info(m, n, 2, 4, 16, 1, h0, false, true, false, true); |
| 215 | } |
Gian Marco Iodice | 12f2b8c | 2020-02-13 12:27:37 +0000 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | } // namespace cl_gemm |
| 219 | } // namespace arm_compute |