Compute Library
 21.02
PadLayer.cpp
Go to the documentation of this file.
1 /*
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 "PadLayer.h"
25 
26 #include "arm_compute/core/Types.h"
29 
30 namespace arm_compute
31 {
32 namespace test
33 {
34 namespace validation
35 {
36 namespace reference
37 {
38 template <typename T>
39 SimpleTensor<T> pad_layer(const SimpleTensor<T> &src, const PaddingList &paddings, const PixelValue const_value, const PaddingMode mode)
40 {
41  const DataType dst_data_type = src.data_type();
42 
43  const TensorShape orig_shape = src.shape();
44 
45  std::vector<PaddingInfo> paddings_extended = paddings;
46 
47  for(size_t i = paddings.size(); i < TensorShape::num_max_dimensions; ++i)
48  {
49  paddings_extended.emplace_back(PaddingInfo{ 0, 0 });
50  }
51 
52  const TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(orig_shape, paddings);
53 
54  SimpleTensor<T> dst(padded_shape, dst_data_type);
55 
56  // Reference algorithm: loop over the different dimension of the input.
57  const uint32_t num_elements = dst.num_elements();
58  for(uint32_t idx = 0; idx < num_elements; ++idx)
59  {
60  const Coordinates coord = index2coord(padded_shape, idx);
61 
62  const size_t i = coord.x();
63  const size_t j = coord.y();
64  const size_t k = coord.z();
65  const size_t l = coord[3];
66  const size_t m = coord[4];
67  const size_t n = coord[5];
68 
69  const std::array<size_t, TensorShape::num_max_dimensions> dims = { { 0, 1, 2, 3, 4, 5 } };
70  const std::array<size_t, TensorShape::num_max_dimensions> coords = { { i, j, k, l, m, n } };
71  auto is_padding_area = [&](size_t i)
72  {
73  return (coords[i] < paddings_extended[i].first || coords[i] > orig_shape[i] + paddings_extended[i].first - 1);
74  };
75 
76  auto orig_coord_reflect = [&](size_t i)
77  {
78  if(is_padding_area(i))
79  {
80  if(coords[i] < paddings_extended[i].first)
81  {
82  return paddings_extended[i].first - coords[i];
83  }
84  else
85  {
86  return 2 * orig_shape[i] + paddings_extended[i].first - 2 - coords[i];
87  }
88  }
89  return coords[i] - paddings_extended[i].first;
90  };
91 
92  auto orig_coord_symm = [&](size_t i)
93  {
94  if(is_padding_area(i))
95  {
96  if(coords[i] < paddings_extended[i].first)
97  {
98  return paddings_extended[i].first - coords[i] - 1;
99  }
100  else
101  {
102  return 2 * orig_shape[i] + paddings_extended[i].first - 1 - coords[i];
103  }
104  }
105  return coords[i] - paddings_extended[i].first;
106  };
107 
108  // If the tuple [i,j,k,l,m] is in the padding area, then simply set the value
109  if(std::any_of(dims.begin(), dims.end(), is_padding_area))
110  {
111  switch(mode)
112  {
114  const_value.get(dst[idx]);
115  break;
117  {
118  const Coordinates orig_coords{ orig_coord_reflect(0),
119  orig_coord_reflect(1),
120  orig_coord_reflect(2),
121  orig_coord_reflect(3),
122  orig_coord_reflect(4),
123  orig_coord_reflect(5) };
124 
125  const size_t idx_src = coord2index(orig_shape, orig_coords);
126  dst[idx] = src[idx_src];
127  break;
128  }
130  {
131  const Coordinates orig_coords{ orig_coord_symm(0),
132  orig_coord_symm(1),
133  orig_coord_symm(2),
134  orig_coord_symm(3),
135  orig_coord_symm(4),
136  orig_coord_symm(5) };
137 
138  const size_t idx_src = coord2index(orig_shape, orig_coords);
139  dst[idx] = src[idx_src];
140  break;
141  }
142  default:
143  ARM_COMPUTE_ERROR("Padding mode not supported.");
144  break;
145  }
146  }
147  else
148  {
149  // If the tuple[i,j,k,l,m] is not in the padding area, then copy the input into the output
150 
151  const Coordinates orig_coords
152  {
153  i - paddings_extended[0].first,
154  j - paddings_extended[1].first,
155  k - paddings_extended[2].first,
156  l - paddings_extended[3].first,
157  m - paddings_extended[4].first,
158  n - paddings_extended[5].first
159  };
160 
161  const size_t idx_src = coord2index(orig_shape, orig_coords);
162  dst[idx] = src[idx_src];
163  }
164  }
165 
166  return dst;
167 }
168 
169 template SimpleTensor<float> pad_layer(const SimpleTensor<float> &src, const PaddingList &paddings, const PixelValue const_value = PixelValue(), const PaddingMode mode);
170 template SimpleTensor<half> pad_layer(const SimpleTensor<half> &src, const PaddingList &paddings, const PixelValue const_value = PixelValue(), const PaddingMode mode);
171 template SimpleTensor<uint8_t> pad_layer(const SimpleTensor<uint8_t> &src, const PaddingList &paddings, const PixelValue const_value = PixelValue(), const PaddingMode mode);
172 template SimpleTensor<int8_t> pad_layer(const SimpleTensor<int8_t> &src, const PaddingList &paddings, const PixelValue const_value = PixelValue(), const PaddingMode mode);
173 template SimpleTensor<uint16_t> pad_layer(const SimpleTensor<uint16_t> &src, const PaddingList &paddings, const PixelValue const_value = PixelValue(), const PaddingMode mode);
174 template SimpleTensor<int16_t> pad_layer(const SimpleTensor<int16_t> &src, const PaddingList &paddings, const PixelValue const_value = PixelValue(), const PaddingMode mode);
175 template SimpleTensor<uint32_t> pad_layer(const SimpleTensor<uint32_t> &src, const PaddingList &paddings, const PixelValue const_value = PixelValue(), const PaddingMode mode);
176 template SimpleTensor<int32_t> pad_layer(const SimpleTensor<int32_t> &src, const PaddingList &paddings, const PixelValue const_value = PixelValue(), const PaddingMode mode);
177 } // namespace reference
178 } // namespace validation
179 } // namespace test
180 } // namespace arm_compute
SimpleTensor< T > pad_layer(const SimpleTensor< T > &src, const PaddingList &paddings, const PixelValue const_value, const PaddingMode mode)
Reference function to pad an ND tensor.
Definition: PadLayer.cpp:39
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
void get(uint8_t &v) const
Interpret the pixel value as a U8.
Definition: PixelValue.h:241
DataType data_type() const override
Data type of the tensor.
Definition: SimpleTensor.h:357
TensorShape shape() const override
Shape of the tensor.
Definition: SimpleTensor.h:320
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2021 Arm Limited.
int coord2index(const TensorShape &shape, const Coordinates &coord)
Linearise the given coordinate.
Definition: Utils.h:489
T x() const
Alias to access the size of the first dimension.
Definition: Dimensions.h:87
PaddingMode
Padding mode to use for PadLayer.
Definition: Types.h:169
T z() const
Alias to access the size of the third dimension.
Definition: Dimensions.h:97
Coordinates of an item.
Definition: Coordinates.h:37
Coordinates index2coord(const TensorShape &shape, int index)
Convert a linear index into n-dimensional coordinates.
Definition: Utils.h:460
TensorShape compute_padded_shape(const TensorShape &input_shape, const PaddingList &padding)
Calculate the padded shape of a tensor.
Simple tensor object that stores elements in a consecutive chunk of memory.
Definition: SimpleTensor.h:58
std::pair< uint32_t, uint32_t > PaddingInfo
Padding information as a pair of unsigned int start/end.
Definition: Types.h:478
int num_elements() const override
Number of elements of the tensor.
Definition: SimpleTensor.h:406
T y() const
Alias to access the size of the second dimension.
Definition: Dimensions.h:92
static constexpr size_t num_max_dimensions
Number of dimensions the tensor has.
Definition: Dimensions.h:46
DataType
Available data types.
Definition: Types.h:77