blob: c6f70c3c095feb7718586c79bc7af20383c03498 [file] [log] [blame]
Michalis Spyrou16934a52018-08-21 18:03:58 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
Michalis Spyrou16934a52018-08-21 18:03:58 +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/CLSpaceToBatchLayerKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
Michalis Spyrou16934a52018-08-21 18:03:58 +010027#include "arm_compute/core/CL/ICLTensor.h"
28#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010029#include "src/core/CL/CLValidate.h"
30#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"
Michalis Spyrou16934a52018-08-21 18:03:58 +010033
34using namespace arm_compute::misc::shape_calculator;
35
36namespace arm_compute
37{
38namespace
39{
40Status validate_arguments(const ITensorInfo *input, const ITensorInfo *block_info, const ITensorInfo *padddings, const ITensorInfo *output)
41{
42 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, block_info, padddings, output);
43 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(block_info, 1, DataType::S32);
Manuel Bottini8481d832019-12-10 15:28:40 +000044 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Michalis Spyrou16934a52018-08-21 18:03:58 +010045 ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4);
Isabella Gottardicc6129c2018-12-14 11:40:40 +000046 ARM_COMPUTE_RETURN_ERROR_ON(block_info->num_dimensions() > 1);
47 ARM_COMPUTE_RETURN_ERROR_ON(padddings->num_dimensions() > 2);
48 ARM_COMPUTE_RETURN_ERROR_ON(padddings->tensor_shape()[1] != block_info->tensor_shape()[0]);
Michalis Spyrou16934a52018-08-21 18:03:58 +010049
50 // Validate output if initialized
51 if(output->total_size() != 0)
52 {
Isabella Gottardicc6129c2018-12-14 11:40:40 +000053 const DataLayout data_layout = input->data_layout();
54 const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
55 ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[idx_channel] != output->tensor_shape()[idx_channel]);
Michalis Spyrou16934a52018-08-21 18:03:58 +010056 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
57 }
58
59 return Status{};
60}
61Status validate_arguments_static(const ITensorInfo *input, const int block_shape_x, const int block_shape_y, const Size2D &padding_left, const Size2D &padding_right,
62 const ITensorInfo *output)
63{
64 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
65 ARM_COMPUTE_RETURN_ERROR_ON(block_shape_x < 1 || block_shape_y < 1);
66 ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4);
67
68 // Validate output if initialized
69 if(output->total_size() != 0)
70 {
Michalis Spyrou13a51e12018-09-18 13:09:30 +010071 const DataLayout data_layout = input->data_layout();
72 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
73 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
74 const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
75 const int idx_batch = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
76 ARM_COMPUTE_RETURN_ERROR_ON(output->tensor_shape()[idx_width] < padding_left.x() + padding_right.y());
Isabella Gottardicc6129c2018-12-14 11:40:40 +000077 ARM_COMPUTE_RETURN_ERROR_ON((input->tensor_shape()[idx_width] + padding_left.x() + padding_right.x()) % block_shape_x != 0);
78 ARM_COMPUTE_RETURN_ERROR_ON((input->tensor_shape()[idx_height] + padding_left.y() + padding_right.y()) % block_shape_y != 0);
Michalis Spyrou13a51e12018-09-18 13:09:30 +010079 ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[idx_channel] != output->tensor_shape()[idx_channel]);
80 ARM_COMPUTE_RETURN_ERROR_ON(output->tensor_shape()[idx_batch] % (block_shape_x * block_shape_y) != 0);
Michalis Spyrou16934a52018-08-21 18:03:58 +010081 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Michele Di Giorgio93c70b82019-08-08 11:59:14 +010082 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Michalis Spyrou16934a52018-08-21 18:03:58 +010083 }
84
85 return Status{};
86}
87} // namespace
88
89CLSpaceToBatchLayerKernel::CLSpaceToBatchLayerKernel()
90 : _input(nullptr), _block_shape(nullptr), _paddings(nullptr), _output(nullptr)
91{
92}
93
94void CLSpaceToBatchLayerKernel::configure(const ICLTensor *input, const ICLTensor *block_shape, const ICLTensor *paddings, ICLTensor *output)
95{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010096 configure(CLKernelLibrary::get().get_compile_context(), input, block_shape, paddings, output);
97}
98
Manuel Bottini2803f702020-04-21 16:20:03 +010099void CLSpaceToBatchLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *block_shape, const ICLTensor *paddings, ICLTensor *output)
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100100{
Michalis Spyrou16934a52018-08-21 18:03:58 +0100101 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
102 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), block_shape->info(), paddings->info(), output->info()));
103
104 _input = input;
105 _block_shape = block_shape;
106 _paddings = paddings;
107 _output = output;
108
Michalis Spyrou13a51e12018-09-18 13:09:30 +0100109 const DataLayout data_layout = input->info()->data_layout();
110 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
111 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
112 const int idx_batch = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
113
Michalis Spyrou16934a52018-08-21 18:03:58 +0100114 // Create kernel
115 CLBuildOptions build_opts;
Sheri Zhang0de45d02020-04-17 14:59:13 +0100116 build_opts.add_option("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(data_size_from_type(input->info()->data_type())));
Michalis Spyrou13a51e12018-09-18 13:09:30 +0100117 build_opts.add_option("-DWIDTH_OUT=" + support::cpp11::to_string(output->info()->dimension(idx_width)));
118 build_opts.add_option("-DHEIGHT_OUT=" + support::cpp11::to_string(output->info()->dimension(idx_height)));
119 build_opts.add_option("-DBATCH_SIZE=" + support::cpp11::to_string(output->info()->dimension(idx_batch)));
Isabella Gottardicc6129c2018-12-14 11:40:40 +0000120 build_opts.add_option("-DWIDTH_IN=" + support::cpp11::to_string(input->info()->dimension(idx_width)));
121 build_opts.add_option("-DHEIGHT_IN=" + support::cpp11::to_string(input->info()->dimension(idx_height)));
122 build_opts.add_option("-DBATCH_IN=" + support::cpp11::to_string(input->info()->dimension(idx_batch)));
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100123 _kernel = create_kernel(compile_context, "space_to_batch_" + lower_string(string_from_data_layout(input->info()->data_layout())), build_opts.options());
Michalis Spyrou16934a52018-08-21 18:03:58 +0100124
125 // Configure kernel window
126 Window win = calculate_max_window(*output->info(), Steps());
127 ICLKernel::configure_internal(win);
128}
129
130void CLSpaceToBatchLayerKernel::configure(const ICLTensor *input, const int block_shape_x, const int block_shape_y, const Size2D &padding_left, const Size2D &padding_right,
131 ICLTensor *output)
132{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100133 configure(CLKernelLibrary::get().get_compile_context(), input, block_shape_x, block_shape_y, padding_left, padding_right, output);
134}
135
Manuel Bottini2803f702020-04-21 16:20:03 +0100136void CLSpaceToBatchLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, const int block_shape_x, const int block_shape_y, const Size2D &padding_left,
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100137 const Size2D &padding_right,
138 ICLTensor *output)
139{
Michalis Spyrou16934a52018-08-21 18:03:58 +0100140 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
141
142 TensorShape output_shape = misc::shape_calculator::compute_space_to_batch_shape(input->info(), block_shape_x, block_shape_y, padding_left, padding_right);
Michele Di Giorgio93c70b82019-08-08 11:59:14 +0100143 auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(), input->info()->quantization_info());
Michalis Spyrou16934a52018-08-21 18:03:58 +0100144
145 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_static(input->info(), block_shape_x, block_shape_y, padding_left, padding_right, output->info()));
146
147 _input = input;
148 _output = output;
149
Michalis Spyrou13a51e12018-09-18 13:09:30 +0100150 const DataLayout data_layout = input->info()->data_layout();
151 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
152 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
153 const int idx_batch = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
154
Michalis Spyrou16934a52018-08-21 18:03:58 +0100155 // Create kernel
156 CLBuildOptions build_opts;
Sheri Zhang0de45d02020-04-17 14:59:13 +0100157 build_opts.add_option("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(data_size_from_type(input->info()->data_type())));
Michalis Spyrou13a51e12018-09-18 13:09:30 +0100158 build_opts.add_option("-DWIDTH_OUT=" + support::cpp11::to_string(output->info()->dimension(idx_width)));
159 build_opts.add_option("-DHEIGHT_OUT=" + support::cpp11::to_string(output->info()->dimension(idx_height)));
160 build_opts.add_option("-DBATCH_SIZE=" + support::cpp11::to_string(output->info()->dimension(idx_batch)));
Isabella Gottardicc6129c2018-12-14 11:40:40 +0000161 build_opts.add_option("-DWIDTH_IN=" + support::cpp11::to_string(input->info()->dimension(idx_width)));
162 build_opts.add_option("-DHEIGHT_IN=" + support::cpp11::to_string(input->info()->dimension(idx_height)));
163 build_opts.add_option("-DBATCH_IN=" + support::cpp11::to_string(input->info()->dimension(idx_batch)));
Michalis Spyrou16934a52018-08-21 18:03:58 +0100164 build_opts.add_option("-DBLOCK_SHAPE_X=" + support::cpp11::to_string(block_shape_x));
165 build_opts.add_option("-DBLOCK_SHAPE_Y=" + support::cpp11::to_string(block_shape_y));
Michalis Spyrouedf26ea2018-11-21 14:17:42 +0000166 build_opts.add_option("-DPAD_LEFT_X=" + support::cpp11::to_string(padding_left.x()));
167 build_opts.add_option("-DPAD_RIGHT_X=" + support::cpp11::to_string(padding_right.x()));
168 build_opts.add_option("-DPAD_LEFT_Y=" + support::cpp11::to_string(padding_left.y()));
169 build_opts.add_option("-DPAD_RIGHT_Y=" + support::cpp11::to_string(padding_right.y()));
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100170 _kernel = create_kernel(compile_context, "space_to_batch_static_" + lower_string(string_from_data_layout(input->info()->data_layout())), build_opts.options());
Michalis Spyrou16934a52018-08-21 18:03:58 +0100171
172 // Configure kernel window
173 Window win = calculate_max_window(*output->info(), Steps());
174 ICLKernel::configure_internal(win);
175}
176
177Status CLSpaceToBatchLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *block_shape, const ITensorInfo *paddings, const ITensorInfo *output)
178{
179 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, block_shape, paddings, output));
180 return Status{};
181}
182Status CLSpaceToBatchLayerKernel::validate(const ITensorInfo *input, const int block_shape_x, const int block_shape_y, const Size2D &padding_left, const Size2D &padding_right,
183 const ITensorInfo *output)
184{
185 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_static(input, block_shape_x, block_shape_y, padding_left, padding_right, output));
186 return Status{};
187}
188
189void CLSpaceToBatchLayerKernel::run(const Window &window, cl::CommandQueue &queue)
190{
191 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
192 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
193
194 Window slice_out = window.first_slice_window_3D();
195
196 Window slice_in = window.first_slice_window_4D();
197 slice_in.set(Window::DimX, Window::Dimension(0, 0, 0));
198 slice_in.set(Window::DimY, Window::Dimension(0, 0, 0));
199 slice_in.set(Window::DimZ, Window::Dimension(0, 0, 0));
200 slice_in.set(3, Window::Dimension(0, 0, 0));
201
202 Window vector_slice = window.first_slice_window_1D();
203 vector_slice.set(Window::DimX, Window::Dimension(0, 0, 0));
204
205 Window padding_slice = window.first_slice_window_2D();
206 padding_slice.set(Window::DimX, Window::Dimension(0, 0, 0));
207 padding_slice.set(Window::DimY, Window::Dimension(0, 0, 0));
208
209 int batch_id = 0;
210 do
211 {
Michalis Spyroue1651a52019-07-11 15:00:49 +0100212 unsigned int idx = 0;
213 const bool cond = (_paddings != nullptr && _block_shape != nullptr);
Michalis Spyrou16934a52018-08-21 18:03:58 +0100214 add_4D_tensor_argument(idx, _input, slice_in);
Michalis Spyroue1651a52019-07-11 15:00:49 +0100215 add_2D_tensor_argument_if(cond, idx, _paddings, padding_slice);
216 add_1D_tensor_argument_if(cond, idx, _block_shape, vector_slice);
217
Michalis Spyrou16934a52018-08-21 18:03:58 +0100218 add_argument(idx, batch_id);
219 add_3D_tensor_argument(idx, _output, slice_out);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100220 enqueue(queue, *this, slice_out, lws_hint());
Michalis Spyrou16934a52018-08-21 18:03:58 +0100221 ++batch_id;
222 }
223 while(window.slide_window_slice_3D(slice_out));
224}
Michele Di Giorgio93c70b82019-08-08 11:59:14 +0100225} // namespace arm_compute