aboutsummaryrefslogtreecommitdiff
path: root/arm_compute/core/NEON/kernels/winograd/transforms/input_2x2_3x3.hpp
blob: 7013c66ac025be64cfcdb09c23b4d1ccb9d0a522 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/*
 * Copyright (c) 2017 ARM Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#pragma once
#include "../tensor.hpp"

namespace winograd {
  /* Transform an input tensor into the Winograd domain.
   */
  template <typename T>
  struct Winograd2x2_3x3GemmInput {
    static void execute(
        const T *inptr,
        const Tensor4DShape& input_shape,
        const PaddingType padding_type,
        const int tile_M,
        const int tile_N,
        T *outptr_base,
        const int matrix_stride,
        const int matrix_batch_stride,
        const int matrix_row_stride
    );

    static size_t bytes_read(const Tensor4DShape &input_shape,
                           const Tensor4DShape &output_shape) {
      const int tile_rows = iceildiv(output_shape.n_rows, 2);
      const int tile_cols = iceildiv(output_shape.n_cols, 2);
      return input_shape.n_batches * tile_rows * (16 + 8*(tile_cols - 1)) * input_shape.n_channels * sizeof(T);
    }

    static int flops_performed(const Tensor4DShape &input_shape,
                                const Tensor4DShape &output_shape) {
      const int tile_rows = iceildiv(output_shape.n_rows, 2);
      const int tile_cols = iceildiv(output_shape.n_cols, 2);
      return input_shape.n_batches * tile_rows * (32 + 24*(tile_cols - 1)) * input_shape.n_channels;
    }

    static size_t bytes_written(const Tensor4DShape &input_shape,
                              const Tensor4DShape &output_shape) {
      const int tile_rows = iceildiv(output_shape.n_rows, 2);
      const int tile_cols = iceildiv(output_shape.n_cols, 2);
      const int M = input_shape.n_batches * tile_rows * tile_cols;
      return 16 * M * input_shape.n_channels * sizeof(T);
    }

    protected:
    template <const PaddingType padding, const int pad_bottom, const int pad_right>
    static void process_tile_tensor(
        const int tile_M,      // Number of rows of tiles
        const int tile_N,      // Number of columns of tiles
        int n_channels,  // Number of input channels
        const T* const input,  // Base input pointer (appropriate to batch and channel)
        const int input_row_stride,  // Stride between rows of the input
        const int input_col_stride,  // Stride between columns of the input
        T* const matrix,              // 1st output matrix (appropriate to batch and channel)
        const int matrix_stride,      // Stride between matrices
        const int matrix_row_stride   // Stride between rows of the output matrix
    );

    template <const int pad_top, const int pad_left,
              const int pad_bottom, const int pad_right,
              const int proc_channels>
    static void process_tile_row(
        const int tile_N,      // Number of tiles in the row
        const T* const input,  // Base input pointer (appropriate to batch, channel and row)
        const int input_row_stride,  // Stride between rows of the input
        const int input_col_stride,  // Stride between columns of the input
        T* const matrix,              // 1st output matrix (appropriate to batch, channel and row)
        const int matrix_stride,      // Stride between matrices
        const int matrix_row_stride   // Stride between rows of the output matrix
    );
  };

  template <typename T>
  struct Winograd2x2_3x3GemmInputChannelwise {
    static void execute(
        const T *inptr,
        const Tensor4DShape& input_shape,
        const PaddingType padding_type,
        const int tile_M,
        const int tile_N,
        T *outptr_base,
        const int matrix_stride,
        const int matrix_batch_stride,
        const int matrix_row_stride
    );

    static size_t bytes_read(const Tensor4DShape &input_shape,
                           const Tensor4DShape &output_shape) {
      // We read as many bytes as we write
      return bytes_written(input_shape, output_shape);
    }

    static int flops_performed(const Tensor4DShape &input_shape,
                                const Tensor4DShape &output_shape) {
      const int tile_rows = iceildiv(output_shape.n_rows, 2);
      const int tile_cols = iceildiv(output_shape.n_cols, 2);
      return input_shape.n_batches * tile_rows * 32 * tile_cols * input_shape.n_channels;
    }

    static size_t bytes_written(const Tensor4DShape &input_shape,
                              const Tensor4DShape &output_shape) {
      return winograd::Winograd2x2_3x3GemmInput<T>::bytes_written(input_shape, output_shape);
    }

    protected:
    typedef void (*tilefunc)(int, const T*, int, int, T*, int);
    template <const int pad_top,
              const int pad_left,
              const int pad_bottom,
              const int pad_right>
    static void process_tile(
        int n_channels,  // Number of channels in the tile
        const T* const input_base,
        const int input_row_stride,
        const int input_col_stride,
        T* const matrix_base,
        const int matrix_stride
    );

    private:
    template <const int pad_top,
              const int pad_left,
              const int pad_bottom,
              const int pad_right,
              const int proc_channels>
    static void _process_tile(
        int &n_channels, const T* &inptr,
        const int input_row_stride, const int input_col_stride,
        T* &outptr, const int matrix_stride
    );
  };
}

