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