blob: 253566df0f93a0135e256b2c76bc4404c6944ae4 [file] [log] [blame]
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +00001/*
Georgios Pinitas0f7ef8a2021-01-10 04:23:52 +00002 * Copyright (c) 2018-2021 Arm Limited.
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +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/runtime/NEON/functions/NEPadLayer.h"
25
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +000026#include "arm_compute/core/Types.h"
27#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010028#include "arm_compute/runtime/NEON/NEScheduler.h"
29
ramelg01cbbb0382021-09-17 17:36:57 +010030#include "src/common/utils/Log.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010031#include "src/core/helpers/AutoConfiguration.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010032#include "src/core/NEON/kernels/NEPadLayerKernel.h"
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +000033
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +000034namespace arm_compute
35{
36namespace
37{
Usama Arif8cf8c112019-03-14 15:36:54 +000038uint32_t last_padding_dimension(const PaddingList &padding)
39{
40 int last_padding_dim = padding.size() - 1;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010041 for (; last_padding_dim >= 0; --last_padding_dim)
Usama Arif8cf8c112019-03-14 15:36:54 +000042 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010043 if (padding[last_padding_dim].first > 0 || padding[last_padding_dim].second > 0)
Usama Arif8cf8c112019-03-14 15:36:54 +000044 {
45 break;
46 }
47 }
48 return static_cast<uint32_t>(last_padding_dim);
49}
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +000050} // namespace
51
Michalis Spyrouebcebf12020-10-21 00:04:14 +010052NEPadLayer::~NEPadLayer() = default;
53
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +000054NEPadLayer::NEPadLayer()
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010055 : _copy_function(),
56 _pad_kernel(),
57 _mode(),
58 _padding(),
59 _num_dimensions(0),
60 _slice_functions(),
61 _concat_functions(),
62 _slice_results(),
63 _concat_results()
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +000064{
65}
66
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010067void NEPadLayer::configure_constant_mode(ITensor *input,
68 ITensor *output,
69 const PaddingList &padding,
70 const PixelValue constant_value)
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +000071{
Georgios Pinitas40f51a62020-11-21 03:04:18 +000072 _pad_kernel = std::make_unique<NEPadLayerKernel>();
Michalis Spyrouebcebf12020-10-21 00:04:14 +010073 _pad_kernel->configure(input, output, padding, constant_value, PaddingMode::CONSTANT);
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +000074}
75
Usama Arif8cf8c112019-03-14 15:36:54 +000076void NEPadLayer::configure_reflect_symmetric_mode(ITensor *input, ITensor *output)
77{
78 // Reflecting can be performed by effectively unfolding the input as follows:
79 // For each dimension starting at DimX:
80 // For before and after:
81 // Use strided slice to extract and reverse the part of the
82 // input / previously produced tensor required for the padding.
83 // Concatenate the before and after padding with the input / previously
84 // produced tensor along the current dimension.
85
86 // Two strided slice functions will be required for each dimension padded as well as a
87 // concatenate function and the tensors to hold the temporary results.
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010088 _slice_functions.resize(2 * _num_dimensions);
89 _slice_results.resize(2 * _num_dimensions);
90 _concat_functions.resize(_num_dimensions);
91 _concat_results.resize(_num_dimensions - 1);
92
93 Coordinates starts_before{};
94 Coordinates ends_before{};
95 Coordinates starts_after{};
96 Coordinates ends_after{};
97 Coordinates strides{};
Usama Arif8cf8c112019-03-14 15:36:54 +000098 ITensor *prev = input;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010099 for (uint32_t i = 0; i < _num_dimensions; ++i)
Usama Arif8cf8c112019-03-14 15:36:54 +0000100 {
101 // Values in strides from the previous dimensions need to be set to 1 to avoid reversing again.
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100102 if (i > 0)
Usama Arif8cf8c112019-03-14 15:36:54 +0000103 {
104 strides.set(i - 1, 1);
105 }
106
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100107 if (_padding[i].first > 0 || _padding[i].second > 0)
Usama Arif8cf8c112019-03-14 15:36:54 +0000108 {
109 // Set the starts, ends, and strides values for the current dimension.
110 // Due to the bit masks passed to strided slice, the values below the current dimension in
111 // starts and ends will be ignored so do not need to be modified.
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100112 if (_mode == PaddingMode::REFLECT)
Usama Arif8cf8c112019-03-14 15:36:54 +0000113 {
114 starts_before.set(i, _padding[i].first);
115 ends_before.set(i, 0);
116 starts_after.set(i, input->info()->dimension(i) - 2);
117 ends_after.set(i, input->info()->dimension(i) - _padding[i].second - 2);
118 strides.set(i, -1);
119 }
120 else
121 {
122 starts_before.set(i, _padding[i].first - 1);
123 ends_before.set(i, -1);
124 starts_after.set(i, input->info()->dimension(i) - 1);
125 ends_after.set(i, input->info()->dimension(i) - _padding[i].second - 1);
126 strides.set(i, -1);
127 }
128
129 // Strided slice wraps negative indexes around to the end of the range,
130 // instead this should indicate use of the full range and so the bit mask will be modified.
131 const int32_t begin_mask_before = starts_before[i] < 0 ? ~0 : ~(1u << i);
132 const int32_t end_mask_before = ends_before[i] < 0 ? ~0 : ~(1u << i);
133 const int32_t begin_mask_after = starts_after[i] < 0 ? ~0 : ~(1u << i);
134 const int32_t end_mask_after = ends_after[i] < 0 ? ~0 : ~(1u << i);
135
136 // Reflect the input values for the padding before and after the input.
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100137 std::vector<const ITensor *> concat_vector;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100138 if (_padding[i].first > 0)
Usama Arif8cf8c112019-03-14 15:36:54 +0000139 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100140 if (i < prev->info()->num_dimensions())
Usama Arif8cf8c112019-03-14 15:36:54 +0000141 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100142 _slice_functions[2 * i].configure(prev, &_slice_results[2 * i], starts_before, ends_before, strides,
143 begin_mask_before, end_mask_before);
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100144 concat_vector.emplace_back(&_slice_results[2 * i]);
Usama Arif8cf8c112019-03-14 15:36:54 +0000145 }
146 else
147 {
148 // Performing the slice is unnecessary if the result would simply be a copy of the tensor.
149 concat_vector.push_back(prev);
150 }
151 }
152 concat_vector.push_back(prev);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100153 if (_padding[i].second > 0)
Usama Arif8cf8c112019-03-14 15:36:54 +0000154 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100155 if (i < prev->info()->num_dimensions())
Usama Arif8cf8c112019-03-14 15:36:54 +0000156 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100157 _slice_functions[2 * i + 1].configure(prev, &_slice_results[2 * i + 1], starts_after, ends_after,
158 strides, begin_mask_after, end_mask_after);
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100159 concat_vector.emplace_back(&_slice_results[2 * i + 1]);
Usama Arif8cf8c112019-03-14 15:36:54 +0000160 }
161 else
162 {
163 // Performing the slice is unnecessary if the result would simply be a copy of the tensor.
164 concat_vector.push_back(prev);
165 }
166 }
167 // Concatenate the padding before and after with the input.
168 ITensor *out = (i == _num_dimensions - 1) ? output : &_concat_results[i];
Pablo Marquez Telloe7a5b0e2021-11-10 15:41:57 +0000169 out->info()->set_quantization_info(output->info()->quantization_info());
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100170 for (auto &v : concat_vector)
Pablo Marquez Telloe7a5b0e2021-11-10 15:41:57 +0000171 {
172 v->info()->set_quantization_info(input->info()->quantization_info());
173 }
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100174 _concat_functions[i].configure(concat_vector, out, i);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100175 if (i != _num_dimensions - 1)
Usama Arif8cf8c112019-03-14 15:36:54 +0000176 {
177 _concat_results[i].allocator()->allocate();
178 }
179 prev = out;
180 }
181 _slice_results[2 * i].allocator()->allocate();
182 _slice_results[2 * i + 1].allocator()->allocate();
183 }
184}
185
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100186void NEPadLayer::configure(ITensor *input,
187 ITensor *output,
188 const PaddingList &padding,
189 const PixelValue constant_value,
190 const PaddingMode mode)
Usama Arif8cf8c112019-03-14 15:36:54 +0000191{
192 ARM_COMPUTE_ERROR_THROW_ON(validate(input->info(), output->info(), padding, constant_value, mode));
ramelg01cbbb0382021-09-17 17:36:57 +0100193 ARM_COMPUTE_LOG_PARAMS(input, output, padding, constant_value, mode);
Usama Arif8cf8c112019-03-14 15:36:54 +0000194
195 _padding = padding;
196 _mode = mode;
197
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100198 const TensorShape padded_shape =
199 misc::shape_calculator::compute_padded_shape(input->info()->tensor_shape(), _padding);
Usama Arif8cf8c112019-03-14 15:36:54 +0000200
201 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(padded_shape));
202
203 // Find the last dimension requiring padding so that it is known when to write to output and whether any padding is applied.
204 _num_dimensions = last_padding_dimension(padding) + 1;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100205 if (_num_dimensions > 0)
Usama Arif8cf8c112019-03-14 15:36:54 +0000206 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100207 switch (_mode)
Usama Arif8cf8c112019-03-14 15:36:54 +0000208 {
209 case PaddingMode::CONSTANT:
210 {
211 configure_constant_mode(input, output, padding, constant_value);
212 break;
213 }
214 case PaddingMode::REFLECT:
215 case PaddingMode::SYMMETRIC:
216 {
217 configure_reflect_symmetric_mode(input, output);
218 break;
219 }
220 default:
221 ARM_COMPUTE_ERROR("Padding mode not supported.");
222 }
223 }
224 else
225 {
226 // Copy the input to the whole output if no padding is applied
Georgios Pinitas0f7ef8a2021-01-10 04:23:52 +0000227 _copy_function.configure(input, output);
Usama Arif8cf8c112019-03-14 15:36:54 +0000228 }
229}
230
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100231Status NEPadLayer::validate(const ITensorInfo *input,
232 const ITensorInfo *output,
233 const PaddingList &padding,
234 const PixelValue constant_value,
235 const PaddingMode mode)
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +0000236{
237 ARM_COMPUTE_UNUSED(constant_value);
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +0000238
Usama Arif8cf8c112019-03-14 15:36:54 +0000239 const TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(input->tensor_shape(), padding);
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +0000240
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100241 if (output->total_size() > 0)
Usama Arif8cf8c112019-03-14 15:36:54 +0000242 {
243 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), padded_shape);
244 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
245 }
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +0000246
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100247 switch (mode)
Usama Arif8cf8c112019-03-14 15:36:54 +0000248 {
249 case PaddingMode::CONSTANT:
250 {
Manuel Bottini9032ee32019-08-07 17:04:11 +0100251 return NEPadLayerKernel::validate(input, output, padding, constant_value, mode);
Usama Arif8cf8c112019-03-14 15:36:54 +0000252 }
253 case PaddingMode::REFLECT:
254 case PaddingMode::SYMMETRIC:
255 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100256 for (uint32_t i = 0; i < padding.size(); ++i)
Usama Arif8cf8c112019-03-14 15:36:54 +0000257 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100258 if (mode == PaddingMode::REFLECT)
Usama Arif8cf8c112019-03-14 15:36:54 +0000259 {
260 ARM_COMPUTE_RETURN_ERROR_ON(padding[i].first >= input->dimension(i));
261 ARM_COMPUTE_RETURN_ERROR_ON(padding[i].second >= input->dimension(i));
262 }
263 else
264 {
265 ARM_COMPUTE_RETURN_ERROR_ON(padding[i].first > input->dimension(i));
266 ARM_COMPUTE_RETURN_ERROR_ON(padding[i].second > input->dimension(i));
267 }
268 }
269 break;
270 }
271 default:
272 {
273 ARM_COMPUTE_ERROR("Invalid mode");
274 }
275 }
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +0000276 return Status{};
277}
278
279void NEPadLayer::run()
280{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100281 if (_num_dimensions > 0)
Usama Arif8cf8c112019-03-14 15:36:54 +0000282 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100283 switch (_mode)
Usama Arif8cf8c112019-03-14 15:36:54 +0000284 {
285 case PaddingMode::CONSTANT:
286 {
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100287 NEScheduler::get().schedule(_pad_kernel.get(), Window::DimZ);
Usama Arif8cf8c112019-03-14 15:36:54 +0000288 break;
289 }
290 case PaddingMode::REFLECT:
291 case PaddingMode::SYMMETRIC:
292 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100293 for (uint32_t i = 0; i < _num_dimensions; ++i)
Usama Arif8cf8c112019-03-14 15:36:54 +0000294 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100295 if (_padding[i].first > 0 || _padding[i].second > 0)
Usama Arif8cf8c112019-03-14 15:36:54 +0000296 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100297 if (_padding[i].first > 0 && _slice_results[2 * i].info()->total_size() > 0)
Usama Arif8cf8c112019-03-14 15:36:54 +0000298 {
299 _slice_functions[2 * i].run();
300 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100301 if (_padding[i].second > 0 && _slice_results[2 * i + 1].info()->total_size() > 0)
Usama Arif8cf8c112019-03-14 15:36:54 +0000302 {
303 _slice_functions[2 * i + 1].run();
304 }
305 _concat_functions[i].run();
306 }
307 }
308 break;
309 }
310 default:
311 ARM_COMPUTE_ERROR("Padding mode not supported.");
312 }
313 }
314 else
315 {
Georgios Pinitas0f7ef8a2021-01-10 04:23:52 +0000316 _copy_function.run();
Usama Arif8cf8c112019-03-14 15:36:54 +0000317 }
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +0000318}
319} // namespace arm_compute