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 | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 40 | #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 | |
| 46 | namespace arm_compute |
| 47 | { |
| 48 | class 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 | */ |
| 54 | class NEConvolutionLayerReshapeWeights : public IFunction |
| 55 | { |
| 56 | public: |
| 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 | |
| 83 | private: |
| 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 | */ |
| 99 | class NEGEMMConvolutionLayer : public IFunction |
| 100 | { |
| 101 | public: |
| 102 | /** Constructor */ |
| 103 | NEGEMMConvolutionLayer(const std::shared_ptr<IMemoryManager> &memory_manager = nullptr); |
Georgios Pinitas | 1562be3 | 2018-03-08 19:09:19 +0000 | [diff] [blame] | 104 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 105 | NEGEMMConvolutionLayer(const NEGEMMConvolutionLayer &) = delete; |
| 106 | /** Default move constructor */ |
| 107 | NEGEMMConvolutionLayer(NEGEMMConvolutionLayer &&) = default; |
| 108 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 109 | NEGEMMConvolutionLayer &operator=(const NEGEMMConvolutionLayer &) = delete; |
| 110 | /** Default move assignment operator */ |
| 111 | NEGEMMConvolutionLayer &operator=(NEGEMMConvolutionLayer &&) = default; |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 112 | /** Set the input and output tensors. |
| 113 | * |
| 114 | * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], |
| 115 | * while every optional dimension from 4 and above represent a batch of inputs. |
| 116 | * Data types supported: QS8/QASYMM8/QS16/F32. |
| 117 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: Same as @p input. |
| 118 | * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. |
| 119 | * Data type supported: Should match @p input data type, except for input of QASYMM8 type where biases should be of S32 type. |
| 120 | * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 121 | * Data types supported: Same as @p input. |
| 122 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 123 | * @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 |
| 124 | * 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] | 125 | * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 126 | */ |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 127 | void configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info = WeightsInfo(), |
| 128 | const Size2D &dilation = Size2D(1U, 1U)); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 129 | /** Static function to check if given info will lead to a valid configuration of @ref NEGEMMConvolutionLayer |
| 130 | * |
| 131 | * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], |
| 132 | * while every optional dimension from 4 and above represent a batch of inputs. |
| 133 | * Data types supported: QS8/QASYMM8/QS16/F16/F32. |
| 134 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported:Same as @p input. |
| 135 | * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. |
| 136 | * Data type supported: Should match @p input data type, except for input of QASYMM8 type where biases should be of S32 type. |
| 137 | * @param[in] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 138 | * Data types supported: Same as @p input. |
| 139 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 140 | * @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 |
| 141 | * 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] | 142 | * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 143 | * |
| 144 | * @return a status |
| 145 | */ |
| 146 | static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 147 | const WeightsInfo &weights_info = WeightsInfo(), const Size2D &dilation = Size2D(1U, 1U)); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 148 | |
| 149 | // Inherited methods overridden: |
| 150 | void run() override; |
| 151 | |
| 152 | private: |
| 153 | /** Configures the appropriate matrix multiply routine |
| 154 | * |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 155 | * @param[in] input Input tensor. Data types supported: QS8/QASYMM8/QS16/F16/F32. |
| 156 | * @param[in] weights Weights tensor. Data type supported: Same as @p input. |
| 157 | * @param[out] output Output tensor. Data types supported: Same as @p input, |
| 158 | * except for input of QASYMM8 type where output should be of S32 type. |
| 159 | * @param[in] is_interleaved (Optional) True if input0 and input1 have been reshaped respectively using @ref CLGEMMInterleave4x4Kernel and @ref CLGEMMTranspose1xWKernel |
| 160 | * @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] | 161 | */ |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 162 | 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] | 163 | |
| 164 | private: |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 165 | AssemblyKernelGlueF32 _asm_glue; |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 166 | MemoryGroup _memory_group; |
| 167 | NEIm2ColKernel _input_im2col_kernel; |
| 168 | NEGEMMInterleave4x4Kernel _input_interleave_kernel; |
| 169 | NEConvolutionLayerReshapeWeights _reshape_weights; |
| 170 | NEGEMMMatrixMultiplyKernel _mm_kernel; |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 171 | NEGEMMLowpMatrixMultiplyCore _mm_gemmlowp; |
| 172 | NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint _gemmlowp_output_stage; |
| 173 | NECol2ImKernel _output_col2im_kernel; |
| 174 | |
Georgios Pinitas | 1562be3 | 2018-03-08 19:09:19 +0000 | [diff] [blame] | 175 | const ITensor *_original_weights; |
| 176 | |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 177 | Tensor _input_im2col_reshaped; |
| 178 | Tensor _input_interleaved_reshaped; |
| 179 | Tensor _weights_reshaped; |
| 180 | Tensor _gemm_output; |
| 181 | Tensor _tmp_output; |
| 182 | Tensor _workspace; |
| 183 | |
| 184 | bool _append_bias; |
| 185 | bool _is_fully_connected_convolution; |
| 186 | bool _are_weights_reshaped; |
| 187 | bool _is_quantized; |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 188 | bool _is_interleaved; |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 189 | }; |
| 190 | } |
| 191 | #endif /* __ARM_COMPUTE_NECONVOLUTIONGEMMLAYER_H__ */ |