blob: e07feb226a4408d6cab8bf14d06b6aeff86fa854 [file] [log] [blame]
Anthony Barbierf45d5a92018-01-24 16:23:15 +00001/*
Anthony Barbier06ea0482018-02-22 15:45:35 +00002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbierf45d5a92018-01-24 16:23:15 +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"
Jenkinsb9abeae2018-11-22 11:58:08 +000030#include "arm_compute/runtime/CL/CLScheduler.h"
31#include "arm_compute/runtime/CPP/CPPScheduler.h"
Anthony Barbierf45d5a92018-01-24 16:23:15 +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(),
Jenkinsb9abeae2018-11-22 11:58:08 +000043 _flip_weights(),
Jenkins52ba29e2018-08-29 15:32:11 +000044 _scaled_output(),
Jenkinsb9abeae2018-11-22 11:58:08 +000045 _original_weights(nullptr),
46 _weights_flipped(),
Jenkins52ba29e2018-08-29 15:32:11 +000047 _is_prepared(false)
Anthony Barbierf45d5a92018-01-24 16:23:15 +000048{
49}
50
51Status CLDeconvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *bias, ITensorInfo *output, const PadStrideInfo &info,
Jenkins52ba29e2018-08-29 15:32:11 +000052 unsigned int inner_border_right, unsigned int inner_border_top, const WeightsInfo &weights_info)
Anthony Barbierf45d5a92018-01-24 16:23:15 +000053{
54 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Jenkinsb9abeae2018-11-22 11:58:08 +000055 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 Barbier06ea0482018-02-22 15:45:35 +000066 ARM_COMPUTE_RETURN_ERROR_ON(!info.padding_is_symmetric());
Anthony Barbierf45d5a92018-01-24 16:23:15 +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
Jenkinsb9abeae2018-11-22 11:58:08 +000074 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 Barbierf45d5a92018-01-24 16:23:15 +000076
Jenkinsb9abeae2018-11-22 11:58:08 +000077 const TensorShape output_shape = compute_deconvolution_output_shape(out_dims, *input, *weights);
Anthony Barbierf45d5a92018-01-24 16:23:15 +000078
wr011235867c8c912018-05-14 10:13:56 +020079 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output, weights);
Anthony Barbierf45d5a92018-01-24 16:23:15 +000080
81 if(bias != nullptr)
82 {
Jenkinsb9abeae2018-11-22 11:58:08 +000083 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 Barbierf45d5a92018-01-24 16:23:15 +000092 }
93
Jenkinsb9abeae2018-11-22 11:58:08 +000094 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 Barbierf45d5a92018-01-24 16:23:15 +000097
Jenkinsb9abeae2018-11-22 11:58:08 +000098 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 Barbierf45d5a92018-01-24 16:23:15 +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));
Jenkins52ba29e2018-08-29 15:32:11 +0000105 ARM_COMPUTE_RETURN_ON_ERROR(CLConvolutionLayer::validate(&scale_out_info, weights, bias, output, conv_info, weights_info));
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000106
107 return Status{};
108}
109
Jenkinsb9abeae2018-11-22 11:58:08 +0000110void CLDeconvolutionLayer::configure(ICLTensor *input, ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const PadStrideInfo &info,
Jenkins52ba29e2018-08-29 15:32:11 +0000111 unsigned int inner_border_right, unsigned int inner_border_top, const WeightsInfo &weights_info)
Anthony Barbierf45d5a92018-01-24 16:23:15 +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
Jenkinsb9abeae2018-11-22 11:58:08 +0000118 const DataLayout data_layout = input->info()->data_layout();
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000119
Jenkinsb9abeae2018-11-22 11:58:08 +0000120 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 Barbierf45d5a92018-01-24 16:23:15 +0000131
132 // Output auto initialization if not yet initialized
Jenkinsb9abeae2018-11-22 11:58:08 +0000133 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape).set_data_layout(data_layout));
Anthony Barbierf45d5a92018-01-24 16:23:15 +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
Jenkinsb9abeae2018-11-22 11:58:08 +0000138 _is_prepared = weights_info.retain_internal_weights();
Jenkins52ba29e2018-08-29 15:32:11 +0000139
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000140 _memory_group.manage(&_scaled_output);
141
Jenkinsb9abeae2018-11-22 11:58:08 +0000142 // 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 Barbierf45d5a92018-01-24 16:23:15 +0000149 _scaled_output.allocator()->init(scale_out_info);
150
Jenkinsb9abeae2018-11-22 11:58:08 +0000151 // 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 Barbierf45d5a92018-01-24 16:23:15 +0000154
155 // setup the function to convolve the upscaled output
156 const PadStrideInfo conv_info(1, 1, 0, 0, 0, 0, DimensionRoundingType::CEIL);
Jenkinsb9abeae2018-11-22 11:58:08 +0000157 _conv_f.configure(&_scaled_output, &_weights_flipped, bias, output, conv_info, weights_info);
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000158 _scaled_output.allocator()->allocate();
159}
160
161void CLDeconvolutionLayer::run()
162{
Jenkins52ba29e2018-08-29 15:32:11 +0000163 prepare();
164
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000165 _memory_group.acquire();
Jenkins52ba29e2018-08-29 15:32:11 +0000166
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000167 _scale_f.run();
168 _conv_f.run();
Jenkins52ba29e2018-08-29 15:32:11 +0000169
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000170 _memory_group.release();
171}
Jenkins52ba29e2018-08-29 15:32:11 +0000172
173void CLDeconvolutionLayer::prepare()
174{
175 if(!_is_prepared)
176 {
Jenkinsb9abeae2018-11-22 11:58:08 +0000177 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
Jenkins52ba29e2018-08-29 15:32:11 +0000189 _conv_f.prepare();
Jenkinsb9abeae2018-11-22 11:58:08 +0000190
191 if(!_weights_flipped.is_used())
192 {
193 _weights_flipped.allocator()->free();
194 }
195
Jenkins52ba29e2018-08-29 15:32:11 +0000196 _is_prepared = true;
197 }
198}