Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +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 "Winograd.h" |
| 25 | |
| 26 | #include "tests/validation/Helpers.h" |
| 27 | #include "tests/validation/reference/Utils.h" |
| 28 | |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/Types.h" |
| 30 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 31 | #include <algorithm> |
| 32 | |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 33 | namespace arm_compute |
| 34 | { |
| 35 | namespace test |
| 36 | { |
| 37 | namespace validation |
| 38 | { |
| 39 | namespace reference |
| 40 | { |
| 41 | namespace |
| 42 | { |
| 43 | template <typename T> |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 44 | void initialize_matrix_transform(SimpleTensor<T> &src, const Size2D &output_tile_size, const Size2D &kernel_size, WinogradTransformType winograd_transform_type) |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame] | 45 | { |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 46 | ARM_COMPUTE_ERROR_ON((output_tile_size != Size2D(2U, 2U)) && (output_tile_size != Size2D(4U, 4U))); |
| 47 | ARM_COMPUTE_ERROR_ON(kernel_size != Size2D(3U, 3U)); |
Giorgio Arena | 2d9de0a | 2018-03-15 17:58:20 +0000 | [diff] [blame] | 48 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 49 | // Winograd input transform matrices |
| 50 | static const float imatrix2x2_3x3[] = |
Giorgio Arena | 2d9de0a | 2018-03-15 17:58:20 +0000 | [diff] [blame] | 51 | { |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 52 | 1.0f, 0.0f, -1.0f, 0.0f, |
| 53 | 0.0f, 1.0f, 1.0f, 0.0f, |
| 54 | 0.0f, -1.0f, 1.0f, 0.0f, |
| 55 | 0.0f, 1.0f, 0.0f, -1.0f |
| 56 | }; |
| 57 | |
| 58 | static const float imatrix4x4_3x3[] = |
| 59 | { |
| 60 | 4.0f, 0.0f, -5.0f, 0.0f, 1.0f, 0.0f, |
| 61 | 0.0f, -4.0f, -4.0f, 1.0f, 1.0f, 0.0f, |
| 62 | 0.0f, 4.0f, -4.0f, -1.0f, 1.0f, 0.0f, |
| 63 | 0.0f, -2.0f, -1.0f, 2.0f, 1.0f, 0.0f, |
| 64 | 0.0f, 2.0f, -1.0f, -2.0f, 1.0f, 0.0f, |
| 65 | 0.0f, 4.0f, 0.0f, -5.0f, 0.0f, 1.0f, |
| 66 | }; |
| 67 | |
| 68 | // ------------------------------------------ |
| 69 | |
| 70 | // Winograd filter transform matrices |
| 71 | static const float fmatrix2x2_3x3[] = |
| 72 | { |
| 73 | 1.0f, 0.0f, 0.0f, |
| 74 | 0.5f, 0.5f, 0.5f, |
| 75 | 0.5f, -0.5f, 0.5f, |
| 76 | 0.0f, 0.0f, 1.0f |
| 77 | }; |
| 78 | |
| 79 | static const float fmatrix4x4_3x3[] = |
| 80 | { |
| 81 | 0.25f, 0.0f, 0.0f, |
| 82 | -1.0f / 6.0f, -1.0f / 6.0f, -1.0f / 6.0f, |
| 83 | -1.0f / 6.0f, 1.0f / 6.0f, -1.0f / 6.0f, |
| 84 | 1.0f / 24.0f, 1.0f / 12.0f, 1.0f / 6.0f, |
| 85 | 1.0f / 24.0f, -1.0f / 12.0f, 1.0f / 6.0f, |
| 86 | 0.0f, 0.0f, 1.0f |
| 87 | }; |
| 88 | |
| 89 | // ------------------------------------------ |
| 90 | |
| 91 | // Winograd output transform matrices |
| 92 | static const float omatrix2x2_3x3[] = |
| 93 | { |
| 94 | 1.0f, 1.0f, 1.0f, 0.0f, |
| 95 | 0.0f, 1.0f, -1.0f, -1.0f |
| 96 | }; |
| 97 | |
| 98 | static const float omatrix4x4_3x3[] = |
| 99 | { |
| 100 | 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, |
| 101 | 0.0f, 1.0f, -1.0f, 2.0f, -2.0f, 0.0f, |
| 102 | 0.0f, 1.0f, 1.0f, 4.0f, 4.0f, 0.0f, |
| 103 | 0.0f, 1.0f, -1.0f, 8.0f, -8.0f, 1.0f |
| 104 | }; |
| 105 | |
| 106 | // ------------------------------------------ |
| 107 | |
| 108 | using WinogradKey = std::tuple<std::pair<int, int>, std::pair<int, int>, WinogradTransformType>; |
| 109 | |
| 110 | // Key = (Output tile size, Kernel size, Winograd transform type) |
| 111 | static std::map<WinogradKey, const float *> matrix_map = |
| 112 | { |
| 113 | { WinogradKey(std::pair<int, int>(2, 2), std::pair<int, int>(3, 3), WinogradTransformType::INPUT), imatrix2x2_3x3 }, |
| 114 | { WinogradKey(std::pair<int, int>(4, 4), std::pair<int, int>(3, 3), WinogradTransformType::INPUT), imatrix4x4_3x3 }, |
| 115 | { WinogradKey(std::pair<int, int>(2, 2), std::pair<int, int>(3, 3), WinogradTransformType::FILTER), fmatrix2x2_3x3 }, |
| 116 | { WinogradKey(std::pair<int, int>(4, 4), std::pair<int, int>(3, 3), WinogradTransformType::FILTER), fmatrix4x4_3x3 }, |
| 117 | { WinogradKey(std::pair<int, int>(2, 2), std::pair<int, int>(3, 3), WinogradTransformType::OUTPUT), omatrix2x2_3x3 }, |
| 118 | { WinogradKey(std::pair<int, int>(4, 4), std::pair<int, int>(3, 3), WinogradTransformType::OUTPUT), omatrix4x4_3x3 }, |
| 119 | }; |
| 120 | |
| 121 | // Find input matrix transform |
| 122 | std::map<WinogradKey, const float *>::iterator it; |
| 123 | |
| 124 | it = matrix_map.find(WinogradKey(std::pair<int, int>(output_tile_size.width, output_tile_size.height), |
| 125 | std::pair<int, int>(kernel_size.width, kernel_size.height), |
| 126 | winograd_transform_type)); |
| 127 | |
| 128 | float const *matrix_values = nullptr; |
| 129 | if(it != matrix_map.end()) |
| 130 | { |
| 131 | // Get matrix pointer |
| 132 | matrix_values = it->second; |
Giorgio Arena | 2d9de0a | 2018-03-15 17:58:20 +0000 | [diff] [blame] | 133 | } |
| 134 | else |
| 135 | { |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 136 | ARM_COMPUTE_ERROR("Winograd configuration not supported"); |
Giorgio Arena | 2d9de0a | 2018-03-15 17:58:20 +0000 | [diff] [blame] | 137 | } |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame] | 138 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 139 | // Copy values |
| 140 | std::copy(&matrix_values[0], &matrix_values[0] + src.num_elements(), &src[0]); |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame] | 141 | } |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 142 | } // namespace |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 143 | |
| 144 | template <typename T> |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 145 | SimpleTensor<T> winograd_input_transform(const SimpleTensor<T> &in, const TensorShape &output_shape, const WinogradInfo &winograd_info) |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 146 | { |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 147 | ARM_COMPUTE_ERROR_ON(in.data_layout() != DataLayout::NCHW); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 148 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 149 | const PadStrideInfo conv_info = winograd_info.convolution_info; |
| 150 | const Size2D output_tile_size = winograd_info.output_tile_size; |
| 151 | const Size2D kernel_size = winograd_info.kernel_size; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 152 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 153 | SimpleTensor<T> out{ output_shape, in.data_type() }; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 154 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 155 | // Calculate dimensions for the tile |
| 156 | const unsigned int tile_w = output_tile_size.width + kernel_size.width - 1; |
| 157 | const unsigned int tile_h = output_tile_size.height + kernel_size.height - 1; |
| 158 | |
| 159 | TensorShape tile_dims(tile_w, tile_h); |
| 160 | |
| 161 | // Simple tensor for the input tile |
| 162 | SimpleTensor<T> src_tile{ tile_dims, in.data_type() }; |
| 163 | |
| 164 | // Simple tensor for the temporary tile |
| 165 | SimpleTensor<T> tmp_tile{ tile_dims, in.data_type() }; |
| 166 | |
| 167 | // Simple tensor for the output tile |
| 168 | SimpleTensor<T> dst_tile{ tile_dims, in.data_type() }; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 169 | |
| 170 | // Simple tensor for the transformation matrix |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 171 | SimpleTensor<T> matrix{ tile_dims, in.data_type() }; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 172 | |
| 173 | // Simple tensor for the transformation matrix transposed |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 174 | SimpleTensor<T> matrix_transposed{ tile_dims, in.data_type() }; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 175 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 176 | // Initialize matrix for the input transform |
| 177 | initialize_matrix_transform(matrix, output_tile_size, kernel_size, WinogradTransformType::INPUT); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 178 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 179 | // Transpose matrix |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 180 | transpose_matrix(matrix, matrix_transposed); |
| 181 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 182 | const int in_w = in.shape().x(); |
| 183 | const int in_h = in.shape().y(); |
| 184 | const int in_d = in.shape().z(); |
| 185 | const int out_d = out.shape().z(); |
| 186 | const int num_batches = in.shape().total_size() / (in_w * in_h * in_d); |
| 187 | const int num_tiles_x = std::ceil((in_w - (kernel_size.width - 1) + conv_info.pad_left() + conv_info.pad_right()) / static_cast<float>(output_tile_size.width)); |
| 188 | const int num_tiles_y = std::ceil((in_h - (kernel_size.height - 1) + conv_info.pad_top() + conv_info.pad_bottom()) / static_cast<float>(output_tile_size.height)); |
| 189 | const int step_x = output_tile_size.width; |
| 190 | const int step_y = output_tile_size.height; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 191 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 192 | ARM_COMPUTE_ERROR_ON((num_tiles_x * num_tiles_y) != static_cast<int>(out.shape().y())); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 193 | |
| 194 | for(int b = 0; b < num_batches; ++b) |
| 195 | { |
| 196 | for(int z = 0; z < in_d; ++z) |
| 197 | { |
| 198 | for(int y = 0; y < num_tiles_y; ++y) |
| 199 | { |
| 200 | for(int x = 0; x < num_tiles_x; ++x) |
| 201 | { |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 202 | int xi = x * step_x - conv_info.pad_left(); |
| 203 | int yi = y * step_y - conv_info.pad_top(); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 204 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 205 | // Get the tile from the input tensor |
| 206 | get_tile(in, src_tile, Coordinates(xi, yi, z, b)); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 207 | |
| 208 | // Compute the transformation |
| 209 | matrix_multiply(matrix, src_tile, tmp_tile); |
| 210 | matrix_multiply(tmp_tile, matrix_transposed, dst_tile); |
| 211 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 212 | // Store the output tile across the channels |
| 213 | for(int i = 0; i < out_d; ++i) |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 214 | { |
| 215 | int xo = z; |
| 216 | int yo = x + y * num_tiles_x; |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 217 | out[coords2index(out.shape(), Coordinates(xo, yo, i, b))] = dst_tile[i]; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | } |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 223 | |
| 224 | return out; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | template <typename T> |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 228 | SimpleTensor<T> winograd_filter_transform(const SimpleTensor<T> &in, const TensorShape &output_shape, const WinogradInfo &winograd_info) |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 229 | { |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 230 | ARM_COMPUTE_ERROR_ON_MSG(in.data_layout() != DataLayout::NCHW, "Only supported NCHW data format"); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 231 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 232 | // Create reference |
| 233 | SimpleTensor<T> out{ output_shape, in.data_type(), 1 }; |
| 234 | |
| 235 | const Size2D output_tile_size = winograd_info.output_tile_size; |
| 236 | const Size2D kernel_size = winograd_info.kernel_size; |
| 237 | |
| 238 | TensorShape kernel_tile_dims(kernel_size.width, kernel_size.height); |
| 239 | |
| 240 | // Calculate dimensions for the tile |
| 241 | const unsigned int input_tile_w = output_tile_size.width + kernel_size.width - 1; |
| 242 | const unsigned int input_tile_h = output_tile_size.height + kernel_size.height - 1; |
| 243 | const unsigned int input_tile_area = input_tile_w * input_tile_h; |
| 244 | |
| 245 | // Simple tensor for the input tile |
| 246 | SimpleTensor<T> input_tile{ kernel_tile_dims, in.data_type(), 1 }; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 247 | |
| 248 | // Simple tensor for the transformation matrix |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 249 | SimpleTensor<T> trans_matrix{ TensorShape(kernel_tile_dims[0], input_tile_w), in.data_type(), 1 }; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 250 | |
| 251 | // Simple tensor for the transformation matrix transpose |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 252 | SimpleTensor<T> trans_matrix_transposed{ TensorShape(input_tile_w, kernel_tile_dims[0]), in.data_type(), 1 }; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 253 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 254 | // Simple tensor for the temporary tile |
| 255 | SimpleTensor<T> tmp_tile{ TensorShape(kernel_tile_dims[0], input_tile_w), in.data_type(), 1 }; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 256 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 257 | // Simple tensor for the output tile |
| 258 | SimpleTensor<T> transf_tile{ TensorShape(input_tile_w, input_tile_w), in.data_type(), 1 }; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 259 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 260 | // Initialize matrix for the filter transform |
| 261 | initialize_matrix_transform(trans_matrix, output_tile_size, kernel_size, WinogradTransformType::FILTER); |
| 262 | |
| 263 | // Transpose the transformation matrix |
| 264 | transpose_matrix(trans_matrix, trans_matrix_transposed); |
| 265 | |
| 266 | const int num_channels = in.shape()[2]; |
| 267 | const int num_filters = in.shape()[3]; |
| 268 | const int num_batches = in.shape().total_size() / (kernel_size.area() * num_channels * num_filters); |
| 269 | |
| 270 | for(int n = 0; n < num_batches; ++n) |
| 271 | { |
| 272 | for(int w = 0; w < num_filters; ++w) |
| 273 | { |
| 274 | for(int z = 0; z < num_channels; ++z) |
| 275 | { |
| 276 | // Load the tile from the input tensor |
| 277 | get_tile(in, input_tile, Coordinates(0, 0, z, w, n)); |
| 278 | |
| 279 | // First transformation |
| 280 | matrix_multiply(trans_matrix, input_tile, tmp_tile); |
| 281 | |
| 282 | // Second transformation |
| 283 | matrix_multiply(tmp_tile, trans_matrix_transposed, transf_tile); |
| 284 | |
| 285 | // Store the output tile across the channels |
| 286 | const int output_offset = w + z * num_filters; |
| 287 | |
| 288 | // Store the values across the channels |
| 289 | for(unsigned int i = 0; i < input_tile_area; ++i) |
| 290 | { |
| 291 | out[output_offset + i * num_filters * num_channels] = transf_tile[i]; |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | return out; |
| 298 | } |
| 299 | |
| 300 | template <typename T> |
| 301 | SimpleTensor<T> winograd_output_transform(const SimpleTensor<T> &in, const TensorShape &output_shape, const WinogradInfo &winograd_info) |
| 302 | { |
| 303 | ARM_COMPUTE_ERROR_ON_MSG(winograd_info.output_data_layout != DataLayout::NCHW, "Only supported NCHW data format"); |
| 304 | |
| 305 | const PadStrideInfo conv_info = winograd_info.convolution_info; |
| 306 | const Size2D input_dimensions = winograd_info.input_dimensions; |
| 307 | const Size2D output_tile_size = winograd_info.output_tile_size; |
| 308 | const Size2D kernel_size = winograd_info.kernel_size; |
| 309 | |
| 310 | // Create reference |
| 311 | SimpleTensor<T> out{ output_shape, in.data_type(), 1 }; |
| 312 | |
| 313 | // Calculate dimensions for the tiles |
| 314 | const unsigned int in_tile_w = output_tile_size.width + kernel_size.width - 1; |
| 315 | const unsigned int in_tile_h = output_tile_size.height + kernel_size.height - 1; |
| 316 | const unsigned int out_tile_w = output_tile_size.width; |
| 317 | const unsigned int out_tile_h = output_tile_size.height; |
| 318 | |
| 319 | ARM_COMPUTE_ERROR_ON(in.shape()[2] != (in_tile_w * in_tile_h)); |
| 320 | ARM_COMPUTE_ERROR_ON(in.shape()[0] != out.shape()[2]); |
| 321 | |
| 322 | // Compute tile dimensions |
| 323 | // Input tile dimensions |
| 324 | TensorShape in_tile_dims(in_tile_w, in_tile_h); |
| 325 | |
| 326 | // Output tile dimensions |
| 327 | TensorShape out_tile_dims(output_tile_size.width, output_tile_size.height); |
| 328 | |
| 329 | // Transformation matrix dimensions |
| 330 | TensorShape tr_tile_dims(in_tile_w, output_tile_size.width); |
| 331 | |
| 332 | // Create tensors |
| 333 | // Simple tensor for the input tile |
| 334 | SimpleTensor<T> input_tile{ in_tile_dims, in.data_type(), 1 }; |
| 335 | |
| 336 | // Simple tensor for the transformation matrix |
| 337 | SimpleTensor<T> trans_matrix{ tr_tile_dims, in.data_type(), 1 }; |
| 338 | |
| 339 | // Simple tensor for the transformation matrix transpose |
| 340 | SimpleTensor<T> trans_matrix_transposed{ TensorShape(tr_tile_dims[1], tr_tile_dims[0]), in.data_type(), 1 }; |
| 341 | |
| 342 | // Simple tensor for the temporary tile |
| 343 | SimpleTensor<T> tmp_tile{ tr_tile_dims, in.data_type(), 1 }; |
| 344 | |
| 345 | // Simple tensor for the output tile |
| 346 | SimpleTensor<T> output_tile{ out_tile_dims, in.data_type(), 1 }; |
| 347 | |
| 348 | // Initialize matrix for the output transform |
| 349 | initialize_matrix_transform(trans_matrix, output_tile_size, kernel_size, WinogradTransformType::OUTPUT); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 350 | |
| 351 | // Transpose the transformation matrix |
| 352 | transpose_matrix(trans_matrix, trans_matrix_transposed); |
| 353 | |
| 354 | const int w_in = in.shape()[0]; |
| 355 | const int h_in = in.shape()[1]; |
| 356 | const int c_in = in.shape()[2]; |
| 357 | const int w_out = out.shape()[0]; |
| 358 | const int h_out = out.shape()[1]; |
| 359 | const int c_out = out.shape()[2]; |
| 360 | const int num_batches = in.shape().total_size() / (w_in * h_in * c_in); |
| 361 | |
| 362 | // Input strides |
| 363 | const int stridey_in = w_in; |
| 364 | const int stridez_in = stridey_in * h_in; |
| 365 | const int stridew_in = stridez_in * c_in; |
| 366 | |
| 367 | // Output strides |
| 368 | const int stridey_out = w_out; |
| 369 | const int stridez_out = stridey_out * h_out; |
| 370 | const int stridew_out = stridez_out * c_out; |
| 371 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 372 | // Compute number of elements to process in the X and Y direction |
| 373 | const int num_elements_x = input_dimensions.width - (kernel_size.width - 1) + conv_info.pad_left() + conv_info.pad_right(); |
| 374 | const int num_elements_y = input_dimensions.height - (kernel_size.height - 1) + conv_info.pad_top() + conv_info.pad_bottom(); |
| 375 | const int num_tiles_x = std::ceil(num_elements_x / static_cast<float>(output_tile_size.width)); |
| 376 | const int num_tiles_y = std::ceil(num_elements_y / static_cast<float>(output_tile_size.height)); |
| 377 | |
| 378 | ARM_COMPUTE_UNUSED(num_tiles_y); |
| 379 | ARM_COMPUTE_ERROR_ON(in.shape()[1] != static_cast<unsigned int>(num_tiles_x * num_tiles_y)); |
| 380 | |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 381 | for(int n = 0; n < num_batches; ++n) |
| 382 | { |
| 383 | for(int y = 0; y < h_in; ++y) |
| 384 | { |
| 385 | for(int x = 0; x < w_in; ++x) |
| 386 | { |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 387 | // Load the input tile tile across the channels of the input tensor |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 388 | for(int z = 0; z < c_in; ++z) |
| 389 | { |
| 390 | input_tile[z] = in[x + (y * stridey_in) + (z * stridez_in) + (n * stridew_in)]; |
| 391 | } |
| 392 | |
| 393 | // First transformation |
| 394 | matrix_multiply(trans_matrix, input_tile, tmp_tile); |
| 395 | |
| 396 | // Second transformation |
| 397 | matrix_multiply(tmp_tile, trans_matrix_transposed, output_tile); |
| 398 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 399 | // Store the output tile |
| 400 | const int xo = (y % num_tiles_x) * out_tile_w; |
| 401 | const int yo = (y / num_tiles_x) * out_tile_h; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 402 | const int zo = x; |
| 403 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 404 | const int output_offset = xo + (yo * stridey_out) + (zo * stridez_out) + (n * stridew_out); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 405 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 406 | for(int yi = 0; yi < static_cast<int>(out_tile_h); ++yi) |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 407 | { |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 408 | for(int xi = 0; xi < static_cast<int>(out_tile_w); ++xi) |
| 409 | { |
| 410 | // Check out-of-bound writes |
| 411 | if((xo + xi < w_out) && (yo + yi < h_out)) |
| 412 | { |
| 413 | out[output_offset + yi * stridey_out + xi] = output_tile[xi + yi * out_tile_w]; |
| 414 | } |
| 415 | } |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | } |
| 419 | } |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame] | 420 | |
| 421 | return out; |
| 422 | } |
| 423 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame^] | 424 | template SimpleTensor<float> winograd_filter_transform(const SimpleTensor<float> &in, const TensorShape &output_shape, const WinogradInfo &winograd_info); |
| 425 | template SimpleTensor<float> winograd_input_transform(const SimpleTensor<float> &in, const TensorShape &output_shape, const WinogradInfo &winograd_info); |
| 426 | template SimpleTensor<float> winograd_output_transform(const SimpleTensor<float> &in, const TensorShape &output_shape, const WinogradInfo &winograd_info); |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 427 | } // namespace reference |
| 428 | } // namespace validation |
| 429 | } // namespace test |
| 430 | } // namespace arm_compute |