/*****************************************************************************/
// Include specialised implementations here
#include "input_2x2_3x3/a64_float.hpp"
#include "input_2x2_3x3/a64_float_channelwise.hpp"
/*****************************************************************************/

/*****************************************************************************/
template <typename T>
void winograd::Winograd2x2_3x3GemmInput<T>::execute(
    const T *inptr_base,
    const Tensor4DShape& input_shape,
    const PaddingType padding_type,
    const int tile_M,
    const int tile_N,
    T *outptr_base,
    const int matrix_stride,
    const int matrix_batch_stride,
    const int matrix_row_stride
) {
  // Select an appropriate matrix processing method for the shape and padding
  // of the input tensor.
  typedef void (*tensorfunc)(int, int, int, const T*, int, int, T*, int, int);
  const auto process_tensor = [&padding_type, &input_shape] () -> tensorfunc {
    if (padding_type == PADDING_VALID) {
      const int pad_bottom = input_shape.n_rows % 2;
      const int pad_right = input_shape.n_cols % 2;

      if (pad_bottom == 0 && pad_right == 0) {
        return process_tile_tensor<PADDING_VALID, 0, 0>;
      } else if (pad_bottom == 0 && pad_right == 1) {
        return process_tile_tensor<PADDING_VALID, 0, 1>;
      } else if (pad_bottom == 1 && pad_right == 0) {
        return process_tile_tensor<PADDING_VALID, 1, 0>;
      } else if (pad_bottom == 1 && pad_right == 1) {
        return process_tile_tensor<PADDING_VALID, 1, 1>;
      }
    } else {  // PADDING_SAME
      const int pad_bottom = 1 + input_shape.n_rows % 2;
      const int pad_right = 1 + input_shape.n_cols % 2;

      if (pad_bottom == 1 && pad_right == 1) {
        return process_tile_tensor<PADDING_SAME, 1, 1>;
      } else if (pad_bottom == 1 && pad_right == 2) {
        return process_tile_tensor<PADDING_SAME, 1, 2>;
      } else if (pad_bottom == 2 && pad_right == 1) {
        return process_tile_tensor<PADDING_SAME, 2, 1>;
      } else if (pad_bottom == 2 && pad_right == 2) {
        return process_tile_tensor<PADDING_SAME, 2, 2>;
      }
    }

    printf("%s::%u Uncovered case.\n", __FILE__, __LINE__);
    exit(-1);
    return NULL;  // No function found
  } ();

  // Compute strides
  const int input_row_stride = input_shape.n_cols * input_shape.n_channels;
  const int input_col_stride = input_shape.n_channels;

  // Process each batch of the tensor in turn.
  for (int batch = 0; batch < input_shape.n_batches; batch++) {
    // Work out pointers
    const T *inptr = inptr_base + (batch * input_shape.n_rows *
                                   input_shape.n_cols * input_shape.n_channels);
    T *outptr = outptr_base + batch * matrix_batch_stride;

    // Delegate doing the actual work
    process_tensor(
      tile_M, tile_N, input_shape.n_channels,
      inptr, input_row_stride, input_col_stride,
      outptr, matrix_stride, matrix_row_stride
    );
  }
}

