blob: 53f313c0d39bc8c569230f9196e688ffcd5e69e5 [file] [log] [blame]
Giorgio Arena205eed82019-08-14 10:13:50 +01001/*
Matthew Bentham314d3e22023-06-23 10:53:52 +00002 * Copyright (c) 2019-2021, 2023 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 */
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010024#include "src/core/CL/kernels/CLPadLayerKernel.h"
Giorgio Arena205eed82019-08-14 10:13:50 +010025
26#include "arm_compute/core/CL/CLHelpers.h"
Michele Di Giorgiof6f78762020-07-06 11:27:21 +010027#include "arm_compute/core/CL/ICLTensor.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000028#include "arm_compute/core/utils/helpers/AdjustVecSize.h"
Michele Di Giorgio4c268b92019-09-20 14:01:48 +010029#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010030#include "src/core/helpers/AutoConfiguration.h"
31#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000032#include "support/StringSupport.h"
Giorgio Arena205eed82019-08-14 10:13:50 +010033
34namespace arm_compute
35{
36namespace
37{
38Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
39{
Manuel Bottini8481d832019-12-10 15:28:40 +000040 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010041 ARM_COMPUTE_UNUSED(constant_value);
Manuel Bottini8481d832019-12-10 15:28:40 +000042 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Giorgio Arena93240222020-12-23 11:55:29 +000043 ARM_COMPUTE_RETURN_ERROR_ON((padding.size() < 1) || (padding.size() > input->num_dimensions()));
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010044 if(mode == PaddingMode::REFLECT || mode == PaddingMode::SYMMETRIC)
45 {
46 ARM_COMPUTE_RETURN_ERROR_ON(padding.size() > 3);
47
48 const auto is_reflect = static_cast<unsigned int>(mode == PaddingMode::REFLECT);
49 for(size_t i = 0; i < padding.size(); ++i)
50 {
51 ARM_COMPUTE_RETURN_ERROR_ON(padding.at(i).first > (input->dimension(i) - is_reflect));
52 ARM_COMPUTE_RETURN_ERROR_ON(padding.at(i).second > (input->dimension(i) - is_reflect));
53 }
54 }
55
56 if(output->total_size() > 0)
57 {
58 TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(input->tensor_shape(), padding);
59
60 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(output, input);
61 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), padded_shape);
62 }
Giorgio Arena205eed82019-08-14 10:13:50 +010063
64 return Status{};
65}
Giorgio Arena205eed82019-08-14 10:13:50 +010066} // namespace
67
68CLPadLayerKernel::CLPadLayerKernel()
Giorgio Arena93240222020-12-23 11:55:29 +000069 : _input(nullptr), _output(nullptr), _4d_enabled(false)
Giorgio Arena205eed82019-08-14 10:13:50 +010070{
Giorgio Arena4a95bba2021-06-28 11:00:27 +010071 _type = CLKernelType::ELEMENTWISE;
Giorgio Arena205eed82019-08-14 10:13:50 +010072}
73
74void CLPadLayerKernel::configure(const ICLTensor *input, ICLTensor *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
75{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010076 configure(CLKernelLibrary::get().get_compile_context(), input, output, padding, constant_value, mode);
77}
78
Manuel Bottini679fc962020-04-21 16:08:53 +010079void CLPadLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010080{
Giorgio Arena205eed82019-08-14 10:13:50 +010081 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Giorgio Arena93240222020-12-23 11:55:29 +000082 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(misc::shape_calculator::compute_padded_shape(input->info()->tensor_shape(), padding)));
Giorgio Arena205eed82019-08-14 10:13:50 +010083 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), padding, constant_value, mode));
84
Giorgio Arena93240222020-12-23 11:55:29 +000085 auto padding_info = get_padding_info({ input, output });
86
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010087 _input = input;
88 _output = output;
89 _4d_enabled = (mode == PaddingMode::CONSTANT) && (padding.size() > 3);
90
Giorgio Arena205eed82019-08-14 10:13:50 +010091 // Set build options
Giorgio Arena93240222020-12-23 11:55:29 +000092 const DataType &data_type = input->info()->data_type();
93 const unsigned int input_width = input->info()->dimension(0);
94 const unsigned int input_height = input->info()->dimension(1);
95 const unsigned int input_depth = input->info()->dimension(2);
96 const unsigned int pad_x_before = padding.at(0).first;
97 const unsigned int pad_y_before = padding.size() > 1 ? padding.at(1).first : 0;
98 const unsigned int pad_z_before = padding.size() > 2 ? padding.at(2).first : 0;
99 const unsigned int vec_size = adjust_vec_size(std::min(16U, 32U / static_cast<unsigned int>(element_size_from_data_type(input->info()->data_type()))), input_width);
100 const unsigned int pad_right_start = input_width + pad_x_before;
101 const unsigned int pad_x_before_remainder = pad_x_before % vec_size;
102 const unsigned int vec_size_leftover_write = vec_size - (ceil_to_multiple(output->info()->dimension(0), vec_size) - output->info()->dimension(0));
Giorgio Arena205eed82019-08-14 10:13:50 +0100103
104 CLBuildOptions build_opts;
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100105 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100106 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(vec_size));
107 build_opts.add_option("-DPAD_X_BEFORE=" + support::cpp11::to_string(pad_x_before));
108 build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(input_width));
Giorgio Arena93240222020-12-23 11:55:29 +0000109 build_opts.add_option("-DPAD_X_BEFORE_REMAINDER=" + support::cpp11::to_string(pad_x_before_remainder));
110 build_opts.add_option("-DVEC_SIZE_LEFTOVER_WRITE=" + support::cpp11::to_string(vec_size_leftover_write));
Giorgio Arena205eed82019-08-14 10:13:50 +0100111 if(padding.size() > 1)
112 {
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100113 build_opts.add_option("-DPAD_Y_BEFORE=" + support::cpp11::to_string(pad_y_before));
114 build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(input_height));
Giorgio Arena205eed82019-08-14 10:13:50 +0100115
116 if(padding.size() > 2)
117 {
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100118 build_opts.add_option("-DPAD_Z_BEFORE=" + support::cpp11::to_string(pad_z_before));
119 build_opts.add_option("-DSRC_DEPTH=" + support::cpp11::to_string(input_depth));
Giorgio Arena205eed82019-08-14 10:13:50 +0100120 }
121 }
122
Giorgio Arena93240222020-12-23 11:55:29 +0000123 std::string kernel_name = "pad_layer_";
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100124 switch(mode)
125 {
126 case PaddingMode::CONSTANT:
127 {
128 kernel_name += "constant";
Giorgio Arena205eed82019-08-14 10:13:50 +0100129
Giorgio Arena93240222020-12-23 11:55:29 +0000130 const unsigned int vec_size_leftover_read = vec_size - (ceil_to_multiple(pad_right_start, vec_size) - pad_right_start);
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100131
Giorgio Arena93240222020-12-23 11:55:29 +0000132 build_opts.add_option("-DCONST_VAL=" + string_from_pixel_value(constant_value, data_type));
133 build_opts.add_option("-DVEC_SIZE_LEFTOVER_READ=" + support::cpp11::to_string(vec_size_leftover_read));
134
135 if(pad_x_before >= vec_size)
136 {
137 build_opts.add_option("-DTHREADS_TO_SKIP_BEFORE=" + support::cpp11::to_string(pad_x_before / vec_size));
138 build_opts.add_option("-DTHREADS_TO_SKIP_AFTER=" + support::cpp11::to_string(pad_right_start / vec_size));
139 }
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100140 if(_4d_enabled)
141 {
142 build_opts.add_option("-DPAD_W_BEFORE=" + support::cpp11::to_string(padding.at(3).first));
143 build_opts.add_option("-DSRC_BATCH=" + support::cpp11::to_string(input->info()->dimension(3)));
144 }
145
146 break;
147 }
148 case PaddingMode::SYMMETRIC:
149 case PaddingMode::REFLECT:
150 {
151 kernel_name += "symmetric_reflect";
152
153 const auto is_reflect = static_cast<unsigned int>(mode == PaddingMode::REFLECT);
154
Giorgio Arena93240222020-12-23 11:55:29 +0000155 const unsigned int pad_x_after_remainder = pad_right_start % vec_size;
156 const unsigned int after_pad_fact_x = (2 * input_width + pad_x_before) - is_reflect;
157 const unsigned int output_last_x = ceil_to_multiple(pad_right_start + padding.at(0).second, vec_size);
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100158
159 build_opts.add_option("-DIS_REFLECT=" + support::cpp11::to_string(is_reflect));
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100160 build_opts.add_option("-DPAD_X_AFTER_REMAINDER=" + support::cpp11::to_string(pad_x_after_remainder));
161 build_opts.add_option("-DPAD_X_BEFORE_REMAINDER_REFL=" + support::cpp11::to_string((pad_x_before_remainder + is_reflect) % vec_size));
162 build_opts.add_option("-DPAD_X_AFTER_REMAINDER_REFL=" + support::cpp11::to_string((pad_x_after_remainder - is_reflect) % vec_size));
163 build_opts.add_option("-DAFTER_PAD_FACT_X=" + support::cpp11::to_string(after_pad_fact_x));
164 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));
165
166 break;
167 }
168 default:
169 ARM_COMPUTE_ERROR("Padding mode not supported.");
170 }
171
172 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100173 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Giorgio Arena93240222020-12-23 11:55:29 +0000174
175 // Configure window
176 Window win = calculate_max_window(*output->info(), Steps(vec_size));
177 ICLKernel::configure_internal(win);
178
179 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Giorgio Arena205eed82019-08-14 10:13:50 +0100180}
181
182Status CLPadLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
183{
184 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, padding, constant_value, mode));
Giorgio Arena205eed82019-08-14 10:13:50 +0100185 return Status{};
186}
187
188void CLPadLayerKernel::run(const Window &window, cl::CommandQueue &queue)
189{
190 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
191 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
192
Giorgio Arena93240222020-12-23 11:55:29 +0000193 Window slice = window.first_slice_window_3D();
194 unsigned int batch = 0;
Giorgio Arena205eed82019-08-14 10:13:50 +0100195 do
196 {
197 unsigned int idx = 0;
Giorgio Arena93240222020-12-23 11:55:29 +0000198 add_3D_tensor_argument(idx, _input, slice);
199 add_3D_tensor_argument(idx, _output, slice);
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100200 if(_4d_enabled)
201 {
202 add_argument<unsigned int>(idx, batch++);
203 }
Giorgio Arena205eed82019-08-14 10:13:50 +0100204
Giorgio Arena93240222020-12-23 11:55:29 +0000205 enqueue(queue, *this, slice, lws_hint());
Giorgio Arena205eed82019-08-14 10:13:50 +0100206 }
Giorgio Arena93240222020-12-23 11:55:29 +0000207 while(window.slide_window_slice_3D(slice));
Giorgio Arena205eed82019-08-14 10:13:50 +0100208}
209} // namespace arm_compute