blob: 1fedeff4443bb0eb3823d7d43734e6626ceaf5da [file] [log] [blame]
giuros0146a49a02019-04-01 13:50:22 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2019-2020 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
27#include "arm_compute/core/CL/kernels/CLDeconvolutionReshapeOutputKernel.h"
giuros0146a49a02019-04-01 13:50:22 +010028#include "arm_compute/runtime/CL/CLTensor.h"
29#include "arm_compute/runtime/CL/functions/CLConvolutionLayer.h"
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +010030#include "arm_compute/runtime/CL/functions/CLGEMMLowpOutputStage.h"
giuros0146a49a02019-04-01 13:50:22 +010031#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"
Georgios Pinitas26014cf2019-09-09 19:00:57 +010037#include "arm_compute/runtime/MemoryGroup.h"
giuros0146a49a02019-04-01 13:50:22 +010038
39#include <memory>
40
41namespace arm_compute
42{
43class 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
Sheri Zhang0cdbda52020-02-25 15:57:21 +000071 * -# @ref CLGEMMLowpOutputStage
giuros0146a49a02019-04-01 13:50:22 +010072 * -# @ref CLPermute
73 * -# @ref CLPermute
74 * -# @ref CLReshapeLayer
75 * -# @ref CLTranspose
76 * -# @ref CLDeconvolutionReshapeOutputKernel
77 * -# @ref CLSlice
78 */
79class CLGEMMDeconvolutionLayer : public IFunction
80{
81public:
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 /** Set the input, weights, biases and output tensors.
93 *
Sheri Zhang0cdbda52020-02-25 15:57:21 +000094 * @param[in,out] input Input tensor. 3 lower dimensions represent a single input, and an optional 4th dimension for batch of inputs.
95 * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32. Data layout supported: NHWC
giuros0146a49a02019-04-01 13:50:22 +010096 * @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.
97 * @param[in] bias (Optional) The biases have one dimension. Data type supported: Same as @p input. Data layout supported: same as @p input.
98 * @param[out] output Output tensor. The output has the same number of dimensions as the @p input. Data layout supported: same as @p input.
99 * @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.
100 */
101 void configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const PadStrideInfo &deconv_info);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100102 /** Set the input, weights, biases and output tensors.
103 *
104 * @param[in] compile_context The compile context to be used.
105 * @param[in,out] input Input tensor. 3 lower dimensions represent a single input, and an optional 4th dimension for batch of inputs.
106 * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32. Data layout supported: NHWC
107 * @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.
108 * @param[in] bias (Optional) The biases have one dimension. Data type supported: Same as @p input. Data layout supported: same as @p input.
109 * @param[out] output Output tensor. The output has the same number of dimensions as the @p input. Data layout supported: same as @p input.
110 * @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.
111 */
112 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 +0100113 /** Static function to check if given info will lead to a valid configuration of @ref CLDeconvolutionLayer
114 *
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000115 * @param[in] input Input tensor info. 3 lower dimensions represent a single input, and an optional 4th dimension for batch of inputs.
116 * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32. Data layout supported: NHWC
giuros0146a49a02019-04-01 13:50:22 +0100117 * @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.
118 * @param[in] bias (Optional) The biases have one dimension. Data type supported: Same as @p input. Data layout supported: same as @p input.
119 * @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.
120 * @param[in] deconv_info Contains padding and policies to be used in the deconvolution, this is described in @ref PadStrideInfo.
121 *
122 * @return a status
123 */
124 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *bias, const ITensorInfo *output, const PadStrideInfo &deconv_info);
125
126 // Inherited methods overridden:
127 void run() override;
128 void prepare() override;
129
130private:
Georgios Pinitas26014cf2019-09-09 19:00:57 +0100131 MemoryGroup _memory_group;
giuros0146a49a02019-04-01 13:50:22 +0100132
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000133 CLGEMM _mm_gemm;
134 CLGEMMLowpMatrixMultiplyCore _mm_gemmlowp;
135 CLGEMMLowpOutputStage _gemmlowp_output_stage;
136 CLPermute _permute_input_to_nhwc;
137 CLPermute _permute_weights_to_nhwc;
138 CLReshapeLayer _reshape_weights;
139 CLTranspose _transpose_weights;
140 CLDeconvolutionReshapeOutputKernel _deconv_reshape;
141 CLSlice _slice_gemm;
giuros0146a49a02019-04-01 13:50:22 +0100142
143 CLTensor _gemmlowp_final;
144 CLTensor _reshaped_weights;
145 CLTensor _reshaped_weights_t;
146 CLTensor _permuted_input;
147 CLTensor _permuted_weights;
148 CLTensor _gemm_output;
149 CLTensor _slice_gemm_input;
150
151 const ICLTensor *_original_weights;
152 bool _is_prepared;
153 bool _padded_input;
154 bool _is_nchw;
155 bool _is_quantized;
156};
157} // namespace arm_compute
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100158#endif /* ARM_COMPUTE_CLGEMMDECONVOLUTIONLAYER_H */