/*****************************************************************************/
template <typename T>
template <const PaddingType padding, const int pad_bottom, const int pad_right>
void winograd::Winograd2x2_3x3GemmInput<T>::process_tile_tensor(
    const int tile_M,      // Number of rows of tiles
    const int tile_N,      // Number of columns of tiles
    int n_channels,  // Number of input channels
    const T* const input,  // Base input pointer (appropriate to batch and channel)
    const int input_row_stride,  // Stride between rows of the input
    const int input_col_stride,  // Stride between columns of the input
    T* const matrix,              // 1st output matrix (appropriate to batch and channel)
    const int matrix_stride,      // Stride between matrices
    const int matrix_row_stride   // Stride between rows of the output matrix
) {
  // Base row processing functions
  typedef void (*rowfunc)(int, const T*, int, int, T*, int, int);
  const rowfunc process_top_row[3] = {
    (padding == PADDING_VALID)
      ? process_tile_row<0, 0, 0, pad_right, 1>
      : process_tile_row<1, 1, 0, pad_right, 1>,
    (padding == PADDING_VALID)
      ? process_tile_row<0, 0, 0, pad_right, 2>
      : process_tile_row<1, 1, 0, pad_right, 2>,
    (padding == PADDING_VALID)
      ? process_tile_row<0, 0, 0, pad_right, 4>
      : process_tile_row<1, 1, 0, pad_right, 4>,
  };
  const rowfunc process_middle_row[3] = {
    (padding == PADDING_VALID)
      ? process_tile_row<0, 0, 0, pad_right, 1>
      : process_tile_row<0, 1, 0, pad_right, 1>,
    (padding == PADDING_VALID)
      ? process_tile_row<0, 0, 0, pad_right, 2>
      : process_tile_row<0, 1, 0, pad_right, 2>,
    (padding == PADDING_VALID)
      ? process_tile_row<0, 0, 0, pad_right, 4>
      : process_tile_row<0, 1, 0, pad_right, 4>,
  };
  const rowfunc process_bottom_row[3] = {
    (padding == PADDING_VALID)
      ? process_tile_row<0, 0, pad_bottom, pad_right, 1>
      : process_tile_row<0, 1, pad_bottom, pad_right, 1>,
    (padding == PADDING_VALID)
      ? process_tile_row<0, 0, pad_bottom, pad_right, 2>
      : process_tile_row<0, 1, pad_bottom, pad_right, 2>,
    (padding == PADDING_VALID)
      ? process_tile_row<0, 0, pad_bottom, pad_right, 4>
      : process_tile_row<0, 1, pad_bottom, pad_right, 4>,
  };

  // Method to get an input pointer for the given tile row
  const auto get_inptr = [&input, &input_row_stride] (const int tile_i) {
    if (padding == PADDING_VALID) {
      return input + 2 * tile_i * input_row_stride;
    } else {
      return input + (2 * tile_i - (tile_i ? 1 : 0)) * input_row_stride;
    }
  };

  // Wrapper to process a row of tiles, covering all channels.
  const auto process_row =
    [tile_N, input_row_stride, input_col_stride, matrix_stride, matrix_row_stride, n_channels]
    (const rowfunc f[3], const T *inptr, T *outptr) {
      int rem_channels = n_channels;

      // While there remain channels to process continue to process the
      // row.
      for (; rem_channels >= 4; rem_channels -= 4, inptr += 4, outptr += 4) {
        f[2](tile_N, inptr, input_row_stride, input_col_stride, outptr, matrix_stride, matrix_row_stride);
      }
      for (; rem_channels >= 2; rem_channels -= 2, inptr += 2, outptr += 2) {
        f[1](tile_N, inptr, input_row_stride, input_col_stride, outptr, matrix_stride, matrix_row_stride);
      }
      if (rem_channels) {
        f[0](tile_N, inptr, input_row_stride, input_col_stride, outptr, matrix_stride, matrix_row_stride);
      }
  };

  // Process all rows of tiles in the tensor
  for (int tile_i = 0; tile_i < tile_M; tile_i++) {
    T* const m_row = matrix + tile_i * tile_N * matrix_row_stride;
    const T *row_inptr = get_inptr(tile_i);

    if (tile_i == 0) {
      // Top row of the input
      process_row(process_top_row, row_inptr, m_row);
    } else if (tile_i == tile_M - 1) {
      // Bottom row of the input
      process_row(process_bottom_row, row_inptr, m_row);
    } else {
      // Any other row of the input
      process_row(process_middle_row, row_inptr, m_row);
    }
  }
}

/*****************************************************************************/
template <typename T>
template <const int pad_top, const int pad_left,
          const int pad_bottom, const int pad_right,
          const int proc_channels>
