blob: 9da02c10adffa2f4757a3f2ad5356ff66057a3bf [file] [log] [blame]
Michalis Spyrou780db4e2017-11-23 09:49:51 +00001/*
giuros01a69a88b2019-01-31 16:29:19 +00002 * Copyright (c) 2017-2019 ARM Limited.
Michalis Spyrou780db4e2017-11-23 09:49:51 +00003 *
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 Spyrouafbc5ff2018-10-03 14:18:19 +010030#include "arm_compute/runtime/CL/CLScheduler.h"
31#include "arm_compute/runtime/CPP/CPPScheduler.h"
Michalis Spyrou780db4e2017-11-23 09:49:51 +000032
33#include <memory>
34#include <tuple>
35
36using namespace arm_compute;
37using namespace arm_compute::misc::shape_calculator;
38
39CLDeconvolutionLayer::CLDeconvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) // NOLINT
40 : _memory_group(std::move(memory_manager)),
41 _scale_f(),
42 _conv_f(),
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +010043 _flip_weights(),
Georgios Pinitas72219332018-06-05 14:56:06 +010044 _scaled_output(),
Michele Di Giorgio061dd362018-10-17 17:10:27 +010045 _original_weights(nullptr),
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +010046 _weights_flipped(),
Georgios Pinitas72219332018-06-05 14:56:06 +010047 _is_prepared(false)
Michalis Spyrou780db4e2017-11-23 09:49:51 +000048{
49}
50
51Status CLDeconvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *bias, ITensorInfo *output, const PadStrideInfo &info,
Michele Di Giorgio70ba7d62018-06-06 17:03:36 +010052 unsigned int inner_border_right, unsigned int inner_border_top, const WeightsInfo &weights_info)
Michalis Spyrou780db4e2017-11-23 09:49:51 +000053{
54 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +010055 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Michele Di Giorgioed5a4922018-09-13 16:22:01 +010056 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 Barbier21f67d62018-02-16 15:17:48 +000066 ARM_COMPUTE_RETURN_ERROR_ON(!info.padding_is_symmetric());
Michalis Spyrou780db4e2017-11-23 09:49:51 +000067
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 Giorgioed5a4922018-09-13 16:22:01 +010074 auto out_dims = deconvolution_output_dimensions(input->dimension(idx_w), input->dimension(idx_h), weights->dimension(idx_w), weights->dimension(idx_h),
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +010075 info.pad().first, info.pad().second, stride_x, stride_y);
Michalis Spyrou780db4e2017-11-23 09:49:51 +000076
Michele Di Giorgioed5a4922018-09-13 16:22:01 +010077 const TensorShape output_shape = compute_deconvolution_output_shape(out_dims, *input, *weights);
Michalis Spyrou780db4e2017-11-23 09:49:51 +000078
wr01123583e3b3712018-05-14 10:13:56 +020079 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output, weights);
Michalis Spyrou780db4e2017-11-23 09:49:51 +000080
81 if(bias != nullptr)
82 {
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +010083 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 Giorgioed5a4922018-09-13 16:22:01 +010091 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, bias);
Michalis Spyrou780db4e2017-11-23 09:49:51 +000092 }
93
Michele Di Giorgioed5a4922018-09-13 16:22:01 +010094 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 Spyrou780db4e2017-11-23 09:49:51 +000097
Michele Di Giorgioed5a4922018-09-13 16:22:01 +010098 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 Spyrou780db4e2017-11-23 09:49:51 +0000102 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 Giorgio70ba7d62018-06-06 17:03:36 +0100105 ARM_COMPUTE_RETURN_ON_ERROR(CLConvolutionLayer::validate(&scale_out_info, weights, bias, output, conv_info, weights_info));
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000106
107 return Status{};
108}
109
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +0100110void CLDeconvolutionLayer::configure(ICLTensor *input, ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const PadStrideInfo &info,
Michele Di Giorgio70ba7d62018-06-06 17:03:36 +0100111 unsigned int inner_border_right, unsigned int inner_border_top, const WeightsInfo &weights_info)
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000112{
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 Giorgioed5a4922018-09-13 16:22:01 +0100118 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 Giorgio061dd362018-10-17 17:10:27 +0100123 _original_weights = weights;
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100124 _weights_flipped.allocator()->init(weights->info()->clone()->set_data_layout(data_layout));
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +0100125 _flip_weights.configure(weights, &_weights_flipped);
126
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100127 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 Spyrouafbc5ff2018-10-03 14:18:19 +0100128 info.pad().first, info.pad().second, stride_x, stride_y);
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000129
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100130 const TensorShape output_shape = compute_deconvolution_output_shape(out_dims, *input->info(), *weights->info());
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000131
132 // Output auto initialization if not yet initialized
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100133 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape).set_data_layout(data_layout));
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000134
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 Giorgio5e25b122018-11-08 12:12:55 +0000138 _is_prepared = weights_info.retain_internal_weights();
Georgios Pinitas72219332018-06-05 14:56:06 +0100139
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000140 _memory_group.manage(&_scaled_output);
141
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100142 // 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 Spyrouafbc5ff2018-10-03 14:18:19 +0100146
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +0100147 TensorInfo scale_out_info(scale_out_shape, 1, input->info()->data_type(), input->info()->quantization_info());
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100148 scale_out_info.set_data_layout(data_layout);
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000149 _scaled_output.allocator()->init(scale_out_info);
150
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +0100151 // 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 Spyrou780db4e2017-11-23 09:49:51 +0000154
155 // setup the function to convolve the upscaled output
156 const PadStrideInfo conv_info(1, 1, 0, 0, 0, 0, DimensionRoundingType::CEIL);
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +0100157 _conv_f.configure(&_scaled_output, &_weights_flipped, bias, output, conv_info, weights_info);
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000158 _scaled_output.allocator()->allocate();
159}
160
giuros01a69a88b2019-01-31 16:29:19 +0000161void 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
167Status 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 Spyrou780db4e2017-11-23 09:49:51 +0000173void CLDeconvolutionLayer::run()
174{
Georgios Pinitas72219332018-06-05 14:56:06 +0100175 prepare();
176
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000177 _memory_group.acquire();
Georgios Pinitas72219332018-06-05 14:56:06 +0100178
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000179 _scale_f.run();
180 _conv_f.run();
Georgios Pinitas72219332018-06-05 14:56:06 +0100181
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000182 _memory_group.release();
183}
Georgios Pinitas72219332018-06-05 14:56:06 +0100184
185void CLDeconvolutionLayer::prepare()
186{
187 if(!_is_prepared)
188 {
Michele Di Giorgio061dd362018-10-17 17:10:27 +0100189 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
190
191 // Run weights flipping and mark original weights tensor as unused
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +0100192 _weights_flipped.allocator()->allocate();
193 _weights_flipped.map(true);
Michele Di Giorgio061dd362018-10-17 17:10:27 +0100194 _original_weights->map(CLScheduler::get().queue(), true);
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +0100195 CPPScheduler::get().schedule(&_flip_weights, Window::DimZ);
196 _weights_flipped.unmap();
Michele Di Giorgio061dd362018-10-17 17:10:27 +0100197 _original_weights->unmap(CLScheduler::get().queue());
198 _original_weights->mark_as_unused();
199
200 // Prepare convolution
Georgios Pinitas72219332018-06-05 14:56:06 +0100201 _conv_f.prepare();
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +0100202
Michele Di Giorgio061dd362018-10-17 17:10:27 +0100203 if(!_weights_flipped.is_used())
204 {
205 _weights_flipped.allocator()->free();
206 }
207
Georgios Pinitas72219332018-06-05 14:56:06 +0100208 _is_prepared = true;
209 }
210}