blob: 88a73b8b0d8be02d3e5f87fde83ff6b6b4e6bd2d [file] [log] [blame]
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 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
26#include "arm_compute/runtime/NEON/NEScheduler.h"
27
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +000028#include "arm_compute/core/Types.h"
29#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Michalis Spyrouebcebf12020-10-21 00:04:14 +010030#include "src/core/NEON/kernels/NECopyKernel.h"
31#include "src/core/NEON/kernels/NEPadLayerKernel.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010032#include "src/core/helpers/AutoConfiguration.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;
41 for(; last_padding_dim >= 0; --last_padding_dim)
42 {
43 if(padding[last_padding_dim].first > 0 || padding[last_padding_dim].second > 0)
44 {
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()
Manuel Bottini9032ee32019-08-07 17:04:11 +010055 : _copy_kernel(), _pad_kernel(), _mode(), _padding(), _num_dimensions(0), _slice_functions(), _concat_functions(), _slice_results(), _concat_results()
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +000056{
57}
58
Usama Arif8cf8c112019-03-14 15:36:54 +000059void NEPadLayer::configure_constant_mode(ITensor *input, ITensor *output, const PaddingList &padding, const PixelValue constant_value)
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +000060{
Georgios Pinitas40f51a62020-11-21 03:04:18 +000061 _pad_kernel = std::make_unique<NEPadLayerKernel>();
Michalis Spyrouebcebf12020-10-21 00:04:14 +010062 _pad_kernel->configure(input, output, padding, constant_value, PaddingMode::CONSTANT);
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +000063}
64
Usama Arif8cf8c112019-03-14 15:36:54 +000065void NEPadLayer::configure_reflect_symmetric_mode(ITensor *input, ITensor *output)
66{
67 // Reflecting can be performed by effectively unfolding the input as follows:
68 // For each dimension starting at DimX:
69 // For before and after:
70 // Use strided slice to extract and reverse the part of the
71 // input / previously produced tensor required for the padding.
72 // Concatenate the before and after padding with the input / previously
73 // produced tensor along the current dimension.
74
75 // Two strided slice functions will be required for each dimension padded as well as a
76 // concatenate function and the tensors to hold the temporary results.
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010077 _slice_functions.resize(2 * _num_dimensions);
78 _slice_results.resize(2 * _num_dimensions);
79 _concat_functions.resize(_num_dimensions);
80 _concat_results.resize(_num_dimensions - 1);
81
82 Coordinates starts_before{};
83 Coordinates ends_before{};
84 Coordinates starts_after{};
85 Coordinates ends_after{};
86 Coordinates strides{};
Usama Arif8cf8c112019-03-14 15:36:54 +000087 ITensor *prev = input;
88 for(uint32_t i = 0; i < _num_dimensions; ++i)
89 {
90 // Values in strides from the previous dimensions need to be set to 1 to avoid reversing again.
91 if(i > 0)
92 {
93 strides.set(i - 1, 1);
94 }
95
96 if(_padding[i].first > 0 || _padding[i].second > 0)
97 {
98 // Set the starts, ends, and strides values for the current dimension.
99 // Due to the bit masks passed to strided slice, the values below the current dimension in
100 // starts and ends will be ignored so do not need to be modified.
101 if(_mode == PaddingMode::REFLECT)
102 {
103 starts_before.set(i, _padding[i].first);
104 ends_before.set(i, 0);
105 starts_after.set(i, input->info()->dimension(i) - 2);
106 ends_after.set(i, input->info()->dimension(i) - _padding[i].second - 2);
107 strides.set(i, -1);
108 }
109 else
110 {
111 starts_before.set(i, _padding[i].first - 1);
112 ends_before.set(i, -1);
113 starts_after.set(i, input->info()->dimension(i) - 1);
114 ends_after.set(i, input->info()->dimension(i) - _padding[i].second - 1);
115 strides.set(i, -1);
116 }
117
118 // Strided slice wraps negative indexes around to the end of the range,
119 // instead this should indicate use of the full range and so the bit mask will be modified.
120 const int32_t begin_mask_before = starts_before[i] < 0 ? ~0 : ~(1u << i);
121 const int32_t end_mask_before = ends_before[i] < 0 ? ~0 : ~(1u << i);
122 const int32_t begin_mask_after = starts_after[i] < 0 ? ~0 : ~(1u << i);
123 const int32_t end_mask_after = ends_after[i] < 0 ? ~0 : ~(1u << i);
124
125 // Reflect the input values for the padding before and after the input.
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100126 std::vector<const ITensor *> concat_vector;
Usama Arif8cf8c112019-03-14 15:36:54 +0000127 if(_padding[i].first > 0)
128 {
129 if(i < prev->info()->num_dimensions())
130 {
131 _slice_functions[2 * i].configure(prev, &_slice_results[2 * i], starts_before, ends_before, strides, begin_mask_before, end_mask_before);
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100132 concat_vector.emplace_back(&_slice_results[2 * i]);
Usama Arif8cf8c112019-03-14 15:36:54 +0000133 }
134 else
135 {
136 // Performing the slice is unnecessary if the result would simply be a copy of the tensor.
137 concat_vector.push_back(prev);
138 }
139 }
140 concat_vector.push_back(prev);
141 if(_padding[i].second > 0)
142 {
143 if(i < prev->info()->num_dimensions())
144 {
145 _slice_functions[2 * i + 1].configure(prev, &_slice_results[2 * i + 1], starts_after, ends_after, strides, begin_mask_after, end_mask_after);
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100146 concat_vector.emplace_back(&_slice_results[2 * i + 1]);
Usama Arif8cf8c112019-03-14 15:36:54 +0000147 }
148 else
149 {
150 // Performing the slice is unnecessary if the result would simply be a copy of the tensor.
151 concat_vector.push_back(prev);
152 }
153 }
154 // Concatenate the padding before and after with the input.
155 ITensor *out = (i == _num_dimensions - 1) ? output : &_concat_results[i];
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100156 _concat_functions[i].configure(concat_vector, out, i);
Usama Arif8cf8c112019-03-14 15:36:54 +0000157 if(i != _num_dimensions - 1)
158 {
159 _concat_results[i].allocator()->allocate();
160 }
161 prev = out;
162 }
163 _slice_results[2 * i].allocator()->allocate();
164 _slice_results[2 * i + 1].allocator()->allocate();
165 }
166}
167
168void NEPadLayer::configure(ITensor *input, ITensor *output, const PaddingList &padding, const PixelValue constant_value, const PaddingMode mode)
169{
170 ARM_COMPUTE_ERROR_THROW_ON(validate(input->info(), output->info(), padding, constant_value, mode));
171
172 _padding = padding;
173 _mode = mode;
174
175 const TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(input->info()->tensor_shape(), _padding);
176
177 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(padded_shape));
178
179 // Find the last dimension requiring padding so that it is known when to write to output and whether any padding is applied.
180 _num_dimensions = last_padding_dimension(padding) + 1;
181 if(_num_dimensions > 0)
182 {
183 switch(_mode)
184 {
185 case PaddingMode::CONSTANT:
186 {
187 configure_constant_mode(input, output, padding, constant_value);
188 break;
189 }
190 case PaddingMode::REFLECT:
191 case PaddingMode::SYMMETRIC:
192 {
193 configure_reflect_symmetric_mode(input, output);
194 break;
195 }
196 default:
197 ARM_COMPUTE_ERROR("Padding mode not supported.");
198 }
199 }
200 else
201 {
202 // Copy the input to the whole output if no padding is applied
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000203 _copy_kernel = std::make_unique<NECopyKernel>();
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100204 _copy_kernel->configure(input, output);
Usama Arif8cf8c112019-03-14 15:36:54 +0000205 }
206}
207
208Status NEPadLayer::validate(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, const PixelValue constant_value, const PaddingMode mode)
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +0000209{
210 ARM_COMPUTE_UNUSED(constant_value);
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +0000211
Usama Arif8cf8c112019-03-14 15:36:54 +0000212 const TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(input->tensor_shape(), padding);
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +0000213
Usama Arif8cf8c112019-03-14 15:36:54 +0000214 if(output->total_size() > 0)
215 {
216 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), padded_shape);
217 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
218 }
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +0000219
Usama Arif8cf8c112019-03-14 15:36:54 +0000220 switch(mode)
221 {
222 case PaddingMode::CONSTANT:
223 {
Manuel Bottini9032ee32019-08-07 17:04:11 +0100224 return NEPadLayerKernel::validate(input, output, padding, constant_value, mode);
Usama Arif8cf8c112019-03-14 15:36:54 +0000225 }
226 case PaddingMode::REFLECT:
227 case PaddingMode::SYMMETRIC:
228 {
229 for(uint32_t i = 0; i < padding.size(); ++i)
230 {
231 if(mode == PaddingMode::REFLECT)
232 {
233 ARM_COMPUTE_RETURN_ERROR_ON(padding[i].first >= input->dimension(i));
234 ARM_COMPUTE_RETURN_ERROR_ON(padding[i].second >= input->dimension(i));
235 }
236 else
237 {
238 ARM_COMPUTE_RETURN_ERROR_ON(padding[i].first > input->dimension(i));
239 ARM_COMPUTE_RETURN_ERROR_ON(padding[i].second > input->dimension(i));
240 }
241 }
242 break;
243 }
244 default:
245 {
246 ARM_COMPUTE_ERROR("Invalid mode");
247 }
248 }
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +0000249 return Status{};
250}
251
252void NEPadLayer::run()
253{
Usama Arif8cf8c112019-03-14 15:36:54 +0000254 if(_num_dimensions > 0)
255 {
256 switch(_mode)
257 {
258 case PaddingMode::CONSTANT:
259 {
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100260 NEScheduler::get().schedule(_pad_kernel.get(), Window::DimZ);
Usama Arif8cf8c112019-03-14 15:36:54 +0000261 break;
262 }
263 case PaddingMode::REFLECT:
264 case PaddingMode::SYMMETRIC:
265 {
266 for(uint32_t i = 0; i < _num_dimensions; ++i)
267 {
268 if(_padding[i].first > 0 || _padding[i].second > 0)
269 {
270 if(_padding[i].first > 0 && _slice_results[2 * i].info()->total_size() > 0)
271 {
272 _slice_functions[2 * i].run();
273 }
274 if(_padding[i].second > 0 && _slice_results[2 * i + 1].info()->total_size() > 0)
275 {
276 _slice_functions[2 * i + 1].run();
277 }
278 _concat_functions[i].run();
279 }
280 }
281 break;
282 }
283 default:
284 ARM_COMPUTE_ERROR("Padding mode not supported.");
285 }
286 }
287 else
288 {
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100289 NEScheduler::get().schedule(_copy_kernel.get(), Window::DimY);
Usama Arif8cf8c112019-03-14 15:36:54 +0000290 }
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +0000291}
292} // namespace arm_compute