blob: 9d418bebb4a527e1ffc8c208a333447280579755 [file] [log] [blame]
Pablo Tello8f43d742019-03-27 09:28:32 +00001/*
2 * Copyright (c) 2019 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
25#pragma once
26
27#include <utility>
28
29#include "arm_gemm_local.hpp"
30#include "arm_gemm.hpp"
31#include "winograd.hpp"
32
33namespace winograd
34{
35
36
37class IWinogradConvolutionLayer
38{
39 public:
40 virtual ~IWinogradConvolutionLayer() = default;
41
42 virtual unsigned int weight_transform_get_window(void) const = 0;
43 virtual void weight_transform_run(unsigned int start, unsigned int stop) = 0;
44
45 virtual ITransform& input_transform(void) = 0; // Expose the input transform
46 virtual ITransform& output_transform(void) = 0; // Expose the output transform
47 virtual arm_gemm::IGemmCommon *gemm(void) = 0; // Expose the underlying GEMM
48};
49
50/** Example of how to construct an ACL-like interface.
51 *
52 * Use `get_weight_storage_size`, `get_input_storage_size` and
53 * `get_output_storage_size` to allocate memory for the convolution engine.
54 * Then create a `WinogradConvolutionLayer`.
55 *
56 * Initialise the weights using `weights_transform.run(...)`.
57 *
58 * For each inference:
59 * 1. Transform the inputs to the Winograd domain using `input_transform.run(...)`
60 * 2. Perform a number of GEMMs using `gemms.run(...)`
61 * 3. Transform the output to the spatial domain using `output_transform.run(...)`
62 */
63template <int OutputTileRows, int OutputTileCols, int KernelRows, int KernelCols,
64 typename TIn, typename TInGEMM, typename TOutGEMM, typename TOut,
65 WinogradRoots Roots>
66class WinogradConvolutionLayer : public IWinogradConvolutionLayer
67{
68 private:
69 static constexpr int InnerTileRows = OutputTileRows + KernelRows - 1;
70 static constexpr int InnerTileCols = OutputTileCols + KernelCols - 1;
71 static constexpr int N_GEMMS = InnerTileRows * InnerTileCols;
72
73 const KernelShape _kernel_shape;
74 const Tensor4DShape _input_shape;
75 const PaddingType _padding;
76 const Tensor4DShape _output_shape;
77 const int _n_output_rows, _n_output_cols;
78 const int _kernel_matrix_stride, _kernel_matrix_row_stride;
79 const int _input_matrix_stride, _input_matrix_row_stride;
80 const int _output_matrix_stride, _output_matrix_row_stride;
81 const int _tile_rows, _tile_cols;
82 const int _m, _k, _n;
83
84 public:
85 using WinogradBase = winograd::WinogradGEMM<OutputTileRows, OutputTileCols, KernelRows, KernelCols, Roots>;
86 using WeightsTransform = typename WinogradBase::template WeightsTransform<TIn, TInGEMM>;
87 using InputTransform = typename WinogradBase::template InputTransform<TIn, TInGEMM>;
88 using WinogradConv = typename WinogradBase::template Convolution<TOut, TIn, TInGEMM, TOutGEMM>;
89 using OutputTransform = typename WinogradBase::template OutputTransform<TOutGEMM, TOut>;
90
91 /* Public member variables. */
92 WeightsTransform weights_transform; /** Operator to transform weights to Winograd domain. */
93 InputTransform _input_transform; /** Operator to transform input to Winograd domain. */
94 arm_gemm::UniqueGemmCommon<TInGEMM, TOutGEMM> gemms; /** Operator to perform multiple GEMMs. */
95 OutputTransform _output_transform; /** Operator to transform output from Winograd domain. */
96
97 /** Determine how much memory (in units of TIn) to allocate for the
98 * transformed weights.
99 */
100 static unsigned int get_weight_storage_size(
101 const int n_output_channels, /** Number of output feature maps. */
102 const int n_input_channels /** Number of input feature maps. */
103 );
104
105 static unsigned int get_weight_stride(
106 const int n_output_channels, /** Number of output feature maps. */
107 const int n_input_channels /** Number of input feature maps. */
108 );
109
110 static unsigned int get_weight_multi_stride(
111 const int n_output_channels, /** Number of output feature maps. */
112 const int n_input_channels /** Number of input feature maps. */
113 );
114
115 /** Determine how much memory (in units of TIn) to allocate for the
116 * transformed input.
117 */
118 static unsigned int get_input_storage_size(
119 const int n_batches, /** Number of batches in the input tensor. */
120 const int n_channels, /** Number of feature maps in the input tensor. */
121 const int n_rows, /** Number of rows in each feature map. */
122 const int n_cols, /** Number of columns in each feature map. */
123 const bool same_padding /** Use "SAME" padding, otherwise use "VALID". */
124 );
125
126 /** Get the row stride for the A matrix in the Winograd domain. */
127 static unsigned int get_input_stride(
128 const int n_batches, /** Number of batches in the input tensor. */
129 const int n_channels, /** Number of feature maps in the input tensor. */
130 const int n_rows, /** Number of rows in each feature map. */
131 const int n_cols, /** Number of columns in each feature map. */
132 const bool same_padding /** Use "SAME" padding, otherwise use "VALID". */
133 );
134
135 /** Get the stride between A matrices in the Winograd domain. */
136 static unsigned int get_input_multi_stride(
137 const int n_batches, /** Number of batches in the input tensor. */
138 const int n_channels, /** Number of feature maps in the input tensor. */
139 const int n_rows, /** Number of rows in each feature map. */
140 const int n_cols, /** Number of columns in each feature map. */
141 const bool same_padding /** Use "SAME" padding, otherwise use "VALID". */
142 );
143
144 /** Determine how much memory (in units of TOut) to allocate for the
145 * (Winograd domain) output.
146 */
147 static unsigned int get_output_storage_size(
148 const int n_batches, /** Number of batches in the output tensor. */
149 const int n_rows, /** Number of rows in each feature map of the input tensor. */
150 const int n_cols, /** Number of columns in each feature map of the input tensor. */
151 const int n_output_channels, /** Number of feature maps in the output tensor. */
152 const bool same_padding /** Use "SAME" padding, otherwise use "VALID". */
153 );
154
155 static unsigned int get_output_stride(
156 const int n_batches, /** Number of batches in the output tensor. */
157 const int n_rows, /** Number of rows in each feature map of the input tensor. */
158 const int n_cols, /** Number of columns in each feature map of the input tensor. */
159 const int n_output_channels, /** Number of feature maps in the output tensor. */
160 const bool same_padding /** Use "SAME" padding, otherwise use "VALID". */
161 );
162
163 static unsigned int get_output_multi_stride(
164 const int n_batches, /** Number of batches in the output tensor. */
165 const int n_rows, /** Number of rows in each feature map of the input tensor. */
166 const int n_cols, /** Number of columns in each feature map of the input tensor. */
167 const int n_output_channels, /** Number of feature maps in the output tensor. */
168 const bool same_padding /** Use "SAME" padding, otherwise use "VALID". */
169 );
170
171 /** Get the shape (rows, cols) of a feature map of the output tensor. */
172 static std::pair<int, int> get_output_feature_map_shape(
173 const int n_input_rows, /** Number of rows in the input feature map. */
174 const int n_input_cols, /** Number of columns in the input feature map. */
175 const bool same_padding /** Use "SAME" padding, otherwise use "VALID". */
176 );
177
178 /** Create a new Winograd convolution layer.
179 */
180 WinogradConvolutionLayer(
181 const arm_gemm::CPUInfo &cpuinfo, /** Describes CPU properties. */
182 const int n_threads, /** Maximum number of threads used to execute the convolution. */
183 const int n_batches, /** Number of batches in the input and output tensors. */
184 const int n_input_channels, /** Number of feature maps in a batch of the input tensor. */
185 const int n_input_rows, /** Number of rows in a feature map of the input tensor. */
186 const int n_input_cols, /** Number of columns in a feature map of the input tensor. */
187 const int n_output_channels, /** Number of feature maps in the output tensor. */
188 const bool same_padding, /** Use "SAME" padding, otherwise use "VALID". */
189 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. */
190 TInGEMM* const weights_storage, /** Pointer to storage for weight tensor in the Winograd domain. Must be at least the size returned by `get_weight_storage_size`. */
191 const TIn* const input, /** Pointer to NHWC ordered input tensor, in the spatial domain. */
192 TInGEMM* 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`. */
193 const TOut* const biases, /** Pointer to biases vector. Pass nullptr if no bias is provided. */
194 TOut* const output, /** Pointer to NHWC ordered output tensor, in the spatial domain. */
195 TOutGEMM* 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`. */
196 const bool pretranspose_B=true, /** Hint that the B matrix can be pretransposed. */
197 arm_gemm::GemmConfig *gemm_cfg=nullptr /** Pointer to GEMM configuration. */
198 );
199
200 /* Utility methods for interacting with the layer. */
201 unsigned int weight_transform_get_window(void) const;
202 void weight_transform_run(const unsigned int start, const unsigned int stop);
203
204 ITransform& input_transform(void);
205 ITransform& output_transform(void);
206
207 /* Get a pointer to the GEMM underlying the Winograd transform. */
208 arm_gemm::IGemmCommon *gemm(void);
209};
210
211}