arm_compute v18.05
[platform/upstream/armcl.git] / arm_compute / runtime / CL / functions / CLGEMMConvolutionLayer.h
1 /*
2  * Copyright (c) 2017-2018 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 #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"
30 #include "arm_compute/core/CL/kernels/CLFillBorderKernel.h"
31 #include "arm_compute/core/CL/kernels/CLGEMMInterleave4x4Kernel.h"
32 #include "arm_compute/core/CL/kernels/CLGEMMMatrixMultiplyKernel.h"
33 #include "arm_compute/core/CL/kernels/CLGEMMTranspose1xWKernel.h"
34 #include "arm_compute/core/CL/kernels/CLIm2ColKernel.h"
35 #include "arm_compute/core/CL/kernels/CLWeightsReshapeKernel.h"
36 #include "arm_compute/core/Types.h"
37 #include "arm_compute/runtime/CL/CLMemoryGroup.h"
38 #include "arm_compute/runtime/CL/CLTensor.h"
39 #include "arm_compute/runtime/CL/functions/CLActivationLayer.h"
40 #include "arm_compute/runtime/CL/functions/CLGEMM.h"
41 #include "arm_compute/runtime/CL/functions/CLGEMMLowpMatrixMultiplyCore.h"
42 #include "arm_compute/runtime/CL/functions/CLGEMMLowpOutputStage.h"
43 #include "arm_compute/runtime/IMemoryManager.h"
44
45 #include <memory>
46
47 namespace arm_compute
48 {
49 class ICLTensor;
50
51 /** Function to reshape and transpose the weights. This function calls the following kernels:
52  * -# @ref CLWeightsReshapeKernel
53  */
54 class CLConvolutionLayerReshapeWeights : public IFunction
55 {
56 public:
57     /** Constructor */
58     CLConvolutionLayerReshapeWeights();
59     /** Set the input and output tensors.
60      *
61      * @param[in]  weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
62      *                     Data type supported: QS8/QASYMM8/QS16/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      */
66     void configure(const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output);
67     /** Static function to check if given info will lead to a valid configuration of @ref CLConvolutionLayerReshapeWeights
68      *
69      * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
70      *                    Data type supported: QS8/QASYMM8/QS16/F16/F32.
71      * @param[in] biases  Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p weights.
72      * @param[in] output  Destination tensor. Data types supported: Same as @p weights.
73      *
74      * @return a status
75      */
76     static Status validate(const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output);
77     // Inherited methods overridden:
78     void run() override;
79
80 private:
81     CLWeightsReshapeKernel _weights_reshape_kernel;
82 };
83
84 /** Basic function to compute the convolution layer. This function calls the following OpenCL kernels/functions:
85  *
86  * Note: weights already reshaped for quantized asymmetric is not supported
87  *
88  * -# @ref CLIm2ColKernel
89  * -# @ref CLGEMMLowpMatrixMultiplyCore (if quantized asymmetric)
90  * -# @ref CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint (if quantized asymmetric)
91  * -# @ref CLCol2ImKernel
92  *
93  * if the weights are already reshaped:
94  * -# @ref CLGEMMInterleave4x4Kernel
95  * -# @ref CLGEMMMatrixMultiplyKernel
96  * else
97  * -# @ref CLGEMM
98  */
99 class CLGEMMConvolutionLayer : public IFunction
100 {
101 public:
102     /** Default constructor
103      *
104      * @param[in] memory_manager (Optional) Memory manager.
105      */
106     CLGEMMConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
107     /** Prevent instances of this class from being copied (As this class contains pointers) */
108     CLGEMMConvolutionLayer(const CLGEMMConvolutionLayer &) = delete;
109     /** Default move constructor */
110     CLGEMMConvolutionLayer(CLGEMMConvolutionLayer &&) = default;
111     /** Prevent instances of this class from being copied (As this class contains pointers) */
112     CLGEMMConvolutionLayer &operator=(const CLGEMMConvolutionLayer &) = delete;
113     /** Default move assignment operator */
114     CLGEMMConvolutionLayer &operator=(CLGEMMConvolutionLayer &&) = default;
115     /** Set the input and output tensors.
116      *
117      * @param[in]  input        Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
118      *                          while every optional dimension from 4 and above represent a batch of inputs.
119      *                          Data types supported: QS8/QASYMM8/QS16/F16/F32.
120      * @param[in]  weights      Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: Same as @p input.
121      * @param[in]  biases       Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
122      *                          Data type supported: Should match @p input data type, except for input of QASYMM8 type where biases should be of S32 type.
123      * @param[out] output       Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
124      *                          Data types supported: Same as @p input.
125      * @param[in]  conv_info    Contains padding and stride information described in @ref PadStrideInfo.
126      * @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
127      *                          tensor has also been transposed with CLGEMMTranspose1xWKernel. Data type supported: Same as @p input.
128      * @param[in]  dilation     (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
129      * @param[in]  act_info     (Optional) Activation layer information in case of a fused activation.
130      */
131     void configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info = WeightsInfo(),
132                    const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo());
133     /** Static function to check if given info will lead to a valid configuration of @ref CLGEMMConvolutionLayer.
134      *
135      * @param[in]  input        Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
136      *                          while every optional dimension from 4 and above represent a batch of inputs.
137      *                          Data types supported: QS8/QASYMM8/QS16/F16/F32.
138      * @param[in]  weights      Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: Same as @p input.
139      * @param[in]  biases       Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
140      *                          Data type supported: Should match @p input data type, except for input of QASYMM8 type where biases should be of S32 type.
141      * @param[out] output       Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
142      *                          Data types supported: Same as @p input.
143      * @param[in]  conv_info    Contains padding and stride information described in @ref PadStrideInfo.
144      * @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
145      *                          tensor has also been transposed with CLGEMMTranspose1xWKernel. Data type supported: Same as @p input.
146      * @param[in]  dilation     (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
147      * @param[in]  act_info     (Optional) Activation layer information in case of a fused activation.
148      *
149      * @return a status
150      */
151     static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
152                            const WeightsInfo &weights_info = WeightsInfo(), const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo());
153
154     // Inherited methods overridden:
155     void run() override;
156     void prepare() override;
157
158 private:
159     /** Configures the appropriate matrix multiply routine
160      *
161      * @param input                     Input tensor. Data types supported: QS8/QASYMM8/QS16/F16/F32.
162      * @param weights                   Weights tensor. Data type supported: Same as @p input.
163      * @param 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      */
166     void configure_mm(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output);
167     /** Static function to check if given info will lead to a valid configuration of @ref CLGEMMConvolutionLayer matrix multiply routines
168      *
169      * @param[in] input   Input tensor. Data types supported: QS8/QASYMM8/QS16/F16/F32.
170      * @param[in] weights Weights tensor. Data type supported: Same as @p input.
171      * @param[in] output  Output tensor. Data types supported: Same as @p input,
172      *                                      except for input of QASYMM8 type where output should be of S32 type.
173      *
174      * @return a status
175      */
176     static Status validate_mm(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output);
177
178 private:
179     CLMemoryGroup                                       _memory_group;
180     CLConvolutionLayerReshapeWeights                    _reshape_weights;
181     CLIm2ColKernel                                      _im2col_kernel;
182     CLGEMM                                              _mm_gemm;
183     CLGEMMLowpMatrixMultiplyCore                        _mm_gemmlowp;
184     CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint _gemmlowp_output_stage;
185     CLCol2ImKernel                                      _col2im_kernel;
186     CLActivationLayer                                   _activationlayer_function;
187
188     const ICLTensor *_original_weights;
189
190     CLTensor _im2col_output;
191     CLTensor _weights_reshaped;
192     CLTensor _gemm_output;
193     CLTensor _tmp_output;
194
195     bool _is_quantized;
196     bool _is_activationlayer_enabled;
197     bool _is_prepared;
198 };
199 }
200 #endif /* __ARM_COMPUTE_CLGEMMCONVOLUTIONLAYER_H__ */