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" |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 30 | #include "arm_compute/core/GLES_COMPUTE/kernels/GCIm2ColKernel.h" |
| 31 | #include "arm_compute/core/GLES_COMPUTE/kernels/GCWeightsReshapeKernel.h" |
| 32 | #include "arm_compute/core/Types.h" |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 33 | #include "arm_compute/runtime/GLES_COMPUTE/GCMemoryGroup.h" |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 34 | #include "arm_compute/runtime/GLES_COMPUTE/GCTensor.h" |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 35 | #include "arm_compute/runtime/GLES_COMPUTE/functions/GCActivationLayer.h" |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame] | 36 | #include "arm_compute/runtime/GLES_COMPUTE/functions/GCGEMM.h" |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 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 |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 47 | */ |
| 48 | class GCConvolutionLayerReshapeWeights : public IFunction |
| 49 | { |
| 50 | public: |
| 51 | /** Constructor */ |
| 52 | GCConvolutionLayerReshapeWeights(); |
| 53 | /** Set the input and output tensors. |
| 54 | * |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame] | 55 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. |
| 56 | * Data type supported: F16/F32. |
| 57 | * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p weights. |
| 58 | * @param[out] output Destination tensor. Data types supported: Same as @p weights. |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 59 | */ |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame] | 60 | void configure(const IGCTensor *weights, const IGCTensor *biases, IGCTensor *output); |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 61 | // Inherited methods overridden: |
| 62 | void run() override; |
| 63 | |
| 64 | private: |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame] | 65 | GCWeightsReshapeKernel _weights_reshape_kernel; |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | /** Basic function to compute the convolution layer. This function calls the following GLES kernels: |
| 69 | * |
| 70 | * -# @ref GCWeightsReshapeKernel (executed only once for each configuration) |
| 71 | * -# @ref GCGEMMTranspose1xWKernel (executed only once for each configuration) |
| 72 | * -# @ref GCIm2ColKernel |
| 73 | * -# @ref GCGEMMInterleave4x4Kernel |
| 74 | * -# @ref GCCol2ImKernel |
| 75 | */ |
| 76 | class GCConvolutionLayer : public IFunction |
| 77 | { |
| 78 | public: |
| 79 | /** Default constructor */ |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 80 | GCConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame] | 81 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 82 | GCConvolutionLayer(const GCConvolutionLayer &) = delete; |
| 83 | /** Default move constructor */ |
| 84 | GCConvolutionLayer(GCConvolutionLayer &&) = default; |
| 85 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 86 | GCConvolutionLayer &operator=(const GCConvolutionLayer &) = delete; |
| 87 | /** Default move assignment operator */ |
| 88 | GCConvolutionLayer &operator=(GCConvolutionLayer &&) = default; |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 89 | /** Set the input and output tensors. |
| 90 | * |
| 91 | * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], |
| 92 | * while every optional dimension from 4 and above represent a batch of inputs. |
| 93 | * Data types supported: F16/F32. |
| 94 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: Same as @p input. |
| 95 | * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. |
| 96 | * Data type supported: Should match @p input data type, except for input of QASYMM8 type where biases should be of S32 type. |
| 97 | * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 98 | * Data types supported: Same as @p input. |
| 99 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 100 | * @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 |
| 101 | * tensor has also been transposed with GCGEMMTranspose1xWKernel. Data type supported: Same as @p input. |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 102 | * @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] | 103 | * @param[in] act_info (Optional) Activation layer information in case of a fused activation. |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 104 | */ |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 105 | void configure(const IGCTensor *input, const IGCTensor *weights, const IGCTensor *biases, IGCTensor *output, const PadStrideInfo &conv_info, |
| 106 | const WeightsInfo &weights_info = WeightsInfo(), const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo()); |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame] | 107 | /** Static function to check if given info will lead to a valid configuration of @ref GCConvolutionLayer. |
| 108 | * |
| 109 | * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], |
| 110 | * while every optional dimension from 4 and above represent a batch of inputs. |
| 111 | * Data types supported: QS8/QASYMM8/QS16/F16/F32. |
| 112 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: Same as @p input. |
| 113 | * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. |
| 114 | * Data type supported: Should match @p input data type, except for input of QASYMM8 type where biases should be of S32 type. |
| 115 | * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 116 | * Data types supported: Same as @p input. |
| 117 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 118 | * @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 |
| 119 | * tensor has also been transposed with GCGEMMTranspose1xWKernel. Data type supported: Same as @p input. |
| 120 | * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). |
| 121 | * @param[in] act_info (Optional) Activation layer information in case of a fused activation. |
| 122 | * |
| 123 | * @return a status |
| 124 | */ |
| 125 | static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
| 126 | const WeightsInfo &weights_info = WeightsInfo(), const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo()); |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 127 | |
| 128 | // Inherited methods overridden: |
| 129 | void run() override; |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame^] | 130 | void prepare() override; |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 131 | |
| 132 | private: |
| 133 | /** Configures the appropriate matrix multiply routine |
| 134 | * |
| 135 | * @param input Input tensor. Data types supported: F16/F32. |
| 136 | * @param weights Weights tensor. Data type supported: Same as @p input. |
| 137 | * @param output Output tensor. Data types supported: Same as @p input, |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 138 | */ |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame] | 139 | void configure_mm(const IGCTensor *input, const IGCTensor *weights, IGCTensor *output); |
| 140 | /** Static function to check if given info will lead to a valid configuration of @ref GCGEMMConvolutionLayer matrix multiply routines |
| 141 | * |
| 142 | * @param[in] input Input tensor. Data types supported: QS8/QASYMM8/QS16/F16/F32. |
| 143 | * @param[in] weights Weights tensor. Data type supported: Same as @p input. |
| 144 | * @param[in] output Output tensor. Data types supported: Same as @p input, |
| 145 | * except for input of QASYMM8 type where output should be of S32 type. |
| 146 | * |
| 147 | * @return a status |
| 148 | */ |
| 149 | static Status validate_mm(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output); |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 150 | |
| 151 | private: |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 152 | GCMemoryGroup _memory_group; |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 153 | GCConvolutionLayerReshapeWeights _reshape_weights; |
| 154 | GCIm2ColKernel _input_im2col_kernel; |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame] | 155 | GCGEMM _mm_gemm; |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 156 | GCCol2ImKernel _output_col2im_kernel; |
| 157 | GCFillBorderKernel _fill_border; |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 158 | GCActivationLayer _activationlayer_function; |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 159 | |
Michele Di Giorgio | 164b65d | 2018-04-13 14:28:08 +0100 | [diff] [blame] | 160 | const IGCTensor *_original_weights; |
| 161 | |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 162 | GCTensor _input_im2col_reshaped; |
| 163 | GCTensor _input_interleaved_reshaped; |
| 164 | GCTensor _weights_reshaped; |
| 165 | GCTensor _weights_transposed; |
| 166 | GCTensor _gemm_output; |
| 167 | GCTensor _tmp_output; |
| 168 | |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 169 | bool _is_activationlayer_enabled; |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame^] | 170 | bool _is_prepared; |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 171 | }; |
| 172 | } |
| 173 | |
| 174 | #endif /* __ARM_COMPUTE_GCCONVOLUTIONLAYER_H__ */ |