1 /* 2 * Copyright (c) 2019-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_CLGEMMDECONVOLUTIONLAYER_H 25 #define ARM_COMPUTE_CLGEMMDECONVOLUTIONLAYER_H 26 27 #include "arm_compute/runtime/CL/CLTensor.h" 28 #include "arm_compute/runtime/CL/functions/CLConvolutionLayer.h" 29 #include "arm_compute/runtime/CL/functions/CLGEMM.h" 30 #include "arm_compute/runtime/CL/functions/CLGEMMLowpMatrixMultiplyCore.h" 31 #include "arm_compute/runtime/CL/functions/CLGEMMLowpOutputStage.h" 32 #include "arm_compute/runtime/CL/functions/CLPermute.h" 33 #include "arm_compute/runtime/CL/functions/CLReshapeLayer.h" 34 #include "arm_compute/runtime/CL/functions/CLSlice.h" 35 #include "arm_compute/runtime/CL/functions/CLTranspose.h" 36 #include "arm_compute/runtime/IFunction.h" 37 #include "arm_compute/runtime/IMemoryManager.h" 38 #include "arm_compute/runtime/MemoryGroup.h" 39 40 #include <memory> 41 42 namespace arm_compute 43 { 44 class CLDeconvolutionReshapeOutputKernel; 45 class ICLTensor; 46 /** Function to run the deconvolution layer through a call to GEMM. 47 * 48 * Deconvolution Layer is the backward pass of Convolution Layer. First we transform the input depending on the stride and pad info and then perform a 1x1 49 * convolution pass. Input stride defines how many zeroes we should put between each element of the input, pad is the amount of padding and finally a is a user 50 * specified value where a < stride - 1, that increases the padding top and right of the input image. 51 * 52 * The relation between input to output is as follows: 53 * \f[ 54 * width\_output = (width\_input - 1) \cdot stride\_x - 2 \cdot padding\_x + kernel\_x 55 * \f] 56 * \f[ 57 * height\_output = (height\_input - 1) \cdot stride\_y - 2 \cdot padding\_y + kernel\_y 58 * \f] 59 * 60 * where: 61 * width_input is the size of the first input dimension. 62 * height_input is the size of the second input dimension. 63 * width_output is the size of the first output dimension. 64 * height_output is the size of the second output dimension. 65 * kernel_x and kernel_y are the convolution sizes in x and y. 66 * stride_x and stride_y is the input stride of the first and second dimension. 67 * 68 * The weights used by Deconvolution are supposed to be the same as the ones used for Convolution. 69 * 70 * This function calls the following OpenCL kernels/functions: 71 * 72 * -# @ref CLGEMMLowpMatrixMultiplyCore 73 * -# @ref CLGEMMLowpOutputStage 74 * -# @ref CLPermute 75 * -# @ref CLPermute 76 * -# @ref CLReshapeLayer 77 * -# @ref CLTranspose 78 * -# @ref CLDeconvolutionReshapeOutputKernel 79 * -# @ref CLSlice 80 */ 81 class CLGEMMDeconvolutionLayer : public IFunction 82 { 83 public: 84 /** Constructor */ 85 CLGEMMDeconvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr); 86 /** Prevent instances of this class from being copied (As this class contains pointers) */ 87 CLGEMMDeconvolutionLayer(const CLGEMMDeconvolutionLayer &) = delete; 88 /** Default move constructor */ 89 CLGEMMDeconvolutionLayer(CLGEMMDeconvolutionLayer &&) = default; 90 /** Prevent instances of this class from being copied (As this class contains pointers) */ 91 CLGEMMDeconvolutionLayer &operator=(const CLGEMMDeconvolutionLayer &) = delete; 92 /** Default move assignment operator */ 93 CLGEMMDeconvolutionLayer &operator=(CLGEMMDeconvolutionLayer &&) = default; 94 /** Default desctructor */ 95 ~CLGEMMDeconvolutionLayer(); 96 /** Set the input, weights, biases and output tensors. 97 * 98 * Valid data layouts: 99 * - NHWC 100 * 101 * Valid data type configurations: 102 * |src0 |src1 |src2 |dst | 103 * |:--------------|:------------------|:--------|:--------------| 104 * |F16 |F16 |F16 |F16 | 105 * |F32 |F32 |F32 |F32 | 106 * |QASYMM8 |QASYMM8 |S32 |QASYMM8 | 107 * |QASYMM8_SIGNED |QASYMM8_SIGNED |S32 |QASYMM8_SIGNED | 108 * 109 * @param[in,out] input Input tensor. 3 lower dimensions represent a single input, and an optional 4th dimension for batch of inputs. 110 * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32. Data layout supported: NHWC 111 * @param[in] weights The 4d weights with dimensions [width, height, IFM, OFM]. Data type supported: Same as @p input. Data layout supported: same as @p input. 112 * @param[in] bias (Optional) The biases have one dimension. Data type supported: Same as @p input. Data layout supported: same as @p input. 113 * @param[out] output Output tensor. The output has the same number of dimensions as the @p input. Data layout supported: same as @p input. 114 * @param[in] deconv_info Contains padding and policies to be used in the deconvolution, this is described in @ref PadStrideInfo. This function supports only stride_x = weights.width && stride_y = weights.height. Moreover, padding is not supported. 115 */ 116 void configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const PadStrideInfo &deconv_info); 117 /** Set the input, weights, biases and output tensors. 118 * 119 * @param[in] compile_context The compile context to be used. 120 * @param[in,out] input Input tensor. 3 lower dimensions represent a single input, and an optional 4th dimension for batch of inputs. 121 * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32. Data layout supported: NHWC 122 * @param[in] weights The 4d weights with dimensions [width, height, IFM, OFM]. Data type supported: Same as @p input. Data layout supported: same as @p input. 123 * @param[in] bias (Optional) The biases have one dimension. Data type supported: Same as @p input. Data layout supported: same as @p input. 124 * @param[out] output Output tensor. The output has the same number of dimensions as the @p input. Data layout supported: same as @p input. 125 * @param[in] deconv_info Contains padding and policies to be used in the deconvolution, this is described in @ref PadStrideInfo. This function supports only stride_x = weights.width && stride_y = weights.height. Moreover, padding is not supported. 126 */ 127 void configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const PadStrideInfo &deconv_info); 128 /** Static function to check if given info will lead to a valid configuration of @ref CLDeconvolutionLayer 129 * 130 * @param[in] input Input tensor info. 3 lower dimensions represent a single input, and an optional 4th dimension for batch of inputs. 131 * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32. Data layout supported: NHWC 132 * @param[in] weights The 4d weights info with dimensions [width, height, IFM, OFM]. Data type supported: Same as @p input. Data layout supported: same as @p input. 133 * @param[in] bias (Optional) The biases have one dimension. Data type supported: Same as @p input. Data layout supported: same as @p input. 134 * @param[in] output Output tensor info. The output has the same number of dimensions as the @p input. Data layout supported: same as @p input. 135 * @param[in] deconv_info Contains padding and policies to be used in the deconvolution, this is described in @ref PadStrideInfo. 136 * 137 * @return a status 138 */ 139 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *bias, const ITensorInfo *output, const PadStrideInfo &deconv_info); 140 141 // Inherited methods overridden: 142 void run() override; 143 void prepare() override; 144 145 private: 146 MemoryGroup _memory_group; 147 148 CLGEMM _mm_gemm; 149 CLGEMMLowpMatrixMultiplyCore _mm_gemmlowp; 150 CLGEMMLowpOutputStage _gemmlowp_output_stage; 151 CLPermute _permute_input_to_nhwc; 152 CLPermute _permute_weights_to_nhwc; 153 CLReshapeLayer _reshape_weights; 154 CLTranspose _transpose_weights; 155 std::unique_ptr<CLDeconvolutionReshapeOutputKernel> _deconv_reshape; 156 CLSlice _slice_gemm; 157 158 CLTensor _gemmlowp_final; 159 CLTensor _reshaped_weights; 160 CLTensor _reshaped_weights_t; 161 CLTensor _permuted_input; 162 CLTensor _permuted_weights; 163 CLTensor _gemm_output; 164 CLTensor _slice_gemm_input; 165 166 const ICLTensor *_original_weights; 167 bool _is_prepared; 168 bool _padded_input; 169 bool _is_nchw; 170 bool _is_quantized; 171 }; 172 } // namespace arm_compute 173 #endif /* ARM_COMPUTE_CLGEMMDECONVOLUTIONLAYER_H */ 174