1 /*
2 * Copyright (c) 2019-2020 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/CL/CLKernelLibrary.h"
27 #include "arm_compute/core/Helpers.h"
28 #include "arm_compute/core/Utils.h"
29 #include "arm_compute/core/Validate.h"
30 #include "arm_compute/core/utils/misc/ShapeCalculator.h"
31 #include "arm_compute/runtime/CL/CLScheduler.h"
32 #include "src/core/CL/kernels/CLDeconvolutionLayerUpsampleKernel.h"
33 #include "src/core/CL/kernels/CLFillBorderKernel.h"
34 #include "src/core/CL/kernels/CLGEMMReshapeRHSMatrixKernel.h"
35 #include "src/core/CL/kernels/CLMemsetKernel.h"
36 #include "src/core/CL/kernels/CLWeightsReshapeKernel.h"
37 #include "src/core/helpers/AutoConfiguration.h"
38
39 #include <memory>
40 #include <tuple>
41
42 namespace arm_compute
43 {
44 using namespace arm_compute::misc::shape_calculator;
45
CLDirectDeconvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)46 CLDirectDeconvolutionLayer::CLDirectDeconvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) // NOLINT
47 : _memory_group(std::move(memory_manager)),
48 _scale_f(),
49 _conv_f(),
50 _flip_weights(),
51 _scaled_output(),
52 _original_weights(nullptr),
53 _weights_flipped(),
54 _flip_axis(),
55 _is_prepared(false)
56 {
57 }
58
validate(const ITensorInfo * input,const ITensorInfo * weights,const ITensorInfo * bias,ITensorInfo * output,const PadStrideInfo & info,const WeightsInfo & weights_info)59 Status CLDirectDeconvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *bias, ITensorInfo *output, const PadStrideInfo &info,
60 const WeightsInfo &weights_info)
61 {
62 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
63 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8_SIGNED, DataType::QASYMM8, DataType::F16, DataType::F32);
64 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, weights);
65 const DataLayout data_layout = input->data_layout();
66
67 const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
68 const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
69 const size_t idx_c = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
70
71 ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_w) != weights->dimension(idx_h));
72 ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_w) < 1);
73
74 auto out_dims = deconvolution_output_dimensions(input->dimension(idx_w), input->dimension(idx_h), weights->dimension(idx_w), weights->dimension(idx_h), info);
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 deconv_pad_x = 0;
98 unsigned int deconv_pad_y = 0;
99 const unsigned int stride_x = info.stride().first;
100 const unsigned int stride_y = info.stride().second;
101 const TensorShape scale_out_shape = compute_deconvolution_upsampled_shape(*input, *weights, stride_x, stride_y, out_dims, deconv_pad_x, deconv_pad_y);
102 TensorInfo scale_out_info(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(scale_out_shape).set_data_layout(data_layout));
103 const PadStrideInfo conv_info(1, 1, 0, 0, 0, 0, DimensionRoundingType::CEIL);
104
105 ARM_COMPUTE_RETURN_ON_ERROR(CLDeconvolutionLayerUpsample::validate(input, &scale_out_info, info));
106 ARM_COMPUTE_RETURN_ON_ERROR(CLConvolutionLayer::validate(&scale_out_info, weights, bias, output, conv_info, weights_info));
107
108 return Status{};
109 }
110
configure(ICLTensor * input,ICLTensor * weights,const ICLTensor * bias,ICLTensor * output,const PadStrideInfo & info,const WeightsInfo & weights_info)111 void CLDirectDeconvolutionLayer::configure(ICLTensor *input, ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const PadStrideInfo &info,
112 const WeightsInfo &weights_info)
113 {
114 configure(CLKernelLibrary::get().get_compile_context(), input, weights, bias, output, info, weights_info);
115 }
116
configure(const CLCompileContext & compile_context,ICLTensor * input,ICLTensor * weights,const ICLTensor * bias,ICLTensor * output,const PadStrideInfo & info,const WeightsInfo & weights_info)117 void CLDirectDeconvolutionLayer::configure(const CLCompileContext &compile_context, ICLTensor *input, ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const PadStrideInfo &info,
118 const WeightsInfo &weights_info)
119 {
120 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
121
122 const unsigned int pad_left = info.pad_left();
123 const unsigned int pad_right = info.pad_right();
124 const unsigned int pad_top = info.pad_top();
125 const unsigned int pad_bottom = info.pad_bottom();
126 const unsigned int stride_x = info.stride().first;
127 const unsigned int stride_y = info.stride().second;
128
129 const DataLayout data_layout = input->info()->data_layout();
130
131 const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
132 const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
133
134 _original_weights = weights;
135 _flip_axis.allocator()->init(TensorInfo(TensorShape(2U), 1, DataType::U32));
136 _weights_flipped.allocator()->init(weights->info()->clone()->set_data_layout(data_layout));
137 _flip_weights.configure(compile_context, weights, &_weights_flipped, &_flip_axis);
138
139 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);
140
141 const TensorShape output_shape = compute_deconvolution_output_shape(out_dims, *input->info(), *weights->info());
142
143 // Output auto initialization if not yet initialized
144 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape).set_data_layout(data_layout));
145
146 // Perform validation step
147 ARM_COMPUTE_ERROR_THROW_ON(CLDirectDeconvolutionLayer::validate(input->info(), weights->info(), bias == nullptr ? nullptr : bias->info(), output->info(), info));
148
149 _is_prepared = weights_info.retain_internal_weights();
150
151 _memory_group.manage(&_scaled_output);
152
153 // Find the upsampled dimensions and the padding needed for the convolution with stride 1 in order to match output shape
154 unsigned int deconv_pad_x = 0;
155 unsigned int deconv_pad_y = 0;
156 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);
157
158 unsigned int deconv_pad_left = pad_right > pad_left ? pad_right - pad_left : 0;
159 unsigned int deconv_pad_right = pad_left > pad_right ? pad_left - pad_right : 0;
160 deconv_pad_x -= deconv_pad_left + deconv_pad_right;
161 ARM_COMPUTE_ERROR_ON((deconv_pad_x % 2) != 0);
162 deconv_pad_left += deconv_pad_x / 2;
163 deconv_pad_right += deconv_pad_x / 2;
164
165 unsigned int deconv_pad_top = pad_bottom > pad_top ? pad_bottom - pad_top : 0;
166 unsigned int deconv_pad_bottom = pad_top > pad_bottom ? pad_top - pad_bottom : 0;
167 deconv_pad_y -= deconv_pad_top + deconv_pad_bottom;
168 ARM_COMPUTE_ERROR_ON((deconv_pad_y % 2) != 0);
169 deconv_pad_top += deconv_pad_y / 2;
170 deconv_pad_bottom += deconv_pad_y / 2;
171
172 TensorInfo scale_out_info(scale_out_shape, 1, input->info()->data_type(), input->info()->quantization_info());
173 scale_out_info.set_data_layout(data_layout);
174 _scaled_output.allocator()->init(scale_out_info);
175
176 // configure scale function
177 const PadStrideInfo upsample_info(stride_x, stride_y, deconv_pad_left, deconv_pad_right, deconv_pad_top, deconv_pad_bottom, DimensionRoundingType::FLOOR);
178 _scale_f.configure(compile_context, input, &_scaled_output, upsample_info);
179
180 // Setup the function to convolve the upscaled output
181 const PadStrideInfo conv_info(1, 1, 0, 0, 0, 0, DimensionRoundingType::CEIL);
182 _conv_f.configure(compile_context, &_scaled_output, &_weights_flipped, bias, output, conv_info, weights_info);
183 _scaled_output.allocator()->allocate();
184
185 // Setup flip axis data
186 _flip_axis.allocator()->allocate();
187 _flip_axis.map(true);
188 auto axis_data = reinterpret_cast<uint32_t *>(_flip_axis.buffer());
189 if(weights->info()->data_layout() == DataLayout::NHWC)
190 {
191 axis_data[0] = 1;
192 axis_data[1] = 2;
193 }
194 else
195 {
196 axis_data[0] = 0;
197 axis_data[1] = 1;
198 }
199 _flip_axis.unmap();
200 }
201
run()202 void CLDirectDeconvolutionLayer::run()
203 {
204 prepare();
205
206 MemoryGroupResourceScope scope_mg(_memory_group);
207
208 _scale_f.run();
209 _conv_f.run();
210 }
211
prepare()212 void CLDirectDeconvolutionLayer::prepare()
213 {
214 if(!_is_prepared)
215 {
216 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
217
218 // Run weights flipping and mark original weights tensor as unused
219 _weights_flipped.allocator()->allocate();
220 _flip_weights.run();
221 _original_weights->mark_as_unused();
222
223 // Prepare convolution
224 _conv_f.prepare();
225
226 // Free flipped weights
227 if(!_weights_flipped.is_used())
228 {
229 _weights_flipped.allocator()->free();
230 }
231
232 _is_prepared = true;
233 }
234 }
235 } // namespace arm_compute
236