blob: 194a78e95f0ce3be8caec1dcd69d0cc8e4e4b1c1 [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>
32
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +000033namespace arm_compute
34{
35namespace test
36{
37namespace validation
38{
39namespace reference
40{
41namespace
42{
43template <typename T>
Gian Marco Iodice247f52c2018-03-22 11:24:56 +000044void 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 +000045{
Gian Marco Iodice247f52c2018-03-22 11:24:56 +000046 // Winograd input transform matrices
47 static const float imatrix2x2_3x3[] =
Giorgio Arena2d9de0a2018-03-15 17:58:20 +000048 {
Gian Marco Iodice247f52c2018-03-22 11:24:56 +000049 1.0f, 0.0f, -1.0f, 0.0f,
50 0.0f, 1.0f, 1.0f, 0.0f,
51 0.0f, -1.0f, 1.0f, 0.0f,
52 0.0f, 1.0f, 0.0f, -1.0f
53 };
54
55 static const float imatrix4x4_3x3[] =
56 {
57 4.0f, 0.0f, -5.0f, 0.0f, 1.0f, 0.0f,
58 0.0f, -4.0f, -4.0f, 1.0f, 1.0f, 0.0f,
59 0.0f, 4.0f, -4.0f, -1.0f, 1.0f, 0.0f,
60 0.0f, -2.0f, -1.0f, 2.0f, 1.0f, 0.0f,
61 0.0f, 2.0f, -1.0f, -2.0f, 1.0f, 0.0f,
62 0.0f, 4.0f, 0.0f, -5.0f, 0.0f, 1.0f,
63 };
64
Giorgio Arenafe5ef382018-04-17 10:14:10 +010065 static const float imatrix4x4_5x5[] =
66 {
67 1.f, 0.f, -21.f / 4.f, 0.f, 21.f / 4.f, 0.f, -1.f, 0.f,
68 0.f, 1.f, 1.f, -17.f / 4.f, -17.f / 4.f, 1.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 / 2.f, 1.f / 4.f, -5.f / 2.f, -5.f / 4.f, 2.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, 2.f, 4.f, -5.f / 2.f, -5.f, 1.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, -1.f, 0.f, 21.f / 4.f, 0.f, -21.f / 4.f, 0.f, 1.f
75 };
76
Gian Marco Iodice247f52c2018-03-22 11:24:56 +000077 // ------------------------------------------
78
79 // Winograd filter transform matrices
80 static const float fmatrix2x2_3x3[] =
81 {
82 1.0f, 0.0f, 0.0f,
83 0.5f, 0.5f, 0.5f,
84 0.5f, -0.5f, 0.5f,
85 0.0f, 0.0f, 1.0f
86 };
87
88 static const float fmatrix4x4_3x3[] =
89 {
90 0.25f, 0.0f, 0.0f,
91 -1.0f / 6.0f, -1.0f / 6.0f, -1.0f / 6.0f,
92 -1.0f / 6.0f, 1.0f / 6.0f, -1.0f / 6.0f,
93 1.0f / 24.0f, 1.0f / 12.0f, 1.0f / 6.0f,
94 1.0f / 24.0f, -1.0f / 12.0f, 1.0f / 6.0f,
95 0.0f, 0.0f, 1.0f
96 };
97
Giorgio Arena9373c8b2018-04-11 19:07:17 +010098 static const float fmatrix4x4_5x5[] =
99 {
100 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
101 -2.0f / 9.0f, -2.0f / 9.0f, -2.0f / 9.0f, -2.0f / 9.0f, -2.0f / 9.0f,
102 -2.0f / 9.0f, 2.0f / 9.0f, -2.0f / 9.0f, 2.0f / 9.0f, -2.0f / 9.0f,
103 1.0f / 90.0f, 1.0f / 45.0f, 2.0f / 45.0f, 4.0f / 45.0f, 8.0f / 45.0f,
104 1.0f / 90.0f, -1.0f / 45.0f, 2.0f / 45.0f, -4.0f / 45.0f, 8.0f / 45.0f,
105 4.0f / 45.0f, 2.0f / 45.0f, 1.0f / 45.0f, 1.0f / 90.0f, 1.0f / 180.0f,
106 4.0f / 45.0f, -2.0f / 45.0f, 1.0f / 45.0f, -1.0f / 90.0f, 1.0f / 180.0f,
107 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
108
109 };
110
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000111 // ------------------------------------------
112
113 // Winograd output transform matrices
114 static const float omatrix2x2_3x3[] =
115 {
116 1.0f, 1.0f, 1.0f, 0.0f,
117 0.0f, 1.0f, -1.0f, -1.0f
118 };
119
120 static const float omatrix4x4_3x3[] =
121 {
122 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
123 0.0f, 1.0f, -1.0f, 2.0f, -2.0f, 0.0f,
124 0.0f, 1.0f, 1.0f, 4.0f, 4.0f, 0.0f,
125 0.0f, 1.0f, -1.0f, 8.0f, -8.0f, 1.0f
126 };
127
Giorgio Arenadd038702018-04-16 11:20:11 +0100128 static const float omatrix4x4_5x5[] =
129 {
130 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 8.0f, 8.0f, 0.0f,
131 0.0f, 1.0f, -1.0f, 2.0f, -2.0f, 4.0f, -4.0f, 0.0f,
132 0.0f, 1.0f, 1.0f, 4.0f, 4.0f, 2.0f, 2.0f, 0.0f,
133 0.0f, 1.0f, -1.0f, 8.0f, -8.0f, 1.0f, -1.0f, 1.0f
134 };
135
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000136 // ------------------------------------------
137
138 using WinogradKey = std::tuple<std::pair<int, int>, std::pair<int, int>, WinogradTransformType>;
139
140 // Key = (Output tile size, Kernel size, Winograd transform type)
141 static std::map<WinogradKey, const float *> matrix_map =
142 {
143 { WinogradKey(std::pair<int, int>(2, 2), std::pair<int, int>(3, 3), WinogradTransformType::INPUT), imatrix2x2_3x3 },
144 { WinogradKey(std::pair<int, int>(4, 4), std::pair<int, int>(3, 3), WinogradTransformType::INPUT), imatrix4x4_3x3 },
Giorgio Arenafe5ef382018-04-17 10:14:10 +0100145 { 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 +0000146 { WinogradKey(std::pair<int, int>(2, 2), std::pair<int, int>(3, 3), WinogradTransformType::FILTER), fmatrix2x2_3x3 },
147 { WinogradKey(std::pair<int, int>(4, 4), std::pair<int, int>(3, 3), WinogradTransformType::FILTER), fmatrix4x4_3x3 },
Giorgio Arena9373c8b2018-04-11 19:07:17 +0100148 { 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 +0000149 { WinogradKey(std::pair<int, int>(2, 2), std::pair<int, int>(3, 3), WinogradTransformType::OUTPUT), omatrix2x2_3x3 },
150 { WinogradKey(std::pair<int, int>(4, 4), std::pair<int, int>(3, 3), WinogradTransformType::OUTPUT), omatrix4x4_3x3 },
Giorgio Arenadd038702018-04-16 11:20:11 +0100151 { 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 +0000152 };
153
Giorgio Arena9373c8b2018-04-11 19:07:17 +0100154 // Find transformation matrix
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000155 std::map<WinogradKey, const float *>::iterator it;
156
157 it = matrix_map.find(WinogradKey(std::pair<int, int>(output_tile_size.width, output_tile_size.height),
158 std::pair<int, int>(kernel_size.width, kernel_size.height),
159 winograd_transform_type));
160
161 float const *matrix_values = nullptr;
162 if(it != matrix_map.end())
163 {
164 // Get matrix pointer
165 matrix_values = it->second;
Giorgio Arena2d9de0a2018-03-15 17:58:20 +0000166 }
167 else
168 {
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000169 ARM_COMPUTE_ERROR("Winograd configuration not supported");
Giorgio Arena2d9de0a2018-03-15 17:58:20 +0000170 }
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000171
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000172 // Copy values
173 std::copy(&matrix_values[0], &matrix_values[0] + src.num_elements(), &src[0]);
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000174}
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000175} // namespace
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000176
177template <typename T>
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000178SimpleTensor<T> winograd_input_transform(const SimpleTensor<T> &in, const TensorShape &output_shape, const WinogradInfo &winograd_info)
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000179{
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000180 ARM_COMPUTE_ERROR_ON(in.data_layout() != DataLayout::NCHW);
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000181
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000182 const PadStrideInfo conv_info = winograd_info.convolution_info;
183 const Size2D output_tile_size = winograd_info.output_tile_size;
184 const Size2D kernel_size = winograd_info.kernel_size;
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000185
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000186 SimpleTensor<T> out{ output_shape, in.data_type() };
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000187
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000188 // Calculate dimensions for the tile
189 const unsigned int tile_w = output_tile_size.width + kernel_size.width - 1;
190 const unsigned int tile_h = output_tile_size.height + kernel_size.height - 1;
191
192 TensorShape tile_dims(tile_w, tile_h);
193
194 // Simple tensor for the input tile
195 SimpleTensor<T> src_tile{ tile_dims, in.data_type() };
196
197 // Simple tensor for the temporary tile
198 SimpleTensor<T> tmp_tile{ tile_dims, in.data_type() };
199
200 // Simple tensor for the output tile
201 SimpleTensor<T> dst_tile{ tile_dims, in.data_type() };
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000202
203 // Simple tensor for the transformation matrix
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000204 SimpleTensor<T> matrix{ tile_dims, in.data_type() };
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000205
206 // Simple tensor for the transformation matrix transposed
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000207 SimpleTensor<T> matrix_transposed{ tile_dims, in.data_type() };
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000208
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000209 // Initialize matrix for the input transform
210 initialize_matrix_transform(matrix, output_tile_size, kernel_size, WinogradTransformType::INPUT);
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000211
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000212 // Transpose matrix
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000213 transpose_matrix(matrix, matrix_transposed);
214
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000215 const int in_w = in.shape().x();
216 const int in_h = in.shape().y();
217 const int in_d = in.shape().z();
218 const int out_d = out.shape().z();
219 const int num_batches = in.shape().total_size() / (in_w * in_h * in_d);
220 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));
221 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));
222 const int step_x = output_tile_size.width;
223 const int step_y = output_tile_size.height;
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000224
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000225 ARM_COMPUTE_ERROR_ON((num_tiles_x * num_tiles_y) != static_cast<int>(out.shape().y()));
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000226
227 for(int b = 0; b < num_batches; ++b)
228 {
229 for(int z = 0; z < in_d; ++z)
230 {
231 for(int y = 0; y < num_tiles_y; ++y)
232 {
233 for(int x = 0; x < num_tiles_x; ++x)
234 {
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000235 int xi = x * step_x - conv_info.pad_left();
236 int yi = y * step_y - conv_info.pad_top();
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000237
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000238 // Get the tile from the input tensor
239 get_tile(in, src_tile, Coordinates(xi, yi, z, b));
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000240
241 // Compute the transformation
242 matrix_multiply(matrix, src_tile, tmp_tile);
243 matrix_multiply(tmp_tile, matrix_transposed, dst_tile);
244
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000245 // Store the output tile across the channels
246 for(int i = 0; i < out_d; ++i)
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000247 {
248 int xo = z;
249 int yo = x + y * num_tiles_x;
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000250 out[coords2index(out.shape(), Coordinates(xo, yo, i, b))] = dst_tile[i];
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000251 }
252 }
253 }
254 }
255 }
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000256
257 return out;
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000258}
259
260template <typename T>
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000261SimpleTensor<T> winograd_filter_transform(const SimpleTensor<T> &in, const TensorShape &output_shape, const WinogradInfo &winograd_info)
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000262{
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000263 ARM_COMPUTE_ERROR_ON_MSG(in.data_layout() != DataLayout::NCHW, "Only supported NCHW data format");
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000264
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000265 // Create reference
266 SimpleTensor<T> out{ output_shape, in.data_type(), 1 };
267
268 const Size2D output_tile_size = winograd_info.output_tile_size;
269 const Size2D kernel_size = winograd_info.kernel_size;
270
271 TensorShape kernel_tile_dims(kernel_size.width, kernel_size.height);
272
273 // Calculate dimensions for the tile
274 const unsigned int input_tile_w = output_tile_size.width + kernel_size.width - 1;
275 const unsigned int input_tile_h = output_tile_size.height + kernel_size.height - 1;
276 const unsigned int input_tile_area = input_tile_w * input_tile_h;
277
278 // Simple tensor for the input tile
279 SimpleTensor<T> input_tile{ kernel_tile_dims, in.data_type(), 1 };
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000280
281 // Simple tensor for the transformation matrix
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000282 SimpleTensor<T> trans_matrix{ TensorShape(kernel_tile_dims[0], input_tile_w), in.data_type(), 1 };
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000283
284 // Simple tensor for the transformation matrix transpose
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000285 SimpleTensor<T> trans_matrix_transposed{ TensorShape(input_tile_w, kernel_tile_dims[0]), in.data_type(), 1 };
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000286
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000287 // Simple tensor for the temporary tile
288 SimpleTensor<T> tmp_tile{ TensorShape(kernel_tile_dims[0], input_tile_w), in.data_type(), 1 };
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000289
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000290 // Simple tensor for the output tile
291 SimpleTensor<T> transf_tile{ TensorShape(input_tile_w, input_tile_w), in.data_type(), 1 };
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000292
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000293 // Initialize matrix for the filter transform
294 initialize_matrix_transform(trans_matrix, output_tile_size, kernel_size, WinogradTransformType::FILTER);
295
296 // Transpose the transformation matrix
297 transpose_matrix(trans_matrix, trans_matrix_transposed);
298
299 const int num_channels = in.shape()[2];
300 const int num_filters = in.shape()[3];
301 const int num_batches = in.shape().total_size() / (kernel_size.area() * num_channels * num_filters);
302
303 for(int n = 0; n < num_batches; ++n)
304 {
305 for(int w = 0; w < num_filters; ++w)
306 {
307 for(int z = 0; z < num_channels; ++z)
308 {
309 // Load the tile from the input tensor
310 get_tile(in, input_tile, Coordinates(0, 0, z, w, n));
311
312 // First transformation
313 matrix_multiply(trans_matrix, input_tile, tmp_tile);
314
315 // Second transformation
316 matrix_multiply(tmp_tile, trans_matrix_transposed, transf_tile);
317
318 // Store the output tile across the channels
319 const int output_offset = w + z * num_filters;
320
321 // Store the values across the channels
322 for(unsigned int i = 0; i < input_tile_area; ++i)
323 {
324 out[output_offset + i * num_filters * num_channels] = transf_tile[i];
325 }
326 }
327 }
328 }
329
330 return out;
331}
332
333template <typename T>
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100334SimpleTensor<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 +0000335{
336 ARM_COMPUTE_ERROR_ON_MSG(winograd_info.output_data_layout != DataLayout::NCHW, "Only supported NCHW data format");
337
338 const PadStrideInfo conv_info = winograd_info.convolution_info;
339 const Size2D input_dimensions = winograd_info.input_dimensions;
340 const Size2D output_tile_size = winograd_info.output_tile_size;
341 const Size2D kernel_size = winograd_info.kernel_size;
342
343 // Create reference
344 SimpleTensor<T> out{ output_shape, in.data_type(), 1 };
345
346 // Calculate dimensions for the tiles
347 const unsigned int in_tile_w = output_tile_size.width + kernel_size.width - 1;
348 const unsigned int in_tile_h = output_tile_size.height + kernel_size.height - 1;
349 const unsigned int out_tile_w = output_tile_size.width;
350 const unsigned int out_tile_h = output_tile_size.height;
351
352 ARM_COMPUTE_ERROR_ON(in.shape()[2] != (in_tile_w * in_tile_h));
353 ARM_COMPUTE_ERROR_ON(in.shape()[0] != out.shape()[2]);
354
355 // Compute tile dimensions
356 // Input tile dimensions
357 TensorShape in_tile_dims(in_tile_w, in_tile_h);
358
359 // Output tile dimensions
360 TensorShape out_tile_dims(output_tile_size.width, output_tile_size.height);
361
362 // Transformation matrix dimensions
363 TensorShape tr_tile_dims(in_tile_w, output_tile_size.width);
364
365 // Create tensors
366 // Simple tensor for the input tile
367 SimpleTensor<T> input_tile{ in_tile_dims, in.data_type(), 1 };
368
369 // Simple tensor for the transformation matrix
370 SimpleTensor<T> trans_matrix{ tr_tile_dims, in.data_type(), 1 };
371
372 // Simple tensor for the transformation matrix transpose
373 SimpleTensor<T> trans_matrix_transposed{ TensorShape(tr_tile_dims[1], tr_tile_dims[0]), in.data_type(), 1 };
374
375 // Simple tensor for the temporary tile
376 SimpleTensor<T> tmp_tile{ tr_tile_dims, in.data_type(), 1 };
377
378 // Simple tensor for the output tile
379 SimpleTensor<T> output_tile{ out_tile_dims, in.data_type(), 1 };
380
381 // Initialize matrix for the output transform
382 initialize_matrix_transform(trans_matrix, output_tile_size, kernel_size, WinogradTransformType::OUTPUT);
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000383
384 // Transpose the transformation matrix
385 transpose_matrix(trans_matrix, trans_matrix_transposed);
386
387 const int w_in = in.shape()[0];
388 const int h_in = in.shape()[1];
389 const int c_in = in.shape()[2];
390 const int w_out = out.shape()[0];
391 const int h_out = out.shape()[1];
392 const int c_out = out.shape()[2];
393 const int num_batches = in.shape().total_size() / (w_in * h_in * c_in);
394
395 // Input strides
396 const int stridey_in = w_in;
397 const int stridez_in = stridey_in * h_in;
398 const int stridew_in = stridez_in * c_in;
399
400 // Output strides
401 const int stridey_out = w_out;
402 const int stridez_out = stridey_out * h_out;
403 const int stridew_out = stridez_out * c_out;
404
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000405 // Compute number of elements to process in the X and Y direction
406 const int num_elements_x = input_dimensions.width - (kernel_size.width - 1) + conv_info.pad_left() + conv_info.pad_right();
407 const int num_elements_y = input_dimensions.height - (kernel_size.height - 1) + conv_info.pad_top() + conv_info.pad_bottom();
408 const int num_tiles_x = std::ceil(num_elements_x / static_cast<float>(output_tile_size.width));
409 const int num_tiles_y = std::ceil(num_elements_y / static_cast<float>(output_tile_size.height));
410
411 ARM_COMPUTE_UNUSED(num_tiles_y);
412 ARM_COMPUTE_ERROR_ON(in.shape()[1] != static_cast<unsigned int>(num_tiles_x * num_tiles_y));
413
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000414 for(int n = 0; n < num_batches; ++n)
415 {
416 for(int y = 0; y < h_in; ++y)
417 {
418 for(int x = 0; x < w_in; ++x)
419 {
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000420 // Load the input tile tile across the channels of the input tensor
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000421 for(int z = 0; z < c_in; ++z)
422 {
423 input_tile[z] = in[x + (y * stridey_in) + (z * stridez_in) + (n * stridew_in)];
424 }
425
426 // First transformation
427 matrix_multiply(trans_matrix, input_tile, tmp_tile);
428
429 // Second transformation
430 matrix_multiply(tmp_tile, trans_matrix_transposed, output_tile);
431
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000432 // Store the output tile
433 const int xo = (y % num_tiles_x) * out_tile_w;
434 const int yo = (y / num_tiles_x) * out_tile_h;
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000435 const int zo = x;
436
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000437 const int output_offset = xo + (yo * stridey_out) + (zo * stridez_out) + (n * stridew_out);
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000438
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000439 for(int yi = 0; yi < static_cast<int>(out_tile_h); ++yi)
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000440 {
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000441 for(int xi = 0; xi < static_cast<int>(out_tile_w); ++xi)
442 {
443 // Check out-of-bound writes
444 if((xo + xi < w_out) && (yo + yi < h_out))
445 {
446 out[output_offset + yi * stridey_out + xi] = output_tile[xi + yi * out_tile_w];
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100447
448 // Add bias
449 out[output_offset + yi * stridey_out + xi] += b[zo];
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000450 }
451 }
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000452 }
453 }
454 }
455 }
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000456
457 return out;
458}
459
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000460template SimpleTensor<float> winograd_filter_transform(const SimpleTensor<float> &in, const TensorShape &output_shape, const WinogradInfo &winograd_info);
461template 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 +0100462template 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 +0000463} // namespace reference
464} // namespace validation
465} // namespace test
466} // namespace arm_compute