1 /*
2 * Copyright (c) 2019-2021 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 "src/core/CL/kernels/CLPadLayerKernel.h"
25
26 #include "arm_compute/core/CL/CLHelpers.h"
27 #include "arm_compute/core/CL/ICLTensor.h"
28 #include "arm_compute/core/utils/misc/ShapeCalculator.h"
29 #include "src/core/helpers/AutoConfiguration.h"
30 #include "src/core/helpers/WindowHelpers.h"
31 #include "support/StringSupport.h"
32
33 namespace arm_compute
34 {
35 namespace
36 {
validate_arguments(const ITensorInfo * input,const ITensorInfo * output,const PaddingList & padding,PixelValue constant_value,PaddingMode mode)37 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
38 {
39 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
40 ARM_COMPUTE_UNUSED(constant_value);
41 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
42 ARM_COMPUTE_RETURN_ERROR_ON((padding.size() < 1) || (padding.size() > input->num_dimensions()));
43 if(mode == PaddingMode::REFLECT || mode == PaddingMode::SYMMETRIC)
44 {
45 ARM_COMPUTE_RETURN_ERROR_ON(padding.size() > 3);
46
47 const auto is_reflect = static_cast<unsigned int>(mode == PaddingMode::REFLECT);
48 for(size_t i = 0; i < padding.size(); ++i)
49 {
50 ARM_COMPUTE_RETURN_ERROR_ON(padding.at(i).first > (input->dimension(i) - is_reflect));
51 ARM_COMPUTE_RETURN_ERROR_ON(padding.at(i).second > (input->dimension(i) - is_reflect));
52 }
53 }
54
55 if(output->total_size() > 0)
56 {
57 TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(input->tensor_shape(), padding);
58
59 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(output, input);
60 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), padded_shape);
61 }
62
63 return Status{};
64 }
65 } // namespace
66
CLPadLayerKernel()67 CLPadLayerKernel::CLPadLayerKernel()
68 : _input(nullptr), _output(nullptr), _4d_enabled(false)
69 {
70 _type = CLKernelType::ELEMENTWISE;
71 }
72
configure(const ICLTensor * input,ICLTensor * output,const PaddingList & padding,PixelValue constant_value,PaddingMode mode)73 void CLPadLayerKernel::configure(const ICLTensor *input, ICLTensor *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
74 {
75 configure(CLKernelLibrary::get().get_compile_context(), input, output, padding, constant_value, mode);
76 }
77
configure(const CLCompileContext & compile_context,const ICLTensor * input,ICLTensor * output,const PaddingList & padding,PixelValue constant_value,PaddingMode mode)78 void CLPadLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
79 {
80 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
81 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(misc::shape_calculator::compute_padded_shape(input->info()->tensor_shape(), padding)));
82 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), padding, constant_value, mode));
83
84 auto padding_info = get_padding_info({ input, output });
85
86 _input = input;
87 _output = output;
88 _4d_enabled = (mode == PaddingMode::CONSTANT) && (padding.size() > 3);
89
90 // Set build options
91 const DataType &data_type = input->info()->data_type();
92 const unsigned int input_width = input->info()->dimension(0);
93 const unsigned int input_height = input->info()->dimension(1);
94 const unsigned int input_depth = input->info()->dimension(2);
95 const unsigned int pad_x_before = padding.at(0).first;
96 const unsigned int pad_y_before = padding.size() > 1 ? padding.at(1).first : 0;
97 const unsigned int pad_z_before = padding.size() > 2 ? padding.at(2).first : 0;
98 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);
99 const unsigned int pad_right_start = input_width + pad_x_before;
100 const unsigned int pad_x_before_remainder = pad_x_before % vec_size;
101 const unsigned int vec_size_leftover_write = vec_size - (ceil_to_multiple(output->info()->dimension(0), vec_size) - output->info()->dimension(0));
102
103 CLBuildOptions build_opts;
104 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
105 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(vec_size));
106 build_opts.add_option("-DPAD_X_BEFORE=" + support::cpp11::to_string(pad_x_before));
107 build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(input_width));
108 build_opts.add_option("-DPAD_X_BEFORE_REMAINDER=" + support::cpp11::to_string(pad_x_before_remainder));
109 build_opts.add_option("-DVEC_SIZE_LEFTOVER_WRITE=" + support::cpp11::to_string(vec_size_leftover_write));
110 if(padding.size() > 1)
111 {
112 build_opts.add_option("-DPAD_Y_BEFORE=" + support::cpp11::to_string(pad_y_before));
113 build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(input_height));
114
115 if(padding.size() > 2)
116 {
117 build_opts.add_option("-DPAD_Z_BEFORE=" + support::cpp11::to_string(pad_z_before));
118 build_opts.add_option("-DSRC_DEPTH=" + support::cpp11::to_string(input_depth));
119 }
120 }
121
122 std::string kernel_name = "pad_layer_";
123 switch(mode)
124 {
125 case PaddingMode::CONSTANT:
126 {
127 kernel_name += "constant";
128
129 const unsigned int vec_size_leftover_read = vec_size - (ceil_to_multiple(pad_right_start, vec_size) - pad_right_start);
130
131 build_opts.add_option("-DCONST_VAL=" + string_from_pixel_value(constant_value, data_type));
132 build_opts.add_option("-DVEC_SIZE_LEFTOVER_READ=" + support::cpp11::to_string(vec_size_leftover_read));
133
134 if(pad_x_before >= vec_size)
135 {
136 build_opts.add_option("-DTHREADS_TO_SKIP_BEFORE=" + support::cpp11::to_string(pad_x_before / vec_size));
137 build_opts.add_option("-DTHREADS_TO_SKIP_AFTER=" + support::cpp11::to_string(pad_right_start / vec_size));
138 }
139 if(_4d_enabled)
140 {
141 build_opts.add_option("-DPAD_W_BEFORE=" + support::cpp11::to_string(padding.at(3).first));
142 build_opts.add_option("-DSRC_BATCH=" + support::cpp11::to_string(input->info()->dimension(3)));
143 }
144
145 break;
146 }
147 case PaddingMode::SYMMETRIC:
148 case PaddingMode::REFLECT:
149 {
150 kernel_name += "symmetric_reflect";
151
152 const auto is_reflect = static_cast<unsigned int>(mode == PaddingMode::REFLECT);
153
154 const unsigned int pad_x_after_remainder = pad_right_start % vec_size;
155 const unsigned int after_pad_fact_x = (2 * input_width + pad_x_before) - is_reflect;
156 const unsigned int output_last_x = ceil_to_multiple(pad_right_start + padding.at(0).second, vec_size);
157
158 build_opts.add_option("-DIS_REFLECT=" + support::cpp11::to_string(is_reflect));
159 build_opts.add_option("-DPAD_X_AFTER_REMAINDER=" + support::cpp11::to_string(pad_x_after_remainder));
160 build_opts.add_option("-DPAD_X_BEFORE_REMAINDER_REFL=" + support::cpp11::to_string((pad_x_before_remainder + is_reflect) % vec_size));
161 build_opts.add_option("-DPAD_X_AFTER_REMAINDER_REFL=" + support::cpp11::to_string((pad_x_after_remainder - is_reflect) % vec_size));
162 build_opts.add_option("-DAFTER_PAD_FACT_X=" + support::cpp11::to_string(after_pad_fact_x));
163 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));
164
165 break;
166 }
167 default:
168 ARM_COMPUTE_ERROR("Padding mode not supported.");
169 }
170
171 // Create kernel
172 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
173
174 // Configure window
175 Window win = calculate_max_window(*output->info(), Steps(vec_size));
176 ICLKernel::configure_internal(win);
177
178 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
179 }
180
validate(const ITensorInfo * input,const ITensorInfo * output,const PaddingList & padding,PixelValue constant_value,PaddingMode mode)181 Status CLPadLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
182 {
183 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, padding, constant_value, mode));
184 return Status{};
185 }
186
run(const Window & window,cl::CommandQueue & queue)187 void CLPadLayerKernel::run(const Window &window, cl::CommandQueue &queue)
188 {
189 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
190 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
191
192 Window slice = window.first_slice_window_3D();
193 unsigned int batch = 0;
194 do
195 {
196 unsigned int idx = 0;
197 add_3D_tensor_argument(idx, _input, slice);
198 add_3D_tensor_argument(idx, _output, slice);
199 if(_4d_enabled)
200 {
201 add_argument<unsigned int>(idx, batch++);
202 }
203
204 enqueue(queue, *this, slice, lws_hint());
205 }
206 while(window.slide_window_slice_3D(slice));
207 }
208 } // namespace arm_compute
209