blob: 2963156d11334eb8a64778b82b01fbebb0fece1b [file] [log] [blame]
Michalis Spyrou780db4e2017-11-23 09:49:51 +00001/*
giuros01a69a88b2019-01-31 16:29:19 +00002 * Copyright (c) 2017-2019 ARM Limited.
Michalis Spyrou780db4e2017-11-23 09:49:51 +00003 *
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 */
24#ifndef __ARM_COMPUTE_CLDECONVOLUTIONLAYER_H__
25#define __ARM_COMPUTE_CLDECONVOLUTIONLAYER_H__
26
Georgios Pinitasced7a8d2018-02-01 16:31:33 +000027#include "arm_compute/runtime/CL/functions/CLConvolutionLayer.h"
Michalis Spyrou780db4e2017-11-23 09:49:51 +000028#include "arm_compute/runtime/CL/functions/CLDeconvolutionLayerUpsample.h"
Michalis Spyrou780db4e2017-11-23 09:49:51 +000029
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +010030#include "arm_compute/core/CPP/kernels/CPPFlipWeightsKernel.h"
31
Michalis Spyrou780db4e2017-11-23 09:49:51 +000032#include "arm_compute/runtime/CL/CLMemoryGroup.h"
33#include "arm_compute/runtime/CL/CLTensor.h"
34#include "arm_compute/runtime/IFunction.h"
35#include "arm_compute/runtime/IMemoryManager.h"
36
37#include <memory>
38
39namespace arm_compute
40{
41class ICLTensor;
42/** Function to run the deconvolution layer.
43 *
44 * 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
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 finally a is a user
46 * specified value where a < stride - 1, that increases the padding top and right of the input image.
47 *
48 * 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]
Michalis Spyrou780db4e2017-11-23 09:49:51 +000055 *
56 * where:
57 * width_input is the size of the first input dimension.
58 * height_input 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.
63 *
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
65 * reverse order to perform an actual convolution. This is achieved by using the @ref CPPFlipWeightsKernel.
66 *
67 * This function calls the following OpenCL kernels/functions:
68 *
69 * -# @ref CLDeconvolutionLayerUpsample
70 * -# @ref CLConvolutionLayer
71 *
Michalis Spyrou780db4e2017-11-23 09:49:51 +000072 */
73class CLDeconvolutionLayer : public IFunction
74{
75public:
76 /** Constructor */
77 CLDeconvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +010078 /** Prevent instances of this class from being copied (As this class contains pointers) */
79 CLDeconvolutionLayer(const CLDeconvolutionLayer &) = delete;
80 /** Default move constructor */
81 CLDeconvolutionLayer(CLDeconvolutionLayer &&) = default;
82 /** Prevent instances of this class from being copied (As this class contains pointers) */
83 CLDeconvolutionLayer &operator=(const CLDeconvolutionLayer &) = delete;
84 /** Default move assignment operator */
85 CLDeconvolutionLayer &operator=(CLDeconvolutionLayer &&) = default;
Michalis Spyrou780db4e2017-11-23 09:49:51 +000086 /** Set the input, weights, biases and output tensors.
87 *
giuros01a69a88b2019-01-31 16:29:19 +000088 * @note This method will be deprecated in the next release.
89 *
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +010090 * @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: QASYMM8/F16/F32.
Michalis Spyroua8ca2b02018-04-04 11:03:51 +010091 * @param[in] weights The 4d weights with dimensions [width, height, IFM, OFM]. Data type supported: Same as @p input.
Michalis Spyrou780db4e2017-11-23 09:49:51 +000092 * @param[in] bias (Optional) The biases have one dimension. Data type supported: Same as @p input.
93 * @param[out] output Output tensor. The output has the same number of dimensions as the @p input.
giuros01a69a88b2019-01-31 16:29:19 +000094 * @param[in] info Contains padding and policies to be used in the deconvolution, this is described in @ref PadStrideInfo.
Michalis Spyrou780db4e2017-11-23 09:49:51 +000095 * @param[in] inner_border_right The number of zeros added to right edge of the input.
96 * @param[in] inner_border_top The number of zeros added to top edge of the input.
Michele Di Giorgio70ba7d62018-06-06 17:03:36 +010097 * @param[in] weights_info (Optional) Weights information needed for @ref CLConvolutionLayer, specifies if the weights tensor has been reshaped with @ref CLWeightsReshapeKernel.
Michalis Spyrou780db4e2017-11-23 09:49:51 +000098 *
99 */
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +0100100 void configure(ICLTensor *input, ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const PadStrideInfo &info,
Michele Di Giorgio70ba7d62018-06-06 17:03:36 +0100101 unsigned int inner_border_right, unsigned int inner_border_top, const WeightsInfo &weights_info = WeightsInfo());
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000102 /** Static function to check if given info will lead to a valid configuration of @ref CLDeconvolutionLayer
103 *
giuros01a69a88b2019-01-31 16:29:19 +0000104 * @note This method will be deprecated in the next release.
105 *
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +0100106 * @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: QASYMM8/F16/F32.
Michalis Spyroua8ca2b02018-04-04 11:03:51 +0100107 * @param[in] weights The 4d weights info with dimensions [width, height, IFM, OFM]. Data type supported: Same as @p input.
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000108 * @param[in] bias (Optional) The biases have one dimension. Data type supported: Same as @p input.
109 * @param[in] output Output tensor info. The output has the same number of dimensions as the @p input.
giuros01a69a88b2019-01-31 16:29:19 +0000110 * @param[in] info Contains padding and policies to be used in the deconvolution, this is described in @ref PadStrideInfo.
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000111 * @param[in] inner_border_right The number of zeros added to right edge of the input.
112 * @param[in] inner_border_top The number of zeros added to top edge of the input.
Michele Di Giorgio70ba7d62018-06-06 17:03:36 +0100113 * @param[in] weights_info (Optional) Weights information needed for @ref CLConvolutionLayer, specifies if the weights tensor has been reshaped with @ref CLWeightsReshapeKernel.
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000114 *
115 * @return a status
116 */
117 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *bias, ITensorInfo *output, const PadStrideInfo &info,
Michele Di Giorgio70ba7d62018-06-06 17:03:36 +0100118 unsigned int inner_border_right, unsigned int inner_border_top, const WeightsInfo &weights_info = WeightsInfo());
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000119
giuros01a69a88b2019-01-31 16:29:19 +0000120 /** Set the input, weights, biases and output tensors.
121 *
122 * @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: QASYMM8/F16/F32.
123 * @param[in] weights The 4d weights with dimensions [width, height, IFM, OFM]. Data type supported: Same as @p input.
124 * @param[in] bias (Optional) The biases have one dimension. Data type supported: Same as @p input.
125 * @param[out] output Output tensor. The output has the same number of dimensions as the @p input.
126 * @param[in] info Contains padding and policies to be used in the deconvolution, this is described in @ref PadStrideInfo.
127 * @param[in] weights_info (Optional) Weights information needed for @ref CLConvolutionLayer, specifies if the weights tensor has been reshaped with @ref CLWeightsReshapeKernel.
128 *
129 */
130 void configure(ICLTensor *input, ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const PadStrideInfo &info, const WeightsInfo &weights_info = WeightsInfo());
131 /** Static function to check if given info will lead to a valid configuration of @ref CLDeconvolutionLayer
132 *
133 * @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: QASYMM8/F16/F32.
134 * @param[in] weights The 4d weights info with dimensions [width, height, IFM, OFM]. Data type supported: Same as @p input.
135 * @param[in] bias (Optional) The biases have one dimension. Data type supported: Same as @p input.
136 * @param[in] output Output tensor info. The output has the same number of dimensions as the @p input.
137 * @param[in] info Contains padding and policies to be used in the deconvolution, this is described in @ref PadStrideInfo.
138 * @param[in] weights_info (Optional) Weights information needed for @ref CLConvolutionLayer, specifies if the weights tensor has been reshaped with @ref CLWeightsReshapeKernel.
139 *
140 * @return a status
141 */
142 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *bias, ITensorInfo *output, const PadStrideInfo &info, const WeightsInfo &weights_info = WeightsInfo());
143
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000144 // Inherited methods overridden:
145 void run() override;
Georgios Pinitas72219332018-06-05 14:56:06 +0100146 void prepare() override;
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000147
148private:
149 CLMemoryGroup _memory_group;
150 CLDeconvolutionLayerUpsample _scale_f;
Georgios Pinitasced7a8d2018-02-01 16:31:33 +0000151 CLConvolutionLayer _conv_f;
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +0100152 CPPFlipWeightsKernel _flip_weights;
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000153 CLTensor _scaled_output;
Michele Di Giorgio061dd362018-10-17 17:10:27 +0100154 ICLTensor *_original_weights;
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +0100155 CLTensor _weights_flipped;
Georgios Pinitas72219332018-06-05 14:56:06 +0100156 bool _is_prepared;
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000157};
158}
Michele Di Giorgio70ba7d62018-06-06 17:03:36 +0100159#endif /* __ARM_COMPUTE_CLDECONVOLUTIONLAYER_H__ */