blob: e9ee23f4d7efea473c3bafbf08f71474c0fcaf16 [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" },
187 { "copy_plane", "channel_extract.cl" },
188 { "copy_planes_3p", "channel_combine.cl" },
189 { "copy_to_keypoint", "fast_corners.cl" },
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000190 { "deconvolution_upsample", "deconvolution_layer.cl" },
Giorgio Arena93a690e2017-08-01 16:09:33 +0100191 { "depthwise_convolution_3x3", "depthwise_convolution.cl" },
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700192 { "depthwise_convolution_3x3_quantized", "depthwise_convolution_quantized.cl" },
Gian Marcoc799ed82018-02-01 16:57:48 +0000193 { "depthwise_convolution_3x3_stridex1_stridey1_bifrost", "depthwise_convolution.cl" },
194 { "depthwise_convolution_3x3_stridex2_stridey2_bifrost", "depthwise_convolution.cl" },
Giorgio Arena9fe41442017-08-23 16:36:24 +0100195 { "depthwise_im2col", "depthwise_convolution.cl" },
196 { "depthwise_vector_to_tensor", "depthwise_convolution.cl" },
197 { "depthwise_weights_reshape", "depthwise_convolution.cl" },
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100198 { "dequantization_layer", "dequantization_layer.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100199 { "derivative", "derivative.cl" },
200 { "dilate", "dilate.cl" },
SiCong Lic51b72f2017-07-28 14:46:20 +0100201 { "direct_convolution1x1", "direct_convolution1x1.cl" },
Gian Marco Iodice1c8409d2017-09-06 17:24:25 +0100202 { "direct_convolution1x1_f32_bifrost", "direct_convolution1x1.cl" },
SiCong Lic51b72f2017-07-28 14:46:20 +0100203 { "direct_convolution3x3", "direct_convolution3x3.cl" },
Gian Marco Iodice1246b632017-08-16 18:38:32 +0100204 { "direct_convolution3x3_f32_bifrost", "direct_convolution3x3.cl" },
steniu01db006682017-08-09 16:26:22 +0100205 { "direct_convolution5x5", "direct_convolution5x5.cl" },
Gian Marco Iodice1246b632017-08-16 18:38:32 +0100206 { "direct_convolution5x5_f32_bifrost", "direct_convolution5x5.cl" },
Chunosovd621bca2017-11-03 17:33:15 +0700207 { "direct_convolution_1x1_3x3_5x5_quantized", "direct_convolution_1x1_3x3_5x5_quantized.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100208 { "erode", "erode.cl" },
209 { "fast_corners", "fast_corners.cl" },
210 { "fill_image_borders_constant", "fill_border.cl" },
211 { "fill_image_borders_replicate", "fill_border.cl" },
212 { "finalize", "optical_flow_pyramid_lk.cl" },
Georgios Pinitasd8e765b2017-08-02 13:44:33 +0100213 { "floor_layer", "floor.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100214 { "gaussian1x5_sub_x", "gaussian_pyramid.cl" },
215 { "gaussian5x1_sub_y", "gaussian_pyramid.cl" },
Gian Marco Iodice578ab612017-06-23 09:34:33 +0100216 { "gemm_accumulate_biases", "gemm.cl" },
Gian Marco36a0a462018-01-12 10:21:40 +0000217 { "gemm_interleave4x4", "gemm.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100218 { "gemm_ma_f16", "gemm.cl" },
219 { "gemm_ma_f32", "gemm.cl" },
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +0100220 { "gemm_ma_qs8", "gemm.cl" },
Gian Marco Iodice8a383692017-07-03 17:41:47 +0100221 { "gemm_ma_qs16", "gemm.cl" },
Giorgio Arena9fe41442017-08-23 16:36:24 +0100222 { "gemm_mv", "gemv.cl" },
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000223 { "gemm_mv_quantized", "gemv.cl" },
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100224 { "gemm_mm_interleaved_transposed_f16", "gemm.cl" },
225 { "gemm_mm_interleaved_transposed_f32_midgard", "gemm.cl" },
226 { "gemm_mm_interleaved_transposed_f32_bifrost", "gemm.cl" },
227 { "gemm_mm_interleaved_transposed_qs8", "gemm.cl" },
228 { "gemm_mm_interleaved_transposed_qs16", "gemm.cl" },
229 { "gemm_mm_floating_point", "gemm.cl" },
Anton Lokhmotov3e80c7f2017-11-20 11:02:10 +0000230 { "gemm_mm_floating_point_f32_bifrost", "gemm.cl" },
231 { "gemm_mm_floating_point_f32_bifrost_1000", "gemm.cl" },
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +0100232 { "gemm_mm_qs8", "gemm.cl" },
Gian Marco Iodice8a383692017-07-03 17:41:47 +0100233 { "gemm_mm_qs16", "gemm.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100234 { "gemm_lc_vm_f32", "gemm.cl" },
Gian Marco36a0a462018-01-12 10:21:40 +0000235 { "gemm_transpose1xW", "gemm.cl" },
Gian Marco05288a22017-11-21 10:57:50 +0000236 { "gemmlowp_matrix_a_reduction", "gemmlowp.cl" },
237 { "gemmlowp_matrix_b_reduction", "gemmlowp.cl" },
Gian Marco7b4d5472018-01-10 15:56:30 +0000238 { "gemmlowp_mm_bifrost", "gemmlowp.cl" },
239 { "gemmlowp_mm_midgard", "gemmlowp.cl" },
Gian Marco05288a22017-11-21 10:57:50 +0000240 { "gemmlowp_mm_interleaved_transposed", "gemmlowp.cl" },
241 { "gemmlowp_offset_contribution", "gemmlowp.cl" },
242 { "gemmlowp_output_stage_quantize_down", "gemmlowp.cl" },
Gian Marco58c57942017-11-28 09:10:03 +0000243 { "gemmlowp_output_stage_quantize_down_fixedpoint", "gemmlowp.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100244 { "harris_score_3x3", "harris_corners.cl" },
245 { "harris_score_5x5", "harris_corners.cl" },
246 { "harris_score_7x7", "harris_corners.cl" },
247 { "hist_border_kernel", "histogram.cl" },
248 { "hist_border_kernel_fixed", "histogram.cl" },
249 { "hist_local_kernel", "histogram.cl" },
250 { "hist_local_kernel_fixed", "histogram.cl" },
251 { "hog_block_normalization", "hog.cl" },
252 { "hog_detector", "hog.cl" },
253 { "hog_orientation_binning", "hog.cl" },
254 { "hysteresis", "canny.cl" },
Gian Marco76faef82018-01-29 12:15:32 +0000255 { "im2col1x1_stridex1_dchw", "im2col.cl" },
256 { "im2col3x3_dchw", "im2col.cl" },
257 { "im2col5x5_dchw", "im2col.cl" },
258 { "im2col11x11_padx0_pady0_dchw", "im2col.cl" },
259 { "im2col_generic_dchw", "im2col.cl" },
260 { "im2col_generic_padx0_pady0_dchw", "im2col.cl" },
261 { "im2col_reduced_dchw", "im2col.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100262 { "init_level", "optical_flow_pyramid_lk.cl" },
263 { "init_level_max", "optical_flow_pyramid_lk.cl" },
264 { "init_level_max_initial_estimate", "optical_flow_pyramid_lk.cl" },
265 { "integral_horizontal", "integral_image.cl" },
266 { "integral_vertical", "integral_image.cl" },
267 { "IYUV_to_NV12_bt709", "color_convert.cl" },
268 { "IYUV_to_RGB888_bt709", "color_convert.cl" },
269 { "IYUV_to_RGBA8888_bt709", "color_convert.cl" },
270 { "IYUV_to_YUV444_bt709", "color_convert.cl" },
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100271 { "l2_normalize", "l2_normalize.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100272 { "lktracker_stage0", "optical_flow_pyramid_lk.cl" },
273 { "lktracker_stage1", "optical_flow_pyramid_lk.cl" },
274 { "magnitude_phase", "magnitude_phase.cl" },
275 { "mean_stddev_accumulate", "mean_stddev.cl" },
276 { "minmax", "minmaxloc.cl" },
277 { "minmax_border", "minmaxloc.cl" },
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100278 { "minmax_layer", "minmax_layer.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100279 { "minmaxloc", "minmaxloc.cl" },
280 { "non_linear_filter_box3x3", "non_linear_filter3x3.cl" },
281 { "non_linear_filter_cross3x3", "non_linear_filter3x3.cl" },
282 { "non_linear_filter_disk3x3", "non_linear_filter3x3.cl" },
283 { "non_linear_filter_box5x5", "non_linear_filter5x5.cl" },
284 { "non_linear_filter_cross5x5", "non_linear_filter5x5.cl" },
285 { "non_linear_filter_disk5x5", "non_linear_filter5x5.cl" },
286 { "non_max_suppression", "nonmax.cl" },
287 { "normalization_layer_cross_map", "normalization_layer.cl" },
Georgios Pinitas01624362017-11-30 10:53:31 +0000288 { "normalization_layer_in_map", "normalization_layer.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100289 { "NV12_to_IYUV_bt709", "color_convert.cl" },
290 { "NV12_to_RGB888_bt709", "color_convert.cl" },
291 { "NV12_to_RGBA8888_bt709", "color_convert.cl" },
292 { "NV12_to_YUV444_bt709", "color_convert.cl" },
293 { "NV21_to_IYUV_bt709", "color_convert.cl" },
294 { "NV21_to_RGB888_bt709", "color_convert.cl" },
295 { "NV21_to_RGBA8888_bt709", "color_convert.cl" },
296 { "NV21_to_YUV444_bt709", "color_convert.cl" },
Giorgio Arenaa086a0a2018-02-16 12:42:16 +0000297 { "output_stage_quantized", "direct_convolution_1x1_3x3_5x5_quantized.cl" },
Michalis Spyrou5237e012018-01-17 09:40:27 +0000298 { "permute_201", "permute.cl" },
299 { "permute_120", "permute.cl" },
300 { "permute_3201", "permute.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100301 { "pixelwise_mul_float", "pixelwise_mul_float.cl" },
302 { "pixelwise_mul_int", "pixelwise_mul_int.cl" },
303 { "pooling_layer_2", "pooling_layer.cl" },
304 { "pooling_layer_3", "pooling_layer.cl" },
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000305 { "pooling_layer_optimized_3", "pooling_layer.cl" },
Georgios Pinitasce093142017-06-19 16:11:53 +0100306 { "pooling_layer_7", "pooling_layer.cl" },
Isabella Gottardia527e8c2018-01-31 17:49:25 +0000307 { "pooling_layer_MxN", "pooling_layer.cl" },
308 { "pooling_layer_MxN_quantized", "pooling_layer_quantized.cl" },
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100309 { "quantization_layer", "quantization_layer.cl" },
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100310 { "reduction_operation", "reduction_operation.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100311 { "remap_nearest_neighbour", "remap.cl" },
312 { "remap_bilinear", "remap.cl" },
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +0100313 { "reshape_layer", "reshape_layer.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100314 { "reshape_to_columns", "convolution_layer.cl" },
315 { "RGB888_to_IYUV_bt709", "color_convert.cl" },
316 { "RGB888_to_NV12_bt709", "color_convert.cl" },
317 { "RGB888_to_RGBA8888_bt709", "color_convert.cl" },
318 { "RGB888_to_YUV444_bt709", "color_convert.cl" },
319 { "RGBA8888_to_IYUV_bt709", "color_convert.cl" },
320 { "RGBA8888_to_NV12_bt709", "color_convert.cl" },
321 { "RGBA8888_to_RGB888_bt709", "color_convert.cl" },
322 { "RGBA8888_to_YUV444_bt709", "color_convert.cl" },
SiCong Li3e363692017-07-04 15:02:10 +0100323 { "roi_pooling_layer", "roi_pooling_layer.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100324 { "scale_nearest_neighbour", "scale.cl" },
325 { "scale_bilinear", "scale.cl" },
326 { "scharr3x3", "scharr_filter.cl" },
327 { "sobel3x3", "sobel_filter.cl" },
328 { "sobel_separable5x1", "sobel_filter.cl" },
329 { "sobel_separable1x5", "sobel_filter.cl" },
330 { "sobel_separable7x1", "sobel_filter.cl" },
331 { "sobel_separable1x7", "sobel_filter.cl" },
332 { "softmax_layer_max", "softmax_layer.cl" },
Chunosovf450caa2017-11-08 16:09:35 +0700333 { "softmax_layer_max_quantized", "softmax_layer_quantized.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100334 { "softmax_layer_shift_exp_sum", "softmax_layer.cl" },
Chunosovf450caa2017-11-08 16:09:35 +0700335 { "softmax_layer_shift_exp_sum_quantized", "softmax_layer_quantized.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100336 { "softmax_layer_norm", "softmax_layer.cl" },
Chunosovf450caa2017-11-08 16:09:35 +0700337 { "softmax_layer_norm_quantized", "softmax_layer_quantized.cl" },
Chunosovd6afedc2017-11-06 22:09:45 +0700338 { "softmax_layer_max_shift_exp_sum_serial", "softmax_layer.cl" },
339 { "softmax_layer_max_shift_exp_sum_parallel", "softmax_layer.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100340 { "suppress_non_maximum", "canny.cl" },
341 { "tablelookup_U8", "tablelookup.cl" },
342 { "tablelookup_S16", "tablelookup.cl" },
343 { "threshold_binary", "threshold.cl" },
344 { "threshold_range", "threshold.cl" },
345 { "transpose", "transpose.cl" },
346 { "UYVY422_to_IYUV_bt709", "color_convert.cl" },
347 { "UYVY422_to_NV12_bt709", "color_convert.cl" },
348 { "UYVY422_to_RGB888_bt709", "color_convert.cl" },
349 { "UYVY422_to_RGBA8888_bt709", "color_convert.cl" },
350 { "warp_affine_nearest_neighbour", "warp_affine.cl" },
351 { "warp_affine_bilinear", "warp_affine.cl" },
352 { "warp_perspective_nearest_neighbour", "warp_perspective.cl" },
353 { "warp_perspective_bilinear", "warp_perspective.cl" },
354 { "YUYV422_to_IYUV_bt709", "color_convert.cl" },
355 { "YUYV422_to_NV12_bt709", "color_convert.cl" },
356 { "YUYV422_to_RGB888_bt709", "color_convert.cl" },
357 { "YUYV422_to_RGBA8888_bt709", "color_convert.cl" },
358};
359
360const std::map<std::string, std::string> CLKernelLibrary::_program_source_map =
361{
362#ifdef EMBEDDED_KERNELS
363 {
364 "absdiff.cl",
365#include "./cl_kernels/absdiff.clembed"
366 },
367 {
368 "accumulate.cl",
369#include "./cl_kernels/accumulate.clembed"
370 },
371 {
372 "activation_layer.cl",
373#include "./cl_kernels/activation_layer.clembed"
374 },
375 {
Michel Iwaniec00633802017-10-12 14:14:15 +0100376 "activation_layer_qa8.cl",
377#include "./cl_kernels/activation_layer_qa8.clembed"
378 },
379 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100380 "arithmetic_op.cl",
381#include "./cl_kernels/arithmetic_op.clembed"
382 },
383 {
384 "bitwise_op.cl",
385#include "./cl_kernels/bitwise_op.clembed"
386 },
387 {
388 "canny.cl",
389#include "./cl_kernels/canny.clembed"
390 },
391 {
392 "channel_combine.cl",
393#include "./cl_kernels/channel_combine.clembed"
394 },
395 {
396 "channel_extract.cl",
397#include "./cl_kernels/channel_extract.clembed"
398 },
399 {
Gian Marco76faef82018-01-29 12:15:32 +0000400 "col2im.cl",
401#include "./cl_kernels/col2im.clembed"
402 },
403 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100404 "concatenate.cl",
405#include "./cl_kernels/concatenate.clembed"
406 },
407 {
408 "color_convert.cl",
409#include "./cl_kernels/color_convert.clembed"
410 },
411 {
412 "convolution3x3.cl",
413#include "./cl_kernels/convolution3x3.clembed"
414 },
415 {
416 "convolution5x5.cl",
417#include "./cl_kernels/convolution5x5.clembed"
418 },
419 {
420 "convolution7x7.cl",
421#include "./cl_kernels/convolution7x7.clembed"
422 },
423 {
424 "convolution9x9.cl",
425#include "./cl_kernels/convolution9x9.clembed"
426 },
427 {
428 "convolution_layer.cl",
429#include "./cl_kernels/convolution_layer.clembed"
430 },
431 {
432 "convolution_rectangle.cl",
433#include "./cl_kernels/convolution_rectangle.clembed"
434 },
435 {
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000436 "deconvolution_layer.cl",
437#include "./cl_kernels/deconvolution_layer.clembed"
438 },
439 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100440 "depth_convert.cl",
441#include "./cl_kernels/depth_convert.clembed"
442 },
443 {
Giorgio Arena93a690e2017-08-01 16:09:33 +0100444 "depthwise_convolution.cl",
445#include "./cl_kernels/depthwise_convolution.clembed"
446 },
447 {
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700448 "depthwise_convolution_quantized.cl",
449#include "./cl_kernels/depthwise_convolution_quantized.clembed"
450 },
451 {
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100452 "dequantization_layer.cl",
453#include "./cl_kernels/dequantization_layer.clembed"
454 },
455 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100456 "derivative.cl",
457#include "./cl_kernels/derivative.clembed"
458 },
459 {
460 "dilate.cl",
461#include "./cl_kernels/dilate.clembed"
462 },
463 {
SiCong Lic51b72f2017-07-28 14:46:20 +0100464 "direct_convolution1x1.cl",
465#include "./cl_kernels/direct_convolution1x1.clembed"
466 },
467 {
468 "direct_convolution3x3.cl",
469#include "./cl_kernels/direct_convolution3x3.clembed"
steniu0127b386c2017-07-18 17:37:43 +0100470 },
471 {
steniu01db006682017-08-09 16:26:22 +0100472 "direct_convolution5x5.cl",
473#include "./cl_kernels/direct_convolution5x5.clembed"
474 },
475 {
Chunosovd621bca2017-11-03 17:33:15 +0700476 "direct_convolution_1x1_3x3_5x5_quantized.cl",
477#include "./cl_kernels/direct_convolution_1x1_3x3_5x5_quantized.clembed"
478 },
479 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100480 "erode.cl",
481#include "./cl_kernels/erode.clembed"
482 },
483 {
484 "fast_corners.cl",
485#include "./cl_kernels/fast_corners.clembed"
486 },
487 {
488 "fill_border.cl",
489#include "./cl_kernels/fill_border.clembed"
490 },
491 {
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100492 "fixed_point.h",
493#include "./cl_kernels/fixed_point.hembed"
494 },
495 {
Georgios Pinitasd8e765b2017-08-02 13:44:33 +0100496 "floor.cl",
497#include "./cl_kernels/floor.clembed"
498 },
499 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100500 "gaussian_pyramid.cl",
501#include "./cl_kernels/gaussian_pyramid.clembed"
502 },
503 {
504 "gemm.cl",
505#include "./cl_kernels/gemm.clembed"
506 },
507 {
Gian Marco05288a22017-11-21 10:57:50 +0000508 "gemmlowp.cl",
509#include "./cl_kernels/gemmlowp.clembed"
510 },
511 {
Giorgio Arena9fe41442017-08-23 16:36:24 +0100512 "gemv.cl",
513#include "./cl_kernels/gemv.clembed"
514 },
515 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100516 "harris_corners.cl",
517#include "./cl_kernels/harris_corners.clembed"
518 },
519 {
520 "helpers.h",
521#include "./cl_kernels/helpers.hembed"
522 },
523 {
Chunosovd621bca2017-11-03 17:33:15 +0700524 "helpers_asymm.h",
525#include "./cl_kernels/helpers_asymm.hembed"
526 },
527 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100528 "histogram.cl",
529#include "./cl_kernels/histogram.clembed"
530 },
531 {
532 "hog.cl",
533#include "./cl_kernels/hog.clembed"
534 },
535 {
Gian Marco76faef82018-01-29 12:15:32 +0000536 "im2col.cl",
537#include "./cl_kernels/im2col.clembed"
538 },
539 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100540 "integral_image.cl",
541#include "./cl_kernels/integral_image.clembed"
542 },
543 {
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100544 "l2_normalize.cl",
545#include "./cl_kernels/l2_normalize.clembed"
546 },
547 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100548 "magnitude_phase.cl",
549#include "./cl_kernels/magnitude_phase.clembed"
550 },
551 {
552 "mean_stddev.cl",
553#include "./cl_kernels/mean_stddev.clembed"
554 },
555 {
556 "minmaxloc.cl",
557#include "./cl_kernels/minmaxloc.clembed"
558 },
559 {
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100560 "minmax_layer.cl",
561#include "./cl_kernels/minmax_layer.clembed"
562 },
563 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100564 "non_linear_filter3x3.cl",
565#include "./cl_kernels/non_linear_filter3x3.clembed"
566 },
567 {
568 "non_linear_filter5x5.cl",
569#include "./cl_kernels/non_linear_filter5x5.clembed"
570 },
571 {
572 "non_linear_filter_helpers.h",
573#include "./cl_kernels/non_linear_filter_helpers.hembed"
574 },
575 {
576 "nonmax.cl",
577#include "./cl_kernels/nonmax.clembed"
578 },
579 {
580 "normalization_layer.cl",
581#include "./cl_kernels/normalization_layer.clembed"
582 },
583 {
584 "batchnormalization_layer.cl",
585#include "./cl_kernels/batchnormalization_layer.clembed"
586 },
587 {
588 "optical_flow_pyramid_lk.cl",
589#include "./cl_kernels/optical_flow_pyramid_lk.clembed"
590 },
591 {
Michalis Spyrou5237e012018-01-17 09:40:27 +0000592 "permute.cl",
593#include "./cl_kernels/permute.clembed"
594 },
595 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100596 "pixelwise_mul_float.cl",
597#include "./cl_kernels/pixelwise_mul_float.clembed"
598 },
599 {
600 "pixelwise_mul_int.cl",
601#include "./cl_kernels/pixelwise_mul_int.clembed"
602 },
603 {
604 "pooling_layer.cl",
605#include "./cl_kernels/pooling_layer.clembed"
606 },
607 {
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000608 "pooling_layer_quantized.cl",
609#include "./cl_kernels/pooling_layer_quantized.clembed"
610 },
611 {
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100612 "quantization_layer.cl",
613#include "./cl_kernels/quantization_layer.clembed"
614 },
615 {
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100616 "reduction_operation.cl",
617#include "./cl_kernels/reduction_operation.clembed"
618 },
619 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100620 "remap.cl",
621#include "./cl_kernels/remap.clembed"
622 },
623 {
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +0100624 "reshape_layer.cl",
625#include "./cl_kernels/reshape_layer.clembed"
626 },
627 {
SiCong Li3e363692017-07-04 15:02:10 +0100628 "roi_pooling_layer.cl",
629#include "./cl_kernels/roi_pooling_layer.clembed"
630 },
631 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100632 "scale.cl",
633#include "./cl_kernels/scale.clembed"
634 },
635 {
636 "scharr_filter.cl",
637#include "./cl_kernels/scharr_filter.clembed"
638 },
639 {
640 "sobel_filter.cl",
641#include "./cl_kernels/sobel_filter.clembed"
642 },
643 {
644 "softmax_layer.cl",
645#include "./cl_kernels/softmax_layer.clembed"
646 },
647 {
Chunosovf450caa2017-11-08 16:09:35 +0700648 "softmax_layer_quantized.cl",
649#include "./cl_kernels/softmax_layer_quantized.clembed"
650 },
651 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100652 "tablelookup.cl",
653#include "./cl_kernels/tablelookup.clembed"
654 },
655 {
656 "threshold.cl",
657#include "./cl_kernels/threshold.clembed"
658 },
659 {
660 "transpose.cl",
661#include "./cl_kernels/transpose.clembed"
662 },
663 {
664 "types.h",
665#include "./cl_kernels/types.hembed"
666 },
667 {
668 "warp_affine.cl",
669#include "./cl_kernels/warp_affine.clembed"
670 },
671 {
672 "warp_helpers.h",
673#include "./cl_kernels/warp_helpers.hembed"
674 },
675 {
676 "warp_perspective.cl",
677#include "./cl_kernels/warp_perspective.clembed"
Michalis Spyroud7e82812017-06-20 15:00:14 +0100678 },
Anthony Barbierac69aa12017-07-03 17:39:37 +0100679#endif /* EMBEDDED_KERNELS */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100680};
681
682CLKernelLibrary::CLKernelLibrary()
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100683 : _context(), _device(), _kernel_path("."), _programs_map(), _built_programs_map()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100684{
685}
686
687CLKernelLibrary &CLKernelLibrary::get()
688{
689 static CLKernelLibrary _kernel_library;
690 return _kernel_library;
691}
692
693Kernel CLKernelLibrary::create_kernel(const std::string &kernel_name, const StringSet &build_options_set) const
694{
695 // Find which program contains the kernel
696 auto kernel_program_it = _kernel_program_map.find(kernel_name);
697
698 if(_kernel_program_map.end() == kernel_program_it)
699 {
700 ARM_COMPUTE_ERROR("Kernel %s not found in the CLKernelLibrary", kernel_name.c_str());
701 }
702
steniu0134702472017-07-11 09:22:58 +0100703 std::string concat_str;
704
Matthew Bentham6f31f8c2017-10-27 11:50:06 +0100705 if(fp16_support(_device))
706 {
707 concat_str += " -DARM_COMPUTE_OPENCL_FP16_ENABLED=1 ";
708 }
709
steniu0134702472017-07-11 09:22:58 +0100710 if(non_uniform_workgroup_support(_device))
711 {
712 concat_str += " -cl-arm-non-uniform-work-group-size ";
713 }
714 else if(get_cl_version(_device) == CLVersion::CL20)
715 {
716 concat_str += " -cl-std=CL2.0 ";
717 }
718 else
719 {
720 ARM_COMPUTE_ERROR("Non uniform workgroup size is not supported!!");
721 }
722
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100723 // Check if the program has been built before with same build options.
steniu0134702472017-07-11 09:22:58 +0100724 const std::string program_name = kernel_program_it->second;
725 const std::string build_options = stringify_set(build_options_set) + concat_str;
726
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100727 const std::string built_program_name = program_name + "_" + build_options;
728 auto built_program_it = _built_programs_map.find(built_program_name);
729
730 cl::Program cl_program;
731
732 if(_built_programs_map.end() != built_program_it)
733 {
734 // If program has been built, retrieve to create kernel from it
735 cl_program = built_program_it->second;
736 }
737 else
738 {
739 // Get program
740 Program program = load_program(program_name);
741
742 // Build program
743 cl_program = program.build(build_options);
744
745 // Add built program to internal map
746 _built_programs_map.emplace(built_program_name, cl_program);
747 }
748
749 // Create and return kernel
750 return Kernel(kernel_name, cl_program);
751}
752
753const Program &CLKernelLibrary::load_program(const std::string &program_name) const
754{
755 const auto program_it = _programs_map.find(program_name);
756
757 if(program_it != _programs_map.end())
758 {
759 return program_it->second;
760 }
761
762 Program program;
763
764#ifdef EMBEDDED_KERNELS
765 const auto program_source_it = _program_source_map.find(program_name);
766
767 if(_program_source_map.end() == program_source_it)
768 {
769 ARM_COMPUTE_ERROR("Embedded program for %s does not exist.", program_name.c_str());
770 }
771
772 program = Program(_context, program_name, program_source_it->second);
Anthony Barbierac69aa12017-07-03 17:39:37 +0100773#else /* EMBEDDED_KERNELS */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100774 // Check for binary
775 std::string source_name = _kernel_path + program_name;
776 std::string binary_name = source_name + "bin";
777
778 if(std::ifstream(binary_name).is_open())
779 {
780 const std::string program_binary = read_file(binary_name, true);
781 program = Program(_context, _device, program_name, std::vector<unsigned char>(program_binary.begin(), program_binary.end()));
782 }
783 else if(std::ifstream(source_name).is_open())
784 {
785 program = Program(_context, program_name, read_file(source_name, false));
786 }
787 else
788 {
789 ARM_COMPUTE_ERROR("Kernel file %s does not exist.", source_name.c_str());
790 }
Anthony Barbierac69aa12017-07-03 17:39:37 +0100791#endif /* EMBEDDED_KERNELS */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100792
793 // Insert program to program map
794 const auto new_program = _programs_map.emplace(program_name, std::move(program));
795
796 return new_program.first->second;
797}
798
799std::string CLKernelLibrary::stringify_set(const StringSet &s) const
800{
steniu0134702472017-07-11 09:22:58 +0100801 std::string concat_set;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100802
803#ifndef EMBEDDED_KERNELS
804 concat_set += "-I" + _kernel_path + " ";
805#endif /* EMBEDDED_KERNELS */
806
807 // Concatenate set
808 for(const auto &el : s)
809 {
810 concat_set += " " + el;
811 }
812
813 return concat_set;
814}
Michalis Spyroud7e82812017-06-20 15:00:14 +0100815
816std::string CLKernelLibrary::get_program_source(const std::string &program_name)
817{
818 const auto program_source_it = _program_source_map.find(program_name);
819
820 if(program_source_it == _program_source_map.end())
821 {
822 ARM_COMPUTE_ERROR("Embedded program for %s does not exist.", program_name.c_str());
823 }
824
825 return program_source_it->second;
826}
steniu015f910722017-08-23 10:15:22 +0100827
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100828size_t CLKernelLibrary::max_local_workgroup_size(const cl::Kernel &kernel) const
steniu015f910722017-08-23 10:15:22 +0100829{
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100830 size_t result;
steniu015f910722017-08-23 10:15:22 +0100831
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100832 size_t err = kernel.getWorkGroupInfo(_device, CL_KERNEL_WORK_GROUP_SIZE, &result);
833 ARM_COMPUTE_ERROR_ON_MSG(err != 0, "clGetKernelWorkGroupInfo failed to return the maximum workgroup size for the kernel");
834 ARM_COMPUTE_UNUSED(err);
835
836 return result;
steniu015f910722017-08-23 10:15:22 +0100837}
838
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100839cl::NDRange CLKernelLibrary::default_ndrange() const
steniu015f910722017-08-23 10:15:22 +0100840{
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100841 return cl::NDRange(128u, 1);
steniu015f910722017-08-23 10:15:22 +0100842}