blob: 7075becf754646de4a63b3fc0586a320c7128908 [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
Michalis Spyroue2503892018-04-23 15:17:31 +010029#include "arm_compute/core/NEON/kernels/NEArithmeticAdditionKernel.h"
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000030#include "arm_compute/core/NEON/kernels/NECol2ImKernel.h"
31#include "arm_compute/core/NEON/kernels/NEFillBorderKernel.h"
32#include "arm_compute/core/NEON/kernels/NEGEMMAssemblyBaseKernel.h"
33#include "arm_compute/core/NEON/kernels/NEGEMMInterleave4x4Kernel.h"
34#include "arm_compute/core/NEON/kernels/NEGEMMMatrixMultiplyKernel.h"
35#include "arm_compute/core/NEON/kernels/NEGEMMTranspose1xWKernel.h"
36#include "arm_compute/core/NEON/kernels/NEIm2ColKernel.h"
37#include "arm_compute/core/NEON/kernels/NEWeightsReshapeKernel.h"
38#include "arm_compute/core/Types.h"
39#include "arm_compute/runtime/MemoryGroup.h"
Pablo Telloeb82fd22018-02-23 13:43:50 +000040#include "arm_compute/runtime/NEON/AssemblyHelper.h"
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000041#include "arm_compute/runtime/NEON/functions/NEActivationLayer.h"
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000042#include "arm_compute/runtime/NEON/functions/NEGEMMLowpMatrixMultiplyCore.h"
43#include "arm_compute/runtime/NEON/functions/NEGEMMLowpOutputStage.h"
44#include "arm_compute/runtime/Tensor.h"
45
46#include <memory>
47
48namespace arm_compute
49{
50class ITensor;
51
52/** Function to reshape and perform 1xW transposition on the weights. This function calls the following kernels:
53 * -# @ref NEWeightsReshapeKernel
54 * -# @ref NEGEMMTranspose1xWKernel (executed in case GEMM is required for the operation)
55 */
56class NEConvolutionLayerReshapeWeights : public IFunction
57{
58public:
59 /** Constructor */
60 NEConvolutionLayerReshapeWeights(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
61 /** Set the input and output tensors.
62 *
63 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: QS8/QASYMM8/QS16/F32.
64 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p weights.
65 * @param[out] output Destination tensor. Data types supported: Same as @p weights.
66 * @param[in] transpose1xW True if the weights are to undergo a 1xW transposition after reshaping (in case of GEMM operation), false otherwise.
67 * Data types supported: Same as @p weights.
68 */
69 void configure(const ITensor *weights, const ITensor *biases, ITensor *output, bool transpose1xW);
70 /** Static function to check if given info will lead to a valid configuration of @ref NEConvolutionLayerReshapeWeights
71 *
72 * @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.
73 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p weights.
74 * @param[in] output Destination tensor. Data types supported: Same as @p weights.
75 * @param[in] transpose1xW True if the weights are to undergo a 1xW transposition after reshaping (in case of GEMM operation), false otherwise.
76 * Data types supported: Same as @p weights.
77 *
78 * @return an error status
79 */
80 static Status validate(const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, bool transpose1xW);
81
82 // Inherited methods overridden:
83 void run() override;
84
85private:
86 MemoryGroup _memory_group;
87 NEWeightsReshapeKernel _weights_reshape_kernel;
88 NEGEMMTranspose1xWKernel _weights_transposed_kernel;
89 Tensor _weights_reshaped;
90 bool _transpose1xW;
91};
92
93/** Basic function to simulate a convolution layer. This function calls the following NEON kernels:
94 * -# @ref NEWeightsReshapeKernel (executed only once for each configuration)
95 * -# @ref NEIm2ColKernel
96 * -# @ref NEGEMMInterleave4x4Kernel (executed only in case GEMM is required for the operation)
97 * -# @ref NEGEMMMatrixMultiplyKernel or @ref NEGEMMLowpMatrixMultiplyCore (if quantized asymmetric)
98 * -# @ref NEGEMMLowpQuantizeDownInt32ToUint8Scale (if quantized asymmetric)
99 * -# @ref NECol2ImKernel
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000100 * -# @ref NEActivationLayer (executed only if the activation layer is enabled)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000101 */
102class NEGEMMConvolutionLayer : public IFunction
103{
104public:
105 /** Constructor */
106 NEGEMMConvolutionLayer(const std::shared_ptr<IMemoryManager> &memory_manager = nullptr);
Georgios Pinitas1562be32018-03-08 19:09:19 +0000107 /** Prevent instances of this class from being copied (As this class contains pointers) */
108 NEGEMMConvolutionLayer(const NEGEMMConvolutionLayer &) = delete;
109 /** Default move constructor */
110 NEGEMMConvolutionLayer(NEGEMMConvolutionLayer &&) = default;
111 /** Prevent instances of this class from being copied (As this class contains pointers) */
112 NEGEMMConvolutionLayer &operator=(const NEGEMMConvolutionLayer &) = delete;
113 /** Default move assignment operator */
114 NEGEMMConvolutionLayer &operator=(NEGEMMConvolutionLayer &&) = default;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000115 /** 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/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 NEWeightsReshapeKernel. If this is not part of the fully connected layer the weights
127 * tensor has also been transposed with NEGEMMTranspose1xWKernel. Data type supported: Same as @p input.
Alex Gilday7da29b62018-03-23 14:16:00 +0000128 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000129 * @param[in] act_info (Optional) Activation layer information in case of a fused activation. Only RELU, BOUNDED_RELU and LU_BOUNDED_RELU supported.
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000130 */
Alex Gilday7da29b62018-03-23 14:16:00 +0000131 void configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info = WeightsInfo(),
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000132 const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo());
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000133 /** Static function to check if given info will lead to a valid configuration of @ref NEGEMMConvolutionLayer
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[in] 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 NEWeightsReshapeKernel. If this is not part of the fully connected layer the weights
145 * tensor has also been transposed with NEGEMMTranspose1xWKernel. Data type supported: Same as @p input.
Alex Gilday7da29b62018-03-23 14:16:00 +0000146 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000147 * @param[in] act_info (Optional) Activation layer information in case of a fused activation. Only RELU, BOUNDED_RELU and LU_BOUNDED_RELU supported.
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000148 *
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,
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000152 const WeightsInfo &weights_info = WeightsInfo(), const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo());
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000153
154 // Inherited methods overridden:
155 void run() override;
Georgios Pinitas72219332018-06-05 14:56:06 +0100156 void prepare() override;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000157
158private:
159 /** Configures the appropriate matrix multiply routine
160 *
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000161 * @param[in] input Input tensor. Data types supported: QS8/QASYMM8/QS16/F16/F32.
162 * @param[in] weights Weights tensor. Data type supported: Same as @p input.
163 * @param[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] is_interleaved (Optional) True if input0 and input1 have been reshaped respectively using @ref CLGEMMInterleave4x4Kernel and @ref CLGEMMTranspose1xWKernel
166 * @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 +0000167 */
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000168 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 +0000169
170private:
Pablo Telloeb82fd22018-02-23 13:43:50 +0000171 AssemblyKernelGlueF32 _asm_glue;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000172 MemoryGroup _memory_group;
173 NEIm2ColKernel _input_im2col_kernel;
174 NEGEMMInterleave4x4Kernel _input_interleave_kernel;
175 NEConvolutionLayerReshapeWeights _reshape_weights;
176 NEGEMMMatrixMultiplyKernel _mm_kernel;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000177 NEGEMMLowpMatrixMultiplyCore _mm_gemmlowp;
178 NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint _gemmlowp_output_stage;
179 NECol2ImKernel _output_col2im_kernel;
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000180 NEActivationLayer _activationlayer_function;
Michalis Spyroue2503892018-04-23 15:17:31 +0100181 NEArithmeticAdditionKernel _add_bias_kernel;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000182
Georgios Pinitas1562be32018-03-08 19:09:19 +0000183 const ITensor *_original_weights;
184
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000185 Tensor _input_im2col_reshaped;
186 Tensor _input_interleaved_reshaped;
187 Tensor _weights_reshaped;
188 Tensor _gemm_output;
189 Tensor _tmp_output;
190 Tensor _workspace;
Georgios Pinitas932b5612018-05-03 13:44:35 +0100191 Tensor _B_pretransposed;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000192
Michalis Spyroue2503892018-04-23 15:17:31 +0100193 DataLayout _data_layout;
194 bool _append_bias;
195 bool _is_fully_connected_convolution;
196 bool _are_weights_reshaped;
197 bool _is_quantized;
198 bool _is_interleaved;
199 bool _is_activationlayer_enabled;
200 bool _skip_im2col;
Georgios Pinitas72219332018-06-05 14:56:06 +0100201 bool _is_prepared;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000202};
203}
204#endif /* __ARM_COMPUTE_NECONVOLUTIONGEMMLAYER_H__ */