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 */ 24 #ifndef ARM_COMPUTE_CL_GEMM_CONV2D_H 25 #define ARM_COMPUTE_CL_GEMM_CONV2D_H 26 27 #include "arm_compute/core/TensorInfo.h" 28 #include "arm_compute/core/Types.h" 29 #include "arm_compute/core/experimental/IPostOp.h" 30 #include "arm_compute/runtime/FunctionDescriptors.h" 31 #include "src/gpu/cl/ClCompileContext.h" 32 #include "src/gpu/cl/IClOperator.h" 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 */ 59 class ClGemmConv2d : public IClOperator 60 { 61 public: 62 /** Constructor */ 63 ClGemmConv2d(); 64 /** Prevent instances of this class from being copied (As this class contains pointers) */ 65 ClGemmConv2d(const ClGemmConv2d &) = delete; 66 /** Default move constructor */ 67 ClGemmConv2d(ClGemmConv2d &&) = default; 68 /** Prevent instances of this class from being copied (As this class contains pointers) */ 69 ClGemmConv2d &operator=(const ClGemmConv2d &) = delete; 70 /** Default move assignment operator */ 71 ClGemmConv2d &operator=(ClGemmConv2d &&) = default; 72 /**Default destructor */ 73 ~ClGemmConv2d(); 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 */ 104 void configure(const ClCompileContext &compile_context, ITensorInfo *src, ITensorInfo *weights, ITensorInfo *biases, ITensorInfo *dst, const Conv2dInfo &conv2d_info, 105 const WeightsInfo &weights_info = WeightsInfo()); 106 /** Static function to check if given info will lead to a valid configuration 107 * 108 * Similar to ClGemmConvolution::configure() 109 * 110 * @return a status 111 */ 112 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const Conv2dInfo &conv2d_info, 113 const WeightsInfo &weights_info = WeightsInfo()); 114 115 // Inherited methods overridden: 116 void run(ITensorPack &tensors) override; 117 void prepare(ITensorPack &constants) override; 118 experimental::MemoryRequirements workspace() const override; 119 120 private: 121 /** Configures the appropriate matrix multiply routine 122 * 123 * @param[in] compile_context The compile context to be used. 124 * @param[in] src Input tensor info. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32. 125 * @param[in] weights Weights tensor info. Data type supported: Same as @p input or QASYMM8/QSYMM8_PER_CHANNEL when @p input is QASYMM8 or 126 * QASYMM8_SIGNED/QSYMM8_PER_CHANNEL when @p input is QASYMM8_SIGNED. 127 * @param[in] biases Biases tensor info. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. 128 * Data type supported: Should match @p input data type, except for input of quantized type where biases should be of S32 type. 129 * @param[in, out] dst Output tensor info. Data types supported: same as @p input. 130 * @param[in] gemmlowp_output_stage GEMMLowp output stage info 131 * @param[in] gemm_3d_depth Depth of GEMM 3D 132 * @param[in] act_info Activation to apply after the matrix multiplication 133 */ 134 void configure_mm(const CLCompileContext &compile_context, const ITensorInfo *src, ITensorInfo *weights, ITensorInfo *biases, ITensorInfo *dst, 135 const GEMMLowpOutputStageInfo &gemmlowp_output_stage, 136 int gemm_3d_depth, const ActivationLayerInfo &act_info, const experimental::PostOpList<ITensorInfo *> &post_ops = experimental::PostOpList<ITensorInfo *> {}); 137 /** Static function to check if given info will lead to a valid configuration of @ref CLGEMMConvolutionLayer matrix multiply routines 138 * 139 * @param[in] src Input tensor info. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32. 140 * @param[in] weights Weights tensor info. Data type supported: Same as @p input or QASYMM8/QSYMM8_PER_CHANNEL when @p input is QASYMM8 or 141 * QASYMM8_SIGNED/QSYMM8_PER_CHANNEL when @p input is QASYMM8_SIGNED. 142 * @param[in] biases Biases tensor info. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. 143 * Data type supported: Should match @p input data type, except for input of quantized type where biases should be of S32 type. 144 * @param[in] dst Output tensor info. Data types supported: same as @p input. 145 * @param[in] gemmlowp_output_stage GEMMLowp output stage info 146 * @param[in] gemm_3d_depth Depth of GEMM 3D 147 * @param[in] skip_im2col Flag which specifies if im2col has to be skipped. i.e. 1x1 convolution with NHWC data layout. 148 * @param[in] act_info Activation to apply after the matrix multiplication 149 * 150 * @return a status 151 */ 152 static Status validate_mm(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst, const GEMMLowpOutputStageInfo &gemmlowp_output_stage, 153 int gemm_3d_depth, bool skip_im2col, const ActivationLayerInfo &act_info, const experimental::PostOpList<ITensorInfo *> &post_ops = experimental::PostOpList<ITensorInfo *> {}); 154 155 enum AuxTensorIdx 156 { 157 // ClGemmLowpMatrixMultiplyCore has up to 7 internal tensors 158 Im2ColOutput = 8, 159 WeightsReshaped, 160 GemmOutput, 161 Count 162 }; 163 164 std::unique_ptr<kernels::ClWeightsReshapeKernel> _weights_reshape_kernel; 165 std::unique_ptr<kernels::ClIm2ColKernel> _im2col_kernel; 166 std::unique_ptr<ClGemm> _mm_gemm; 167 std::unique_ptr<ClGemmLowpMatrixMultiplyCore> _mm_gemmlowp; 168 std::unique_ptr<opencl::kernels::ClCol2ImKernel> _col2im_kernel; 169 std::unique_ptr<kernels::ClActivationKernel> _activation_kernel; 170 171 TensorInfo _im2col_output; 172 TensorInfo _weights_reshaped; 173 TensorInfo _gemm_output; 174 175 bool _skip_im2col; 176 bool _skip_col2im; 177 bool _is_quantized; 178 bool _fuse_activation; 179 bool _append_bias; 180 bool _is_prepared; 181 bool _use_post_ops; 182 183 experimental::MemoryRequirements _aux_mem; 184 }; 185 } // namespace opencl 186 } // namespace arm_compute 187 #endif /* ARM_COMPUTE_CL_GEMM_CONV2D_H */ 188