blob: 3eeef5583ca0ce646a20978259f138f4f11468dd [file] [log] [blame]
Giorgio Arena205eed82019-08-14 10:13:50 +01001/*
Manuel Bottini8481d832019-12-10 15:28:40 +00002 * Copyright (c) 2019-2020 ARM Limited.
Giorgio Arena205eed82019-08-14 10:13:50 +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/kernels/CLPadLayerKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
Michele Di Giorgio4c268b92019-09-20 14:01:48 +010027#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000028#include "support/StringSupport.h"
Giorgio Arena205eed82019-08-14 10:13:50 +010029
30namespace arm_compute
31{
32namespace
33{
34Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
35{
Manuel Bottini8481d832019-12-10 15:28:40 +000036 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010037 ARM_COMPUTE_UNUSED(constant_value);
Manuel Bottini8481d832019-12-10 15:28:40 +000038 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010039 ARM_COMPUTE_RETURN_ERROR_ON(padding.size() > input->num_dimensions());
40 if(mode == PaddingMode::REFLECT || mode == PaddingMode::SYMMETRIC)
41 {
42 ARM_COMPUTE_RETURN_ERROR_ON(padding.size() > 3);
43
44 const auto is_reflect = static_cast<unsigned int>(mode == PaddingMode::REFLECT);
45 for(size_t i = 0; i < padding.size(); ++i)
46 {
47 ARM_COMPUTE_RETURN_ERROR_ON(padding.at(i).first > (input->dimension(i) - is_reflect));
48 ARM_COMPUTE_RETURN_ERROR_ON(padding.at(i).second > (input->dimension(i) - is_reflect));
49 }
50 }
51
52 if(output->total_size() > 0)
53 {
54 TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(input->tensor_shape(), padding);
55
56 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(output, input);
57 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), padded_shape);
58 }
Giorgio Arena205eed82019-08-14 10:13:50 +010059
60 return Status{};
61}
62
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010063std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode,
64 unsigned int &num_elems_processed_per_iteration)
Giorgio Arena205eed82019-08-14 10:13:50 +010065{
66 ARM_COMPUTE_UNUSED(constant_value, mode);
Michele Di Giorgio4c268b92019-09-20 14:01:48 +010067
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010068 const TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(input->tensor_shape(), padding);
69 auto_init_if_empty(*output, input->clone()->set_tensor_shape(padded_shape));
70
71 num_elems_processed_per_iteration = std::min(16U, 32U / static_cast<unsigned int>(element_size_from_data_type(input->data_type())));
72 if(input->dimension(0) < num_elems_processed_per_iteration)
73 {
74 num_elems_processed_per_iteration = 1 << static_cast<unsigned int>(std::log2(input->dimension(0)));
75 }
Giorgio Arena205eed82019-08-14 10:13:50 +010076
77 // Configure kernel window
78 Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
79
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010080 const int input_start_x = mode == PaddingMode::CONSTANT ? -(padding.at(0).first % num_elems_processed_per_iteration) : 0;
81 const int input_start_y = (mode == PaddingMode::CONSTANT && padding.size() > 1) ? -padding.at(1).first : 0;
Giorgio Arena205eed82019-08-14 10:13:50 +010082
83 AccessWindowRectangle input_access(input, input_start_x, input_start_y, num_elems_processed_per_iteration, 1);
84 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
85
86 const bool window_changed = update_window_and_padding(win, input_access, output_access);
87 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
88
89 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
90 return std::make_pair(err, win);
91}
92} // namespace
93
94CLPadLayerKernel::CLPadLayerKernel()
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010095 : _input(nullptr), _output(nullptr), _input_start_x(0), _input_start_y(0), _4d_enabled(false)
Giorgio Arena205eed82019-08-14 10:13:50 +010096{
97}
98
99void CLPadLayerKernel::configure(const ICLTensor *input, ICLTensor *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
100{
Michele Di Giorgio4c268b92019-09-20 14:01:48 +0100101 // Perform validation step
Giorgio Arena205eed82019-08-14 10:13:50 +0100102 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Giorgio Arena205eed82019-08-14 10:13:50 +0100103 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), padding, constant_value, mode));
104
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100105 _input = input;
106 _output = output;
107 _4d_enabled = (mode == PaddingMode::CONSTANT) && (padding.size() > 3);
108
109 // Configure window
110 unsigned int vec_size;
111 auto win_config = validate_and_configure_window(input->info(), output->info(), padding, constant_value, mode, vec_size);
112 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
113 ICLKernel::configure_internal(win_config.second);
Giorgio Arena205eed82019-08-14 10:13:50 +0100114
115 // Set build options
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100116 std::string kernel_name = "pad_layer_";
117
118 const DataType &data_type = input->info()->data_type();
119 const unsigned int input_width = input->info()->dimension(0);
120 const unsigned int input_height = input->info()->dimension(1);
121 const unsigned int input_depth = input->info()->dimension(2);
122 const unsigned int pad_x_before = padding.at(0).first;
123 const unsigned int pad_y_before = padding.size() > 1 ? padding.at(1).first : 0;
124 const unsigned int pad_z_before = padding.size() > 2 ? padding.at(2).first : 0;
125 const unsigned int pad_right_start = input_width + pad_x_before;
126
127 _input_start_x = mode == PaddingMode::CONSTANT ? -(pad_x_before % vec_size) : 0;
128 _input_start_y = (mode == PaddingMode::CONSTANT && padding.size() > 1) ? -padding.at(1).first : 0;
Giorgio Arena205eed82019-08-14 10:13:50 +0100129
130 CLBuildOptions build_opts;
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100131 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
132 build_opts.add_option("-DSELECT_DT=" + get_cl_select_type_from_data_type(data_type));
133 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(vec_size));
134 build_opts.add_option("-DPAD_X_BEFORE=" + support::cpp11::to_string(pad_x_before));
135 build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(input_width));
Giorgio Arena205eed82019-08-14 10:13:50 +0100136 if(padding.size() > 1)
137 {
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100138 build_opts.add_option("-DPAD_Y_BEFORE=" + support::cpp11::to_string(pad_y_before));
139 build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(input_height));
Giorgio Arena205eed82019-08-14 10:13:50 +0100140
141 if(padding.size() > 2)
142 {
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100143 build_opts.add_option("-DPAD_Z_BEFORE=" + support::cpp11::to_string(pad_z_before));
144 build_opts.add_option("-DSRC_DEPTH=" + support::cpp11::to_string(input_depth));
Giorgio Arena205eed82019-08-14 10:13:50 +0100145 }
146 }
147
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100148 switch(mode)
149 {
150 case PaddingMode::CONSTANT:
151 {
152 kernel_name += "constant";
Giorgio Arena205eed82019-08-14 10:13:50 +0100153
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100154 build_opts.add_option("-DCONST_VAL=" + string_from_pixel_value(constant_value, data_type));
155 build_opts.add_option_if(pad_x_before >= vec_size, "-DNUM_THREADS_TO_SKIP_X=" + support::cpp11::to_string(pad_x_before / vec_size));
156
157 if(_4d_enabled)
158 {
159 build_opts.add_option("-DPAD_W_BEFORE=" + support::cpp11::to_string(padding.at(3).first));
160 build_opts.add_option("-DSRC_BATCH=" + support::cpp11::to_string(input->info()->dimension(3)));
161 }
162
163 break;
164 }
165 case PaddingMode::SYMMETRIC:
166 case PaddingMode::REFLECT:
167 {
168 kernel_name += "symmetric_reflect";
169
170 const auto is_reflect = static_cast<unsigned int>(mode == PaddingMode::REFLECT);
171
172 const unsigned int pad_x_before_remainder = pad_x_before % vec_size;
173 const unsigned int pad_x_after_remainder = pad_right_start % vec_size;
174 const unsigned int after_pad_fact_x = (2 * input_width + pad_x_before) - is_reflect;
175 const unsigned int output_last_x = ceil_to_multiple(pad_right_start + padding.at(0).second, vec_size);
176
177 build_opts.add_option("-DIS_REFLECT=" + support::cpp11::to_string(is_reflect));
178 build_opts.add_option("-DPAD_X_BEFORE_REMAINDER=" + support::cpp11::to_string(pad_x_before_remainder));
179 build_opts.add_option("-DPAD_X_AFTER_REMAINDER=" + support::cpp11::to_string(pad_x_after_remainder));
180 build_opts.add_option("-DPAD_X_BEFORE_REMAINDER_REFL=" + support::cpp11::to_string((pad_x_before_remainder + is_reflect) % vec_size));
181 build_opts.add_option("-DPAD_X_AFTER_REMAINDER_REFL=" + support::cpp11::to_string((pad_x_after_remainder - is_reflect) % vec_size));
182 build_opts.add_option("-DAFTER_PAD_FACT_X=" + support::cpp11::to_string(after_pad_fact_x));
183 build_opts.add_option_if(after_pad_fact_x < output_last_x, "-DAFTER_PAD_REM=" + support::cpp11::to_string(after_pad_fact_x % vec_size));
184
185 break;
186 }
187 default:
188 ARM_COMPUTE_ERROR("Padding mode not supported.");
189 }
190
191 // Create kernel
192 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
Giorgio Arena205eed82019-08-14 10:13:50 +0100193}
194
195Status CLPadLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
196{
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100197 unsigned int vec_size;
Giorgio Arena205eed82019-08-14 10:13:50 +0100198 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, padding, constant_value, mode));
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100199 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), padding, constant_value, mode, vec_size).first);
Giorgio Arena205eed82019-08-14 10:13:50 +0100200
201 return Status{};
202}
203
204void CLPadLayerKernel::run(const Window &window, cl::CommandQueue &queue)
205{
206 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
207 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
208
209 Window win_in = window;
210 win_in.adjust(Window::DimX, _input_start_x, true);
211 win_in.adjust(Window::DimY, _input_start_y, true);
212
Michele Di Giorgio4c268b92019-09-20 14:01:48 +0100213 Window slice_out = window.first_slice_window_3D();
214 Window slice_in = win_in.first_slice_window_3D();
215 unsigned int batch = 0;
Giorgio Arena205eed82019-08-14 10:13:50 +0100216 do
217 {
218 unsigned int idx = 0;
219 add_3D_tensor_argument(idx, _input, slice_in);
220 add_3D_tensor_argument(idx, _output, slice_out);
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100221 if(_4d_enabled)
222 {
223 add_argument<unsigned int>(idx, batch++);
224 }
Giorgio Arena205eed82019-08-14 10:13:50 +0100225
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100226 enqueue(queue, *this, slice_out, lws_hint());
Giorgio Arena205eed82019-08-14 10:13:50 +0100227 }
228 while(window.slide_window_slice_3D(slice_out) && win_in.slide_window_slice_3D(slice_in));
229}
230} // namespace arm_compute