blob: 511a109777623ae576b6b0bcf36b38eeb6b78134 [file] [log] [blame]
giuros01ba368252019-02-19 13:53:10 +00001/*
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/NEON/kernels/NESpaceToBatchLayerKernel.h"
25
26#include "arm_compute/core/Helpers.h"
27#include "arm_compute/core/ITensor.h"
28#include "arm_compute/core/NEON/wrapper/wrapper.h"
29#include "arm_compute/core/Types.h"
30#include "arm_compute/core/Validate.h"
31#include "arm_compute/core/utils/misc/ShapeCalculator.h"
32#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);
44 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(block_info, 1, DataType::S32);
45 ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4);
46 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]);
49
50 // Validate output if initialized
51 if(output->total_size() != 0)
52 {
53 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]);
56 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 {
71 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());
77 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);
79 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);
81 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
82 }
83
84 return Status{};
85}
86} // namespace
87
88NESpaceToBatchLayerKernel::NESpaceToBatchLayerKernel()
89 : _input(nullptr), _block_shape(nullptr), _paddings(nullptr), _output(nullptr), _padding_left(), _block_shape_x(), _block_shape_y()
90{
91}
92
93void NESpaceToBatchLayerKernel::configure(const ITensor *input, const ITensor *block_shape, const ITensor *paddings, ITensor *output)
94{
95 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
96 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), block_shape->info(), paddings->info(), output->info()));
97
98 _input = input;
99 _block_shape = block_shape;
100 _paddings = paddings;
101 _output = output;
102
103 // Configure kernel window
104 Window win = calculate_max_window(*output->info(), Steps());
105 ICPPKernel::configure(win);
106}
107
108void NESpaceToBatchLayerKernel::configure(const ITensor *input, const int block_shape_x, const int block_shape_y, const Size2D &padding_left, const Size2D &padding_right,
109 ITensor *output)
110{
111 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
112
113 TensorShape output_shape = misc::shape_calculator::compute_space_to_batch_shape(input->info(), block_shape_x, block_shape_y, padding_left, padding_right);
114 auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type());
115
116 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_static(input->info(), block_shape_x, block_shape_y, padding_left, padding_right, output->info()));
117
118 _input = input;
119 _output = output;
120 _block_shape_x = block_shape_x;
121 _block_shape_y = block_shape_y;
122 _padding_left = padding_left;
123
124 // Configure kernel window
125 Window win = calculate_max_window(*output->info(), Steps());
126 INEKernel::configure(win);
127}
128
129Status NESpaceToBatchLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *block_shape, const ITensorInfo *paddings, const ITensorInfo *output)
130{
131 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, block_shape, paddings, output));
132 return Status{};
133}
134Status NESpaceToBatchLayerKernel::validate(const ITensorInfo *input, const int block_shape_x, const int block_shape_y, const Size2D &padding_left, const Size2D &padding_right,
135 const ITensorInfo *output)
136{
137 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_static(input, block_shape_x, block_shape_y, padding_left, padding_right, output));
138 return Status{};
139}
140
141void NESpaceToBatchLayerKernel::run(const Window &window, const ThreadInfo &info)
142{
143 ARM_COMPUTE_UNUSED(info);
144 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
145 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICPPKernel::window(), window);
146
147 if(_block_shape != nullptr)
148 {
149 // Retrieve the block shapes dynamically
150 _block_shape_x = *(reinterpret_cast<const int *>(_block_shape->ptr_to_element(0)));
151 _block_shape_y = *(reinterpret_cast<const int *>(_block_shape->ptr_to_element(1)));
152 }
153
154 if(_paddings != nullptr)
155 {
156 const size_t pad_left_x = *reinterpret_cast<const size_t *>(_paddings->ptr_to_element({ 0, 0 }));
157 const size_t pad_left_y = *reinterpret_cast<const size_t *>(_paddings->ptr_to_element({ 1, 0 }));
158 _padding_left = Size2D(pad_left_x, pad_left_y);
159 }
160 const DataLayout data_layout = _input->info()->data_layout();
161 const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
162 const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
163 const int element_size = _input->info()->element_size();
164
165 const size_t height = _input->info()->dimension(height_idx);
166 const size_t width = _input->info()->dimension(width_idx);
167 const size_t batch_size = _input->info()->dimension(3);
168
169 Window slice_out = window.first_slice_window_3D();
giuros01ba368252019-02-19 13:53:10 +0000170
171 int batch_id = 0;
172
173 // Main loop for NCHW and NHWC
174 if(_output->info()->data_layout() == DataLayout::NCHW)
175 {
176 do
177 {
178 Iterator out(_output, slice_out);
179 execute_window_loop(slice_out, [&](const Coordinates & id)
180 {
181 const size_t out_x = id.x();
182 const size_t out_y = id.y();
183 const size_t z = id.z();
184 const size_t pos_x = out_x * _block_shape_x + (batch_id / batch_size) % _block_shape_x;
185 const size_t pos_y = out_y * _block_shape_y + (batch_id / batch_size) / _block_shape_x;
186 if(pos_y >= _padding_left.y() && pos_y < _padding_left.y() + height && pos_x >= _padding_left.x() && pos_x < _padding_left.x() + width)
187 {
188 const int w = batch_id % batch_size;
189 const int in_x = pos_x - _padding_left.x();
190 const int in_y = pos_y - _padding_left.y();
191 Coordinates input_coords{ in_x, in_y, z, w };
192 memcpy(out.ptr(), _input->ptr_to_element(input_coords), element_size);
193 }
194 },
195 out);
196 ++batch_id;
197 }
198 while(window.slide_window_slice_3D(slice_out));
199 }
200 else
201 {
202 do
203 {
204 Iterator out(_output, slice_out);
205 execute_window_loop(slice_out, [&](const Coordinates & id)
206 {
207 const size_t out_x = id.y();
208 const size_t out_y = id.z();
209 const size_t z = id.x();
210 const size_t pos_x = out_x * _block_shape_x + (batch_id / batch_size) % _block_shape_x;
211 const size_t pos_y = out_y * _block_shape_y + (batch_id / batch_size) / _block_shape_x;
212 if(pos_y >= _padding_left.y() && pos_y < _padding_left.y() + height && pos_x >= _padding_left.x() && pos_x < _padding_left.x() + width)
213 {
214 const int w = batch_id % batch_size;
215 const int in_x = pos_x - _padding_left.x();
216 const int in_y = pos_y - _padding_left.y();
217 Coordinates input_coords{ z, in_x, in_y, w };
218 memcpy(out.ptr(), _input->ptr_to_element(input_coords), element_size);
219 }
220 },
221 out);
222 ++batch_id;
223 }
224 while(window.slide_window_slice_3D(slice_out));
225 }
226}
227} // namespace arm_compute