blob: 5be4fe274b2e2af167e547535f5005a3e78a17aa [file] [log] [blame]
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +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 "Winograd.h"
25
26#include "tests/validation/Helpers.h"
27#include "tests/validation/reference/Utils.h"
28
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +000029#include "arm_compute/core/Types.h"
30
Gian Marco Iodice247f52c2018-03-22 11:24:56 +000031#include <algorithm>
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +010032#include <cmath>
Gian Marco Iodice247f52c2018-03-22 11:24:56 +000033
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +000034namespace arm_compute
35{
36namespace test
37{
38namespace validation
39{
40namespace reference
41{
42namespace
43{
44template <typename T>
Gian Marco Iodice247f52c2018-03-22 11:24:56 +000045void initialize_matrix_transform(SimpleTensor<T> &src, const Size2D &output_tile_size, const Size2D &kernel_size, WinogradTransformType winograd_transform_type)
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +000046{
Gian Marco Iodice247f52c2018-03-22 11:24:56 +000047 // Winograd input transform matrices
48 static const float imatrix2x2_3x3[] =
Giorgio Arena2d9de0a2018-03-15 17:58:20 +000049 {
Gian Marco Iodice247f52c2018-03-22 11:24:56 +000050 1.0f, 0.0f, -1.0f, 0.0f,
51 0.0f, 1.0f, 1.0f, 0.0f,
52 0.0f, -1.0f, 1.0f, 0.0f,
53 0.0f, 1.0f, 0.0f, -1.0f
54 };
55
56 static const float imatrix4x4_3x3[] =
57 {
58 4.0f, 0.0f, -5.0f, 0.0f, 1.0f, 0.0f,
59 0.0f, -4.0f, -4.0f, 1.0f, 1.0f, 0.0f,
60 0.0f, 4.0f, -4.0f, -1.0f, 1.0f, 0.0f,
61 0.0f, -2.0f, -1.0f, 2.0f, 1.0f, 0.0f,
62 0.0f, 2.0f, -1.0f, -2.0f, 1.0f, 0.0f,
63 0.0f, 4.0f, 0.0f, -5.0f, 0.0f, 1.0f,
64 };
65
Giorgio Arenafe5ef382018-04-17 10:14:10 +010066 static const float imatrix4x4_5x5[] =
67 {
68 1.f, 0.f, -21.f / 4.f, 0.f, 21.f / 4.f, 0.f, -1.f, 0.f,
69 0.f, 1.f, 1.f, -17.f / 4.f, -17.f / 4.f, 1.f, 1.f, 0.f,
70 0.f, -1.f, 1.f, 17.f / 4.f, -17.f / 4.f, -1.f, 1.f, 0.f,
71 0.f, 1.f / 2.f, 1.f / 4.f, -5.f / 2.f, -5.f / 4.f, 2.f, 1.f, 0.f,
72 0.f, -1.f / 2.f, 1.f / 4.f, 5.f / 2.f, -5.f / 4.f, -2.f, 1.f, 0.f,
73 0.f, 2.f, 4.f, -5.f / 2.f, -5.f, 1.f / 2.f, 1.f, 0.f,
74 0.f, -2.f, 4.f, 5.f / 2.f, -5.f, -1.f / 2.f, 1.f, 0.f,
75 0.f, -1.f, 0.f, 21.f / 4.f, 0.f, -21.f / 4.f, 0.f, 1.f
76 };
77
Gian Marco Iodice247f52c2018-03-22 11:24:56 +000078 // ------------------------------------------
79
80 // Winograd filter transform matrices
81 static const float fmatrix2x2_3x3[] =
82 {
83 1.0f, 0.0f, 0.0f,
84 0.5f, 0.5f, 0.5f,
85 0.5f, -0.5f, 0.5f,
86 0.0f, 0.0f, 1.0f
87 };
88
89 static const float fmatrix4x4_3x3[] =
90 {
91 0.25f, 0.0f, 0.0f,
92 -1.0f / 6.0f, -1.0f / 6.0f, -1.0f / 6.0f,
93 -1.0f / 6.0f, 1.0f / 6.0f, -1.0f / 6.0f,
94 1.0f / 24.0f, 1.0f / 12.0f, 1.0f / 6.0f,
95 1.0f / 24.0f, -1.0f / 12.0f, 1.0f / 6.0f,
96 0.0f, 0.0f, 1.0f
97 };
98
Giorgio Arena9373c8b2018-04-11 19:07:17 +010099 static const float fmatrix4x4_5x5[] =
100 {
101 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
102 -2.0f / 9.0f, -2.0f / 9.0f, -2.0f / 9.0f, -2.0f / 9.0f, -2.0f / 9.0f,
103 -2.0f / 9.0f, 2.0f / 9.0f, -2.0f / 9.0f, 2.0f / 9.0f, -2.0f / 9.0f,
104 1.0f / 90.0f, 1.0f / 45.0f, 2.0f / 45.0f, 4.0f / 45.0f, 8.0f / 45.0f,
105 1.0f / 90.0f, -1.0f / 45.0f, 2.0f / 45.0f, -4.0f / 45.0f, 8.0f / 45.0f,
106 4.0f / 45.0f, 2.0f / 45.0f, 1.0f / 45.0f, 1.0f / 90.0f, 1.0f / 180.0f,
107 4.0f / 45.0f, -2.0f / 45.0f, 1.0f / 45.0f, -1.0f / 90.0f, 1.0f / 180.0f,
108 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
109
110 };
111
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000112 // ------------------------------------------
113
114 // Winograd output transform matrices
115 static const float omatrix2x2_3x3[] =
116 {
117 1.0f, 1.0f, 1.0f, 0.0f,
118 0.0f, 1.0f, -1.0f, -1.0f
119 };
120
121 static const float omatrix4x4_3x3[] =
122 {
123 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
124 0.0f, 1.0f, -1.0f, 2.0f, -2.0f, 0.0f,
125 0.0f, 1.0f, 1.0f, 4.0f, 4.0f, 0.0f,
126 0.0f, 1.0f, -1.0f, 8.0f, -8.0f, 1.0f
127 };
128
Giorgio Arenadd038702018-04-16 11:20:11 +0100129 static const float omatrix4x4_5x5[] =
130 {
131 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 8.0f, 8.0f, 0.0f,
132 0.0f, 1.0f, -1.0f, 2.0f, -2.0f, 4.0f, -4.0f, 0.0f,
133 0.0f, 1.0f, 1.0f, 4.0f, 4.0f, 2.0f, 2.0f, 0.0f,
134 0.0f, 1.0f, -1.0f, 8.0f, -8.0f, 1.0f, -1.0f, 1.0f
135 };
136
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000137 // ------------------------------------------
138
139 using WinogradKey = std::tuple<std::pair<int, int>, std::pair<int, int>, WinogradTransformType>;
140
141 // Key = (Output tile size, Kernel size, Winograd transform type)
142 static std::map<WinogradKey, const float *> matrix_map =
143 {
144 { WinogradKey(std::pair<int, int>(2, 2), std::pair<int, int>(3, 3), WinogradTransformType::INPUT), imatrix2x2_3x3 },
145 { WinogradKey(std::pair<int, int>(4, 4), std::pair<int, int>(3, 3), WinogradTransformType::INPUT), imatrix4x4_3x3 },
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100146 { WinogradKey(std::pair<int, int>(2, 1), std::pair<int, int>(3, 1), WinogradTransformType::INPUT), imatrix2x2_3x3 },
147 { WinogradKey(std::pair<int, int>(4, 1), std::pair<int, int>(3, 1), WinogradTransformType::INPUT), imatrix4x4_3x3 },
148 { WinogradKey(std::pair<int, int>(1, 2), std::pair<int, int>(1, 3), WinogradTransformType::INPUT), imatrix2x2_3x3 },
149 { WinogradKey(std::pair<int, int>(1, 4), std::pair<int, int>(1, 3), WinogradTransformType::INPUT), imatrix4x4_3x3 },
Giorgio Arenafe5ef382018-04-17 10:14:10 +0100150 { WinogradKey(std::pair<int, int>(4, 4), std::pair<int, int>(5, 5), WinogradTransformType::INPUT), imatrix4x4_5x5 },
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000151 { WinogradKey(std::pair<int, int>(2, 2), std::pair<int, int>(3, 3), WinogradTransformType::FILTER), fmatrix2x2_3x3 },
152 { WinogradKey(std::pair<int, int>(4, 4), std::pair<int, int>(3, 3), WinogradTransformType::FILTER), fmatrix4x4_3x3 },
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100153 { WinogradKey(std::pair<int, int>(2, 1), std::pair<int, int>(3, 1), WinogradTransformType::FILTER), fmatrix2x2_3x3 },
154 { WinogradKey(std::pair<int, int>(4, 1), std::pair<int, int>(3, 1), WinogradTransformType::FILTER), fmatrix4x4_3x3 },
155 { WinogradKey(std::pair<int, int>(1, 2), std::pair<int, int>(1, 3), WinogradTransformType::FILTER), fmatrix2x2_3x3 },
156 { WinogradKey(std::pair<int, int>(1, 4), std::pair<int, int>(1, 3), WinogradTransformType::FILTER), fmatrix4x4_3x3 },
Giorgio Arena9373c8b2018-04-11 19:07:17 +0100157 { WinogradKey(std::pair<int, int>(4, 4), std::pair<int, int>(5, 5), WinogradTransformType::FILTER), fmatrix4x4_5x5 },
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000158 { WinogradKey(std::pair<int, int>(2, 2), std::pair<int, int>(3, 3), WinogradTransformType::OUTPUT), omatrix2x2_3x3 },
159 { WinogradKey(std::pair<int, int>(4, 4), std::pair<int, int>(3, 3), WinogradTransformType::OUTPUT), omatrix4x4_3x3 },
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100160 { WinogradKey(std::pair<int, int>(2, 1), std::pair<int, int>(3, 1), WinogradTransformType::OUTPUT), omatrix2x2_3x3 },
161 { WinogradKey(std::pair<int, int>(4, 1), std::pair<int, int>(3, 1), WinogradTransformType::OUTPUT), omatrix4x4_3x3 },
162 { WinogradKey(std::pair<int, int>(1, 2), std::pair<int, int>(1, 3), WinogradTransformType::OUTPUT), omatrix2x2_3x3 },
163 { WinogradKey(std::pair<int, int>(1, 4), std::pair<int, int>(1, 3), WinogradTransformType::OUTPUT), omatrix4x4_3x3 },
Giorgio Arenadd038702018-04-16 11:20:11 +0100164 { WinogradKey(std::pair<int, int>(4, 4), std::pair<int, int>(5, 5), WinogradTransformType::OUTPUT), omatrix4x4_5x5 },
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000165 };
166
Giorgio Arena9373c8b2018-04-11 19:07:17 +0100167 // Find transformation matrix
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000168 std::map<WinogradKey, const float *>::iterator it;
169
170 it = matrix_map.find(WinogradKey(std::pair<int, int>(output_tile_size.width, output_tile_size.height),
171 std::pair<int, int>(kernel_size.width, kernel_size.height),
172 winograd_transform_type));
173
174 float const *matrix_values = nullptr;
175 if(it != matrix_map.end())
176 {
177 // Get matrix pointer
178 matrix_values = it->second;
Giorgio Arena2d9de0a2018-03-15 17:58:20 +0000179 }
180 else
181 {
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000182 ARM_COMPUTE_ERROR("Winograd configuration not supported");
Giorgio Arena2d9de0a2018-03-15 17:58:20 +0000183 }
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000184
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000185 // Copy values
186 std::copy(&matrix_values[0], &matrix_values[0] + src.num_elements(), &src[0]);
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000187}
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000188} // namespace
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000189
190template <typename T>
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100191void print_tile(SimpleTensor<T> &in)
192{
193 for(int y = 0; y < in.shape()[1]; y++)
194 {
195 for(int x = 0; x < in.shape()[0]; x++)
196 {
197 std::cout << in[x + y * in.shape()[0]] << " ";
198 }
199
200 std::cout << std::endl;
201 }
202}
203
204template <typename T>
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000205SimpleTensor<T> winograd_input_transform(const SimpleTensor<T> &in, const TensorShape &output_shape, const WinogradInfo &winograd_info)
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000206{
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000207 ARM_COMPUTE_ERROR_ON(in.data_layout() != DataLayout::NCHW);
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000208
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000209 const PadStrideInfo conv_info = winograd_info.convolution_info;
210 const Size2D output_tile_size = winograd_info.output_tile_size;
211 const Size2D kernel_size = winograd_info.kernel_size;
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000212
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000213 SimpleTensor<T> out{ output_shape, in.data_type() };
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000214
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000215 // Calculate dimensions for the tile
216 const unsigned int tile_w = output_tile_size.width + kernel_size.width - 1;
217 const unsigned int tile_h = output_tile_size.height + kernel_size.height - 1;
218
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100219 // Get the maximum dimension from the tile size
220 const unsigned int tile_max_dim = std::max(tile_w, tile_h);
221
222 TensorShape tile_dims(tile_max_dim, tile_max_dim);
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000223
224 // Simple tensor for the input tile
225 SimpleTensor<T> src_tile{ tile_dims, in.data_type() };
226
227 // Simple tensor for the temporary tile
228 SimpleTensor<T> tmp_tile{ tile_dims, in.data_type() };
229
230 // Simple tensor for the output tile
231 SimpleTensor<T> dst_tile{ tile_dims, in.data_type() };
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000232
233 // Simple tensor for the transformation matrix
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000234 SimpleTensor<T> matrix{ tile_dims, in.data_type() };
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000235
236 // Simple tensor for the transformation matrix transposed
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000237 SimpleTensor<T> matrix_transposed{ tile_dims, in.data_type() };
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000238
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000239 // Initialize matrix for the input transform
240 initialize_matrix_transform(matrix, output_tile_size, kernel_size, WinogradTransformType::INPUT);
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000241
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000242 // Transpose matrix
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000243 transpose_matrix(matrix, matrix_transposed);
244
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000245 const int in_w = in.shape().x();
246 const int in_h = in.shape().y();
247 const int in_d = in.shape().z();
248 const int out_d = out.shape().z();
249 const int num_batches = in.shape().total_size() / (in_w * in_h * in_d);
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000250 const int step_x = output_tile_size.width;
251 const int step_y = output_tile_size.height;
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000252
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100253 // Compute the number of output tiles along the x and y direction of size "output_tile_size"
254 const Size2D num_tiles = compute_winograd_convolution_tiles(Size2D(in_w, in_h),
255 kernel_size,
256 output_tile_size,
257 conv_info);
258
259 const int num_tiles_x = num_tiles.width;
260 const int num_tiles_y = num_tiles.height;
261
262 // In case of 1D convolution, the input tile has to be partially filled with zeros
263 int start_x_zero = 0;
264 int start_y_zero = 0;
265 int end_x_zero = 0;
266 int end_y_zero = 0;
267
268 if(output_tile_size.width == 1)
269 {
270 start_x_zero = 1;
271 start_y_zero = 0;
272 end_x_zero = tile_max_dim - 1;
273 end_y_zero = tile_max_dim;
274 }
275 else if(output_tile_size.height == 1)
276 {
277 start_x_zero = 0;
278 start_y_zero = 1;
279 end_x_zero = tile_max_dim;
280 end_y_zero = tile_max_dim - 1;
281 }
282
283 // Set the anchor and shape of the zeros area
284 const Coordinates anchor_zeros(start_x_zero, start_y_zero);
285 const TensorShape shape_zeros(end_x_zero, end_y_zero);
286
287 // If we have a vertical filter (i.e. 1x3, 1x5,..), we need to take the elements along the y direction (step = width of the output tile)
288 const int step_y_transf_tile = kernel_size.width == 1 ? tile_max_dim : 1;
289
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000290 ARM_COMPUTE_ERROR_ON((num_tiles_x * num_tiles_y) != static_cast<int>(out.shape().y()));
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000291
292 for(int b = 0; b < num_batches; ++b)
293 {
294 for(int z = 0; z < in_d; ++z)
295 {
296 for(int y = 0; y < num_tiles_y; ++y)
297 {
298 for(int x = 0; x < num_tiles_x; ++x)
299 {
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000300 int xi = x * step_x - conv_info.pad_left();
301 int yi = y * step_y - conv_info.pad_top();
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000302
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000303 // Get the tile from the input tensor
304 get_tile(in, src_tile, Coordinates(xi, yi, z, b));
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000305
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100306 // Fill partially with zeros in case of 1D convolution
307 zeros(src_tile, anchor_zeros, shape_zeros);
308
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000309 // Compute the transformation
310 matrix_multiply(matrix, src_tile, tmp_tile);
311 matrix_multiply(tmp_tile, matrix_transposed, dst_tile);
312
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000313 // Store the output tile across the channels
314 for(int i = 0; i < out_d; ++i)
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000315 {
316 int xo = z;
317 int yo = x + y * num_tiles_x;
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100318 out[coords2index(out.shape(), Coordinates(xo, yo, i, b))] = dst_tile[i * step_y_transf_tile];
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000319 }
320 }
321 }
322 }
323 }
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000324
325 return out;
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000326}
327
328template <typename T>
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000329SimpleTensor<T> winograd_filter_transform(const SimpleTensor<T> &in, const TensorShape &output_shape, const WinogradInfo &winograd_info)
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000330{
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000331 ARM_COMPUTE_ERROR_ON_MSG(in.data_layout() != DataLayout::NCHW, "Only supported NCHW data format");
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000332
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000333 // Create reference
334 SimpleTensor<T> out{ output_shape, in.data_type(), 1 };
335
336 const Size2D output_tile_size = winograd_info.output_tile_size;
337 const Size2D kernel_size = winograd_info.kernel_size;
338
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000339 // Calculate dimensions for the tile
340 const unsigned int input_tile_w = output_tile_size.width + kernel_size.width - 1;
341 const unsigned int input_tile_h = output_tile_size.height + kernel_size.height - 1;
342 const unsigned int input_tile_area = input_tile_w * input_tile_h;
343
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100344 // Get the maximum dimension from the filter size
345 const unsigned int kernel_max_dim = std::max(kernel_size.width, kernel_size.height);
346
347 // Get the maximum dimension from the input tile
348 const unsigned int input_tile_max_dim = std::max(input_tile_w, input_tile_h);
349
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000350 // Simple tensor for the input tile
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100351 SimpleTensor<T> input_tile{ TensorShape(kernel_max_dim, kernel_max_dim), in.data_type(), 1 };
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000352
353 // Simple tensor for the transformation matrix
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100354 SimpleTensor<T> trans_matrix{ TensorShape(kernel_max_dim, input_tile_max_dim), in.data_type(), 1 };
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000355
356 // Simple tensor for the transformation matrix transpose
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100357 SimpleTensor<T> trans_matrix_transposed{ TensorShape(input_tile_max_dim, kernel_max_dim), in.data_type(), 1 };
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000358
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000359 // Simple tensor for the temporary tile
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100360 SimpleTensor<T> tmp_tile{ TensorShape(kernel_max_dim, input_tile_max_dim), in.data_type(), 1 };
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000361
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000362 // Simple tensor for the output tile
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100363 SimpleTensor<T> transf_tile{ TensorShape(input_tile_max_dim, input_tile_max_dim), in.data_type(), 1 };
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000364
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000365 // Initialize matrix for the filter transform
366 initialize_matrix_transform(trans_matrix, output_tile_size, kernel_size, WinogradTransformType::FILTER);
367
368 // Transpose the transformation matrix
369 transpose_matrix(trans_matrix, trans_matrix_transposed);
370
371 const int num_channels = in.shape()[2];
372 const int num_filters = in.shape()[3];
373 const int num_batches = in.shape().total_size() / (kernel_size.area() * num_channels * num_filters);
374
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100375 // If we have a vertical filter (i.e. 1x3, 1x5,..), we need to take the elements along the y direction (step_y_transf_tile = width of the output tile)
376 const int step_y_transf_tile = kernel_size.width == 1 ? input_tile_max_dim : 1;
377
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000378 for(int n = 0; n < num_batches; ++n)
379 {
380 for(int w = 0; w < num_filters; ++w)
381 {
382 for(int z = 0; z < num_channels; ++z)
383 {
384 // Load the tile from the input tensor
385 get_tile(in, input_tile, Coordinates(0, 0, z, w, n));
386
387 // First transformation
388 matrix_multiply(trans_matrix, input_tile, tmp_tile);
389
390 // Second transformation
391 matrix_multiply(tmp_tile, trans_matrix_transposed, transf_tile);
392
393 // Store the output tile across the channels
394 const int output_offset = w + z * num_filters;
395
396 // Store the values across the channels
397 for(unsigned int i = 0; i < input_tile_area; ++i)
398 {
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100399 out[output_offset + i * num_filters * num_channels] = transf_tile[i * step_y_transf_tile];
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000400 }
401 }
402 }
403 }
404
405 return out;
406}
407
408template <typename T>
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100409SimpleTensor<T> winograd_output_transform(const SimpleTensor<T> &in, const SimpleTensor<T> &b, const TensorShape &output_shape, const WinogradInfo &winograd_info)
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000410{
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000411 const PadStrideInfo conv_info = winograd_info.convolution_info;
412 const Size2D input_dimensions = winograd_info.input_dimensions;
413 const Size2D output_tile_size = winograd_info.output_tile_size;
414 const Size2D kernel_size = winograd_info.kernel_size;
415
416 // Create reference
417 SimpleTensor<T> out{ output_shape, in.data_type(), 1 };
418
419 // Calculate dimensions for the tiles
420 const unsigned int in_tile_w = output_tile_size.width + kernel_size.width - 1;
421 const unsigned int in_tile_h = output_tile_size.height + kernel_size.height - 1;
422 const unsigned int out_tile_w = output_tile_size.width;
423 const unsigned int out_tile_h = output_tile_size.height;
424
425 ARM_COMPUTE_ERROR_ON(in.shape()[2] != (in_tile_w * in_tile_h));
Giorgio Arena3695f9a2018-04-23 17:41:22 +0100426 ARM_COMPUTE_ERROR_ON(in.shape()[0] != out.shape()[get_data_layout_dimension_index(winograd_info.output_data_layout, DataLayoutDimension::CHANNEL)]);
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000427
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100428 // Get the maximum dimension from the tile size
429 const unsigned int in_tile_max_dim = std::max(in_tile_w, in_tile_h);
430 const unsigned int out_tile_max_dim = std::max(output_tile_size.width, output_tile_size.height);
431
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000432 // Compute tile dimensions
433 // Input tile dimensions
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100434 TensorShape in_tile_dims(in_tile_max_dim, in_tile_max_dim);
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000435
436 // Output tile dimensions
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100437 TensorShape out_tile_dims(out_tile_max_dim, out_tile_max_dim);
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000438
439 // Transformation matrix dimensions
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100440 TensorShape tr_tile_dims(in_tile_max_dim, out_tile_max_dim);
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000441
442 // Create tensors
443 // Simple tensor for the input tile
444 SimpleTensor<T> input_tile{ in_tile_dims, in.data_type(), 1 };
445
446 // Simple tensor for the transformation matrix
447 SimpleTensor<T> trans_matrix{ tr_tile_dims, in.data_type(), 1 };
448
449 // Simple tensor for the transformation matrix transpose
450 SimpleTensor<T> trans_matrix_transposed{ TensorShape(tr_tile_dims[1], tr_tile_dims[0]), in.data_type(), 1 };
451
452 // Simple tensor for the temporary tile
453 SimpleTensor<T> tmp_tile{ tr_tile_dims, in.data_type(), 1 };
454
455 // Simple tensor for the output tile
456 SimpleTensor<T> output_tile{ out_tile_dims, in.data_type(), 1 };
457
458 // Initialize matrix for the output transform
459 initialize_matrix_transform(trans_matrix, output_tile_size, kernel_size, WinogradTransformType::OUTPUT);
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000460
461 // Transpose the transformation matrix
462 transpose_matrix(trans_matrix, trans_matrix_transposed);
463
464 const int w_in = in.shape()[0];
465 const int h_in = in.shape()[1];
466 const int c_in = in.shape()[2];
467 const int w_out = out.shape()[0];
468 const int h_out = out.shape()[1];
469 const int c_out = out.shape()[2];
470 const int num_batches = in.shape().total_size() / (w_in * h_in * c_in);
471
472 // Input strides
473 const int stridey_in = w_in;
474 const int stridez_in = stridey_in * h_in;
475 const int stridew_in = stridez_in * c_in;
476
477 // Output strides
478 const int stridey_out = w_out;
479 const int stridez_out = stridey_out * h_out;
480 const int stridew_out = stridez_out * c_out;
481
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100482 // Compute the number of output tiles along the x and y direction of size "output_tile_size"
483 const Size2D num_tiles = compute_winograd_convolution_tiles(Size2D(input_dimensions.width, input_dimensions.height),
484 kernel_size,
485 output_tile_size,
486 conv_info);
487
488 const int num_tiles_x = num_tiles.width;
489 const int num_tiles_y = num_tiles.height;
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000490
491 ARM_COMPUTE_UNUSED(num_tiles_y);
492 ARM_COMPUTE_ERROR_ON(in.shape()[1] != static_cast<unsigned int>(num_tiles_x * num_tiles_y));
493
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100494 // If we have a vertical filter (i.e. 1x3, 1x5,..), we still need to take the elements along the x direction (step_y_transf_tile = 1)
495 const int step_y_transf_tile = kernel_size.width == 1 ? 1 : output_tile.shape()[0];
496
497 // Initialize with zeros the input tile
498 zeros(input_tile, Coordinates(0, 0), input_tile.shape());
499
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000500 for(int n = 0; n < num_batches; ++n)
501 {
502 for(int y = 0; y < h_in; ++y)
503 {
504 for(int x = 0; x < w_in; ++x)
505 {
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000506 // Load the input tile tile across the channels of the input tensor
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000507 for(int z = 0; z < c_in; ++z)
508 {
509 input_tile[z] = in[x + (y * stridey_in) + (z * stridez_in) + (n * stridew_in)];
510 }
511
512 // First transformation
513 matrix_multiply(trans_matrix, input_tile, tmp_tile);
514
515 // Second transformation
516 matrix_multiply(tmp_tile, trans_matrix_transposed, output_tile);
517
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000518 // Store the output tile
519 const int xo = (y % num_tiles_x) * out_tile_w;
520 const int yo = (y / num_tiles_x) * out_tile_h;
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000521 const int zo = x;
522
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000523 const int output_offset = xo + (yo * stridey_out) + (zo * stridez_out) + (n * stridew_out);
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000524
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000525 for(int yi = 0; yi < static_cast<int>(out_tile_h); ++yi)
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000526 {
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000527 for(int xi = 0; xi < static_cast<int>(out_tile_w); ++xi)
528 {
529 // Check out-of-bound writes
530 if((xo + xi < w_out) && (yo + yi < h_out))
531 {
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100532 out[output_offset + yi * stridey_out + xi] = output_tile[xi + yi * step_y_transf_tile];
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100533
534 // Add bias
535 out[output_offset + yi * stridey_out + xi] += b[zo];
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000536 }
537 }
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000538 }
539 }
540 }
541 }
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000542
543 return out;
544}
545
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000546template SimpleTensor<float> winograd_filter_transform(const SimpleTensor<float> &in, const TensorShape &output_shape, const WinogradInfo &winograd_info);
547template SimpleTensor<float> winograd_input_transform(const SimpleTensor<float> &in, const TensorShape &output_shape, const WinogradInfo &winograd_info);
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100548template SimpleTensor<float> winograd_output_transform(const SimpleTensor<float> &in, const SimpleTensor<float> &b, const TensorShape &output_shape, const WinogradInfo &winograd_info);
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000549} // namespace reference
550} // namespace validation
551} // namespace test
552} // namespace arm_compute