1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2018-2020 Arm Limited.
3*c217d954SCole Faust *
4*c217d954SCole Faust * SPDX-License-Identifier: MIT
5*c217d954SCole Faust *
6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust *
13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust * copies or substantial portions of the Software.
15*c217d954SCole Faust *
16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust * SOFTWARE.
23*c217d954SCole Faust */
24*c217d954SCole Faust #include "Im2Col.h"
25*c217d954SCole Faust
26*c217d954SCole Faust #include "arm_compute/core/Types.h"
27*c217d954SCole Faust #include "tests/validation/Helpers.h"
28*c217d954SCole Faust #include "tests/validation/reference/Utils.h"
29*c217d954SCole Faust
30*c217d954SCole Faust namespace arm_compute
31*c217d954SCole Faust {
32*c217d954SCole Faust namespace test
33*c217d954SCole Faust {
34*c217d954SCole Faust namespace validation
35*c217d954SCole Faust {
36*c217d954SCole Faust namespace reference
37*c217d954SCole Faust {
38*c217d954SCole Faust template <typename T>
im2col_nchw(const SimpleTensor<T> & src,SimpleTensor<T> & dst,const Size2D & kernel_dims,const PadStrideInfo & conv_info,bool has_bias,unsigned int num_groups)39*c217d954SCole Faust 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*c217d954SCole Faust {
41*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(src.data_layout() != DataLayout::NCHW);
42*c217d954SCole Faust const int stride_x = conv_info.stride().first;
43*c217d954SCole Faust const int stride_y = conv_info.stride().second;
44*c217d954SCole Faust const int kernel_width = kernel_dims.width;
45*c217d954SCole Faust const int kernel_height = kernel_dims.height;
46*c217d954SCole Faust const int pad_x = conv_info.pad().first;
47*c217d954SCole Faust const int pad_y = conv_info.pad().second;
48*c217d954SCole Faust const int src_width = src.shape().x();
49*c217d954SCole Faust const int src_height = src.shape().y();
50*c217d954SCole Faust const int src_channels = src.shape().z();
51*c217d954SCole Faust const int batches = src.shape().total_size_upper(3);
52*c217d954SCole Faust const int dst_height = dst.shape().y();
53*c217d954SCole Faust const int pad_val = is_data_type_quantized_asymmetric(src.data_type()) ? src.quantization_info().uniform().offset : 0;
54*c217d954SCole Faust int dst_idx = 0;
55*c217d954SCole Faust
56*c217d954SCole Faust // Compute width and height of the convolved tensors
57*c217d954SCole Faust std::pair<unsigned int, unsigned int> convolved_dims = scaled_dimensions(src_width, src_height, kernel_dims.width, kernel_dims.height, conv_info);
58*c217d954SCole Faust
59*c217d954SCole Faust for(int b = 0; b < batches; ++b)
60*c217d954SCole Faust {
61*c217d954SCole Faust for(int g = 0; g < static_cast<int>(num_groups); ++g)
62*c217d954SCole Faust {
63*c217d954SCole Faust const int first_group_ch = g * (src_channels / num_groups);
64*c217d954SCole Faust const int last_group_ch = (g + 1) * (src_channels / num_groups);
65*c217d954SCole Faust
66*c217d954SCole Faust for(int yo = 0; yo < dst_height; ++yo)
67*c217d954SCole Faust {
68*c217d954SCole Faust // Compute input spatial coordinates
69*c217d954SCole Faust const int xi = (yo % convolved_dims.first) * stride_x;
70*c217d954SCole Faust const int yi = (yo / convolved_dims.first) * stride_y;
71*c217d954SCole Faust
72*c217d954SCole Faust for(int ci = first_group_ch; ci < last_group_ch; ++ci)
73*c217d954SCole Faust {
74*c217d954SCole Faust for(int yk = 0; yk < kernel_height; ++yk)
75*c217d954SCole Faust {
76*c217d954SCole Faust for(int xk = 0; xk < kernel_width; ++xk)
77*c217d954SCole Faust {
78*c217d954SCole Faust 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*c217d954SCole Faust }
80*c217d954SCole Faust }
81*c217d954SCole Faust }
82*c217d954SCole Faust
83*c217d954SCole Faust if(has_bias)
84*c217d954SCole Faust {
85*c217d954SCole Faust dst[dst_idx++] = static_cast<T>(1);
86*c217d954SCole Faust }
87*c217d954SCole Faust }
88*c217d954SCole Faust }
89*c217d954SCole Faust }
90*c217d954SCole Faust }
91*c217d954SCole Faust
92*c217d954SCole Faust template <typename T>
im2col_nhwc(const SimpleTensor<T> & src,SimpleTensor<T> & dst,const Size2D & kernel_dims,const PadStrideInfo & conv_info,bool has_bias)93*c217d954SCole Faust void im2col_nhwc(const SimpleTensor<T> &src, SimpleTensor<T> &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias)
94*c217d954SCole Faust {
95*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(src.data_layout() != DataLayout::NHWC);
96*c217d954SCole Faust const int stride_x = conv_info.stride().first;
97*c217d954SCole Faust const int stride_y = conv_info.stride().second;
98*c217d954SCole Faust const int kernel_width = kernel_dims.width;
99*c217d954SCole Faust const int kernel_height = kernel_dims.height;
100*c217d954SCole Faust const int pad_x = conv_info.pad().first;
101*c217d954SCole Faust const int pad_y = conv_info.pad().second;
102*c217d954SCole Faust const int src_width = src.shape().y();
103*c217d954SCole Faust const int src_height = src.shape().z();
104*c217d954SCole Faust const int src_channels = src.shape().x();
105*c217d954SCole Faust const int batches = src.shape().total_size_upper(3);
106*c217d954SCole Faust const int dst_width = has_bias ? dst.shape().x() - 1 : dst.shape().x();
107*c217d954SCole Faust const int dst_height = dst.shape().y();
108*c217d954SCole Faust const int pad_val = is_data_type_quantized_asymmetric(src.data_type()) ? src.quantization_info().uniform().offset : 0;
109*c217d954SCole Faust
110*c217d954SCole Faust // Compute width and height of the convolved tensors
111*c217d954SCole Faust std::pair<unsigned int, unsigned int> convolved_dims = scaled_dimensions(src_width, src_height, kernel_dims.width, kernel_dims.height, conv_info);
112*c217d954SCole Faust #if defined(_OPENMP)
113*c217d954SCole Faust #pragma omp parallel for schedule(dynamic, 1) collapse(2)
114*c217d954SCole Faust #endif /* _OPENMP */
115*c217d954SCole Faust for(int b = 0; b < batches; ++b)
116*c217d954SCole Faust {
117*c217d954SCole Faust for(int yo = 0; yo < dst_height; ++yo)
118*c217d954SCole Faust {
119*c217d954SCole Faust // Compute input spatial coordinates
120*c217d954SCole Faust const int xi = (yo % convolved_dims.first) * stride_x;
121*c217d954SCole Faust const int yi = (yo / convolved_dims.first) * stride_y;
122*c217d954SCole Faust
123*c217d954SCole Faust for(int ci = 0; ci < src_channels; ++ci)
124*c217d954SCole Faust {
125*c217d954SCole Faust for(int yk = 0; yk < kernel_height; ++yk)
126*c217d954SCole Faust {
127*c217d954SCole Faust for(int xk = 0; xk < kernel_width; ++xk)
128*c217d954SCole Faust {
129*c217d954SCole Faust 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*c217d954SCole Faust BorderMode::CONSTANT, static_cast<T>(pad_val));
131*c217d954SCole Faust }
132*c217d954SCole Faust }
133*c217d954SCole Faust }
134*c217d954SCole Faust
135*c217d954SCole Faust if(has_bias)
136*c217d954SCole Faust {
137*c217d954SCole Faust dst[dst_width + yo * dst.shape().x() + b * dst.shape().x() * dst.shape().y()] = static_cast<T>(1);
138*c217d954SCole Faust }
139*c217d954SCole Faust }
140*c217d954SCole Faust }
141*c217d954SCole Faust }
142*c217d954SCole Faust
143*c217d954SCole Faust template <typename T>
im2col(const SimpleTensor<T> & src,SimpleTensor<T> & dst,const Size2D & kernel_dims,const PadStrideInfo & conv_info,bool has_bias,unsigned int num_groups)144*c217d954SCole Faust 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*c217d954SCole Faust {
146*c217d954SCole Faust switch(src.data_layout())
147*c217d954SCole Faust {
148*c217d954SCole Faust case DataLayout::NCHW:
149*c217d954SCole Faust {
150*c217d954SCole Faust im2col_nchw(src, dst, kernel_dims, conv_info, has_bias, num_groups);
151*c217d954SCole Faust break;
152*c217d954SCole Faust }
153*c217d954SCole Faust case DataLayout::NHWC:
154*c217d954SCole Faust {
155*c217d954SCole Faust im2col_nhwc(src, dst, kernel_dims, conv_info, has_bias);
156*c217d954SCole Faust break;
157*c217d954SCole Faust }
158*c217d954SCole Faust default:
159*c217d954SCole Faust {
160*c217d954SCole Faust ARM_COMPUTE_ERROR("Not supported.");
161*c217d954SCole Faust break;
162*c217d954SCole Faust }
163*c217d954SCole Faust }
164*c217d954SCole Faust }
165*c217d954SCole Faust
166*c217d954SCole Faust 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*c217d954SCole Faust 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*c217d954SCole Faust 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*c217d954SCole Faust } // namespace reference
170*c217d954SCole Faust } // namespace validation
171*c217d954SCole Faust } // namespace test
172*c217d954SCole Faust } // namespace arm_compute
173