Compute Library
 21.02
Im2Col.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  */
24 #include "Im2Col.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 void im2col_nchw(const SimpleTensor<T> &src, SimpleTensor<T> &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, unsigned int num_groups)
40 {
42  const int stride_x = conv_info.stride().first;
43  const int stride_y = conv_info.stride().second;
44  const int kernel_width = kernel_dims.width;
45  const int kernel_height = kernel_dims.height;
46  const int pad_x = conv_info.pad().first;
47  const int pad_y = conv_info.pad().second;
48  const int src_width = src.shape().x();
49  const int src_height = src.shape().y();
50  const int src_channels = src.shape().z();
51  const int batches = src.shape().total_size_upper(3);
52  const int dst_height = dst.shape().y();
53  const int pad_val = is_data_type_quantized_asymmetric(src.data_type()) ? src.quantization_info().uniform().offset : 0;
54  int dst_idx = 0;
55 
56  // Compute width and height of the convolved tensors
57  std::pair<unsigned int, unsigned int> convolved_dims = scaled_dimensions(src_width, src_height, kernel_dims.width, kernel_dims.height, conv_info);
58 
59  for(int b = 0; b < batches; ++b)
60  {
61  for(int g = 0; g < static_cast<int>(num_groups); ++g)
62  {
63  const int first_group_ch = g * (src_channels / num_groups);
64  const int last_group_ch = (g + 1) * (src_channels / num_groups);
65 
66  for(int yo = 0; yo < dst_height; ++yo)
67  {
68  // Compute input spatial coordinates
69  const int xi = (yo % convolved_dims.first) * stride_x;
70  const int yi = (yo / convolved_dims.first) * stride_y;
71 
72  for(int ci = first_group_ch; ci < last_group_ch; ++ci)
73  {
74  for(int yk = 0; yk < kernel_height; ++yk)
75  {
76  for(int xk = 0; xk < kernel_width; ++xk)
77  {
78  dst[dst_idx++] = tensor_elem_at(src, Coordinates(xi + xk - pad_x, yi + yk - pad_y, ci, b), BorderMode::CONSTANT, static_cast<T>(pad_val));
79  }
80  }
81  }
82 
83  if(has_bias)
84  {
85  dst[dst_idx++] = static_cast<T>(1);
86  }
87  }
88  }
89  }
90 }
91 
92 template <typename T>
93 void im2col_nhwc(const SimpleTensor<T> &src, SimpleTensor<T> &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias)
94 {
96  const int stride_x = conv_info.stride().first;
97  const int stride_y = conv_info.stride().second;
98  const int kernel_width = kernel_dims.width;
99  const int kernel_height = kernel_dims.height;
100  const int pad_x = conv_info.pad().first;
101  const int pad_y = conv_info.pad().second;
102  const int src_width = src.shape().y();
103  const int src_height = src.shape().z();
104  const int src_channels = src.shape().x();
105  const int batches = src.shape().total_size_upper(3);
106  const int dst_width = has_bias ? dst.shape().x() - 1 : dst.shape().x();
107  const int dst_height = dst.shape().y();
108  const int pad_val = is_data_type_quantized_asymmetric(src.data_type()) ? src.quantization_info().uniform().offset : 0;
109 
110  // Compute width and height of the convolved tensors
111  std::pair<unsigned int, unsigned int> convolved_dims = scaled_dimensions(src_width, src_height, kernel_dims.width, kernel_dims.height, conv_info);
112 #if defined(_OPENMP)
113  #pragma omp parallel for schedule(dynamic, 1) collapse(2)
114 #endif /* _OPENMP */
115  for(int b = 0; b < batches; ++b)
116  {
117  for(int yo = 0; yo < dst_height; ++yo)
118  {
119  // Compute input spatial coordinates
120  const int xi = (yo % convolved_dims.first) * stride_x;
121  const int yi = (yo / convolved_dims.first) * stride_y;
122 
123  for(int ci = 0; ci < src_channels; ++ci)
124  {
125  for(int yk = 0; yk < kernel_height; ++yk)
126  {
127  for(int xk = 0; xk < kernel_width; ++xk)
128  {
129  dst[ci + (xk + yk * kernel_width) * src_channels + yo * dst.shape().x() + b * dst.shape().x() * dst.shape().y()] = tensor_elem_at(src, Coordinates(ci, xi + xk - pad_x, yi + yk - pad_y, b),
130  BorderMode::CONSTANT, static_cast<T>(pad_val));
131  }
132  }
133  }
134 
135  if(has_bias)
136  {
137  dst[dst_width + yo * dst.shape().x() + b * dst.shape().x() * dst.shape().y()] = static_cast<T>(1);
138  }
139  }
140  }
141 }
142 
143 template <typename T>
144 void im2col(const SimpleTensor<T> &src, SimpleTensor<T> &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, unsigned int num_groups)
145 {
146  switch(src.data_layout())
147  {
148  case DataLayout::NCHW:
149  {
150  im2col_nchw(src, dst, kernel_dims, conv_info, has_bias, num_groups);
151  break;
152  }
153  case DataLayout::NHWC:
154  {
155  im2col_nhwc(src, dst, kernel_dims, conv_info, has_bias);
156  break;
157  }
158  default:
159  {
160  ARM_COMPUTE_ERROR("Not supported.");
161  break;
162  }
163  }
164 }
165 
166 template void im2col(const SimpleTensor<uint8_t> &src, SimpleTensor<uint8_t> &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, unsigned int num_groups);
167 template void im2col(const SimpleTensor<half> &src, SimpleTensor<half> &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, unsigned int num_groups);
168 template void im2col(const SimpleTensor<float> &src, SimpleTensor<float> &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, unsigned int num_groups);
169 } // namespace reference
170 } // namespace validation
171 } // namespace test
172 } // namespace arm_compute
T tensor_elem_at(const SimpleTensor< T > &src, Coordinates coord, BorderMode border_mode, T constant_border_value)
Definition: Utils.h:64
SimpleTensor< float > b
Definition: DFT.cpp:157
void im2col_nchw(const SimpleTensor< T > &src, SimpleTensor< T > &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, unsigned int num_groups)
Definition: Im2Col.cpp:39
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
unsigned int batches
DataType data_type() const override
Data type of the tensor.
Definition: SimpleTensor.h:357
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
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.
size_t height
Height of the image region or rectangle.
Definition: Size2D.h:90
void im2col(const SimpleTensor< T > &src, SimpleTensor< T > &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, unsigned int num_groups)
Definition: Im2Col.cpp:144
std::pair< unsigned int, unsigned int > scaled_dimensions(int width, int height, int kernel_width, int kernel_height, const PadStrideInfo &pad_stride_info, const Size2D &dilation=Size2D(1U, 1U))
Returns expected width and height of output scaled tensor depending on dimensions rounding mode...
Definition: Utils.cpp:419
const unsigned int num_groups
Definition: Im2Col.cpp:153
Coordinates of an item.
Definition: Coordinates.h:37
std::pair< unsigned int, unsigned int > stride() const
Get the stride.
Definition: Types.h:770
Padding and stride information class.
Definition: Types.h:722
DataLayout data_layout() const override
Data layout of the tensor.
Definition: SimpleTensor.h:351
void im2col_nhwc(const SimpleTensor< T > &src, SimpleTensor< T > &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias)
Definition: Im2Col.cpp:93
Num samples, channels, height, width.
bool is_data_type_quantized_asymmetric(DataType dt)
Check if a given data type is of asymmetric quantized type.
Definition: Utils.h:1190
Simple tensor object that stores elements in a consecutive chunk of memory.
Definition: SimpleTensor.h:58
size_t width
Width of the image region or rectangle.
Definition: Size2D.h:89
Class for specifying the size of an image or rectangle.
Definition: Size2D.h:34
Num samples, height, width, channels.
std::pair< unsigned int, unsigned int > pad() const
Get the padding.
Definition: Types.h:788
QuantizationInfo quantization_info() const override
Quantization info in case of asymmetric quantized type.
Definition: SimpleTensor.h:332