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