Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 1 | /* |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +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" |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 30 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 31 | #include "arm_compute/runtime/CPP/CPPScheduler.h" |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +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(), |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 43 | _flip_weights(), |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 44 | _scaled_output(), |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 45 | _original_weights(nullptr), |
| 46 | _weights_flipped(), |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 47 | _is_prepared(false) |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 48 | { |
| 49 | } |
| 50 | |
| 51 | Status CLDeconvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *bias, ITensorInfo *output, const PadStrideInfo &info, |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 52 | unsigned int inner_border_right, unsigned int inner_border_top, const WeightsInfo &weights_info) |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 53 | { |
| 54 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output); |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 55 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); |
| 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 | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 66 | ARM_COMPUTE_RETURN_ERROR_ON(!info.padding_is_symmetric()); |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +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 | |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [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), |
| 75 | info.pad().first, info.pad().second, stride_x, stride_y); |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 76 | |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 77 | const TensorShape output_shape = compute_deconvolution_output_shape(out_dims, *input, *weights); |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 78 | |
wr0112358 | 67c8c91 | 2018-05-14 10:13:56 +0200 | [diff] [blame] | 79 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output, weights); |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 80 | |
| 81 | if(bias != nullptr) |
| 82 | { |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [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 | } |
| 91 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, bias); |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [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."); |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 97 | |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [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)); |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +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)); |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 105 | ARM_COMPUTE_RETURN_ON_ERROR(CLConvolutionLayer::validate(&scale_out_info, weights, bias, output, conv_info, weights_info)); |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 106 | |
| 107 | return Status{}; |
| 108 | } |
| 109 | |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 110 | void CLDeconvolutionLayer::configure(ICLTensor *input, ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const PadStrideInfo &info, |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 111 | unsigned int inner_border_right, unsigned int inner_border_top, const WeightsInfo &weights_info) |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +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 | |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 118 | const DataLayout data_layout = input->info()->data_layout(); |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 119 | |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 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 | |
| 123 | _original_weights = weights; |
| 124 | _weights_flipped.allocator()->init(weights->info()->clone()->set_data_layout(data_layout)); |
| 125 | _flip_weights.configure(weights, &_weights_flipped); |
| 126 | |
| 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), |
| 128 | info.pad().first, info.pad().second, stride_x, stride_y); |
| 129 | |
| 130 | const TensorShape output_shape = compute_deconvolution_output_shape(out_dims, *input->info(), *weights->info()); |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 131 | |
| 132 | // Output auto initialization if not yet initialized |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 133 | auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape).set_data_layout(data_layout)); |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +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 | |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 138 | _is_prepared = weights_info.retain_internal_weights(); |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 139 | |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 140 | _memory_group.manage(&_scaled_output); |
| 141 | |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [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); |
| 146 | |
| 147 | TensorInfo scale_out_info(scale_out_shape, 1, input->info()->data_type(), input->info()->quantization_info()); |
| 148 | scale_out_info.set_data_layout(data_layout); |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 149 | _scaled_output.allocator()->init(scale_out_info); |
| 150 | |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [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); |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +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); |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 157 | _conv_f.configure(&_scaled_output, &_weights_flipped, bias, output, conv_info, weights_info); |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 158 | _scaled_output.allocator()->allocate(); |
| 159 | } |
| 160 | |
| 161 | void CLDeconvolutionLayer::run() |
| 162 | { |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 163 | prepare(); |
| 164 | |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 165 | _memory_group.acquire(); |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 166 | |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 167 | _scale_f.run(); |
| 168 | _conv_f.run(); |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 169 | |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 170 | _memory_group.release(); |
| 171 | } |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 172 | |
| 173 | void CLDeconvolutionLayer::prepare() |
| 174 | { |
| 175 | if(!_is_prepared) |
| 176 | { |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 177 | ARM_COMPUTE_ERROR_ON(!_original_weights->is_used()); |
| 178 | |
| 179 | // Run weights flipping and mark original weights tensor as unused |
| 180 | _weights_flipped.allocator()->allocate(); |
| 181 | _weights_flipped.map(true); |
| 182 | _original_weights->map(CLScheduler::get().queue(), true); |
| 183 | CPPScheduler::get().schedule(&_flip_weights, Window::DimZ); |
| 184 | _weights_flipped.unmap(); |
| 185 | _original_weights->unmap(CLScheduler::get().queue()); |
| 186 | _original_weights->mark_as_unused(); |
| 187 | |
| 188 | // Prepare convolution |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 189 | _conv_f.prepare(); |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 190 | |
| 191 | if(!_weights_flipped.is_used()) |
| 192 | { |
| 193 | _weights_flipped.allocator()->free(); |
| 194 | } |
| 195 | |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 196 | _is_prepared = true; |
| 197 | } |
| 198 | } |