blob: 34ab0707c2b987bdf80890e80ead8446d034d6c3 [file] [log] [blame]
Pablo Tellof5f34bb2017-08-22 13:34:13 +01001/*
Sheri Zhangac6499a2021-02-10 15:32:38 +00002 * Copyright (c) 2017-2021 Arm Limited.
Pablo Tellof5f34bb2017-08-22 13:34:13 +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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_NEDECONVOLUTIONLAYER_H
25#define ARM_COMPUTE_NEDECONVOLUTIONLAYER_H
Pablo Tellof5f34bb2017-08-22 13:34:13 +010026
Michalis Spyrou33a69902018-02-23 15:01:52 +000027#include "arm_compute/runtime/CPP/functions/CPPUpsample.h"
Georgios Pinitasced7a8d2018-02-01 16:31:33 +000028#include "arm_compute/runtime/NEON/functions/NEConvolutionLayer.h"
Pablo Tellof5f34bb2017-08-22 13:34:13 +010029#include "arm_compute/runtime/NEON/functions/NEDirectConvolutionLayer.h"
Luca Foschianifedefc32020-02-17 17:02:49 +000030#include "arm_compute/runtime/NEON/functions/NEReverse.h"
Pablo Tellof5f34bb2017-08-22 13:34:13 +010031
32#include "arm_compute/core/Types.h"
33#include "arm_compute/runtime/IFunction.h"
34#include "arm_compute/runtime/IMemoryManager.h"
35#include "arm_compute/runtime/MemoryGroup.h"
36#include "arm_compute/runtime/Tensor.h"
37
38#include <memory>
39
40namespace arm_compute
41{
42/** Function to run the deconvolution layer.
43 *
Michalis Spyrou780db4e2017-11-23 09:49:51 +000044 * Deconvolution Layer is the backward pass of Convolution Layer. First we transform the input depending on the stride and pad info and then perfrom a 1x1
45 * convolution pass. Input stride defines how many zeroes we should put between each element of the input, pad is the amount of padding and finaly a is a user
46 * specified value where a < stride - 1 that increases the padding top and right of the input image.
Pablo Tellof5f34bb2017-08-22 13:34:13 +010047 *
Michalis Spyrou780db4e2017-11-23 09:49:51 +000048 * The relation between input to output is as follows:
Michele Di Giorgioed5a4922018-09-13 16:22:01 +010049 * \f[
50 * width\_output = (width\_input - 1) \cdot stride\_x - 2 \cdot padding\_x + kernel\_x
51 * \f]
52 * \f[
53 * height\_output = (height\_input - 1) \cdot stride\_y - 2 \cdot padding\_y + kernel\_y
54 * \f]
Pablo Tellof5f34bb2017-08-22 13:34:13 +010055 *
56 * where
57 * width is the size of the first input dimension.
58 * height is the size of the second input dimension.
59 * width_output is the size of the first output dimension.
60 * height_output is the size of the second output dimension.
61 * kernel_x and kernel_y are the convolution sizes in x and y.
Michalis Spyrou780db4e2017-11-23 09:49:51 +000062 * stride_x and stride_y is the input stride of the first and second dimension.
Pablo Tellof5f34bb2017-08-22 13:34:13 +010063 *
Michele Di Giorgioed5a4922018-09-13 16:22:01 +010064 * The weights used by Deconvolution are supposed to be the same as the ones used for Convolution. Therefore, it will be necessary to use the weights in the
Luca Foschianifedefc32020-02-17 17:02:49 +000065 * reverse order to perform an actual convolution. This is achieved by using @ref NEReverse.
Pablo Tellof5f34bb2017-08-22 13:34:13 +010066 *
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +000067 * This function calls the following kernels/functions:
Michele Di Giorgioed5a4922018-09-13 16:22:01 +010068 *
69 * -# @ref CPPUpsample
70 * -# @ref NEConvolutionLayer
Luca Foschianifedefc32020-02-17 17:02:49 +000071 * -# @ref NEReverse
Pablo Tellof5f34bb2017-08-22 13:34:13 +010072 *
73 */
74class NEDeconvolutionLayer : public IFunction
75{
76public:
Michalis Spyrou1a569a32019-09-10 17:20:34 +010077 /** Constructor */
Pablo Tellof5f34bb2017-08-22 13:34:13 +010078 NEDeconvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
Michalis Spyrou780db4e2017-11-23 09:49:51 +000079
80 /** Prevent instances of this class from being copied (As this class contains pointers) */
81 NEDeconvolutionLayer(const NEDeconvolutionLayer &) = delete;
82 /** Prevent instances of this class from being copied (As this class contains pointers) */
83 NEDeconvolutionLayer &operator=(const NEDeconvolutionLayer &) = delete;
Michalis Spyrou770dfeb2020-11-04 18:55:34 +000084 /** Prevent instances of this class from being moved (As this class contains pointers) */
85 NEDeconvolutionLayer(NEDeconvolutionLayer &&) = delete;
86 /** Prevent instances of this class from being moved (As this class contains pointers) */
87 NEDeconvolutionLayer &operator=(NEDeconvolutionLayer &&) = delete;
Michalis Spyrou780db4e2017-11-23 09:49:51 +000088 /** Default destructor */
89 virtual ~NEDeconvolutionLayer() = default;
Pablo Tellof5f34bb2017-08-22 13:34:13 +010090
giuros01a69a88b2019-01-31 16:29:19 +000091 /** Set the input, weights, biases and output tensors.
92 *
Teresa Charlin62687422021-04-28 10:58:49 +010093 * Valid data layouts:
94 * - NHWC
95 * - NCHW
96 *
97 * Valid data type configurations:
98 * |src0 |src1 |src2 |dst |
99 * |:--------------|:------------------|:------|:--------------|
100 * |F16 |F16 |F16 |F16 |
101 * |F32 |F32 |F32 |F32 |
102 * |QASYMM8 |QASYMM8 |S32 |QASYMM8 |
103 * |QASYMM8 |QSYMM8_PER_CHANNEL |S32 |QASYMM8 |
104 * |QASYMM8_SIGNED |QASYMM8_SIGNED |S32 |QASYMM8_SIGNED |
105 * |QASYMM8_SIGNED |QSYMM8_PER_CHANNEL |S32 |QASYMM8_SIGNED |
106 *
Luca Foschianifedefc32020-02-17 17:02:49 +0000107 * @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: F32/F16/QASYMM8/QASYMM8_SIGNED.
Freddie Liardet9d061b02021-04-06 15:59:28 +0100108 * @param[in] weights The 4d weights with dimensions [width, height, IFM, OFM]. Data type supported: Same as @p input, also could be QSYMM8_PER_CHANNEL if input is QASYMM8/QASYMM8_SIGNED.
109 * @param[in] bias Optional, ignored if NULL. The biases have one dimension. Data type supported: Data types supported: S32 for QASYMM8/QASYMM8_SIGNED input, F32 for F32 input, F16 for F16 input.
giuros01a69a88b2019-01-31 16:29:19 +0000110 * @param[out] output Output tensor. The output has the same number of dimensions as the @p input.
111 * @param[in] info Contains padding and policies to be used in the deconvolution, this is decribed in @ref PadStrideInfo.
112 *
113 */
114 void configure(ITensor *input, const ITensor *weights, const ITensor *bias, ITensor *output, const PadStrideInfo &info);
115 /** Static function to check if given info will lead to a valid configuration of @ref NEDeconvolutionLayer
116 *
Luca Foschianifedefc32020-02-17 17:02:49 +0000117 * @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: F32/F16/QASYMM8/QASYMM8_SIGNED.
Freddie Liardet9d061b02021-04-06 15:59:28 +0100118 * @param[in] weights The 4d weights info with dimensions [width, height, IFM, OFM]. Data type supported: Same as @p input, also could be QSYMM8_PER_CHANNEL if input is QASYMM8/QASYMM8_SIGNED.
119 * @param[in] bias (Optional) The biases have one dimension. Data type supported: Data types supported: S32 for QASYMM8/QASYMM8_SIGNED input, F32 for F32 input, F16 for F16 input.
giuros01a69a88b2019-01-31 16:29:19 +0000120 * @param[in] output Output tensor info. The output has the same number of dimensions as the @p input.
121 * @param[in] info Contains padding and policies to be used in the deconvolution, this is decribed in @ref PadStrideInfo.
122 *
123 * @return a status
124 */
125 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *bias, const ITensorInfo *output, const PadStrideInfo &info);
126
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100127 // Inherited methods overridden:
128 void run() override;
Georgios Pinitas72219332018-06-05 14:56:06 +0100129 void prepare() override;
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100130
131private:
Luca Foschianifedefc32020-02-17 17:02:49 +0000132 MemoryGroup _memory_group;
133 NEConvolutionLayer _conv_f;
134 CPPUpsample _upsample_f;
135 NEReverse _flip_weights;
Luca Foschianifedefc32020-02-17 17:02:49 +0000136 Tensor _scaled_output;
137 Tensor _weights_flipped;
Luca Foschianifedefc32020-02-17 17:02:49 +0000138 Tensor _flip_axis;
Luca Foschianifedefc32020-02-17 17:02:49 +0000139 const ITensor *_original_weights;
140 ITensor *_input;
141 PadStrideInfo _info;
142 bool _is_prepared;
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100143};
144} // arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000145#endif /* ARM_COMPUTE_NEDECONVOLUTIONLAYER_H */