blob: 1a639e47f92cd35c19d153afcd1a0d57bfd0e36b [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Viet-Hoa Do48b6d172023-08-11 17:48:54 +01002 * Copyright (c) 2016-2023 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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_CLHELPERS_H
25#define ARM_COMPUTE_CLHELPERS_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
Michele Di Giorgiob8fc60f2018-04-25 11:58:07 +010027#include "arm_compute/core/CL/CLTypes.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/CL/OpenCL.h"
Georgios Pinitasc6f95102021-03-30 10:03:01 +010029#include "arm_compute/core/Types.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030
Michalis Spyrou11d49182020-03-26 10:31:32 +000031#include <set>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032#include <string>
33
34namespace arm_compute
35{
Michalis Spyrou11d49182020-03-26 10:31:32 +000036class CLCompileContext;
Pablo Tellodb8485a2019-09-24 11:03:47 +010037class CLBuildOptions;
38
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039enum class DataType;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040
41/** Max vector width of an OpenCL vector */
Anthony Barbier7068f992017-10-26 15:23:08 +010042static constexpr unsigned int max_cl_vector_width = 16;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010043
Gian Marco Iodice5e281812021-07-06 13:19:41 +010044/** Max number of manual loop unrolling */
45static constexpr int max_manual_loop_unrolling = 128;
46
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047/** Translates a tensor data type to the appropriate OpenCL type.
48 *
49 * @param[in] dt @ref DataType to be translated to OpenCL type.
50 *
51 * @return The string specifying the OpenCL type to be used.
52 */
53std::string get_cl_type_from_data_type(const DataType &dt);
54
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010055/** Translates a tensor data type to the appropriate OpenCL promoted type.
56 *
57 * @param[in] dt @ref DataType to be used to get the promoted OpenCL type.
58 *
59 * @return The string specifying the OpenCL type to be used.
60 */
61std::string get_cl_promoted_type_from_data_type(const DataType &dt);
62
63/** Translates the element size to an unsigned integer data type
64 *
65 * @param[in] element_size Size in bytes of an element.
66 *
67 * @return The string specifying the OpenCL type to be used.
68 */
69std::string get_cl_unsigned_type_from_element_size(size_t element_size);
70
Michalis Spyrou7317e392020-01-17 11:27:49 +000071/** Translates the element size to an signed integer data type
72 *
73 * @param[in] element_size Size in bytes of an element.
74 *
75 * @return The string specifying the OpenCL type to be used.
76 */
77std::string get_cl_signed_type_from_element_size(size_t element_size);
78
Giorgio Arena73023022018-09-04 14:55:55 +010079/** Translates a tensor data type to the appropriate OpenCL select type.
80 *
81 * @param[in] dt @ref DataType to be translated to OpenCL select type.
82 *
83 * @return The string specifying the OpenCL select type to be used.
84 */
85std::string get_cl_select_type_from_data_type(const DataType &dt);
86
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000087/** Translates a tensor data type to the appropriate OpenCL dot8 accumulator type.
88 *
89 * @param[in] dt @ref DataType to be translated to OpenCL dot8 accumulator type.
90 *
91 * @return The string specifying the OpenCL dot8 accumulator type to be used.
92 */
93std::string get_cl_dot8_acc_type_from_data_type(const DataType &dt);
94
SiCong Lic51b72f2017-07-28 14:46:20 +010095/** Get the size of a data type in number of bits.
96 *
97 * @param[in] dt @ref DataType.
98 *
99 * @return Number of bits in the data type specified.
100 */
101std::string get_data_size_from_data_type(const DataType &dt);
102
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103/** Helper function to get the GPU target from CL device
104 *
105 * @param[in] device A CL device
106 *
107 * @return the GPU target
108 */
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100109GPUTarget get_target_from_device(const cl::Device &device);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100110
steniu0134702472017-07-11 09:22:58 +0100111/** Helper function to get the highest OpenCL version supported
112 *
113 * @param[in] device A CL device
114 *
115 * @return the highest OpenCL version supported
116 */
117CLVersion get_cl_version(const cl::Device &device);
Vidhya Sudhan Loganathaneb8a3992018-04-10 12:23:22 +0100118
Gian Marco Iodicea98dee22020-06-02 12:12:35 +0100119/** Helper function to get the cl_image pitch alignment in pixels
120 *
121 * @param[in] device A CL device
122 *
123 * @return the cl_image pitch alignment in pixels. If an error occurs, the function will return 0
124 */
125size_t get_cl_image_pitch_alignment(const cl::Device &device);
126
Viet-Hoa Dof8bb0922022-05-30 15:15:15 +0100127/** Helper function to check whether non-uniform work group is supported
128 *
129 * @param[in] device A CL device
130 *
131 * @return True if the feature is supported
132 */
133bool get_cl_non_uniform_work_group_supported(const cl::Device &device);
134
Vidhya Sudhan Loganathaneb8a3992018-04-10 12:23:22 +0100135/** Helper function to check whether a given extension is supported
136 *
137 * @param[in] device A CL device
138 * @param[in] extension_name Name of the extension to be checked
139 *
140 * @return True if the extension is supported
141 */
142bool device_supports_extension(const cl::Device &device, const char *extension_name);
143
Matthew Bentham6f31f8c2017-10-27 11:50:06 +0100144/** Helper function to check whether the cl_khr_fp16 extension is supported
145 *
146 * @param[in] device A CL device
147 *
148 * @return True if the extension is supported
149 */
Anthony Barbierd727e852018-04-20 11:05:29 +0100150bool fp16_supported(const cl::Device &device);
steniu0134702472017-07-11 09:22:58 +0100151/** Helper function to check whether the arm_non_uniform_work_group_size extension is supported
152 *
153 * @param[in] device A CL device
154 *
155 * @return True if the extension is supported
156 */
Anthony Barbierd727e852018-04-20 11:05:29 +0100157bool arm_non_uniform_workgroup_supported(const cl::Device &device);
Michalis Spyroue03342e2018-01-15 14:39:13 +0000158/** Helper function to check whether the cl_arm_integer_dot_product_int8 extension is supported
159 *
160 * @param[in] device A CL device
161 *
162 * @return True if the extension is supported
163 */
164bool dot8_supported(const cl::Device &device);
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100165
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100166/** Helper function to check whether the cl_arm_integer_dot_product_accumulate_int8 extension is supported
167 *
168 * @param[in] device A CL device
169 *
170 * @return True if the extension is supported
171 */
172bool dot8_acc_supported(const cl::Device &device);
173
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100174/** This function checks if the Winograd configuration (defined through the output tile, kernel size and the data layout) is supported on OpenCL
175 *
176 * @param[in] output_tile Output tile for the Winograd filtering algorithm
177 * @param[in] kernel_size Kernel size for the Winograd filtering algorithm
178 * @param[in] data_layout Data layout of the input tensor
179 *
180 * @return True if the configuration is supported
181 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100182bool cl_winograd_convolution_layer_supported(const Size2D &output_tile,
183 const Size2D &kernel_size,
184 DataLayout data_layout);
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +0000185
186/** Helper function to get the preferred native vector width size for built-in scalar types that can be put into vectors
187 *
188 * @param[in] device A CL device
189 * @param[in] dt data type
190 *
191 * @return preferred vector width
192 */
193size_t preferred_vector_width(const cl::Device &device, DataType dt);
Gian Marco Iodiceb0c50372019-03-15 10:13:05 +0000194
195/** Helper function to check if "dummy work-items" are preferred to have a power of two NDRange
196 * In case dummy work-items is enabled, it is OpenCL kernel responsibility to check if the work-item is out-of range or not
197 *
198 * @param[in] device A CL device
199 *
200 * @return True if dummy work-items should be preferred to dispatch the NDRange
201 */
202bool preferred_dummy_work_items_support(const cl::Device &device);
Pablo Tellodb8485a2019-09-24 11:03:47 +0100203
Gian Marco Iodicea98dee22020-06-02 12:12:35 +0100204/** Helper function to check whether the cl_khr_image2d_from_buffer extension is supported
205 *
206 * @param[in] device A CL device
207 *
208 * @return True if the extension is supported
209 */
210bool image2d_from_buffer_supported(const cl::Device &device);
211
Michalis Spyrou11d49182020-03-26 10:31:32 +0000212/** Creates an opencl kernel using a compile context
213 *
214 * @param[in] ctx A compile context to be used to create the opencl kernel.
215 * @param[in] kernel_name The kernel name.
216 * @param[in] build_opts The build options to be used for the opencl kernel compilation.
217 *
218 * @return An opencl kernel
219 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100220cl::Kernel create_kernel(const CLCompileContext &ctx,
221 const std::string &kernel_name,
222 const std::set<std::string> &build_opts = std::set<std::string>());
Michalis Spyrou11d49182020-03-26 10:31:32 +0000223
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100224/** Creates a suitable LWS hint object for parallel implementations. Sets the number of WG based on the input size.
225 * If input width is smaller than 128 we can use fewer threads than 8.
226 *
227 * @param[in] input_dimension number of elements along the dimension to apply the parallellization
228 * @param[in] vector_size size of the vector in OpenCL
229 *
230 * @return An LWS hint object
231 */
232cl::NDRange create_lws_hint_parallel_implementations(unsigned int input_dimension, unsigned int vector_size);
233
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000234/* Helper function to check if the workgroup batch size modifier parameter is supported on the cl device
235 *
236 * @param[in] device cl device to check for support
237 *
238 * @return true if the workgroup batch size modifier parameter is supported, false otherwise
239 */
240bool get_wbsm_support_info(const cl::Device &device);
241
242/* Helper function to set the workgroup batch size modifier parameter in the kernel
243 *
244 * @param[in] kernel cl kernel to set the workgroup batch size modifier parameter
245 * @param[in] wbsm_hint workgroup batch size modifier to use
246 */
247void set_wbsm(cl::Kernel &kernel, cl_int wbsm_hint);
248
Gian Marco Iodicead9a7ed2022-09-16 14:14:21 +0100249/* Helper function to check if we can export the tensor to cl_image
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100250 *
Gian Marco Iodicead9a7ed2022-09-16 14:14:21 +0100251 * @param[in] input tensor
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100252 *
Gian Marco Iodicead9a7ed2022-09-16 14:14:21 +0100253 * @return true if we can export the tensor to cl_image
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100254 */
Gian Marco Iodicead9a7ed2022-09-16 14:14:21 +0100255bool export_to_cl_image(const ITensorInfo *tensor);
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100256
Gian Marco Iodice5e281812021-07-06 13:19:41 +0100257/* Helper function to force unroll with pragma when any of the input values (iterations) are greater than @ref max_manual_loop_unrolling
258 *
259 * This function passes UNROLL_WITH_PRAGMA at compile time when any of the input values are greater than @ref max_manual_loop_unrolling
260 *
261 * @param[in] built_opts OpenCL kernel build options
262 * @param[in] values Input values (iterations)
263 *
264 */
265void set_unroll_with_pragma(CLBuildOptions &built_opts, std::initializer_list<int> values);
266
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000267/** Helper function to check whether the cl_arm_matrix_multiply extension is supported
268 *
269 * @param[in] device A CL device
270 *
271 * @return True if the extension is supported
272 */
273bool arm_matrix_multiply_supported(const cl::Device &device);
Viet-Hoa Do48b6d172023-08-11 17:48:54 +0100274
275/** Check whether cl_khr_command_buffer extension is supported by the specified CL device.
276 *
277 * @param[in] device The CL device
278 *
279 * @return True if the extension is supported by the CL device.
280 */
281bool command_buffer_supported(const cl::Device &device);
282
283/** Check whether cl_khr_command_buffer_mutable_dispatch extension is supported by the specified CL device.
284 *
285 * @param[in] device The CL device
286 *
287 * @return True if the extension is supported by the CL device.
288 */
289bool command_buffer_mutable_dispatch_supported(const cl::Device &device);
290
Georgios Pinitas7ae80a92019-10-25 18:25:17 +0100291} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000292#endif /* ARM_COMPUTE_CLHELPERS_H */