blob: a3c73677c762c38e50f66c6231dfe58cdc3783ae [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"
Pablo Tellodb8485a2019-09-24 11:03:47 +010025#include "arm_compute/core/CL/CLCoreRuntimeContext.h"
26#include "arm_compute/core/CL/CLKernelLibrary.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/CL/CLTypes.h"
28#include "arm_compute/core/Error.h"
Georgios Pinitas3faea252017-10-30 14:13:50 +000029#include "arm_compute/core/Log.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/Types.h"
31
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +010032#include <utility>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033#include <vector>
34
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035namespace arm_compute
36{
37std::string get_cl_type_from_data_type(const DataType &dt)
38{
39 switch(dt)
40 {
41 case DataType::U8:
Michel Iwaniec00633802017-10-12 14:14:15 +010042 case DataType::QASYMM8:
Michalis Spyrou3f632f32019-08-22 16:52:00 +010043 case DataType::QASYMM8_PER_CHANNEL:
Michel Iwaniec00633802017-10-12 14:14:15 +010044 return "uchar";
Georgios Pinitas3d13af82019-06-04 13:04:16 +010045 case DataType::S8:
46 case DataType::QSYMM8:
Michalis Spyrou3f632f32019-08-22 16:52:00 +010047 case DataType::QSYMM8_PER_CHANNEL:
Georgios Pinitas3d13af82019-06-04 13:04:16 +010048 return "char";
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049 case DataType::U16:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +010050 case DataType::QASYMM16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +010051 return "ushort";
52 case DataType::S16:
Michele Di Giorgio6997fc92019-06-18 10:23:22 +010053 case DataType::QSYMM16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +010054 return "short";
Anthony Barbier6ff3b192017-09-04 18:44:23 +010055 case DataType::U32:
56 return "uint";
57 case DataType::S32:
58 return "int";
59 case DataType::U64:
60 return "ulong";
61 case DataType::S64:
62 return "long";
63 case DataType::F16:
64 return "half";
65 case DataType::F32:
66 return "float";
67 default:
68 ARM_COMPUTE_ERROR("Unsupported input data type.");
69 return "";
70 }
71}
72
Giorgio Arena73023022018-09-04 14:55:55 +010073std::string get_cl_select_type_from_data_type(const DataType &dt)
74{
75 switch(dt)
76 {
77 case DataType::U8:
Giorgio Arena73023022018-09-04 14:55:55 +010078 case DataType::QASYMM8:
Michalis Spyrou3f632f32019-08-22 16:52:00 +010079 case DataType::QASYMM8_PER_CHANNEL:
Giorgio Arena73023022018-09-04 14:55:55 +010080 return "uchar";
Georgios Pinitas3d13af82019-06-04 13:04:16 +010081 case DataType::S8:
82 case DataType::QSYMM8:
Michalis Spyrou3f632f32019-08-22 16:52:00 +010083 case DataType::QSYMM8_PER_CHANNEL:
Georgios Pinitas3d13af82019-06-04 13:04:16 +010084 return "char";
Giorgio Arena73023022018-09-04 14:55:55 +010085 case DataType::U16:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +010086 case DataType::QASYMM16:
Giorgio Arena73023022018-09-04 14:55:55 +010087 return "ushort";
88 case DataType::F16:
89 case DataType::S16:
Michele Di Giorgio6997fc92019-06-18 10:23:22 +010090 case DataType::QSYMM16:
Giorgio Arena73023022018-09-04 14:55:55 +010091 return "short";
92 case DataType::U32:
93 return "uint";
94 case DataType::F32:
95 case DataType::S32:
96 return "int";
97 case DataType::U64:
98 return "ulong";
99 case DataType::S64:
100 return "long";
101 default:
102 ARM_COMPUTE_ERROR("Unsupported input data type.");
103 return "";
104 }
105}
106
SiCong Lic51b72f2017-07-28 14:46:20 +0100107std::string get_data_size_from_data_type(const DataType &dt)
108{
109 switch(dt)
110 {
111 case DataType::U8:
SiCong Lic51b72f2017-07-28 14:46:20 +0100112 case DataType::S8:
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100113 case DataType::QSYMM8:
Michel Iwaniec00633802017-10-12 14:14:15 +0100114 case DataType::QASYMM8:
Michalis Spyrou3f632f32019-08-22 16:52:00 +0100115 case DataType::QSYMM8_PER_CHANNEL:
116 case DataType::QASYMM8_PER_CHANNEL:
SiCong Lic51b72f2017-07-28 14:46:20 +0100117 return "8";
118 case DataType::U16:
119 case DataType::S16:
Michele Di Giorgio6997fc92019-06-18 10:23:22 +0100120 case DataType::QSYMM16:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100121 case DataType::QASYMM16:
SiCong Lic51b72f2017-07-28 14:46:20 +0100122 case DataType::F16:
123 return "16";
124 case DataType::U32:
125 case DataType::S32:
126 case DataType::F32:
127 return "32";
128 case DataType::U64:
129 case DataType::S64:
130 return "64";
131 default:
132 ARM_COMPUTE_ERROR("Unsupported input data type.");
133 return "0";
134 }
135}
136
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100137std::string get_underlying_cl_type_from_data_type(const DataType &dt)
138{
Vidhya Sudhan Loganathanf4cb81b2018-07-04 15:13:14 +0100139 return get_cl_type_from_data_type(dt);
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100140}
141
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100142GPUTarget get_target_from_device(const cl::Device &device)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100143{
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100144 // Query device name size
Anthony Barbier30a63422018-02-28 18:18:24 +0000145 std::string device_name = device.getInfo<CL_DEVICE_NAME>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100146
Michalis Spyroua9676112018-02-22 18:07:43 +0000147 return get_target_from_name(device_name);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100148}
149
Anthony Barbierd727e852018-04-20 11:05:29 +0100150bool arm_non_uniform_workgroup_supported(const cl::Device &device)
steniu0134702472017-07-11 09:22:58 +0100151{
Vidhya Sudhan Loganathaneb8a3992018-04-10 12:23:22 +0100152 return device_supports_extension(device, "cl_arm_non_uniform_work_group_size");
Matthew Bentham6f31f8c2017-10-27 11:50:06 +0100153}
steniu0134702472017-07-11 09:22:58 +0100154
Anthony Barbierd727e852018-04-20 11:05:29 +0100155bool fp16_supported(const cl::Device &device)
Matthew Bentham6f31f8c2017-10-27 11:50:06 +0100156{
Vidhya Sudhan Loganathaneb8a3992018-04-10 12:23:22 +0100157 return device_supports_extension(device, "cl_khr_fp16");
steniu0134702472017-07-11 09:22:58 +0100158}
159
Michalis Spyroue03342e2018-01-15 14:39:13 +0000160bool dot8_supported(const cl::Device &device)
161{
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100162 std::string device_name = device.getInfo<CL_DEVICE_NAME>();
163 const GPUTarget gpu_target = get_target_from_name(device_name);
164
165 // SW_WORKAROUND: Workaround for DDK revision r14p0.to enable cl_arm_integer_dot_product_int8
giuros018b6b4a92018-12-18 19:01:33 +0000166 std::set<GPUTarget> sw_workaround_issue = { GPUTarget::G76 };
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100167 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 +0000168}
169
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100170bool dot8_acc_supported(const cl::Device &device)
171{
172 return device_supports_extension(device, "cl_arm_integer_dot_product_accumulate_int8");
173}
174
steniu0134702472017-07-11 09:22:58 +0100175CLVersion get_cl_version(const cl::Device &device)
176{
Anthony Barbier30a63422018-02-28 18:18:24 +0000177 std::string version_str = device.getInfo<CL_DEVICE_VERSION>();
steniu0134702472017-07-11 09:22:58 +0100178 if(version_str.find("OpenCL 2") != std::string::npos)
179 {
180 return CLVersion::CL20;
181 }
182 else if(version_str.find("OpenCL 1.2") != std::string::npos)
183 {
184 return CLVersion::CL12;
185 }
186 else if(version_str.find("OpenCL 1.1") != std::string::npos)
187 {
188 return CLVersion::CL11;
189 }
190 else if(version_str.find("OpenCL 1.0") != std::string::npos)
191 {
192 return CLVersion::CL10;
193 }
194
195 return CLVersion::UNKNOWN;
196}
197
Vidhya Sudhan Loganathaneb8a3992018-04-10 12:23:22 +0100198bool device_supports_extension(const cl::Device &device, const char *extension_name)
199{
200 std::string extensions = device.getInfo<CL_DEVICE_EXTENSIONS>();
201 auto pos = extensions.find(extension_name);
202 return (pos != std::string::npos);
203}
204
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100205bool cl_winograd_convolution_layer_supported(const Size2D &output_tile, const Size2D &kernel_size, DataLayout data_layout)
206{
207 ARM_COMPUTE_ERROR_ON(data_layout == DataLayout::UNKNOWN);
208
209 using WinogradConfiguration = std::pair<std::pair<int, int>, std::pair<int, int>>;
210
Giorgio Arena149fdf32018-07-04 17:03:33 +0100211 std::vector<WinogradConfiguration> winograd_configs_nchw =
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100212 {
213 WinogradConfiguration(std::pair<int, int>(1, 2), std::pair<int, int>(1, 3)),
214 WinogradConfiguration(std::pair<int, int>(1, 4), std::pair<int, int>(1, 3)),
215 WinogradConfiguration(std::pair<int, int>(2, 1), std::pair<int, int>(3, 1)),
216 WinogradConfiguration(std::pair<int, int>(4, 1), std::pair<int, int>(3, 1)),
217 WinogradConfiguration(std::pair<int, int>(2, 2), std::pair<int, int>(3, 3)),
218 WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(3, 3)),
Gian Marco Iodice876be2a2018-07-03 12:22:09 +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)),
221 WinogradConfiguration(std::pair<int, int>(1, 4), std::pair<int, int>(1, 5))
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100222 };
223
Giorgio Arena149fdf32018-07-04 17:03:33 +0100224 std::vector<WinogradConfiguration> winograd_configs_nhwc =
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100225 {
226 WinogradConfiguration(std::pair<int, int>(2, 2), std::pair<int, int>(3, 3)),
Giorgio Arena149fdf32018-07-04 17:03:33 +0100227 WinogradConfiguration(std::pair<int, int>(1, 4), std::pair<int, int>(1, 3)),
228 WinogradConfiguration(std::pair<int, int>(4, 1), std::pair<int, int>(3, 1)),
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100229 WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(3, 3)),
Gian Marco Iodiced28b7512018-07-06 12:59:28 +0100230 WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(5, 5)),
231 WinogradConfiguration(std::pair<int, int>(4, 1), std::pair<int, int>(5, 1)),
Michele Di Giorgiof955d512019-02-27 14:26:51 +0000232 WinogradConfiguration(std::pair<int, int>(1, 4), std::pair<int, int>(1, 5)),
233 WinogradConfiguration(std::pair<int, int>(1, 2), std::pair<int, int>(1, 7)),
234 WinogradConfiguration(std::pair<int, int>(2, 1), std::pair<int, int>(7, 1)),
235 WinogradConfiguration(std::pair<int, int>(2, 2), std::pair<int, int>(7, 7)),
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100236 };
237
238 auto p = std::make_pair(std::pair<int, int>(output_tile.width, output_tile.height),
239 std::pair<int, int>(kernel_size.width, kernel_size.height));
240
241 // Return true if supported
242 if(data_layout == DataLayout::NCHW)
243 {
Giorgio Arena149fdf32018-07-04 17:03:33 +0100244 return (std::find(winograd_configs_nchw.begin(), winograd_configs_nchw.end(), p) != winograd_configs_nchw.end());
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100245 }
246 else
247 {
Giorgio Arena149fdf32018-07-04 17:03:33 +0100248 return (std::find(winograd_configs_nhwc.begin(), winograd_configs_nhwc.end(), p) != winograd_configs_nhwc.end());
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100249 }
250}
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +0000251
252size_t preferred_vector_width(const cl::Device &device, const DataType dt)
253{
254 switch(dt)
255 {
256 case DataType::U8:
257 case DataType::S8:
258 case DataType::QASYMM8:
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100259 case DataType::QSYMM8:
Michalis Spyrou3f632f32019-08-22 16:52:00 +0100260 case DataType::QSYMM8_PER_CHANNEL:
261 case DataType::QASYMM8_PER_CHANNEL:
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +0000262 return device.getInfo<CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR>();
263 case DataType::U16:
264 case DataType::S16:
Michele Di Giorgio6997fc92019-06-18 10:23:22 +0100265 case DataType::QSYMM16:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100266 case DataType::QASYMM16:
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +0000267 return device.getInfo<CL_DEVICE_PREFERRED_VECTOR_WIDTH_SHORT>();
268 case DataType::U32:
269 case DataType::S32:
270 return device.getInfo<CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT>();
271 case DataType::F16:
272 case DataType::F32:
273 return device.getInfo<CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT>();
274 case DataType::U64:
275 case DataType::S64:
276 return device.getInfo<CL_DEVICE_PREFERRED_VECTOR_WIDTH_LONG>();
277 default:
278 return 1;
279 }
280}
Gian Marco Iodiceb0c50372019-03-15 10:13:05 +0000281
282bool preferred_dummy_work_items_support(const cl::Device &device)
283{
284 ARM_COMPUTE_UNUSED(device);
285 // TODO (COMPMID-2044)
286 return true;
287}
Pablo Tellodb8485a2019-09-24 11:03:47 +0100288
289cl::Kernel create_opencl_kernel(CLCoreRuntimeContext *ctx, const std::string &kernel_name, const CLBuildOptions &build_opts)
290{
291 if(ctx && ctx->kernel_library())
292 {
293 //New api going through the core context
294 return static_cast<cl::Kernel>(ctx->kernel_library()->create_kernel(kernel_name, build_opts.options()));
295 }
296 else
297 {
298 //Legacy code through the singleton
299 return static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
300 }
301}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100302} // namespace arm_compute