giuros01 | 4a8ec80 | 2019-03-18 13:25:05 +0000 | [diff] [blame] | 1 | /* |
Freddie Liardet | e92b045 | 2021-04-22 14:55:17 +0100 | [diff] [blame^] | 2 | * Copyright (c) 2019-2021 Arm Limited. |
giuros01 | 4a8ec80 | 2019-03-18 13:25:05 +0000 | [diff] [blame] | 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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_CLDIRECTDECONVOLUTIONLAYER_H |
| 25 | #define ARM_COMPUTE_CLDIRECTDECONVOLUTIONLAYER_H |
giuros01 | 4a8ec80 | 2019-03-18 13:25:05 +0000 | [diff] [blame] | 26 | |
| 27 | #include "arm_compute/runtime/CL/functions/CLConvolutionLayer.h" |
| 28 | #include "arm_compute/runtime/CL/functions/CLDeconvolutionLayerUpsample.h" |
Georgios Pinitas | dbfc2dc | 2019-04-02 12:51:21 +0100 | [diff] [blame] | 29 | #include "arm_compute/runtime/CL/functions/CLReverse.h" |
giuros01 | 4a8ec80 | 2019-03-18 13:25:05 +0000 | [diff] [blame] | 30 | #include "arm_compute/runtime/CL/functions/CLTranspose.h" |
| 31 | |
giuros01 | 4a8ec80 | 2019-03-18 13:25:05 +0000 | [diff] [blame] | 32 | #include "arm_compute/runtime/CL/CLTensor.h" |
| 33 | #include "arm_compute/runtime/IFunction.h" |
| 34 | #include "arm_compute/runtime/IMemoryManager.h" |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 35 | #include "arm_compute/runtime/MemoryGroup.h" |
giuros01 | 4a8ec80 | 2019-03-18 13:25:05 +0000 | [diff] [blame] | 36 | |
| 37 | #include <memory> |
| 38 | |
| 39 | namespace arm_compute |
| 40 | { |
| 41 | class 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 and pad is the amount of padding. |
| 46 | * |
| 47 | * The relation between input to output is as follows: |
| 48 | * \f[ |
| 49 | * width\_output = (width\_input - 1) \cdot stride\_x - 2 \cdot padding\_x + kernel\_x |
| 50 | * \f] |
| 51 | * \f[ |
| 52 | * height\_output = (height\_input - 1) \cdot stride\_y - 2 \cdot padding\_y + kernel\_y |
| 53 | * \f] |
| 54 | * |
| 55 | * where: |
| 56 | * width_input is the size of the first input dimension. |
| 57 | * height_input is the size of the second input dimension. |
| 58 | * width_output is the size of the first output dimension. |
| 59 | * height_output is the size of the second output dimension. |
| 60 | * kernel_x and kernel_y are the convolution sizes in x and y. |
| 61 | * stride_x and stride_y is the input stride of the first and second dimension. |
| 62 | * |
| 63 | * 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 |
Luca Foschiani | fedefc3 | 2020-02-17 17:02:49 +0000 | [diff] [blame] | 64 | * reverse order to perform an actual convolution. This is achieved by using @ref CLReverse. |
giuros01 | 4a8ec80 | 2019-03-18 13:25:05 +0000 | [diff] [blame] | 65 | * |
| 66 | * This function calls the following OpenCL kernels/functions: |
| 67 | * |
| 68 | * -# @ref CLDeconvolutionLayerUpsample |
| 69 | * -# @ref CLConvolutionLayer |
| 70 | * |
| 71 | * And the following CPP kernels: |
Georgios Pinitas | dbfc2dc | 2019-04-02 12:51:21 +0100 | [diff] [blame] | 72 | * -# @ref CLReverse |
giuros01 | 4a8ec80 | 2019-03-18 13:25:05 +0000 | [diff] [blame] | 73 | * |
| 74 | */ |
| 75 | class CLDirectDeconvolutionLayer : public IFunction |
| 76 | { |
| 77 | public: |
| 78 | /** Constructor */ |
| 79 | CLDirectDeconvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
| 80 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 81 | CLDirectDeconvolutionLayer(const CLDirectDeconvolutionLayer &) = delete; |
| 82 | /** Default move constructor */ |
| 83 | CLDirectDeconvolutionLayer(CLDirectDeconvolutionLayer &&) = default; |
| 84 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 85 | CLDirectDeconvolutionLayer &operator=(const CLDirectDeconvolutionLayer &) = delete; |
| 86 | /** Default move assignment operator */ |
| 87 | CLDirectDeconvolutionLayer &operator=(CLDirectDeconvolutionLayer &&) = default; |
| 88 | /** Set the input, weights, biases and output tensors. |
| 89 | * |
Sheri Zhang | 0ef6032 | 2020-02-20 17:37:12 +0000 | [diff] [blame] | 90 | * @param[in,out] input Input tensor. 3 lower dimensions represent a single input, and an optional 4th dimension for batch of inputs. |
| 91 | * Data types supported: QASYMM8_SIGNED/QASYMM8/F16/F32. |
Freddie Liardet | e92b045 | 2021-04-22 14:55:17 +0100 | [diff] [blame^] | 92 | * @param[in] weights The 4d weights with dimensions [width, height, IFM, OFM]. Data type supported: Same as @p input or QSYMM8_PER_CHANNEL if @p input is QASYMM8/QASYMM8_SIGNED. |
Sheri Zhang | 0ef6032 | 2020-02-20 17:37:12 +0000 | [diff] [blame] | 93 | * @param[in] bias (Optional) The biases have one dimension. |
| 94 | * Data type supported: Should match @p input data type, except for input of QASYMM8 and QASYMM8_SIGNED type where biases should be of S32 type |
giuros01 | 4a8ec80 | 2019-03-18 13:25:05 +0000 | [diff] [blame] | 95 | * @param[out] output Output tensor. The output has the same number of dimensions as the @p input. |
| 96 | * @param[in] info Contains padding and policies to be used in the deconvolution, this is decribed in @ref PadStrideInfo. |
| 97 | * @param[in] weights_info (Optional) Weights information needed for @ref CLConvolutionLayer, specifies if the weights tensor has been reshaped with @ref CLWeightsReshapeKernel. |
| 98 | * |
| 99 | */ |
| 100 | void configure(ICLTensor *input, ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const PadStrideInfo &info, const WeightsInfo &weights_info = WeightsInfo()); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 101 | /** Set the input, weights, biases and output tensors. |
| 102 | * |
| 103 | * @param[in] compile_context The compile context to be used. |
| 104 | * @param[in,out] input Input tensor. 3 lower dimensions represent a single input, and an optional 4th dimension for batch of inputs. |
| 105 | * Data types supported: QASYMM8_SIGNED/QASYMM8/F16/F32. |
Freddie Liardet | e92b045 | 2021-04-22 14:55:17 +0100 | [diff] [blame^] | 106 | * @param[in] weights The 4d weights with dimensions [width, height, IFM, OFM]. Data type supported: Same as @p input or QSYMM8_PER_CHANNEL if @p input is QASYMM8/QASYMM8_SIGNED. |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 107 | * @param[in] bias (Optional) The biases have one dimension. |
| 108 | * Data type supported: Should match @p input data type, except for input of QASYMM8 and QASYMM8_SIGNED type where biases should be of S32 type |
| 109 | * @param[out] output Output tensor. The output has the same number of dimensions as the @p input. |
| 110 | * @param[in] info Contains padding and policies to be used in the deconvolution, this is decribed in @ref PadStrideInfo. |
| 111 | * @param[in] weights_info (Optional) Weights information needed for @ref CLConvolutionLayer, specifies if the weights tensor has been reshaped with @ref CLWeightsReshapeKernel. |
| 112 | * |
| 113 | */ |
| 114 | void configure(const CLCompileContext &compile_context, ICLTensor *input, ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const PadStrideInfo &info, |
| 115 | const WeightsInfo &weights_info = WeightsInfo()); |
giuros01 | 4a8ec80 | 2019-03-18 13:25:05 +0000 | [diff] [blame] | 116 | /** Static function to check if given info will lead to a valid configuration of @ref CLDirectDeconvolutionLayer |
| 117 | * |
Sheri Zhang | 0ef6032 | 2020-02-20 17:37:12 +0000 | [diff] [blame] | 118 | * @param[in] input Input tensor info. 3 lower dimensions represent a single input, and an optional 4th dimension for batch of inputs. |
| 119 | * Data types supported: QASYMM8_SIGNED/QASYMM8/F16/F32. |
Freddie Liardet | e92b045 | 2021-04-22 14:55:17 +0100 | [diff] [blame^] | 120 | * @param[in] weights The 4d weights info with dimensions [width, height, IFM, OFM]. Data type supported: Same as @p input or QSYMM8_PER_CHANNEL if @p input is QASYMM8/QASYMM8_SIGNED. |
Sheri Zhang | 0ef6032 | 2020-02-20 17:37:12 +0000 | [diff] [blame] | 121 | * @param[in] bias (Optional) The biases have one dimension. |
| 122 | * Data type supported: Should match @p input data type, except for input of QASYMM8 and QASYMM8_SIGNED type where biases should be of S32 type |
giuros01 | 4a8ec80 | 2019-03-18 13:25:05 +0000 | [diff] [blame] | 123 | * @param[in] output Output tensor info. The output has the same number of dimensions as the @p input. |
| 124 | * @param[in] info Contains padding and policies to be used in the deconvolution, this is decribed in @ref PadStrideInfo. |
| 125 | * @param[in] weights_info (Optional) Weights information needed for @ref CLConvolutionLayer, specifies if the weights tensor has been reshaped with @ref CLWeightsReshapeKernel. |
| 126 | * |
| 127 | * @return a status |
| 128 | */ |
| 129 | static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *bias, ITensorInfo *output, const PadStrideInfo &info, |
| 130 | const WeightsInfo &weights_info = WeightsInfo()); |
| 131 | |
| 132 | // Inherited methods overridden: |
| 133 | void run() override; |
| 134 | void prepare() override; |
| 135 | |
| 136 | private: |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 137 | MemoryGroup _memory_group; |
giuros01 | 4a8ec80 | 2019-03-18 13:25:05 +0000 | [diff] [blame] | 138 | CLDeconvolutionLayerUpsample _scale_f; |
| 139 | CLConvolutionLayer _conv_f; |
Georgios Pinitas | dbfc2dc | 2019-04-02 12:51:21 +0100 | [diff] [blame] | 140 | CLReverse _flip_weights; |
giuros01 | 4a8ec80 | 2019-03-18 13:25:05 +0000 | [diff] [blame] | 141 | |
| 142 | CLTensor _scaled_output; |
| 143 | ICLTensor *_original_weights; |
| 144 | CLTensor _weights_flipped; |
Georgios Pinitas | dbfc2dc | 2019-04-02 12:51:21 +0100 | [diff] [blame] | 145 | CLTensor _flip_axis; |
giuros01 | 4a8ec80 | 2019-03-18 13:25:05 +0000 | [diff] [blame] | 146 | |
| 147 | bool _is_prepared; |
| 148 | }; |
| 149 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 150 | #endif /* ARM_COMPUTE_CLDECONVOLUTIONLAYER_H */ |