void winograd::Winograd2x2_3x3GemmInput<T>::process_tile_row(
    const int tile_N,      // Number of tiles in the row
    const T* const input,  // Base input pointer (appropriate to batch, channel and row)
    const int input_row_stride,  // Stride between rows of the input
    const int input_col_stride,  // Stride between columns of the input
    T* const matrix,              // 1st output matrix (appropriate to batch, channel and row)
    const int matrix_stride,      // Stride between matrices
    const int matrix_row_stride   // Stride between rows of the output matrix
) {
  // Construct copies of the pointers
  const T *inptr = input;
  T *outptr = matrix;

  // Storage for the tensors x, X.T x, and X.T x X.
  T x[4][4][proc_channels], XTx[4][4][proc_channels], XTxX[4][4][proc_channels];

  // For every tile in the row
  for (int tile_j = 0; tile_j < tile_N; tile_j++) {
    // Determine the padding for the tile
    const int tile_pad_left = (tile_j == 0) ? pad_left : 0;
    const int tile_pad_right = (tile_j == tile_N - 1) ? pad_right : 0;

    // Load tile values. If this is the first tile in the row then we must load
    // all values, otherwise we can just load the final two columns of the input.
    for (int i = 0; i < 4; i++) {
      for (int j = ((tile_j == 0) ? 0 : 2); j < 4; j++) {
        // Fill with padding if required
        if (i < pad_top || 4 - pad_bottom <= i ||
            j < tile_pad_left || 4 - tile_pad_right <= j) {
          for (int c = 0; c < proc_channels; c++) {
            x[i][j][c] = static_cast<T>(0);  // Padding
          }
        } else {
          // Load values, note that the initial padding offsets the pointer we
          // were provided.
          for (int c = 0; c < proc_channels; c++) {
            const int row_offset = (i - pad_top) * input_row_stride;
            const int col_offset = (j - tile_pad_left) * input_col_stride;
            x[i][j][c] = inptr[row_offset + col_offset + c];
          }
        }
      }
    }

    // Compute the matrix X.T x.  Note, can elide operations depending on the
    // padding. Furthermore, if this isn't the left-most tile we can skip half
    // of the operations by copying results from the previous version of X.T x.
    // This latter optimisation can be simplified by unrolling the outermost
    // loop by two and by renaming the registers containing XTx.
    if (tile_j == 0) {
      for (int j = 0; j < 4; j++) {
        for (int c = 0; c < proc_channels; c++) {
          XTx[0][j][c] =  x[0][j][c] - x[2][j][c];
          XTx[1][j][c] =  x[1][j][c] + x[2][j][c];
          XTx[2][j][c] = -x[1][j][c] + x[2][j][c];
          XTx[3][j][c] =  x[1][j][c] - x[3][j][c];
        }
      }
    } else {
      for (int j = 0; j < 2; j++) {
        for (int c = 0; c < proc_channels; c++) {
          XTx[0][j][c] = XTx[0][j + 2][c];
          XTx[1][j][c] = XTx[1][j + 2][c];
          XTx[2][j][c] = XTx[2][j + 2][c];
          XTx[3][j][c] = XTx[3][j + 2][c];
        }
      }
      for (int j = 2; j < 4; j++) {
        for (int c = 0; c < proc_channels; c++) {
          XTx[0][j][c] =  x[0][j][c] - x[2][j][c];
          XTx[1][j][c] =  x[1][j][c] + x[2][j][c];
          XTx[2][j][c] = -x[1][j][c] + x[2][j][c];
          XTx[3][j][c] =  x[1][j][c] - x[3][j][c];
        }
      }
    }

    // Compute the matrix X.T x X. Note, can elide operations based on the
    // padding.
    for (int i = 0; i < 4; i++) {
      for (int c = 0; c < proc_channels; c++) {
        XTxX[i][0][c] =  XTx[i][0][c] - XTx[i][2][c];
        XTxX[i][1][c] =  XTx[i][1][c] + XTx[i][2][c];
        XTxX[i][2][c] = -XTx[i][1][c] + XTx[i][2][c];
        XTxX[i][3][c] =  XTx[i][1][c] - XTx[i][3][c];
      }
    }

    // Store the output matrix (X.T x X)
    for (int i = 0; i < 4; i++) {
      for (int j = 0; j < 4; j++) {
        // Get a pointer to the relevant output matrix
        T *mptr = outptr + (i*4 + j)*matrix_stride;

        // Write out the channels
        for (int c = 0; c < proc_channels; c++) {
          mptr[c] = XTxX[i][j][c];
        }
      }
    }

    // Update the pointers
    inptr += input_col_stride * ((tile_j == 0 && pad_left) ? 1 : 2);
    outptr += matrix_row_stride;
  }
}

