blob: e80349e486d8fd0b25306dd6119a7e36e354f274 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Gian Marco Iodiceb0c50372019-03-15 10:13:05 +00002 * Copyright (c) 2016-2019 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 Pinitas3faea252017-10-30 14:13:50 +000027#include "arm_compute/core/Log.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/Types.h"
29
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +010030#include <utility>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#include <vector>
32
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033namespace arm_compute
34{
35std::string get_cl_type_from_data_type(const DataType &dt)
36{
37 switch(dt)
38 {
39 case DataType::U8:
Michel Iwaniec00633802017-10-12 14:14:15 +010040 case DataType::QASYMM8:
41 return "uchar";
Georgios Pinitas3d13af82019-06-04 13:04:16 +010042 case DataType::S8:
43 case DataType::QSYMM8:
44 return "char";
Anthony Barbier6ff3b192017-09-04 18:44:23 +010045 case DataType::U16:
46 return "ushort";
47 case DataType::S16:
Michele Di Giorgio6997fc92019-06-18 10:23:22 +010048 case DataType::QSYMM16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049 return "short";
Anthony Barbier6ff3b192017-09-04 18:44:23 +010050 case DataType::U32:
51 return "uint";
52 case DataType::S32:
53 return "int";
54 case DataType::U64:
55 return "ulong";
56 case DataType::S64:
57 return "long";
58 case DataType::F16:
59 return "half";
60 case DataType::F32:
61 return "float";
62 default:
63 ARM_COMPUTE_ERROR("Unsupported input data type.");
64 return "";
65 }
66}
67
Giorgio Arena73023022018-09-04 14:55:55 +010068std::string get_cl_select_type_from_data_type(const DataType &dt)
69{
70 switch(dt)
71 {
72 case DataType::U8:
Giorgio Arena73023022018-09-04 14:55:55 +010073 case DataType::QASYMM8:
74 return "uchar";
Georgios Pinitas3d13af82019-06-04 13:04:16 +010075 case DataType::S8:
76 case DataType::QSYMM8:
77 return "char";
Giorgio Arena73023022018-09-04 14:55:55 +010078 case DataType::U16:
79 return "ushort";
80 case DataType::F16:
81 case DataType::S16:
Michele Di Giorgio6997fc92019-06-18 10:23:22 +010082 case DataType::QSYMM16:
Giorgio Arena73023022018-09-04 14:55:55 +010083 return "short";
84 case DataType::U32:
85 return "uint";
86 case DataType::F32:
87 case DataType::S32:
88 return "int";
89 case DataType::U64:
90 return "ulong";
91 case DataType::S64:
92 return "long";
93 default:
94 ARM_COMPUTE_ERROR("Unsupported input data type.");
95 return "";
96 }
97}
98
SiCong Lic51b72f2017-07-28 14:46:20 +010099std::string get_data_size_from_data_type(const DataType &dt)
100{
101 switch(dt)
102 {
103 case DataType::U8:
SiCong Lic51b72f2017-07-28 14:46:20 +0100104 case DataType::S8:
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100105 case DataType::QSYMM8:
Michel Iwaniec00633802017-10-12 14:14:15 +0100106 case DataType::QASYMM8:
SiCong Lic51b72f2017-07-28 14:46:20 +0100107 return "8";
108 case DataType::U16:
109 case DataType::S16:
Michele Di Giorgio6997fc92019-06-18 10:23:22 +0100110 case DataType::QSYMM16:
SiCong Lic51b72f2017-07-28 14:46:20 +0100111 case DataType::F16:
112 return "16";
113 case DataType::U32:
114 case DataType::S32:
115 case DataType::F32:
116 return "32";
117 case DataType::U64:
118 case DataType::S64:
119 return "64";
120 default:
121 ARM_COMPUTE_ERROR("Unsupported input data type.");
122 return "0";
123 }
124}
125
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100126std::string get_underlying_cl_type_from_data_type(const DataType &dt)
127{
Vidhya Sudhan Loganathanf4cb81b2018-07-04 15:13:14 +0100128 return get_cl_type_from_data_type(dt);
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100129}
130
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100131GPUTarget get_target_from_device(const cl::Device &device)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100132{
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100133 // Query device name size
Anthony Barbier30a63422018-02-28 18:18:24 +0000134 std::string device_name = device.getInfo<CL_DEVICE_NAME>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100135
Michalis Spyroua9676112018-02-22 18:07:43 +0000136 return get_target_from_name(device_name);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137}
138
Anthony Barbierd727e852018-04-20 11:05:29 +0100139bool arm_non_uniform_workgroup_supported(const cl::Device &device)
steniu0134702472017-07-11 09:22:58 +0100140{
Vidhya Sudhan Loganathaneb8a3992018-04-10 12:23:22 +0100141 return device_supports_extension(device, "cl_arm_non_uniform_work_group_size");
Matthew Bentham6f31f8c2017-10-27 11:50:06 +0100142}
steniu0134702472017-07-11 09:22:58 +0100143
Anthony Barbierd727e852018-04-20 11:05:29 +0100144bool fp16_supported(const cl::Device &device)
Matthew Bentham6f31f8c2017-10-27 11:50:06 +0100145{
Vidhya Sudhan Loganathaneb8a3992018-04-10 12:23:22 +0100146 return device_supports_extension(device, "cl_khr_fp16");
steniu0134702472017-07-11 09:22:58 +0100147}
148
Michalis Spyroue03342e2018-01-15 14:39:13 +0000149bool dot8_supported(const cl::Device &device)
150{
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100151 std::string device_name = device.getInfo<CL_DEVICE_NAME>();
152 const GPUTarget gpu_target = get_target_from_name(device_name);
153
154 // SW_WORKAROUND: Workaround for DDK revision r14p0.to enable cl_arm_integer_dot_product_int8
giuros018b6b4a92018-12-18 19:01:33 +0000155 std::set<GPUTarget> sw_workaround_issue = { GPUTarget::G76 };
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100156 return (device_supports_extension(device, "cl_arm_integer_dot_product_int8") || sw_workaround_issue.count(gpu_target) != 0);
Michalis Spyroue03342e2018-01-15 14:39:13 +0000157}
158
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100159bool dot8_acc_supported(const cl::Device &device)
160{
161 return device_supports_extension(device, "cl_arm_integer_dot_product_accumulate_int8");
162}
163
steniu0134702472017-07-11 09:22:58 +0100164CLVersion get_cl_version(const cl::Device &device)
165{
Anthony Barbier30a63422018-02-28 18:18:24 +0000166 std::string version_str = device.getInfo<CL_DEVICE_VERSION>();
steniu0134702472017-07-11 09:22:58 +0100167 if(version_str.find("OpenCL 2") != std::string::npos)
168 {
169 return CLVersion::CL20;
170 }
171 else if(version_str.find("OpenCL 1.2") != std::string::npos)
172 {
173 return CLVersion::CL12;
174 }
175 else if(version_str.find("OpenCL 1.1") != std::string::npos)
176 {
177 return CLVersion::CL11;
178 }
179 else if(version_str.find("OpenCL 1.0") != std::string::npos)
180 {
181 return CLVersion::CL10;
182 }
183
184 return CLVersion::UNKNOWN;
185}
186
Vidhya Sudhan Loganathaneb8a3992018-04-10 12:23:22 +0100187bool device_supports_extension(const cl::Device &device, const char *extension_name)
188{
189 std::string extensions = device.getInfo<CL_DEVICE_EXTENSIONS>();
190 auto pos = extensions.find(extension_name);
191 return (pos != std::string::npos);
192}
193
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100194bool cl_winograd_convolution_layer_supported(const Size2D &output_tile, const Size2D &kernel_size, DataLayout data_layout)
195{
196 ARM_COMPUTE_ERROR_ON(data_layout == DataLayout::UNKNOWN);
197
198 using WinogradConfiguration = std::pair<std::pair<int, int>, std::pair<int, int>>;
199
Giorgio Arena149fdf32018-07-04 17:03:33 +0100200 std::vector<WinogradConfiguration> winograd_configs_nchw =
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100201 {
202 WinogradConfiguration(std::pair<int, int>(1, 2), std::pair<int, int>(1, 3)),
203 WinogradConfiguration(std::pair<int, int>(1, 4), std::pair<int, int>(1, 3)),
204 WinogradConfiguration(std::pair<int, int>(2, 1), std::pair<int, int>(3, 1)),
205 WinogradConfiguration(std::pair<int, int>(4, 1), std::pair<int, int>(3, 1)),
206 WinogradConfiguration(std::pair<int, int>(2, 2), std::pair<int, int>(3, 3)),
207 WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(3, 3)),
Gian Marco Iodice876be2a2018-07-03 12:22:09 +0100208 WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(5, 5)),
209 WinogradConfiguration(std::pair<int, int>(4, 1), std::pair<int, int>(5, 1)),
210 WinogradConfiguration(std::pair<int, int>(1, 4), std::pair<int, int>(1, 5))
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100211 };
212
Giorgio Arena149fdf32018-07-04 17:03:33 +0100213 std::vector<WinogradConfiguration> winograd_configs_nhwc =
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100214 {
215 WinogradConfiguration(std::pair<int, int>(2, 2), std::pair<int, int>(3, 3)),
Giorgio Arena149fdf32018-07-04 17:03:33 +0100216 WinogradConfiguration(std::pair<int, int>(1, 4), std::pair<int, int>(1, 3)),
217 WinogradConfiguration(std::pair<int, int>(4, 1), std::pair<int, int>(3, 1)),
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100218 WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(3, 3)),
Gian Marco Iodiced28b7512018-07-06 12:59:28 +0100219 WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(5, 5)),
220 WinogradConfiguration(std::pair<int, int>(4, 1), std::pair<int, int>(5, 1)),
Michele Di Giorgiof955d512019-02-27 14:26:51 +0000221 WinogradConfiguration(std::pair<int, int>(1, 4), std::pair<int, int>(1, 5)),
222 WinogradConfiguration(std::pair<int, int>(1, 2), std::pair<int, int>(1, 7)),
223 WinogradConfiguration(std::pair<int, int>(2, 1), std::pair<int, int>(7, 1)),
224 WinogradConfiguration(std::pair<int, int>(2, 2), std::pair<int, int>(7, 7)),
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100225 };
226
227 auto p = std::make_pair(std::pair<int, int>(output_tile.width, output_tile.height),
228 std::pair<int, int>(kernel_size.width, kernel_size.height));
229
230 // Return true if supported
231 if(data_layout == DataLayout::NCHW)
232 {
Giorgio Arena149fdf32018-07-04 17:03:33 +0100233 return (std::find(winograd_configs_nchw.begin(), winograd_configs_nchw.end(), p) != winograd_configs_nchw.end());
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100234 }
235 else
236 {
Giorgio Arena149fdf32018-07-04 17:03:33 +0100237 return (std::find(winograd_configs_nhwc.begin(), winograd_configs_nhwc.end(), p) != winograd_configs_nhwc.end());
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100238 }
239}
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +0000240
241size_t preferred_vector_width(const cl::Device &device, const DataType dt)
242{
243 switch(dt)
244 {
245 case DataType::U8:
246 case DataType::S8:
247 case DataType::QASYMM8:
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100248 case DataType::QSYMM8:
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +0000249 return device.getInfo<CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR>();
250 case DataType::U16:
251 case DataType::S16:
Michele Di Giorgio6997fc92019-06-18 10:23:22 +0100252 case DataType::QSYMM16:
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +0000253 return device.getInfo<CL_DEVICE_PREFERRED_VECTOR_WIDTH_SHORT>();
254 case DataType::U32:
255 case DataType::S32:
256 return device.getInfo<CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT>();
257 case DataType::F16:
258 case DataType::F32:
259 return device.getInfo<CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT>();
260 case DataType::U64:
261 case DataType::S64:
262 return device.getInfo<CL_DEVICE_PREFERRED_VECTOR_WIDTH_LONG>();
263 default:
264 return 1;
265 }
266}
Gian Marco Iodiceb0c50372019-03-15 10:13:05 +0000267
268bool preferred_dummy_work_items_support(const cl::Device &device)
269{
270 ARM_COMPUTE_UNUSED(device);
271 // TODO (COMPMID-2044)
272 return true;
273}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100274} // namespace arm_compute