blob: 0ffafa02211b360355de0475f136ee1bd58b0843 [file] [log] [blame]
giuros014a8ec802019-03-18 13:25:05 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2019-2020 Arm Limited.
giuros014a8ec802019-03-18 13:25:05 +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/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"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010031#include "src/core/helpers/AutoConfiguration.h"
giuros014a8ec802019-03-18 13:25:05 +000032
33#include <memory>
34#include <tuple>
35
36namespace arm_compute
37{
38using namespace arm_compute::misc::shape_calculator;
39
40CLDirectDeconvolutionLayer::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 Pinitasdbfc2dc2019-04-02 12:51:21 +010048 _flip_axis(),
giuros014a8ec802019-03-18 13:25:05 +000049 _is_prepared(false)
50{
51}
52
53Status 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);
Sheri Zhang0ef60322020-02-20 17:37:12 +000057 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8_SIGNED, DataType::QASYMM8, DataType::F16, DataType::F32);
giuros014a8ec802019-03-18 13:25:05 +000058 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, weights);
giuros014a8ec802019-03-18 13:25:05 +000059 const DataLayout data_layout = input->data_layout();
60
61 const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
62 const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
63 const size_t idx_c = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
64
65 ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_w) != weights->dimension(idx_h));
66 ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_w) < 1);
giuros014a8ec802019-03-18 13:25:05 +000067
Matthew Jacksonb9070a42019-08-22 16:13:27 +010068 auto out_dims = deconvolution_output_dimensions(input->dimension(idx_w), input->dimension(idx_h), weights->dimension(idx_w), weights->dimension(idx_h), info);
giuros014a8ec802019-03-18 13:25:05 +000069
70 const TensorShape output_shape = compute_deconvolution_output_shape(out_dims, *input, *weights);
71
72 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output, weights);
73
74 if(bias != nullptr)
75 {
76 if(is_data_type_quantized_asymmetric(input->data_type()))
77 {
78 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(bias, 1, DataType::S32);
79 }
80 else
81 {
82 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, bias);
83 }
84 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, bias);
85 }
86
87 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->dimension(idx_w) != output_shape[idx_w], "Output's width is invalid.");
88 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->dimension(idx_h) != output_shape[idx_h], "Output's height is invalid.");
89 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->dimension(idx_c) != output_shape[idx_c], "Output's depth is invalid.");
90
Manuel Bottini2b84be52020-04-08 10:15:51 +010091 unsigned int deconv_pad_x = 0;
92 unsigned int deconv_pad_y = 0;
93 const unsigned int stride_x = info.stride().first;
94 const unsigned int stride_y = info.stride().second;
Matthew Jacksonb9070a42019-08-22 16:13:27 +010095 const TensorShape scale_out_shape = compute_deconvolution_upsampled_shape(*input, *weights, stride_x, stride_y, out_dims, deconv_pad_x, deconv_pad_y);
giuros014a8ec802019-03-18 13:25:05 +000096 TensorInfo scale_out_info(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(scale_out_shape).set_data_layout(data_layout));
97 const PadStrideInfo conv_info(1, 1, 0, 0, 0, 0, DimensionRoundingType::CEIL);
98
Manuel Bottinic1b76fa2019-06-17 12:04:40 +010099 ARM_COMPUTE_RETURN_ON_ERROR(CLDeconvolutionLayerUpsample::validate(input, &scale_out_info, info));
giuros014a8ec802019-03-18 13:25:05 +0000100 ARM_COMPUTE_RETURN_ON_ERROR(CLConvolutionLayer::validate(&scale_out_info, weights, bias, output, conv_info, weights_info));
101
102 return Status{};
103}
104
105void CLDirectDeconvolutionLayer::configure(ICLTensor *input, ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const PadStrideInfo &info,
106 const WeightsInfo &weights_info)
107{
Manuel Bottini2b84be52020-04-08 10:15:51 +0100108 configure(CLKernelLibrary::get().get_compile_context(), input, weights, bias, output, info, weights_info);
109}
110
111void CLDirectDeconvolutionLayer::configure(const CLCompileContext &compile_context, ICLTensor *input, ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const PadStrideInfo &info,
112 const WeightsInfo &weights_info)
113{
giuros014a8ec802019-03-18 13:25:05 +0000114 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
115
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100116 const unsigned int pad_left = info.pad_left();
117 const unsigned int pad_right = info.pad_right();
118 const unsigned int pad_top = info.pad_top();
119 const unsigned int pad_bottom = info.pad_bottom();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100120 const unsigned int stride_x = info.stride().first;
121 const unsigned int stride_y = info.stride().second;
giuros014a8ec802019-03-18 13:25:05 +0000122
123 const DataLayout data_layout = input->info()->data_layout();
124
125 const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
126 const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
127
128 _original_weights = weights;
Georgios Pinitasdbfc2dc2019-04-02 12:51:21 +0100129 _flip_axis.allocator()->init(TensorInfo(TensorShape(2U), 1, DataType::U32));
giuros014a8ec802019-03-18 13:25:05 +0000130 _weights_flipped.allocator()->init(weights->info()->clone()->set_data_layout(data_layout));
Manuel Bottini2b84be52020-04-08 10:15:51 +0100131 _flip_weights.configure(compile_context, weights, &_weights_flipped, &_flip_axis);
giuros014a8ec802019-03-18 13:25:05 +0000132
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100133 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), info);
giuros014a8ec802019-03-18 13:25:05 +0000134
135 const TensorShape output_shape = compute_deconvolution_output_shape(out_dims, *input->info(), *weights->info());
136
137 // Output auto initialization if not yet initialized
138 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape).set_data_layout(data_layout));
139
140 // Perform validation step
141 ARM_COMPUTE_ERROR_THROW_ON(CLDirectDeconvolutionLayer::validate(input->info(), weights->info(), bias == nullptr ? nullptr : bias->info(), output->info(), info));
142
143 _is_prepared = weights_info.retain_internal_weights();
144
145 _memory_group.manage(&_scaled_output);
146
147 // Find the upsampled dimensions and the padding needed for the convolution with stride 1 in order to match output shape
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100148 unsigned int deconv_pad_x = 0;
149 unsigned int deconv_pad_y = 0;
150 const TensorShape scale_out_shape = compute_deconvolution_upsampled_shape(*input->info(), *weights->info(), stride_x, stride_y, out_dims, deconv_pad_x, deconv_pad_y);
151
152 unsigned int deconv_pad_left = pad_right > pad_left ? pad_right - pad_left : 0;
153 unsigned int deconv_pad_right = pad_left > pad_right ? pad_left - pad_right : 0;
154 deconv_pad_x -= deconv_pad_left + deconv_pad_right;
155 ARM_COMPUTE_ERROR_ON((deconv_pad_x % 2) != 0);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100156 deconv_pad_left += deconv_pad_x / 2;
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100157 deconv_pad_right += deconv_pad_x / 2;
158
159 unsigned int deconv_pad_top = pad_bottom > pad_top ? pad_bottom - pad_top : 0;
160 unsigned int deconv_pad_bottom = pad_top > pad_bottom ? pad_top - pad_bottom : 0;
161 deconv_pad_y -= deconv_pad_top + deconv_pad_bottom;
162 ARM_COMPUTE_ERROR_ON((deconv_pad_y % 2) != 0);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100163 deconv_pad_top += deconv_pad_y / 2;
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100164 deconv_pad_bottom += deconv_pad_y / 2;
giuros014a8ec802019-03-18 13:25:05 +0000165
166 TensorInfo scale_out_info(scale_out_shape, 1, input->info()->data_type(), input->info()->quantization_info());
167 scale_out_info.set_data_layout(data_layout);
168 _scaled_output.allocator()->init(scale_out_info);
169
170 // configure scale function
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100171 const PadStrideInfo upsample_info(stride_x, stride_y, deconv_pad_left, deconv_pad_right, deconv_pad_top, deconv_pad_bottom, DimensionRoundingType::FLOOR);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100172 _scale_f.configure(compile_context, input, &_scaled_output, upsample_info);
giuros014a8ec802019-03-18 13:25:05 +0000173
Georgios Pinitasdbfc2dc2019-04-02 12:51:21 +0100174 // Setup the function to convolve the upscaled output
giuros014a8ec802019-03-18 13:25:05 +0000175 const PadStrideInfo conv_info(1, 1, 0, 0, 0, 0, DimensionRoundingType::CEIL);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100176 _conv_f.configure(compile_context, &_scaled_output, &_weights_flipped, bias, output, conv_info, weights_info);
giuros014a8ec802019-03-18 13:25:05 +0000177 _scaled_output.allocator()->allocate();
Georgios Pinitasdbfc2dc2019-04-02 12:51:21 +0100178
179 // Setup flip axis data
180 _flip_axis.allocator()->allocate();
181 _flip_axis.map(true);
182 auto axis_data = reinterpret_cast<uint32_t *>(_flip_axis.buffer());
giuros0146a49a02019-04-01 13:50:22 +0100183 if(weights->info()->data_layout() == DataLayout::NHWC)
184 {
185 axis_data[0] = 1;
186 axis_data[1] = 2;
187 }
188 else
189 {
190 axis_data[0] = 0;
191 axis_data[1] = 1;
192 }
Georgios Pinitasdbfc2dc2019-04-02 12:51:21 +0100193 _flip_axis.unmap();
giuros014a8ec802019-03-18 13:25:05 +0000194}
195
196void CLDirectDeconvolutionLayer::run()
197{
198 prepare();
199
Georgios Pinitasda953f22019-04-02 17:27:03 +0100200 MemoryGroupResourceScope scope_mg(_memory_group);
giuros014a8ec802019-03-18 13:25:05 +0000201
202 _scale_f.run();
203 _conv_f.run();
giuros014a8ec802019-03-18 13:25:05 +0000204}
205
206void CLDirectDeconvolutionLayer::prepare()
207{
208 if(!_is_prepared)
209 {
210 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
211
212 // Run weights flipping and mark original weights tensor as unused
213 _weights_flipped.allocator()->allocate();
Georgios Pinitasdbfc2dc2019-04-02 12:51:21 +0100214 _flip_weights.run();
giuros014a8ec802019-03-18 13:25:05 +0000215 _original_weights->mark_as_unused();
216
217 // Prepare convolution
218 _conv_f.prepare();
219
Georgios Pinitasdbfc2dc2019-04-02 12:51:21 +0100220 // Free flipped weights
giuros014a8ec802019-03-18 13:25:05 +0000221 if(!_weights_flipped.is_used())
222 {
223 _weights_flipped.allocator()->free();
224 }
225
226 _is_prepared = true;
227 }
228}
229} // namespace arm_compute