/*****************************************************************************/
template <typename T>
void winograd::Winograd2x2_3x3GemmInputChannelwise<T>::execute(
    const T *inptr,
    const Tensor4DShape& input_shape,
    const PaddingType padding_type,
    const int tile_M,
    const int tile_N,
    T *outptr_base,
    const int matrix_stride,
    const int matrix_batch_stride,
    const int matrix_row_stride
) {
  const int n_channels = input_shape.n_channels;
  const int input_col_stride = n_channels;
  const int input_row_stride = input_shape.n_cols * input_col_stride;

  // Determine the padding and hence select appropriate methods for each tile.
  tilefunc fs[3][3];

  if (padding_type == PADDING_VALID) {
    constexpr int pad_top = 0;
    constexpr int pad_left = 0;
    const int pad_right = input_shape.n_cols % 2 == 0;

    fs[0][0] = process_tile<pad_top, pad_left, 0, 0>;
    fs[0][1] = process_tile<pad_top, 0, 0, 0>;
    fs[0][2] = (pad_right) ? process_tile<pad_top, 0, 0, 0> : process_tile<pad_top, 0, 0, 1>;

    fs[1][0] = process_tile<0, pad_left, 0, 0>;
    fs[1][1] = process_tile<0, 0, 0, 0>;
    fs[1][2] = (pad_right) ? process_tile<0, 0, 0, 0> : process_tile<0, 0, 0, 1>;

    if (input_shape.n_rows % 2 == 0) {
      constexpr int pad_bottom = 0;
      fs[2][0] = process_tile<0, pad_left, pad_bottom, 0>;
      fs[2][1] = process_tile<0, 0, pad_bottom, 0>;
      fs[2][2] = (pad_right) ? process_tile<0, 0, pad_bottom, 0> : process_tile<0, 0, pad_bottom, 1>;
    } else {
      constexpr int pad_bottom = 1;
      fs[2][0] = process_tile<0, pad_left, pad_bottom, 0>;
      fs[2][1] = process_tile<0, 0, pad_bottom, 0>;
      fs[2][2] = (pad_right) ? process_tile<0, 0, pad_bottom, 0> : process_tile<0, 0, pad_bottom, 1>;
    }
  } else {
    constexpr int pad_top = 1;
    constexpr int pad_left = 1;
    const int pad_right = input_shape.n_cols % 2 == 0;

    fs[0][0] = process_tile<pad_top, pad_left, 0, 0>;
    fs[0][1] = process_tile<pad_top, 0, 0, 0>;
    fs[0][2] = (pad_right) ? process_tile<pad_top, 0, 0, 1> : process_tile<pad_top, 0, 0, 2>;

    fs[1][0] = process_tile<0, pad_left, 0, 0>;
    fs[1][1] = process_tile<0, 0, 0, 0>;
    fs[1][2] = (pad_right) ? process_tile<0, 0, 0, 1> : process_tile<0, 0, 0, 2>;

    if (input_shape.n_rows % 2 == 0) {
      constexpr int pad_bottom = 1;
      fs[2][0] = process_tile<0, pad_left, pad_bottom, 0>;
      fs[2][1] = process_tile<0, 0, pad_bottom, 0>;
      fs[2][2] = (pad_right) ? process_tile<0, 0, pad_bottom, 1> : process_tile<0, 0, pad_bottom, 2>;
    } else {
      constexpr int pad_bottom = 2;
      fs[2][0] = process_tile<0, pad_left, pad_bottom, 0>;
      fs[2][1] = process_tile<0, 0, pad_bottom, 0>;
      fs[2][2] = (pad_right) ? process_tile<0, 0, pad_bottom, 1> : process_tile<0, 0, pad_bottom, 2>;
    }
  }

  // Process each tile in turn
  for (int batch = 0; batch < input_shape.n_batches; batch++) {
    const T* const input_base_batch = inptr + batch*input_shape.n_rows*input_shape.n_cols*n_channels;

    for (int tile_i = 0; tile_i < tile_M; tile_i++) {
      const int row_offset = (tile_i == 0) ? 0 : ((padding_type == PADDING_VALID) ? 0 : 1);
      const T* const input_base_row = input_base_batch + (2*tile_i - row_offset)*input_shape.n_cols*n_channels;

      // Select the set of functions for the row
      const int fs_i = (tile_i == 0) ? 0 : ((tile_i < tile_M - 1) ? 1 : 2);

      for (int tile_j = 0; tile_j < tile_N; tile_j++) {
        // Select the function for the column
        const int fs_j = (tile_j == 0) ? 0 : ((tile_j < tile_N - 1) ? 1 : 2);
        const auto f = fs[fs_i][fs_j];

        // Get pointers into the input and outputs
        const int col_offset = (tile_j == 0) ? 0 : ((padding_type == PADDING_VALID) ? 0 : 1);
        const T* const input_base_col = input_base_row + (2*tile_j - col_offset)*n_channels;
        T* const matrix_base = outptr_base + batch*matrix_batch_stride + (tile_i*tile_N + tile_j)*matrix_row_stride;
        f(n_channels, input_base_col, input_row_stride, input_col_stride,
          matrix_base, matrix_stride);
      }
    }
  }
}

