blob: 5f8830a361f92ed1a21eb43659c5d4e5dd3aad58 [file] [log] [blame]
Isabella Gottardif07d28d2018-02-06 14:52:43 +00001/*
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/CLGEMM.h"
40#include "arm_compute/runtime/CL/functions/CLGEMMLowpMatrixMultiplyCore.h"
41#include "arm_compute/runtime/CL/functions/CLGEMMLowpOutputStage.h"
42#include "arm_compute/runtime/IMemoryManager.h"
43
44#include <memory>
45
46namespace arm_compute
47{
48class ICLTensor;
49
50/** Function to reshape and transpose the weights. This function calls the following kernels:
51 * -# @ref CLWeightsReshapeKernel
52 * -# @ref CLGEMMTranspose1xWKernel
53 */
54class CLConvolutionLayerReshapeWeights : public IFunction
55{
56public:
57 /** Constructor */
58 CLConvolutionLayerReshapeWeights(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
59 /** Set the input and output tensors.
60 *
Georgios Pinitas78c00902018-01-09 17:33:11 +000061 * @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.
Isabella Gottardif07d28d2018-02-06 14:52:43 +000065 */
Georgios Pinitas78c00902018-01-09 17:33:11 +000066 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);
Isabella Gottardif07d28d2018-02-06 14:52:43 +000077 // Inherited methods overridden:
78 void run() override;
79
80private:
81 CLMemoryGroup _memory_group;
82 CLWeightsReshapeKernel _weights_reshape_kernel;
83 CLGEMMTranspose1xWKernel _weights_transposed_kernel;
84 CLTensor _weights_reshaped;
Isabella Gottardif07d28d2018-02-06 14:52:43 +000085};
86
87/** Basic function to compute the convolution layer. This function calls the following OpenCL kernels/functions:
88 *
89 * Note: weights already reshaped for quantized asymmetric is not supported
90 *
91 * -# @ref CLIm2ColKernel
92 * -# @ref CLGEMMLowpMatrixMultiplyCore (if quantized asymmetric)
93 * -# @ref CLGEMMLowpQuantizeDownInt32ToUint8Scale (if quantized asymmetric)
94 * -# @ref CLCol2ImKernel
95 *
96 * if the weights are already reshaped:
97 * -# @ref CLGEMMInterleave4x4Kernel
98 * -# @ref CLGEMMMatrixMultiplyKernel
99 * else
100 * -# @ref CLGEMM
101 */
102class CLGEMMConvolutionLayer : public IFunction
103{
104public:
105 /** Default constructor */
106 CLGEMMConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
107 /** Set the input and output tensors.
108 *
109 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
110 * while every optional dimension from 4 and above represent a batch of inputs.
111 * Data types supported: QS8/QASYMM8/QS16/F16/F32.
112 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: Same as @p input.
113 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
114 * Data type supported: Should match @p input data type, except for input of QASYMM8 type where biases should be of S32 type.
115 * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
116 * Data types supported: Same as @p input.
117 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
118 * @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
119 * tensor has also been transposed with CLGEMMTranspose1xWKernel. Data type supported: Same as @p input.
120 */
121 void configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info = WeightsInfo());
Georgios Pinitas78c00902018-01-09 17:33:11 +0000122 /** Static function to check if given info will lead to a valid configuration of @ref CLGEMMConvolutionLayer.
123 *
124 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
125 * while every optional dimension from 4 and above represent a batch of inputs.
126 * Data types supported: QS8/QASYMM8/QS16/F16/F32.
127 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: Same as @p input.
128 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
129 * Data type supported: Should match @p input data type, except for input of QASYMM8 type where biases should be of S32 type.
130 * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
131 * Data types supported: Same as @p input.
132 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
133 * @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
134 * tensor has also been transposed with CLGEMMTranspose1xWKernel. Data type supported: Same as @p input.
135 */
136 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
137 const WeightsInfo &weights_info = WeightsInfo());
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000138
139 // Inherited methods overridden:
140 void run() override;
141
142private:
143 /** Configures the appropriate matrix multiply routine
144 *
145 * @param input Input tensor. Data types supported: QS8/QASYMM8/QS16/F16/F32.
146 * @param weights Weights tensor. Data type supported: Same as @p input.
147 * @param output Output tensor. Data types supported: Same as @p input,
148 * except for input of QASYMM8 type where output should be of S32 type.
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000149 */
Georgios Pinitas78c00902018-01-09 17:33:11 +0000150 void configure_mm(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output);
151 /** Static function to check if given info will lead to a valid configuration of @ref CLGEMMConvolutionLayer matrix multiply routines
152 *
153 * @param[in] input Input tensor. Data types supported: QS8/QASYMM8/QS16/F16/F32.
154 * @param[in] weights Weights tensor. Data type supported: Same as @p input.
155 * @param[in] output Output tensor. Data types supported: Same as @p input,
156 * except for input of QASYMM8 type where output should be of S32 type.
157 *
158 * @return a status
159 */
160 static Status validate_mm(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output);
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000161
162private:
163 CLMemoryGroup _memory_group;
164 CLConvolutionLayerReshapeWeights _reshape_weights;
165 CLIm2ColKernel _im2col_kernel;
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000166 CLGEMM _mm_gemm;
167 CLGEMMLowpMatrixMultiplyCore _mm_gemmlowp;
168 CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint _gemmlowp_output_stage;
169 CLCol2ImKernel _col2im_kernel;
170
171 CLTensor _im2col_output;
172 CLTensor _interleave_output;
173 CLTensor _weights_reshaped;
174 CLTensor _weights_transposed;
175 CLTensor _gemm_output;
176 CLTensor _tmp_output;
177
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000178 bool _is_quantized;
Gian Marco35026132018-02-22 15:28:44 +0000179 bool _is_first_run;
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000180};
181}
182#endif /* __ARM_COMPUTE_CLGEMMCONVOLUTIONLAYER_H__ */