Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Gian Marco Iodice | b0c5037 | 2019-03-15 10:13:05 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2019 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [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/CLHelpers.h" |
| 25 | #include "arm_compute/core/CL/CLTypes.h" |
| 26 | #include "arm_compute/core/Error.h" |
Georgios Pinitas | 3faea25 | 2017-10-30 14:13:50 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/Log.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/Types.h" |
| 29 | |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 30 | #include <utility> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 31 | #include <vector> |
| 32 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 33 | namespace arm_compute |
| 34 | { |
| 35 | std::string get_cl_type_from_data_type(const DataType &dt) |
| 36 | { |
| 37 | switch(dt) |
| 38 | { |
| 39 | case DataType::U8: |
Michel Iwaniec | 0063380 | 2017-10-12 14:14:15 +0100 | [diff] [blame] | 40 | case DataType::QASYMM8: |
| 41 | return "uchar"; |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 42 | case DataType::S8: |
| 43 | case DataType::QSYMM8: |
| 44 | return "char"; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 45 | case DataType::U16: |
| 46 | return "ushort"; |
| 47 | case DataType::S16: |
| 48 | return "short"; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 49 | case DataType::U32: |
| 50 | return "uint"; |
| 51 | case DataType::S32: |
| 52 | return "int"; |
| 53 | case DataType::U64: |
| 54 | return "ulong"; |
| 55 | case DataType::S64: |
| 56 | return "long"; |
| 57 | case DataType::F16: |
| 58 | return "half"; |
| 59 | case DataType::F32: |
| 60 | return "float"; |
| 61 | default: |
| 62 | ARM_COMPUTE_ERROR("Unsupported input data type."); |
| 63 | return ""; |
| 64 | } |
| 65 | } |
| 66 | |
Giorgio Arena | 7302302 | 2018-09-04 14:55:55 +0100 | [diff] [blame] | 67 | std::string get_cl_select_type_from_data_type(const DataType &dt) |
| 68 | { |
| 69 | switch(dt) |
| 70 | { |
| 71 | case DataType::U8: |
Giorgio Arena | 7302302 | 2018-09-04 14:55:55 +0100 | [diff] [blame] | 72 | case DataType::QASYMM8: |
| 73 | return "uchar"; |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 74 | case DataType::S8: |
| 75 | case DataType::QSYMM8: |
| 76 | return "char"; |
Giorgio Arena | 7302302 | 2018-09-04 14:55:55 +0100 | [diff] [blame] | 77 | case DataType::U16: |
| 78 | return "ushort"; |
| 79 | case DataType::F16: |
| 80 | case DataType::S16: |
| 81 | return "short"; |
| 82 | case DataType::U32: |
| 83 | return "uint"; |
| 84 | case DataType::F32: |
| 85 | case DataType::S32: |
| 86 | return "int"; |
| 87 | case DataType::U64: |
| 88 | return "ulong"; |
| 89 | case DataType::S64: |
| 90 | return "long"; |
| 91 | default: |
| 92 | ARM_COMPUTE_ERROR("Unsupported input data type."); |
| 93 | return ""; |
| 94 | } |
| 95 | } |
| 96 | |
SiCong Li | c51b72f | 2017-07-28 14:46:20 +0100 | [diff] [blame] | 97 | std::string get_data_size_from_data_type(const DataType &dt) |
| 98 | { |
| 99 | switch(dt) |
| 100 | { |
| 101 | case DataType::U8: |
SiCong Li | c51b72f | 2017-07-28 14:46:20 +0100 | [diff] [blame] | 102 | case DataType::S8: |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 103 | case DataType::QSYMM8: |
Michel Iwaniec | 0063380 | 2017-10-12 14:14:15 +0100 | [diff] [blame] | 104 | case DataType::QASYMM8: |
SiCong Li | c51b72f | 2017-07-28 14:46:20 +0100 | [diff] [blame] | 105 | return "8"; |
| 106 | case DataType::U16: |
| 107 | case DataType::S16: |
SiCong Li | c51b72f | 2017-07-28 14:46:20 +0100 | [diff] [blame] | 108 | case DataType::F16: |
| 109 | return "16"; |
| 110 | case DataType::U32: |
| 111 | case DataType::S32: |
| 112 | case DataType::F32: |
| 113 | return "32"; |
| 114 | case DataType::U64: |
| 115 | case DataType::S64: |
| 116 | return "64"; |
| 117 | default: |
| 118 | ARM_COMPUTE_ERROR("Unsupported input data type."); |
| 119 | return "0"; |
| 120 | } |
| 121 | } |
| 122 | |
Georgios Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 123 | std::string get_underlying_cl_type_from_data_type(const DataType &dt) |
| 124 | { |
Vidhya Sudhan Loganathan | f4cb81b | 2018-07-04 15:13:14 +0100 | [diff] [blame] | 125 | return get_cl_type_from_data_type(dt); |
Georgios Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 126 | } |
| 127 | |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 128 | GPUTarget get_target_from_device(const cl::Device &device) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 129 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 130 | // Query device name size |
Anthony Barbier | 30a6342 | 2018-02-28 18:18:24 +0000 | [diff] [blame] | 131 | std::string device_name = device.getInfo<CL_DEVICE_NAME>(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 132 | |
Michalis Spyrou | a967611 | 2018-02-22 18:07:43 +0000 | [diff] [blame] | 133 | return get_target_from_name(device_name); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 134 | } |
| 135 | |
Anthony Barbier | d727e85 | 2018-04-20 11:05:29 +0100 | [diff] [blame] | 136 | bool arm_non_uniform_workgroup_supported(const cl::Device &device) |
steniu01 | 3470247 | 2017-07-11 09:22:58 +0100 | [diff] [blame] | 137 | { |
Vidhya Sudhan Loganathan | eb8a399 | 2018-04-10 12:23:22 +0100 | [diff] [blame] | 138 | return device_supports_extension(device, "cl_arm_non_uniform_work_group_size"); |
Matthew Bentham | 6f31f8c | 2017-10-27 11:50:06 +0100 | [diff] [blame] | 139 | } |
steniu01 | 3470247 | 2017-07-11 09:22:58 +0100 | [diff] [blame] | 140 | |
Anthony Barbier | d727e85 | 2018-04-20 11:05:29 +0100 | [diff] [blame] | 141 | bool fp16_supported(const cl::Device &device) |
Matthew Bentham | 6f31f8c | 2017-10-27 11:50:06 +0100 | [diff] [blame] | 142 | { |
Vidhya Sudhan Loganathan | eb8a399 | 2018-04-10 12:23:22 +0100 | [diff] [blame] | 143 | return device_supports_extension(device, "cl_khr_fp16"); |
steniu01 | 3470247 | 2017-07-11 09:22:58 +0100 | [diff] [blame] | 144 | } |
| 145 | |
Michalis Spyrou | e03342e | 2018-01-15 14:39:13 +0000 | [diff] [blame] | 146 | bool dot8_supported(const cl::Device &device) |
| 147 | { |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 148 | std::string device_name = device.getInfo<CL_DEVICE_NAME>(); |
| 149 | const GPUTarget gpu_target = get_target_from_name(device_name); |
| 150 | |
| 151 | // SW_WORKAROUND: Workaround for DDK revision r14p0.to enable cl_arm_integer_dot_product_int8 |
giuros01 | 8b6b4a9 | 2018-12-18 19:01:33 +0000 | [diff] [blame] | 152 | std::set<GPUTarget> sw_workaround_issue = { GPUTarget::G76 }; |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 153 | return (device_supports_extension(device, "cl_arm_integer_dot_product_int8") || sw_workaround_issue.count(gpu_target) != 0); |
Michalis Spyrou | e03342e | 2018-01-15 14:39:13 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 156 | bool dot8_acc_supported(const cl::Device &device) |
| 157 | { |
| 158 | return device_supports_extension(device, "cl_arm_integer_dot_product_accumulate_int8"); |
| 159 | } |
| 160 | |
steniu01 | 3470247 | 2017-07-11 09:22:58 +0100 | [diff] [blame] | 161 | CLVersion get_cl_version(const cl::Device &device) |
| 162 | { |
Anthony Barbier | 30a6342 | 2018-02-28 18:18:24 +0000 | [diff] [blame] | 163 | std::string version_str = device.getInfo<CL_DEVICE_VERSION>(); |
steniu01 | 3470247 | 2017-07-11 09:22:58 +0100 | [diff] [blame] | 164 | if(version_str.find("OpenCL 2") != std::string::npos) |
| 165 | { |
| 166 | return CLVersion::CL20; |
| 167 | } |
| 168 | else if(version_str.find("OpenCL 1.2") != std::string::npos) |
| 169 | { |
| 170 | return CLVersion::CL12; |
| 171 | } |
| 172 | else if(version_str.find("OpenCL 1.1") != std::string::npos) |
| 173 | { |
| 174 | return CLVersion::CL11; |
| 175 | } |
| 176 | else if(version_str.find("OpenCL 1.0") != std::string::npos) |
| 177 | { |
| 178 | return CLVersion::CL10; |
| 179 | } |
| 180 | |
| 181 | return CLVersion::UNKNOWN; |
| 182 | } |
| 183 | |
Vidhya Sudhan Loganathan | eb8a399 | 2018-04-10 12:23:22 +0100 | [diff] [blame] | 184 | bool device_supports_extension(const cl::Device &device, const char *extension_name) |
| 185 | { |
| 186 | std::string extensions = device.getInfo<CL_DEVICE_EXTENSIONS>(); |
| 187 | auto pos = extensions.find(extension_name); |
| 188 | return (pos != std::string::npos); |
| 189 | } |
| 190 | |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 191 | bool cl_winograd_convolution_layer_supported(const Size2D &output_tile, const Size2D &kernel_size, DataLayout data_layout) |
| 192 | { |
| 193 | ARM_COMPUTE_ERROR_ON(data_layout == DataLayout::UNKNOWN); |
| 194 | |
| 195 | using WinogradConfiguration = std::pair<std::pair<int, int>, std::pair<int, int>>; |
| 196 | |
Giorgio Arena | 149fdf3 | 2018-07-04 17:03:33 +0100 | [diff] [blame] | 197 | std::vector<WinogradConfiguration> winograd_configs_nchw = |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 198 | { |
| 199 | WinogradConfiguration(std::pair<int, int>(1, 2), std::pair<int, int>(1, 3)), |
| 200 | WinogradConfiguration(std::pair<int, int>(1, 4), std::pair<int, int>(1, 3)), |
| 201 | WinogradConfiguration(std::pair<int, int>(2, 1), std::pair<int, int>(3, 1)), |
| 202 | WinogradConfiguration(std::pair<int, int>(4, 1), std::pair<int, int>(3, 1)), |
| 203 | WinogradConfiguration(std::pair<int, int>(2, 2), std::pair<int, int>(3, 3)), |
| 204 | WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(3, 3)), |
Gian Marco Iodice | 876be2a | 2018-07-03 12:22:09 +0100 | [diff] [blame] | 205 | WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(5, 5)), |
| 206 | WinogradConfiguration(std::pair<int, int>(4, 1), std::pair<int, int>(5, 1)), |
| 207 | WinogradConfiguration(std::pair<int, int>(1, 4), std::pair<int, int>(1, 5)) |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 208 | }; |
| 209 | |
Giorgio Arena | 149fdf3 | 2018-07-04 17:03:33 +0100 | [diff] [blame] | 210 | std::vector<WinogradConfiguration> winograd_configs_nhwc = |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 211 | { |
| 212 | WinogradConfiguration(std::pair<int, int>(2, 2), std::pair<int, int>(3, 3)), |
Giorgio Arena | 149fdf3 | 2018-07-04 17:03:33 +0100 | [diff] [blame] | 213 | WinogradConfiguration(std::pair<int, int>(1, 4), std::pair<int, int>(1, 3)), |
| 214 | WinogradConfiguration(std::pair<int, int>(4, 1), std::pair<int, int>(3, 1)), |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 215 | WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(3, 3)), |
Gian Marco Iodice | d28b751 | 2018-07-06 12:59:28 +0100 | [diff] [blame] | 216 | WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(5, 5)), |
| 217 | WinogradConfiguration(std::pair<int, int>(4, 1), std::pair<int, int>(5, 1)), |
Michele Di Giorgio | f955d51 | 2019-02-27 14:26:51 +0000 | [diff] [blame] | 218 | WinogradConfiguration(std::pair<int, int>(1, 4), std::pair<int, int>(1, 5)), |
| 219 | WinogradConfiguration(std::pair<int, int>(1, 2), std::pair<int, int>(1, 7)), |
| 220 | WinogradConfiguration(std::pair<int, int>(2, 1), std::pair<int, int>(7, 1)), |
| 221 | WinogradConfiguration(std::pair<int, int>(2, 2), std::pair<int, int>(7, 7)), |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 222 | }; |
| 223 | |
| 224 | auto p = std::make_pair(std::pair<int, int>(output_tile.width, output_tile.height), |
| 225 | std::pair<int, int>(kernel_size.width, kernel_size.height)); |
| 226 | |
| 227 | // Return true if supported |
| 228 | if(data_layout == DataLayout::NCHW) |
| 229 | { |
Giorgio Arena | 149fdf3 | 2018-07-04 17:03:33 +0100 | [diff] [blame] | 230 | return (std::find(winograd_configs_nchw.begin(), winograd_configs_nchw.end(), p) != winograd_configs_nchw.end()); |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 231 | } |
| 232 | else |
| 233 | { |
Giorgio Arena | 149fdf3 | 2018-07-04 17:03:33 +0100 | [diff] [blame] | 234 | return (std::find(winograd_configs_nhwc.begin(), winograd_configs_nhwc.end(), p) != winograd_configs_nhwc.end()); |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 235 | } |
| 236 | } |
Vidhya Sudhan Loganathan | 5e96be7 | 2018-12-18 14:17:00 +0000 | [diff] [blame] | 237 | |
| 238 | size_t preferred_vector_width(const cl::Device &device, const DataType dt) |
| 239 | { |
| 240 | switch(dt) |
| 241 | { |
| 242 | case DataType::U8: |
| 243 | case DataType::S8: |
| 244 | case DataType::QASYMM8: |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 245 | case DataType::QSYMM8: |
Vidhya Sudhan Loganathan | 5e96be7 | 2018-12-18 14:17:00 +0000 | [diff] [blame] | 246 | return device.getInfo<CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR>(); |
| 247 | case DataType::U16: |
| 248 | case DataType::S16: |
| 249 | return device.getInfo<CL_DEVICE_PREFERRED_VECTOR_WIDTH_SHORT>(); |
| 250 | case DataType::U32: |
| 251 | case DataType::S32: |
| 252 | return device.getInfo<CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT>(); |
| 253 | case DataType::F16: |
| 254 | case DataType::F32: |
| 255 | return device.getInfo<CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT>(); |
| 256 | case DataType::U64: |
| 257 | case DataType::S64: |
| 258 | return device.getInfo<CL_DEVICE_PREFERRED_VECTOR_WIDTH_LONG>(); |
| 259 | default: |
| 260 | return 1; |
| 261 | } |
| 262 | } |
Gian Marco Iodice | b0c5037 | 2019-03-15 10:13:05 +0000 | [diff] [blame] | 263 | |
| 264 | bool preferred_dummy_work_items_support(const cl::Device &device) |
| 265 | { |
| 266 | ARM_COMPUTE_UNUSED(device); |
| 267 | // TODO (COMPMID-2044) |
| 268 | return true; |
| 269 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 270 | } // namespace arm_compute |