blob: 52b65c39b1f2dddad732a282424271b38b85c22f [file] [log] [blame]
Giorgio Arena205eed82019-08-14 10:13:50 +01001/*
2 * Copyright (c) 2019 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/kernels/CLPadLayerKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27
28namespace arm_compute
29{
30namespace
31{
32Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
33{
34 ARM_COMPUTE_UNUSED(input, output, constant_value);
35 ARM_COMPUTE_RETURN_ERROR_ON(padding.empty());
36 ARM_COMPUTE_RETURN_ERROR_ON_MSG(mode != PaddingMode::CONSTANT, "Only CONSTANT mode supported.");
37
38 return Status{};
39}
40
41std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
42{
43 ARM_COMPUTE_UNUSED(constant_value, mode);
44 const unsigned int num_elems_processed_per_iteration = std::min(16U, 32U / static_cast<unsigned int>(element_size_from_data_type(input->data_type())));
45
46 // Configure kernel window
47 Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
48
49 const int input_start_x = -(padding.at(0).first % num_elems_processed_per_iteration);
50 const int input_start_y = padding.size() > 1 ? -padding.at(1).first : 0;
51
52 AccessWindowRectangle input_access(input, input_start_x, input_start_y, num_elems_processed_per_iteration, 1);
53 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
54
55 const bool window_changed = update_window_and_padding(win, input_access, output_access);
56 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
57
58 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
59 return std::make_pair(err, win);
60}
61} // namespace
62
63CLPadLayerKernel::CLPadLayerKernel()
64 : _input(nullptr), _output(nullptr), _input_start_x(0), _input_start_y(0)
65{
66}
67
68void CLPadLayerKernel::configure(const ICLTensor *input, ICLTensor *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
69{
70 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
71
72 // Output tensor auto initialisation if not yet initialized
73 auto_init_if_empty(*output->info(), *input->info()->clone());
74 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), padding, constant_value, mode));
75
76 _input = input;
77 _output = output;
78
79 // Set build options
80 const unsigned int num_elems_processed_per_iteration = std::min(16U, 32U / static_cast<unsigned int>(element_size_from_data_type(input->info()->data_type())));
81 _input_start_x = -(padding.at(0).first % num_elems_processed_per_iteration);
82 _input_start_y = padding.size() > 1 ? -padding.at(1).first : 0;
83
84 CLBuildOptions build_opts;
85 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
86 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration));
87 build_opts.add_option("-DCONST_VAL=" + string_from_pixel_value(constant_value, input->info()->data_type()));
88 build_opts.add_option("-DPAD_LEFT=" + support::cpp11::to_string(padding.at(0).first));
89 build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(input->info()->dimension(0)));
90 build_opts.add_option("-DSELECT_DT=" + get_cl_select_type_from_data_type(input->info()->data_type()));
91 build_opts.add_option_if(padding.at(0).first > num_elems_processed_per_iteration, "-DTHREADS_TO_SKIP_X=" + support::cpp11::to_string(padding.at(0).first / num_elems_processed_per_iteration));
92
93 if(padding.size() > 1)
94 {
95 build_opts.add_option("-DPAD_TOP=" + support::cpp11::to_string(padding.at(1).first));
96 build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(input->info()->dimension(1)));
97
98 if(padding.size() > 2)
99 {
100 build_opts.add_option("-DPAD_NEAR=" + support::cpp11::to_string(padding.at(2).first));
101 build_opts.add_option("-DSRC_DEPTH=" + support::cpp11::to_string(input->info()->dimension(2)));
Matthew Jacksonc2a60592019-08-30 15:19:42 +0100102
103 if(padding.size() > 3)
104 {
105 build_opts.add_option("-DPAD_BTOP=" + support::cpp11::to_string(padding.at(3).first));
106 build_opts.add_option("-DSRC_BATCH=" + support::cpp11::to_string(input->info()->dimension(3)));
107 }
Giorgio Arena205eed82019-08-14 10:13:50 +0100108 }
109 }
110
111 // Create kernel
112 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("pad_layer", build_opts.options()));
113
114 // Configure kernel window
115 auto win_config = validate_and_configure_window(input->info(), output->info(), padding, constant_value, mode);
116 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
117 ICLKernel::configure_internal(win_config.second);
118}
119
120Status CLPadLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
121{
122 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, padding, constant_value, mode));
123 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), padding, constant_value, mode).first);
124
125 return Status{};
126}
127
128void CLPadLayerKernel::run(const Window &window, cl::CommandQueue &queue)
129{
130 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
131 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
132
133 Window win_in = window;
134 win_in.adjust(Window::DimX, _input_start_x, true);
135 win_in.adjust(Window::DimY, _input_start_y, true);
136
137 Window slice_out = window.first_slice_window_3D();
138 Window slice_in = win_in.first_slice_window_3D();
Matthew Jacksonc2a60592019-08-30 15:19:42 +0100139 unsigned int batch = 0;
Giorgio Arena205eed82019-08-14 10:13:50 +0100140 do
141 {
142 unsigned int idx = 0;
143 add_3D_tensor_argument(idx, _input, slice_in);
144 add_3D_tensor_argument(idx, _output, slice_out);
Matthew Jacksonc2a60592019-08-30 15:19:42 +0100145 add_argument<unsigned int>(idx, batch++);
Giorgio Arena205eed82019-08-14 10:13:50 +0100146
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100147 enqueue(queue, *this, slice_out, lws_hint());
Giorgio Arena205eed82019-08-14 10:13:50 +0100148 }
149 while(window.slide_window_slice_3D(slice_out) && win_in.slide_window_slice_3D(slice_in));
150}
151} // namespace arm_compute