Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021 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 | */ |
Georgios Pinitas | 1988463 | 2021-08-16 12:38:54 +0100 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_CL_GEMM_CONV2D_H |
| 25 | #define ARM_COMPUTE_CL_GEMM_CONV2D_H |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 26 | |
| 27 | #include "arm_compute/core/TensorInfo.h" |
| 28 | #include "arm_compute/core/Types.h" |
| 29 | #include "arm_compute/runtime/FunctionDescriptors.h" |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame^] | 30 | #include "src/gpu/cl/ClCompileContext.h" |
| 31 | #include "src/gpu/cl/IClOperator.h" |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 32 | |
| 33 | #include <memory> |
| 34 | |
| 35 | namespace arm_compute |
| 36 | { |
| 37 | namespace opencl |
| 38 | { |
| 39 | class ClGemm; |
| 40 | class ClGemmLowpMatrixMultiplyCore; |
| 41 | namespace kernels |
| 42 | { |
| 43 | class ClIm2ColKernel; |
| 44 | class ClCol2ImKernel; |
| 45 | class ClWeightsReshapeKernel; |
| 46 | class ClActivationKernel; |
| 47 | } // namespace kernels |
| 48 | |
| 49 | /** Basic function to compute the convolution layer. This function calls the following OpenCL kernels/functions: |
| 50 | * |
| 51 | * -# @ref opencl::kernels::ClIm2ColKernel |
| 52 | * -# @ref ClGemm (if the data type is FP32 or FP16) |
| 53 | * -# @ref CLGEMMLowpMatrixMultiplyCore (if the data type is QASYMM8/QASYMM8_SIGNED) |
| 54 | * -# @ref ClGemmLowpOutputStage with QUANTIZE_DOWN_FIXEDPOINT type of quantization (if the data type is QASYMM8/QASYMM8_SIGNED) |
| 55 | * -# @ref opencl::kernels::ClCol2ImKernel (if NCHW data layout) |
| 56 | * -# @ref opencl::kernels::ClActivationKernel |
| 57 | */ |
Georgios Pinitas | 1988463 | 2021-08-16 12:38:54 +0100 | [diff] [blame] | 58 | class ClGemmConv2d : public IClOperator |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 59 | { |
| 60 | public: |
| 61 | /** Constructor */ |
Georgios Pinitas | 1988463 | 2021-08-16 12:38:54 +0100 | [diff] [blame] | 62 | ClGemmConv2d(); |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 63 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
Georgios Pinitas | 1988463 | 2021-08-16 12:38:54 +0100 | [diff] [blame] | 64 | ClGemmConv2d(const ClGemmConv2d &) = delete; |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 65 | /** Default move constructor */ |
Georgios Pinitas | 1988463 | 2021-08-16 12:38:54 +0100 | [diff] [blame] | 66 | ClGemmConv2d(ClGemmConv2d &&) = default; |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 67 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
Georgios Pinitas | 1988463 | 2021-08-16 12:38:54 +0100 | [diff] [blame] | 68 | ClGemmConv2d &operator=(const ClGemmConv2d &) = delete; |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 69 | /** Default move assignment operator */ |
Georgios Pinitas | 1988463 | 2021-08-16 12:38:54 +0100 | [diff] [blame] | 70 | ClGemmConv2d &operator=(ClGemmConv2d &&) = default; |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 71 | /**Default destructor */ |
Georgios Pinitas | 1988463 | 2021-08-16 12:38:54 +0100 | [diff] [blame] | 72 | ~ClGemmConv2d(); |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 73 | /** Set the input and output tensors. |
| 74 | * |
| 75 | * Valid data layouts: |
| 76 | * - NHWC |
| 77 | * - NCHW |
| 78 | * |
| 79 | * Valid data type configurations: |
| 80 | * |src0 |src1 |src2 |dst | |
| 81 | * |:--------------|:------------------|:--------|:--------------| |
| 82 | * |F16 |F16 |F16 |F16 | |
| 83 | * |F32 |F32 |F32 |F32 | |
| 84 | * |QASYMM8 |QASYMM8 |S32 |QASYMM8 | |
| 85 | * |QASYMM8 |QSYMM8_PER_CHANNEL |S32 |QASYMM8 | |
| 86 | * |QASYMM8_SIGNED |QASYMM8_SIGNED |S32 |QASYMM8_SIGNED | |
| 87 | * |QASYMM8_SIGNED |QSYMM8_PER_CHANNEL |S32 |QASYMM8_SIGNED | |
| 88 | * |
| 89 | * @param[in] compile_context The compile context to be used. |
| 90 | * @param[in] src Source tensor info. 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: QASYMM8/QASYMM8_SIGNED/F16/F32. |
| 93 | * @param[in] weights Weights tensor info. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. |
| 94 | * Data type supported: Same as @p input or QASYMM8/QSYMM8_PER_CHANNEL when @p input is QASYMM8 or QASYMM8_SIGNED/QSYMM8_PER_CHANNEL when @p input is QASYMM8_SIGNED. |
| 95 | * @param[in] biases Biases tensor info. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. |
| 96 | * Data type supported: Should match @p input data type, except for input of quantized type where biases should be of S32 type. |
| 97 | * @param[out] dst Destination tensor info. 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] conv2d_info Contains convolution 2d info described in @ref Conv2dInfo. |
| 100 | * @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 |
| 101 | * tensor has also been transposed with CLGEMMReshapeRHSMatrixKernel. Data type supported: Same as @p input. |
| 102 | */ |
| 103 | void configure(const ClCompileContext &compile_context, ITensorInfo *src, ITensorInfo *weights, ITensorInfo *biases, ITensorInfo *dst, const Conv2dInfo &conv2d_info, |
| 104 | const WeightsInfo &weights_info = WeightsInfo()); |
| 105 | /** Static function to check if given info will lead to a valid configuration |
| 106 | * |
| 107 | * Similar to ClGemmConvolution::configure() |
| 108 | * |
| 109 | * @return a status |
| 110 | */ |
| 111 | static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const Conv2dInfo &conv2d_info, |
| 112 | const WeightsInfo &weights_info = WeightsInfo()); |
| 113 | |
| 114 | // Inherited methods overridden: |
| 115 | void run(ITensorPack &tensors) override; |
| 116 | void prepare(ITensorPack &constants) override; |
| 117 | experimental::MemoryRequirements workspace() const override; |
| 118 | |
| 119 | private: |
| 120 | /** Configures the appropriate matrix multiply routine |
| 121 | * |
| 122 | * @param[in] compile_context The compile context to be used. |
| 123 | * @param[in] src Input tensor info. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32. |
| 124 | * @param[in] weights Weights tensor info. Data type supported: Same as @p input or QASYMM8/QSYMM8_PER_CHANNEL when @p input is QASYMM8 or |
| 125 | * QASYMM8_SIGNED/QSYMM8_PER_CHANNEL when @p input is QASYMM8_SIGNED. |
| 126 | * @param[in] biases Biases tensor info. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. |
| 127 | * Data type supported: Should match @p input data type, except for input of quantized type where biases should be of S32 type. |
| 128 | * @param[in, out] dst Output tensor info. Data types supported: same as @p input. |
| 129 | * @param[in] gemmlowp_output_stage GEMMLowp output stage info |
| 130 | * @param[in] gemm_3d_depth Depth of GEMM 3D |
| 131 | * @param[in] act_info Activation to apply after the matrix multiplication |
| 132 | */ |
| 133 | void configure_mm(const CLCompileContext &compile_context, const ITensorInfo *src, ITensorInfo *weights, ITensorInfo *biases, ITensorInfo *dst, |
| 134 | const GEMMLowpOutputStageInfo &gemmlowp_output_stage, |
| 135 | int gemm_3d_depth, const ActivationLayerInfo &act_info); |
| 136 | /** Static function to check if given info will lead to a valid configuration of @ref CLGEMMConvolutionLayer matrix multiply routines |
| 137 | * |
| 138 | * @param[in] src Input tensor info. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32. |
| 139 | * @param[in] weights Weights tensor info. Data type supported: Same as @p input or QASYMM8/QSYMM8_PER_CHANNEL when @p input is QASYMM8 or |
| 140 | * QASYMM8_SIGNED/QSYMM8_PER_CHANNEL when @p input is QASYMM8_SIGNED. |
| 141 | * @param[in] biases Biases tensor info. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. |
| 142 | * Data type supported: Should match @p input data type, except for input of quantized type where biases should be of S32 type. |
| 143 | * @param[in] dst Output tensor info. Data types supported: same as @p input. |
| 144 | * @param[in] gemmlowp_output_stage GEMMLowp output stage info |
| 145 | * @param[in] gemm_3d_depth Depth of GEMM 3D |
| 146 | * @param[in] skip_im2col Flag which specifies if im2col has to be skipped. i.e. 1x1 convolution with NHWC data layout. |
| 147 | * @param[in] act_info Activation to apply after the matrix multiplication |
| 148 | * |
| 149 | * @return a status |
| 150 | */ |
| 151 | static Status validate_mm(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst, const GEMMLowpOutputStageInfo &gemmlowp_output_stage, |
| 152 | int gemm_3d_depth, bool skip_im2col, const ActivationLayerInfo &act_info); |
| 153 | |
| 154 | enum AuxTensorIdx |
| 155 | { |
| 156 | // ClGemmLowpMatrixMultiplyCore has up to 7 internal tensors |
| 157 | Im2ColOutput = 8, |
| 158 | WeightsReshaped, |
| 159 | GemmOutput, |
| 160 | Count |
| 161 | }; |
| 162 | |
| 163 | std::unique_ptr<kernels::ClWeightsReshapeKernel> _weights_reshape_kernel; |
| 164 | std::unique_ptr<kernels::ClIm2ColKernel> _im2col_kernel; |
| 165 | std::unique_ptr<ClGemm> _mm_gemm; |
| 166 | std::unique_ptr<ClGemmLowpMatrixMultiplyCore> _mm_gemmlowp; |
| 167 | std::unique_ptr<opencl::kernels::ClCol2ImKernel> _col2im_kernel; |
| 168 | std::unique_ptr<kernels::ClActivationKernel> _activation_kernel; |
| 169 | |
| 170 | TensorInfo _im2col_output; |
| 171 | TensorInfo _weights_reshaped; |
| 172 | TensorInfo _gemm_output; |
| 173 | |
| 174 | bool _skip_im2col; |
| 175 | bool _skip_col2im; |
| 176 | bool _is_quantized; |
| 177 | bool _fuse_activation; |
| 178 | bool _append_bias; |
| 179 | bool _is_prepared; |
| 180 | |
| 181 | experimental::MemoryRequirements _aux_mem; |
| 182 | }; |
| 183 | } // namespace opencl |
| 184 | } // namespace arm_compute |
Georgios Pinitas | 1988463 | 2021-08-16 12:38:54 +0100 | [diff] [blame] | 185 | #endif /* ARM_COMPUTE_CL_GEMM_CONV2D_H */ |