Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 1 | /* |
giuros01 | a69a88b | 2019-01-31 16:29:19 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +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 | */ |
| 24 | #include "arm_compute/runtime/CL/functions/CLDeconvolutionLayer.h" |
| 25 | |
| 26 | #include "arm_compute/core/Helpers.h" |
| 27 | #include "arm_compute/core/Utils.h" |
| 28 | #include "arm_compute/core/Validate.h" |
| 29 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Michalis Spyrou | afbc5ff | 2018-10-03 14:18:19 +0100 | [diff] [blame] | 30 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 31 | #include "arm_compute/runtime/CPP/CPPScheduler.h" |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 32 | |
| 33 | #include <memory> |
| 34 | #include <tuple> |
| 35 | |
| 36 | using namespace arm_compute; |
| 37 | using namespace arm_compute::misc::shape_calculator; |
| 38 | |
| 39 | CLDeconvolutionLayer::CLDeconvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) // NOLINT |
| 40 | : _memory_group(std::move(memory_manager)), |
| 41 | _scale_f(), |
| 42 | _conv_f(), |
Michalis Spyrou | afbc5ff | 2018-10-03 14:18:19 +0100 | [diff] [blame] | 43 | _flip_weights(), |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 44 | _scaled_output(), |
Michele Di Giorgio | 061dd36 | 2018-10-17 17:10:27 +0100 | [diff] [blame] | 45 | _original_weights(nullptr), |
Michalis Spyrou | afbc5ff | 2018-10-03 14:18:19 +0100 | [diff] [blame] | 46 | _weights_flipped(), |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 47 | _is_prepared(false) |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 48 | { |
| 49 | } |
| 50 | |
| 51 | Status CLDeconvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *bias, ITensorInfo *output, const PadStrideInfo &info, |
Michele Di Giorgio | 70ba7d6 | 2018-06-06 17:03:36 +0100 | [diff] [blame] | 52 | unsigned int inner_border_right, unsigned int inner_border_top, const WeightsInfo &weights_info) |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 53 | { |
| 54 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output); |
Michele Di Giorgio | 9fef38a | 2018-07-06 18:06:58 +0100 | [diff] [blame] | 55 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 56 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, weights); |
| 57 | |
| 58 | const DataLayout data_layout = input->data_layout(); |
| 59 | |
| 60 | const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 61 | const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 62 | const size_t idx_c = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
| 63 | |
| 64 | ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_w) != weights->dimension(idx_h)); |
| 65 | ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_w) < 1); |
Anthony Barbier | 21f67d6 | 2018-02-16 15:17:48 +0000 | [diff] [blame] | 66 | ARM_COMPUTE_RETURN_ERROR_ON(!info.padding_is_symmetric()); |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 67 | |
| 68 | const unsigned int stride_x = info.stride().first; |
| 69 | const unsigned int stride_y = info.stride().second; |
| 70 | |
| 71 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(inner_border_right > stride_x - 1, "inner_border_right must be smaller than stride_x"); |
| 72 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(inner_border_top > stride_y - 1, "inner_border_top must be smaller than stride_y"); |
| 73 | |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 74 | auto out_dims = deconvolution_output_dimensions(input->dimension(idx_w), input->dimension(idx_h), weights->dimension(idx_w), weights->dimension(idx_h), |
Michalis Spyrou | afbc5ff | 2018-10-03 14:18:19 +0100 | [diff] [blame] | 75 | info.pad().first, info.pad().second, stride_x, stride_y); |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 76 | |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 77 | const TensorShape output_shape = compute_deconvolution_output_shape(out_dims, *input, *weights); |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 78 | |
wr0112358 | 3e3b371 | 2018-05-14 10:13:56 +0200 | [diff] [blame] | 79 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output, weights); |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 80 | |
| 81 | if(bias != nullptr) |
| 82 | { |
Michele Di Giorgio | 9fef38a | 2018-07-06 18:06:58 +0100 | [diff] [blame] | 83 | if(is_data_type_quantized_asymmetric(input->data_type())) |
| 84 | { |
| 85 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(bias, 1, DataType::S32); |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, bias); |
| 90 | } |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 91 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, bias); |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 94 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->dimension(idx_w) != output_shape[idx_w], "Output's width is invalid."); |
| 95 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->dimension(idx_h) != output_shape[idx_h], "Output's height is invalid."); |
| 96 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->dimension(idx_c) != output_shape[idx_c], "Output's depth is invalid."); |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 97 | |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 98 | unsigned int padx = 0; |
| 99 | unsigned int pady = 0; |
| 100 | const TensorShape scale_out_shape = compute_deconvolution_upsampled_shape(*input, *weights, stride_x, stride_y, inner_border_right, inner_border_top, out_dims, padx, pady); |
| 101 | TensorInfo scale_out_info(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(scale_out_shape).set_data_layout(data_layout)); |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 102 | const PadStrideInfo conv_info(1, 1, 0, 0, 0, 0, DimensionRoundingType::CEIL); |
| 103 | |
| 104 | ARM_COMPUTE_RETURN_ON_ERROR(CLDeconvolutionLayerUpsample::validate(input, &scale_out_info, BorderSize(inner_border_right, inner_border_top), info)); |
Michele Di Giorgio | 70ba7d6 | 2018-06-06 17:03:36 +0100 | [diff] [blame] | 105 | ARM_COMPUTE_RETURN_ON_ERROR(CLConvolutionLayer::validate(&scale_out_info, weights, bias, output, conv_info, weights_info)); |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 106 | |
| 107 | return Status{}; |
| 108 | } |
| 109 | |
Michalis Spyrou | afbc5ff | 2018-10-03 14:18:19 +0100 | [diff] [blame] | 110 | void CLDeconvolutionLayer::configure(ICLTensor *input, ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const PadStrideInfo &info, |
Michele Di Giorgio | 70ba7d6 | 2018-06-06 17:03:36 +0100 | [diff] [blame] | 111 | unsigned int inner_border_right, unsigned int inner_border_top, const WeightsInfo &weights_info) |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 112 | { |
| 113 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output); |
| 114 | |
| 115 | const unsigned int stride_x = info.stride().first; |
| 116 | const unsigned int stride_y = info.stride().second; |
| 117 | |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 118 | const DataLayout data_layout = input->info()->data_layout(); |
| 119 | |
| 120 | const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 121 | const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 122 | |
Michele Di Giorgio | 061dd36 | 2018-10-17 17:10:27 +0100 | [diff] [blame] | 123 | _original_weights = weights; |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 124 | _weights_flipped.allocator()->init(weights->info()->clone()->set_data_layout(data_layout)); |
Michalis Spyrou | afbc5ff | 2018-10-03 14:18:19 +0100 | [diff] [blame] | 125 | _flip_weights.configure(weights, &_weights_flipped); |
| 126 | |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 127 | auto out_dims = deconvolution_output_dimensions(input->info()->dimension(idx_w), input->info()->dimension(idx_h), weights->info()->dimension(idx_w), weights->info()->dimension(idx_h), |
Michalis Spyrou | afbc5ff | 2018-10-03 14:18:19 +0100 | [diff] [blame] | 128 | info.pad().first, info.pad().second, stride_x, stride_y); |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 129 | |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 130 | const TensorShape output_shape = compute_deconvolution_output_shape(out_dims, *input->info(), *weights->info()); |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 131 | |
| 132 | // Output auto initialization if not yet initialized |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 133 | auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape).set_data_layout(data_layout)); |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 134 | |
| 135 | // Perform validation step |
| 136 | ARM_COMPUTE_ERROR_THROW_ON(CLDeconvolutionLayer::validate(input->info(), weights->info(), bias == nullptr ? nullptr : bias->info(), output->info(), info, inner_border_right, inner_border_top)); |
| 137 | |
Michele Di Giorgio | 5e25b12 | 2018-11-08 12:12:55 +0000 | [diff] [blame] | 138 | _is_prepared = weights_info.retain_internal_weights(); |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 139 | |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 140 | _memory_group.manage(&_scaled_output); |
| 141 | |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 142 | // Find the upsampled dimensions and the padding needed for the convolution with stride 1 in order to match output shape |
| 143 | unsigned int padx = 0; |
| 144 | unsigned int pady = 0; |
| 145 | const TensorShape scale_out_shape = compute_deconvolution_upsampled_shape(*input->info(), *weights->info(), stride_x, stride_y, inner_border_right, inner_border_top, out_dims, padx, pady); |
Michalis Spyrou | afbc5ff | 2018-10-03 14:18:19 +0100 | [diff] [blame] | 146 | |
Michele Di Giorgio | 9fef38a | 2018-07-06 18:06:58 +0100 | [diff] [blame] | 147 | TensorInfo scale_out_info(scale_out_shape, 1, input->info()->data_type(), input->info()->quantization_info()); |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 148 | scale_out_info.set_data_layout(data_layout); |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 149 | _scaled_output.allocator()->init(scale_out_info); |
| 150 | |
Michalis Spyrou | afbc5ff | 2018-10-03 14:18:19 +0100 | [diff] [blame] | 151 | // configure scale function |
| 152 | const PadStrideInfo upsample_info(stride_x, stride_y, padx / 2, pady / 2); |
| 153 | _scale_f.configure(input, &_scaled_output, BorderSize(inner_border_top, inner_border_right), upsample_info); |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 154 | |
| 155 | // setup the function to convolve the upscaled output |
| 156 | const PadStrideInfo conv_info(1, 1, 0, 0, 0, 0, DimensionRoundingType::CEIL); |
Michalis Spyrou | afbc5ff | 2018-10-03 14:18:19 +0100 | [diff] [blame] | 157 | _conv_f.configure(&_scaled_output, &_weights_flipped, bias, output, conv_info, weights_info); |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 158 | _scaled_output.allocator()->allocate(); |
| 159 | } |
| 160 | |
giuros01 | a69a88b | 2019-01-31 16:29:19 +0000 | [diff] [blame] | 161 | void CLDeconvolutionLayer::configure(ICLTensor *input, ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const PadStrideInfo &info, |
| 162 | const WeightsInfo &weights_info) |
| 163 | { |
| 164 | configure(input, weights, bias, output, info, 0, 0, weights_info); |
| 165 | } |
| 166 | |
| 167 | Status CLDeconvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *bias, ITensorInfo *output, const PadStrideInfo &info, |
| 168 | const WeightsInfo &weights_info) |
| 169 | { |
| 170 | return CLDeconvolutionLayer::validate(input, weights, bias, output, info, 0, 0, weights_info); |
| 171 | } |
| 172 | |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 173 | void CLDeconvolutionLayer::run() |
| 174 | { |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 175 | prepare(); |
| 176 | |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 177 | _memory_group.acquire(); |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 178 | |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 179 | _scale_f.run(); |
| 180 | _conv_f.run(); |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 181 | |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 182 | _memory_group.release(); |
| 183 | } |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 184 | |
| 185 | void CLDeconvolutionLayer::prepare() |
| 186 | { |
| 187 | if(!_is_prepared) |
| 188 | { |
Michele Di Giorgio | 061dd36 | 2018-10-17 17:10:27 +0100 | [diff] [blame] | 189 | ARM_COMPUTE_ERROR_ON(!_original_weights->is_used()); |
| 190 | |
| 191 | // Run weights flipping and mark original weights tensor as unused |
Michalis Spyrou | afbc5ff | 2018-10-03 14:18:19 +0100 | [diff] [blame] | 192 | _weights_flipped.allocator()->allocate(); |
| 193 | _weights_flipped.map(true); |
Michele Di Giorgio | 061dd36 | 2018-10-17 17:10:27 +0100 | [diff] [blame] | 194 | _original_weights->map(CLScheduler::get().queue(), true); |
Michalis Spyrou | afbc5ff | 2018-10-03 14:18:19 +0100 | [diff] [blame] | 195 | CPPScheduler::get().schedule(&_flip_weights, Window::DimZ); |
| 196 | _weights_flipped.unmap(); |
Michele Di Giorgio | 061dd36 | 2018-10-17 17:10:27 +0100 | [diff] [blame] | 197 | _original_weights->unmap(CLScheduler::get().queue()); |
| 198 | _original_weights->mark_as_unused(); |
| 199 | |
| 200 | // Prepare convolution |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 201 | _conv_f.prepare(); |
Michalis Spyrou | afbc5ff | 2018-10-03 14:18:19 +0100 | [diff] [blame] | 202 | |
Michele Di Giorgio | 061dd36 | 2018-10-17 17:10:27 +0100 | [diff] [blame] | 203 | if(!_weights_flipped.is_used()) |
| 204 | { |
| 205 | _weights_flipped.allocator()->free(); |
| 206 | } |
| 207 | |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 208 | _is_prepared = true; |
| 209 | } |
| 210 | } |