blob: 660f55953e8ba741651ffcef687aef869c4f8254 [file] [log] [blame]
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +00001/*
George Wort2d7e6832019-02-22 16:37:41 +00002 * Copyright (c) 2017-2019 ARM Limited.
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +00003 *
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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_NEGEMMCONVOLUTIONLAYER_H
25#define ARM_COMPUTE_NEGEMMCONVOLUTIONLAYER_H
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000026
27#include "arm_compute/runtime/IFunction.h"
28
29#include "arm_compute/core/NEON/kernels/NECol2ImKernel.h"
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000030#include "arm_compute/core/NEON/kernels/NEGEMMTranspose1xWKernel.h"
31#include "arm_compute/core/NEON/kernels/NEIm2ColKernel.h"
32#include "arm_compute/core/NEON/kernels/NEWeightsReshapeKernel.h"
33#include "arm_compute/core/Types.h"
Michalis Spyrou1a569a32019-09-10 17:20:34 +010034#include "arm_compute/runtime/IWeightsManager.h"
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000035#include "arm_compute/runtime/MemoryGroup.h"
Gian Marco Iodice597a8562018-08-01 15:06:06 +010036#include "arm_compute/runtime/NEON/functions/NEGEMM.h"
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000037#include "arm_compute/runtime/NEON/functions/NEGEMMLowpMatrixMultiplyCore.h"
38#include "arm_compute/runtime/NEON/functions/NEGEMMLowpOutputStage.h"
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +010039#include "arm_compute/runtime/NEON/functions/NEReshapeLayer.h"
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000040#include "arm_compute/runtime/Tensor.h"
41
42#include <memory>
43
44namespace arm_compute
45{
46class ITensor;
47
Gian Marco Iodice597a8562018-08-01 15:06:06 +010048/** Function to reshape the weights. This function calls the following kernel:
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000049 * -# @ref NEWeightsReshapeKernel
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000050 */
51class NEConvolutionLayerReshapeWeights : public IFunction
52{
53public:
54 /** Constructor */
Gian Marco Iodice597a8562018-08-01 15:06:06 +010055 NEConvolutionLayerReshapeWeights();
Michalis Spyrou1a569a32019-09-10 17:20:34 +010056 /** Prevent instances of this class from being copied (As this class contains pointers) */
57 NEConvolutionLayerReshapeWeights(const NEConvolutionLayerReshapeWeights &) = delete;
58 /** Default move constructor */
59 NEConvolutionLayerReshapeWeights(NEConvolutionLayerReshapeWeights &&) = default;
60 /** Prevent instances of this class from being copied (As this class contains pointers) */
61 NEConvolutionLayerReshapeWeights &operator=(const NEConvolutionLayerReshapeWeights &) = delete;
62 /** Default move assignment operator */
63 NEConvolutionLayerReshapeWeights &operator=(NEConvolutionLayerReshapeWeights &&) = default;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000064 /** Set the input and output tensors.
65 *
Georgios Pinitas6e1791b2019-12-02 19:01:25 +000066 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL/F16/F32.
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +000067 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
68 * Data type supported: Same as @p weights, S32 if @p weights is QASYMM8/QASYMM8_SIGNED.
Gian Marco Iodice597a8562018-08-01 15:06:06 +010069 * @param[out] output Destination tensor. Data types supported: Same as @p weights.
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000070 */
Gian Marco Iodice597a8562018-08-01 15:06:06 +010071 void configure(const ITensor *weights, const ITensor *biases, ITensor *output);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000072 /** Static function to check if given info will lead to a valid configuration of @ref NEConvolutionLayerReshapeWeights
73 *
Georgios Pinitas6e1791b2019-12-02 19:01:25 +000074 * @param[in] weights Weights tensor info. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL/F16/F32.
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +000075 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
76 * Data type supported: Same as @p weights, S32 if @p weights is QASYMM8/QASYMM8_SIGNED.
77 * @param[in] output Destination tensor. Data types supported: Same as @p weights.
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000078 *
79 * @return an error status
80 */
Gian Marco Iodice597a8562018-08-01 15:06:06 +010081 static Status validate(const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000082
83 // Inherited methods overridden:
84 void run() override;
85
86private:
Gian Marco Iodice597a8562018-08-01 15:06:06 +010087 NEWeightsReshapeKernel _weights_reshape_kernel;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000088};
89
Michalis Spyrou1a569a32019-09-10 17:20:34 +010090namespace weights_transformations
91{
92/** Basic function to manage the reshape weights generated from @ref NEConvolutionLayerReshapeWeights */
93class NEConvolutionLayerReshapeWeightsTransform : public ITransformWeights
94{
95public:
96 void configure(const ITensor *input, const ITensor *biases)
97 {
98 _bias_bit = (biases != nullptr) ? 1 : 0;
99 _func.configure(input, biases, &_output);
100 }
101
102 void run() override
103 {
104 _output.allocator()->allocate();
105 _func.run();
106 _reshape_run = true;
107 }
108
109 ITensor *get_weights() override
110 {
111 return &_output;
112 }
113
114 void release() override
115 {
116 _output.allocator()->free();
117 }
118
119 uint32_t uid() override
120 {
121 return ((0x8) | (_bias_bit << 7));
122 }
123
124 bool is_reshape_run()
125 {
126 return _reshape_run;
127 }
128
129private:
130 Tensor _output{};
131 NEConvolutionLayerReshapeWeights _func{};
132 int32_t _bias_bit{ 0 };
133};
134} // namespace weights_transformations
135
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100136/** Basic function to compute the convolution layer. This function calls the following NEON kernels/functions:
137 *
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000138 * -# @ref NEIm2ColKernel
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100139 * -# @ref NEGEMM (if the data type is FP32 or FP16)
Georgios Pinitas6e1791b2019-12-02 19:01:25 +0000140 * -# @ref NEGEMMLowpMatrixMultiplyCore (if the data type is QASYMM8/QASYMM8_SIGNED)
141 * -# @ref NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint (if the data type is QASYMM8/QASYMM8_SIGNED)
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100142 * -# @ref NEArithmeticAdditionKernel (if biases != nullptr and we have a 1x1 convolution with the NHWC data layout)
Georgios Pinitas932491f2018-09-21 16:33:15 +0100143 * -# @ref NECol2ImKernel (if NCHW data layout)
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100144 *
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000145 */
146class NEGEMMConvolutionLayer : public IFunction
147{
148public:
149 /** Constructor */
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100150 NEGEMMConvolutionLayer(const std::shared_ptr<IMemoryManager> &memory_manager = nullptr, IWeightsManager *weights_manager = nullptr);
Georgios Pinitas1562be32018-03-08 19:09:19 +0000151 /** Prevent instances of this class from being copied (As this class contains pointers) */
152 NEGEMMConvolutionLayer(const NEGEMMConvolutionLayer &) = delete;
153 /** Default move constructor */
154 NEGEMMConvolutionLayer(NEGEMMConvolutionLayer &&) = default;
155 /** Prevent instances of this class from being copied (As this class contains pointers) */
156 NEGEMMConvolutionLayer &operator=(const NEGEMMConvolutionLayer &) = delete;
157 /** Default move assignment operator */
158 NEGEMMConvolutionLayer &operator=(NEGEMMConvolutionLayer &&) = default;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000159 /** Set the input and output tensors.
160 *
161 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
162 * while every optional dimension from 4 and above represent a batch of inputs.
Georgios Pinitas6e1791b2019-12-02 19:01:25 +0000163 * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
164 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL/F16/F32.
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000165 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
Georgios Pinitas6e1791b2019-12-02 19:01:25 +0000166 * Data type supported: Should match @p input data type, except for input of QASYMM8/QASYMM8_SIGNED type where biases should be of S32 type.
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000167 * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
168 * Data types supported: Same as @p input.
169 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
170 * @param[in] weights_info Specifies if the weights tensor has been reshaped with NEWeightsReshapeKernel. If this is not part of the fully connected layer the weights
171 * tensor has also been transposed with NEGEMMTranspose1xWKernel. Data type supported: Same as @p input.
Alex Gilday7da29b62018-03-23 14:16:00 +0000172 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000173 * @param[in] act_info (Optional) Activation layer information in case of a fused activation. Only RELU, BOUNDED_RELU and LU_BOUNDED_RELU supported.
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100174 * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is not supported
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000175 */
Alex Gilday7da29b62018-03-23 14:16:00 +0000176 void configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info = WeightsInfo(),
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100177 const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(), unsigned int num_groups = 1);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000178 /** Static function to check if given info will lead to a valid configuration of @ref NEGEMMConvolutionLayer
179 *
Georgios Pinitas6e1791b2019-12-02 19:01:25 +0000180 * @param[in] input Source tensor info. 3 lower dimensions represent a single input [width, height, IFM],
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000181 * while every optional dimension from 4 and above represent a batch of inputs.
Georgios Pinitas6e1791b2019-12-02 19:01:25 +0000182 * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
183 * @param[in] weights Weights tensor info. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL/F16/F32.
184 * @param[in] biases Biases tensor info. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
185 * Data type supported: Should match @p input data type, except for input of QASYMM8/QASYMM8_SIGNED type where biases should be of S32 type.
186 * @param[in] output Destination tensor info. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000187 * Data types supported: Same as @p input.
188 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
189 * @param[in] weights_info Specifies if the weights tensor has been reshaped with NEWeightsReshapeKernel. If this is not part of the fully connected layer the weights
190 * tensor has also been transposed with NEGEMMTranspose1xWKernel. Data type supported: Same as @p input.
Alex Gilday7da29b62018-03-23 14:16:00 +0000191 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000192 * @param[in] act_info (Optional) Activation layer information in case of a fused activation. Only RELU, BOUNDED_RELU and LU_BOUNDED_RELU supported.
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100193 * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is not supported
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000194 *
195 * @return a status
196 */
197 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100198 const WeightsInfo &weights_info = WeightsInfo(), const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(), unsigned int num_groups = 1);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000199
200 // Inherited methods overridden:
201 void run() override;
Georgios Pinitas72219332018-06-05 14:56:06 +0100202 void prepare() override;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000203
204private:
205 /** Configures the appropriate matrix multiply routine
206 *
Georgios Pinitas6e1791b2019-12-02 19:01:25 +0000207 * @param[in] input Input tensor. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
208 * @param[in] weights Weights tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL/F16/F32.
George Wort2d7e6832019-02-22 16:37:41 +0000209 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
Georgios Pinitas6e1791b2019-12-02 19:01:25 +0000210 * Data type supported: Should match @p input data type, except for input of QASYMM8/QASYMM8_SIGNED type where biases should be of S32 type.
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100211 * @param[out] output Output tensor. Data types supported: Same as @p input,
Georgios Pinitas6e1791b2019-12-02 19:01:25 +0000212 * except for input of QASYMM8/QASYMM8_SIGNED type where output should be of S32 type.
George Wort2d7e6832019-02-22 16:37:41 +0000213 * @param[in] act_info (Optional) Activation layer information in case of a fused activation. Only RELU, BOUNDED_RELU and LU_BOUNDED_RELU supported.
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100214 * @param[in] gemm_3d_depth (Optional) Depth of GEMM 3D (Defaults to 1)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000215 */
George Wort2d7e6832019-02-22 16:37:41 +0000216 void configure_mm(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const ActivationLayerInfo &act_info = ActivationLayerInfo(), int gemm_3d_depth = 1);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100217 /** Static function to check if given info will lead to a valid configuration of @ref NEGEMMConvolutionLayer matrix multiply routines
218 *
Georgios Pinitas6e1791b2019-12-02 19:01:25 +0000219 * @param[in] input Input tensor info. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
220 * @param[in] weights Weights tensor info. Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL/F16/F32.
221 * @param[in] biases Biases tensor info. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
222 * Data type supported: Should match @p input data type, except for input of QASYMM8/QASYMM8_SIGNED type where biases should be of S32 type.
223 * @param[in] output Output tensor info. Data types supported: Same as @p input,
224 * except for input of QASYMM8/QASYMM8_SIGNED type where output should be of S32 type.
George Wort2d7e6832019-02-22 16:37:41 +0000225 * @param[in] act_info (Optional) Activation layer information in case of a fused activation. Only RELU, BOUNDED_RELU and LU_BOUNDED_RELU supported.
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100226 * @param[in] gemm_3d_depth (Optional) Depth of GEMM 3D (Defaults to 1)
227 * @param[in] skip_im2col (Optional) Flag which specifies if im2col has to be skipped. i.e. 1x1 convolution with NHWC data layout. (Default to false)
228 *
229 * @return a status
230 */
George Wort2d7e6832019-02-22 16:37:41 +0000231 static Status validate_mm(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const ActivationLayerInfo &act_info = ActivationLayerInfo(),
232 int gemm_3d_depth = 1, bool skip_im2col = false);
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100233 /** Static function to check if GEMM3D is supported in @ref NEGEMM or in @ref NEGEMMLowpMatrixMultiplyCore
234 *
Georgios Pinitas6e1791b2019-12-02 19:01:25 +0000235 * @param[in] input_info Input tensor info. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
236 * @param[in] weights_info Weights tensor info. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
George Wort2d7e6832019-02-22 16:37:41 +0000237 * @param[in] act_info Activation layer information in case of a fused activation. Only RELU, BOUNDED_RELU and LU_BOUNDED_RELU supported.
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100238 * @param[in] gemm_3d_depth Depth of GEMM 3D
239 * @param[in] skip_im2col Flag which specifies if im2col has to be skipped. i.e. 1x1 convolution with NHWC data layout
240 *
241 * @return a status
242 */
Giorgio Arena7a669a82019-11-13 17:07:13 +0000243 static Status validate_gemm3d(const ITensorInfo *input_info, const ITensorInfo *weights_info, const ActivationLayerInfo &act_info, int gemm_3d_depth, bool skip_im2col);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000244
245private:
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100246 MemoryGroup _memory_group;
247 IWeightsManager *_weights_manager;
248 NEConvolutionLayerReshapeWeights _reshape_weights;
249 weights_transformations::NEConvolutionLayerReshapeWeightsTransform _reshape_weights_managed;
250 NEIm2ColKernel _im2col_kernel;
251 NEGEMM _mm_gemm;
252 NEGEMMLowpMatrixMultiplyCore _mm_gemmlowp;
253 NECol2ImKernel _col2im_kernel;
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100254 NEReshapeLayer _reshape_layer;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000255
Georgios Pinitas1562be32018-03-08 19:09:19 +0000256 const ITensor *_original_weights;
257
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100258 Tensor _im2col_output;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000259 Tensor _weights_reshaped;
260 Tensor _gemm_output;
261 Tensor _tmp_output;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000262
Michalis Spyroue2503892018-04-23 15:17:31 +0100263 DataLayout _data_layout;
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100264
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100265 bool _skip_im2col;
266 bool _skip_col2im;
267 bool _is_quantized;
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100268 bool _is_prepared;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000269};
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100270} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000271#endif /* ARM_COMPUTE_NECONVOLUTIONGEMMLAYER_H */