blob: df06aff647e7316bed5bb296a284d1dc8e3ce409 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Anthony Barbier30a63422018-02-28 18:18:24 +00002 * Copyright (c) 2016-2018 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:
40 return "uchar";
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010041 case DataType::QS8:
42 return "qs8";
Anthony Barbier6ff3b192017-09-04 18:44:23 +010043 case DataType::S8:
44 return "char";
Michel Iwaniec00633802017-10-12 14:14:15 +010045 case DataType::QASYMM8:
46 return "uchar";
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047 case DataType::U16:
48 return "ushort";
49 case DataType::S16:
50 return "short";
Gian Marco Iodice5cb4c422017-06-23 10:38:25 +010051 case DataType::QS16:
52 return "qs16";
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053 case DataType::U32:
54 return "uint";
55 case DataType::S32:
56 return "int";
Michalis Spyroudef665a2017-08-14 11:26:37 +010057 case DataType::QS32:
58 return "qs32";
Anthony Barbier6ff3b192017-09-04 18:44:23 +010059 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
SiCong Lic51b72f2017-07-28 14:46:20 +010073std::string get_data_size_from_data_type(const DataType &dt)
74{
75 switch(dt)
76 {
77 case DataType::U8:
78 case DataType::QS8:
79 case DataType::S8:
Michel Iwaniec00633802017-10-12 14:14:15 +010080 case DataType::QASYMM8:
SiCong Lic51b72f2017-07-28 14:46:20 +010081 return "8";
82 case DataType::U16:
83 case DataType::S16:
84 case DataType::QS16:
85 case DataType::F16:
86 return "16";
87 case DataType::U32:
88 case DataType::S32:
89 case DataType::F32:
90 return "32";
91 case DataType::U64:
92 case DataType::S64:
93 return "64";
94 default:
95 ARM_COMPUTE_ERROR("Unsupported input data type.");
96 return "0";
97 }
98}
99
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100100std::string get_underlying_cl_type_from_data_type(const DataType &dt)
101{
102 switch(dt)
103 {
104 case DataType::QS8:
105 return "char";
106 case DataType::QS16:
107 return "short";
Michalis Spyroudef665a2017-08-14 11:26:37 +0100108 case DataType::QS32:
109 return "int";
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100110 default:
111 return get_cl_type_from_data_type(dt);
112 }
113}
114
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115GPUTarget get_target_from_device(cl::Device &device)
116{
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100117 // Query device name size
Anthony Barbier30a63422018-02-28 18:18:24 +0000118 std::string device_name = device.getInfo<CL_DEVICE_NAME>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100119
Michalis Spyroua9676112018-02-22 18:07:43 +0000120 return get_target_from_name(device_name);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100121}
122
Anthony Barbierd727e852018-04-20 11:05:29 +0100123bool arm_non_uniform_workgroup_supported(const cl::Device &device)
steniu0134702472017-07-11 09:22:58 +0100124{
Vidhya Sudhan Loganathaneb8a3992018-04-10 12:23:22 +0100125 return device_supports_extension(device, "cl_arm_non_uniform_work_group_size");
Matthew Bentham6f31f8c2017-10-27 11:50:06 +0100126}
steniu0134702472017-07-11 09:22:58 +0100127
Anthony Barbierd727e852018-04-20 11:05:29 +0100128bool fp16_supported(const cl::Device &device)
Matthew Bentham6f31f8c2017-10-27 11:50:06 +0100129{
Vidhya Sudhan Loganathaneb8a3992018-04-10 12:23:22 +0100130 return device_supports_extension(device, "cl_khr_fp16");
steniu0134702472017-07-11 09:22:58 +0100131}
132
Michalis Spyroue03342e2018-01-15 14:39:13 +0000133bool dot8_supported(const cl::Device &device)
134{
135 return device_supports_extension(device, "cl_arm_integer_dot_product_int8");
136}
137
steniu0134702472017-07-11 09:22:58 +0100138CLVersion get_cl_version(const cl::Device &device)
139{
Anthony Barbier30a63422018-02-28 18:18:24 +0000140 std::string version_str = device.getInfo<CL_DEVICE_VERSION>();
steniu0134702472017-07-11 09:22:58 +0100141 if(version_str.find("OpenCL 2") != std::string::npos)
142 {
143 return CLVersion::CL20;
144 }
145 else if(version_str.find("OpenCL 1.2") != std::string::npos)
146 {
147 return CLVersion::CL12;
148 }
149 else if(version_str.find("OpenCL 1.1") != std::string::npos)
150 {
151 return CLVersion::CL11;
152 }
153 else if(version_str.find("OpenCL 1.0") != std::string::npos)
154 {
155 return CLVersion::CL10;
156 }
157
158 return CLVersion::UNKNOWN;
159}
160
Vidhya Sudhan Loganathaneb8a3992018-04-10 12:23:22 +0100161bool device_supports_extension(const cl::Device &device, const char *extension_name)
162{
163 std::string extensions = device.getInfo<CL_DEVICE_EXTENSIONS>();
164 auto pos = extensions.find(extension_name);
165 return (pos != std::string::npos);
166}
167
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100168bool cl_winograd_convolution_layer_supported(const Size2D &output_tile, const Size2D &kernel_size, DataLayout data_layout)
169{
170 ARM_COMPUTE_ERROR_ON(data_layout == DataLayout::UNKNOWN);
171
172 using WinogradConfiguration = std::pair<std::pair<int, int>, std::pair<int, int>>;
173
174 std::vector<WinogradConfiguration> winograd_filter_transform_nchw =
175 {
176 WinogradConfiguration(std::pair<int, int>(1, 2), std::pair<int, int>(1, 3)),
177 WinogradConfiguration(std::pair<int, int>(1, 4), std::pair<int, int>(1, 3)),
178 WinogradConfiguration(std::pair<int, int>(2, 1), std::pair<int, int>(3, 1)),
179 WinogradConfiguration(std::pair<int, int>(4, 1), std::pair<int, int>(3, 1)),
180 WinogradConfiguration(std::pair<int, int>(2, 2), std::pair<int, int>(3, 3)),
181 WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(3, 3)),
182 WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(5, 5))
183 };
184
185 std::vector<WinogradConfiguration> winograd_filter_transform_nhwc =
186 {
187 WinogradConfiguration(std::pair<int, int>(2, 2), std::pair<int, int>(3, 3)),
188 WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(3, 3)),
189 WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(5, 5))
190 };
191
192 auto p = std::make_pair(std::pair<int, int>(output_tile.width, output_tile.height),
193 std::pair<int, int>(kernel_size.width, kernel_size.height));
194
195 // Return true if supported
196 if(data_layout == DataLayout::NCHW)
197 {
198 return (std::find(winograd_filter_transform_nchw.begin(), winograd_filter_transform_nchw.end(), p) != winograd_filter_transform_nchw.end());
199 }
200 else
201 {
202 return (std::find(winograd_filter_transform_nhwc.begin(), winograd_filter_transform_nhwc.end(), p) != winograd_filter_transform_nhwc.end());
203 }
204}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100205} // namespace arm_compute