blob: 0509f1e7858707cbb3f0e99be2138172b1a6ed26 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michalis Spyrou5237e012018-01-17 09:40:27 +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/CLKernelLibrary.h"
25
steniu0134702472017-07-11 09:22:58 +010026#include "arm_compute/core/CL/CLHelpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/Error.h"
28#include "arm_compute/core/Utils.h"
29
steniu015f910722017-08-23 10:15:22 +010030#include <algorithm>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#include <fstream>
32#include <iostream>
33#include <utility>
34#include <vector>
35
36using namespace arm_compute;
37
Georgios Pinitas388d3ec2017-11-02 12:17:56 +000038CLBuildOptions::CLBuildOptions()
39 : _build_opts()
40{
41}
42
43void CLBuildOptions::add_option(std::string option)
44{
45 _build_opts.emplace(std::move(option));
46}
47
48void CLBuildOptions::add_option_if(bool cond, std::string option)
49{
50 if(cond)
51 {
52 add_option(std::move(option));
53 }
54}
55
56void CLBuildOptions::add_option_if_else(bool cond, std::string option_true, std::string option_false)
57{
58 (cond) ? add_option(std::move(option_true)) : add_option(std::move(option_false));
59}
60
Chunosovf450caa2017-11-08 16:09:35 +070061void CLBuildOptions::add_options(const StringSet &options)
62{
63 _build_opts.insert(options.begin(), options.end());
64}
65
66void CLBuildOptions::add_options_if(bool cond, const StringSet &options)
67{
68 if(cond)
69 {
70 add_options(options);
71 }
72}
73
Chunosovd621bca2017-11-03 17:33:15 +070074const CLBuildOptions::StringSet &CLBuildOptions::options() const
Georgios Pinitas388d3ec2017-11-02 12:17:56 +000075{
76 return _build_opts;
77}
78
Anthony Barbier6ff3b192017-09-04 18:44:23 +010079Program::Program()
80 : _context(), _device(), _is_binary(false), _name(), _source(), _binary()
81{
82}
83
84Program::Program(cl::Context context, std::string name, std::string source)
85 : _context(std::move(context)), _device(), _is_binary(false), _name(std::move(name)), _source(std::move(source)), _binary()
86{
87}
88
89Program::Program(cl::Context context, cl::Device device, std::string name, std::vector<unsigned char> binary)
90 : _context(std::move(context)), _device(std::move(device)), _is_binary(true), _name(std::move(name)), _source(), _binary(std::move(binary))
91{
92}
93
94Program::operator cl::Program() const
95{
96 if(_is_binary)
97 {
98 return cl::Program(_context, { _device }, { _binary });
99 }
100 else
101 {
102 return cl::Program(_context, _source, false);
103 }
104}
105
106bool Program::build(const cl::Program &program, const std::string &build_options)
107{
108 try
109 {
110 return program.build(build_options.c_str()) == CL_SUCCESS;
111 }
112 catch(const cl::Error &e)
113 {
114 cl_int err = CL_SUCCESS;
115 const auto build_info = program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(&err);
116
117 for(auto &pair : build_info)
118 {
119 std::cerr << pair.second << std::endl;
120 }
121
122 return false;
123 }
124}
125
126cl::Program Program::build(const std::string &build_options) const
127{
128 cl::Program cl_program = static_cast<cl::Program>(*this);
129 build(cl_program, build_options);
130 return cl_program;
131}
132
133Kernel::Kernel()
134 : _name(), _kernel()
135{
136}
137
138Kernel::Kernel(std::string name, const cl::Program &program)
139 : _name(std::move(name)),
140 _kernel(cl::Kernel(program, _name.c_str()))
141{
142}
143
144const std::map<std::string, std::string> CLKernelLibrary::_kernel_program_map =
145{
146 { "absdiff", "absdiff.cl" },
147 { "accumulate", "accumulate.cl" },
148 { "accumulate_squared", "accumulate.cl" },
149 { "accumulate_weighted", "accumulate.cl" },
150 { "activation_layer", "activation_layer.cl" },
Michel Iwaniec00633802017-10-12 14:14:15 +0100151 { "activation_layer_qa8", "activation_layer_qa8.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152 { "arithmetic_add", "arithmetic_op.cl" },
153 { "arithmetic_sub", "arithmetic_op.cl" },
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000154 { "batchnormalization_layer", "batchnormalization_layer.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100155 { "bitwise_or", "bitwise_op.cl" },
156 { "bitwise_and", "bitwise_op.cl" },
157 { "bitwise_xor", "bitwise_op.cl" },
158 { "bitwise_not", "bitwise_op.cl" },
159 { "channel_combine_NV", "channel_combine.cl" },
160 { "channel_combine_RGB888", "channel_combine.cl" },
161 { "channel_combine_RGBA8888", "channel_combine.cl" },
162 { "channel_combine_UYVY422", "channel_combine.cl" },
163 { "channel_combine_YUYV422", "channel_combine.cl" },
164 { "channel_extract_NV12", "channel_extract.cl" },
165 { "channel_extract_NV21", "channel_extract.cl" },
166 { "channel_extract_RGB888", "channel_extract.cl" },
167 { "channel_extract_RGBA8888", "channel_extract.cl" },
168 { "channel_extract_UYVY422", "channel_extract.cl" },
169 { "channel_extract_YUYV422", "channel_extract.cl" },
170 { "combine_gradients_L1", "canny.cl" },
171 { "combine_gradients_L2", "canny.cl" },
172 { "concatenate_depth", "concatenate.cl" },
173 { "convolution_rectangle", "convolution_rectangle.cl" },
Gian Marco76faef82018-01-29 12:15:32 +0000174 { "col2im", "col2im.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100175 { "convolution3x3_static", "convolution3x3.cl" },
176 { "convolution5x5_static", "convolution5x5.cl" },
177 { "convolution7x7_static", "convolution7x7.cl" },
178 { "convolution9x9_static", "convolution9x9.cl" },
179 { "convolution_separable1x5_static", "convolution5x5.cl" },
180 { "convolution_separable5x1_static", "convolution5x5.cl" },
181 { "convolution_separable1x7_static", "convolution7x7.cl" },
182 { "convolution_separable7x1_static", "convolution7x7.cl" },
183 { "convolution_separable1x9_static", "convolution9x9.cl" },
184 { "convolution_separable9x1_static", "convolution9x9.cl" },
185 { "convert_depth_down", "depth_convert.cl" },
186 { "convert_depth_up", "depth_convert.cl" },
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +0000187 { "copy_tensor", "copy_tensor.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100188 { "copy_plane", "channel_extract.cl" },
189 { "copy_planes_3p", "channel_combine.cl" },
190 { "copy_to_keypoint", "fast_corners.cl" },
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000191 { "deconvolution_upsample", "deconvolution_layer.cl" },
Giorgio Arena93a690e2017-08-01 16:09:33 +0100192 { "depthwise_convolution_3x3", "depthwise_convolution.cl" },
Michele Di Giorgio933fe862018-02-19 15:42:12 +0000193 { "depthwise_convolution_3x3_f16", "depthwise_convolution.cl" },
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000194 { "depthwise_convolution_3x3_quantized_nchw", "depthwise_convolution_quantized.cl" },
195 { "depthwise_convolution_3x3_quantized_nhwc_stride1", "depthwise_convolution_quantized.cl" },
196 { "depthwise_convolution_3x3_quantized_nhwc_stride2", "depthwise_convolution_quantized.cl" },
Michele Di Giorgio3ebef322018-02-21 10:02:58 +0000197 { "depthwise_convolution_3x3_stridex1_stridey1_bifrost_f16", "depthwise_convolution.cl" },
198 { "depthwise_convolution_3x3_stridex2_stridey2_bifrost_f16", "depthwise_convolution.cl" },
199 { "depthwise_convolution_3x3_stridex1_stridey1_bifrost_f32", "depthwise_convolution.cl" },
200 { "depthwise_convolution_3x3_stridex2_stridey2_bifrost_f32", "depthwise_convolution.cl" },
Giorgio Arena9fe41442017-08-23 16:36:24 +0100201 { "depthwise_im2col", "depthwise_convolution.cl" },
202 { "depthwise_vector_to_tensor", "depthwise_convolution.cl" },
203 { "depthwise_weights_reshape", "depthwise_convolution.cl" },
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100204 { "dequantization_layer", "dequantization_layer.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100205 { "derivative", "derivative.cl" },
206 { "dilate", "dilate.cl" },
SiCong Lic51b72f2017-07-28 14:46:20 +0100207 { "direct_convolution1x1", "direct_convolution1x1.cl" },
Gian Marco Iodice1c8409d2017-09-06 17:24:25 +0100208 { "direct_convolution1x1_f32_bifrost", "direct_convolution1x1.cl" },
SiCong Lic51b72f2017-07-28 14:46:20 +0100209 { "direct_convolution3x3", "direct_convolution3x3.cl" },
Gian Marco Iodice1246b632017-08-16 18:38:32 +0100210 { "direct_convolution3x3_f32_bifrost", "direct_convolution3x3.cl" },
steniu01db006682017-08-09 16:26:22 +0100211 { "direct_convolution5x5", "direct_convolution5x5.cl" },
Gian Marco Iodice1246b632017-08-16 18:38:32 +0100212 { "direct_convolution5x5_f32_bifrost", "direct_convolution5x5.cl" },
Chunosovd621bca2017-11-03 17:33:15 +0700213 { "direct_convolution_1x1_3x3_5x5_quantized", "direct_convolution_1x1_3x3_5x5_quantized.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100214 { "erode", "erode.cl" },
215 { "fast_corners", "fast_corners.cl" },
216 { "fill_image_borders_constant", "fill_border.cl" },
217 { "fill_image_borders_replicate", "fill_border.cl" },
218 { "finalize", "optical_flow_pyramid_lk.cl" },
Georgios Pinitasd8e765b2017-08-02 13:44:33 +0100219 { "floor_layer", "floor.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100220 { "gaussian1x5_sub_x", "gaussian_pyramid.cl" },
221 { "gaussian5x1_sub_y", "gaussian_pyramid.cl" },
Gian Marco Iodice578ab612017-06-23 09:34:33 +0100222 { "gemm_accumulate_biases", "gemm.cl" },
Gian Marco36a0a462018-01-12 10:21:40 +0000223 { "gemm_interleave4x4", "gemm.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100224 { "gemm_ma_f16", "gemm.cl" },
225 { "gemm_ma_f32", "gemm.cl" },
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +0100226 { "gemm_ma_qs8", "gemm.cl" },
Gian Marco Iodice8a383692017-07-03 17:41:47 +0100227 { "gemm_ma_qs16", "gemm.cl" },
Giorgio Arena9fe41442017-08-23 16:36:24 +0100228 { "gemm_mv", "gemv.cl" },
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000229 { "gemm_mv_quantized", "gemv.cl" },
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100230 { "gemm_mm_interleaved_transposed_f16", "gemm.cl" },
231 { "gemm_mm_interleaved_transposed_f32_midgard", "gemm.cl" },
232 { "gemm_mm_interleaved_transposed_f32_bifrost", "gemm.cl" },
233 { "gemm_mm_interleaved_transposed_qs8", "gemm.cl" },
234 { "gemm_mm_interleaved_transposed_qs16", "gemm.cl" },
235 { "gemm_mm_floating_point", "gemm.cl" },
Anton Lokhmotov3e80c7f2017-11-20 11:02:10 +0000236 { "gemm_mm_floating_point_f32_bifrost", "gemm.cl" },
237 { "gemm_mm_floating_point_f32_bifrost_1000", "gemm.cl" },
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +0100238 { "gemm_mm_qs8", "gemm.cl" },
Gian Marco Iodice8a383692017-07-03 17:41:47 +0100239 { "gemm_mm_qs16", "gemm.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100240 { "gemm_lc_vm_f32", "gemm.cl" },
Gian Marco36a0a462018-01-12 10:21:40 +0000241 { "gemm_transpose1xW", "gemm.cl" },
Gian Marco05288a22017-11-21 10:57:50 +0000242 { "gemmlowp_matrix_a_reduction", "gemmlowp.cl" },
243 { "gemmlowp_matrix_b_reduction", "gemmlowp.cl" },
Gian Marco7b4d5472018-01-10 15:56:30 +0000244 { "gemmlowp_mm_bifrost", "gemmlowp.cl" },
245 { "gemmlowp_mm_midgard", "gemmlowp.cl" },
Gian Marco19835e52018-01-30 13:35:54 +0000246 { "gemmlowp_mm_interleaved_transposed_bifrost", "gemmlowp.cl" },
247 { "gemmlowp_mm_interleaved_transposed_midgard", "gemmlowp.cl" },
Gian Marco05288a22017-11-21 10:57:50 +0000248 { "gemmlowp_offset_contribution", "gemmlowp.cl" },
249 { "gemmlowp_output_stage_quantize_down", "gemmlowp.cl" },
Gian Marco58c57942017-11-28 09:10:03 +0000250 { "gemmlowp_output_stage_quantize_down_fixedpoint", "gemmlowp.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100251 { "harris_score_3x3", "harris_corners.cl" },
252 { "harris_score_5x5", "harris_corners.cl" },
253 { "harris_score_7x7", "harris_corners.cl" },
254 { "hist_border_kernel", "histogram.cl" },
255 { "hist_border_kernel_fixed", "histogram.cl" },
256 { "hist_local_kernel", "histogram.cl" },
257 { "hist_local_kernel_fixed", "histogram.cl" },
258 { "hog_block_normalization", "hog.cl" },
259 { "hog_detector", "hog.cl" },
260 { "hog_orientation_binning", "hog.cl" },
261 { "hysteresis", "canny.cl" },
Gian Marco76faef82018-01-29 12:15:32 +0000262 { "im2col1x1_stridex1_dchw", "im2col.cl" },
263 { "im2col3x3_dchw", "im2col.cl" },
264 { "im2col5x5_dchw", "im2col.cl" },
265 { "im2col11x11_padx0_pady0_dchw", "im2col.cl" },
266 { "im2col_generic_dchw", "im2col.cl" },
267 { "im2col_generic_padx0_pady0_dchw", "im2col.cl" },
268 { "im2col_reduced_dchw", "im2col.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100269 { "init_level", "optical_flow_pyramid_lk.cl" },
270 { "init_level_max", "optical_flow_pyramid_lk.cl" },
271 { "init_level_max_initial_estimate", "optical_flow_pyramid_lk.cl" },
272 { "integral_horizontal", "integral_image.cl" },
273 { "integral_vertical", "integral_image.cl" },
274 { "IYUV_to_NV12_bt709", "color_convert.cl" },
275 { "IYUV_to_RGB888_bt709", "color_convert.cl" },
276 { "IYUV_to_RGBA8888_bt709", "color_convert.cl" },
277 { "IYUV_to_YUV444_bt709", "color_convert.cl" },
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100278 { "l2_normalize", "l2_normalize.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100279 { "lktracker_stage0", "optical_flow_pyramid_lk.cl" },
280 { "lktracker_stage1", "optical_flow_pyramid_lk.cl" },
281 { "magnitude_phase", "magnitude_phase.cl" },
282 { "mean_stddev_accumulate", "mean_stddev.cl" },
283 { "minmax", "minmaxloc.cl" },
284 { "minmax_border", "minmaxloc.cl" },
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100285 { "minmax_layer", "minmax_layer.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100286 { "minmaxloc", "minmaxloc.cl" },
287 { "non_linear_filter_box3x3", "non_linear_filter3x3.cl" },
288 { "non_linear_filter_cross3x3", "non_linear_filter3x3.cl" },
289 { "non_linear_filter_disk3x3", "non_linear_filter3x3.cl" },
290 { "non_linear_filter_box5x5", "non_linear_filter5x5.cl" },
291 { "non_linear_filter_cross5x5", "non_linear_filter5x5.cl" },
292 { "non_linear_filter_disk5x5", "non_linear_filter5x5.cl" },
293 { "non_max_suppression", "nonmax.cl" },
294 { "normalization_layer_cross_map", "normalization_layer.cl" },
Georgios Pinitas01624362017-11-30 10:53:31 +0000295 { "normalization_layer_in_map", "normalization_layer.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100296 { "NV12_to_IYUV_bt709", "color_convert.cl" },
297 { "NV12_to_RGB888_bt709", "color_convert.cl" },
298 { "NV12_to_RGBA8888_bt709", "color_convert.cl" },
299 { "NV12_to_YUV444_bt709", "color_convert.cl" },
300 { "NV21_to_IYUV_bt709", "color_convert.cl" },
301 { "NV21_to_RGB888_bt709", "color_convert.cl" },
302 { "NV21_to_RGBA8888_bt709", "color_convert.cl" },
303 { "NV21_to_YUV444_bt709", "color_convert.cl" },
Giorgio Arenaa086a0a2018-02-16 12:42:16 +0000304 { "output_stage_quantized", "direct_convolution_1x1_3x3_5x5_quantized.cl" },
Michalis Spyrou5237e012018-01-17 09:40:27 +0000305 { "permute_201", "permute.cl" },
306 { "permute_120", "permute.cl" },
307 { "permute_3201", "permute.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100308 { "pixelwise_mul_float", "pixelwise_mul_float.cl" },
309 { "pixelwise_mul_int", "pixelwise_mul_int.cl" },
310 { "pooling_layer_2", "pooling_layer.cl" },
311 { "pooling_layer_3", "pooling_layer.cl" },
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000312 { "pooling_layer_optimized_3", "pooling_layer.cl" },
Georgios Pinitasce093142017-06-19 16:11:53 +0100313 { "pooling_layer_7", "pooling_layer.cl" },
Isabella Gottardia527e8c2018-01-31 17:49:25 +0000314 { "pooling_layer_MxN", "pooling_layer.cl" },
315 { "pooling_layer_MxN_quantized", "pooling_layer_quantized.cl" },
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100316 { "quantization_layer", "quantization_layer.cl" },
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100317 { "reduction_operation", "reduction_operation.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100318 { "remap_nearest_neighbour", "remap.cl" },
319 { "remap_bilinear", "remap.cl" },
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +0100320 { "reshape_layer", "reshape_layer.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100321 { "reshape_to_columns", "convolution_layer.cl" },
322 { "RGB888_to_IYUV_bt709", "color_convert.cl" },
323 { "RGB888_to_NV12_bt709", "color_convert.cl" },
324 { "RGB888_to_RGBA8888_bt709", "color_convert.cl" },
325 { "RGB888_to_YUV444_bt709", "color_convert.cl" },
326 { "RGBA8888_to_IYUV_bt709", "color_convert.cl" },
327 { "RGBA8888_to_NV12_bt709", "color_convert.cl" },
328 { "RGBA8888_to_RGB888_bt709", "color_convert.cl" },
329 { "RGBA8888_to_YUV444_bt709", "color_convert.cl" },
SiCong Li3e363692017-07-04 15:02:10 +0100330 { "roi_pooling_layer", "roi_pooling_layer.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100331 { "scale_nearest_neighbour", "scale.cl" },
332 { "scale_bilinear", "scale.cl" },
333 { "scharr3x3", "scharr_filter.cl" },
334 { "sobel3x3", "sobel_filter.cl" },
335 { "sobel_separable5x1", "sobel_filter.cl" },
336 { "sobel_separable1x5", "sobel_filter.cl" },
337 { "sobel_separable7x1", "sobel_filter.cl" },
338 { "sobel_separable1x7", "sobel_filter.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100339 { "softmax_layer_norm", "softmax_layer.cl" },
Chunosovf450caa2017-11-08 16:09:35 +0700340 { "softmax_layer_norm_quantized", "softmax_layer_quantized.cl" },
Giorgio Arena4402cb92018-02-15 13:37:40 +0000341 { "softmax_layer_max_shift_exp_sum_quantized_serial", "softmax_layer_quantized.cl" },
342 { "softmax_layer_max_shift_exp_sum_quantized_parallel", "softmax_layer_quantized.cl" },
Chunosovd6afedc2017-11-06 22:09:45 +0700343 { "softmax_layer_max_shift_exp_sum_serial", "softmax_layer.cl" },
344 { "softmax_layer_max_shift_exp_sum_parallel", "softmax_layer.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100345 { "suppress_non_maximum", "canny.cl" },
346 { "tablelookup_U8", "tablelookup.cl" },
347 { "tablelookup_S16", "tablelookup.cl" },
348 { "threshold_binary", "threshold.cl" },
349 { "threshold_range", "threshold.cl" },
350 { "transpose", "transpose.cl" },
351 { "UYVY422_to_IYUV_bt709", "color_convert.cl" },
352 { "UYVY422_to_NV12_bt709", "color_convert.cl" },
353 { "UYVY422_to_RGB888_bt709", "color_convert.cl" },
354 { "UYVY422_to_RGBA8888_bt709", "color_convert.cl" },
355 { "warp_affine_nearest_neighbour", "warp_affine.cl" },
356 { "warp_affine_bilinear", "warp_affine.cl" },
357 { "warp_perspective_nearest_neighbour", "warp_perspective.cl" },
358 { "warp_perspective_bilinear", "warp_perspective.cl" },
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000359 { "winograd_filter_transform_2x2_3x3_nchw", "winograd.cl" },
Giorgio Arena2d9de0a2018-03-15 17:58:20 +0000360 { "winograd_filter_transform_4x4_3x3_nchw", "winograd.cl" },
Giorgio Arena9373c8b2018-04-11 19:07:17 +0100361 { "winograd_filter_transform_4x4_5x5_nchw", "winograd.cl" },
Giorgio Arenafe5ef382018-04-17 10:14:10 +0100362 { "winograd_input_transform_4x4_5x5_stepz1_nchw", "winograd.cl" },
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000363 { "winograd_input_transform_2x2_3x3_stepz1_nchw", "winograd.cl" },
364 { "winograd_input_transform_2x2_3x3_stepz2_nchw", "winograd.cl" },
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000365 { "winograd_output_transform_2x2_3x3_nchw", "winograd.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100366 { "YUYV422_to_IYUV_bt709", "color_convert.cl" },
367 { "YUYV422_to_NV12_bt709", "color_convert.cl" },
368 { "YUYV422_to_RGB888_bt709", "color_convert.cl" },
369 { "YUYV422_to_RGBA8888_bt709", "color_convert.cl" },
370};
371
372const std::map<std::string, std::string> CLKernelLibrary::_program_source_map =
373{
374#ifdef EMBEDDED_KERNELS
375 {
376 "absdiff.cl",
377#include "./cl_kernels/absdiff.clembed"
378 },
379 {
380 "accumulate.cl",
381#include "./cl_kernels/accumulate.clembed"
382 },
383 {
384 "activation_layer.cl",
385#include "./cl_kernels/activation_layer.clembed"
386 },
387 {
Michel Iwaniec00633802017-10-12 14:14:15 +0100388 "activation_layer_qa8.cl",
389#include "./cl_kernels/activation_layer_qa8.clembed"
390 },
391 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100392 "arithmetic_op.cl",
393#include "./cl_kernels/arithmetic_op.clembed"
394 },
395 {
396 "bitwise_op.cl",
397#include "./cl_kernels/bitwise_op.clembed"
398 },
399 {
400 "canny.cl",
401#include "./cl_kernels/canny.clembed"
402 },
403 {
404 "channel_combine.cl",
405#include "./cl_kernels/channel_combine.clembed"
406 },
407 {
408 "channel_extract.cl",
409#include "./cl_kernels/channel_extract.clembed"
410 },
411 {
Gian Marco76faef82018-01-29 12:15:32 +0000412 "col2im.cl",
413#include "./cl_kernels/col2im.clembed"
414 },
415 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100416 "concatenate.cl",
417#include "./cl_kernels/concatenate.clembed"
418 },
419 {
420 "color_convert.cl",
421#include "./cl_kernels/color_convert.clembed"
422 },
423 {
424 "convolution3x3.cl",
425#include "./cl_kernels/convolution3x3.clembed"
426 },
427 {
428 "convolution5x5.cl",
429#include "./cl_kernels/convolution5x5.clembed"
430 },
431 {
432 "convolution7x7.cl",
433#include "./cl_kernels/convolution7x7.clembed"
434 },
435 {
436 "convolution9x9.cl",
437#include "./cl_kernels/convolution9x9.clembed"
438 },
439 {
440 "convolution_layer.cl",
441#include "./cl_kernels/convolution_layer.clembed"
442 },
443 {
444 "convolution_rectangle.cl",
445#include "./cl_kernels/convolution_rectangle.clembed"
446 },
447 {
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +0000448 "copy_tensor.cl",
449#include "./cl_kernels/copy_tensor.clembed"
450 },
451 {
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000452 "deconvolution_layer.cl",
453#include "./cl_kernels/deconvolution_layer.clembed"
454 },
455 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100456 "depth_convert.cl",
457#include "./cl_kernels/depth_convert.clembed"
458 },
459 {
Giorgio Arena93a690e2017-08-01 16:09:33 +0100460 "depthwise_convolution.cl",
461#include "./cl_kernels/depthwise_convolution.clembed"
462 },
463 {
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700464 "depthwise_convolution_quantized.cl",
465#include "./cl_kernels/depthwise_convolution_quantized.clembed"
466 },
467 {
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100468 "dequantization_layer.cl",
469#include "./cl_kernels/dequantization_layer.clembed"
470 },
471 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100472 "derivative.cl",
473#include "./cl_kernels/derivative.clembed"
474 },
475 {
476 "dilate.cl",
477#include "./cl_kernels/dilate.clembed"
478 },
479 {
SiCong Lic51b72f2017-07-28 14:46:20 +0100480 "direct_convolution1x1.cl",
481#include "./cl_kernels/direct_convolution1x1.clembed"
482 },
483 {
484 "direct_convolution3x3.cl",
485#include "./cl_kernels/direct_convolution3x3.clembed"
steniu0127b386c2017-07-18 17:37:43 +0100486 },
487 {
steniu01db006682017-08-09 16:26:22 +0100488 "direct_convolution5x5.cl",
489#include "./cl_kernels/direct_convolution5x5.clembed"
490 },
491 {
Chunosovd621bca2017-11-03 17:33:15 +0700492 "direct_convolution_1x1_3x3_5x5_quantized.cl",
493#include "./cl_kernels/direct_convolution_1x1_3x3_5x5_quantized.clembed"
494 },
495 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100496 "erode.cl",
497#include "./cl_kernels/erode.clembed"
498 },
499 {
500 "fast_corners.cl",
501#include "./cl_kernels/fast_corners.clembed"
502 },
503 {
504 "fill_border.cl",
505#include "./cl_kernels/fill_border.clembed"
506 },
507 {
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100508 "fixed_point.h",
509#include "./cl_kernels/fixed_point.hembed"
510 },
511 {
Georgios Pinitasd8e765b2017-08-02 13:44:33 +0100512 "floor.cl",
513#include "./cl_kernels/floor.clembed"
514 },
515 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100516 "gaussian_pyramid.cl",
517#include "./cl_kernels/gaussian_pyramid.clembed"
518 },
519 {
520 "gemm.cl",
521#include "./cl_kernels/gemm.clembed"
522 },
523 {
Gian Marco05288a22017-11-21 10:57:50 +0000524 "gemmlowp.cl",
525#include "./cl_kernels/gemmlowp.clembed"
526 },
527 {
Giorgio Arena9fe41442017-08-23 16:36:24 +0100528 "gemv.cl",
529#include "./cl_kernels/gemv.clembed"
530 },
531 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100532 "harris_corners.cl",
533#include "./cl_kernels/harris_corners.clembed"
534 },
535 {
536 "helpers.h",
537#include "./cl_kernels/helpers.hembed"
538 },
539 {
Chunosovd621bca2017-11-03 17:33:15 +0700540 "helpers_asymm.h",
541#include "./cl_kernels/helpers_asymm.hembed"
542 },
543 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100544 "histogram.cl",
545#include "./cl_kernels/histogram.clembed"
546 },
547 {
548 "hog.cl",
549#include "./cl_kernels/hog.clembed"
550 },
551 {
Gian Marco76faef82018-01-29 12:15:32 +0000552 "im2col.cl",
553#include "./cl_kernels/im2col.clembed"
554 },
555 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100556 "integral_image.cl",
557#include "./cl_kernels/integral_image.clembed"
558 },
559 {
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100560 "l2_normalize.cl",
561#include "./cl_kernels/l2_normalize.clembed"
562 },
563 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100564 "magnitude_phase.cl",
565#include "./cl_kernels/magnitude_phase.clembed"
566 },
567 {
568 "mean_stddev.cl",
569#include "./cl_kernels/mean_stddev.clembed"
570 },
571 {
572 "minmaxloc.cl",
573#include "./cl_kernels/minmaxloc.clembed"
574 },
575 {
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100576 "minmax_layer.cl",
577#include "./cl_kernels/minmax_layer.clembed"
578 },
579 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100580 "non_linear_filter3x3.cl",
581#include "./cl_kernels/non_linear_filter3x3.clembed"
582 },
583 {
584 "non_linear_filter5x5.cl",
585#include "./cl_kernels/non_linear_filter5x5.clembed"
586 },
587 {
588 "non_linear_filter_helpers.h",
589#include "./cl_kernels/non_linear_filter_helpers.hembed"
590 },
591 {
592 "nonmax.cl",
593#include "./cl_kernels/nonmax.clembed"
594 },
595 {
596 "normalization_layer.cl",
597#include "./cl_kernels/normalization_layer.clembed"
598 },
599 {
600 "batchnormalization_layer.cl",
601#include "./cl_kernels/batchnormalization_layer.clembed"
602 },
603 {
604 "optical_flow_pyramid_lk.cl",
605#include "./cl_kernels/optical_flow_pyramid_lk.clembed"
606 },
607 {
Michalis Spyrou5237e012018-01-17 09:40:27 +0000608 "permute.cl",
609#include "./cl_kernels/permute.clembed"
610 },
611 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100612 "pixelwise_mul_float.cl",
613#include "./cl_kernels/pixelwise_mul_float.clembed"
614 },
615 {
616 "pixelwise_mul_int.cl",
617#include "./cl_kernels/pixelwise_mul_int.clembed"
618 },
619 {
620 "pooling_layer.cl",
621#include "./cl_kernels/pooling_layer.clembed"
622 },
623 {
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000624 "pooling_layer_quantized.cl",
625#include "./cl_kernels/pooling_layer_quantized.clembed"
626 },
627 {
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100628 "quantization_layer.cl",
629#include "./cl_kernels/quantization_layer.clembed"
630 },
631 {
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100632 "reduction_operation.cl",
633#include "./cl_kernels/reduction_operation.clembed"
634 },
635 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100636 "remap.cl",
637#include "./cl_kernels/remap.clembed"
638 },
639 {
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +0100640 "reshape_layer.cl",
641#include "./cl_kernels/reshape_layer.clembed"
642 },
643 {
SiCong Li3e363692017-07-04 15:02:10 +0100644 "roi_pooling_layer.cl",
645#include "./cl_kernels/roi_pooling_layer.clembed"
646 },
647 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100648 "scale.cl",
649#include "./cl_kernels/scale.clembed"
650 },
651 {
652 "scharr_filter.cl",
653#include "./cl_kernels/scharr_filter.clembed"
654 },
655 {
656 "sobel_filter.cl",
657#include "./cl_kernels/sobel_filter.clembed"
658 },
659 {
660 "softmax_layer.cl",
661#include "./cl_kernels/softmax_layer.clembed"
662 },
663 {
Chunosovf450caa2017-11-08 16:09:35 +0700664 "softmax_layer_quantized.cl",
665#include "./cl_kernels/softmax_layer_quantized.clembed"
666 },
667 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100668 "tablelookup.cl",
669#include "./cl_kernels/tablelookup.clembed"
670 },
671 {
672 "threshold.cl",
673#include "./cl_kernels/threshold.clembed"
674 },
675 {
676 "transpose.cl",
677#include "./cl_kernels/transpose.clembed"
678 },
679 {
680 "types.h",
681#include "./cl_kernels/types.hembed"
682 },
683 {
684 "warp_affine.cl",
685#include "./cl_kernels/warp_affine.clembed"
686 },
687 {
688 "warp_helpers.h",
689#include "./cl_kernels/warp_helpers.hembed"
690 },
691 {
692 "warp_perspective.cl",
693#include "./cl_kernels/warp_perspective.clembed"
Michalis Spyroud7e82812017-06-20 15:00:14 +0100694 },
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000695 {
696 "winograd.cl",
697#include "./cl_kernels/winograd.clembed"
698 },
Anthony Barbierac69aa12017-07-03 17:39:37 +0100699#endif /* EMBEDDED_KERNELS */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100700};
701
702CLKernelLibrary::CLKernelLibrary()
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100703 : _context(), _device(), _kernel_path("."), _programs_map(), _built_programs_map()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100704{
Anthony Barbierecb1c622018-04-17 11:45:10 +0100705 opencl_is_available(); // Make sure the OpenCL symbols are initialised *before* the CLKernelLibrary is built
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100706}
707
708CLKernelLibrary &CLKernelLibrary::get()
709{
710 static CLKernelLibrary _kernel_library;
711 return _kernel_library;
712}
713
714Kernel CLKernelLibrary::create_kernel(const std::string &kernel_name, const StringSet &build_options_set) const
715{
716 // Find which program contains the kernel
717 auto kernel_program_it = _kernel_program_map.find(kernel_name);
718
719 if(_kernel_program_map.end() == kernel_program_it)
720 {
721 ARM_COMPUTE_ERROR("Kernel %s not found in the CLKernelLibrary", kernel_name.c_str());
722 }
steniu0134702472017-07-11 09:22:58 +0100723 std::string concat_str;
724
Matthew Bentham6f31f8c2017-10-27 11:50:06 +0100725 if(fp16_support(_device))
726 {
727 concat_str += " -DARM_COMPUTE_OPENCL_FP16_ENABLED=1 ";
728 }
729
Pablo Telloe86a09f2018-01-11 15:44:48 +0000730 if(get_cl_version(_device) == CLVersion::CL20)
steniu0134702472017-07-11 09:22:58 +0100731 {
732 concat_str += " -cl-std=CL2.0 ";
733 }
Pablo Telloe86a09f2018-01-11 15:44:48 +0000734 else if(non_uniform_workgroup_support(_device))
735 {
736 concat_str += " -cl-arm-non-uniform-work-group-size ";
737 }
steniu0134702472017-07-11 09:22:58 +0100738 else
739 {
740 ARM_COMPUTE_ERROR("Non uniform workgroup size is not supported!!");
741 }
742
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100743 // Check if the program has been built before with same build options.
steniu0134702472017-07-11 09:22:58 +0100744 const std::string program_name = kernel_program_it->second;
745 const std::string build_options = stringify_set(build_options_set) + concat_str;
746
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100747 const std::string built_program_name = program_name + "_" + build_options;
748 auto built_program_it = _built_programs_map.find(built_program_name);
749
750 cl::Program cl_program;
751
752 if(_built_programs_map.end() != built_program_it)
753 {
754 // If program has been built, retrieve to create kernel from it
755 cl_program = built_program_it->second;
756 }
757 else
758 {
759 // Get program
760 Program program = load_program(program_name);
761
762 // Build program
763 cl_program = program.build(build_options);
764
765 // Add built program to internal map
766 _built_programs_map.emplace(built_program_name, cl_program);
767 }
768
769 // Create and return kernel
770 return Kernel(kernel_name, cl_program);
771}
772
773const Program &CLKernelLibrary::load_program(const std::string &program_name) const
774{
775 const auto program_it = _programs_map.find(program_name);
776
777 if(program_it != _programs_map.end())
778 {
779 return program_it->second;
780 }
781
782 Program program;
783
784#ifdef EMBEDDED_KERNELS
785 const auto program_source_it = _program_source_map.find(program_name);
786
787 if(_program_source_map.end() == program_source_it)
788 {
789 ARM_COMPUTE_ERROR("Embedded program for %s does not exist.", program_name.c_str());
790 }
791
792 program = Program(_context, program_name, program_source_it->second);
Anthony Barbierac69aa12017-07-03 17:39:37 +0100793#else /* EMBEDDED_KERNELS */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100794 // Check for binary
795 std::string source_name = _kernel_path + program_name;
796 std::string binary_name = source_name + "bin";
797
798 if(std::ifstream(binary_name).is_open())
799 {
800 const std::string program_binary = read_file(binary_name, true);
801 program = Program(_context, _device, program_name, std::vector<unsigned char>(program_binary.begin(), program_binary.end()));
802 }
803 else if(std::ifstream(source_name).is_open())
804 {
805 program = Program(_context, program_name, read_file(source_name, false));
806 }
807 else
808 {
809 ARM_COMPUTE_ERROR("Kernel file %s does not exist.", source_name.c_str());
810 }
Anthony Barbierac69aa12017-07-03 17:39:37 +0100811#endif /* EMBEDDED_KERNELS */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100812
813 // Insert program to program map
814 const auto new_program = _programs_map.emplace(program_name, std::move(program));
815
816 return new_program.first->second;
817}
818
819std::string CLKernelLibrary::stringify_set(const StringSet &s) const
820{
steniu0134702472017-07-11 09:22:58 +0100821 std::string concat_set;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100822
823#ifndef EMBEDDED_KERNELS
824 concat_set += "-I" + _kernel_path + " ";
825#endif /* EMBEDDED_KERNELS */
826
827 // Concatenate set
828 for(const auto &el : s)
829 {
830 concat_set += " " + el;
831 }
832
833 return concat_set;
834}
Michalis Spyroud7e82812017-06-20 15:00:14 +0100835
836std::string CLKernelLibrary::get_program_source(const std::string &program_name)
837{
838 const auto program_source_it = _program_source_map.find(program_name);
839
840 if(program_source_it == _program_source_map.end())
841 {
842 ARM_COMPUTE_ERROR("Embedded program for %s does not exist.", program_name.c_str());
843 }
844
845 return program_source_it->second;
846}
steniu015f910722017-08-23 10:15:22 +0100847
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100848size_t CLKernelLibrary::max_local_workgroup_size(const cl::Kernel &kernel) const
steniu015f910722017-08-23 10:15:22 +0100849{
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100850 size_t result;
steniu015f910722017-08-23 10:15:22 +0100851
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100852 size_t err = kernel.getWorkGroupInfo(_device, CL_KERNEL_WORK_GROUP_SIZE, &result);
853 ARM_COMPUTE_ERROR_ON_MSG(err != 0, "clGetKernelWorkGroupInfo failed to return the maximum workgroup size for the kernel");
854 ARM_COMPUTE_UNUSED(err);
855
856 return result;
steniu015f910722017-08-23 10:15:22 +0100857}
858
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100859cl::NDRange CLKernelLibrary::default_ndrange() const
steniu015f910722017-08-23 10:15:22 +0100860{
Michalis Spyroua9676112018-02-22 18:07:43 +0000861 cl::Device device = cl::Device::getDefault();
862 GPUTarget _target = get_target_from_device(device);
863 cl::NDRange default_range;
864
865 switch(_target)
866 {
867 case GPUTarget::MIDGARD:
868 case GPUTarget::T600:
869 case GPUTarget::T700:
870 case GPUTarget::T800:
871 default_range = cl::NDRange(128u, 1);
872 break;
873 default:
874 default_range = cl::NullRange;
875 }
876
877 return default_range;
steniu015f910722017-08-23 10:15:22 +0100878}
Anthony Barbier847864d2018-03-07 11:35:53 +0000879
880std::string CLKernelLibrary::get_device_version()
881{
882 return _device.getInfo<CL_DEVICE_VERSION>();
883}