Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 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_NEGEMMCONVOLUTIONLAYER_H__ |
| 25 | #define __ARM_COMPUTE_NEGEMMCONVOLUTIONLAYER_H__ |
| 26 | |
| 27 | #include "arm_compute/runtime/IFunction.h" |
| 28 | |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/NEON/kernels/NEArithmeticAdditionKernel.h" |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 30 | #include "arm_compute/core/NEON/kernels/NECol2ImKernel.h" |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 31 | #include "arm_compute/core/NEON/kernels/NEGEMMTranspose1xWKernel.h" |
| 32 | #include "arm_compute/core/NEON/kernels/NEIm2ColKernel.h" |
| 33 | #include "arm_compute/core/NEON/kernels/NEWeightsReshapeKernel.h" |
| 34 | #include "arm_compute/core/Types.h" |
| 35 | #include "arm_compute/runtime/MemoryGroup.h" |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 36 | #include "arm_compute/runtime/NEON/functions/NEActivationLayer.h" |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 37 | #include "arm_compute/runtime/NEON/functions/NEGEMM.h" |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 38 | #include "arm_compute/runtime/NEON/functions/NEGEMMLowpMatrixMultiplyCore.h" |
| 39 | #include "arm_compute/runtime/NEON/functions/NEGEMMLowpOutputStage.h" |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 40 | #include "arm_compute/runtime/NEON/functions/NEReshapeLayer.h" |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 41 | #include "arm_compute/runtime/Tensor.h" |
| 42 | |
| 43 | #include <memory> |
| 44 | |
| 45 | namespace arm_compute |
| 46 | { |
| 47 | class ITensor; |
| 48 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 49 | /** Function to reshape the weights. This function calls the following kernel: |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 50 | * -# @ref NEWeightsReshapeKernel |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 51 | */ |
| 52 | class NEConvolutionLayerReshapeWeights : public IFunction |
| 53 | { |
| 54 | public: |
| 55 | /** Constructor */ |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 56 | NEConvolutionLayerReshapeWeights(); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 57 | /** Set the input and output tensors. |
| 58 | * |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 59 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: QASYMM8/F16/F32. |
| 60 | * @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. |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 62 | */ |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 63 | void configure(const ITensor *weights, const ITensor *biases, ITensor *output); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 64 | /** Static function to check if given info will lead to a valid configuration of @ref NEConvolutionLayerReshapeWeights |
| 65 | * |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 66 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: QASYMM8/F16/F32. |
| 67 | * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p weights. |
| 68 | * @param[in] output Destination tensor. Data types supported: Same as @p weights. |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 69 | * |
| 70 | * @return an error status |
| 71 | */ |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 72 | static Status validate(const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 73 | |
| 74 | // Inherited methods overridden: |
| 75 | void run() override; |
| 76 | |
| 77 | private: |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 78 | NEWeightsReshapeKernel _weights_reshape_kernel; |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 81 | /** Basic function to compute the convolution layer. This function calls the following NEON kernels/functions: |
| 82 | * |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 83 | * -# @ref NEIm2ColKernel |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 84 | * -# @ref NEGEMM (if the data type is FP32 or FP16) |
| 85 | * -# @ref NEGEMMLowpMatrixMultiplyCore (if the data type is QASYMM8) |
| 86 | * -# @ref NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint (if the data type is QASYMM8) |
| 87 | * -# @ref NEArithmeticAdditionKernel (if biases != nullptr and we have a 1x1 convolution with the NHWC data layout) |
Georgios Pinitas | 932491f | 2018-09-21 16:33:15 +0100 | [diff] [blame] | 88 | * -# @ref NECol2ImKernel (if NCHW data layout) |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 89 | * |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 90 | */ |
| 91 | class NEGEMMConvolutionLayer : public IFunction |
| 92 | { |
| 93 | public: |
| 94 | /** Constructor */ |
| 95 | NEGEMMConvolutionLayer(const std::shared_ptr<IMemoryManager> &memory_manager = nullptr); |
Georgios Pinitas | 1562be3 | 2018-03-08 19:09:19 +0000 | [diff] [blame] | 96 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 97 | NEGEMMConvolutionLayer(const NEGEMMConvolutionLayer &) = delete; |
| 98 | /** Default move constructor */ |
| 99 | NEGEMMConvolutionLayer(NEGEMMConvolutionLayer &&) = default; |
| 100 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 101 | NEGEMMConvolutionLayer &operator=(const NEGEMMConvolutionLayer &) = delete; |
| 102 | /** Default move assignment operator */ |
| 103 | NEGEMMConvolutionLayer &operator=(NEGEMMConvolutionLayer &&) = default; |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 104 | /** Set the input and output tensors. |
| 105 | * |
| 106 | * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], |
| 107 | * while every optional dimension from 4 and above represent a batch of inputs. |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 108 | * Data types supported: QASYMM8/F32. |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 109 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: Same as @p input. |
| 110 | * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. |
| 111 | * Data type supported: Should match @p input data type, except for input of QASYMM8 type where biases should be of S32 type. |
| 112 | * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 113 | * Data types supported: Same as @p input. |
| 114 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 115 | * @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 |
| 116 | * tensor has also been transposed with NEGEMMTranspose1xWKernel. Data type supported: Same as @p input. |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 117 | * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 118 | * @param[in] act_info (Optional) Activation layer information in case of a fused activation. Only RELU, BOUNDED_RELU and LU_BOUNDED_RELU supported. |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 119 | * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is not supported |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 120 | */ |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 121 | void configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info = WeightsInfo(), |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 122 | const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(), unsigned int num_groups = 1); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 123 | /** Static function to check if given info will lead to a valid configuration of @ref NEGEMMConvolutionLayer |
| 124 | * |
| 125 | * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], |
| 126 | * while every optional dimension from 4 and above represent a batch of inputs. |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 127 | * Data types supported: QASYMM8/F16/F32. |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 128 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported:Same as @p input. |
| 129 | * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. |
| 130 | * Data type supported: Should match @p input data type, except for input of QASYMM8 type where biases should be of S32 type. |
| 131 | * @param[in] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 132 | * Data types supported: Same as @p input. |
| 133 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 134 | * @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 |
| 135 | * tensor has also been transposed with NEGEMMTranspose1xWKernel. Data type supported: Same as @p input. |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 136 | * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 137 | * @param[in] act_info (Optional) Activation layer information in case of a fused activation. Only RELU, BOUNDED_RELU and LU_BOUNDED_RELU supported. |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 138 | * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is not supported |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 139 | * |
| 140 | * @return a status |
| 141 | */ |
| 142 | static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 143 | const WeightsInfo &weights_info = WeightsInfo(), const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(), unsigned int num_groups = 1); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 144 | |
| 145 | // Inherited methods overridden: |
| 146 | void run() override; |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 147 | void prepare() override; |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 148 | |
| 149 | private: |
| 150 | /** Configures the appropriate matrix multiply routine |
| 151 | * |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 152 | * @param[in] input Input tensor. Data types supported: QASYMM8/F16/F32. |
| 153 | * @param[in] weights Weights tensor. Data type supported: Same as @p input. |
| 154 | * @param[out] output Output tensor. Data types supported: Same as @p input, |
| 155 | * except for input of QASYMM8 type where output should be of S32 type. |
| 156 | * @param[in] gemm_3d_depth (Optional) Depth of GEMM 3D (Defaults to 1) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 157 | */ |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 158 | void configure_mm(const ITensor *input, const ITensor *weights, ITensor *output, int gemm_3d_depth = 1); |
| 159 | /** Static function to check if given info will lead to a valid configuration of @ref NEGEMMConvolutionLayer matrix multiply routines |
| 160 | * |
| 161 | * @param[in] input Input tensor. Data types supported: QASYMM8/F16/F32. |
| 162 | * @param[in] weights Weights tensor. Data type supported: Same as @p input. |
| 163 | * @param[in] 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] gemm_3d_depth (Optional) Depth of GEMM 3D (Defaults to 1) |
| 166 | * @param[in] skip_im2col (Optional) Flag which specifies if im2col has to be skipped. i.e. 1x1 convolution with NHWC data layout. (Default to false) |
| 167 | * |
| 168 | * @return a status |
| 169 | */ |
| 170 | static Status validate_mm(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output, int gemm_3d_depth = 1, bool skip_im2col = false); |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 171 | /** Static function to check if GEMM3D is supported in @ref NEGEMM or in @ref NEGEMMLowpMatrixMultiplyCore |
| 172 | * |
| 173 | * @param[in] data_type Input data type |
| 174 | * @param[in] gemm_3d_depth Depth of GEMM 3D |
| 175 | * @param[in] skip_im2col Flag which specifies if im2col has to be skipped. i.e. 1x1 convolution with NHWC data layout |
| 176 | * |
| 177 | * @return a status |
| 178 | */ |
| 179 | static Status validate_gemm3d(DataType data_type, int gemm_3d_depth, bool skip_im2col); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 180 | |
| 181 | private: |
| 182 | MemoryGroup _memory_group; |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 183 | NEConvolutionLayerReshapeWeights _reshape_weights; |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 184 | NEIm2ColKernel _im2col_kernel; |
| 185 | NEGEMM _mm_gemm; |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 186 | NEGEMMLowpMatrixMultiplyCore _mm_gemmlowp; |
| 187 | NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint _gemmlowp_output_stage; |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 188 | NECol2ImKernel _col2im_kernel; |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 189 | NEActivationLayer _activationlayer_function; |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 190 | NEArithmeticAdditionKernel _add_bias_kernel; |
Georgios Pinitas | e413d25 | 2018-11-14 18:29:58 +0000 | [diff] [blame] | 191 | NEReshapeLayer _reshape_layer; |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 192 | |
Georgios Pinitas | 1562be3 | 2018-03-08 19:09:19 +0000 | [diff] [blame] | 193 | const ITensor *_original_weights; |
| 194 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 195 | Tensor _im2col_output; |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 196 | Tensor _weights_reshaped; |
| 197 | Tensor _gemm_output; |
| 198 | Tensor _tmp_output; |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 199 | |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 200 | DataLayout _data_layout; |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 201 | |
| 202 | bool _append_bias; |
| 203 | bool _skip_im2col; |
| 204 | bool _skip_col2im; |
| 205 | bool _is_quantized; |
| 206 | bool _is_activationlayer_enabled; |
| 207 | bool _is_prepared; |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 208 | }; |
Georgios Pinitas | 041f36d | 2018-09-18 18:38:37 +0100 | [diff] [blame] | 209 | } // namespace arm_compute |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 210 | #endif /* __ARM_COMPUTE_NECONVOLUTIONGEMMLAYER_H__ */ |