blob: 4ae8ee1fb3adf5aac88bd2b43eb5d20473ae2337 [file] [log] [blame]
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +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_NEGEMMCONVOLUTIONLAYER_H__
25#define __ARM_COMPUTE_NEGEMMCONVOLUTIONLAYER_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"
31#include "arm_compute/core/NEON/kernels/NEGEMMAssemblyBaseKernel.h"
32#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"
38#include "arm_compute/runtime/MemoryGroup.h"
Pablo Telloeb82fd22018-02-23 13:43:50 +000039#include "arm_compute/runtime/NEON/AssemblyHelper.h"
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000040#include "arm_compute/runtime/NEON/functions/NEGEMMLowpMatrixMultiplyCore.h"
41#include "arm_compute/runtime/NEON/functions/NEGEMMLowpOutputStage.h"
42#include "arm_compute/runtime/Tensor.h"
43
44#include <memory>
45
46namespace arm_compute
47{
48class ITensor;
49
50/** Function to reshape and perform 1xW transposition on the weights. This function calls the following kernels:
51 * -# @ref NEWeightsReshapeKernel
52 * -# @ref NEGEMMTranspose1xWKernel (executed in case GEMM is required for the operation)
53 */
54class NEConvolutionLayerReshapeWeights : public IFunction
55{
56public:
57 /** Constructor */
58 NEConvolutionLayerReshapeWeights(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
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]. Data type supported: QS8/QASYMM8/QS16/F32.
62 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p weights.
63 * @param[out] output Destination tensor. Data types supported: Same as @p weights.
64 * @param[in] transpose1xW True if the weights are to undergo a 1xW transposition after reshaping (in case of GEMM operation), false otherwise.
65 * Data types supported: Same as @p weights.
66 */
67 void configure(const ITensor *weights, const ITensor *biases, ITensor *output, bool transpose1xW);
68 /** Static function to check if given info will lead to a valid configuration of @ref NEConvolutionLayerReshapeWeights
69 *
70 * @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.
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 * @param[in] transpose1xW True if the weights are to undergo a 1xW transposition after reshaping (in case of GEMM operation), false otherwise.
74 * Data types supported: Same as @p weights.
75 *
76 * @return an error status
77 */
78 static Status validate(const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, bool transpose1xW);
79
80 // Inherited methods overridden:
81 void run() override;
82
83private:
84 MemoryGroup _memory_group;
85 NEWeightsReshapeKernel _weights_reshape_kernel;
86 NEGEMMTranspose1xWKernel _weights_transposed_kernel;
87 Tensor _weights_reshaped;
88 bool _transpose1xW;
89};
90
91/** Basic function to simulate a convolution layer. This function calls the following NEON kernels:
92 * -# @ref NEWeightsReshapeKernel (executed only once for each configuration)
93 * -# @ref NEIm2ColKernel
94 * -# @ref NEGEMMInterleave4x4Kernel (executed only in case GEMM is required for the operation)
95 * -# @ref NEGEMMMatrixMultiplyKernel or @ref NEGEMMLowpMatrixMultiplyCore (if quantized asymmetric)
96 * -# @ref NEGEMMLowpQuantizeDownInt32ToUint8Scale (if quantized asymmetric)
97 * -# @ref NECol2ImKernel
98 */
99class NEGEMMConvolutionLayer : public IFunction
100{
101public:
102 /** Constructor */
103 NEGEMMConvolutionLayer(const std::shared_ptr<IMemoryManager> &memory_manager = nullptr);
104
105 /** Set the input and output tensors.
106 *
107 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
108 * while every optional dimension from 4 and above represent a batch of inputs.
109 * Data types supported: QS8/QASYMM8/QS16/F32.
110 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: Same as @p input.
111 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
112 * Data type supported: Should match @p input data type, except for input of QASYMM8 type where biases should be of S32 type.
113 * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
114 * Data types supported: Same as @p input.
115 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
116 * @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
117 * tensor has also been transposed with NEGEMMTranspose1xWKernel. Data type supported: Same as @p input.
118 */
119 void configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info = WeightsInfo());
120 /** Static function to check if given info will lead to a valid configuration of @ref NEGEMMConvolutionLayer
121 *
122 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
123 * while every optional dimension from 4 and above represent a batch of inputs.
124 * Data types supported: QS8/QASYMM8/QS16/F16/F32.
125 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported:Same as @p input.
126 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
127 * Data type supported: Should match @p input data type, except for input of QASYMM8 type where biases should be of S32 type.
128 * @param[in] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
129 * Data types supported: Same as @p input.
130 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
131 * @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
132 * tensor has also been transposed with NEGEMMTranspose1xWKernel. Data type supported: Same as @p input.
133 *
134 * @return a status
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());
138
139 // Inherited methods overridden:
140 void run() override;
141
142private:
143 /** Configures the appropriate matrix multiply routine
144 *
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000145 * @param[in] input Input tensor. Data types supported: QS8/QASYMM8/QS16/F16/F32.
146 * @param[in] weights Weights tensor. Data type supported: Same as @p input.
147 * @param[out] output Output tensor. Data types supported: Same as @p input,
148 * except for input of QASYMM8 type where output should be of S32 type.
149 * @param[in] is_interleaved (Optional) True if input0 and input1 have been reshaped respectively using @ref CLGEMMInterleave4x4Kernel and @ref CLGEMMTranspose1xWKernel
150 * @param[in] reshape_info (Optional) GEMM reshape info. If is_interleaved_transposed = true, this object must contain the information to understand how the matrix A and matrix B have been reshaped
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000151 */
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000152 void configure_mm(const ITensor *input, const ITensor *weights, ITensor *output, bool is_interleaved, const GEMMReshapeInfo &reshape_info = GEMMReshapeInfo());
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000153
154private:
Pablo Telloeb82fd22018-02-23 13:43:50 +0000155 AssemblyKernelGlueF32 _asm_glue;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000156 MemoryGroup _memory_group;
157 NEIm2ColKernel _input_im2col_kernel;
158 NEGEMMInterleave4x4Kernel _input_interleave_kernel;
159 NEConvolutionLayerReshapeWeights _reshape_weights;
160 NEGEMMMatrixMultiplyKernel _mm_kernel;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000161 NEGEMMLowpMatrixMultiplyCore _mm_gemmlowp;
162 NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint _gemmlowp_output_stage;
163 NECol2ImKernel _output_col2im_kernel;
164
165 Tensor _input_im2col_reshaped;
166 Tensor _input_interleaved_reshaped;
167 Tensor _weights_reshaped;
168 Tensor _gemm_output;
169 Tensor _tmp_output;
170 Tensor _workspace;
171
172 bool _append_bias;
173 bool _is_fully_connected_convolution;
174 bool _are_weights_reshaped;
175 bool _is_quantized;
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000176 bool _is_interleaved;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000177};
178}
179#endif /* __ARM_COMPUTE_NECONVOLUTIONGEMMLAYER_H__ */