Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 1 | /* |
| 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 Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/Types.h" |
| 27 | #include "tests/validation/Helpers.h" |
| 28 | #include "tests/validation/reference/Utils.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) |
| 40 | { |
Pablo Tello | 764b1af | 2018-04-23 16:11:45 +0100 | [diff] [blame] | 41 | ARM_COMPUTE_ERROR_ON(src.data_layout() != DataLayout::NCHW); |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 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; |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 46 | const int pad_x = conv_info.pad().first; |
| 47 | const int pad_y = conv_info.pad().second; |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 48 | const int src_width = src.shape().x(); |
| 49 | const int src_height = src.shape().y(); |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 50 | const int src_channels = src.shape().z(); |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 51 | const int batches = src.shape().total_size_upper(3); |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 52 | const int dst_height = dst.shape().y(); |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 53 | const int pad_val = is_data_type_quantized_asymmetric(src.data_type()) ? src.quantization_info().offset : 0; |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 54 | int dst_idx = 0; |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 55 | |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 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); |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame] | 58 | |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 59 | for(int b = 0; b < batches; ++b) |
| 60 | { |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 61 | for(int yo = 0; yo < dst_height; ++yo) |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 62 | { |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 63 | // Compute input spatial coordinates |
| 64 | const int xi = (yo % convolved_dims.first) * stride_x; |
| 65 | const int yi = (yo / convolved_dims.first) * stride_y; |
| 66 | |
| 67 | for(int ci = 0; ci < src_channels; ++ci) |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 68 | { |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 69 | for(int yk = 0; yk < kernel_height; ++yk) |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 70 | { |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 71 | for(int xk = 0; xk < kernel_width; ++xk) |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 72 | { |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 73 | 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)); |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 74 | } |
| 75 | } |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 76 | } |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 77 | |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 78 | if(has_bias) |
| 79 | { |
| 80 | dst[dst_idx++] = static_cast<T>(1); |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | template <typename T> |
Pablo Tello | 764b1af | 2018-04-23 16:11:45 +0100 | [diff] [blame] | 87 | void im2col_nhwc(const SimpleTensor<T> &src, SimpleTensor<T> &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias) |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 88 | { |
Pablo Tello | 764b1af | 2018-04-23 16:11:45 +0100 | [diff] [blame] | 89 | ARM_COMPUTE_ERROR_ON(src.data_layout() != DataLayout::NHWC); |
| 90 | const int pad_x = conv_info.pad().first; |
| 91 | const int pad_y = conv_info.pad().second; |
| 92 | const int stride_x = conv_info.stride().first; |
| 93 | const int stride_y = conv_info.stride().second; |
| 94 | const int kernel_width = kernel_dims.width; |
| 95 | const int kernel_height = kernel_dims.height; |
| 96 | const int src_width = src.shape().y(); |
| 97 | const int src_height = src.shape().z(); |
| 98 | const int src_depth = src.shape().x(); |
| 99 | const int batches = src.shape().total_size_upper(3); |
| 100 | const int pad_val = is_data_type_quantized_asymmetric(src.data_type()) ? src.quantization_info().offset : 0; |
| 101 | int dst_idx = 0; |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame] | 102 | |
| 103 | const int lasty = src_height + (kernel_height > 1 ? pad_y : 0) - kernel_height; |
| 104 | const int lastx = src_width + (kernel_width > 1 ? pad_x : 0) - kernel_width; |
| 105 | |
Pablo Tello | 764b1af | 2018-04-23 16:11:45 +0100 | [diff] [blame] | 106 | for(int b = 0; b < batches; ++b) |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 107 | { |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame] | 108 | for(int y = -pad_y; y <= lasty; y += stride_y) |
Pablo Tello | 764b1af | 2018-04-23 16:11:45 +0100 | [diff] [blame] | 109 | { |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame] | 110 | for(int x = -pad_x; x <= lastx; x += stride_x) |
Pablo Tello | 764b1af | 2018-04-23 16:11:45 +0100 | [diff] [blame] | 111 | { |
| 112 | for(int z = 0; z < src_depth; ++z) |
| 113 | { |
| 114 | for(int patch_y = y; patch_y < (y + kernel_height); ++patch_y) |
| 115 | { |
| 116 | for(int patch_x = x; patch_x < (x + kernel_width); ++patch_x) |
| 117 | { |
| 118 | dst[dst_idx++] = tensor_elem_at(src, Coordinates(z, patch_x, patch_y, b), BorderMode::CONSTANT, static_cast<T>(pad_val)); |
| 119 | } |
| 120 | } |
| 121 | } |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 122 | |
Pablo Tello | 764b1af | 2018-04-23 16:11:45 +0100 | [diff] [blame] | 123 | if(has_bias) |
| 124 | { |
| 125 | dst[dst_idx++] = static_cast<T>(1); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | } |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Pablo Tello | 764b1af | 2018-04-23 16:11:45 +0100 | [diff] [blame] | 132 | template <typename T> |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 133 | void im2col_nhwc_channel_first(const SimpleTensor<T> &src, SimpleTensor<T> &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias) |
| 134 | { |
| 135 | ARM_COMPUTE_ERROR_ON(src.data_layout() != DataLayout::NHWC); |
| 136 | const int stride_x = conv_info.stride().first; |
| 137 | const int stride_y = conv_info.stride().second; |
| 138 | const int kernel_width = kernel_dims.width; |
| 139 | const int kernel_height = kernel_dims.height; |
| 140 | const int pad_x = conv_info.pad().first; |
| 141 | const int pad_y = conv_info.pad().second; |
| 142 | const int src_width = src.shape().y(); |
| 143 | const int src_height = src.shape().z(); |
| 144 | const int src_channels = src.shape().x(); |
| 145 | const int batches = src.shape().total_size_upper(3); |
| 146 | const int dst_width = has_bias ? dst.shape().x() - 1 : dst.shape().x(); |
| 147 | const int dst_height = dst.shape().y(); |
| 148 | const int pad_val = is_data_type_quantized_asymmetric(src.data_type()) ? src.quantization_info().offset : 0; |
| 149 | |
| 150 | // Compute width and height of the convolved tensors |
| 151 | std::pair<unsigned int, unsigned int> convolved_dims = scaled_dimensions(src_width, src_height, kernel_dims.width, kernel_dims.height, conv_info); |
| 152 | |
| 153 | for(int b = 0; b < batches; ++b) |
| 154 | { |
| 155 | for(int yo = 0; yo < dst_height; ++yo) |
| 156 | { |
| 157 | // Compute input spatial coordinates |
| 158 | const int xi = (yo % convolved_dims.first) * stride_x; |
| 159 | const int yi = (yo / convolved_dims.first) * stride_y; |
| 160 | |
| 161 | for(int ci = 0; ci < src_channels; ++ci) |
| 162 | { |
| 163 | for(int yk = 0; yk < kernel_height; ++yk) |
| 164 | { |
| 165 | for(int xk = 0; xk < kernel_width; ++xk) |
| 166 | { |
| 167 | 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), |
| 168 | BorderMode::CONSTANT, static_cast<T>(pad_val)); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | if(has_bias) |
| 174 | { |
| 175 | dst[dst_width + yo * dst.shape().x() + b * dst.shape().x() * dst.shape().y()] = static_cast<T>(1); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | template <typename T> |
| 182 | void im2col(const SimpleTensor<T> &src, SimpleTensor<T> &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, bool channels_first_output_nhwc) |
Pablo Tello | 764b1af | 2018-04-23 16:11:45 +0100 | [diff] [blame] | 183 | { |
| 184 | switch(src.data_layout()) |
| 185 | { |
| 186 | case DataLayout::NCHW: |
| 187 | { |
| 188 | im2col_nchw(src, dst, kernel_dims, conv_info, has_bias); |
| 189 | break; |
| 190 | } |
| 191 | case DataLayout::NHWC: |
| 192 | { |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 193 | if(channels_first_output_nhwc) |
| 194 | { |
| 195 | im2col_nhwc_channel_first(src, dst, kernel_dims, conv_info, has_bias); |
| 196 | } |
| 197 | else |
| 198 | { |
| 199 | im2col_nhwc(src, dst, kernel_dims, conv_info, has_bias); |
| 200 | } |
Pablo Tello | 764b1af | 2018-04-23 16:11:45 +0100 | [diff] [blame] | 201 | break; |
| 202 | } |
| 203 | default: |
| 204 | { |
| 205 | ARM_COMPUTE_ERROR("Not supported."); |
| 206 | break; |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 211 | template void im2col(const SimpleTensor<uint8_t> &src, SimpleTensor<uint8_t> &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, bool channels_first_output_nhwc); |
| 212 | template void im2col(const SimpleTensor<half> &src, SimpleTensor<half> &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, bool channels_first_output_nhwc); |
| 213 | template void im2col(const SimpleTensor<float> &src, SimpleTensor<float> &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, bool channels_first_output_nhwc); |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 214 | } // namespace reference |
| 215 | } // namespace validation |
| 216 | } // namespace test |
| 217 | } // namespace arm_compute |