Compute Library
 21.08
Winograd.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2020 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 
28 
29 #include "arm_compute/core/Types.h"
30 
31 #include <algorithm>
32 #include <cmath>
33 
34 namespace arm_compute
35 {
36 namespace test
37 {
38 namespace validation
39 {
40 namespace reference
41 {
42 namespace
43 {
44 template <typename T>
45 void initialize_matrix_transform(SimpleTensor<T> &src, const Size2D &output_tile_size, const Size2D &kernel_size, WinogradTransformType winograd_transform_type)
46 {
47  // Winograd input transform matrices
48  static const std::array<float, 16> imatrix2x2_3x3 =
49  {
50  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 std::array<float, 36> 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 
66  static const std::array<float, 64> 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 
78  static const std::array<float, 64> imatrix2x1_7x7 =
79  {
80  -36.0f, 0.0f, 49.0f, 0.0f, -14.0f, 0.0f, 1.0f, 0.0f,
81  0.0f, -36.0f, 36.0f, 13.0f, -13.0f, -1.0f, 1.0f, 0.0f,
82  0.0f, 36.0f, 36.0f, -13.0f, -13.0f, 1.0f, 1.0f, 0.0f,
83  0.0f, -18.0f, 9.0f, 20.0f, -10.0f, -2.0f, 1.0f, 0.0f,
84  0.0f, 18.0f, 9.0f, -20.0f, -10.0f, 2.0f, 1.0f, 0.0f,
85  0.0f, -12.0f, 4.0f, 15.0f, -5.0f, -3.0f, 1.0f, 0.0f,
86  0.0f, 12.0f, 4.0f, -15.0f, -5.0f, 3.0f, 1.0f, 0.0f,
87  0.0f, -36.0f, 0.0f, 49.0f, 0.0f, -14.0f, 0.0f, 1.0f
88  };
89 
90  // ------------------------------------------
91 
92  // Winograd filter transform matrices
93  static const std::array<float, 12> fmatrix2x2_3x3 =
94  {
95  1.0f, 0.0f, 0.0f,
96  0.5f, 0.5f, 0.5f,
97  0.5f, -0.5f, 0.5f,
98  0.0f, 0.0f, 1.0f
99  };
100 
101  static const std::array<float, 18> fmatrix4x4_3x3 =
102  {
103  0.25f, 0.0f, 0.0f,
104  -1.0f / 6.0f, -1.0f / 6.0f, -1.0f / 6.0f,
105  -1.0f / 6.0f, 1.0f / 6.0f, -1.0f / 6.0f,
106  1.0f / 24.0f, 1.0f / 12.0f, 1.0f / 6.0f,
107  1.0f / 24.0f, -1.0f / 12.0f, 1.0f / 6.0f,
108  0.0f, 0.0f, 1.0f
109  };
110 
111  static const std::array<float, 40> fmatrix4x4_5x5 =
112  {
113  1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
114  -2.0f / 9.0f, -2.0f / 9.0f, -2.0f / 9.0f, -2.0f / 9.0f, -2.0f / 9.0f,
115  -2.0f / 9.0f, 2.0f / 9.0f, -2.0f / 9.0f, 2.0f / 9.0f, -2.0f / 9.0f,
116  1.0f / 90.0f, 1.0f / 45.0f, 2.0f / 45.0f, 4.0f / 45.0f, 8.0f / 45.0f,
117  1.0f / 90.0f, -1.0f / 45.0f, 2.0f / 45.0f, -4.0f / 45.0f, 8.0f / 45.0f,
118  4.0f / 45.0f, 2.0f / 45.0f, 1.0f / 45.0f, 1.0f / 90.0f, 1.0f / 180.0f,
119  4.0f / 45.0f, -2.0f / 45.0f, 1.0f / 45.0f, -1.0f / 90.0f, 1.0f / 180.0f,
120  0.0f, 0.0f, 0.0f, 0.0f, 1.0f
121 
122  };
123 
124  static const std::array<float, 56> fmatrix2x1_7x7 =
125  {
126  -1.0f / 36.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
127  1.0f / 48.0f, -1.0f / 48.0f, 1.0f / 48.0f, -1.0f / 48.0f, 1.0f / 48.0f, -1.0f / 48.0f, 1.0f / 48.0f,
128  1.0f / 48.0f, 1.0f / 48.0f, 1.0f / 48.0f, 1.0f / 48.0f, 1.0f / 48.0f, 1.0f / 48.0f, 1.0f / 48.0f,
129  -1.0f / 120.0f, 1.0f / 60.0f, -1.0f / 30.0f, 1.0f / 15.0f, -2.0f / 15.0f, 4.0f / 15.0f, -8.0f / 15.0f,
130  -1.0f / 120.0f, -1.0f / 60.0f, -1.0f / 30.0f, -1.0f / 15.0f, -2.0f / 15.0f, -4.0f / 15.0f, -8.0f / 15.0f,
131  1.0f / 720.0f, -1.0f / 240.0f, 1.0f / 80.0f, -3.0f / 80.0f, 9.0f / 80.0f, -27.0f / 80.0f, 81.0f / 80.0f,
132  1.0f / 720.0f, 1.0f / 240.0f, 1.0f / 80.0f, 3.0f / 80.0f, 9.0f / 80.0f, 27.0f / 80.0f, 81.0f / 80.0f,
133  0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
134  };
135 
136  // ------------------------------------------
137 
138  // Winograd output transform matrices
139  static const std::array<float, 8> omatrix2x2_3x3 =
140  {
141  1.0f, 1.0f, 1.0f, 0.0f,
142  0.0f, 1.0f, -1.0f, -1.0f
143  };
144 
145  static const std::array<float, 24> omatrix4x4_3x3 =
146  {
147  1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
148  0.0f, 1.0f, -1.0f, 2.0f, -2.0f, 0.0f,
149  0.0f, 1.0f, 1.0f, 4.0f, 4.0f, 0.0f,
150  0.0f, 1.0f, -1.0f, 8.0f, -8.0f, 1.0f
151  };
152 
153  static const std::array<float, 36> omatrix4x4_5x5 =
154  {
155  1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 8.0f, 8.0f, 0.0f,
156  0.0f, 1.0f, -1.0f, 2.0f, -2.0f, 4.0f, -4.0f, 0.0f,
157  0.0f, 1.0f, 1.0f, 4.0f, 4.0f, 2.0f, 2.0f, 0.0f,
158  0.0f, 1.0f, -1.0f, 8.0f, -8.0f, 1.0f, -1.0f, 1.0f
159  };
160 
161  static const std::array<float, 16> omatrix2x1_7x7 =
162  {
163  1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
164  0.0f, -1.0f, 1.0f, -2.0f, 2.0f, -3.0f, 3.0f, 1.0f
165  };
166 
167  // ------------------------------------------
168 
169  using WinogradKey = std::tuple<std::pair<int, int>, std::pair<int, int>, WinogradTransformType>;
170 
171  // Key = (Output tile size, Kernel size, Winograd transform type)
172  static std::map<WinogradKey, const float *> matrix_map =
173  {
174  { WinogradKey(std::pair<int, int>(2, 2), std::pair<int, int>(3, 3), WinogradTransformType::INPUT), imatrix2x2_3x3.data() },
175  { WinogradKey(std::pair<int, int>(4, 4), std::pair<int, int>(3, 3), WinogradTransformType::INPUT), imatrix4x4_3x3.data() },
176  { WinogradKey(std::pair<int, int>(2, 1), std::pair<int, int>(3, 1), WinogradTransformType::INPUT), imatrix2x2_3x3.data() },
177  { WinogradKey(std::pair<int, int>(4, 1), std::pair<int, int>(3, 1), WinogradTransformType::INPUT), imatrix4x4_3x3.data() },
178  { WinogradKey(std::pair<int, int>(1, 2), std::pair<int, int>(1, 3), WinogradTransformType::INPUT), imatrix2x2_3x3.data() },
179  { WinogradKey(std::pair<int, int>(1, 4), std::pair<int, int>(1, 3), WinogradTransformType::INPUT), imatrix4x4_3x3.data() },
180  { WinogradKey(std::pair<int, int>(4, 4), std::pair<int, int>(5, 5), WinogradTransformType::INPUT), imatrix4x4_5x5.data() },
181  { WinogradKey(std::pair<int, int>(4, 1), std::pair<int, int>(5, 1), WinogradTransformType::INPUT), imatrix4x4_5x5.data() },
182  { WinogradKey(std::pair<int, int>(2, 1), std::pair<int, int>(7, 1), WinogradTransformType::INPUT), imatrix2x1_7x7.data() },
183  { WinogradKey(std::pair<int, int>(1, 2), std::pair<int, int>(1, 7), WinogradTransformType::INPUT), imatrix2x1_7x7.data() },
184  { WinogradKey(std::pair<int, int>(2, 2), std::pair<int, int>(7, 7), WinogradTransformType::INPUT), imatrix2x1_7x7.data() },
185  { WinogradKey(std::pair<int, int>(1, 4), std::pair<int, int>(1, 5), WinogradTransformType::INPUT), imatrix4x4_5x5.data() },
186  { WinogradKey(std::pair<int, int>(2, 2), std::pair<int, int>(3, 3), WinogradTransformType::FILTER), fmatrix2x2_3x3.data() },
187  { WinogradKey(std::pair<int, int>(4, 4), std::pair<int, int>(3, 3), WinogradTransformType::FILTER), fmatrix4x4_3x3.data() },
188  { WinogradKey(std::pair<int, int>(2, 1), std::pair<int, int>(3, 1), WinogradTransformType::FILTER), fmatrix2x2_3x3.data() },
189  { WinogradKey(std::pair<int, int>(4, 1), std::pair<int, int>(3, 1), WinogradTransformType::FILTER), fmatrix4x4_3x3.data() },
190  { WinogradKey(std::pair<int, int>(1, 2), std::pair<int, int>(1, 3), WinogradTransformType::FILTER), fmatrix2x2_3x3.data() },
191  { WinogradKey(std::pair<int, int>(1, 4), std::pair<int, int>(1, 3), WinogradTransformType::FILTER), fmatrix4x4_3x3.data() },
192  { WinogradKey(std::pair<int, int>(4, 4), std::pair<int, int>(5, 5), WinogradTransformType::FILTER), fmatrix4x4_5x5.data() },
193  { WinogradKey(std::pair<int, int>(4, 1), std::pair<int, int>(5, 1), WinogradTransformType::FILTER), fmatrix4x4_5x5.data() },
194  { WinogradKey(std::pair<int, int>(2, 1), std::pair<int, int>(7, 1), WinogradTransformType::FILTER), fmatrix2x1_7x7.data() },
195  { WinogradKey(std::pair<int, int>(1, 2), std::pair<int, int>(1, 7), WinogradTransformType::FILTER), fmatrix2x1_7x7.data() },
196  { WinogradKey(std::pair<int, int>(2, 2), std::pair<int, int>(7, 7), WinogradTransformType::FILTER), fmatrix2x1_7x7.data() },
197  { WinogradKey(std::pair<int, int>(1, 4), std::pair<int, int>(1, 5), WinogradTransformType::FILTER), fmatrix4x4_5x5.data() },
198  { WinogradKey(std::pair<int, int>(2, 2), std::pair<int, int>(3, 3), WinogradTransformType::OUTPUT), omatrix2x2_3x3.data() },
199  { WinogradKey(std::pair<int, int>(4, 4), std::pair<int, int>(3, 3), WinogradTransformType::OUTPUT), omatrix4x4_3x3.data() },
200  { WinogradKey(std::pair<int, int>(2, 1), std::pair<int, int>(3, 1), WinogradTransformType::OUTPUT), omatrix2x2_3x3.data() },
201  { WinogradKey(std::pair<int, int>(4, 1), std::pair<int, int>(3, 1), WinogradTransformType::OUTPUT), omatrix4x4_3x3.data() },
202  { WinogradKey(std::pair<int, int>(1, 2), std::pair<int, int>(1, 3), WinogradTransformType::OUTPUT), omatrix2x2_3x3.data() },
203  { WinogradKey(std::pair<int, int>(1, 4), std::pair<int, int>(1, 3), WinogradTransformType::OUTPUT), omatrix4x4_3x3.data() },
204  { WinogradKey(std::pair<int, int>(4, 4), std::pair<int, int>(5, 5), WinogradTransformType::OUTPUT), omatrix4x4_5x5.data() },
205  { WinogradKey(std::pair<int, int>(4, 1), std::pair<int, int>(5, 1), WinogradTransformType::OUTPUT), omatrix4x4_5x5.data() },
206  { WinogradKey(std::pair<int, int>(2, 1), std::pair<int, int>(7, 1), WinogradTransformType::OUTPUT), omatrix2x1_7x7.data() },
207  { WinogradKey(std::pair<int, int>(1, 2), std::pair<int, int>(1, 7), WinogradTransformType::OUTPUT), omatrix2x1_7x7.data() },
208  { WinogradKey(std::pair<int, int>(2, 2), std::pair<int, int>(7, 7), WinogradTransformType::OUTPUT), omatrix2x1_7x7.data() },
209  { WinogradKey(std::pair<int, int>(1, 4), std::pair<int, int>(1, 5), WinogradTransformType::OUTPUT), omatrix4x4_5x5.data() },
210  };
211 
212  // Find transformation matrix
213  std::map<WinogradKey, const float *>::iterator it;
214 
215  it = matrix_map.find(WinogradKey(std::pair<int, int>(output_tile_size.width, output_tile_size.height),
216  std::pair<int, int>(kernel_size.width, kernel_size.height),
217  winograd_transform_type));
218 
219  float const *matrix_values = nullptr;
220  if(it != matrix_map.end())
221  {
222  // Get matrix pointer
223  matrix_values = it->second;
224  }
225  else
226  {
227  ARM_COMPUTE_ERROR("Winograd configuration not supported");
228  }
229 
230  // Copy values
231  std::copy(&matrix_values[0], &matrix_values[0] + src.num_elements(), &src[0]);
232 }
233 } // namespace
234 
235 template <typename T>
237 {
239 
240  const PadStrideInfo conv_info = winograd_info.convolution_info;
241  const Size2D output_tile_size = winograd_info.output_tile_size;
242  const Size2D kernel_size = winograd_info.kernel_size;
243 
245 
246  // Calculate dimensions for the tile
247  const unsigned int tile_w = output_tile_size.width + kernel_size.width - 1;
248  const unsigned int tile_h = output_tile_size.height + kernel_size.height - 1;
249 
250  // Get the maximum dimension from the tile size
251  const unsigned int tile_max_dim = std::max(tile_w, tile_h);
252 
253  TensorShape tile_dims(tile_max_dim, tile_max_dim);
254 
255  // Simple tensor for the input tile
256  SimpleTensor<T> src_tile{ tile_dims, in.data_type() };
257 
258  // Simple tensor for the temporary tile
259  SimpleTensor<T> tmp_tile{ tile_dims, in.data_type() };
260 
261  // Simple tensor for the output tile
262  SimpleTensor<T> dst_tile{ tile_dims, in.data_type() };
263 
264  // Simple tensor for the transformation matrix
265  SimpleTensor<T> matrix{ tile_dims, in.data_type() };
266 
267  // Simple tensor for the transformation matrix transposed
268  SimpleTensor<T> matrix_transposed{ tile_dims, in.data_type() };
269 
270  // Initialize matrix for the input transform
271  initialize_matrix_transform(matrix, output_tile_size, kernel_size, WinogradTransformType::INPUT);
272 
273  // Transpose matrix
274  transpose_matrix<T>(matrix, matrix_transposed);
275 
276  const int in_w = in.shape().x();
277  const int in_h = in.shape().y();
278  const int in_d = in.shape().z();
279  const int out_d = out.shape().z();
280  const int num_batches = in.shape().total_size() / (in_w * in_h * in_d);
281  const int step_x = output_tile_size.width;
282  const int step_y = output_tile_size.height;
283 
284  // Compute the number of output tiles along the x and y direction of size "output_tile_size"
285  const Size2D num_tiles = compute_winograd_convolution_tiles(Size2D(in_w, in_h),
286  kernel_size,
287  output_tile_size,
288  conv_info);
289 
290  const int num_tiles_x = num_tiles.width;
291  const int num_tiles_y = num_tiles.height;
292 
293  // In case of 1D convolution, the input tile has to be partially filled with zeros
294  int start_x_zero = 0;
295  int start_y_zero = 0;
296  int end_x_zero = 0;
297  int end_y_zero = 0;
298 
299  if(output_tile_size.width == 1)
300  {
301  start_x_zero = 1;
302  start_y_zero = 0;
303  end_x_zero = tile_max_dim - 1;
304  end_y_zero = tile_max_dim;
305  }
306  else if(output_tile_size.height == 1)
307  {
308  start_x_zero = 0;
309  start_y_zero = 1;
310  end_x_zero = tile_max_dim;
311  end_y_zero = tile_max_dim - 1;
312  }
313 
314  // Set the anchor and shape of the zeros area
315  const Coordinates anchor_zeros(start_x_zero, start_y_zero);
316  const TensorShape shape_zeros(end_x_zero, end_y_zero);
317 
318  // 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)
319  const int step_y_transf_tile = kernel_size.width == 1 ? tile_max_dim : 1;
320 
321  ARM_COMPUTE_ERROR_ON((num_tiles_x * num_tiles_y) != static_cast<int>(out.shape().y()));
322 
323  for(int b = 0; b < num_batches; ++b)
324  {
325  for(int z = 0; z < in_d; ++z)
326  {
327  for(int y = 0; y < num_tiles_y; ++y)
328  {
329  for(int x = 0; x < num_tiles_x; ++x)
330  {
331  int xi = x * step_x - conv_info.pad_left();
332  int yi = y * step_y - conv_info.pad_top();
333 
334  // Get the tile from the input tensor
335  get_tile<T>(in, src_tile, Coordinates(xi, yi, z, b));
336 
337  // Fill partially with zeros in case of 1D convolution
338  zeros<T>(src_tile, anchor_zeros, shape_zeros);
339 
340  // Compute the transformation
341  matrix_multiply<T>(matrix, src_tile, tmp_tile);
342  matrix_multiply<T>(tmp_tile, matrix_transposed, dst_tile);
343 
344  // Store the output tile across the channels
345  for(int i = 0; i < out_d; ++i)
346  {
347  int xo = z;
348  int yo = x + y * num_tiles_x;
349  out[coords2index(out.shape(), Coordinates(xo, yo, i, b))] = dst_tile[i * step_y_transf_tile];
350  }
351  }
352  }
353  }
354  }
355 
356  return out;
357 }
358 
359 template <typename T>
361 {
362  ARM_COMPUTE_ERROR_ON_MSG(in.data_layout() != DataLayout::NCHW, "Only supported NCHW data format");
363 
364  // Create reference
365  SimpleTensor<T> out{ output_shape, in.data_type(), 1 };
366 
367  const Size2D output_tile_size = winograd_info.output_tile_size;
368  const Size2D kernel_size = winograd_info.kernel_size;
369 
370  // Calculate dimensions for the tile
371  const unsigned int input_tile_w = output_tile_size.width + kernel_size.width - 1;
372  const unsigned int input_tile_h = output_tile_size.height + kernel_size.height - 1;
373  const unsigned int input_tile_area = input_tile_w * input_tile_h;
374 
375  // Get the maximum dimension from the filter size
376  const unsigned int kernel_max_dim = std::max(kernel_size.width, kernel_size.height);
377 
378  // Get the maximum dimension from the input tile
379  const unsigned int input_tile_max_dim = std::max(input_tile_w, input_tile_h);
380 
381  // Simple tensor for the input tile
382  SimpleTensor<T> input_tile{ TensorShape(kernel_max_dim, kernel_max_dim), in.data_type(), 1 };
383 
384  // Simple tensor for the transformation matrix
385  SimpleTensor<T> trans_matrix{ TensorShape(kernel_max_dim, input_tile_max_dim), in.data_type(), 1 };
386 
387  // Simple tensor for the transformation matrix transpose
388  SimpleTensor<T> trans_matrix_transposed{ TensorShape(input_tile_max_dim, kernel_max_dim), in.data_type(), 1 };
389 
390  // Simple tensor for the temporary tile
391  SimpleTensor<T> tmp_tile{ TensorShape(kernel_max_dim, input_tile_max_dim), in.data_type(), 1 };
392 
393  // Simple tensor for the output tile
394  SimpleTensor<T> transf_tile{ TensorShape(input_tile_max_dim, input_tile_max_dim), in.data_type(), 1 };
395 
396  // Initialize matrix for the filter transform
397  initialize_matrix_transform(trans_matrix, output_tile_size, kernel_size, WinogradTransformType::FILTER);
398 
399  // Transpose the transformation matrix
400  transpose_matrix<T>(trans_matrix, trans_matrix_transposed);
401 
402  const int num_channels = in.shape()[2];
403  const int num_filters = in.shape()[3];
404  const int num_batches = in.shape().total_size() / (kernel_size.area() * num_channels * num_filters);
405 
406  // 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)
407  const int step_y_transf_tile = kernel_size.width == 1 ? input_tile_max_dim : 1;
408 
409  for(int n = 0; n < num_batches; ++n)
410  {
411  for(int w = 0; w < num_filters; ++w)
412  {
413  for(int z = 0; z < num_channels; ++z)
414  {
415  // Load the tile from the input tensor
416  get_tile<T>(in, input_tile, Coordinates(0, 0, z, w, n));
417 
418  // First transformation
419  matrix_multiply<T>(trans_matrix, input_tile, tmp_tile);
420 
421  // Second transformation
422  matrix_multiply<T>(tmp_tile, trans_matrix_transposed, transf_tile);
423 
424  // Store the output tile across the channels
425  const int output_offset = w + z * num_filters;
426 
427  // Store the values across the channels
428  for(unsigned int i = 0; i < input_tile_area; ++i)
429  {
430  out[output_offset + i * num_filters * num_channels] = transf_tile[i * step_y_transf_tile];
431  }
432  }
433  }
434  }
435 
436  return out;
437 }
438 
439 template <typename T>
441 {
442  const PadStrideInfo conv_info = winograd_info.convolution_info;
443  const Size2D input_dimensions = winograd_info.input_dimensions;
444  const Size2D output_tile_size = winograd_info.output_tile_size;
445  const Size2D kernel_size = winograd_info.kernel_size;
446 
447  // Create reference
448  SimpleTensor<T> out{ output_shape, in.data_type(), 1 };
449 
450  // Calculate dimensions for the tiles
451  const unsigned int in_tile_w = output_tile_size.width + kernel_size.width - 1;
452  const unsigned int in_tile_h = output_tile_size.height + kernel_size.height - 1;
453  const unsigned int out_tile_w = output_tile_size.width;
454  const unsigned int out_tile_h = output_tile_size.height;
455 
456  ARM_COMPUTE_ERROR_ON(in.shape()[2] != (in_tile_w * in_tile_h));
458 
459  // Get the maximum dimension from the tile size
460  const unsigned int in_tile_max_dim = std::max(in_tile_w, in_tile_h);
461  const unsigned int out_tile_max_dim = std::max(output_tile_size.width, output_tile_size.height);
462 
463  // Compute tile dimensions
464  // Input tile dimensions
465  TensorShape in_tile_dims(in_tile_max_dim, in_tile_max_dim);
466 
467  // Output tile dimensions
468  TensorShape out_tile_dims(out_tile_max_dim, out_tile_max_dim);
469 
470  // Transformation matrix dimensions
471  TensorShape tr_tile_dims(in_tile_max_dim, out_tile_max_dim);
472 
473  // Create tensors
474  // Simple tensor for the input tile
475  SimpleTensor<T> input_tile{ in_tile_dims, in.data_type(), 1 };
476 
477  // Simple tensor for the transformation matrix
478  SimpleTensor<T> trans_matrix{ tr_tile_dims, in.data_type(), 1 };
479 
480  // Simple tensor for the transformation matrix transpose
481  SimpleTensor<T> trans_matrix_transposed{ TensorShape(tr_tile_dims[1], tr_tile_dims[0]), in.data_type(), 1 };
482 
483  // Simple tensor for the temporary tile
484  SimpleTensor<T> tmp_tile{ tr_tile_dims, in.data_type(), 1 };
485 
486  // Simple tensor for the output tile
487  SimpleTensor<T> output_tile{ out_tile_dims, in.data_type(), 1 };
488 
489  // Initialize matrix for the output transform
490  initialize_matrix_transform(trans_matrix, output_tile_size, kernel_size, WinogradTransformType::OUTPUT);
491 
492  // Transpose the transformation matrix
493  transpose_matrix<T>(trans_matrix, trans_matrix_transposed);
494 
495  const int w_in = in.shape()[0];
496  const int h_in = in.shape()[1];
497  const int c_in = in.shape()[2];
498  const int w_out = out.shape()[0];
499  const int h_out = out.shape()[1];
500  const int c_out = out.shape()[2];
501  const int num_batches = in.shape().total_size() / (w_in * h_in * c_in);
502 
503  // Input strides
504  const int stridey_in = w_in;
505  const int stridez_in = stridey_in * h_in;
506  const int stridew_in = stridez_in * c_in;
507 
508  // Output strides
509  const int stridey_out = w_out;
510  const int stridez_out = stridey_out * h_out;
511  const int stridew_out = stridez_out * c_out;
512 
513  // Compute the number of output tiles along the x and y direction of size "output_tile_size"
514  const Size2D num_tiles = compute_winograd_convolution_tiles(Size2D(input_dimensions.width, input_dimensions.height),
515  kernel_size,
516  output_tile_size,
517  conv_info);
518 
519  const int num_tiles_x = num_tiles.width;
520  const int num_tiles_y = num_tiles.height;
521 
522  ARM_COMPUTE_UNUSED(num_tiles_y);
523  ARM_COMPUTE_ERROR_ON(in.shape()[1] != static_cast<unsigned int>(num_tiles_x * num_tiles_y));
524 
525  // 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)
526  const int step_y_transf_tile = kernel_size.width == 1 ? 1 : output_tile.shape()[0];
527 
528  // Initialize with zeros the input tile
529  zeros<T>(input_tile, Coordinates(0, 0), input_tile.shape());
530 
531  for(int n = 0; n < num_batches; ++n)
532  {
533  for(int y = 0; y < h_in; ++y)
534  {
535  for(int x = 0; x < w_in; ++x)
536  {
537  // Load the input tile tile across the channels of the input tensor
538  for(int z = 0; z < c_in; ++z)
539  {
540  input_tile[z] = in[x + (y * stridey_in) + (z * stridez_in) + (n * stridew_in)];
541  }
542 
543  // First transformation
544  matrix_multiply<T>(trans_matrix, input_tile, tmp_tile);
545 
546  // Second transformation
547  matrix_multiply<T>(tmp_tile, trans_matrix_transposed, output_tile);
548 
549  // Store the output tile
550  const int xo = (y % num_tiles_x) * out_tile_w;
551  const int yo = (y / num_tiles_x) * out_tile_h;
552  const int zo = x;
553 
554  const int output_offset = xo + (yo * stridey_out) + (zo * stridez_out) + (n * stridew_out);
555 
556  for(int yi = 0; yi < static_cast<int>(out_tile_h); ++yi)
557  {
558  for(int xi = 0; xi < static_cast<int>(out_tile_w); ++xi)
559  {
560  // Check out-of-bound writes
561  if((xo + xi < w_out) && (yo + yi < h_out))
562  {
563  out[output_offset + yi * stridey_out + xi] = output_tile[xi + yi * step_y_transf_tile];
564 
565  // Add bias
566  out[output_offset + yi * stridey_out + xi] += b[zo];
567  }
568  }
569  }
570  }
571  }
572  }
573 
574  return out;
575 }
576 
578 template SimpleTensor<float> winograd_input_transform(const SimpleTensor<float> &in, const TensorShape &output_shape, const WinogradInfo &winograd_info);
579 template SimpleTensor<float> winograd_output_transform(const SimpleTensor<float> &in, const SimpleTensor<float> &b, const TensorShape &output_shape, const WinogradInfo &winograd_info);
580 template SimpleTensor<half> winograd_filter_transform(const SimpleTensor<half> &in, const TensorShape &output_shape, const WinogradInfo &winograd_info);
581 template SimpleTensor<half> winograd_input_transform(const SimpleTensor<half> &in, const TensorShape &output_shape, const WinogradInfo &winograd_info);
582 template SimpleTensor<half> winograd_output_transform(const SimpleTensor<half> &in, const SimpleTensor<half> &b, const TensorShape &output_shape, const WinogradInfo &winograd_info);
583 
584 } // namespace reference
585 } // namespace validation
586 } // namespace test
587 } // namespace arm_compute
SimpleTensor< float > w
Definition: DFT.cpp:156
Shape of a tensor.
Definition: TensorShape.h:39
DataLayout output_data_layout
Data layout to use for the output tensor once the convolution has been applied (NCHW or NHWC) ...
Definition: Types.h:2161
SimpleTensor< float > b
Definition: DFT.cpp:157
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
Winograd information.
Definition: Types.h:2142
PadStrideInfo convolution_info
Convolution info (Pads, strides,...)
Definition: Types.h:2160
DataType data_type() const override
Data type of the tensor.
Definition: SimpleTensor.h:357
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
Size2D compute_winograd_convolution_tiles(const Size2D &in_dims, const Size2D &kernel_size, const Size2D &output_tile_size, const PadStrideInfo &conv_info)
Calculate the number of output tiles required by Winograd Convolution layer.
Definition: Helpers.h:211
TensorShape shape() const override
Shape of the tensor.
Definition: SimpleTensor.h:320
SimpleTensor< T > copy(const SimpleTensor< T > &src, const TensorShape &output_shape)
Definition: Copy.cpp:37
SimpleTensor< float > src
Definition: DFT.cpp:155
SimpleTensor< T > winograd_output_transform(const SimpleTensor< T > &in, const SimpleTensor< T > &b, const TensorShape &output_shape, const WinogradInfo &winograd_info)
Definition: Winograd.cpp:440
Copyright (c) 2017-2021 Arm Limited.
size_t height
Height of the image region or rectangle.
Definition: Size2D.h:90
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
Size2D output_tile_size
Width and height of the output tile.
Definition: Types.h:2157
SimpleTensor< T > winograd_filter_transform(const SimpleTensor< T > &in, const TensorShape &output_shape, const WinogradInfo &winograd_info)
Definition: Winograd.cpp:360
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
Coordinates of an item.
Definition: Coordinates.h:37
int coords2index(const TensorShape &shape, const Coordinates &coord)
Convert n-dimensional coordinates into a linear index.
Definition: Helpers.inl:175
Padding and stride information class.
Definition: Types.h:647
DataLayout data_layout() const override
Data layout of the tensor.
Definition: SimpleTensor.h:351
Num samples, channels, height, width.
WinogradTransformType
Winograd transform type.
Definition: Winograd.h:40
Simple tensor object that stores elements in a consecutive chunk of memory.
Definition: SimpleTensor.h:58
size_t width
Width of the image region or rectangle.
Definition: Size2D.h:89
Class for specifying the size of an image or rectangle.
Definition: Size2D.h:34
Size2D kernel_size
Width and height of the kernel.
Definition: Types.h:2158
SimpleTensor< T > winograd_input_transform(const SimpleTensor< T > &in, const TensorShape &output_shape, const WinogradInfo &winograd_info)
Definition: Winograd.cpp:236
size_t get_data_layout_dimension_index(const DataLayout data_layout, const DataLayoutDimension data_layout_dimension)
Get the index of the given dimension.
Definition: Helpers.inl:193
Size2D input_dimensions
Width and height of the input tensor before the convolution is applied.
Definition: Types.h:2159
size_t area() const
The area of the image or rectangle calculated as (width * height)
Definition: Size2D.h:53