blob: d167cbb05d52b30d03c49686937441befcb1cd40 [file] [log] [blame]
giuros01fc1da132019-02-18 16:48:35 +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/NEBatchToSpaceLayerKernel.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 *output)
42{
43 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, block_info, 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);
Georgios Pinitas33843562019-12-10 13:33:18 +000046 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
giuros01fc1da132019-02-18 16:48:35 +000047
48 // Validate output if initialized
49 if(output->total_size() != 0)
50 {
51 ARM_COMPUTE_RETURN_ERROR_ON(output->num_dimensions() > 4);
52 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
53 }
54
55 return Status{};
56}
57Status validate_arguments_static(const ITensorInfo *input, const int block_shape_x, const int block_shape_y, const ITensorInfo *output)
58{
59 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
60 ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4);
61 ARM_COMPUTE_RETURN_ERROR_ON(block_shape_x <= 0);
62 ARM_COMPUTE_RETURN_ERROR_ON(block_shape_y <= 0);
63
64 const DataLayout data_layout = input->data_layout();
65 const int idx_batch = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
66 ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[idx_batch] % (block_shape_x * block_shape_y) != 0);
giuros01fc1da132019-02-18 16:48:35 +000067 // Validate output if initialized
68 if(output->total_size() != 0)
69 {
70 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
71 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
72 const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
73 ARM_COMPUTE_RETURN_ERROR_ON(output->tensor_shape()[idx_width] != (block_shape_x * input->tensor_shape()[idx_width]));
Pablo Tello0a33a212019-04-23 12:06:19 +010074 ARM_COMPUTE_RETURN_ERROR_ON(output->tensor_shape()[idx_height] != (block_shape_y * input->tensor_shape()[idx_height]));
giuros01fc1da132019-02-18 16:48:35 +000075 ARM_COMPUTE_RETURN_ERROR_ON(output->tensor_shape()[idx_channel] != input->tensor_shape()[idx_channel]);
76 ARM_COMPUTE_RETURN_ERROR_ON(output->num_dimensions() > 4);
77 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
78 }
79
80 return Status{};
81}
82} // namespace
83
84NEBatchToSpaceLayerKernel::NEBatchToSpaceLayerKernel()
85 : _input(nullptr), _block_shape(nullptr), _output(nullptr), _block_shape_x(), _block_shape_y()
86{
87}
88
89void NEBatchToSpaceLayerKernel::configure(const ITensor *input, const ITensor *block_shape, ITensor *output)
90{
91 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
92 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), block_shape->info(), output->info()));
93
94 _input = input;
95 _block_shape = block_shape;
96 _output = output;
97
98 // Configure kernel window
99 Window win = calculate_max_window(*input->info(), Steps());
100 ICPPKernel::configure(win);
101}
102
103void NEBatchToSpaceLayerKernel::configure(const ITensor *input, const int32_t block_shape_x, const int32_t block_shape_y, ITensor *output)
104{
105 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
106 TensorShape output_shape = compute_batch_to_space_shape(input->info(), block_shape_x, block_shape_y);
107 // Output auto inizialitation if not yet initialized
108 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
109
110 // Perform validation step
111 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_static(input->info(), block_shape_x, block_shape_y, output->info()));
112
113 _input = input;
114 _output = output;
115 _block_shape_x = block_shape_x;
116 _block_shape_y = block_shape_y;
117
118 // Configure kernel window
119 Window win = calculate_max_window(*input->info(), Steps());
120 ICPPKernel::configure(win);
121}
122
123Status NEBatchToSpaceLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *block_shape, const ITensorInfo *output)
124{
125 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, block_shape, output);
126 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, block_shape, output));
127 return Status{};
128}
129
130Status NEBatchToSpaceLayerKernel::validate(const ITensorInfo *input, const int32_t block_shape_x, const int32_t block_shape_y, const ITensorInfo *output)
131{
132 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
133 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_static(input, block_shape_x, block_shape_y, output));
134 return Status{};
135}
136
137void NEBatchToSpaceLayerKernel::run(const Window &window, const ThreadInfo &info)
138{
139 ARM_COMPUTE_UNUSED(info);
140 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
141 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICPPKernel::window(), window);
142
143 if(_block_shape != nullptr)
144 {
145 // Retrieve the block shapes dynamically
146 _block_shape_x = *(reinterpret_cast<const int *>(_block_shape->ptr_to_element(0)));
147 _block_shape_y = *(reinterpret_cast<const int *>(_block_shape->ptr_to_element(1)));
148 }
149
150 const int batch_size = _input->info()->dimension(3);
151 const int r = (batch_size / (_block_shape_x * _block_shape_y));
152 const int element_size = _input->info()->element_size();
153
154 Window slice_in = window.first_slice_window_3D();
155 Window slice_out = window.first_slice_window_4D();
156
157 // The slice_out slice does not move
158 slice_out.set(Window::DimX, Window::Dimension(0, 0, 0));
159 slice_out.set(Window::DimY, Window::Dimension(0, 0, 0));
160 slice_out.set(Window::DimZ, Window::Dimension(0, 0, 0));
161 slice_out.set(3, Window::Dimension(0, 0, 0));
162
163 int batch_id = 0;
164 // Main loop for NCHW and NHWC
165 if(_input->info()->data_layout() == DataLayout::NCHW)
166 {
167 do
168 {
169 Iterator in(_input, slice_in);
170 execute_window_loop(slice_in, [&](const Coordinates & id)
171 {
172
173 const int x = id.x();
174 const int y = id.y();
175 const int z = id.z();
176
177 const int w = batch_id % r;
178 const int out_x = x * _block_shape_x + (batch_id / r) % _block_shape_x;
179 const int out_y = y * _block_shape_y + (batch_id / r) / _block_shape_x;
180 Coordinates output_coords{ out_x, out_y, z, w };
181 memcpy(_output->ptr_to_element(output_coords), in.ptr(), element_size);
182 },
183 in);
184 ++batch_id;
185 }
186 while(window.slide_window_slice_3D(slice_in));
187 }
188 else
189 {
190 do
191 {
192 Iterator in(_input, slice_in);
193 execute_window_loop(slice_in, [&](const Coordinates & id)
194 {
195
196 const int z = id.x();
197 const int x = id.y();
198 const int y = id.z();
199
200 const int w = batch_id % r;
201 const int out_x = x * _block_shape_x + (batch_id / r) % _block_shape_x;
202 const int out_y = y * _block_shape_y + (batch_id / r) / _block_shape_x;
203 Coordinates output_coords{ z, out_x, out_y, w };
204 memcpy(_output->ptr_to_element(output_coords), in.ptr(), element_size);
205 },
206 in);
207 ++batch_id;
208 }
209 while(window.slide_window_slice_3D(slice_in));
210 }
211}
212} // namespace arm_compute