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