blob: 0c41d88f3ec65eb7f74efc319999f675229c375f [file] [log] [blame]
Giorgio Arena156fcf32018-03-09 15:30:43 +00001/*
2 * Copyright (c) 2018 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
Giorgio Arena156fcf32018-03-09 15:30:43 +000026#include "arm_compute/core/Types.h"
27#include "tests/validation/Helpers.h"
28#include "tests/validation/reference/Utils.h"
29
30namespace arm_compute
31{
32namespace test
33{
34namespace validation
35{
36namespace reference
37{
38template <typename T>
Giorgio Arena0f170392018-07-18 16:13:12 +010039void im2col_nchw(const SimpleTensor<T> &src, SimpleTensor<T> &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, unsigned int num_groups)
Giorgio Arena156fcf32018-03-09 15:30:43 +000040{
Pablo Tello764b1af2018-04-23 16:11:45 +010041 ARM_COMPUTE_ERROR_ON(src.data_layout() != DataLayout::NCHW);
Giorgio Arena156fcf32018-03-09 15:30:43 +000042 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;
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010046 const int pad_x = conv_info.pad().first;
47 const int pad_y = conv_info.pad().second;
Giorgio Arena156fcf32018-03-09 15:30:43 +000048 const int src_width = src.shape().x();
49 const int src_height = src.shape().y();
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010050 const int src_channels = src.shape().z();
Giorgio Arena156fcf32018-03-09 15:30:43 +000051 const int batches = src.shape().total_size_upper(3);
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010052 const int dst_height = dst.shape().y();
Giorgio Arena156fcf32018-03-09 15:30:43 +000053 const int pad_val = is_data_type_quantized_asymmetric(src.data_type()) ? src.quantization_info().offset : 0;
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010054 int dst_idx = 0;
Giorgio Arena156fcf32018-03-09 15:30:43 +000055
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010056 // 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);
Pablo Tello4a626a72018-04-04 10:01:14 +010058
Giorgio Arena156fcf32018-03-09 15:30:43 +000059 for(int b = 0; b < batches; ++b)
60 {
Giorgio Arena0f170392018-07-18 16:13:12 +010061 for(int g = 0; g < static_cast<int>(num_groups); ++g)
Giorgio Arena156fcf32018-03-09 15:30:43 +000062 {
Giorgio Arena0f170392018-07-18 16:13:12 +010063 const int first_group_ch = g * (src_channels / num_groups);
64 const int last_group_ch = (g + 1) * (src_channels / num_groups);
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010065
Giorgio Arena0f170392018-07-18 16:13:12 +010066 for(int yo = 0; yo < dst_height; ++yo)
Giorgio Arena156fcf32018-03-09 15:30:43 +000067 {
Giorgio Arena0f170392018-07-18 16:13:12 +010068 // 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)
Giorgio Arena156fcf32018-03-09 15:30:43 +000073 {
Giorgio Arena0f170392018-07-18 16:13:12 +010074 for(int yk = 0; yk < kernel_height; ++yk)
Giorgio Arena156fcf32018-03-09 15:30:43 +000075 {
Giorgio Arena0f170392018-07-18 16:13:12 +010076 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 }
Giorgio Arena156fcf32018-03-09 15:30:43 +000080 }
81 }
82
Giorgio Arena0f170392018-07-18 16:13:12 +010083 if(has_bias)
84 {
85 dst[dst_idx++] = static_cast<T>(1);
86 }
Giorgio Arena156fcf32018-03-09 15:30:43 +000087 }
88 }
89 }
90}
91
92template <typename T>
Pablo Tello764b1af2018-04-23 16:11:45 +010093void im2col_nhwc(const SimpleTensor<T> &src, SimpleTensor<T> &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias)
Giorgio Arena156fcf32018-03-09 15:30:43 +000094{
Pablo Tello764b1af2018-04-23 16:11:45 +010095 ARM_COMPUTE_ERROR_ON(src.data_layout() != DataLayout::NHWC);
96 const int pad_x = conv_info.pad().first;
97 const int pad_y = conv_info.pad().second;
98 const int stride_x = conv_info.stride().first;
99 const int stride_y = conv_info.stride().second;
100 const int kernel_width = kernel_dims.width;
101 const int kernel_height = kernel_dims.height;
102 const int src_width = src.shape().y();
103 const int src_height = src.shape().z();
104 const int src_depth = src.shape().x();
105 const int batches = src.shape().total_size_upper(3);
106 const int pad_val = is_data_type_quantized_asymmetric(src.data_type()) ? src.quantization_info().offset : 0;
107 int dst_idx = 0;
Pablo Tello4a626a72018-04-04 10:01:14 +0100108
109 const int lasty = src_height + (kernel_height > 1 ? pad_y : 0) - kernel_height;
110 const int lastx = src_width + (kernel_width > 1 ? pad_x : 0) - kernel_width;
111
Pablo Tello764b1af2018-04-23 16:11:45 +0100112 for(int b = 0; b < batches; ++b)
Giorgio Arena156fcf32018-03-09 15:30:43 +0000113 {
Pablo Tello4a626a72018-04-04 10:01:14 +0100114 for(int y = -pad_y; y <= lasty; y += stride_y)
Pablo Tello764b1af2018-04-23 16:11:45 +0100115 {
Pablo Tello4a626a72018-04-04 10:01:14 +0100116 for(int x = -pad_x; x <= lastx; x += stride_x)
Pablo Tello764b1af2018-04-23 16:11:45 +0100117 {
118 for(int z = 0; z < src_depth; ++z)
119 {
120 for(int patch_y = y; patch_y < (y + kernel_height); ++patch_y)
121 {
122 for(int patch_x = x; patch_x < (x + kernel_width); ++patch_x)
123 {
124 dst[dst_idx++] = tensor_elem_at(src, Coordinates(z, patch_x, patch_y, b), BorderMode::CONSTANT, static_cast<T>(pad_val));
125 }
126 }
127 }
Giorgio Arena156fcf32018-03-09 15:30:43 +0000128
Pablo Tello764b1af2018-04-23 16:11:45 +0100129 if(has_bias)
130 {
131 dst[dst_idx++] = static_cast<T>(1);
132 }
133 }
134 }
135 }
Giorgio Arena156fcf32018-03-09 15:30:43 +0000136}
137
Pablo Tello764b1af2018-04-23 16:11:45 +0100138template <typename T>
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100139void im2col_nhwc_channel_first(const SimpleTensor<T> &src, SimpleTensor<T> &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias)
140{
141 ARM_COMPUTE_ERROR_ON(src.data_layout() != DataLayout::NHWC);
142 const int stride_x = conv_info.stride().first;
143 const int stride_y = conv_info.stride().second;
144 const int kernel_width = kernel_dims.width;
145 const int kernel_height = kernel_dims.height;
146 const int pad_x = conv_info.pad().first;
147 const int pad_y = conv_info.pad().second;
148 const int src_width = src.shape().y();
149 const int src_height = src.shape().z();
150 const int src_channels = src.shape().x();
151 const int batches = src.shape().total_size_upper(3);
152 const int dst_width = has_bias ? dst.shape().x() - 1 : dst.shape().x();
153 const int dst_height = dst.shape().y();
154 const int pad_val = is_data_type_quantized_asymmetric(src.data_type()) ? src.quantization_info().offset : 0;
155
156 // Compute width and height of the convolved tensors
157 std::pair<unsigned int, unsigned int> convolved_dims = scaled_dimensions(src_width, src_height, kernel_dims.width, kernel_dims.height, conv_info);
158
159 for(int b = 0; b < batches; ++b)
160 {
161 for(int yo = 0; yo < dst_height; ++yo)
162 {
163 // Compute input spatial coordinates
164 const int xi = (yo % convolved_dims.first) * stride_x;
165 const int yi = (yo / convolved_dims.first) * stride_y;
166
167 for(int ci = 0; ci < src_channels; ++ci)
168 {
169 for(int yk = 0; yk < kernel_height; ++yk)
170 {
171 for(int xk = 0; xk < kernel_width; ++xk)
172 {
173 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),
174 BorderMode::CONSTANT, static_cast<T>(pad_val));
175 }
176 }
177 }
178
179 if(has_bias)
180 {
181 dst[dst_width + yo * dst.shape().x() + b * dst.shape().x() * dst.shape().y()] = static_cast<T>(1);
182 }
183 }
184 }
185}
186
187template <typename T>
Giorgio Arena0f170392018-07-18 16:13:12 +0100188void im2col(const SimpleTensor<T> &src, SimpleTensor<T> &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const unsigned int num_groups, bool channels_first_output_nhwc)
Pablo Tello764b1af2018-04-23 16:11:45 +0100189{
190 switch(src.data_layout())
191 {
192 case DataLayout::NCHW:
193 {
Giorgio Arena0f170392018-07-18 16:13:12 +0100194 im2col_nchw(src, dst, kernel_dims, conv_info, has_bias, num_groups);
Pablo Tello764b1af2018-04-23 16:11:45 +0100195 break;
196 }
197 case DataLayout::NHWC:
198 {
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100199 if(channels_first_output_nhwc)
200 {
201 im2col_nhwc_channel_first(src, dst, kernel_dims, conv_info, has_bias);
202 }
203 else
204 {
205 im2col_nhwc(src, dst, kernel_dims, conv_info, has_bias);
206 }
Pablo Tello764b1af2018-04-23 16:11:45 +0100207 break;
208 }
209 default:
210 {
211 ARM_COMPUTE_ERROR("Not supported.");
212 break;
213 }
214 }
215}
216
Giorgio Arena0f170392018-07-18 16:13:12 +0100217template 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,
218 bool channels_first_output_nhwc);
219template void im2col(const SimpleTensor<half> &src, SimpleTensor<half> &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, unsigned int num_groups,
220 bool channels_first_output_nhwc);
221template void im2col(const SimpleTensor<float> &src, SimpleTensor<float> &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, unsigned int num_groups,
222 bool channels_first_output_nhwc);
Giorgio Arena156fcf32018-03-09 15:30:43 +0000223} // namespace reference
224} // namespace validation
225} // namespace test
226} // namespace arm_compute