aboutsummaryrefslogtreecommitdiff
path: root/src/core/NEON/kernels/winograd/winograd_layer.cpp
blob: 689ecba5fb4719b07a728b6ef6d6f978ee09c29d (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
/*
 * 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.
 */

#include "convolution.hpp"
#include "winograd_layer.hpp"
#include "tensor.hpp"


/** Determine how much memory (in units of TIn) to allocate for the transformed
 * weights.
 */
template <
  int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols,
  typename TIn, typename TOut
>
unsigned int WinogradConvolutionLayer<
  OutputTileRows, OutputTileCols, KernelRows, KernelCols, TIn, TOut
>::get_weight_storage_size(
  const int n_output_channels,  /** Number of output feature maps. */
  const int n_input_channels    /** Number of input feature maps. */
)
{
  const KernelShape shape(
    n_output_channels, KernelRows, KernelCols, n_input_channels
  );
  return static_cast<unsigned int>(
    // WinogradConv returns the size in bytes, we divide by `sizeof(TIn)` to
    // express that in units of TIn.
    WinogradConv::get_kernel_storage_size(shape) / sizeof(TIn)
  );
}


/** Determine how much memory (in units of TIn) to allocate for the transformed
 * input.
 */
template <
  int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols,
  typename TIn, typename TOut
>
unsigned int WinogradConvolutionLayer<
  OutputTileRows, OutputTileCols, KernelRows, KernelCols, TIn, TOut
>::get_input_storage_size(
  const int n_batches,     /** Number of batches in the input tensor. */
  const int n_channels,    /** Number of feature maps in the input tensor. */
  const int n_rows,        /** Number of rows in each feature map. */
  const int n_cols,        /** Number of columns in each feature map. */
  const bool same_padding  /** Use "SAME" padding, otherwise use "VALID". */
)
{
  // Construct shapes for the input and kernel tensors.
  const Tensor4DShape input_shape(n_batches, n_rows, n_cols, n_channels);
  const KernelShape kern_shape(1, KernelRows, KernelCols, n_channels);
  const PaddingType padding = (same_padding) ? PADDING_SAME : PADDING_VALID;

  // Return the size, converted into units of TIn
  return static_cast<unsigned int>(
    WinogradConv::get_input_storage_size(kern_shape, input_shape, padding) /
    sizeof(TIn)
  );
}


/** Determine how much memory (in units of TOut) to allocate for the (Winograd
 * domain) output.
 */
template <
  int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols,
  typename TIn, typename TOut
>
unsigned int WinogradConvolutionLayer<
  OutputTileRows, OutputTileCols, KernelRows, KernelCols, TIn, TOut
>::get_output_storage_size(
  const int n_batches,          /** Number of batches in the output tensor. */
  const int n_rows,             /** Number of rows in each feature map of the input tensor. */
  const int n_cols,             /** Number of columns in each feature map of the input tensor. */
  const int n_output_channels,  /** Number of feature maps in the output tensor. */
  const bool same_padding       /** Use "SAME" padding, otherwise use "VALID". */
)
{
  // Construct shapes for the input and kernel tensors.
  const Tensor4DShape input_shape(n_batches, n_rows, n_cols, 1);
  const KernelShape kern_shape(n_output_channels, KernelRows, KernelCols, 1);
  const PaddingType padding = (same_padding) ? PADDING_SAME : PADDING_VALID;

  // Return the size, converted into units of TOut
  return static_cast<unsigned int>(
    WinogradConv::get_output_storage_size(kern_shape, input_shape, padding) /
    sizeof(TOut)
  );
}


/** Get the shape (rows, cols) of a feature map of the output tensor. */
template <
  int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols,
  typename TIn, typename TOut
>
std::pair<int, int> WinogradConvolutionLayer<
  OutputTileRows, OutputTileCols, KernelRows, KernelCols, TIn, TOut
>::get_output_feature_map_shape(
  const int n_input_rows,  /** Number of rows in the input feature map. */
  const int n_input_cols,  /** Number of columns in the input feature map. */
  const bool same_padding  /** Use "SAME" padding, otherwise use "VALID". */
)
{
  // Construct shapes for the input and kernel tensors.
  const Tensor4DShape input_shape(1, n_input_rows, n_input_cols, 1);
  const KernelShape kern_shape(1, KernelRows, KernelCols, 1);
  const PaddingType padding = (same_padding) ? PADDING_SAME : PADDING_VALID;

  // Compute the new shape
  const auto output_shape = WinogradConv::get_output_shape(
    kern_shape, input_shape, padding
  );

  return std::make_pair(output_shape.n_rows, output_shape.n_cols);
}


/** Create a new Winograd convolution layer.
 */
template <
  int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols,
  typename TIn, typename TOut
>
WinogradConvolutionLayer<OutputTileRows, OutputTileCols, KernelRows, KernelCols, TIn, TOut>::
WinogradConvolutionLayer(
  const int n_batches,          /** Number of batches in the input and output tensors. */
  const int n_input_channels,   /** Number of feature maps in a batch of the input tensor. */
  const int n_input_rows,       /** Number of rows in a feature map of the input tensor. */
  const int n_input_cols,       /** Number of columns in a feature map of the input tensor. */
  const int n_output_channels,  /** Number of feature maps in the output tensor. */
  const bool same_padding,      /** Use "SAME" padding, otherwise use "VALID". */
  const TIn* const weights,     /** Pointer to weight tensor in spatial domain. Must be ordered as "Height x Rows x Input Feature Maps x Output Feature Maps. */
  TIn* const winograd_weights,  /** Pointer to storage for weight tensor in the Winograd domain. Must be at least the size returned by `get_weight_storage_size`. */
  const TIn* const input,       /** Pointer to NHWC ordered input tensor, in the spatial domain. */
  TIn* const winograd_input,    /** Pointer to working space for the input tensor in the Winograd domain. Must be at least the size returned by `get_input_storage_size`. */
  TOut* const output,           /** Pointer to NHWC ordered output tensor, in the spatial domain. */
  TOut* const winograd_output   /** Pointer to working space for the output tensor in the Winograd domain. Must be at least the size returned by `get_output_storage_size`. */
) : _kernel_shape(n_output_channels, KernelRows, KernelCols, n_input_channels),
    _input_shape(n_batches, n_input_rows, n_input_cols, n_input_channels),
    _padding(same_padding ? PADDING_SAME : PADDING_VALID),
    _output_shape(WinogradConv::get_output_shape(_kernel_shape, _input_shape, _padding)),
    _n_output_rows(_output_shape.n_rows),
    _n_output_cols(_output_shape.n_cols),
    _kernel_matrix_stride(WinogradConv::get_kernel_matrix_stride(_kernel_shape)),
    _kernel_matrix_row_stride(roundup(n_output_channels, WinogradConv::N_BLOCK)),
    _input_matrix_stride(WinogradConv::get_input_matrix_stride(_kernel_shape, _input_shape, _padding)),
    _input_matrix_row_stride(n_input_channels),
    _output_matrix_stride(WinogradConv::get_output_matrix_stride(_kernel_shape, _input_shape, _padding)),
    _output_matrix_row_stride(_kernel_matrix_row_stride),
    _tile_rows(iceildiv(_n_output_rows, OutputTileRows)),
    _tile_cols(iceildiv(_n_output_cols, OutputTileCols)),
    _m(n_batches * _tile_rows * _tile_cols),
    _k(n_input_channels),
    _n(n_output_channels),
    weights_transform(
      weights, winograd_weights,
      _kernel_matrix_stride, _kernel_matrix_row_stride,
      n_output_channels, n_input_channels
    ),
    input_transform(
      input, n_batches, n_input_rows, n_input_cols, n_input_channels, _padding,
      winograd_input, _input_matrix_stride, _input_matrix_row_stride
    ),
    gemms(
      WinogradBase::N_GEMMS, _m, _k, _n,
      _input_matrix_stride, _input_matrix_row_stride,
      _kernel_matrix_stride, _kernel_matrix_row_stride,
      _output_matrix_stride, _output_matrix_row_stride,
      winograd_input, winograd_weights, winograd_output
    ),
    output_transform(
      winograd_output, _output_matrix_stride, _output_matrix_row_stride,
      output, n_batches, _n_output_rows, _n_output_cols, n_output_channels
    )
{
}

// Instantiate valid implementations.
template class WinogradConvolutionLayer<2, 2, 3, 3, float, float>;
template class WinogradConvolutionLayer<4, 4, 3, 3, float, float>;