blob: ccad92a685458dac4929fa3f2b60b75b19542130 [file] [log] [blame]
giuros01ba368252019-02-19 13:53:10 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2019-2020 Arm Limited.
giuros01ba368252019-02-19 13:53:10 +00003 *
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/NEON/kernels/NESpaceToBatchLayerKernel.h"
25
26#include "arm_compute/core/Helpers.h"
27#include "arm_compute/core/ITensor.h"
giuros01ba368252019-02-19 13:53:10 +000028#include "arm_compute/core/Types.h"
29#include "arm_compute/core/Validate.h"
30#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Georgios Pinitasddb93bb2020-10-02 16:38:59 +010031#include "src/core/NEON/wrapper/wrapper.h"
giuros01ba368252019-02-19 13:53:10 +000032#include <arm_neon.h>
33#include <cstdint>
34
35using namespace arm_compute::misc::shape_calculator;
36
37namespace arm_compute
38{
39namespace
40{
41Status validate_arguments(const ITensorInfo *input, const ITensorInfo *block_info, const ITensorInfo *padddings, const ITensorInfo *output)
42{
43 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, block_info, padddings, output);
Georgios Pinitas33843562019-12-10 13:33:18 +000044 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
giuros01ba368252019-02-19 13:53:10 +000045 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(block_info, 1, DataType::S32);
46 ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4);
47 ARM_COMPUTE_RETURN_ERROR_ON(block_info->num_dimensions() > 1);
48 ARM_COMPUTE_RETURN_ERROR_ON(padddings->num_dimensions() > 2);
49 ARM_COMPUTE_RETURN_ERROR_ON(padddings->tensor_shape()[1] != block_info->tensor_shape()[0]);
50
51 // Validate output if initialized
52 if(output->total_size() != 0)
53 {
54 const DataLayout data_layout = input->data_layout();
55 const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
56 ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[idx_channel] != output->tensor_shape()[idx_channel]);
57 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
58 }
59
60 return Status{};
61}
62Status validate_arguments_static(const ITensorInfo *input, const int block_shape_x, const int block_shape_y, const Size2D &padding_left, const Size2D &padding_right,
63 const ITensorInfo *output)
64{
65 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Georgios Pinitas33843562019-12-10 13:33:18 +000066 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
giuros01ba368252019-02-19 13:53:10 +000067 ARM_COMPUTE_RETURN_ERROR_ON(block_shape_x < 1 || block_shape_y < 1);
68 ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4);
69
70 // Validate output if initialized
71 if(output->total_size() != 0)
72 {
73 const DataLayout data_layout = input->data_layout();
74 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
75 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
76 const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
77 const int idx_batch = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
78 ARM_COMPUTE_RETURN_ERROR_ON(output->tensor_shape()[idx_width] < padding_left.x() + padding_right.y());
79 ARM_COMPUTE_RETURN_ERROR_ON((input->tensor_shape()[idx_width] + padding_left.x() + padding_right.x()) % block_shape_x != 0);
80 ARM_COMPUTE_RETURN_ERROR_ON((input->tensor_shape()[idx_height] + padding_left.y() + padding_right.y()) % block_shape_y != 0);
81 ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[idx_channel] != output->tensor_shape()[idx_channel]);
82 ARM_COMPUTE_RETURN_ERROR_ON(output->tensor_shape()[idx_batch] % (block_shape_x * block_shape_y) != 0);
83 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Michele Di Giorgio93c70b82019-08-08 11:59:14 +010084 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
giuros01ba368252019-02-19 13:53:10 +000085 }
86
87 return Status{};
88}
89} // namespace
90
91NESpaceToBatchLayerKernel::NESpaceToBatchLayerKernel()
Sadik Armagan29658042020-05-11 10:35:08 +010092 : _input(nullptr), _block_shape(nullptr), _paddings(nullptr), _output(nullptr), _data_layout(DataLayout::UNKNOWN), _padding_left(), _block_shape_x(), _block_shape_y()
giuros01ba368252019-02-19 13:53:10 +000093{
94}
95
96void NESpaceToBatchLayerKernel::configure(const ITensor *input, const ITensor *block_shape, const ITensor *paddings, ITensor *output)
97{
98 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
99 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), block_shape->info(), paddings->info(), output->info()));
100
101 _input = input;
102 _block_shape = block_shape;
103 _paddings = paddings;
104 _output = output;
Sadik Armagan29658042020-05-11 10:35:08 +0100105 _data_layout = input->info()->data_layout();
giuros01ba368252019-02-19 13:53:10 +0000106
107 // Configure kernel window
108 Window win = calculate_max_window(*output->info(), Steps());
109 ICPPKernel::configure(win);
110}
111
112void NESpaceToBatchLayerKernel::configure(const ITensor *input, const int block_shape_x, const int block_shape_y, const Size2D &padding_left, const Size2D &padding_right,
113 ITensor *output)
114{
115 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
116
117 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 +0100118 auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(), input->info()->quantization_info());
giuros01ba368252019-02-19 13:53:10 +0000119
120 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_static(input->info(), block_shape_x, block_shape_y, padding_left, padding_right, output->info()));
121
122 _input = input;
123 _output = output;
124 _block_shape_x = block_shape_x;
125 _block_shape_y = block_shape_y;
126 _padding_left = padding_left;
Sadik Armagan29658042020-05-11 10:35:08 +0100127 _data_layout = input->info()->data_layout();
giuros01ba368252019-02-19 13:53:10 +0000128
129 // Configure kernel window
130 Window win = calculate_max_window(*output->info(), Steps());
131 INEKernel::configure(win);
132}
133
134Status NESpaceToBatchLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *block_shape, const ITensorInfo *paddings, const ITensorInfo *output)
135{
136 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, block_shape, paddings, output));
137 return Status{};
138}
139Status NESpaceToBatchLayerKernel::validate(const ITensorInfo *input, const int block_shape_x, const int block_shape_y, const Size2D &padding_left, const Size2D &padding_right,
140 const ITensorInfo *output)
141{
142 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_static(input, block_shape_x, block_shape_y, padding_left, padding_right, output));
143 return Status{};
144}
145
146void NESpaceToBatchLayerKernel::run(const Window &window, const ThreadInfo &info)
147{
148 ARM_COMPUTE_UNUSED(info);
149 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
150 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICPPKernel::window(), window);
151
152 if(_block_shape != nullptr)
153 {
154 // Retrieve the block shapes dynamically
155 _block_shape_x = *(reinterpret_cast<const int *>(_block_shape->ptr_to_element(0)));
156 _block_shape_y = *(reinterpret_cast<const int *>(_block_shape->ptr_to_element(1)));
157 }
158
159 if(_paddings != nullptr)
160 {
161 const size_t pad_left_x = *reinterpret_cast<const size_t *>(_paddings->ptr_to_element({ 0, 0 }));
162 const size_t pad_left_y = *reinterpret_cast<const size_t *>(_paddings->ptr_to_element({ 1, 0 }));
163 _padding_left = Size2D(pad_left_x, pad_left_y);
164 }
Sadik Armagan29658042020-05-11 10:35:08 +0100165 const int height_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
166 const int width_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
167 const int batch_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::BATCHES);
168 const int element_size = _input->info()->element_size();
giuros01ba368252019-02-19 13:53:10 +0000169
170 const size_t height = _input->info()->dimension(height_idx);
171 const size_t width = _input->info()->dimension(width_idx);
Sadik Armagan29658042020-05-11 10:35:08 +0100172 const size_t batch_size = _input->info()->dimension(batch_idx);
giuros01ba368252019-02-19 13:53:10 +0000173
174 Window slice_out = window.first_slice_window_3D();
giuros01ba368252019-02-19 13:53:10 +0000175
176 int batch_id = 0;
177
178 // Main loop for NCHW and NHWC
Sadik Armagan29658042020-05-11 10:35:08 +0100179 if(_data_layout == DataLayout::NCHW)
giuros01ba368252019-02-19 13:53:10 +0000180 {
181 do
182 {
183 Iterator out(_output, slice_out);
184 execute_window_loop(slice_out, [&](const Coordinates & id)
185 {
186 const size_t out_x = id.x();
187 const size_t out_y = id.y();
188 const size_t z = id.z();
189 const size_t pos_x = out_x * _block_shape_x + (batch_id / batch_size) % _block_shape_x;
190 const size_t pos_y = out_y * _block_shape_y + (batch_id / batch_size) / _block_shape_x;
191 if(pos_y >= _padding_left.y() && pos_y < _padding_left.y() + height && pos_x >= _padding_left.x() && pos_x < _padding_left.x() + width)
192 {
193 const int w = batch_id % batch_size;
194 const int in_x = pos_x - _padding_left.x();
195 const int in_y = pos_y - _padding_left.y();
196 Coordinates input_coords{ in_x, in_y, z, w };
197 memcpy(out.ptr(), _input->ptr_to_element(input_coords), element_size);
198 }
199 },
200 out);
201 ++batch_id;
202 }
203 while(window.slide_window_slice_3D(slice_out));
204 }
205 else
206 {
207 do
208 {
209 Iterator out(_output, slice_out);
210 execute_window_loop(slice_out, [&](const Coordinates & id)
211 {
212 const size_t out_x = id.y();
213 const size_t out_y = id.z();
214 const size_t z = id.x();
215 const size_t pos_x = out_x * _block_shape_x + (batch_id / batch_size) % _block_shape_x;
216 const size_t pos_y = out_y * _block_shape_y + (batch_id / batch_size) / _block_shape_x;
217 if(pos_y >= _padding_left.y() && pos_y < _padding_left.y() + height && pos_x >= _padding_left.x() && pos_x < _padding_left.x() + width)
218 {
219 const int w = batch_id % batch_size;
220 const int in_x = pos_x - _padding_left.x();
221 const int in_y = pos_y - _padding_left.y();
222 Coordinates input_coords{ z, in_x, in_y, w };
223 memcpy(out.ptr(), _input->ptr_to_element(input_coords), element_size);
224 }
225 },
226 out);
227 ++batch_id;
228 }
229 while(window.slide_window_slice_3D(slice_out));
230 }
231}
232} // namespace arm_compute