blob: d309b7d5e6bb58ff7e89ad3afcc52b9c97fe8e64 [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{
43 // Create reference
44 const int pad_x = conv_info.pad().first;
45 const int pad_y = conv_info.pad().second;
46 const int stride_x = conv_info.stride().first;
47 const int stride_y = conv_info.stride().second;
48 const int kernel_width = kernel_dims.width;
49 const int kernel_height = kernel_dims.height;
50 const int src_width = src.shape().x();
51 const int src_height = src.shape().y();
52 const int src_depth = src.shape().z();
53 const int batches = src.shape().total_size_upper(3);
54 const int pad_val = is_data_type_quantized_asymmetric(src.data_type()) ? src.quantization_info().offset : 0;
55
56 int dst_idx = 0;
57 for(int b = 0; b < batches; ++b)
58 {
59 for(int y = -pad_y; y <= (src_height + pad_y - kernel_height); y += stride_y)
60 {
61 for(int x = -pad_x; x <= (src_width + pad_x - kernel_width); x += stride_x)
62 {
63 for(int z = 0; z < src_depth; ++z)
64 {
65 for(int patch_y = y; patch_y < (y + kernel_height); ++patch_y)
66 {
67 for(int patch_x = x; patch_x < (x + kernel_width); ++patch_x)
68 {
69 dst[dst_idx++] = tensor_elem_at(src, Coordinates(patch_x, patch_y, z, b), BorderMode::CONSTANT, static_cast<T>(pad_val));
70 }
71 }
72 }
73
74 if(has_bias)
75 {
76 dst[dst_idx++] = static_cast<T>(1);
77 }
78 }
79 }
80 }
81}
82
83template <typename T>
84SimpleTensor<T> im2col(const SimpleTensor<T> &src, const TensorShape &dst_shape, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias)
85{
86 SimpleTensor<T> dst{ dst_shape, src.data_type(), 1, src.fixed_point_position(), src.quantization_info() };
87
88 if(src.data_layout() == DataLayout::NHWC)
89 {
90 SimpleTensor<T> src_nchw = reference::permute<T>(src, PermutationVector(1U, 2U, 0U));
Giorgio Arenaf485a102018-04-20 16:06:21 +010091 im2col_nchw(src_nchw, dst, kernel_dims, conv_info, has_bias);
Giorgio Arena156fcf32018-03-09 15:30:43 +000092 }
Giorgio Arenaf485a102018-04-20 16:06:21 +010093 else
94 {
95 im2col_nchw(src, dst, kernel_dims, conv_info, has_bias);
96 }
Giorgio Arena156fcf32018-03-09 15:30:43 +000097
98 return dst;
99}
100
101template SimpleTensor<uint8_t> im2col(const SimpleTensor<uint8_t> &src, const TensorShape &output_shape, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias);
102template SimpleTensor<half> im2col(const SimpleTensor<half> &src, const TensorShape &output_shape, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias);
103template SimpleTensor<float> im2col(const SimpleTensor<float> &src, const TensorShape &output_shape, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias);
104} // namespace reference
105} // namespace validation
106} // namespace test
107} // namespace arm_compute