5fb102e478880c45556291e04cdf6a3966f5f6e8
[platform/core/ml/nnfw.git] / compute / ARMComputeEx / arm_compute / runtime / CL / functions / CLTransposeConvLayer.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_CLTRANSPOSECONVLAYER_H__
41 #define __ARM_COMPUTE_CLTRANSPOSECONVLAYER_H__
42
43 #include "arm_compute/runtime/CL/functions/CLDirectTransposeConvLayer.h"
44 #include "arm_compute/runtime/CL/functions/CLGEMMDeconvolutionLayer.h"
45 #include "arm_compute/runtime/IFunction.h"
46 #include "arm_compute/runtime/IMemoryManager.h"
47
48 #include <memory>
49
50 namespace arm_compute
51 {
52 /** Basic function to compute the deconvolution layer. This function calls the following OpenCL
53  * kernels/functions:
54  *
55  * -# @ref CLGEMMDeconvolutionLayer
56  * -# @ref CLDirectTransposeConvLayer
57  */
58 class CLTransposeConvLayer : public IFunction
59 {
60 public:
61   /** Default constructor */
62   CLTransposeConvLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
63
64   /** Set the input, weights, biases and output tensors.
65    *
66    * @param[in,out] input        Input tensor. 3 lower dimensions represent a single input, and an
67  * optional 4th dimension for batch of inputs. Data types supported: QASYMM8_SIGNED/QASYMM8/F16/F32.
68    * @param[in]     weights      The 4d weights with dimensions [width, height, IFM, OFM]. Data type
69  * supported: Same as @p input.
70    * @param[in]     bias         (Optional) The biases have one dimension. Data type supported: Same
71  * as @p input.
72    * @param[out]    output       Output tensor. The output has the same number of dimensions as the
73  * @p input.
74    * @param[in]     deconv_info  Contains padding and policies to be used in the deconvolution, this
75  * is described in @ref PadStrideInfo.
76  * @param[in] invalid_right  The number of zeros added to right edge of the output.
77  * @param[in] invalid_bottom  The number of zeros added to bottom edge of the output.
78    * @param[in]     weights_info (Optional) Weights information needed for @ref CLConvolutionLayer,
79  * specifies if the weights tensor has been reshaped with @ref CLWeightsReshapeKernel.
80    *
81    */
82   void configure(ICLTensor *input, ICLTensor *weights, const ICLTensor *bias, ICLTensor *output,
83                  const PadStrideInfo &deconv_info, unsigned int invalid_right,
84                  unsigned int invalid_bottom, const WeightsInfo &weights_info = WeightsInfo());
85   /** Set the input, weights, biases and output tensors.
86    *
87    * @param[in]     compile_context The compile context to be used.
88    * @param[in,out] input           Input tensor. 3 lower dimensions represent a single input, and
89  * an optional 4th dimension for batch of inputs. Data types supported:
90  * QASYMM8_SIGNED/QASYMM8/F16/F32.
91    * @param[in]     weights         The 4d weights with dimensions [width, height, IFM, OFM]. Data
92  * type supported: Same as @p input.
93    * @param[in]     bias            (Optional) The biases have one dimension. Data type supported:
94  * Same as @p input.
95    * @param[out]    output          Output tensor. The output has the same number of dimensions as
96  * the @p input.
97    * @param[in]     deconv_info     Contains padding and policies to be used in the deconvolution,
98  * this is described in @ref PadStrideInfo.
99  * @param[in] invalid_right  The number of zeros added to right edge of the output.
100  * @param[in] invalid_bottom  The number of zeros added to bottom edge of the output.
101    * @param[in]     weights_info    (Optional) Weights information needed for @ref
102  * CLConvolutionLayer, specifies if the weights tensor has been reshaped with @ref
103  * CLWeightsReshapeKernel.
104    *
105    */
106   void configure(const CLCompileContext &compile_context, ICLTensor *input, ICLTensor *weights,
107                  const ICLTensor *bias, ICLTensor *output, const PadStrideInfo &deconv_info,
108                  unsigned int invalid_right, unsigned int invalid_bottom,
109                  const WeightsInfo &weights_info = WeightsInfo());
110   /** Static function to check if given info will lead to a valid configuration of @ref
111  * CLTransposeConvLayer
112    *
113    * @param[in] input        Input tensor info. 3 lower dimensions represent a single input, and an
114  * optional 4th dimension for batch of inputs. Data types supported: QASYMM8_SIGNED/QASYMM8/F16/F32.
115    * @param[in] weights      The 4d weights info with dimensions [width, height, IFM, OFM]. Data
116  * type supported: Same as @p input.
117    * @param[in] bias         (Optional) The biases have one dimension. Data type supported: Same as
118  * @p input.
119    * @param[in] output       Output tensor info. The output has the same number of dimensions as the
120  * @p input.
121    * @param[in] deconv_info  Contains padding and policies to be used in the deconvolution, this is
122  * described in @ref PadStrideInfo.
123  * @param[in] invalid_right  The number of zeros added to right edge of the output.
124  * @param[in] invalid_bottom  The number of zeros added to bottom edge of the output.
125    * @param[in] weights_info (Optional) Weights information needed for @ref CLConvolutionLayer,
126  * specifies if the weights tensor has been reshaped with @ref CLWeightsReshapeKernel.
127    *
128    * @return a status
129    */
130   static Status validate(const ITensorInfo *input, const ITensorInfo *weights,
131                          const ITensorInfo *bias, ITensorInfo *output,
132                          const PadStrideInfo &deconv_info, unsigned int invalid_right,
133                          unsigned int invalid_bottom,
134                          const WeightsInfo &weights_info = WeightsInfo());
135
136   static DeconvolutionMethod
137   get_deconvolution_method(const ITensorInfo *input, const ITensorInfo *weights,
138                            const ITensorInfo *bias, ITensorInfo *output,
139                            const PadStrideInfo &deconv_info, unsigned int invalid_right,
140                            unsigned int invalid_bottom, const WeightsInfo &weights_info);
141   // Inherited methods overridden:
142   void run() override;
143   void prepare() override;
144
145 private:
146   std::shared_ptr<IMemoryManager> _memory_manager;
147   std::unique_ptr<IFunction> _function;
148 };
149 } // namespace arm_compute
150 #endif /* __ARM_COMPUTE_CLTRANSPOSECONVLAYER_H__ */