blob: b304576f33d25817df555c99e29ba40a631471c8 [file] [log] [blame]
Isabella Gottardif07d28d2018-02-06 14:52:43 +00001/*
Michele Di Giorgioebc3a902018-11-16 16:04:25 +00002 * Copyright (c) 2017-2019 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 */
24#ifndef __ARM_COMPUTE_CLGEMMCONVOLUTIONLAYER_H__
25#define __ARM_COMPUTE_CLGEMMCONVOLUTIONLAYER_H__
26
27#include "arm_compute/runtime/IFunction.h"
28
29#include "arm_compute/core/CL/kernels/CLCol2ImKernel.h"
giuros01164a2722018-11-20 18:34:46 +000030#include "arm_compute/core/CL/kernels/CLElementwiseOperationKernel.h"
Isabella Gottardif07d28d2018-02-06 14:52:43 +000031#include "arm_compute/core/CL/kernels/CLGEMMInterleave4x4Kernel.h"
32#include "arm_compute/core/CL/kernels/CLGEMMMatrixMultiplyKernel.h"
Isabella Gottardif07d28d2018-02-06 14:52:43 +000033#include "arm_compute/core/CL/kernels/CLIm2ColKernel.h"
34#include "arm_compute/core/CL/kernels/CLWeightsReshapeKernel.h"
35#include "arm_compute/core/Types.h"
36#include "arm_compute/runtime/CL/CLMemoryGroup.h"
37#include "arm_compute/runtime/CL/CLTensor.h"
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000038#include "arm_compute/runtime/CL/functions/CLActivationLayer.h"
Isabella Gottardif07d28d2018-02-06 14:52:43 +000039#include "arm_compute/runtime/CL/functions/CLGEMM.h"
40#include "arm_compute/runtime/CL/functions/CLGEMMLowpMatrixMultiplyCore.h"
41#include "arm_compute/runtime/CL/functions/CLGEMMLowpOutputStage.h"
Gian Marco Iodicedff601d2018-08-09 13:28:41 +010042#include "arm_compute/runtime/CL/functions/CLReshapeLayer.h"
Isabella Gottardif07d28d2018-02-06 14:52:43 +000043#include "arm_compute/runtime/IMemoryManager.h"
44
45#include <memory>
46
47namespace arm_compute
48{
49class ICLTensor;
50
51/** Function to reshape and transpose the weights. This function calls the following kernels:
52 * -# @ref CLWeightsReshapeKernel
Isabella Gottardif07d28d2018-02-06 14:52:43 +000053 */
54class CLConvolutionLayerReshapeWeights : public IFunction
55{
56public:
57 /** Constructor */
Georgios Pinitasd8734b52017-12-22 15:27:52 +000058 CLConvolutionLayerReshapeWeights();
Isabella Gottardif07d28d2018-02-06 14:52:43 +000059 /** Set the input and output tensors.
60 *
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010061 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
62 * Data type supported: QASYMM8/F16/F32.
63 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p weights.
64 * @param[out] output Destination tensor. Data types supported: Same as @p weights.
65 * @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 +000066 */
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010067 void configure(const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, unsigned int num_groups = 1);
Georgios Pinitas78c00902018-01-09 17:33:11 +000068 /** Static function to check if given info will lead to a valid configuration of @ref CLConvolutionLayerReshapeWeights
69 *
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010070 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
71 * Data type supported: QASYMM8/F16/F32.
72 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p weights.
73 * @param[in] output Destination tensor. Data types supported: Same as @p weights.
74 * @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 +000075 *
76 * @return a status
77 */
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010078 static Status validate(const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, unsigned int num_groups = 1);
Isabella Gottardif07d28d2018-02-06 14:52:43 +000079 // Inherited methods overridden:
80 void run() override;
81
82private:
Georgios Pinitasd8734b52017-12-22 15:27:52 +000083 CLWeightsReshapeKernel _weights_reshape_kernel;
Isabella Gottardif07d28d2018-02-06 14:52:43 +000084};
85
86/** Basic function to compute the convolution layer. This function calls the following OpenCL kernels/functions:
87 *
Isabella Gottardif07d28d2018-02-06 14:52:43 +000088 * -# @ref CLIm2ColKernel
Gian Marco Iodice68a3f562018-07-26 11:44:03 +010089 * -# @ref CLGEMM (if the data type is FP32 or FP16)
90 * -# @ref CLGEMMLowpMatrixMultiplyCore (if the data type is QASYMM8)
91 * -# @ref CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint (if the data type is QASYMM8)
giuros01164a2722018-11-20 18:34:46 +000092 * -# @ref CLElementwiseOperationKernel for addition (if biases != nullptr and we have a 1x1 convolution with the NHWC data layout)
Georgios Pinitas932491f2018-09-21 16:33:15 +010093 * -# @ref CLCol2ImKernel (if NCHW data layout)
Isabella Gottardif07d28d2018-02-06 14:52:43 +000094 */
95class CLGEMMConvolutionLayer : public IFunction
96{
97public:
Alex Gildayc357c472018-03-21 13:54:09 +000098 /** Default constructor
99 *
100 * @param[in] memory_manager (Optional) Memory manager.
101 */
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000102 CLGEMMConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
Georgios Pinitas1562be32018-03-08 19:09:19 +0000103 /** Prevent instances of this class from being copied (As this class contains pointers) */
104 CLGEMMConvolutionLayer(const CLGEMMConvolutionLayer &) = delete;
105 /** Default move constructor */
106 CLGEMMConvolutionLayer(CLGEMMConvolutionLayer &&) = default;
107 /** Prevent instances of this class from being copied (As this class contains pointers) */
108 CLGEMMConvolutionLayer &operator=(const CLGEMMConvolutionLayer &) = delete;
109 /** Default move assignment operator */
110 CLGEMMConvolutionLayer &operator=(CLGEMMConvolutionLayer &&) = default;
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000111 /** Set the input and output tensors.
112 *
113 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
114 * while every optional dimension from 4 and above represent a batch of inputs.
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100115 * Data types supported: QASYMM8/F16/F32.
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000116 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: Same as @p input.
117 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
118 * Data type supported: Should match @p input data type, except for input of QASYMM8 type where biases should be of S32 type.
119 * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
120 * Data types supported: Same as @p input.
121 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
122 * @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
123 * tensor has also been transposed with CLGEMMTranspose1xWKernel. Data type supported: Same as @p input.
Alex Gilday7da29b62018-03-23 14:16:00 +0000124 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000125 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100126 * @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 +0000127 */
Alex Gilday7da29b62018-03-23 14:16:00 +0000128 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 +0100129 const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(), unsigned int num_groups = 1);
Georgios Pinitas78c00902018-01-09 17:33:11 +0000130 /** Static function to check if given info will lead to a valid configuration of @ref CLGEMMConvolutionLayer.
131 *
132 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
133 * while every optional dimension from 4 and above represent a batch of inputs.
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100134 * Data types supported: QASYMM8/F16/F32.
Georgios Pinitas78c00902018-01-09 17:33:11 +0000135 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: Same as @p input.
136 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
137 * Data type supported: Should match @p input data type, except for input of QASYMM8 type where biases should be of S32 type.
138 * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
139 * Data types supported: Same as @p input.
140 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
141 * @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
142 * tensor has also been transposed with CLGEMMTranspose1xWKernel. Data type supported: Same as @p input.
Alex Gilday7da29b62018-03-23 14:16:00 +0000143 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000144 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100145 * @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 +0000146 *
147 * @return a status
Georgios Pinitas78c00902018-01-09 17:33:11 +0000148 */
149 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 +0100150 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 +0000151
152 // Inherited methods overridden:
153 void run() override;
Georgios Pinitase0437672018-05-02 14:07:55 +0100154 void prepare() override;
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000155
156private:
157 /** Configures the appropriate matrix multiply routine
158 *
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100159 * @param[in] input Input tensor. Data types supported: QASYMM8/F16/F32.
160 * @param[in] weights Weights tensor. Data type supported: Same as @p input.
161 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
162 * Data type supported: Should match @p input data type, except for input of QASYMM8 type where biases should be of S32 type.
163 * @param[in, out] output Output tensor. Data types supported: Same as @p input,
164 * except for input of QASYMM8 type where output should be of S32 type.
165 * @param[in] gemmlowp_output_stage GEMMLowp output stage info
Michele Di Giorgioebc3a902018-11-16 16:04:25 +0000166 * @param[in] gemm_3d_depth Depth of GEMM 3D
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000167 */
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100168 void configure_mm(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const GEMMLowpOutputStageInfo &gemmlowp_output_stage, int gemm_3d_depth = 1);
Georgios Pinitas78c00902018-01-09 17:33:11 +0000169 /** Static function to check if given info will lead to a valid configuration of @ref CLGEMMConvolutionLayer matrix multiply routines
170 *
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100171 * @param[in] input Input tensor. Data types supported: QASYMM8/F16/F32.
172 * @param[in] weights Weights tensor. Data type supported: Same as @p input.
173 * @param[in] output Output tensor. Data types supported: Same as @p input,
174 * except for input of QASYMM8 type where output should be of S32 type.
175 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
176 * Data type supported: Should match @p input data type, except for input of QASYMM8 type where biases should be of S32 type.
177 * @param[in] gemmlowp_output_stage GEMMLowp output stage info
Michele Di Giorgioebc3a902018-11-16 16:04:25 +0000178 * @param[in] gemm_3d_depth Depth of GEMM 3D
179 * @param[in] skip_im2col Flag which specifies if im2col has to be skipped. i.e. 1x1 convolution with NHWC data layout.
180 * @param[in] run_addition Flag which specifies if @ref CLGEMMMatrixMatrixMultiplyAddition to be run.
Georgios Pinitas78c00902018-01-09 17:33:11 +0000181 *
182 * @return a status
183 */
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100184 static Status validate_mm(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const GEMMLowpOutputStageInfo &gemmlowp_output_stage,
Michele Di Giorgioebc3a902018-11-16 16:04:25 +0000185 int gemm_3d_depth, bool skip_im2col, bool run_addition);
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000186
187private:
giuros01164a2722018-11-20 18:34:46 +0000188 CLMemoryGroup _memory_group;
189 CLConvolutionLayerReshapeWeights _reshape_weights;
190 CLIm2ColKernel _im2col_kernel;
191 CLGEMM _mm_gemm;
192 CLGEMMLowpMatrixMultiplyCore _mm_gemmlowp;
193 CLCol2ImKernel _col2im_kernel;
194 CLActivationLayer _activationlayer_function;
195 CLSaturatedArithmeticOperationKernel _add_bias_kernel;
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000196
Georgios Pinitas1562be32018-03-08 19:09:19 +0000197 const ICLTensor *_original_weights;
198
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000199 CLTensor _im2col_output;
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000200 CLTensor _weights_reshaped;
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000201 CLTensor _gemm_output;
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000202
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100203 DataLayout _data_layout;
204
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100205 bool _append_bias;
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100206 bool _skip_im2col;
Georgios Pinitas932491f2018-09-21 16:33:15 +0100207 bool _skip_col2im;
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000208 bool _is_quantized;
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000209 bool _is_activationlayer_enabled;
Georgios Pinitase0437672018-05-02 14:07:55 +0100210 bool _is_prepared;
Michele Di Giorgioebc3a902018-11-16 16:04:25 +0000211 bool _run_addition;
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000212};
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100213} // namespace arm_compute
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000214#endif /* __ARM_COMPUTE_CLGEMMCONVOLUTIONLAYER_H__ */