blob: 45a247db1a2e7708e9044374da3af0dc7ebfe506 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2016, 2017 ARM Limited.
3 *
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
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Utils.h"
28
29#include <fstream>
30#include <iostream>
31#include <utility>
32#include <vector>
33
34using namespace arm_compute;
35
36Program::Program()
37 : _context(), _device(), _is_binary(false), _name(), _source(), _binary()
38{
39}
40
41Program::Program(cl::Context context, std::string name, std::string source)
42 : _context(std::move(context)), _device(), _is_binary(false), _name(std::move(name)), _source(std::move(source)), _binary()
43{
44}
45
46Program::Program(cl::Context context, cl::Device device, std::string name, std::vector<unsigned char> binary)
47 : _context(std::move(context)), _device(std::move(device)), _is_binary(true), _name(std::move(name)), _source(), _binary(std::move(binary))
48{
49}
50
51Program::operator cl::Program() const
52{
53 if(_is_binary)
54 {
55 return cl::Program(_context, { _device }, { _binary });
56 }
57 else
58 {
59 return cl::Program(_context, _source, false);
60 }
61}
62
63bool Program::build(const cl::Program &program, const std::string &build_options)
64{
65 try
66 {
67 return program.build(build_options.c_str()) == CL_SUCCESS;
68 }
69 catch(const cl::Error &e)
70 {
71 cl_int err = CL_SUCCESS;
72 const auto build_info = program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(&err);
73
74 for(auto &pair : build_info)
75 {
76 std::cerr << pair.second << std::endl;
77 }
78
79 return false;
80 }
81}
82
83cl::Program Program::build(const std::string &build_options) const
84{
85 cl::Program cl_program = static_cast<cl::Program>(*this);
86 build(cl_program, build_options);
87 return cl_program;
88}
89
90Kernel::Kernel()
91 : _name(), _kernel()
92{
93}
94
95Kernel::Kernel(std::string name, const cl::Program &program)
96 : _name(std::move(name)),
97 _kernel(cl::Kernel(program, _name.c_str()))
98{
99}
100
101const std::map<std::string, std::string> CLKernelLibrary::_kernel_program_map =
102{
103 { "absdiff", "absdiff.cl" },
104 { "accumulate", "accumulate.cl" },
105 { "accumulate_squared", "accumulate.cl" },
106 { "accumulate_weighted", "accumulate.cl" },
107 { "activation_layer", "activation_layer.cl" },
108 { "arithmetic_add", "arithmetic_op.cl" },
109 { "arithmetic_sub", "arithmetic_op.cl" },
110 { "bitwise_or", "bitwise_op.cl" },
111 { "bitwise_and", "bitwise_op.cl" },
112 { "bitwise_xor", "bitwise_op.cl" },
113 { "bitwise_not", "bitwise_op.cl" },
114 { "channel_combine_NV", "channel_combine.cl" },
115 { "channel_combine_RGB888", "channel_combine.cl" },
116 { "channel_combine_RGBA8888", "channel_combine.cl" },
117 { "channel_combine_UYVY422", "channel_combine.cl" },
118 { "channel_combine_YUYV422", "channel_combine.cl" },
119 { "channel_extract_NV12", "channel_extract.cl" },
120 { "channel_extract_NV21", "channel_extract.cl" },
121 { "channel_extract_RGB888", "channel_extract.cl" },
122 { "channel_extract_RGBA8888", "channel_extract.cl" },
123 { "channel_extract_UYVY422", "channel_extract.cl" },
124 { "channel_extract_YUYV422", "channel_extract.cl" },
125 { "combine_gradients_L1", "canny.cl" },
126 { "combine_gradients_L2", "canny.cl" },
127 { "concatenate_depth", "concatenate.cl" },
128 { "convolution_rectangle", "convolution_rectangle.cl" },
129 { "col2im", "convolution_layer.cl" },
130 { "convolution3x3_static", "convolution3x3.cl" },
131 { "convolution5x5_static", "convolution5x5.cl" },
132 { "convolution7x7_static", "convolution7x7.cl" },
133 { "convolution9x9_static", "convolution9x9.cl" },
134 { "convolution_separable1x5_static", "convolution5x5.cl" },
135 { "convolution_separable5x1_static", "convolution5x5.cl" },
136 { "convolution_separable1x7_static", "convolution7x7.cl" },
137 { "convolution_separable7x1_static", "convolution7x7.cl" },
138 { "convolution_separable1x9_static", "convolution9x9.cl" },
139 { "convolution_separable9x1_static", "convolution9x9.cl" },
140 { "convert_depth_down", "depth_convert.cl" },
141 { "convert_depth_up", "depth_convert.cl" },
142 { "copy_plane", "channel_extract.cl" },
143 { "copy_planes_3p", "channel_combine.cl" },
144 { "copy_to_keypoint", "fast_corners.cl" },
145 { "derivative", "derivative.cl" },
146 { "dilate", "dilate.cl" },
147 { "erode", "erode.cl" },
148 { "fast_corners", "fast_corners.cl" },
149 { "fill_image_borders_constant", "fill_border.cl" },
150 { "fill_image_borders_replicate", "fill_border.cl" },
151 { "finalize", "optical_flow_pyramid_lk.cl" },
152 { "gaussian1x5_sub_x", "gaussian_pyramid.cl" },
153 { "gaussian5x1_sub_y", "gaussian_pyramid.cl" },
154 { "gemm_accumulate_biases_f16", "gemm.cl" },
155 { "gemm_accumulate_biases_f32", "gemm.cl" },
156 { "gemm_interleave4x4_8bit", "gemm.cl" },
157 { "gemm_interleave4x4_16bit", "gemm.cl" },
158 { "gemm_interleave4x4_32bit", "gemm.cl" },
159 { "gemm_ma_f16", "gemm.cl" },
160 { "gemm_ma_f32", "gemm.cl" },
161 { "gemm_mm_u8", "gemm.cl" },
162 { "gemm_mm_f16", "gemm.cl" },
163 { "gemm_mm_f32_midgard", "gemm.cl" },
164 { "gemm_mm_f32_bifrost", "gemm.cl" },
165 { "gemm_vm_f16", "gemm.cl" },
166 { "gemm_vm_f32", "gemm.cl" },
167 { "gemm_lc_vm_f32", "gemm.cl" },
Gian Marco Iodice9f89bae2017-06-22 12:09:49 +0100168 { "gemm_transpose1x16", "gemm.cl" },
169 { "gemm_transpose1x8", "gemm.cl" },
170 { "gemm_transpose1x4", "gemm.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100171 { "harris_score_3x3", "harris_corners.cl" },
172 { "harris_score_5x5", "harris_corners.cl" },
173 { "harris_score_7x7", "harris_corners.cl" },
174 { "hist_border_kernel", "histogram.cl" },
175 { "hist_border_kernel_fixed", "histogram.cl" },
176 { "hist_local_kernel", "histogram.cl" },
177 { "hist_local_kernel_fixed", "histogram.cl" },
178 { "hog_block_normalization", "hog.cl" },
179 { "hog_detector", "hog.cl" },
180 { "hog_orientation_binning", "hog.cl" },
181 { "hysteresis", "canny.cl" },
182 { "im2col_generic", "convolution_layer.cl" },
183 { "im2col_reduced", "convolution_layer.cl" },
184 { "init_level", "optical_flow_pyramid_lk.cl" },
185 { "init_level_max", "optical_flow_pyramid_lk.cl" },
186 { "init_level_max_initial_estimate", "optical_flow_pyramid_lk.cl" },
187 { "integral_horizontal", "integral_image.cl" },
188 { "integral_vertical", "integral_image.cl" },
189 { "IYUV_to_NV12_bt709", "color_convert.cl" },
190 { "IYUV_to_RGB888_bt709", "color_convert.cl" },
191 { "IYUV_to_RGBA8888_bt709", "color_convert.cl" },
192 { "IYUV_to_YUV444_bt709", "color_convert.cl" },
193 { "lktracker_stage0", "optical_flow_pyramid_lk.cl" },
194 { "lktracker_stage1", "optical_flow_pyramid_lk.cl" },
195 { "magnitude_phase", "magnitude_phase.cl" },
196 { "mean_stddev_accumulate", "mean_stddev.cl" },
197 { "minmax", "minmaxloc.cl" },
198 { "minmax_border", "minmaxloc.cl" },
199 { "minmaxloc", "minmaxloc.cl" },
200 { "non_linear_filter_box3x3", "non_linear_filter3x3.cl" },
201 { "non_linear_filter_cross3x3", "non_linear_filter3x3.cl" },
202 { "non_linear_filter_disk3x3", "non_linear_filter3x3.cl" },
203 { "non_linear_filter_box5x5", "non_linear_filter5x5.cl" },
204 { "non_linear_filter_cross5x5", "non_linear_filter5x5.cl" },
205 { "non_linear_filter_disk5x5", "non_linear_filter5x5.cl" },
206 { "non_max_suppression", "nonmax.cl" },
207 { "normalization_layer_cross_map", "normalization_layer.cl" },
208 { "normalization_layer_in_map_1D", "normalization_layer.cl" },
209 { "batchnormalization_layer", "batchnormalization_layer.cl" },
210 { "NV12_to_IYUV_bt709", "color_convert.cl" },
211 { "NV12_to_RGB888_bt709", "color_convert.cl" },
212 { "NV12_to_RGBA8888_bt709", "color_convert.cl" },
213 { "NV12_to_YUV444_bt709", "color_convert.cl" },
214 { "NV21_to_IYUV_bt709", "color_convert.cl" },
215 { "NV21_to_RGB888_bt709", "color_convert.cl" },
216 { "NV21_to_RGBA8888_bt709", "color_convert.cl" },
217 { "NV21_to_YUV444_bt709", "color_convert.cl" },
218 { "pixelwise_mul_float", "pixelwise_mul_float.cl" },
219 { "pixelwise_mul_int", "pixelwise_mul_int.cl" },
220 { "pooling_layer_2", "pooling_layer.cl" },
221 { "pooling_layer_3", "pooling_layer.cl" },
Georgios Pinitasce093142017-06-19 16:11:53 +0100222 { "pooling_layer_7", "pooling_layer.cl" },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100223 { "remap_nearest_neighbour", "remap.cl" },
224 { "remap_bilinear", "remap.cl" },
225 { "reshape_to_columns", "convolution_layer.cl" },
226 { "RGB888_to_IYUV_bt709", "color_convert.cl" },
227 { "RGB888_to_NV12_bt709", "color_convert.cl" },
228 { "RGB888_to_RGBA8888_bt709", "color_convert.cl" },
229 { "RGB888_to_YUV444_bt709", "color_convert.cl" },
230 { "RGBA8888_to_IYUV_bt709", "color_convert.cl" },
231 { "RGBA8888_to_NV12_bt709", "color_convert.cl" },
232 { "RGBA8888_to_RGB888_bt709", "color_convert.cl" },
233 { "RGBA8888_to_YUV444_bt709", "color_convert.cl" },
234 { "scale_nearest_neighbour", "scale.cl" },
235 { "scale_bilinear", "scale.cl" },
236 { "scharr3x3", "scharr_filter.cl" },
237 { "sobel3x3", "sobel_filter.cl" },
238 { "sobel_separable5x1", "sobel_filter.cl" },
239 { "sobel_separable1x5", "sobel_filter.cl" },
240 { "sobel_separable7x1", "sobel_filter.cl" },
241 { "sobel_separable1x7", "sobel_filter.cl" },
242 { "softmax_layer_max", "softmax_layer.cl" },
243 { "softmax_layer_shift_exp_sum", "softmax_layer.cl" },
244 { "softmax_layer_norm", "softmax_layer.cl" },
245 { "suppress_non_maximum", "canny.cl" },
246 { "tablelookup_U8", "tablelookup.cl" },
247 { "tablelookup_S16", "tablelookup.cl" },
248 { "threshold_binary", "threshold.cl" },
249 { "threshold_range", "threshold.cl" },
250 { "transpose", "transpose.cl" },
251 { "UYVY422_to_IYUV_bt709", "color_convert.cl" },
252 { "UYVY422_to_NV12_bt709", "color_convert.cl" },
253 { "UYVY422_to_RGB888_bt709", "color_convert.cl" },
254 { "UYVY422_to_RGBA8888_bt709", "color_convert.cl" },
255 { "warp_affine_nearest_neighbour", "warp_affine.cl" },
256 { "warp_affine_bilinear", "warp_affine.cl" },
257 { "warp_perspective_nearest_neighbour", "warp_perspective.cl" },
258 { "warp_perspective_bilinear", "warp_perspective.cl" },
259 { "YUYV422_to_IYUV_bt709", "color_convert.cl" },
260 { "YUYV422_to_NV12_bt709", "color_convert.cl" },
261 { "YUYV422_to_RGB888_bt709", "color_convert.cl" },
262 { "YUYV422_to_RGBA8888_bt709", "color_convert.cl" },
263};
264
265const std::map<std::string, std::string> CLKernelLibrary::_program_source_map =
266{
267#ifdef EMBEDDED_KERNELS
268 {
269 "absdiff.cl",
270#include "./cl_kernels/absdiff.clembed"
271 },
272 {
273 "accumulate.cl",
274#include "./cl_kernels/accumulate.clembed"
275 },
276 {
277 "activation_layer.cl",
278#include "./cl_kernels/activation_layer.clembed"
279 },
280 {
281 "arithmetic_op.cl",
282#include "./cl_kernels/arithmetic_op.clembed"
283 },
284 {
285 "bitwise_op.cl",
286#include "./cl_kernels/bitwise_op.clembed"
287 },
288 {
289 "canny.cl",
290#include "./cl_kernels/canny.clembed"
291 },
292 {
293 "channel_combine.cl",
294#include "./cl_kernels/channel_combine.clembed"
295 },
296 {
297 "channel_extract.cl",
298#include "./cl_kernels/channel_extract.clembed"
299 },
300 {
301 "concatenate.cl",
302#include "./cl_kernels/concatenate.clembed"
303 },
304 {
305 "color_convert.cl",
306#include "./cl_kernels/color_convert.clembed"
307 },
308 {
309 "convolution3x3.cl",
310#include "./cl_kernels/convolution3x3.clembed"
311 },
312 {
313 "convolution5x5.cl",
314#include "./cl_kernels/convolution5x5.clembed"
315 },
316 {
317 "convolution7x7.cl",
318#include "./cl_kernels/convolution7x7.clembed"
319 },
320 {
321 "convolution9x9.cl",
322#include "./cl_kernels/convolution9x9.clembed"
323 },
324 {
325 "convolution_layer.cl",
326#include "./cl_kernels/convolution_layer.clembed"
327 },
328 {
329 "convolution_rectangle.cl",
330#include "./cl_kernels/convolution_rectangle.clembed"
331 },
332 {
333 "depth_convert.cl",
334#include "./cl_kernels/depth_convert.clembed"
335 },
336 {
337 "derivative.cl",
338#include "./cl_kernels/derivative.clembed"
339 },
340 {
341 "dilate.cl",
342#include "./cl_kernels/dilate.clembed"
343 },
344 {
345 "erode.cl",
346#include "./cl_kernels/erode.clembed"
347 },
348 {
349 "fast_corners.cl",
350#include "./cl_kernels/fast_corners.clembed"
351 },
352 {
353 "fill_border.cl",
354#include "./cl_kernels/fill_border.clembed"
355 },
356 {
357 "gaussian_pyramid.cl",
358#include "./cl_kernels/gaussian_pyramid.clembed"
359 },
360 {
361 "gemm.cl",
362#include "./cl_kernels/gemm.clembed"
363 },
364 {
365 "harris_corners.cl",
366#include "./cl_kernels/harris_corners.clembed"
367 },
368 {
369 "helpers.h",
370#include "./cl_kernels/helpers.hembed"
371 },
372 {
373 "histogram.cl",
374#include "./cl_kernels/histogram.clembed"
375 },
376 {
377 "hog.cl",
378#include "./cl_kernels/hog.clembed"
379 },
380 {
381 "integral_image.cl",
382#include "./cl_kernels/integral_image.clembed"
383 },
384 {
385 "magnitude_phase.cl",
386#include "./cl_kernels/magnitude_phase.clembed"
387 },
388 {
389 "mean_stddev.cl",
390#include "./cl_kernels/mean_stddev.clembed"
391 },
392 {
393 "minmaxloc.cl",
394#include "./cl_kernels/minmaxloc.clembed"
395 },
396 {
397 "non_linear_filter3x3.cl",
398#include "./cl_kernels/non_linear_filter3x3.clembed"
399 },
400 {
401 "non_linear_filter5x5.cl",
402#include "./cl_kernels/non_linear_filter5x5.clembed"
403 },
404 {
405 "non_linear_filter_helpers.h",
406#include "./cl_kernels/non_linear_filter_helpers.hembed"
407 },
408 {
409 "nonmax.cl",
410#include "./cl_kernels/nonmax.clembed"
411 },
412 {
413 "normalization_layer.cl",
414#include "./cl_kernels/normalization_layer.clembed"
415 },
416 {
417 "batchnormalization_layer.cl",
418#include "./cl_kernels/batchnormalization_layer.clembed"
419 },
420 {
421 "optical_flow_pyramid_lk.cl",
422#include "./cl_kernels/optical_flow_pyramid_lk.clembed"
423 },
424 {
425 "pixelwise_mul_float.cl",
426#include "./cl_kernels/pixelwise_mul_float.clembed"
427 },
428 {
429 "pixelwise_mul_int.cl",
430#include "./cl_kernels/pixelwise_mul_int.clembed"
431 },
432 {
433 "pooling_layer.cl",
434#include "./cl_kernels/pooling_layer.clembed"
435 },
436 {
437 "remap.cl",
438#include "./cl_kernels/remap.clembed"
439 },
440 {
441 "scale.cl",
442#include "./cl_kernels/scale.clembed"
443 },
444 {
445 "scharr_filter.cl",
446#include "./cl_kernels/scharr_filter.clembed"
447 },
448 {
449 "sobel_filter.cl",
450#include "./cl_kernels/sobel_filter.clembed"
451 },
452 {
453 "softmax_layer.cl",
454#include "./cl_kernels/softmax_layer.clembed"
455 },
456 {
457 "tablelookup.cl",
458#include "./cl_kernels/tablelookup.clembed"
459 },
460 {
461 "threshold.cl",
462#include "./cl_kernels/threshold.clembed"
463 },
464 {
465 "transpose.cl",
466#include "./cl_kernels/transpose.clembed"
467 },
468 {
469 "types.h",
470#include "./cl_kernels/types.hembed"
471 },
472 {
473 "warp_affine.cl",
474#include "./cl_kernels/warp_affine.clembed"
475 },
476 {
477 "warp_helpers.h",
478#include "./cl_kernels/warp_helpers.hembed"
479 },
480 {
481 "warp_perspective.cl",
482#include "./cl_kernels/warp_perspective.clembed"
483 }
484#endif
485};
486
487CLKernelLibrary::CLKernelLibrary()
488 : _context(), _device(), _kernel_path("."), _programs_map(), _built_programs_map()
489{
490}
491
492CLKernelLibrary &CLKernelLibrary::get()
493{
494 static CLKernelLibrary _kernel_library;
495 return _kernel_library;
496}
497
498Kernel CLKernelLibrary::create_kernel(const std::string &kernel_name, const StringSet &build_options_set) const
499{
500 // Find which program contains the kernel
501 auto kernel_program_it = _kernel_program_map.find(kernel_name);
502
503 if(_kernel_program_map.end() == kernel_program_it)
504 {
505 ARM_COMPUTE_ERROR("Kernel %s not found in the CLKernelLibrary", kernel_name.c_str());
506 }
507
508 // Check if the program has been built before with same build options.
509 const std::string program_name = kernel_program_it->second;
510 const std::string build_options = stringify_set(build_options_set);
511 const std::string built_program_name = program_name + "_" + build_options;
512 auto built_program_it = _built_programs_map.find(built_program_name);
513
514 cl::Program cl_program;
515
516 if(_built_programs_map.end() != built_program_it)
517 {
518 // If program has been built, retrieve to create kernel from it
519 cl_program = built_program_it->second;
520 }
521 else
522 {
523 // Get program
524 Program program = load_program(program_name);
525
526 // Build program
527 cl_program = program.build(build_options);
528
529 // Add built program to internal map
530 _built_programs_map.emplace(built_program_name, cl_program);
531 }
532
533 // Create and return kernel
534 return Kernel(kernel_name, cl_program);
535}
536
537const Program &CLKernelLibrary::load_program(const std::string &program_name) const
538{
539 const auto program_it = _programs_map.find(program_name);
540
541 if(program_it != _programs_map.end())
542 {
543 return program_it->second;
544 }
545
546 Program program;
547
548#ifdef EMBEDDED_KERNELS
549 const auto program_source_it = _program_source_map.find(program_name);
550
551 if(_program_source_map.end() == program_source_it)
552 {
553 ARM_COMPUTE_ERROR("Embedded program for %s does not exist.", program_name.c_str());
554 }
555
556 program = Program(_context, program_name, program_source_it->second);
557#else
558 // Check for binary
559 std::string source_name = _kernel_path + program_name;
560 std::string binary_name = source_name + "bin";
561
562 if(std::ifstream(binary_name).is_open())
563 {
564 const std::string program_binary = read_file(binary_name, true);
565 program = Program(_context, _device, program_name, std::vector<unsigned char>(program_binary.begin(), program_binary.end()));
566 }
567 else if(std::ifstream(source_name).is_open())
568 {
569 program = Program(_context, program_name, read_file(source_name, false));
570 }
571 else
572 {
573 ARM_COMPUTE_ERROR("Kernel file %s does not exist.", source_name.c_str());
574 }
575#endif
576
577 // Insert program to program map
578 const auto new_program = _programs_map.emplace(program_name, std::move(program));
579
580 return new_program.first->second;
581}
582
583std::string CLKernelLibrary::stringify_set(const StringSet &s) const
584{
585 std::string concat_set = "-cl-arm-non-uniform-work-group-size ";
586
587#ifndef EMBEDDED_KERNELS
588 concat_set += "-I" + _kernel_path + " ";
589#endif /* EMBEDDED_KERNELS */
590
591 // Concatenate set
592 for(const auto &el : s)
593 {
594 concat_set += " " + el;
595 }
596
597 return concat_set;
598}