Imported Upstream version 1.8.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, and an
114  * optional 4th dimension for batch of inputs. Data types supported: F32/F16/QASYMM8/QASYMM8_SIGNED.
115    * @param[in]     weights The 4d weights with dimensions [width, height, IFM, OFM]. Data type
116  * supported: Same as @p input.
117    * @param[in]     bias    Optional, ignored if NULL. The biases have one dimension. Data type
118  * supported: Data types supported: S32 for QASYMM8 and QASYMM8_SIGNED input, F32 for F32 input, F16
119  * for F16 input.
120    * @param[out]    output  Output tensor. The output has the same number of dimensions as the @p
121  * input.
122    * @param[in]     info    Contains padding and policies to be used in the deconvolution, this is
123  * decribed in @ref PadStrideInfo.
124  * @param[in]     invalid_right  The number of zeros added to right edge of the output.
125  * @param[in]     invalid_bottom The number of zeros added to bottom edge of the output.
126    *
127    */
128   void configure(ITensor *input, const ITensor *weights, const ITensor *bias, ITensor *output,
129                  const PadStrideInfo &info, unsigned int invalid_right,
130                  unsigned int invalid_bottom);
131   /** Static function to check if given info will lead to a valid configuration of @ref
132  * NETransposeConvLayer
133    *
134    * @param[in] input   Input tensor info. 3 lower dimensions represent a single input, and an
135  * optional 4th dimension for batch of inputs. Data types supported: F32/F16/QASYMM8/QASYMM8_SIGNED.
136    * @param[in] weights The 4d weights info with dimensions [width, height, IFM, OFM]. Data type
137  * supported: Same as @p input.
138    * @param[in] bias    (Optional) The biases have one dimension. Data type supported: Data types
139  * supported: S32 for QASYMM8 and QASYMM8_SIGNED input, F32 for F32 input, F16 for F16 input.
140    * @param[in] output  Output tensor info. The output has the same number of dimensions as the @p
141  * input.
142    * @param[in] info    Contains padding and policies to be used in the deconvolution, this is
143  * decribed in @ref PadStrideInfo.
144  * @param[in] innvalid_right  The number of zeros added to right edge of the output.
145  * @param[in] invalid_bottom  The number of zeros added to bottom edge of the output.
146    *
147    * @return a status
148    */
149   static Status validate(const ITensorInfo *input, const ITensorInfo *weights,
150                          const ITensorInfo *bias, const ITensorInfo *output,
151                          const PadStrideInfo &info, unsigned int invalid_right,
152                          unsigned int invalid_bottom);
153
154   // Inherited methods overridden:
155   void run() override;
156   void prepare() override;
157
158 private:
159   MemoryGroup _memory_group;
160   NEConvolutionLayer _conv_f;
161   CPPUpsample _upsample_f;
162   NEReverse _flip_weights;
163   Tensor _scaled_output;
164   Tensor _weights_flipped;
165   Tensor _flip_axis;
166   const ITensor *_original_weights;
167   ITensor *_input;
168   PadStrideInfo _info;
169   bool _is_prepared;
170 };
171 } // arm_compute
172 #endif /* __ARM_COMPUTE_NETRANSPOSECONVLAYER_H__ */