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 | |
| 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 Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 39 | #include "arm_compute/runtime/NEON/AssemblyHelper.h" |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 40 | #include "arm_compute/runtime/NEON/functions/NEActivationLayer.h" |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 41 | #include "arm_compute/runtime/NEON/functions/NEGEMMLowpMatrixMultiplyCore.h" |
| 42 | #include "arm_compute/runtime/NEON/functions/NEGEMMLowpOutputStage.h" |
| 43 | #include "arm_compute/runtime/Tensor.h" |
| 44 | |
| 45 | #include <memory> |
| 46 | |
| 47 | namespace arm_compute |
| 48 | { |
| 49 | class ITensor; |
| 50 | |
| 51 | /** Function to reshape and perform 1xW transposition on the weights. This function calls the following kernels: |
| 52 | * -# @ref NEWeightsReshapeKernel |
| 53 | * -# @ref NEGEMMTranspose1xWKernel (executed in case GEMM is required for the operation) |
| 54 | */ |
| 55 | class NEConvolutionLayerReshapeWeights : public IFunction |
| 56 | { |
| 57 | public: |
| 58 | /** Constructor */ |
| 59 | NEConvolutionLayerReshapeWeights(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
| 60 | /** Set the input and output tensors. |
| 61 | * |
| 62 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: QS8/QASYMM8/QS16/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. |
| 65 | * @param[in] transpose1xW True if the weights are to undergo a 1xW transposition after reshaping (in case of GEMM operation), false otherwise. |
| 66 | * Data types supported: Same as @p weights. |
| 67 | */ |
| 68 | void configure(const ITensor *weights, const ITensor *biases, ITensor *output, bool transpose1xW); |
| 69 | /** Static function to check if given info will lead to a valid configuration of @ref NEConvolutionLayerReshapeWeights |
| 70 | * |
| 71 | * @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. |
| 72 | * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p weights. |
| 73 | * @param[in] output Destination tensor. Data types supported: Same as @p weights. |
| 74 | * @param[in] transpose1xW True if the weights are to undergo a 1xW transposition after reshaping (in case of GEMM operation), false otherwise. |
| 75 | * Data types supported: Same as @p weights. |
| 76 | * |
| 77 | * @return an error status |
| 78 | */ |
| 79 | static Status validate(const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, bool transpose1xW); |
| 80 | |
| 81 | // Inherited methods overridden: |
| 82 | void run() override; |
| 83 | |
| 84 | private: |
| 85 | MemoryGroup _memory_group; |
| 86 | NEWeightsReshapeKernel _weights_reshape_kernel; |
| 87 | NEGEMMTranspose1xWKernel _weights_transposed_kernel; |
| 88 | Tensor _weights_reshaped; |
| 89 | bool _transpose1xW; |
| 90 | }; |
| 91 | |
| 92 | /** Basic function to simulate a convolution layer. This function calls the following NEON kernels: |
| 93 | * -# @ref NEWeightsReshapeKernel (executed only once for each configuration) |
| 94 | * -# @ref NEIm2ColKernel |
| 95 | * -# @ref NEGEMMInterleave4x4Kernel (executed only in case GEMM is required for the operation) |
| 96 | * -# @ref NEGEMMMatrixMultiplyKernel or @ref NEGEMMLowpMatrixMultiplyCore (if quantized asymmetric) |
| 97 | * -# @ref NEGEMMLowpQuantizeDownInt32ToUint8Scale (if quantized asymmetric) |
| 98 | * -# @ref NECol2ImKernel |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 99 | * -# @ref NEActivationLayer (executed only if the activation layer is enabled) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 100 | */ |
| 101 | class NEGEMMConvolutionLayer : public IFunction |
| 102 | { |
| 103 | public: |
| 104 | /** Constructor */ |
| 105 | NEGEMMConvolutionLayer(const std::shared_ptr<IMemoryManager> &memory_manager = nullptr); |
Georgios Pinitas | 1562be3 | 2018-03-08 19:09:19 +0000 | [diff] [blame] | 106 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 107 | NEGEMMConvolutionLayer(const NEGEMMConvolutionLayer &) = delete; |
| 108 | /** Default move constructor */ |
| 109 | NEGEMMConvolutionLayer(NEGEMMConvolutionLayer &&) = default; |
| 110 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 111 | NEGEMMConvolutionLayer &operator=(const NEGEMMConvolutionLayer &) = delete; |
| 112 | /** Default move assignment operator */ |
| 113 | NEGEMMConvolutionLayer &operator=(NEGEMMConvolutionLayer &&) = default; |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 114 | /** Set the input and output tensors. |
| 115 | * |
| 116 | * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], |
| 117 | * while every optional dimension from 4 and above represent a batch of inputs. |
| 118 | * Data types supported: QS8/QASYMM8/QS16/F32. |
| 119 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: Same as @p input. |
| 120 | * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. |
| 121 | * Data type supported: Should match @p input data type, except for input of QASYMM8 type where biases should be of S32 type. |
| 122 | * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 123 | * Data types supported: Same as @p input. |
| 124 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 125 | * @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 |
| 126 | * 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] | 127 | * @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] | 128 | * @param[in] act_info (Optional) Activation layer information in case of a fused activation. Only RELU, BOUNDED_RELU and LU_BOUNDED_RELU supported. |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 129 | */ |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 130 | void configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info = WeightsInfo(), |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 131 | const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo()); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 132 | /** Static function to check if given info will lead to a valid configuration of @ref NEGEMMConvolutionLayer |
| 133 | * |
| 134 | * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], |
| 135 | * while every optional dimension from 4 and above represent a batch of inputs. |
| 136 | * Data types supported: QS8/QASYMM8/QS16/F16/F32. |
| 137 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported:Same as @p input. |
| 138 | * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. |
| 139 | * Data type supported: Should match @p input data type, except for input of QASYMM8 type where biases should be of S32 type. |
| 140 | * @param[in] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 141 | * Data types supported: Same as @p input. |
| 142 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 143 | * @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 |
| 144 | * 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] | 145 | * @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] | 146 | * @param[in] act_info (Optional) Activation layer information in case of a fused activation. Only RELU, BOUNDED_RELU and LU_BOUNDED_RELU supported. |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 147 | * |
| 148 | * @return a status |
| 149 | */ |
| 150 | static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 151 | const WeightsInfo &weights_info = WeightsInfo(), const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo()); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 152 | |
| 153 | // Inherited methods overridden: |
| 154 | void run() override; |
| 155 | |
| 156 | private: |
| 157 | /** Configures the appropriate matrix multiply routine |
| 158 | * |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 159 | * @param[in] input Input tensor. Data types supported: QS8/QASYMM8/QS16/F16/F32. |
| 160 | * @param[in] weights Weights tensor. Data type supported: Same as @p input. |
| 161 | * @param[out] output Output tensor. Data types supported: Same as @p input, |
| 162 | * except for input of QASYMM8 type where output should be of S32 type. |
| 163 | * @param[in] is_interleaved (Optional) True if input0 and input1 have been reshaped respectively using @ref CLGEMMInterleave4x4Kernel and @ref CLGEMMTranspose1xWKernel |
| 164 | * @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 Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 165 | */ |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 166 | void configure_mm(const ITensor *input, const ITensor *weights, ITensor *output, bool is_interleaved, const GEMMReshapeInfo &reshape_info = GEMMReshapeInfo()); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 167 | |
| 168 | private: |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 169 | AssemblyKernelGlueF32 _asm_glue; |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 170 | MemoryGroup _memory_group; |
| 171 | NEIm2ColKernel _input_im2col_kernel; |
| 172 | NEGEMMInterleave4x4Kernel _input_interleave_kernel; |
| 173 | NEConvolutionLayerReshapeWeights _reshape_weights; |
| 174 | NEGEMMMatrixMultiplyKernel _mm_kernel; |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 175 | NEGEMMLowpMatrixMultiplyCore _mm_gemmlowp; |
| 176 | NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint _gemmlowp_output_stage; |
| 177 | NECol2ImKernel _output_col2im_kernel; |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 178 | NEActivationLayer _activationlayer_function; |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 179 | |
Georgios Pinitas | 1562be3 | 2018-03-08 19:09:19 +0000 | [diff] [blame] | 180 | const ITensor *_original_weights; |
| 181 | |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 182 | Tensor _input_im2col_reshaped; |
| 183 | Tensor _input_interleaved_reshaped; |
| 184 | Tensor _weights_reshaped; |
| 185 | Tensor _gemm_output; |
| 186 | Tensor _tmp_output; |
| 187 | Tensor _workspace; |
| 188 | |
| 189 | bool _append_bias; |
| 190 | bool _is_fully_connected_convolution; |
| 191 | bool _are_weights_reshaped; |
| 192 | bool _is_quantized; |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 193 | bool _is_interleaved; |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 194 | bool _is_activationlayer_enabled; |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 195 | }; |
| 196 | } |
| 197 | #endif /* __ARM_COMPUTE_NECONVOLUTIONGEMMLAYER_H__ */ |