blob: 936263d6351c9988134cb6cd4c83e9e6c686dd33 [file] [log] [blame]
giuros014a8ec802019-03-18 13:25:05 +00001/*
2 * Copyright (c) 2019 ARM Limited.
3 *
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_CLDIRECTDECONVOLUTIONLAYER_H__
25#define __ARM_COMPUTE_CLDIRECTDECONVOLUTIONLAYER_H__
26
27#include "arm_compute/runtime/CL/functions/CLConvolutionLayer.h"
28#include "arm_compute/runtime/CL/functions/CLDeconvolutionLayerUpsample.h"
29#include "arm_compute/runtime/CL/functions/CLTranspose.h"
30
31#include "arm_compute/core/CPP/kernels/CPPFlipWeightsKernel.h"
32
33#include "arm_compute/runtime/CL/CLMemoryGroup.h"
34#include "arm_compute/runtime/CL/CLTensor.h"
35#include "arm_compute/runtime/IFunction.h"
36#include "arm_compute/runtime/IMemoryManager.h"
37
38#include <memory>
39
40namespace arm_compute
41{
42class ICLTensor;
43/** Function to run the deconvolution layer.
44 *
45 * 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
46 * convolution pass. Input stride defines how many zeroes we should put between each element of the input and pad is the amount of padding.
47 *
48 * The relation between input to output is as follows:
49 * \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]
55 *
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.
62 * stride_x and stride_y is the input stride of the first and second dimension.
63 *
64 * 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 *
72 * And the following CPP kernels:
73 * -# @ref CPPFlipWeightsKernel
74 *
75 */
76class CLDirectDeconvolutionLayer : public IFunction
77{
78public:
79 /** Constructor */
80 CLDirectDeconvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
81 /** Prevent instances of this class from being copied (As this class contains pointers) */
82 CLDirectDeconvolutionLayer(const CLDirectDeconvolutionLayer &) = delete;
83 /** Default move constructor */
84 CLDirectDeconvolutionLayer(CLDirectDeconvolutionLayer &&) = default;
85 /** Prevent instances of this class from being copied (As this class contains pointers) */
86 CLDirectDeconvolutionLayer &operator=(const CLDirectDeconvolutionLayer &) = delete;
87 /** Default move assignment operator */
88 CLDirectDeconvolutionLayer &operator=(CLDirectDeconvolutionLayer &&) = default;
89 /** Set the input, weights, biases and output tensors.
90 *
91 * @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.
92 * @param[in] weights The 4d weights with dimensions [width, height, IFM, OFM]. Data type supported: Same as @p input.
93 * @param[in] bias (Optional) The biases have one dimension. Data type supported: Same as @p input.
94 * @param[out] output Output tensor. The output has the same number of dimensions as the @p input.
95 * @param[in] info Contains padding and policies to be used in the deconvolution, this is decribed in @ref PadStrideInfo.
96 * @param[in] weights_info (Optional) Weights information needed for @ref CLConvolutionLayer, specifies if the weights tensor has been reshaped with @ref CLWeightsReshapeKernel.
97 *
98 */
99 void configure(ICLTensor *input, ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const PadStrideInfo &info, const WeightsInfo &weights_info = WeightsInfo());
100 /** Static function to check if given info will lead to a valid configuration of @ref CLDirectDeconvolutionLayer
101 *
102 * @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.
103 * @param[in] weights The 4d weights info with dimensions [width, height, IFM, OFM]. Data type supported: Same as @p input.
104 * @param[in] bias (Optional) The biases have one dimension. Data type supported: Same as @p input.
105 * @param[in] output Output tensor info. The output has the same number of dimensions as the @p input.
106 * @param[in] info Contains padding and policies to be used in the deconvolution, this is decribed in @ref PadStrideInfo.
107 * @param[in] weights_info (Optional) Weights information needed for @ref CLConvolutionLayer, specifies if the weights tensor has been reshaped with @ref CLWeightsReshapeKernel.
108 *
109 * @return a status
110 */
111 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *bias, ITensorInfo *output, const PadStrideInfo &info,
112 const WeightsInfo &weights_info = WeightsInfo());
113
114 // Inherited methods overridden:
115 void run() override;
116 void prepare() override;
117
118private:
119 CLMemoryGroup _memory_group;
120 CLDeconvolutionLayerUpsample _scale_f;
121 CLConvolutionLayer _conv_f;
122 CPPFlipWeightsKernel _flip_weights;
123
124 CLTensor _scaled_output;
125 ICLTensor *_original_weights;
126 CLTensor _weights_flipped;
127
128 bool _is_prepared;
129};
130} // namespace arm_compute
131#endif /* __ARM_COMPUTE_CLDECONVOLUTIONLAYER_H__ */