blob: 5685b600262af9922972447633ae77a7fa997a7c [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
26#include "Permute.h"
27
28#include "arm_compute/core/Types.h"
29#include "tests/validation/Helpers.h"
30#include "tests/validation/reference/Utils.h"
31
32namespace arm_compute
33{
34namespace test
35{
36namespace validation
37{
38namespace reference
39{
40template <typename T>
41void im2col_nchw(const SimpleTensor<T> &src, SimpleTensor<T> &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias)
42{
Pablo Tello764b1af2018-04-23 16:11:45 +010043 ARM_COMPUTE_ERROR_ON(src.data_layout() != DataLayout::NCHW);
Giorgio Arena156fcf32018-03-09 15:30:43 +000044 // Create reference
45 const int pad_x = conv_info.pad().first;
46 const int pad_y = conv_info.pad().second;
47 const int stride_x = conv_info.stride().first;
48 const int stride_y = conv_info.stride().second;
49 const int kernel_width = kernel_dims.width;
50 const int kernel_height = kernel_dims.height;
51 const int src_width = src.shape().x();
52 const int src_height = src.shape().y();
53 const int src_depth = src.shape().z();
54 const int batches = src.shape().total_size_upper(3);
55 const int pad_val = is_data_type_quantized_asymmetric(src.data_type()) ? src.quantization_info().offset : 0;
56
57 int dst_idx = 0;
58 for(int b = 0; b < batches; ++b)
59 {
60 for(int y = -pad_y; y <= (src_height + pad_y - kernel_height); y += stride_y)
61 {
62 for(int x = -pad_x; x <= (src_width + pad_x - kernel_width); x += stride_x)
63 {
64 for(int z = 0; z < src_depth; ++z)
65 {
66 for(int patch_y = y; patch_y < (y + kernel_height); ++patch_y)
67 {
68 for(int patch_x = x; patch_x < (x + kernel_width); ++patch_x)
69 {
70 dst[dst_idx++] = tensor_elem_at(src, Coordinates(patch_x, patch_y, z, b), BorderMode::CONSTANT, static_cast<T>(pad_val));
71 }
72 }
73 }
74
75 if(has_bias)
76 {
77 dst[dst_idx++] = static_cast<T>(1);
78 }
79 }
80 }
81 }
82}
83
84template <typename T>
Pablo Tello764b1af2018-04-23 16:11:45 +010085void 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 +000086{
Pablo Tello764b1af2018-04-23 16:11:45 +010087 ARM_COMPUTE_ERROR_ON(src.data_layout() != DataLayout::NHWC);
88 const int pad_x = conv_info.pad().first;
89 const int pad_y = conv_info.pad().second;
90 const int stride_x = conv_info.stride().first;
91 const int stride_y = conv_info.stride().second;
92 const int kernel_width = kernel_dims.width;
93 const int kernel_height = kernel_dims.height;
94 const int src_width = src.shape().y();
95 const int src_height = src.shape().z();
96 const int src_depth = src.shape().x();
97 const int batches = src.shape().total_size_upper(3);
98 const int pad_val = is_data_type_quantized_asymmetric(src.data_type()) ? src.quantization_info().offset : 0;
99 int dst_idx = 0;
100 for(int b = 0; b < batches; ++b)
Giorgio Arena156fcf32018-03-09 15:30:43 +0000101 {
Pablo Tello764b1af2018-04-23 16:11:45 +0100102 for(int y = -pad_y; y <= (src_height + pad_y - kernel_height); y += stride_y)
103 {
104 for(int x = -pad_x; x <= (src_width + pad_x - kernel_width); x += stride_x)
105 {
106 for(int z = 0; z < src_depth; ++z)
107 {
108 for(int patch_y = y; patch_y < (y + kernel_height); ++patch_y)
109 {
110 for(int patch_x = x; patch_x < (x + kernel_width); ++patch_x)
111 {
112 dst[dst_idx++] = tensor_elem_at(src, Coordinates(z, patch_x, patch_y, b), BorderMode::CONSTANT, static_cast<T>(pad_val));
113 }
114 }
115 }
Giorgio Arena156fcf32018-03-09 15:30:43 +0000116
Pablo Tello764b1af2018-04-23 16:11:45 +0100117 if(has_bias)
118 {
119 dst[dst_idx++] = static_cast<T>(1);
120 }
121 }
122 }
123 }
Giorgio Arena156fcf32018-03-09 15:30:43 +0000124}
125
Pablo Tello764b1af2018-04-23 16:11:45 +0100126template <typename T>
127void im2col(const SimpleTensor<T> &src, SimpleTensor<T> &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias)
128{
129 switch(src.data_layout())
130 {
131 case DataLayout::NCHW:
132 {
133 im2col_nchw(src, dst, kernel_dims, conv_info, has_bias);
134 break;
135 }
136 case DataLayout::NHWC:
137 {
138 im2col_nhwc(src, dst, kernel_dims, conv_info, has_bias);
139 break;
140 }
141 default:
142 {
143 ARM_COMPUTE_ERROR("Not supported.");
144 break;
145 }
146 }
147}
148
149template void im2col(const SimpleTensor<uint8_t> &src, SimpleTensor<uint8_t> &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias);
150template void im2col(const SimpleTensor<half> &src, SimpleTensor<half> &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias);
151template void im2col(const SimpleTensor<float> &src, SimpleTensor<float> &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias);
Giorgio Arena156fcf32018-03-09 15:30:43 +0000152} // namespace reference
153} // namespace validation
154} // namespace test
155} // namespace arm_compute