blob: 9dfd380f7ca08c066e238cf9b8243f9f1525148f [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"
Michele Di Giorgio4c268b92019-09-20 14:01:48 +010027#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Giorgio Arena205eed82019-08-14 10:13:50 +010028
29namespace arm_compute
30{
31namespace
32{
33Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
34{
35 ARM_COMPUTE_UNUSED(input, output, constant_value);
36 ARM_COMPUTE_RETURN_ERROR_ON(padding.empty());
37 ARM_COMPUTE_RETURN_ERROR_ON_MSG(mode != PaddingMode::CONSTANT, "Only CONSTANT mode supported.");
38
39 return Status{};
40}
41
42std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
43{
44 ARM_COMPUTE_UNUSED(constant_value, mode);
Michele Di Giorgio4c268b92019-09-20 14:01:48 +010045 // Output auto initialization if not yet initialized
46 const TensorShape expected_output_shape = arm_compute::misc::shape_calculator::compute_padded_shape(input->tensor_shape(), padding);
47 auto_init_if_empty(*output, input->clone()->set_tensor_shape(expected_output_shape));
48
Giorgio Arena205eed82019-08-14 10:13:50 +010049 const unsigned int num_elems_processed_per_iteration = std::min(16U, 32U / static_cast<unsigned int>(element_size_from_data_type(input->data_type())));
50
51 // Configure kernel window
52 Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
53
54 const int input_start_x = -(padding.at(0).first % num_elems_processed_per_iteration);
55 const int input_start_y = padding.size() > 1 ? -padding.at(1).first : 0;
56
57 AccessWindowRectangle input_access(input, input_start_x, input_start_y, num_elems_processed_per_iteration, 1);
58 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
59
60 const bool window_changed = update_window_and_padding(win, input_access, output_access);
61 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
62
63 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
64 return std::make_pair(err, win);
65}
66} // namespace
67
68CLPadLayerKernel::CLPadLayerKernel()
69 : _input(nullptr), _output(nullptr), _input_start_x(0), _input_start_y(0)
70{
71}
72
73void CLPadLayerKernel::configure(const ICLTensor *input, ICLTensor *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
74{
Michele Di Giorgio4c268b92019-09-20 14:01:48 +010075 // Perform validation step
Giorgio Arena205eed82019-08-14 10:13:50 +010076 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Giorgio Arena205eed82019-08-14 10:13:50 +010077 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), padding, constant_value, mode));
78
79 _input = input;
80 _output = output;
81
82 // Set build options
83 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())));
84 _input_start_x = -(padding.at(0).first % num_elems_processed_per_iteration);
85 _input_start_y = padding.size() > 1 ? -padding.at(1).first : 0;
86
87 CLBuildOptions build_opts;
88 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
89 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration));
90 build_opts.add_option("-DCONST_VAL=" + string_from_pixel_value(constant_value, input->info()->data_type()));
91 build_opts.add_option("-DPAD_LEFT=" + support::cpp11::to_string(padding.at(0).first));
92 build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(input->info()->dimension(0)));
93 build_opts.add_option("-DSELECT_DT=" + get_cl_select_type_from_data_type(input->info()->data_type()));
94 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));
95
96 if(padding.size() > 1)
97 {
98 build_opts.add_option("-DPAD_TOP=" + support::cpp11::to_string(padding.at(1).first));
99 build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(input->info()->dimension(1)));
100
101 if(padding.size() > 2)
102 {
103 build_opts.add_option("-DPAD_NEAR=" + support::cpp11::to_string(padding.at(2).first));
104 build_opts.add_option("-DSRC_DEPTH=" + support::cpp11::to_string(input->info()->dimension(2)));
Matthew Jacksonc2a60592019-08-30 15:19:42 +0100105
106 if(padding.size() > 3)
107 {
108 build_opts.add_option("-DPAD_BTOP=" + support::cpp11::to_string(padding.at(3).first));
109 build_opts.add_option("-DSRC_BATCH=" + support::cpp11::to_string(input->info()->dimension(3)));
110 }
Giorgio Arena205eed82019-08-14 10:13:50 +0100111 }
112 }
113
114 // Create kernel
115 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("pad_layer", build_opts.options()));
116
117 // Configure kernel window
118 auto win_config = validate_and_configure_window(input->info(), output->info(), padding, constant_value, mode);
119 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
120 ICLKernel::configure_internal(win_config.second);
121}
122
123Status CLPadLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
124{
125 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, padding, constant_value, mode));
126 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), padding, constant_value, mode).first);
127
128 return Status{};
129}
130
131void CLPadLayerKernel::run(const Window &window, cl::CommandQueue &queue)
132{
133 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
134 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
135
136 Window win_in = window;
137 win_in.adjust(Window::DimX, _input_start_x, true);
138 win_in.adjust(Window::DimY, _input_start_y, true);
139
Michele Di Giorgio4c268b92019-09-20 14:01:48 +0100140 Window slice_out = window.first_slice_window_3D();
141 Window slice_in = win_in.first_slice_window_3D();
142 unsigned int batch = 0;
Giorgio Arena205eed82019-08-14 10:13:50 +0100143 do
144 {
145 unsigned int idx = 0;
146 add_3D_tensor_argument(idx, _input, slice_in);
147 add_3D_tensor_argument(idx, _output, slice_out);
Matthew Jacksonc2a60592019-08-30 15:19:42 +0100148 add_argument<unsigned int>(idx, batch++);
Giorgio Arena205eed82019-08-14 10:13:50 +0100149
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100150 enqueue(queue, *this, slice_out, lws_hint());
Giorgio Arena205eed82019-08-14 10:13:50 +0100151 }
152 while(window.slide_window_slice_3D(slice_out) && win_in.slide_window_slice_3D(slice_in));
153}
154} // namespace arm_compute