blob: 83fb5f6f511d1853032fbc215715cf9148614490 [file] [log] [blame]
giuros01fc1da132019-02-18 16:48:35 +00001/*
SiCong Li5a7d1572023-03-21 12:00:15 +00002 * Copyright (c) 2019-2020, 2023 Arm Limited.
giuros01fc1da132019-02-18 16:48:35 +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 */
Michalis Spyrouebcebf12020-10-21 00:04:14 +010024#include "src/core/NEON/kernels/NEBatchToSpaceLayerKernel.h"
giuros01fc1da132019-02-18 16:48:35 +000025
26#include "arm_compute/core/Helpers.h"
27#include "arm_compute/core/ITensor.h"
SiCong Li8893e452023-03-23 12:06:45 +000028#include "arm_compute/core/TensorInfo.h"
giuros01fc1da132019-02-18 16:48:35 +000029#include "arm_compute/core/Types.h"
30#include "arm_compute/core/Validate.h"
31#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010032#include "src/core/helpers/AutoConfiguration.h"
33#include "src/core/helpers/WindowHelpers.h"
giuros01fc1da132019-02-18 16:48:35 +000034
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}
SiCong Li8893e452023-03-23 12:06:45 +000057Status validate_arguments_static(const ITensorInfo *input, int block_shape_x, int block_shape_y, const ITensorInfo *output, const CropInfo &crop_info)
giuros01fc1da132019-02-18 16:48:35 +000058{
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 {
giuros01fc1da132019-02-18 16:48:35 +000070 ARM_COMPUTE_RETURN_ERROR_ON(output->num_dimensions() > 4);
71 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
SiCong Li8893e452023-03-23 12:06:45 +000072
73 const TensorShape expected_output_shape = compute_batch_to_space_shape(input->data_layout(), input->tensor_shape(), block_shape_x, block_shape_y, crop_info);
74 const TensorInfo expected_output = output->clone()->set_tensor_shape(expected_output_shape);
75 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &expected_output);
giuros01fc1da132019-02-18 16:48:35 +000076 }
77
78 return Status{};
79}
80} // namespace
81
82NEBatchToSpaceLayerKernel::NEBatchToSpaceLayerKernel()
SiCong Li8893e452023-03-23 12:06:45 +000083 : _input(nullptr), _block_shape(nullptr), _output(nullptr), _data_layout(DataLayout::UNKNOWN), _block_shape_x(), _block_shape_y(), _crop_info()
giuros01fc1da132019-02-18 16:48:35 +000084{
85}
86
87void NEBatchToSpaceLayerKernel::configure(const ITensor *input, const ITensor *block_shape, ITensor *output)
88{
89 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
90 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), block_shape->info(), output->info()));
91
92 _input = input;
93 _block_shape = block_shape;
94 _output = output;
Sadik Armagan29658042020-05-11 10:35:08 +010095 _data_layout = input->info()->data_layout();
giuros01fc1da132019-02-18 16:48:35 +000096
97 // Configure kernel window
SiCong Li8893e452023-03-23 12:06:45 +000098 Window win = calculate_max_window(*output->info(), Steps());
giuros01fc1da132019-02-18 16:48:35 +000099 ICPPKernel::configure(win);
100}
101
SiCong Li8893e452023-03-23 12:06:45 +0000102void NEBatchToSpaceLayerKernel::configure(const ITensor *input, int32_t block_shape_x, int32_t block_shape_y, ITensor *output, const CropInfo &crop_info)
giuros01fc1da132019-02-18 16:48:35 +0000103{
104 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
SiCong Li5a7d1572023-03-21 12:00:15 +0000105 const TensorShape output_shape = compute_batch_to_space_shape(input->info()->data_layout(), input->info()->tensor_shape(), block_shape_x, block_shape_y);
SiCong Li8893e452023-03-23 12:06:45 +0000106 // Output auto initialization if not yet initialized
giuros01fc1da132019-02-18 16:48:35 +0000107 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
108
109 // Perform validation step
SiCong Li8893e452023-03-23 12:06:45 +0000110 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_static(input->info(), block_shape_x, block_shape_y, output->info(), crop_info));
giuros01fc1da132019-02-18 16:48:35 +0000111
112 _input = input;
113 _output = output;
114 _block_shape_x = block_shape_x;
115 _block_shape_y = block_shape_y;
Sadik Armagan29658042020-05-11 10:35:08 +0100116 _data_layout = input->info()->data_layout();
SiCong Li8893e452023-03-23 12:06:45 +0000117 _crop_info = crop_info;
giuros01fc1da132019-02-18 16:48:35 +0000118
119 // Configure kernel window
SiCong Li8893e452023-03-23 12:06:45 +0000120 Window win = calculate_max_window(*output->info(), Steps());
giuros01fc1da132019-02-18 16:48:35 +0000121 ICPPKernel::configure(win);
122}
123
124Status NEBatchToSpaceLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *block_shape, const ITensorInfo *output)
125{
126 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, block_shape, output);
127 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, block_shape, output));
128 return Status{};
129}
130
SiCong Li8893e452023-03-23 12:06:45 +0000131Status NEBatchToSpaceLayerKernel::validate(const ITensorInfo *input, int32_t block_shape_x, int32_t block_shape_y, const ITensorInfo *output, const CropInfo &crop_info)
giuros01fc1da132019-02-18 16:48:35 +0000132{
133 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
SiCong Li8893e452023-03-23 12:06:45 +0000134 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_static(input, block_shape_x, block_shape_y, output, crop_info));
giuros01fc1da132019-02-18 16:48:35 +0000135 return Status{};
136}
137
138void NEBatchToSpaceLayerKernel::run(const Window &window, const ThreadInfo &info)
139{
140 ARM_COMPUTE_UNUSED(info);
141 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
142 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICPPKernel::window(), window);
143
144 if(_block_shape != nullptr)
145 {
146 // Retrieve the block shapes dynamically
147 _block_shape_x = *(reinterpret_cast<const int *>(_block_shape->ptr_to_element(0)));
148 _block_shape_y = *(reinterpret_cast<const int *>(_block_shape->ptr_to_element(1)));
149 }
150
SiCong Li8893e452023-03-23 12:06:45 +0000151 const int batch_size = _output->info()->dimension(3);
152 const int element_size = _output->info()->element_size();
giuros01fc1da132019-02-18 16:48:35 +0000153
SiCong Li8893e452023-03-23 12:06:45 +0000154 Window slice_out = window.first_slice_window_3D();
giuros01fc1da132019-02-18 16:48:35 +0000155
156 int batch_id = 0;
157 // Main loop for NCHW and NHWC
Sadik Armagan29658042020-05-11 10:35:08 +0100158 if(_data_layout == DataLayout::NCHW)
giuros01fc1da132019-02-18 16:48:35 +0000159 {
160 do
161 {
SiCong Li8893e452023-03-23 12:06:45 +0000162 Iterator out(_output, slice_out);
163 execute_window_loop(slice_out, [&](const Coordinates & id)
giuros01fc1da132019-02-18 16:48:35 +0000164 {
165
166 const int x = id.x();
167 const int y = id.y();
168 const int z = id.z();
SiCong Li8893e452023-03-23 12:06:45 +0000169 // Translate x, y to uncropped version
170 const int x_c = x + _crop_info.left;
171 const int y_c = y + _crop_info.top;
giuros01fc1da132019-02-18 16:48:35 +0000172
SiCong Li8893e452023-03-23 12:06:45 +0000173 const int in_batch = batch_id + ((x_c % _block_shape_x) + (y_c % _block_shape_y) * _block_shape_x) * batch_size;
174 const int in_x = x_c / _block_shape_x;
175 const int in_y = y_c / _block_shape_y;
176 Coordinates input_coords{ in_x, in_y, z, in_batch };
177 memcpy(out.ptr(), _input->ptr_to_element(input_coords), element_size);
giuros01fc1da132019-02-18 16:48:35 +0000178 },
SiCong Li8893e452023-03-23 12:06:45 +0000179 out);
giuros01fc1da132019-02-18 16:48:35 +0000180 ++batch_id;
181 }
SiCong Li8893e452023-03-23 12:06:45 +0000182 while(window.slide_window_slice_3D(slice_out));
giuros01fc1da132019-02-18 16:48:35 +0000183 }
184 else
185 {
SiCong Li8893e452023-03-23 12:06:45 +0000186 // For NHWC we can perform a block copy on the Channel (first) dimension. Thus we do not need to iterate over this dimension
187 slice_out.set(0U, Window::Dimension(0U, 1U, 1U));
giuros01fc1da132019-02-18 16:48:35 +0000188 do
189 {
SiCong Li8893e452023-03-23 12:06:45 +0000190 Iterator out(_output, slice_out);
191 execute_window_loop(slice_out, [&](const Coordinates & id)
giuros01fc1da132019-02-18 16:48:35 +0000192 {
193
giuros01fc1da132019-02-18 16:48:35 +0000194 const int x = id.y();
195 const int y = id.z();
196
SiCong Li8893e452023-03-23 12:06:45 +0000197 // Translate x, y to uncropped version
198 const int x_c = x + _crop_info.left;
199 const int y_c = y + _crop_info.top;
200
201 const int in_batch = batch_id + ((x_c % _block_shape_x) + (y_c % _block_shape_y) * _block_shape_x) * batch_size;
202 const int in_x = x_c / _block_shape_x;
203 const int in_y = y_c / _block_shape_y;
204 Coordinates input_coords{ 0, in_x, in_y, in_batch };
205 memcpy(out.ptr(), _input->ptr_to_element(input_coords), element_size * _input->info()->dimension(0));
giuros01fc1da132019-02-18 16:48:35 +0000206 },
SiCong Li8893e452023-03-23 12:06:45 +0000207 out);
giuros01fc1da132019-02-18 16:48:35 +0000208 ++batch_id;
209 }
SiCong Li8893e452023-03-23 12:06:45 +0000210 while(window.slide_window_slice_3D(slice_out));
giuros01fc1da132019-02-18 16:48:35 +0000211 }
212}
213} // namespace arm_compute