Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [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 | |
| 25 | #ifndef __ARM_COMPUTE_GCCONVOLUTIONLAYER_H__ |
| 26 | #define __ARM_COMPUTE_GCCONVOLUTIONLAYER_H__ |
| 27 | |
| 28 | #include "arm_compute/core/GLES_COMPUTE/kernels/GCCol2ImKernel.h" |
| 29 | #include "arm_compute/core/GLES_COMPUTE/kernels/GCFillBorderKernel.h" |
| 30 | #include "arm_compute/core/GLES_COMPUTE/kernels/GCGEMMInterleave4x4Kernel.h" |
| 31 | #include "arm_compute/core/GLES_COMPUTE/kernels/GCGEMMMatrixMultiplyKernel.h" |
| 32 | #include "arm_compute/core/GLES_COMPUTE/kernels/GCGEMMTranspose1xWKernel.h" |
| 33 | #include "arm_compute/core/GLES_COMPUTE/kernels/GCIm2ColKernel.h" |
| 34 | #include "arm_compute/core/GLES_COMPUTE/kernels/GCWeightsReshapeKernel.h" |
| 35 | #include "arm_compute/core/Types.h" |
| 36 | #include "arm_compute/runtime/GLES_COMPUTE/GCTensor.h" |
| 37 | #include "arm_compute/runtime/IFunction.h" |
| 38 | |
| 39 | #include <memory> |
| 40 | |
| 41 | namespace arm_compute |
| 42 | { |
| 43 | class IGCTensor; |
| 44 | |
| 45 | /** Function to reshape and transpose the weights. This function calls the following kernels: |
| 46 | * -# @ref GCWeightsReshapeKernel |
| 47 | * -# @ref GCGEMMTranspose1xWKernel |
| 48 | */ |
| 49 | class GCConvolutionLayerReshapeWeights : public IFunction |
| 50 | { |
| 51 | public: |
| 52 | /** Constructor */ |
| 53 | GCConvolutionLayerReshapeWeights(); |
| 54 | /** Set the input and output tensors. |
| 55 | * |
| 56 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. |
| 57 | * Data type supported: F16/F32. |
| 58 | * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p weights. |
| 59 | * @param[out] output Destination tensor. Data types supported: Same as @p weights. |
| 60 | * @param[in] transpose1xW True if the weights are to undergo a 1xW transposition after reshaping (in case of GEMM operation), false otherwise. |
| 61 | * Data types supported: Same as @p weights. |
| 62 | */ |
| 63 | void configure(const IGCTensor *weights, const IGCTensor *biases, IGCTensor *output, bool transpose1xW); |
| 64 | // Inherited methods overridden: |
| 65 | void run() override; |
| 66 | |
| 67 | private: |
| 68 | GCWeightsReshapeKernel _weights_reshape_kernel; |
| 69 | GCGEMMTranspose1xWKernel _weights_transposed_kernel; |
| 70 | GCTensor _weights_reshaped; |
| 71 | bool _transpose1xW; |
| 72 | }; |
| 73 | |
| 74 | /** Basic function to compute the convolution layer. This function calls the following GLES kernels: |
| 75 | * |
| 76 | * -# @ref GCWeightsReshapeKernel (executed only once for each configuration) |
| 77 | * -# @ref GCGEMMTranspose1xWKernel (executed only once for each configuration) |
| 78 | * -# @ref GCIm2ColKernel |
| 79 | * -# @ref GCGEMMInterleave4x4Kernel |
| 80 | * -# @ref GCCol2ImKernel |
| 81 | */ |
| 82 | class GCConvolutionLayer : public IFunction |
| 83 | { |
| 84 | public: |
| 85 | /** Default constructor */ |
| 86 | GCConvolutionLayer(); |
| 87 | |
| 88 | /** Set the input and output tensors. |
| 89 | * |
| 90 | * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], |
| 91 | * while every optional dimension from 4 and above represent a batch of inputs. |
| 92 | * Data types supported: F16/F32. |
| 93 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: Same as @p input. |
| 94 | * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. |
| 95 | * Data type supported: Should match @p input data type, except for input of QASYMM8 type where biases should be of S32 type. |
| 96 | * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 97 | * Data types supported: Same as @p input. |
| 98 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 99 | * @param[in] weights_info Specifies if the weights tensor has been reshaped with GCWeightsReshapeKernel. If this is not part of the fully connected layer the weights |
| 100 | * tensor has also been transposed with GCGEMMTranspose1xWKernel. Data type supported: Same as @p input. |
| 101 | */ |
| 102 | void configure(const IGCTensor *input, const IGCTensor *weights, const IGCTensor *biases, IGCTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info = WeightsInfo()); |
| 103 | |
| 104 | // Inherited methods overridden: |
| 105 | void run() override; |
| 106 | |
| 107 | private: |
| 108 | /** Configures the appropriate matrix multiply routine |
| 109 | * |
| 110 | * @param input Input tensor. Data types supported: F16/F32. |
| 111 | * @param weights Weights tensor. Data type supported: Same as @p input. |
| 112 | * @param output Output tensor. Data types supported: Same as @p input, |
| 113 | * @param is_interleaved_transposed Flag that signals if matrix is interleaved transposed |
| 114 | */ |
| 115 | void configure_mm(const IGCTensor *input, const IGCTensor *weights, IGCTensor *output, bool is_interleaved_transposed = true); |
| 116 | |
| 117 | private: |
| 118 | GCConvolutionLayerReshapeWeights _reshape_weights; |
| 119 | GCIm2ColKernel _input_im2col_kernel; |
| 120 | GCGEMMInterleave4x4Kernel _input_interleave_kernel; |
| 121 | GCGEMMMatrixMultiplyKernel _mm_kernel; |
| 122 | GCCol2ImKernel _output_col2im_kernel; |
| 123 | GCFillBorderKernel _fill_border; |
| 124 | |
| 125 | GCTensor _input_im2col_reshaped; |
| 126 | GCTensor _input_interleaved_reshaped; |
| 127 | GCTensor _weights_reshaped; |
| 128 | GCTensor _weights_transposed; |
| 129 | GCTensor _gemm_output; |
| 130 | GCTensor _tmp_output; |
| 131 | |
| 132 | bool _append_bias; |
| 133 | bool _is_fully_connected_convolution; |
| 134 | bool _are_weights_reshaped; |
| 135 | }; |
| 136 | } |
| 137 | |
| 138 | #endif /* __ARM_COMPUTE_GCCONVOLUTIONLAYER_H__ */ |