blob: f80f67d944819d0ad41bf7c7515040ad544a216e [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Isabella Gottardie6630e42018-01-18 15:50:39 +00002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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_NECONVOLUTIONLAYER_H__
25#define __ARM_COMPUTE_NECONVOLUTIONLAYER_H__
26
27#include "arm_compute/runtime/IFunction.h"
28
29#include "arm_compute/core/NEON/kernels/NECol2ImKernel.h"
30#include "arm_compute/core/NEON/kernels/NEFillBorderKernel.h"
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010031#include "arm_compute/core/NEON/kernels/NEGEMMAssemblyBaseKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032#include "arm_compute/core/NEON/kernels/NEGEMMInterleave4x4Kernel.h"
33#include "arm_compute/core/NEON/kernels/NEGEMMMatrixMultiplyKernel.h"
34#include "arm_compute/core/NEON/kernels/NEGEMMTranspose1xWKernel.h"
35#include "arm_compute/core/NEON/kernels/NEIm2ColKernel.h"
36#include "arm_compute/core/NEON/kernels/NEWeightsReshapeKernel.h"
37#include "arm_compute/core/Types.h"
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010038#include "arm_compute/runtime/MemoryGroup.h"
Isabella Gottardie6630e42018-01-18 15:50:39 +000039#include "arm_compute/runtime/NEON/functions/NEGEMMLowpMatrixMultiplyCore.h"
40#include "arm_compute/runtime/NEON/functions/NEGEMMLowpOutputStage.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041#include "arm_compute/runtime/Tensor.h"
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010042#include <memory>
43
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044namespace arm_compute
45{
46class ITensor;
47
48/** Function to reshape and perform 1xW transposition on the weights. This function calls the following kernels:
49 * -# @ref NEWeightsReshapeKernel
50 * -# @ref NEGEMMTranspose1xWKernel (executed in case GEMM is required for the operation)
51 */
52class NEConvolutionLayerReshapeWeights : public IFunction
53{
54public:
55 /** Constructor */
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010056 NEConvolutionLayerReshapeWeights(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010057 /** Set the input and output tensors.
58 *
Isabella Gottardie6630e42018-01-18 15:50:39 +000059 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: QS8/QASYMM8/QS16/F32.
Anthony Barbier6ff3b192017-09-04 18:44:23 +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] transpose1xW True if the weights are to undergo a 1xW transposition after reshaping (in case of GEMM operation), false otherwise.
63 * Data types supported: Same as @p weights.
64 */
65 void configure(const ITensor *weights, const ITensor *biases, ITensor *output, bool transpose1xW);
Giorgio Arena7c23ad02017-11-30 15:08:38 +000066 /** Static function to check if given info will lead to a valid configuration of @ref NEConvolutionLayerReshapeWeights
67 *
Isabella Gottardie6630e42018-01-18 15:50:39 +000068 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: QS8/QASYMM8/QS16/F16/F32.
Giorgio Arena7c23ad02017-11-30 15:08:38 +000069 * @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] transpose1xW True if the weights are to undergo a 1xW transposition after reshaping (in case of GEMM operation), false otherwise.
72 * Data types supported: Same as @p weights.
73 *
74 * @return an error status
75 */
76 static Status validate(const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, bool transpose1xW);
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010077
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078 // Inherited methods overridden:
79 void run() override;
80
81private:
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010082 MemoryGroup _memory_group;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010083 NEWeightsReshapeKernel _weights_reshape_kernel;
84 NEGEMMTranspose1xWKernel _weights_transposed_kernel;
85 Tensor _weights_reshaped;
86 bool _transpose1xW;
87};
88
89/** Basic function to simulate a convolution layer. This function calls the following NEON kernels:
90 * -# @ref NEWeightsReshapeKernel (executed only once for each configuration)
91 * -# @ref NEIm2ColKernel
92 * -# @ref NEGEMMInterleave4x4Kernel (executed only in case GEMM is required for the operation)
Isabella Gottardie6630e42018-01-18 15:50:39 +000093 * -# @ref NEGEMMMatrixMultiplyKernel or @ref NEGEMMLowpMatrixMultiplyCore (if quantized asymmetric)
94 * -# @ref NEGEMMLowpQuantizeDownInt32ToUint8Scale (if quantized asymmetric)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010095 * -# @ref NECol2ImKernel
96 */
97class NEConvolutionLayer : public IFunction
98{
99public:
100 /** Constructor */
Pablo Tellof6169d82018-02-02 10:05:50 +0000101 NEConvolutionLayer(const std::shared_ptr<IMemoryManager> &memory_manager = nullptr);
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100102
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103 /** Set the input and output tensors.
104 *
105 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
106 * while every optional dimension from 4 and above represent a batch of inputs.
Isabella Gottardie6630e42018-01-18 15:50:39 +0000107 * Data types supported: QS8/QASYMM8/QS16/F32.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: Same as @p input.
Isabella Gottardie6630e42018-01-18 15:50:39 +0000109 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
110 * Data type supported: Should match @p input data type, except for input of QASYMM8 type where biases should be of S32 type.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100111 * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
112 * Data types supported: Same as @p input.
113 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
114 * @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
115 * tensor has also been transposed with NEGEMMTranspose1xWKernel. Data type supported: Same as @p input.
116 */
117 void configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info = WeightsInfo());
Giorgio Arena7c23ad02017-11-30 15:08:38 +0000118 /** Static function to check if given info will lead to a valid configuration of @ref NEConvolutionLayer
119 *
120 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
121 * while every optional dimension from 4 and above represent a batch of inputs.
Isabella Gottardie6630e42018-01-18 15:50:39 +0000122 * Data types supported: QS8/QASYMM8/QS16/F16/F32.
Giorgio Arena7c23ad02017-11-30 15:08:38 +0000123 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported:Same as @p input.
Isabella Gottardie6630e42018-01-18 15:50:39 +0000124 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
125 * Data type supported: Should match @p input data type, except for input of QASYMM8 type where biases should be of S32 type.
Giorgio Arena7c23ad02017-11-30 15:08:38 +0000126 * @param[in] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
127 * Data types supported: Same as @p input.
128 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
129 * @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
130 * tensor has also been transposed with NEGEMMTranspose1xWKernel. Data type supported: Same as @p input.
131 *
132 * @return a status
133 */
134 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
135 const WeightsInfo &weights_info = WeightsInfo());
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100136
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137 // Inherited methods overridden:
138 void run() override;
139
140private:
Isabella Gottardie6630e42018-01-18 15:50:39 +0000141 /** Configures the appropriate matrix multiply routine
142 *
143 * @param[in] input Input tensor. Data types supported: QS8/QASYMM8/QS16/F16/F32.
144 * @param[in] weights Weights tensor. Data type supported: Same as @p input.
145 * @param[out] output Output tensor. Data types supported: Same as @p input,
146 * except for input of QASYMM8 type where output should be of S32 type.
147 */
148 void configure_mm(const ITensor *input, const ITensor *weights, ITensor *output);
Georgios Pinitas284cfe22018-02-13 12:15:13 +0000149 /** Prepare the appropriate assembly optimized kernel
150 *
151 * @param[in] ci CPU information
152 * @param[in] M M parameter of matrix multiplication
153 * @param[in] N N parameter of matrix multiplication
154 * @param[in] K K parameter of matrix multiplication
155 */
156 void configure_asm_mm(const struct CPUInfo &ci, int M, int N, int K);
Isabella Gottardie6630e42018-01-18 15:50:39 +0000157
158private:
159 MemoryGroup _memory_group;
160 NEIm2ColKernel _input_im2col_kernel;
161 NEGEMMInterleave4x4Kernel _input_interleave_kernel;
162 NEConvolutionLayerReshapeWeights _reshape_weights;
163 NEGEMMMatrixMultiplyKernel _mm_kernel;
164 std::unique_ptr<NEGEMMAssemblyBaseKernel> _mm_optimised_kernel;
165 NEGEMMLowpMatrixMultiplyCore _mm_gemmlowp;
166 NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint _gemmlowp_output_stage;
167 NECol2ImKernel _output_col2im_kernel;
168
169 Tensor _input_im2col_reshaped;
170 Tensor _input_interleaved_reshaped;
171 Tensor _weights_reshaped;
172 Tensor _gemm_output;
173 Tensor _tmp_output;
174 Tensor _workspace;
175
176 bool _append_bias;
177 bool _is_fully_connected_convolution;
178 bool _are_weights_reshaped;
179 bool _is_quantized;
180 bool _is_interleaved_transposed;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100181};
182}
183#endif /* __ARM_COMPUTE_NECONVOLUTIONLAYER_H__ */