template <typename T>
template <const int pad_top,
          const int pad_left,
          const int pad_bottom,
          const int pad_right>
void winograd::Winograd2x2_3x3GemmInputChannelwise<T>::process_tile(
    int n_channels,  // Number of channels in the tile
    const T* const input_base,
    const int input_row_stride,
    const int input_col_stride,
    T* const matrix_base,
    const int matrix_stride
) {
  // Copy pointers
  const T *inptr = input_base;
  T *outptr = matrix_base;

  // Process channels (modifies inptr, outptr and n_channels)
  _process_tile<pad_top, pad_left, pad_bottom, pad_right, 4>(
    n_channels, inptr, input_row_stride, input_col_stride,
    outptr, matrix_stride
  );
  _process_tile<pad_top, pad_left, pad_bottom, pad_right, 2>(
    n_channels, inptr, input_row_stride, input_col_stride,
    outptr, matrix_stride
  );
  _process_tile<pad_top, pad_left, pad_bottom, pad_right, 1>(
    n_channels, inptr, input_row_stride, input_col_stride,
    outptr, matrix_stride
  );
}

template <typename T>
template <const int pad_top,
          const int pad_left,
          const int pad_bottom,
          const int pad_right,
          const int proc_channels>
void winograd::Winograd2x2_3x3GemmInputChannelwise<T>::_process_tile(
    int &n_channels,
    const T* &inptr, const int input_row_stride, const int input_col_stride,
    T* &outptr, const int matrix_stride
) {
  // We use 4 pointers to point at matrices 0, 4, 8 and 12 and use three
  // offsets to access the intermediate matrices.
  T* outptrs[4] = {
    outptr,
    outptr + matrix_stride * 4,
    outptr + matrix_stride * 8,
    outptr + matrix_stride * 12
  };

  // The matrix X; zeroed to account for padding.
  T x[4][4];
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
      x[i][j] = 0;
    }
  }

  // The matrices X.T x and U
  T XTx[4][4], U[4][4];

  // Now progress through each channel
  for (; n_channels >= proc_channels; n_channels -= proc_channels) {
    for (int n = 0; n < proc_channels; n++) {
      // Load the matrix X
      for (int cell_i = pad_top, i = 0; cell_i < 4 - pad_bottom; cell_i++, i++) {
        for (int cell_j = pad_left, j = 0; cell_j < 4 - pad_right; cell_j++, j++) {
          x[cell_i][cell_j] = inptr[i*input_row_stride + j*input_col_stride];
        }
      }
      inptr++;

      // Compute the matrix X.T
      for (int j = 0; j < 4; j++) {
        XTx[0][j] = x[0][j] - x[2][j];
        XTx[1][j] = x[1][j] + x[2][j];
        XTx[2][j] = x[2][j] - x[1][j];
        XTx[3][j] = x[1][j] - x[3][j];
      }

      // Hence compute the matrix U
      for (int i = 0; i < 4; i++) {
        U[i][0] = XTx[i][0] - XTx[i][2];
        U[i][1] = XTx[i][1] + XTx[i][2];
        U[i][2] = XTx[i][2] - XTx[i][1];
        U[i][3] = XTx[i][1] - XTx[i][3];
      }

      // Store the matrix U
      for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
          outptrs[i][j * matrix_stride] = U[i][j];
        }
        outptrs[i]++;
      }
    }
  }

  // Update the output pointer for future calls
  outptr = outptrs[0];
}