blob: cf86240cabdc0225576986d99f9510f8301dc995 [file] [log] [blame]
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +00001/*
2 * Copyright (c) 2018-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/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"
30
31#include "support/ToolchainSupport.h"
32
33namespace arm_compute
34{
35namespace
36{
Usama Arif8cf8c112019-03-14 15:36:54 +000037uint32_t last_padding_dimension(const PaddingList &padding)
38{
39 int last_padding_dim = padding.size() - 1;
40 for(; last_padding_dim >= 0; --last_padding_dim)
41 {
42 if(padding[last_padding_dim].first > 0 || padding[last_padding_dim].second > 0)
43 {
44 break;
45 }
46 }
47 return static_cast<uint32_t>(last_padding_dim);
48}
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +000049} // namespace
50
51NEPadLayer::NEPadLayer()
Manuel Bottini9032ee32019-08-07 17:04:11 +010052 : _copy_kernel(), _pad_kernel(), _mode(), _padding(), _num_dimensions(0), _slice_functions(), _concat_functions(), _slice_results(), _concat_results()
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +000053{
54}
55
Usama Arif8cf8c112019-03-14 15:36:54 +000056void NEPadLayer::configure_constant_mode(ITensor *input, ITensor *output, const PaddingList &padding, const PixelValue constant_value)
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +000057{
Manuel Bottini9032ee32019-08-07 17:04:11 +010058 _pad_kernel.configure(input, output, padding, constant_value, PaddingMode::CONSTANT);
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +000059}
60
Usama Arif8cf8c112019-03-14 15:36:54 +000061void NEPadLayer::configure_reflect_symmetric_mode(ITensor *input, ITensor *output)
62{
63 // Reflecting can be performed by effectively unfolding the input as follows:
64 // For each dimension starting at DimX:
65 // For before and after:
66 // Use strided slice to extract and reverse the part of the
67 // input / previously produced tensor required for the padding.
68 // Concatenate the before and after padding with the input / previously
69 // produced tensor along the current dimension.
70
71 // Two strided slice functions will be required for each dimension padded as well as a
72 // concatenate function and the tensors to hold the temporary results.
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010073 _slice_functions.resize(2 * _num_dimensions);
74 _slice_results.resize(2 * _num_dimensions);
75 _concat_functions.resize(_num_dimensions);
76 _concat_results.resize(_num_dimensions - 1);
77
78 Coordinates starts_before{};
79 Coordinates ends_before{};
80 Coordinates starts_after{};
81 Coordinates ends_after{};
82 Coordinates strides{};
Usama Arif8cf8c112019-03-14 15:36:54 +000083 ITensor *prev = input;
84 for(uint32_t i = 0; i < _num_dimensions; ++i)
85 {
86 // Values in strides from the previous dimensions need to be set to 1 to avoid reversing again.
87 if(i > 0)
88 {
89 strides.set(i - 1, 1);
90 }
91
92 if(_padding[i].first > 0 || _padding[i].second > 0)
93 {
94 // Set the starts, ends, and strides values for the current dimension.
95 // Due to the bit masks passed to strided slice, the values below the current dimension in
96 // starts and ends will be ignored so do not need to be modified.
97 if(_mode == PaddingMode::REFLECT)
98 {
99 starts_before.set(i, _padding[i].first);
100 ends_before.set(i, 0);
101 starts_after.set(i, input->info()->dimension(i) - 2);
102 ends_after.set(i, input->info()->dimension(i) - _padding[i].second - 2);
103 strides.set(i, -1);
104 }
105 else
106 {
107 starts_before.set(i, _padding[i].first - 1);
108 ends_before.set(i, -1);
109 starts_after.set(i, input->info()->dimension(i) - 1);
110 ends_after.set(i, input->info()->dimension(i) - _padding[i].second - 1);
111 strides.set(i, -1);
112 }
113
114 // Strided slice wraps negative indexes around to the end of the range,
115 // instead this should indicate use of the full range and so the bit mask will be modified.
116 const int32_t begin_mask_before = starts_before[i] < 0 ? ~0 : ~(1u << i);
117 const int32_t end_mask_before = ends_before[i] < 0 ? ~0 : ~(1u << i);
118 const int32_t begin_mask_after = starts_after[i] < 0 ? ~0 : ~(1u << i);
119 const int32_t end_mask_after = ends_after[i] < 0 ? ~0 : ~(1u << i);
120
121 // Reflect the input values for the padding before and after the input.
122 std::vector<ITensor *> concat_vector;
123 if(_padding[i].first > 0)
124 {
125 if(i < prev->info()->num_dimensions())
126 {
127 _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 +0100128 concat_vector.emplace_back(&_slice_results[2 * i]);
Usama Arif8cf8c112019-03-14 15:36:54 +0000129 }
130 else
131 {
132 // Performing the slice is unnecessary if the result would simply be a copy of the tensor.
133 concat_vector.push_back(prev);
134 }
135 }
136 concat_vector.push_back(prev);
137 if(_padding[i].second > 0)
138 {
139 if(i < prev->info()->num_dimensions())
140 {
141 _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 +0100142 concat_vector.emplace_back(&_slice_results[2 * i + 1]);
Usama Arif8cf8c112019-03-14 15:36:54 +0000143 }
144 else
145 {
146 // Performing the slice is unnecessary if the result would simply be a copy of the tensor.
147 concat_vector.push_back(prev);
148 }
149 }
150 // Concatenate the padding before and after with the input.
151 ITensor *out = (i == _num_dimensions - 1) ? output : &_concat_results[i];
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100152 _concat_functions[i].configure(concat_vector, out, i);
Usama Arif8cf8c112019-03-14 15:36:54 +0000153 if(i != _num_dimensions - 1)
154 {
155 _concat_results[i].allocator()->allocate();
156 }
157 prev = out;
158 }
159 _slice_results[2 * i].allocator()->allocate();
160 _slice_results[2 * i + 1].allocator()->allocate();
161 }
162}
163
164void NEPadLayer::configure(ITensor *input, ITensor *output, const PaddingList &padding, const PixelValue constant_value, const PaddingMode mode)
165{
166 ARM_COMPUTE_ERROR_THROW_ON(validate(input->info(), output->info(), padding, constant_value, mode));
167
168 _padding = padding;
169 _mode = mode;
170
171 const TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(input->info()->tensor_shape(), _padding);
172
173 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(padded_shape));
174
175 // Find the last dimension requiring padding so that it is known when to write to output and whether any padding is applied.
176 _num_dimensions = last_padding_dimension(padding) + 1;
177 if(_num_dimensions > 0)
178 {
179 switch(_mode)
180 {
181 case PaddingMode::CONSTANT:
182 {
183 configure_constant_mode(input, output, padding, constant_value);
184 break;
185 }
186 case PaddingMode::REFLECT:
187 case PaddingMode::SYMMETRIC:
188 {
189 configure_reflect_symmetric_mode(input, output);
190 break;
191 }
192 default:
193 ARM_COMPUTE_ERROR("Padding mode not supported.");
194 }
195 }
196 else
197 {
198 // Copy the input to the whole output if no padding is applied
199 _copy_kernel.configure(input, output);
200 }
201}
202
203Status 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 +0000204{
205 ARM_COMPUTE_UNUSED(constant_value);
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +0000206
Usama Arif8cf8c112019-03-14 15:36:54 +0000207 const TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(input->tensor_shape(), padding);
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +0000208
Usama Arif8cf8c112019-03-14 15:36:54 +0000209 if(output->total_size() > 0)
210 {
211 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), padded_shape);
212 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
213 }
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +0000214
Usama Arif8cf8c112019-03-14 15:36:54 +0000215 switch(mode)
216 {
217 case PaddingMode::CONSTANT:
218 {
Manuel Bottini9032ee32019-08-07 17:04:11 +0100219 return NEPadLayerKernel::validate(input, output, padding, constant_value, mode);
Usama Arif8cf8c112019-03-14 15:36:54 +0000220 }
221 case PaddingMode::REFLECT:
222 case PaddingMode::SYMMETRIC:
223 {
224 for(uint32_t i = 0; i < padding.size(); ++i)
225 {
226 if(mode == PaddingMode::REFLECT)
227 {
228 ARM_COMPUTE_RETURN_ERROR_ON(padding[i].first >= input->dimension(i));
229 ARM_COMPUTE_RETURN_ERROR_ON(padding[i].second >= input->dimension(i));
230 }
231 else
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 }
237 break;
238 }
239 default:
240 {
241 ARM_COMPUTE_ERROR("Invalid mode");
242 }
243 }
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +0000244 return Status{};
245}
246
247void NEPadLayer::run()
248{
Usama Arif8cf8c112019-03-14 15:36:54 +0000249 if(_num_dimensions > 0)
250 {
251 switch(_mode)
252 {
253 case PaddingMode::CONSTANT:
254 {
Manuel Bottini9032ee32019-08-07 17:04:11 +0100255 NEScheduler::get().schedule(&_pad_kernel, Window::DimZ);
Usama Arif8cf8c112019-03-14 15:36:54 +0000256 break;
257 }
258 case PaddingMode::REFLECT:
259 case PaddingMode::SYMMETRIC:
260 {
261 for(uint32_t i = 0; i < _num_dimensions; ++i)
262 {
263 if(_padding[i].first > 0 || _padding[i].second > 0)
264 {
265 if(_padding[i].first > 0 && _slice_results[2 * i].info()->total_size() > 0)
266 {
267 _slice_functions[2 * i].run();
268 }
269 if(_padding[i].second > 0 && _slice_results[2 * i + 1].info()->total_size() > 0)
270 {
271 _slice_functions[2 * i + 1].run();
272 }
273 _concat_functions[i].run();
274 }
275 }
276 break;
277 }
278 default:
279 ARM_COMPUTE_ERROR("Padding mode not supported.");
280 }
281 }
282 else
283 {
284 NEScheduler::get().schedule(&_copy_kernel, Window::DimY);
285 }
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +0000286}
287} // namespace arm_compute