Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +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_CLGEMMCONVOLUTIONLAYER_H__ |
| 25 | #define __ARM_COMPUTE_CLGEMMCONVOLUTIONLAYER_H__ |
| 26 | |
| 27 | #include "arm_compute/runtime/IFunction.h" |
| 28 | |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/CL/kernels/CLArithmeticAdditionKernel.h" |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 30 | #include "arm_compute/core/CL/kernels/CLCol2ImKernel.h" |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 31 | #include "arm_compute/core/CL/kernels/CLGEMMInterleave4x4Kernel.h" |
| 32 | #include "arm_compute/core/CL/kernels/CLGEMMMatrixMultiplyKernel.h" |
| 33 | #include "arm_compute/core/CL/kernels/CLGEMMTranspose1xWKernel.h" |
| 34 | #include "arm_compute/core/CL/kernels/CLIm2ColKernel.h" |
| 35 | #include "arm_compute/core/CL/kernels/CLWeightsReshapeKernel.h" |
| 36 | #include "arm_compute/core/Types.h" |
| 37 | #include "arm_compute/runtime/CL/CLMemoryGroup.h" |
| 38 | #include "arm_compute/runtime/CL/CLTensor.h" |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 39 | #include "arm_compute/runtime/CL/functions/CLActivationLayer.h" |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 40 | #include "arm_compute/runtime/CL/functions/CLGEMM.h" |
| 41 | #include "arm_compute/runtime/CL/functions/CLGEMMLowpMatrixMultiplyCore.h" |
| 42 | #include "arm_compute/runtime/CL/functions/CLGEMMLowpOutputStage.h" |
Gian Marco Iodice | dff601d | 2018-08-09 13:28:41 +0100 | [diff] [blame^] | 43 | #include "arm_compute/runtime/CL/functions/CLReshapeLayer.h" |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 44 | #include "arm_compute/runtime/IMemoryManager.h" |
| 45 | |
| 46 | #include <memory> |
| 47 | |
| 48 | namespace arm_compute |
| 49 | { |
| 50 | class ICLTensor; |
| 51 | |
| 52 | /** Function to reshape and transpose the weights. This function calls the following kernels: |
| 53 | * -# @ref CLWeightsReshapeKernel |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 54 | */ |
| 55 | class CLConvolutionLayerReshapeWeights : public IFunction |
| 56 | { |
| 57 | public: |
| 58 | /** Constructor */ |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 59 | CLConvolutionLayerReshapeWeights(); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 60 | /** Set the input and output tensors. |
| 61 | * |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 62 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 63 | * Data type supported: QASYMM8/F16/F32. |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 64 | * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p weights. |
| 65 | * @param[out] output Destination tensor. Data types supported: Same as @p weights. |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 66 | */ |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 67 | void configure(const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output); |
| 68 | /** Static function to check if given info will lead to a valid configuration of @ref CLConvolutionLayerReshapeWeights |
| 69 | * |
| 70 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 71 | * Data type supported: QASYMM8/F16/F32. |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 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 | * |
| 75 | * @return a status |
| 76 | */ |
| 77 | static Status validate(const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 78 | // Inherited methods overridden: |
| 79 | void run() override; |
| 80 | |
| 81 | private: |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 82 | CLWeightsReshapeKernel _weights_reshape_kernel; |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | /** Basic function to compute the convolution layer. This function calls the following OpenCL kernels/functions: |
| 86 | * |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 87 | * -# @ref CLIm2ColKernel |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 88 | * -# @ref CLGEMM (if the data type is FP32 or FP16) |
| 89 | * -# @ref CLGEMMLowpMatrixMultiplyCore (if the data type is QASYMM8) |
| 90 | * -# @ref CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint (if the data type is QASYMM8) |
| 91 | * -# @ref CLArithmeticAdditionKernel (if biases != nullptr and we have a 1x1 convolution with the NHWC data layout) |
Gian Marco Iodice | dff601d | 2018-08-09 13:28:41 +0100 | [diff] [blame^] | 92 | * -# @ref CLCol2ImKernel (if NCHW data layout) or @ref CLReshapeLayer (if NHWC with QASYMM8) |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 93 | */ |
| 94 | class CLGEMMConvolutionLayer : public IFunction |
| 95 | { |
| 96 | public: |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 97 | /** Default constructor |
| 98 | * |
| 99 | * @param[in] memory_manager (Optional) Memory manager. |
| 100 | */ |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 101 | CLGEMMConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
Georgios Pinitas | 1562be3 | 2018-03-08 19:09:19 +0000 | [diff] [blame] | 102 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 103 | CLGEMMConvolutionLayer(const CLGEMMConvolutionLayer &) = delete; |
| 104 | /** Default move constructor */ |
| 105 | CLGEMMConvolutionLayer(CLGEMMConvolutionLayer &&) = default; |
| 106 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 107 | CLGEMMConvolutionLayer &operator=(const CLGEMMConvolutionLayer &) = delete; |
| 108 | /** Default move assignment operator */ |
| 109 | CLGEMMConvolutionLayer &operator=(CLGEMMConvolutionLayer &&) = default; |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 110 | /** Set the input and output tensors. |
| 111 | * |
| 112 | * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], |
| 113 | * while every optional dimension from 4 and above represent a batch of inputs. |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 114 | * Data types supported: QASYMM8/F16/F32. |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 115 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: Same as @p input. |
| 116 | * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. |
| 117 | * Data type supported: Should match @p input data type, except for input of QASYMM8 type where biases should be of S32 type. |
| 118 | * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 119 | * Data types supported: Same as @p input. |
| 120 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 121 | * @param[in] weights_info Specifies if the weights tensor has been reshaped with CLWeightsReshapeKernel. If this is not part of the fully connected layer the weights |
| 122 | * tensor has also been transposed with CLGEMMTranspose1xWKernel. Data type supported: Same as @p input. |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 123 | * @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] | 124 | * @param[in] act_info (Optional) Activation layer information in case of a fused activation. |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 125 | */ |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 126 | void configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info = WeightsInfo(), |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 127 | const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo()); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 128 | /** Static function to check if given info will lead to a valid configuration of @ref CLGEMMConvolutionLayer. |
| 129 | * |
| 130 | * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], |
| 131 | * while every optional dimension from 4 and above represent a batch of inputs. |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 132 | * Data types supported: QASYMM8/F16/F32. |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 133 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: Same as @p input. |
| 134 | * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. |
| 135 | * Data type supported: Should match @p input data type, except for input of QASYMM8 type where biases should be of S32 type. |
| 136 | * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 137 | * Data types supported: Same as @p input. |
| 138 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 139 | * @param[in] weights_info Specifies if the weights tensor has been reshaped with CLWeightsReshapeKernel. If this is not part of the fully connected layer the weights |
| 140 | * tensor has also been transposed with CLGEMMTranspose1xWKernel. Data type supported: Same as @p input. |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 141 | * @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] | 142 | * @param[in] act_info (Optional) Activation layer information in case of a fused activation. |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 143 | * |
| 144 | * @return a status |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 145 | */ |
| 146 | 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] | 147 | const WeightsInfo &weights_info = WeightsInfo(), const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo()); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 148 | |
| 149 | // Inherited methods overridden: |
| 150 | void run() override; |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 151 | void prepare() override; |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 152 | |
| 153 | private: |
| 154 | /** Configures the appropriate matrix multiply routine |
| 155 | * |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 156 | * @param[in] input Input tensor. Data types supported: QASYMM8/F16/F32. |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 157 | * @param[in] weights Weights tensor. Data type supported: Same as @p input. |
| 158 | * @param[in, out] output Output tensor. Data types supported: Same as @p input, |
| 159 | * except for input of QASYMM8 type where output should be of S32 type. |
| 160 | * @param[in] gemm_3d_depth (Optional) Depth of GEMM 3D (Defaults to 1) |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 161 | */ |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 162 | void configure_mm(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output, int gemm_3d_depth = 1); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 163 | /** Static function to check if given info will lead to a valid configuration of @ref CLGEMMConvolutionLayer matrix multiply routines |
| 164 | * |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 165 | * @param[in] input Input tensor. Data types supported: QASYMM8/F16/F32. |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 166 | * @param[in] weights Weights tensor. Data type supported: Same as @p input. |
| 167 | * @param[in] output Output tensor. Data types supported: Same as @p input, |
| 168 | * except for input of QASYMM8 type where output should be of S32 type. |
| 169 | * @param[in] gemm_3d_depth (Optional) Depth of GEMM 3D (Defaults to 1) |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 170 | * @param[in] skip_im2col (Optional) Flag which specifies if im2col has to be skipped. i.e. 1x1 convolution with NHWC data layout. (Default to false) |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 171 | * |
| 172 | * @return a status |
| 173 | */ |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 174 | static Status validate_mm(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output, int gemm_3d_depth = 1, bool skip_im2col = false); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 175 | |
| 176 | private: |
| 177 | CLMemoryGroup _memory_group; |
| 178 | CLConvolutionLayerReshapeWeights _reshape_weights; |
| 179 | CLIm2ColKernel _im2col_kernel; |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 180 | CLGEMM _mm_gemm; |
| 181 | CLGEMMLowpMatrixMultiplyCore _mm_gemmlowp; |
| 182 | CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint _gemmlowp_output_stage; |
| 183 | CLCol2ImKernel _col2im_kernel; |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 184 | CLActivationLayer _activationlayer_function; |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 185 | CLArithmeticAdditionKernel _add_bias_kernel; |
Gian Marco Iodice | dff601d | 2018-08-09 13:28:41 +0100 | [diff] [blame^] | 186 | CLReshapeLayer _reshape_layer; |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 187 | |
Georgios Pinitas | 1562be3 | 2018-03-08 19:09:19 +0000 | [diff] [blame] | 188 | const ICLTensor *_original_weights; |
| 189 | |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 190 | CLTensor _im2col_output; |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 191 | CLTensor _weights_reshaped; |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 192 | CLTensor _gemm_output; |
| 193 | CLTensor _tmp_output; |
| 194 | |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 195 | DataLayout _data_layout; |
| 196 | |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 197 | bool _append_bias; |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 198 | bool _skip_im2col; |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 199 | bool _is_quantized; |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 200 | bool _is_activationlayer_enabled; |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 201 | bool _is_prepared; |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 202 | }; |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 203 | } // namespace arm_compute |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 204 | #endif /* __ARM_COMPUTE_CLGEMMCONVOLUTIONLAYER_H__ */ |