giuros01 | 4a8ec80 | 2019-03-18 13:25:05 +0000 | [diff] [blame] | 1 | /* |
| 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 | #include "arm_compute/runtime/CL/functions/CLDirectDeconvolutionLayer.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" |
| 30 | #include "arm_compute/runtime/CL/CLScheduler.h" |
giuros01 | 4a8ec80 | 2019-03-18 13:25:05 +0000 | [diff] [blame] | 31 | #include "utils/TypePrinter.h" |
| 32 | |
| 33 | #include <memory> |
| 34 | #include <tuple> |
| 35 | |
| 36 | namespace arm_compute |
| 37 | { |
| 38 | using namespace arm_compute::misc::shape_calculator; |
| 39 | |
| 40 | CLDirectDeconvolutionLayer::CLDirectDeconvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) // NOLINT |
| 41 | : _memory_group(std::move(memory_manager)), |
| 42 | _scale_f(), |
| 43 | _conv_f(), |
| 44 | _flip_weights(), |
| 45 | _scaled_output(), |
| 46 | _original_weights(nullptr), |
| 47 | _weights_flipped(), |
Georgios Pinitas | dbfc2dc | 2019-04-02 12:51:21 +0100 | [diff] [blame] | 48 | _flip_axis(), |
giuros01 | 4a8ec80 | 2019-03-18 13:25:05 +0000 | [diff] [blame] | 49 | _is_prepared(false) |
| 50 | { |
| 51 | } |
| 52 | |
| 53 | Status CLDirectDeconvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *bias, ITensorInfo *output, const PadStrideInfo &info, |
| 54 | const WeightsInfo &weights_info) |
| 55 | { |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output); |
| 57 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); |
| 58 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, weights); |
| 59 | |
| 60 | const DataLayout data_layout = input->data_layout(); |
| 61 | |
| 62 | const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 63 | const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 64 | const size_t idx_c = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
| 65 | |
| 66 | ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_w) != weights->dimension(idx_h)); |
| 67 | ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_w) < 1); |
| 68 | ARM_COMPUTE_RETURN_ERROR_ON(!info.padding_is_symmetric()); |
| 69 | |
| 70 | const unsigned int stride_x = info.stride().first; |
| 71 | const unsigned int stride_y = info.stride().second; |
| 72 | |
| 73 | auto out_dims = deconvolution_output_dimensions(input->dimension(idx_w), input->dimension(idx_h), weights->dimension(idx_w), weights->dimension(idx_h), |
| 74 | info.pad().first, info.pad().second, stride_x, stride_y); |
| 75 | |
| 76 | const TensorShape output_shape = compute_deconvolution_output_shape(out_dims, *input, *weights); |
| 77 | |
| 78 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output, weights); |
| 79 | |
| 80 | if(bias != nullptr) |
| 81 | { |
| 82 | if(is_data_type_quantized_asymmetric(input->data_type())) |
| 83 | { |
| 84 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(bias, 1, DataType::S32); |
| 85 | } |
| 86 | else |
| 87 | { |
| 88 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, bias); |
| 89 | } |
| 90 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, bias); |
| 91 | } |
| 92 | |
| 93 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->dimension(idx_w) != output_shape[idx_w], "Output's width is invalid."); |
| 94 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->dimension(idx_h) != output_shape[idx_h], "Output's height is invalid."); |
| 95 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->dimension(idx_c) != output_shape[idx_c], "Output's depth is invalid."); |
| 96 | |
| 97 | unsigned int padx = 0; |
| 98 | unsigned int pady = 0; |
| 99 | const TensorShape scale_out_shape = compute_deconvolution_upsampled_shape(*input, *weights, stride_x, stride_y, 0, 0, out_dims, padx, pady); |
| 100 | TensorInfo scale_out_info(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(scale_out_shape).set_data_layout(data_layout)); |
| 101 | const PadStrideInfo conv_info(1, 1, 0, 0, 0, 0, DimensionRoundingType::CEIL); |
| 102 | |
| 103 | ARM_COMPUTE_RETURN_ON_ERROR(CLDeconvolutionLayerUpsample::validate(input, &scale_out_info, BorderSize(), info)); |
| 104 | ARM_COMPUTE_RETURN_ON_ERROR(CLConvolutionLayer::validate(&scale_out_info, weights, bias, output, conv_info, weights_info)); |
| 105 | |
| 106 | return Status{}; |
| 107 | } |
| 108 | |
| 109 | void CLDirectDeconvolutionLayer::configure(ICLTensor *input, ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const PadStrideInfo &info, |
| 110 | const WeightsInfo &weights_info) |
| 111 | { |
| 112 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output); |
| 113 | |
| 114 | const unsigned int stride_x = info.stride().first; |
| 115 | const unsigned int stride_y = info.stride().second; |
| 116 | |
| 117 | const DataLayout data_layout = input->info()->data_layout(); |
| 118 | |
| 119 | const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 120 | const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 121 | |
| 122 | _original_weights = weights; |
Georgios Pinitas | dbfc2dc | 2019-04-02 12:51:21 +0100 | [diff] [blame] | 123 | _flip_axis.allocator()->init(TensorInfo(TensorShape(2U), 1, DataType::U32)); |
giuros01 | 4a8ec80 | 2019-03-18 13:25:05 +0000 | [diff] [blame] | 124 | _weights_flipped.allocator()->init(weights->info()->clone()->set_data_layout(data_layout)); |
Georgios Pinitas | dbfc2dc | 2019-04-02 12:51:21 +0100 | [diff] [blame] | 125 | _flip_weights.configure(weights, &_weights_flipped, &_flip_axis); |
giuros01 | 4a8ec80 | 2019-03-18 13:25:05 +0000 | [diff] [blame] | 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()); |
| 131 | |
| 132 | // Output auto initialization if not yet initialized |
| 133 | auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape).set_data_layout(data_layout)); |
| 134 | |
| 135 | // Perform validation step |
| 136 | ARM_COMPUTE_ERROR_THROW_ON(CLDirectDeconvolutionLayer::validate(input->info(), weights->info(), bias == nullptr ? nullptr : bias->info(), output->info(), info)); |
| 137 | |
| 138 | _is_prepared = weights_info.retain_internal_weights(); |
| 139 | |
| 140 | _memory_group.manage(&_scaled_output); |
| 141 | |
| 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, 0, 0, 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); |
| 149 | _scaled_output.allocator()->init(scale_out_info); |
| 150 | |
| 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(), upsample_info); |
| 154 | |
Georgios Pinitas | dbfc2dc | 2019-04-02 12:51:21 +0100 | [diff] [blame] | 155 | // Setup the function to convolve the upscaled output |
giuros01 | 4a8ec80 | 2019-03-18 13:25:05 +0000 | [diff] [blame] | 156 | const PadStrideInfo conv_info(1, 1, 0, 0, 0, 0, DimensionRoundingType::CEIL); |
| 157 | _conv_f.configure(&_scaled_output, &_weights_flipped, bias, output, conv_info, weights_info); |
| 158 | _scaled_output.allocator()->allocate(); |
Georgios Pinitas | dbfc2dc | 2019-04-02 12:51:21 +0100 | [diff] [blame] | 159 | |
| 160 | // Setup flip axis data |
| 161 | _flip_axis.allocator()->allocate(); |
| 162 | _flip_axis.map(true); |
| 163 | auto axis_data = reinterpret_cast<uint32_t *>(_flip_axis.buffer()); |
| 164 | axis_data[0] = 0; |
| 165 | axis_data[1] = 1; |
| 166 | _flip_axis.unmap(); |
giuros01 | 4a8ec80 | 2019-03-18 13:25:05 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void CLDirectDeconvolutionLayer::run() |
| 170 | { |
| 171 | prepare(); |
| 172 | |
| 173 | _memory_group.acquire(); |
| 174 | |
| 175 | _scale_f.run(); |
| 176 | _conv_f.run(); |
| 177 | |
| 178 | _memory_group.release(); |
| 179 | } |
| 180 | |
| 181 | void CLDirectDeconvolutionLayer::prepare() |
| 182 | { |
| 183 | if(!_is_prepared) |
| 184 | { |
| 185 | ARM_COMPUTE_ERROR_ON(!_original_weights->is_used()); |
| 186 | |
| 187 | // Run weights flipping and mark original weights tensor as unused |
| 188 | _weights_flipped.allocator()->allocate(); |
Georgios Pinitas | dbfc2dc | 2019-04-02 12:51:21 +0100 | [diff] [blame] | 189 | _flip_weights.run(); |
giuros01 | 4a8ec80 | 2019-03-18 13:25:05 +0000 | [diff] [blame] | 190 | _original_weights->mark_as_unused(); |
| 191 | |
| 192 | // Prepare convolution |
| 193 | _conv_f.prepare(); |
| 194 | |
Georgios Pinitas | dbfc2dc | 2019-04-02 12:51:21 +0100 | [diff] [blame] | 195 | // Free flipped weights |
giuros01 | 4a8ec80 | 2019-03-18 13:25:05 +0000 | [diff] [blame] | 196 | if(!_weights_flipped.is_used()) |
| 197 | { |
| 198 | _weights_flipped.allocator()->free(); |
| 199 | } |
| 200 | |
| 201 | _is_prepared = true; |
| 202 | } |
| 203 | } |
| 204 | } // namespace arm_compute |