Compute Library
 20.11
NEPadLayer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2020 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  */
25 
27 
28 #include "arm_compute/core/Types.h"
33 #include "support/MemorySupport.h"
34 
35 namespace arm_compute
36 {
37 namespace
38 {
39 uint32_t last_padding_dimension(const PaddingList &padding)
40 {
41  int last_padding_dim = padding.size() - 1;
42  for(; last_padding_dim >= 0; --last_padding_dim)
43  {
44  if(padding[last_padding_dim].first > 0 || padding[last_padding_dim].second > 0)
45  {
46  break;
47  }
48  }
49  return static_cast<uint32_t>(last_padding_dim);
50 }
51 } // namespace
52 
53 NEPadLayer::~NEPadLayer() = default;
54 
56  : _copy_kernel(), _pad_kernel(), _mode(), _padding(), _num_dimensions(0), _slice_functions(), _concat_functions(), _slice_results(), _concat_results()
57 {
58 }
59 
60 void NEPadLayer::configure_constant_mode(ITensor *input, ITensor *output, const PaddingList &padding, const PixelValue constant_value)
61 {
62  _pad_kernel = arm_compute::support::cpp14::make_unique<NEPadLayerKernel>();
63  _pad_kernel->configure(input, output, padding, constant_value, PaddingMode::CONSTANT);
64 }
65 
66 void NEPadLayer::configure_reflect_symmetric_mode(ITensor *input, ITensor *output)
67 {
68  // Reflecting can be performed by effectively unfolding the input as follows:
69  // For each dimension starting at DimX:
70  // For before and after:
71  // Use strided slice to extract and reverse the part of the
72  // input / previously produced tensor required for the padding.
73  // Concatenate the before and after padding with the input / previously
74  // produced tensor along the current dimension.
75 
76  // Two strided slice functions will be required for each dimension padded as well as a
77  // concatenate function and the tensors to hold the temporary results.
78  _slice_functions.resize(2 * _num_dimensions);
79  _slice_results.resize(2 * _num_dimensions);
80  _concat_functions.resize(_num_dimensions);
81  _concat_results.resize(_num_dimensions - 1);
82 
83  Coordinates starts_before{};
84  Coordinates ends_before{};
85  Coordinates starts_after{};
86  Coordinates ends_after{};
87  Coordinates strides{};
88  ITensor *prev = input;
89  for(uint32_t i = 0; i < _num_dimensions; ++i)
90  {
91  // Values in strides from the previous dimensions need to be set to 1 to avoid reversing again.
92  if(i > 0)
93  {
94  strides.set(i - 1, 1);
95  }
96 
97  if(_padding[i].first > 0 || _padding[i].second > 0)
98  {
99  // Set the starts, ends, and strides values for the current dimension.
100  // Due to the bit masks passed to strided slice, the values below the current dimension in
101  // starts and ends will be ignored so do not need to be modified.
102  if(_mode == PaddingMode::REFLECT)
103  {
104  starts_before.set(i, _padding[i].first);
105  ends_before.set(i, 0);
106  starts_after.set(i, input->info()->dimension(i) - 2);
107  ends_after.set(i, input->info()->dimension(i) - _padding[i].second - 2);
108  strides.set(i, -1);
109  }
110  else
111  {
112  starts_before.set(i, _padding[i].first - 1);
113  ends_before.set(i, -1);
114  starts_after.set(i, input->info()->dimension(i) - 1);
115  ends_after.set(i, input->info()->dimension(i) - _padding[i].second - 1);
116  strides.set(i, -1);
117  }
118 
119  // Strided slice wraps negative indexes around to the end of the range,
120  // instead this should indicate use of the full range and so the bit mask will be modified.
121  const int32_t begin_mask_before = starts_before[i] < 0 ? ~0 : ~(1u << i);
122  const int32_t end_mask_before = ends_before[i] < 0 ? ~0 : ~(1u << i);
123  const int32_t begin_mask_after = starts_after[i] < 0 ? ~0 : ~(1u << i);
124  const int32_t end_mask_after = ends_after[i] < 0 ? ~0 : ~(1u << i);
125 
126  // Reflect the input values for the padding before and after the input.
127  std::vector<const ITensor *> concat_vector;
128  if(_padding[i].first > 0)
129  {
130  if(i < prev->info()->num_dimensions())
131  {
132  _slice_functions[2 * i].configure(prev, &_slice_results[2 * i], starts_before, ends_before, strides, begin_mask_before, end_mask_before);
133  concat_vector.emplace_back(&_slice_results[2 * i]);
134  }
135  else
136  {
137  // Performing the slice is unnecessary if the result would simply be a copy of the tensor.
138  concat_vector.push_back(prev);
139  }
140  }
141  concat_vector.push_back(prev);
142  if(_padding[i].second > 0)
143  {
144  if(i < prev->info()->num_dimensions())
145  {
146  _slice_functions[2 * i + 1].configure(prev, &_slice_results[2 * i + 1], starts_after, ends_after, strides, begin_mask_after, end_mask_after);
147  concat_vector.emplace_back(&_slice_results[2 * i + 1]);
148  }
149  else
150  {
151  // Performing the slice is unnecessary if the result would simply be a copy of the tensor.
152  concat_vector.push_back(prev);
153  }
154  }
155  // Concatenate the padding before and after with the input.
156  ITensor *out = (i == _num_dimensions - 1) ? output : &_concat_results[i];
157  _concat_functions[i].configure(concat_vector, out, i);
158  if(i != _num_dimensions - 1)
159  {
160  _concat_results[i].allocator()->allocate();
161  }
162  prev = out;
163  }
164  _slice_results[2 * i].allocator()->allocate();
165  _slice_results[2 * i + 1].allocator()->allocate();
166  }
167 }
168 
169 void NEPadLayer::configure(ITensor *input, ITensor *output, const PaddingList &padding, const PixelValue constant_value, const PaddingMode mode)
170 {
171  ARM_COMPUTE_ERROR_THROW_ON(validate(input->info(), output->info(), padding, constant_value, mode));
172 
173  _padding = padding;
174  _mode = mode;
175 
176  const TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(input->info()->tensor_shape(), _padding);
177 
178  auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(padded_shape));
179 
180  // Find the last dimension requiring padding so that it is known when to write to output and whether any padding is applied.
181  _num_dimensions = last_padding_dimension(padding) + 1;
182  if(_num_dimensions > 0)
183  {
184  switch(_mode)
185  {
187  {
188  configure_constant_mode(input, output, padding, constant_value);
189  break;
190  }
193  {
194  configure_reflect_symmetric_mode(input, output);
195  break;
196  }
197  default:
198  ARM_COMPUTE_ERROR("Padding mode not supported.");
199  }
200  }
201  else
202  {
203  // Copy the input to the whole output if no padding is applied
204  _copy_kernel = arm_compute::support::cpp14::make_unique<NECopyKernel>();
205  _copy_kernel->configure(input, output);
206  }
207 }
208 
209 Status NEPadLayer::validate(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, const PixelValue constant_value, const PaddingMode mode)
210 {
211  ARM_COMPUTE_UNUSED(constant_value);
212 
213  const TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(input->tensor_shape(), padding);
214 
215  if(output->total_size() > 0)
216  {
219  }
220 
221  switch(mode)
222  {
224  {
225  return NEPadLayerKernel::validate(input, output, padding, constant_value, mode);
226  }
229  {
230  for(uint32_t i = 0; i < padding.size(); ++i)
231  {
233  {
234  ARM_COMPUTE_RETURN_ERROR_ON(padding[i].first >= input->dimension(i));
235  ARM_COMPUTE_RETURN_ERROR_ON(padding[i].second >= input->dimension(i));
236  }
237  else
238  {
239  ARM_COMPUTE_RETURN_ERROR_ON(padding[i].first > input->dimension(i));
240  ARM_COMPUTE_RETURN_ERROR_ON(padding[i].second > input->dimension(i));
241  }
242  }
243  break;
244  }
245  default:
246  {
247  ARM_COMPUTE_ERROR("Invalid mode");
248  }
249  }
250  return Status{};
251 }
252 
254 {
255  if(_num_dimensions > 0)
256  {
257  switch(_mode)
258  {
260  {
261  NEScheduler::get().schedule(_pad_kernel.get(), Window::DimZ);
262  break;
263  }
266  {
267  for(uint32_t i = 0; i < _num_dimensions; ++i)
268  {
269  if(_padding[i].first > 0 || _padding[i].second > 0)
270  {
271  if(_padding[i].first > 0 && _slice_results[2 * i].info()->total_size() > 0)
272  {
273  _slice_functions[2 * i].run();
274  }
275  if(_padding[i].second > 0 && _slice_results[2 * i + 1].info()->total_size() > 0)
276  {
277  _slice_functions[2 * i + 1].run();
278  }
279  _concat_functions[i].run();
280  }
281  }
282  break;
283  }
284  default:
285  ARM_COMPUTE_ERROR("Padding mode not supported.");
286  }
287  }
288  else
289  {
290  NEScheduler::get().schedule(_copy_kernel.get(), Window::DimY);
291  }
292 }
293 } // namespace arm_compute
Class describing the value of a pixel for any image format.
Definition: PixelValue.h:34
Shape of a tensor.
Definition: TensorShape.h:39
std::vector< PaddingInfo > PaddingList
List of padding information.
Definition: Types.h:481
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
Store the tensor's metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Status class.
Definition: Error.h:52
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
Interface for NEON tensor.
Definition: ITensor.h:36
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(...)
Definition: Validate.h:288
Copyright (c) 2017-2020 Arm Limited.
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
PaddingMode
Padding mode to use for PadLayer.
Definition: Types.h:169
static Status validate(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, const PixelValue constant_value=PixelValue(), const PaddingMode mode=PaddingMode::CONSTANT)
Static function to check if given info will lead to a valid configuration of NEPadLayer.
Definition: NEPadLayer.cpp:209
~NEPadLayer()
Default destructor.
bool auto_init_if_empty(ITensorInfo &info, const TensorShape &shape, int num_channels, DataType data_type, QuantizationInfo quantization_info=QuantizationInfo())
Auto initialize the tensor info (shape, number of channels and data type) if the current assignment i...
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
TensorShape compute_padded_shape(const TensorShape &input_shape, const PaddingList &padding)
Calculate the padded shape of a tensor.
static Status validate(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, const PixelValue constant_value=PixelValue(), const PaddingMode mode=PaddingMode::CONSTANT)
Static function to check if given info will lead to a valid configuration of NEPadLayer.
NEPadLayer()
Default Constructor.
Definition: NEPadLayer.cpp:55
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
virtual size_t total_size() const =0
Returns the total size of the tensor in bytes.
virtual void schedule(ICPPKernel *kernel, const Hints &hints)=0
Runs the kernel in the same thread as the caller synchronously.
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:545
void run() override
Run the kernels contained in the function.
Definition: NEPadLayer.cpp:253
void configure(ITensor *input, ITensor *output, const PaddingList &padding, const PixelValue constant_value=PixelValue(), const PaddingMode mode=PaddingMode::CONSTANT)
Initialize the function.
Definition: NEPadLayer.cpp:169
static IScheduler & get()
Access the scheduler singleton.
Definition: Scheduler.cpp:95