blob: 4952029c9d353308cb8c4f37755214b8b0a71199 [file] [log] [blame]
Isabella Gottardif07d28d2018-02-06 14:52:43 +00001/*
Manuel Bottini8481d832019-12-10 15:28:40 +00002 * Copyright (c) 2017-2020 ARM Limited.
Isabella Gottardif07d28d2018-02-06 14:52:43 +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 */
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +010024#ifndef ARM_COMPUTE_CLGEMMCONVOLUTIONLAYER_H
25#define ARM_COMPUTE_CLGEMMCONVOLUTIONLAYER_H
Isabella Gottardif07d28d2018-02-06 14:52:43 +000026
27#include "arm_compute/runtime/IFunction.h"
28
29#include "arm_compute/core/CL/kernels/CLCol2ImKernel.h"
Isabella Gottardif07d28d2018-02-06 14:52:43 +000030#include "arm_compute/core/CL/kernels/CLIm2ColKernel.h"
31#include "arm_compute/core/CL/kernels/CLWeightsReshapeKernel.h"
32#include "arm_compute/core/Types.h"
Isabella Gottardif07d28d2018-02-06 14:52:43 +000033#include "arm_compute/runtime/CL/CLTensor.h"
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000034#include "arm_compute/runtime/CL/functions/CLActivationLayer.h"
Isabella Gottardif07d28d2018-02-06 14:52:43 +000035#include "arm_compute/runtime/CL/functions/CLGEMM.h"
36#include "arm_compute/runtime/CL/functions/CLGEMMLowpMatrixMultiplyCore.h"
Isabella Gottardif07d28d2018-02-06 14:52:43 +000037#include "arm_compute/runtime/IMemoryManager.h"
Michalis Spyroub27e13a2019-09-27 11:04:27 +010038#include "arm_compute/runtime/ITransformWeights.h"
39#include "arm_compute/runtime/IWeightsManager.h"
Georgios Pinitas26014cf2019-09-09 19:00:57 +010040#include "arm_compute/runtime/MemoryGroup.h"
Isabella Gottardif07d28d2018-02-06 14:52:43 +000041
42#include <memory>
43
44namespace arm_compute
45{
46class ICLTensor;
47
48/** Function to reshape and transpose the weights. This function calls the following kernels:
49 * -# @ref CLWeightsReshapeKernel
Isabella Gottardif07d28d2018-02-06 14:52:43 +000050 */
51class CLConvolutionLayerReshapeWeights : public IFunction
52{
53public:
54 /** Constructor */
Georgios Pinitasd8734b52017-12-22 15:27:52 +000055 CLConvolutionLayerReshapeWeights();
Isabella Gottardif07d28d2018-02-06 14:52:43 +000056 /** Set the input and output tensors.
57 *
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010058 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
Manuel Bottini8481d832019-12-10 15:28:40 +000059 * Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL/F16/F32.
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010060 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p weights.
61 * @param[out] output Destination tensor. Data types supported: Same as @p weights.
62 * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is only supported for NCHW data layout
Isabella Gottardif07d28d2018-02-06 14:52:43 +000063 */
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010064 void configure(const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, unsigned int num_groups = 1);
Georgios Pinitas78c00902018-01-09 17:33:11 +000065 /** Static function to check if given info will lead to a valid configuration of @ref CLConvolutionLayerReshapeWeights
66 *
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010067 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
Manuel Bottini8481d832019-12-10 15:28:40 +000068 * Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL/F16/F32.
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010069 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p weights.
70 * @param[in] output Destination tensor. Data types supported: Same as @p weights.
71 * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is only supported for NCHW data layout
Georgios Pinitas78c00902018-01-09 17:33:11 +000072 *
73 * @return a status
74 */
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010075 static Status validate(const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, unsigned int num_groups = 1);
Isabella Gottardif07d28d2018-02-06 14:52:43 +000076 // Inherited methods overridden:
77 void run() override;
78
79private:
Georgios Pinitasd8734b52017-12-22 15:27:52 +000080 CLWeightsReshapeKernel _weights_reshape_kernel;
Isabella Gottardif07d28d2018-02-06 14:52:43 +000081};
82
Michalis Spyroub27e13a2019-09-27 11:04:27 +010083namespace weights_transformations
84{
85/** Basic function to manage the reshape weights generated from @ref CLConvolutionLayerReshapeWeights */
86class CLConvolutionLayerReshapeWeightsTransform : public ITransformWeights
87{
88public:
89 /** Configures the @ref CLConvolutionLayerReshapeWeights function
90 *
Manuel Bottini8481d832019-12-10 15:28:40 +000091 * @param[in] input Input tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
Michalis Spyroub27e13a2019-09-27 11:04:27 +010092 * @param[in] biases Biases tensor. Data type supported: Same as @p input.
93 * @param[in] num_groups Number of groups when performing a grouped convolution.
94 */
95 void configure(const ICLTensor *input, const ICLTensor *biases, unsigned int num_groups)
96 {
97 _bias_bit = (biases != nullptr) ? 1 : 0;
98 _num_groups = num_groups;
99 _func.configure(input, biases, &_output, num_groups);
100 }
101
102 //Inherited method override
103 void run() override
104 {
105 _output.allocator()->allocate();
106 _func.run();
107 _reshape_run = true;
108 }
109
110 //Inherited method override
111 ICLTensor *get_weights() override
112 {
113 return &_output;
114 }
115
116 //Inherited method override
117 void release() override
118 {
119 _output.allocator()->free();
120 }
121
122 //Inherited method override
123 uint32_t uid() override
124 {
125 return ((0x9) | (_bias_bit << 7) | (_num_groups << 8));
126 }
127
128private:
129 CLTensor _output{};
130 CLConvolutionLayerReshapeWeights _func{};
131 int32_t _bias_bit{ 0 };
132 unsigned int _num_groups{ 0 };
133};
134} // namespace weights_transformations
135
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000136/** Basic function to compute the convolution layer. This function calls the following OpenCL kernels/functions:
137 *
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000138 * -# @ref CLIm2ColKernel
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100139 * -# @ref CLGEMM (if the data type is FP32 or FP16)
140 * -# @ref CLGEMMLowpMatrixMultiplyCore (if the data type is QASYMM8)
141 * -# @ref CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint (if the data type is QASYMM8)
Georgios Pinitas932491f2018-09-21 16:33:15 +0100142 * -# @ref CLCol2ImKernel (if NCHW data layout)
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000143 */
144class CLGEMMConvolutionLayer : public IFunction
145{
146public:
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100147 /** Constructor
Alex Gildayc357c472018-03-21 13:54:09 +0000148 *
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100149 * @param[in] memory_manager (Optional) Memory manager.
150 * @param[in] weights_manager (Optional) Weights manager.
Alex Gildayc357c472018-03-21 13:54:09 +0000151 */
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100152 CLGEMMConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr, IWeightsManager *weights_manager = nullptr);
Georgios Pinitas1562be32018-03-08 19:09:19 +0000153 /** Prevent instances of this class from being copied (As this class contains pointers) */
154 CLGEMMConvolutionLayer(const CLGEMMConvolutionLayer &) = delete;
155 /** Default move constructor */
156 CLGEMMConvolutionLayer(CLGEMMConvolutionLayer &&) = default;
157 /** Prevent instances of this class from being copied (As this class contains pointers) */
158 CLGEMMConvolutionLayer &operator=(const CLGEMMConvolutionLayer &) = delete;
159 /** Default move assignment operator */
160 CLGEMMConvolutionLayer &operator=(CLGEMMConvolutionLayer &&) = default;
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000161 /** Set the input and output tensors.
162 *
163 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
164 * while every optional dimension from 4 and above represent a batch of inputs.
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100165 * Data types supported: QASYMM8/F16/F32.
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000166 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
167 * Data type supported: Same as @p input or QASYMM8/QSYMM8_PER_CHANNEL when @p input is QASYMM8.
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000168 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
169 * Data type supported: Should match @p input data type, except for input of QASYMM8 type where biases should be of S32 type.
170 * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
171 * Data types supported: Same as @p input.
172 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
173 * @param[in] weights_info Specifies if the weights tensor has been reshaped with CLWeightsReshapeKernel. If this is not part of the fully connected layer the weights
Gian Marco Iodice5fc07aa2019-05-15 17:08:02 +0100174 * tensor has also been transposed with CLGEMMReshapeRHSMatrixKernel. Data type supported: Same as @p input.
Alex Gilday7da29b62018-03-23 14:16:00 +0000175 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000176 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100177 * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is only supported for NCHW data layout
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000178 */
Alex Gilday7da29b62018-03-23 14:16:00 +0000179 void configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info = WeightsInfo(),
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100180 const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(), unsigned int num_groups = 1);
Georgios Pinitas78c00902018-01-09 17:33:11 +0000181 /** Static function to check if given info will lead to a valid configuration of @ref CLGEMMConvolutionLayer.
182 *
183 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
184 * while every optional dimension from 4 and above represent a batch of inputs.
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100185 * Data types supported: QASYMM8/F16/F32.
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000186 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
187 * Data type supported: Same as @p input or QASYMM8/QSYMM8_PER_CHANNEL when @p input is QASYMM8.
Georgios Pinitas78c00902018-01-09 17:33:11 +0000188 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
189 * Data type supported: Should match @p input data type, except for input of QASYMM8 type where biases should be of S32 type.
190 * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
191 * Data types supported: Same as @p input.
192 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
193 * @param[in] weights_info Specifies if the weights tensor has been reshaped with CLWeightsReshapeKernel. If this is not part of the fully connected layer the weights
Gian Marco Iodice5fc07aa2019-05-15 17:08:02 +0100194 * tensor has also been transposed with CLGEMMReshapeRHSMatrixKernel. Data type supported: Same as @p input.
Alex Gilday7da29b62018-03-23 14:16:00 +0000195 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000196 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100197 * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is only supported for NCHW data layout
Alex Gildayc357c472018-03-21 13:54:09 +0000198 *
199 * @return a status
Georgios Pinitas78c00902018-01-09 17:33:11 +0000200 */
201 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 +0100202 const WeightsInfo &weights_info = WeightsInfo(), const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(), unsigned int num_groups = 1);
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000203
204 // Inherited methods overridden:
205 void run() override;
Georgios Pinitase0437672018-05-02 14:07:55 +0100206 void prepare() override;
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000207
208private:
209 /** Configures the appropriate matrix multiply routine
210 *
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100211 * @param[in] input Input tensor. Data types supported: QASYMM8/F16/F32.
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000212 * @param[in] weights Weights tensor. Data type supported: Same as @p input or QASYMM8/QSYMM8_PER_CHANNEL when @p input is QASYMM8.
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100213 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
214 * Data type supported: Should match @p input data type, except for input of QASYMM8 type where biases should be of S32 type.
215 * @param[in, out] output Output tensor. Data types supported: Same as @p input,
216 * except for input of QASYMM8 type where output should be of S32 type.
217 * @param[in] gemmlowp_output_stage GEMMLowp output stage info
Michele Di Giorgioebc3a902018-11-16 16:04:25 +0000218 * @param[in] gemm_3d_depth Depth of GEMM 3D
Gian Marco Iodicef3622be2019-07-29 14:27:16 +0100219 * @param[in] act_info Activation to apply after the matrix multiplication
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000220 */
Gian Marco Iodicef3622be2019-07-29 14:27:16 +0100221 void configure_mm(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const GEMMLowpOutputStageInfo &gemmlowp_output_stage, int gemm_3d_depth,
222 const ActivationLayerInfo &act_info);
Georgios Pinitas78c00902018-01-09 17:33:11 +0000223 /** Static function to check if given info will lead to a valid configuration of @ref CLGEMMConvolutionLayer matrix multiply routines
224 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000225 * @param[in] input Input tensor info. Data types supported: QASYMM8/F16/F32.
226 * @param[in] weights Weights tensor info. Data type supported: Same as @p input or QASYMM8/QSYMM8_PER_CHANNEL when @p input is QASYMM8.
227 * @param[in] biases Biases tensor info. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100228 * Data type supported: Should match @p input data type, except for input of QASYMM8 type where biases should be of S32 type.
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000229 * @param[in] output Output tensor info. Data types supported: Same as @p input,
230 * except for input of QASYMM8 type where output should be of S32 type.
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100231 * @param[in] gemmlowp_output_stage GEMMLowp output stage info
Michele Di Giorgioebc3a902018-11-16 16:04:25 +0000232 * @param[in] gemm_3d_depth Depth of GEMM 3D
233 * @param[in] skip_im2col Flag which specifies if im2col has to be skipped. i.e. 1x1 convolution with NHWC data layout.
Gian Marco Iodicef3622be2019-07-29 14:27:16 +0100234 * @param[in] act_info Activation to apply after the matrix multiplication
Georgios Pinitas78c00902018-01-09 17:33:11 +0000235 *
236 * @return a status
237 */
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100238 static Status validate_mm(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const GEMMLowpOutputStageInfo &gemmlowp_output_stage,
Gian Marco Iodicef3622be2019-07-29 14:27:16 +0100239 int gemm_3d_depth, bool skip_im2col, const ActivationLayerInfo &act_info);
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000240
241private:
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100242 MemoryGroup _memory_group;
243 IWeightsManager *_weights_manager;
244 CLConvolutionLayerReshapeWeights _reshape_weights;
245 weights_transformations::CLConvolutionLayerReshapeWeightsTransform _reshape_weights_managed;
246 CLIm2ColKernel _im2col_kernel;
247 CLGEMM _mm_gemm;
248 CLGEMMLowpMatrixMultiplyCore _mm_gemmlowp;
249 CLCol2ImKernel _col2im_kernel;
250 CLActivationLayer _activationlayer_function;
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000251
Georgios Pinitas1562be32018-03-08 19:09:19 +0000252 const ICLTensor *_original_weights;
253
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000254 CLTensor _im2col_output;
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000255 CLTensor _weights_reshaped;
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000256 CLTensor _gemm_output;
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000257
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100258 bool _skip_im2col;
Georgios Pinitas932491f2018-09-21 16:33:15 +0100259 bool _skip_col2im;
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000260 bool _is_quantized;
Gian Marco Iodicef3622be2019-07-29 14:27:16 +0100261 bool _fuse_activation;
Georgios Pinitase0437672018-05-02 14:07:55 +0100262 bool _is_prepared;
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000263};
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100264} // namespace arm_compute
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100265#endif /* ARM_COMPUTE_CLGEMMCONVOLUTIONLAYER_H */