Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 1 | /* |
Annop Wongwathanarat | 11f7d7e | 2023-01-12 11:35:37 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021, 2023 Arm Limited. |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [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 | */ |
| 24 | #include "arm_compute/runtime/NEON/functions/NEDeconvolutionLayer.h" |
| 25 | |
| 26 | #include "arm_compute/core/Helpers.h" |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Utils.h" |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/Validate.h" |
Georgios Pinitas | 421405b | 2018-10-26 19:05:32 +0100 | [diff] [blame] | 30 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 31 | |
ramelg01 | cbbb038 | 2021-09-17 17:36:57 +0100 | [diff] [blame] | 32 | #include "src/common/utils/Log.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 33 | #include "src/core/helpers/AutoConfiguration.h" |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 34 | |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 35 | using namespace arm_compute::misc::shape_calculator; |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 36 | |
Manuel Bottini | f391fff | 2019-05-15 13:01:26 +0100 | [diff] [blame] | 37 | namespace arm_compute |
| 38 | { |
Manuel Bottini | 6e10aa3 | 2020-04-30 13:28:23 +0100 | [diff] [blame] | 39 | namespace |
| 40 | { |
| 41 | PadStrideInfo compute_upsample_info(const PadStrideInfo &info, uint32_t deconv_pad_x, uint32_t deconv_pad_y) |
| 42 | { |
| 43 | const unsigned int pad_left = info.pad_left(); |
| 44 | const unsigned int pad_right = info.pad_right(); |
| 45 | const unsigned int pad_top = info.pad_top(); |
| 46 | const unsigned int pad_bottom = info.pad_bottom(); |
| 47 | const unsigned int stride_x = info.stride().first; |
| 48 | const unsigned int stride_y = info.stride().second; |
| 49 | |
| 50 | // Find the upsampled dimensions and the padding needed for the convolution with stride 1 in order to match output shape |
| 51 | unsigned int deconv_pad_left = pad_right > pad_left ? pad_right - pad_left : 0; |
| 52 | unsigned int deconv_pad_right = pad_left > pad_right ? pad_left - pad_right : 0; |
| 53 | deconv_pad_x -= deconv_pad_left + deconv_pad_right; |
| 54 | ARM_COMPUTE_ERROR_ON((deconv_pad_x % 2) != 0); |
| 55 | deconv_pad_left += deconv_pad_x / 2; |
| 56 | deconv_pad_right += deconv_pad_x / 2; |
| 57 | |
| 58 | unsigned int deconv_pad_top = pad_bottom > pad_top ? pad_bottom - pad_top : 0; |
| 59 | unsigned int deconv_pad_bottom = pad_top > pad_bottom ? pad_top - pad_bottom : 0; |
| 60 | deconv_pad_y -= deconv_pad_top + deconv_pad_bottom; |
| 61 | ARM_COMPUTE_ERROR_ON((deconv_pad_y % 2) != 0); |
| 62 | deconv_pad_top += deconv_pad_y / 2; |
| 63 | deconv_pad_bottom += deconv_pad_y / 2; |
| 64 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 65 | return PadStrideInfo(stride_x, stride_y, deconv_pad_left, deconv_pad_right, deconv_pad_top, deconv_pad_bottom, |
| 66 | DimensionRoundingType::FLOOR); |
Manuel Bottini | 6e10aa3 | 2020-04-30 13:28:23 +0100 | [diff] [blame] | 67 | } |
Manuel Bottini | 6e10aa3 | 2020-04-30 13:28:23 +0100 | [diff] [blame] | 68 | } // namespace |
| 69 | |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 70 | NEDeconvolutionLayer::NEDeconvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) // NOLINT |
| 71 | : _memory_group(std::move(memory_manager)), |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 72 | _conv_f(), |
Michalis Spyrou | 33a6990 | 2018-02-23 15:01:52 +0000 | [diff] [blame] | 73 | _upsample_f(), |
Michalis Spyrou | afbc5ff | 2018-10-03 14:18:19 +0100 | [diff] [blame] | 74 | _flip_weights(), |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 75 | _scaled_output(), |
Michalis Spyrou | afbc5ff | 2018-10-03 14:18:19 +0100 | [diff] [blame] | 76 | _weights_flipped(), |
Luca Foschiani | fedefc3 | 2020-02-17 17:02:49 +0000 | [diff] [blame] | 77 | _flip_axis(), |
Michele Di Giorgio | 061dd36 | 2018-10-17 17:10:27 +0100 | [diff] [blame] | 78 | _original_weights(nullptr), |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 79 | _input(nullptr), |
| 80 | _info(), |
Annop Wongwathanarat | b609c93 | 2023-01-16 14:36:45 +0000 | [diff] [blame] | 81 | _is_prepared(false), |
| 82 | _do_upsampling(true) |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 83 | { |
| 84 | } |
| 85 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 86 | Status NEDeconvolutionLayer::validate(const ITensorInfo *input, |
| 87 | const ITensorInfo *weights, |
| 88 | const ITensorInfo *bias, |
| 89 | const ITensorInfo *output, |
| 90 | const PadStrideInfo &info, |
| 91 | bool enable_fast_math, |
| 92 | const WeightsInfo &weights_info) |
Alex Gilday | 27c08ab | 2018-02-22 11:36:16 +0000 | [diff] [blame] | 93 | { |
| 94 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 95 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32, DataType::F16, DataType::QASYMM8, |
| 96 | DataType::QASYMM8_SIGNED); |
| 97 | const unsigned int width_idx = get_data_layout_dimension_index(weights->data_layout(), DataLayoutDimension::WIDTH); |
| 98 | const unsigned int height_idx = |
| 99 | get_data_layout_dimension_index(weights->data_layout(), DataLayoutDimension::HEIGHT); |
Manuel Bottini | d25af67 | 2019-07-10 17:06:12 +0100 | [diff] [blame] | 100 | ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(width_idx) < 1); |
Viet-Hoa Do | 019a7d9 | 2023-06-27 16:33:57 +0100 | [diff] [blame] | 101 | ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(height_idx) < 1); |
Freddie Liardet | 9d061b0 | 2021-04-06 15:59:28 +0100 | [diff] [blame] | 102 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(weights, input); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 103 | if (is_data_type_quantized_per_channel(weights->data_type()) && is_data_type_quantized(input->data_type())) |
Freddie Liardet | 9d061b0 | 2021-04-06 15:59:28 +0100 | [diff] [blame] | 104 | { |
| 105 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QSYMM8_PER_CHANNEL); |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
| 110 | } |
Alex Gilday | 27c08ab | 2018-02-22 11:36:16 +0000 | [diff] [blame] | 111 | |
Pablo Marquez Tello | 745153b | 2023-09-27 15:20:40 +0100 | [diff] [blame] | 112 | const unsigned int pad_left = info.pad_left(); |
| 113 | const unsigned int pad_top = info.pad_top(); |
| 114 | const unsigned int pad_right = info.pad_right(); |
| 115 | const unsigned int pad_bottom = info.pad_bottom(); |
| 116 | |
| 117 | ARM_COMPUTE_RETURN_ERROR_ON(((input->dimension(width_idx) - 1) * info.stride().first + |
| 118 | weights->dimension(width_idx)) < (pad_left + pad_right)); |
| 119 | ARM_COMPUTE_RETURN_ERROR_ON(((input->dimension(height_idx) - 1) * info.stride().second + |
| 120 | weights->dimension(height_idx)) < (pad_top + pad_bottom)); |
| 121 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 122 | auto out_dims = |
| 123 | deconvolution_output_dimensions(input->dimension(width_idx), input->dimension(height_idx), |
| 124 | weights->dimension(width_idx), weights->dimension(height_idx), info); |
Alex Gilday | 27c08ab | 2018-02-22 11:36:16 +0000 | [diff] [blame] | 125 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 126 | if (bias != nullptr) |
Usama Arif | 2899e00 | 2019-04-16 14:32:25 +0100 | [diff] [blame] | 127 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 128 | if (is_data_type_quantized_asymmetric(input->data_type())) |
Manuel Bottini | 9f0d5ec | 2019-08-19 13:31:38 +0100 | [diff] [blame] | 129 | { |
| 130 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(bias, 1, DataType::S32); |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, bias); |
| 135 | } |
Alex Gilday | 27c08ab | 2018-02-22 11:36:16 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 138 | if (output->tensor_shape().total_size() > 0) |
Alex Gilday | 27c08ab | 2018-02-22 11:36:16 +0000 | [diff] [blame] | 139 | { |
| 140 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Alex Gilday | 27c08ab | 2018-02-22 11:36:16 +0000 | [diff] [blame] | 141 | |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 142 | const TensorShape output_shape = compute_deconvolution_output_shape(out_dims, *input, *weights); |
| 143 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 144 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->dimension(Window::DimX) != output_shape.x(), |
| 145 | "Output's width is invalid."); |
| 146 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->dimension(Window::DimY) != output_shape.y(), |
| 147 | "Output's height is invalid."); |
| 148 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->dimension(Window::DimZ) != output_shape.z(), |
| 149 | "Output's depth is invalid."); |
Alex Gilday | 27c08ab | 2018-02-22 11:36:16 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Pablo Marquez Tello | 745153b | 2023-09-27 15:20:40 +0100 | [diff] [blame] | 152 | uint32_t deconv_pad_x = 0; |
| 153 | uint32_t deconv_pad_y = 0; |
| 154 | const uint32_t stride_x = info.stride().first; |
| 155 | const uint32_t stride_y = info.stride().second; |
| 156 | const auto deconv_padding = compute_deconvolution_padding(*input, *weights, static_cast<int32_t>(stride_x), |
| 157 | static_cast<int32_t>(stride_y), out_dims); |
| 158 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(deconv_padding.first < 0 || deconv_padding.second < 0, |
| 159 | "Negative padding not supported"); |
Pablo Marquez Tello | 2a0939d | 2021-01-20 15:02:59 +0000 | [diff] [blame] | 160 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 161 | const TensorShape scale_out_shape = compute_deconvolution_upsampled_shape(*input, *weights, stride_x, stride_y, |
| 162 | out_dims, deconv_pad_x, deconv_pad_y); |
| 163 | TensorInfo scale_out_info(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(scale_out_shape)); |
Annop Wongwathanarat | c4ed2fd | 2023-02-09 16:33:13 +0000 | [diff] [blame] | 164 | const PadStrideInfo upsample_info = compute_upsample_info(info, deconv_pad_x, deconv_pad_y); |
| 165 | |
| 166 | // Do not perform upsampling when the operation uses unit stride in all dimensions |
| 167 | const bool do_upsampling = stride_x != 1 || stride_y != 1; |
Alex Gilday | 27c08ab | 2018-02-22 11:36:16 +0000 | [diff] [blame] | 168 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 169 | const unsigned int batches_idx = |
| 170 | get_data_layout_dimension_index(weights->data_layout(), DataLayoutDimension::BATCHES); |
| 171 | const unsigned int channel_idx = |
| 172 | get_data_layout_dimension_index(weights->data_layout(), DataLayoutDimension::CHANNEL); |
Manuel Bottini | d25af67 | 2019-07-10 17:06:12 +0100 | [diff] [blame] | 173 | ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(batches_idx) != scale_out_info.dimension(batches_idx)); |
| 174 | ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(channel_idx) != scale_out_info.dimension(channel_idx)); |
Alex Gilday | 27c08ab | 2018-02-22 11:36:16 +0000 | [diff] [blame] | 175 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 176 | if (do_upsampling) |
Annop Wongwathanarat | c4ed2fd | 2023-02-09 16:33:13 +0000 | [diff] [blame] | 177 | { |
| 178 | const PadStrideInfo conv_info(1, 1, 0, 0, 0, 0, DimensionRoundingType::CEIL); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 179 | ARM_COMPUTE_RETURN_ON_ERROR(NEConvolutionLayer::validate(&scale_out_info, weights, bias, output, conv_info, |
| 180 | weights_info, Size2D(1U, 1U), ActivationLayerInfo(), |
| 181 | enable_fast_math)); |
Annop Wongwathanarat | c4ed2fd | 2023-02-09 16:33:13 +0000 | [diff] [blame] | 182 | } |
| 183 | else |
| 184 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 185 | const PadStrideInfo conv_info(1, 1, upsample_info.pad_left(), upsample_info.pad_right(), |
| 186 | upsample_info.pad_top(), upsample_info.pad_bottom(), DimensionRoundingType::CEIL); |
| 187 | ARM_COMPUTE_RETURN_ON_ERROR(NEConvolutionLayer::validate(input, weights, bias, output, conv_info, weights_info, |
| 188 | Size2D(1U, 1U), ActivationLayerInfo(), |
| 189 | enable_fast_math)); |
Annop Wongwathanarat | c4ed2fd | 2023-02-09 16:33:13 +0000 | [diff] [blame] | 190 | } |
Alex Gilday | 27c08ab | 2018-02-22 11:36:16 +0000 | [diff] [blame] | 191 | |
| 192 | return Status{}; |
| 193 | } |
| 194 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 195 | void NEDeconvolutionLayer::configure(ITensor *input, |
| 196 | const ITensor *weights, |
| 197 | const ITensor *bias, |
| 198 | ITensor *output, |
| 199 | const PadStrideInfo &info, |
| 200 | bool enable_fast_math, |
| 201 | const WeightsInfo &weights_info) |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 202 | { |
Michele Di Giorgio | 0c5191c | 2019-08-15 15:11:45 +0100 | [diff] [blame] | 203 | // Perform validation step |
Alex Gilday | 27c08ab | 2018-02-22 11:36:16 +0000 | [diff] [blame] | 204 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 205 | ARM_COMPUTE_ERROR_THROW_ON(NEDeconvolutionLayer::validate(input->info(), weights->info(), |
| 206 | (bias == nullptr) ? nullptr : bias->info(), |
| 207 | output->info(), info, enable_fast_math, weights_info)); |
Annop Wongwathanarat | adfcacc | 2023-03-01 15:19:50 +0000 | [diff] [blame] | 208 | ARM_COMPUTE_LOG_PARAMS(input, weights, bias, output, info, enable_fast_math, weights_info); |
Manuel Bottini | d25af67 | 2019-07-10 17:06:12 +0100 | [diff] [blame] | 209 | |
Manuel Bottini | 6e10aa3 | 2020-04-30 13:28:23 +0100 | [diff] [blame] | 210 | const DataLayout data_layout = input->info()->data_layout(); |
| 211 | const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 212 | const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 213 | auto out_dims = deconvolution_output_dimensions( |
| 214 | input->info()->dimension(width_idx), input->info()->dimension(height_idx), |
| 215 | weights->info()->dimension(width_idx), weights->info()->dimension(height_idx), info); |
Manuel Bottini | 6e10aa3 | 2020-04-30 13:28:23 +0100 | [diff] [blame] | 216 | |
| 217 | const TensorShape output_shape = compute_deconvolution_output_shape(out_dims, *input->info(), *weights->info()); |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 218 | |
Michele Di Giorgio | 061dd36 | 2018-10-17 17:10:27 +0100 | [diff] [blame] | 219 | _input = input; |
| 220 | _original_weights = weights; |
| 221 | _info = info; |
Michele Di Giorgio | 061dd36 | 2018-10-17 17:10:27 +0100 | [diff] [blame] | 222 | _is_prepared = false; |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 223 | |
Manuel Bottini | 6e10aa3 | 2020-04-30 13:28:23 +0100 | [diff] [blame] | 224 | const unsigned int stride_x = info.stride().first; |
| 225 | const unsigned int stride_y = info.stride().second; |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 226 | |
Michalis Spyrou | afbc5ff | 2018-10-03 14:18:19 +0100 | [diff] [blame] | 227 | // Output auto initialization if not yet initialized |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 228 | auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(), |
| 229 | input->info()->quantization_info()); |
Michalis Spyrou | afbc5ff | 2018-10-03 14:18:19 +0100 | [diff] [blame] | 230 | |
Luca Foschiani | fedefc3 | 2020-02-17 17:02:49 +0000 | [diff] [blame] | 231 | _flip_axis.allocator()->init(TensorInfo(TensorShape(2U), 1, DataType::U32)); |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 232 | |
Manuel Bottini | 6e10aa3 | 2020-04-30 13:28:23 +0100 | [diff] [blame] | 233 | _weights_flipped.allocator()->init(weights->info()->clone()->set_data_layout(data_layout)); |
| 234 | _flip_weights.configure(weights, &_weights_flipped, &_flip_axis); |
Michalis Spyrou | afbc5ff | 2018-10-03 14:18:19 +0100 | [diff] [blame] | 235 | |
Manuel Bottini | 6e10aa3 | 2020-04-30 13:28:23 +0100 | [diff] [blame] | 236 | // setup the function to convolve the upscaled output |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 237 | uint32_t deconv_pad_x = 0; |
| 238 | uint32_t deconv_pad_y = 0; |
| 239 | const TensorShape scale_out_shape = compute_deconvolution_upsampled_shape( |
| 240 | *input->info(), *weights->info(), stride_x, stride_y, out_dims, deconv_pad_x, deconv_pad_y); |
Pablo Marquez Tello | 745153b | 2023-09-27 15:20:40 +0100 | [diff] [blame] | 241 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 242 | const PadStrideInfo upsample_info = compute_upsample_info(info, deconv_pad_x, deconv_pad_y); |
Annop Wongwathanarat | c4ed2fd | 2023-02-09 16:33:13 +0000 | [diff] [blame] | 243 | |
| 244 | // Do not perform upsampling when the operation uses unit stride in all dimensions |
| 245 | _do_upsampling = stride_x != 1 || stride_y != 1; |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 246 | |
Luca Foschiani | fedefc3 | 2020-02-17 17:02:49 +0000 | [diff] [blame] | 247 | // Setup flip axis data |
| 248 | _flip_axis.allocator()->allocate(); |
| 249 | auto axis_data = reinterpret_cast<uint32_t *>(_flip_axis.buffer()); |
Manuel Bottini | 6e10aa3 | 2020-04-30 13:28:23 +0100 | [diff] [blame] | 250 | axis_data[0] = static_cast<uint32_t>(width_idx); |
| 251 | axis_data[1] = static_cast<uint32_t>(height_idx); |
| 252 | |
Annop Wongwathanarat | b609c93 | 2023-01-16 14:36:45 +0000 | [diff] [blame] | 253 | // Setup convolution and upsampling, if needed |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 254 | if (_do_upsampling) |
Annop Wongwathanarat | b609c93 | 2023-01-16 14:36:45 +0000 | [diff] [blame] | 255 | { |
| 256 | _memory_group.manage(&_scaled_output); |
Annop Wongwathanarat | b609c93 | 2023-01-16 14:36:45 +0000 | [diff] [blame] | 257 | |
Annop Wongwathanarat | c4ed2fd | 2023-02-09 16:33:13 +0000 | [diff] [blame] | 258 | const PadStrideInfo conv_info(1, 1, 0, 0, 0, 0, DimensionRoundingType::CEIL); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 259 | TensorInfo scale_out_info(scale_out_shape, 1, input->info()->data_type(), input->info()->quantization_info()); |
Annop Wongwathanarat | b609c93 | 2023-01-16 14:36:45 +0000 | [diff] [blame] | 260 | scale_out_info.set_data_layout(data_layout); |
| 261 | _scaled_output.allocator()->init(scale_out_info); |
| 262 | |
Annop Wongwathanarat | c4ed2fd | 2023-02-09 16:33:13 +0000 | [diff] [blame] | 263 | // Minor optimization: In the upsampling step, we do not need to allocate space for the padding in the upsampled image. |
| 264 | // The padding amount can be given as input to the convolution layer. |
Annop Wongwathanarat | b609c93 | 2023-01-16 14:36:45 +0000 | [diff] [blame] | 265 | _upsample_f.configure(input, &_scaled_output, upsample_info); |
| 266 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 267 | _conv_f.configure(&_scaled_output, &_weights_flipped, bias, output, conv_info, weights_info, Size2D(1U, 1U), |
| 268 | ActivationLayerInfo(), enable_fast_math); |
Annop Wongwathanarat | b609c93 | 2023-01-16 14:36:45 +0000 | [diff] [blame] | 269 | |
| 270 | _scaled_output.allocator()->allocate(); |
| 271 | } |
| 272 | else |
| 273 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 274 | const PadStrideInfo conv_info(1, 1, upsample_info.pad_left(), upsample_info.pad_right(), |
| 275 | upsample_info.pad_top(), upsample_info.pad_bottom(), DimensionRoundingType::CEIL); |
| 276 | _conv_f.configure(input, &_weights_flipped, bias, output, conv_info, weights_info, Size2D(1U, 1U), |
| 277 | ActivationLayerInfo(), enable_fast_math); |
Annop Wongwathanarat | b609c93 | 2023-01-16 14:36:45 +0000 | [diff] [blame] | 278 | } |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | void NEDeconvolutionLayer::run() |
| 282 | { |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 283 | prepare(); |
| 284 | |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 285 | MemoryGroupResourceScope scope_mg(_memory_group); |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 286 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 287 | if (_do_upsampling) |
Annop Wongwathanarat | b609c93 | 2023-01-16 14:36:45 +0000 | [diff] [blame] | 288 | { |
| 289 | _upsample_f.run(); |
| 290 | } |
Georgios Pinitas | 056b5d9 | 2018-02-13 18:50:55 +0000 | [diff] [blame] | 291 | _conv_f.run(); |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | void NEDeconvolutionLayer::prepare() |
| 295 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 296 | if (!_is_prepared) |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 297 | { |
Michele Di Giorgio | 061dd36 | 2018-10-17 17:10:27 +0100 | [diff] [blame] | 298 | ARM_COMPUTE_ERROR_ON(!_original_weights->is_used()); |
SiCong Li | ecb1427 | 2019-08-20 11:09:11 +0100 | [diff] [blame] | 299 | |
| 300 | // Run weights flipping and mark original weights tensor as unused |
| 301 | _weights_flipped.allocator()->allocate(); |
Luca Foschiani | fedefc3 | 2020-02-17 17:02:49 +0000 | [diff] [blame] | 302 | _flip_weights.run(); |
Michele Di Giorgio | 061dd36 | 2018-10-17 17:10:27 +0100 | [diff] [blame] | 303 | _original_weights->mark_as_unused(); |
| 304 | |
| 305 | // Prepare convolution |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 306 | _conv_f.prepare(); |
Michele Di Giorgio | 061dd36 | 2018-10-17 17:10:27 +0100 | [diff] [blame] | 307 | |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 308 | _is_prepared = true; |
| 309 | } |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 310 | } |
Manuel Bottini | c1b76fa | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 311 | } // namespace arm_compute |