Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / compute / ARMComputeEx / arm_compute / runtime / NEON / functions / NETransposeConvLayer.h
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /*
18  * Copyright (c) 2017-2020 ARM Limited.
19  *
20  * SPDX-License-Identifier: MIT
21  *
22  * Permission is hereby granted, free of charge, to any person obtaining a copy
23  * of this software and associated documentation files (the "Software"), to
24  * deal in the Software without restriction, including without limitation the
25  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
26  * sell copies of the Software, and to permit persons to whom the Software is
27  * furnished to do so, subject to the following conditions:
28  *
29  * The above copyright notice and this permission notice shall be included in all
30  * copies or substantial portions of the Software.
31  *
32  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38  * SOFTWARE.
39  */
40 #ifndef __ARM_COMPUTE_NETRANSPOSECONVLAYER_H__
41 #define __ARM_COMPUTE_NETRANSPOSECONVLAYER_H__
42
43 #include "arm_compute/runtime/CPP/functions/CPPUpsample.h"
44 #include "arm_compute/runtime/NEON/functions/NEConvolutionLayer.h"
45 #include "arm_compute/runtime/NEON/functions/NEDirectConvolutionLayer.h"
46 #include "arm_compute/runtime/NEON/functions/NEReverse.h"
47
48 #include "arm_compute/core/Types.h"
49 #include "arm_compute/runtime/IFunction.h"
50 #include "arm_compute/runtime/IMemoryManager.h"
51 #include "arm_compute/runtime/MemoryGroup.h"
52 #include "arm_compute/runtime/Tensor.h"
53
54 #include <memory>
55
56 namespace arm_compute
57 {
58 /** Function to run the deconvolution layer.
59  *
60  * Deconvolution Layer is the backward pass of Convolution Layer. First we transform the input
61  * depending on the stride and pad info and then perfrom a 1x1
62  * convolution pass. Input stride defines how many zeroes we should put between each element of the
63  * input, pad is the amount of padding and finaly a is a user
64  * specified value where a < stride - 1 that increases the padding top and right of the input image.
65  *
66  *  The relation between input to output is as follows:
67  *  \f[
68  *       width\_output = (width\_input - 1) \cdot stride\_x - 2 \cdot padding\_x + kernel\_x
69  *  \f]
70  *  \f[
71  *       height\_output = (height\_input - 1) \cdot stride\_y - 2 \cdot padding\_y + kernel\_y
72  *  \f]
73  *
74  *  where
75  *      width is the size of the first input dimension.
76  *      height is the size of the second input dimension.
77  *      width_output is the size of the first output dimension.
78  *      height_output is the size of the second output dimension.
79  *      kernel_x and kernel_y are the convolution sizes in x and y.
80  *      stride_x and stride_y is the input stride of the first and second dimension.
81  *
82  * The weights used by Deconvolution are supposed to be the same as the ones used for Convolution.
83  * Therefore, it will be necessary to use the weights in the
84  * reverse order to perform an actual convolution. This is achieved by using @ref NEReverse.
85  *
86  * This function calls the following NEON kernels/functions:
87  *
88  * -# @ref CPPUpsampleEx
89  * -# @ref NEConvolutionLayer
90  * -# @ref NEPermute
91  * -# @ref NEReverse
92  *
93  */
94 class NETransposeConvLayer : public IFunction
95 {
96 public:
97   /** Constructor */
98   NETransposeConvLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
99
100   /** Prevent instances of this class from being copied (As this class contains pointers) */
101   NETransposeConvLayer(const NETransposeConvLayer &) = delete;
102   /** Prevent instances of this class from being copied (As this class contains pointers) */
103   NETransposeConvLayer &operator=(const NETransposeConvLayer &) = delete;
104   /** Allow instances of this class to be moved */
105   NETransposeConvLayer(NETransposeConvLayer &&) = default;
106   /** Allow instances of this class to be moved */
107   NETransposeConvLayer &operator=(NETransposeConvLayer &&) = default;
108   /** Default destructor */
109   virtual ~NETransposeConvLayer() = default;
110
111   /** Set the input, weights, biases and output tensors.
112    *
113    * @param[in,out] input           Input tensor. 3 lower dimensions represent a single input,
114    *                                and an optional 4th dimension for batch of inputs.
115    *                                Data types supported: F32/F16/QASYMM8/QASYMM8_SIGNED.
116    * @param[in]     weights         The 4d weights with dimensions [width, height, IFM, OFM].
117    *                                Data type supported: Same as @p input.
118    * @param[in]     bias            Optional, ignored if NULL. The biases have one dimension.
119    *                                Data type supported: Data types supported: S32 for QASYMM8 and
120    * QASYMM8_SIGNED input, F32 for F32 input, F16 for F16 input.
121    * @param[out]    output          Output tensor. The output has the same number of dimensions as
122    *                                the @p input.
123    * @param[in]     info            Contains padding and policies to be used in the deconvolution,
124    *                                this is decribed in @ref PadStrideInfo.
125    * @param[in]     invalid_right   The number of zeros added to right edge of the output.
126    * @param[in]     invalid_bottom  The number of zeros added to bottom edge of the output.
127    *
128    */
129   void configure(ITensor *input, const ITensor *weights, const ITensor *bias, ITensor *output,
130                  const PadStrideInfo &info, unsigned int invalid_right,
131                  unsigned int invalid_bottom);
132   /** Static function to check if given info will lead to a valid configuration of @ref
133    * NETransposeConvLayer
134    *
135    * @param[in] input           Input tensor info. 3 lower dimensions represent a single input,
136    *                            and an optional 4th dimension for batch of inputs.
137    *                            Data types supported: F32/F16/QASYMM8/QASYMM8_SIGNED.
138    * @param[in] weights         The 4d weights info with dimensions [width, height, IFM, OFM].
139    *                            Data type supported: Same as @p input.
140    * @param[in] bias            (Optional) The biases have one dimension.
141    *                            Data types supported: S32 for QASYMM8 and QASYMM8_SIGNED input,
142    *                                                  F32 for F32 input, F16 for F16 input.
143    * @param[in] output          Output tensor info. The output has the same number of dimensions as
144    *                            the @p input.
145    * @param[in] info            Contains padding and policies to be used in the deconvolution,
146    *                            this is decribed in @ref PadStrideInfo.
147    * @param[in] innvalid_right  The number of zeros added to right edge of the output.
148    * @param[in] invalid_bottom  The number of zeros added to bottom edge of the output.
149    *
150    * @return a status
151    */
152   static Status validate(const ITensorInfo *input, const ITensorInfo *weights,
153                          const ITensorInfo *bias, const ITensorInfo *output,
154                          const PadStrideInfo &info, unsigned int invalid_right,
155                          unsigned int invalid_bottom);
156
157   // Inherited methods overridden:
158   void run() override;
159   void prepare() override;
160
161 private:
162   MemoryGroup _memory_group;
163   NEConvolutionLayer _conv_f;
164   CPPUpsample _upsample_f;
165   NEReverse _flip_weights;
166   Tensor _scaled_output;
167   Tensor _weights_flipped;
168   Tensor _flip_axis;
169   const ITensor *_original_weights;
170   ITensor *_input;
171   PadStrideInfo _info;
172   bool _is_prepared;
173 };
174 } // arm_compute
175 #endif /* __ARM_COMPUTE_NETRANSPOSECONVLAYER_H__ */