blob: c985738a9ca1a610057eb6fed02f3a7e4eb6cfd2 [file] [log] [blame]
giuros0146a49a02019-04-01 13:50:22 +01001/*
Sheri Zhang6124ce62021-05-04 14:03:13 +01002 * Copyright (c) 2019-2021 Arm Limited.
giuros0146a49a02019-04-01 13:50:22 +01003 *
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 */
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +010024#ifndef ARM_COMPUTE_CLGEMMDECONVOLUTIONLAYER_H
25#define ARM_COMPUTE_CLGEMMDECONVOLUTIONLAYER_H
giuros0146a49a02019-04-01 13:50:22 +010026
giuros0146a49a02019-04-01 13:50:22 +010027#include "arm_compute/runtime/CL/CLTensor.h"
28#include "arm_compute/runtime/CL/functions/CLConvolutionLayer.h"
Manuel Bottinid87aded2021-07-16 10:23:31 +010029#include "arm_compute/runtime/CL/functions/CLGEMM.h"
30#include "arm_compute/runtime/CL/functions/CLGEMMLowpMatrixMultiplyCore.h"
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +010031#include "arm_compute/runtime/CL/functions/CLGEMMLowpOutputStage.h"
giuros0146a49a02019-04-01 13:50:22 +010032#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"
Georgios Pinitas26014cf2019-09-09 19:00:57 +010038#include "arm_compute/runtime/MemoryGroup.h"
giuros0146a49a02019-04-01 13:50:22 +010039
40#include <memory>
41
42namespace arm_compute
43{
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010044class CLDeconvolutionReshapeOutputKernel;
giuros0146a49a02019-04-01 13:50:22 +010045class 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
Sheri Zhang0cdbda52020-02-25 15:57:21 +000073 * -# @ref CLGEMMLowpOutputStage
giuros0146a49a02019-04-01 13:50:22 +010074 * -# @ref CLPermute
75 * -# @ref CLPermute
76 * -# @ref CLReshapeLayer
77 * -# @ref CLTranspose
78 * -# @ref CLDeconvolutionReshapeOutputKernel
79 * -# @ref CLSlice
80 */
81class CLGEMMDeconvolutionLayer : public IFunction
82{
83public:
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;
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010094 /** Default desctructor */
95 ~CLGEMMDeconvolutionLayer();
giuros0146a49a02019-04-01 13:50:22 +010096 /** Set the input, weights, biases and output tensors.
97 *
Sheri Zhang6124ce62021-05-04 14:03:13 +010098 * 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 *
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000109 * @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
giuros0146a49a02019-04-01 13:50:22 +0100111 * @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);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100117 /** 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);
giuros0146a49a02019-04-01 13:50:22 +0100128 /** Static function to check if given info will lead to a valid configuration of @ref CLDeconvolutionLayer
129 *
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000130 * @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
giuros0146a49a02019-04-01 13:50:22 +0100132 * @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
145private:
Georgios Pinitas26014cf2019-09-09 19:00:57 +0100146 MemoryGroup _memory_group;
giuros0146a49a02019-04-01 13:50:22 +0100147
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100148 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;
giuros0146a49a02019-04-01 13:50:22 +0100157
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
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100173#endif /* ARM_COMPUTE_CLGEMMDECONVOLUTIONLAYER_H */