blob: 0e83ff20c8291e16f6ca8279aa7360de87bdf613 [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";
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041 case DataType::S8:
42 return "char";
Michel Iwaniec00633802017-10-12 14:14:15 +010043 case DataType::QASYMM8:
44 return "uchar";
Anthony Barbier6ff3b192017-09-04 18:44:23 +010045 case DataType::U16:
46 return "ushort";
47 case DataType::S16:
48 return "short";
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049 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
SiCong Lic51b72f2017-07-28 14:46:20 +010067std::string get_data_size_from_data_type(const DataType &dt)
68{
69 switch(dt)
70 {
71 case DataType::U8:
SiCong Lic51b72f2017-07-28 14:46:20 +010072 case DataType::S8:
Michel Iwaniec00633802017-10-12 14:14:15 +010073 case DataType::QASYMM8:
SiCong Lic51b72f2017-07-28 14:46:20 +010074 return "8";
75 case DataType::U16:
76 case DataType::S16:
SiCong Lic51b72f2017-07-28 14:46:20 +010077 case DataType::F16:
78 return "16";
79 case DataType::U32:
80 case DataType::S32:
81 case DataType::F32:
82 return "32";
83 case DataType::U64:
84 case DataType::S64:
85 return "64";
86 default:
87 ARM_COMPUTE_ERROR("Unsupported input data type.");
88 return "0";
89 }
90}
91
Georgios Pinitasac4e8732017-07-05 17:02:25 +010092std::string get_underlying_cl_type_from_data_type(const DataType &dt)
93{
Vidhya Sudhan Loganathanf4cb81b2018-07-04 15:13:14 +010094 return get_cl_type_from_data_type(dt);
Georgios Pinitasac4e8732017-07-05 17:02:25 +010095}
96
Anthony Barbier6ff3b192017-09-04 18:44:23 +010097GPUTarget get_target_from_device(cl::Device &device)
98{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099 // Query device name size
Anthony Barbier30a63422018-02-28 18:18:24 +0000100 std::string device_name = device.getInfo<CL_DEVICE_NAME>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101
Michalis Spyroua9676112018-02-22 18:07:43 +0000102 return get_target_from_name(device_name);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103}
104
Anthony Barbierd727e852018-04-20 11:05:29 +0100105bool arm_non_uniform_workgroup_supported(const cl::Device &device)
steniu0134702472017-07-11 09:22:58 +0100106{
Vidhya Sudhan Loganathaneb8a3992018-04-10 12:23:22 +0100107 return device_supports_extension(device, "cl_arm_non_uniform_work_group_size");
Matthew Bentham6f31f8c2017-10-27 11:50:06 +0100108}
steniu0134702472017-07-11 09:22:58 +0100109
Anthony Barbierd727e852018-04-20 11:05:29 +0100110bool fp16_supported(const cl::Device &device)
Matthew Bentham6f31f8c2017-10-27 11:50:06 +0100111{
Vidhya Sudhan Loganathaneb8a3992018-04-10 12:23:22 +0100112 return device_supports_extension(device, "cl_khr_fp16");
steniu0134702472017-07-11 09:22:58 +0100113}
114
Michalis Spyroue03342e2018-01-15 14:39:13 +0000115bool dot8_supported(const cl::Device &device)
116{
117 return device_supports_extension(device, "cl_arm_integer_dot_product_int8");
118}
119
steniu0134702472017-07-11 09:22:58 +0100120CLVersion get_cl_version(const cl::Device &device)
121{
Anthony Barbier30a63422018-02-28 18:18:24 +0000122 std::string version_str = device.getInfo<CL_DEVICE_VERSION>();
steniu0134702472017-07-11 09:22:58 +0100123 if(version_str.find("OpenCL 2") != std::string::npos)
124 {
125 return CLVersion::CL20;
126 }
127 else if(version_str.find("OpenCL 1.2") != std::string::npos)
128 {
129 return CLVersion::CL12;
130 }
131 else if(version_str.find("OpenCL 1.1") != std::string::npos)
132 {
133 return CLVersion::CL11;
134 }
135 else if(version_str.find("OpenCL 1.0") != std::string::npos)
136 {
137 return CLVersion::CL10;
138 }
139
140 return CLVersion::UNKNOWN;
141}
142
Vidhya Sudhan Loganathaneb8a3992018-04-10 12:23:22 +0100143bool device_supports_extension(const cl::Device &device, const char *extension_name)
144{
145 std::string extensions = device.getInfo<CL_DEVICE_EXTENSIONS>();
146 auto pos = extensions.find(extension_name);
147 return (pos != std::string::npos);
148}
149
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100150bool cl_winograd_convolution_layer_supported(const Size2D &output_tile, const Size2D &kernel_size, DataLayout data_layout)
151{
152 ARM_COMPUTE_ERROR_ON(data_layout == DataLayout::UNKNOWN);
153
154 using WinogradConfiguration = std::pair<std::pair<int, int>, std::pair<int, int>>;
155
Giorgio Arena149fdf32018-07-04 17:03:33 +0100156 std::vector<WinogradConfiguration> winograd_configs_nchw =
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100157 {
158 WinogradConfiguration(std::pair<int, int>(1, 2), std::pair<int, int>(1, 3)),
159 WinogradConfiguration(std::pair<int, int>(1, 4), std::pair<int, int>(1, 3)),
160 WinogradConfiguration(std::pair<int, int>(2, 1), std::pair<int, int>(3, 1)),
161 WinogradConfiguration(std::pair<int, int>(4, 1), std::pair<int, int>(3, 1)),
162 WinogradConfiguration(std::pair<int, int>(2, 2), std::pair<int, int>(3, 3)),
163 WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(3, 3)),
Gian Marco Iodice876be2a2018-07-03 12:22:09 +0100164 WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(5, 5)),
165 WinogradConfiguration(std::pair<int, int>(4, 1), std::pair<int, int>(5, 1)),
166 WinogradConfiguration(std::pair<int, int>(1, 4), std::pair<int, int>(1, 5))
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100167 };
168
Giorgio Arena149fdf32018-07-04 17:03:33 +0100169 std::vector<WinogradConfiguration> winograd_configs_nhwc =
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100170 {
171 WinogradConfiguration(std::pair<int, int>(2, 2), std::pair<int, int>(3, 3)),
Giorgio Arena149fdf32018-07-04 17:03:33 +0100172 WinogradConfiguration(std::pair<int, int>(1, 4), std::pair<int, int>(1, 3)),
173 WinogradConfiguration(std::pair<int, int>(4, 1), std::pair<int, int>(3, 1)),
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100174 WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(3, 3)),
Gian Marco Iodiced28b7512018-07-06 12:59:28 +0100175 WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(5, 5)),
176 WinogradConfiguration(std::pair<int, int>(4, 1), std::pair<int, int>(5, 1)),
177 WinogradConfiguration(std::pair<int, int>(1, 4), std::pair<int, int>(1, 5))
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100178 };
179
180 auto p = std::make_pair(std::pair<int, int>(output_tile.width, output_tile.height),
181 std::pair<int, int>(kernel_size.width, kernel_size.height));
182
183 // Return true if supported
184 if(data_layout == DataLayout::NCHW)
185 {
Giorgio Arena149fdf32018-07-04 17:03:33 +0100186 return (std::find(winograd_configs_nchw.begin(), winograd_configs_nchw.end(), p) != winograd_configs_nchw.end());
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100187 }
188 else
189 {
Giorgio Arena149fdf32018-07-04 17:03:33 +0100190 return (std::find(winograd_configs_nhwc.begin(), winograd_configs_nhwc.end(), p) != winograd_configs_nhwc.end());
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100191 }
192}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100193} // namespace arm_compute