Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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_NECONVOLUTIONLAYER_H__ |
| 25 | #define __ARM_COMPUTE_NECONVOLUTIONLAYER_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/NEGEMMInterleave4x4Kernel.h" |
| 32 | #include "arm_compute/core/NEON/kernels/NEGEMMMatrixMultiplyKernel.h" |
| 33 | #include "arm_compute/core/NEON/kernels/NEGEMMTranspose1xWKernel.h" |
| 34 | #include "arm_compute/core/NEON/kernels/NEIm2ColKernel.h" |
| 35 | #include "arm_compute/core/NEON/kernels/NEWeightsReshapeKernel.h" |
| 36 | #include "arm_compute/core/Types.h" |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame^] | 37 | #include "arm_compute/runtime/MemoryGroup.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 38 | #include "arm_compute/runtime/Tensor.h" |
| 39 | |
| 40 | namespace arm_compute |
| 41 | { |
| 42 | class ITensor; |
| 43 | |
| 44 | /** Function to reshape and perform 1xW transposition on the weights. This function calls the following kernels: |
| 45 | * -# @ref NEWeightsReshapeKernel |
| 46 | * -# @ref NEGEMMTranspose1xWKernel (executed in case GEMM is required for the operation) |
| 47 | */ |
| 48 | class NEConvolutionLayerReshapeWeights : public IFunction |
| 49 | { |
| 50 | public: |
| 51 | /** Constructor */ |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame^] | 52 | NEConvolutionLayerReshapeWeights(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 53 | /** Set the input and output tensors. |
| 54 | * |
Gian Marco Iodice | 2bbd964 | 2017-07-04 16:46:32 +0100 | [diff] [blame] | 55 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: QS8/QS16/F32. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 56 | * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p weights. |
| 57 | * @param[out] output Destination tensor. Data types supported: Same as @p weights. |
| 58 | * @param[in] transpose1xW True if the weights are to undergo a 1xW transposition after reshaping (in case of GEMM operation), false otherwise. |
| 59 | * Data types supported: Same as @p weights. |
| 60 | */ |
| 61 | void configure(const ITensor *weights, const ITensor *biases, ITensor *output, bool transpose1xW); |
| 62 | // Inherited methods overridden: |
| 63 | void run() override; |
| 64 | |
| 65 | private: |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame^] | 66 | MemoryGroup _memory_group; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 67 | NEWeightsReshapeKernel _weights_reshape_kernel; |
| 68 | NEGEMMTranspose1xWKernel _weights_transposed_kernel; |
| 69 | Tensor _weights_reshaped; |
| 70 | bool _transpose1xW; |
| 71 | }; |
| 72 | |
| 73 | /** Basic function to simulate a convolution layer. This function calls the following NEON kernels: |
| 74 | * -# @ref NEWeightsReshapeKernel (executed only once for each configuration) |
| 75 | * -# @ref NEIm2ColKernel |
| 76 | * -# @ref NEGEMMInterleave4x4Kernel (executed only in case GEMM is required for the operation) |
| 77 | * -# @ref NEGEMMMatrixMultiplyKernel |
| 78 | * -# @ref NECol2ImKernel |
| 79 | */ |
| 80 | class NEConvolutionLayer : public IFunction |
| 81 | { |
| 82 | public: |
| 83 | /** Constructor */ |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame^] | 84 | NEConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 85 | /** Set the input and output tensors. |
| 86 | * |
| 87 | * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], |
| 88 | * while every optional dimension from 4 and above represent a batch of inputs. |
Gian Marco Iodice | 2bbd964 | 2017-07-04 16:46:32 +0100 | [diff] [blame] | 89 | * Data types supported: QS8/QS16/F32. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 90 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: Same as @p input. |
| 91 | * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p input. |
| 92 | * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 93 | * Data types supported: Same as @p input. |
| 94 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 95 | * @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 |
| 96 | * tensor has also been transposed with NEGEMMTranspose1xWKernel. Data type supported: Same as @p input. |
| 97 | */ |
| 98 | void configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info = WeightsInfo()); |
| 99 | // Inherited methods overridden: |
| 100 | void run() override; |
| 101 | |
| 102 | private: |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame^] | 103 | MemoryGroup _memory_group; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 104 | NEIm2ColKernel _input_im2col_kernel; |
| 105 | NEGEMMInterleave4x4Kernel _input_interleave_kernel; |
| 106 | NEConvolutionLayerReshapeWeights _reshape_weights; |
| 107 | NEGEMMMatrixMultiplyKernel _mm_kernel; |
| 108 | NECol2ImKernel _output_col2im_kernel; |
| 109 | Tensor _input_im2col_reshaped; |
| 110 | Tensor _input_interleaved_reshaped; |
| 111 | Tensor _weights_reshaped; |
| 112 | Tensor _gemm_output; |
| 113 | bool _has_bias; |
| 114 | bool _is_fully_connected_convolution; |
| 115 | bool _are_weights_reshaped; |
| 116 | }; |
| 117 | } |
| 118 | #endif /* __ARM_COMPUTE_NECONVOLUTIONLAYER_H__ */ |