1 *c217d954SCole Faust /*
2 *c217d954SCole Faust * Copyright (c) 2019-2021 Arm Limited.
3 *c217d954SCole Faust *
4 *c217d954SCole Faust * SPDX-License-Identifier: MIT
5 *c217d954SCole Faust *
6 *c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy
7 *c217d954SCole Faust * of this software and associated documentation files (the "Software"), to
8 *c217d954SCole Faust * deal in the Software without restriction, including without limitation the
9 *c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 *c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is
11 *c217d954SCole Faust * furnished to do so, subject to the following conditions:
12 *c217d954SCole Faust *
13 *c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all
14 *c217d954SCole Faust * copies or substantial portions of the Software.
15 *c217d954SCole Faust *
16 *c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 *c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 *c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 *c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 *c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 *c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 *c217d954SCole Faust * SOFTWARE.
23 *c217d954SCole Faust */
24 *c217d954SCole Faust #include "arm_compute/runtime/CL/functions/CLDirectDeconvolutionLayer.h"
25 *c217d954SCole Faust
26 *c217d954SCole Faust #include "arm_compute/core/CL/CLKernelLibrary.h"
27 *c217d954SCole Faust #include "arm_compute/core/Helpers.h"
28 *c217d954SCole Faust #include "arm_compute/core/Utils.h"
29 *c217d954SCole Faust #include "arm_compute/core/Validate.h"
30 *c217d954SCole Faust #include "arm_compute/core/utils/misc/ShapeCalculator.h"
31 *c217d954SCole Faust #include "arm_compute/runtime/CL/CLScheduler.h"
32 *c217d954SCole Faust #include "src/core/CL/kernels/CLDeconvolutionLayerUpsampleKernel.h"
33 *c217d954SCole Faust #include "src/core/CL/kernels/CLFillBorderKernel.h"
34 *c217d954SCole Faust #include "src/core/helpers/AutoConfiguration.h"
35 *c217d954SCole Faust
36 *c217d954SCole Faust #include "src/common/utils/Log.h"
37 *c217d954SCole Faust
38 *c217d954SCole Faust #include <memory>
39 *c217d954SCole Faust #include <tuple>
40 *c217d954SCole Faust
41 *c217d954SCole Faust namespace arm_compute
42 *c217d954SCole Faust {
43 *c217d954SCole Faust using namespace arm_compute::misc::shape_calculator;
44 *c217d954SCole Faust
CLDirectDeconvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)45 *c217d954SCole Faust CLDirectDeconvolutionLayer::CLDirectDeconvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) // NOLINT
46 *c217d954SCole Faust : _memory_group(std::move(memory_manager)),
47 *c217d954SCole Faust _scale_f(),
48 *c217d954SCole Faust _conv_f(),
49 *c217d954SCole Faust _flip_weights(),
50 *c217d954SCole Faust _scaled_output(),
51 *c217d954SCole Faust _original_weights(nullptr),
52 *c217d954SCole Faust _weights_flipped(),
53 *c217d954SCole Faust _flip_axis(),
54 *c217d954SCole Faust _is_prepared(false)
55 *c217d954SCole Faust {
56 *c217d954SCole Faust }
57 *c217d954SCole Faust
validate(const ITensorInfo * input,const ITensorInfo * weights,const ITensorInfo * bias,ITensorInfo * output,const PadStrideInfo & info,const WeightsInfo & weights_info)58 *c217d954SCole Faust Status CLDirectDeconvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *bias, ITensorInfo *output, const PadStrideInfo &info,
59 *c217d954SCole Faust const WeightsInfo &weights_info)
60 *c217d954SCole Faust {
61 *c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
62 *c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8_SIGNED, DataType::QASYMM8, DataType::F16, DataType::F32);
63 *c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, weights);
64 *c217d954SCole Faust const DataLayout data_layout = input->data_layout();
65 *c217d954SCole Faust
66 *c217d954SCole Faust const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
67 *c217d954SCole Faust const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
68 *c217d954SCole Faust const size_t idx_c = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
69 *c217d954SCole Faust
70 *c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_w) != weights->dimension(idx_h));
71 *c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_w) < 1);
72 *c217d954SCole Faust
73 *c217d954SCole Faust auto out_dims = deconvolution_output_dimensions(input->dimension(idx_w), input->dimension(idx_h), weights->dimension(idx_w), weights->dimension(idx_h), info);
74 *c217d954SCole Faust
75 *c217d954SCole Faust const TensorShape output_shape = compute_deconvolution_output_shape(out_dims, *input, *weights);
76 *c217d954SCole Faust
77 *c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
78 *c217d954SCole Faust
79 *c217d954SCole Faust if(input->data_type() != weights->data_type())
80 *c217d954SCole Faust {
81 *c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(weights->data_type() != DataType::QSYMM8_PER_CHANNEL || !is_data_type_quantized_asymmetric(input->data_type()));
82 *c217d954SCole Faust }
83 *c217d954SCole Faust
84 *c217d954SCole Faust if(bias != nullptr)
85 *c217d954SCole Faust {
86 *c217d954SCole Faust if(is_data_type_quantized_asymmetric(input->data_type()))
87 *c217d954SCole Faust {
88 *c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(bias, 1, DataType::S32);
89 *c217d954SCole Faust }
90 *c217d954SCole Faust else
91 *c217d954SCole Faust {
92 *c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, bias);
93 *c217d954SCole Faust }
94 *c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, bias);
95 *c217d954SCole Faust }
96 *c217d954SCole Faust
97 *c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->dimension(idx_w) != output_shape[idx_w], "Output's width is invalid.");
98 *c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->dimension(idx_h) != output_shape[idx_h], "Output's height is invalid.");
99 *c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->dimension(idx_c) != output_shape[idx_c], "Output's depth is invalid.");
100 *c217d954SCole Faust
101 *c217d954SCole Faust unsigned int deconv_pad_x = 0;
102 *c217d954SCole Faust unsigned int deconv_pad_y = 0;
103 *c217d954SCole Faust const unsigned int stride_x = info.stride().first;
104 *c217d954SCole Faust const unsigned int stride_y = info.stride().second;
105 *c217d954SCole Faust const TensorShape scale_out_shape = compute_deconvolution_upsampled_shape(*input, *weights, stride_x, stride_y, out_dims, deconv_pad_x, deconv_pad_y);
106 *c217d954SCole Faust TensorInfo scale_out_info(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(scale_out_shape).set_data_layout(data_layout));
107 *c217d954SCole Faust const PadStrideInfo conv_info(1, 1, 0, 0, 0, 0, DimensionRoundingType::CEIL);
108 *c217d954SCole Faust
109 *c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLDeconvolutionLayerUpsample::validate(input, &scale_out_info, info));
110 *c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLConvolutionLayer::validate(&scale_out_info, weights, bias, output, conv_info, weights_info));
111 *c217d954SCole Faust
112 *c217d954SCole Faust return Status{};
113 *c217d954SCole Faust }
114 *c217d954SCole Faust
configure(ICLTensor * input,ICLTensor * weights,const ICLTensor * bias,ICLTensor * output,const PadStrideInfo & info,const WeightsInfo & weights_info)115 *c217d954SCole Faust void CLDirectDeconvolutionLayer::configure(ICLTensor *input, ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const PadStrideInfo &info,
116 *c217d954SCole Faust const WeightsInfo &weights_info)
117 *c217d954SCole Faust {
118 *c217d954SCole Faust configure(CLKernelLibrary::get().get_compile_context(), input, weights, bias, output, info, weights_info);
119 *c217d954SCole Faust }
120 *c217d954SCole Faust
configure(const CLCompileContext & compile_context,ICLTensor * input,ICLTensor * weights,const ICLTensor * bias,ICLTensor * output,const PadStrideInfo & info,const WeightsInfo & weights_info)121 *c217d954SCole Faust void CLDirectDeconvolutionLayer::configure(const CLCompileContext &compile_context, ICLTensor *input, ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const PadStrideInfo &info,
122 *c217d954SCole Faust const WeightsInfo &weights_info)
123 *c217d954SCole Faust {
124 *c217d954SCole Faust ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
125 *c217d954SCole Faust ARM_COMPUTE_LOG_PARAMS(input, weights, bias, output, info, weights_info);
126 *c217d954SCole Faust
127 *c217d954SCole Faust const unsigned int pad_left = info.pad_left();
128 *c217d954SCole Faust const unsigned int pad_right = info.pad_right();
129 *c217d954SCole Faust const unsigned int pad_top = info.pad_top();
130 *c217d954SCole Faust const unsigned int pad_bottom = info.pad_bottom();
131 *c217d954SCole Faust const unsigned int stride_x = info.stride().first;
132 *c217d954SCole Faust const unsigned int stride_y = info.stride().second;
133 *c217d954SCole Faust
134 *c217d954SCole Faust const DataLayout data_layout = input->info()->data_layout();
135 *c217d954SCole Faust
136 *c217d954SCole Faust const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
137 *c217d954SCole Faust const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
138 *c217d954SCole Faust
139 *c217d954SCole Faust _original_weights = weights;
140 *c217d954SCole Faust _flip_axis.allocator()->init(TensorInfo(TensorShape(2U), 1, DataType::U32));
141 *c217d954SCole Faust _weights_flipped.allocator()->init(weights->info()->clone()->set_data_layout(data_layout));
142 *c217d954SCole Faust _flip_weights.configure(compile_context, weights, &_weights_flipped, &_flip_axis);
143 *c217d954SCole Faust
144 *c217d954SCole Faust 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);
145 *c217d954SCole Faust
146 *c217d954SCole Faust const TensorShape output_shape = compute_deconvolution_output_shape(out_dims, *input->info(), *weights->info());
147 *c217d954SCole Faust
148 *c217d954SCole Faust // Output auto initialization if not yet initialized
149 *c217d954SCole Faust auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape).set_data_layout(data_layout));
150 *c217d954SCole Faust
151 *c217d954SCole Faust // Perform validation step
152 *c217d954SCole Faust ARM_COMPUTE_ERROR_THROW_ON(CLDirectDeconvolutionLayer::validate(input->info(), weights->info(), bias == nullptr ? nullptr : bias->info(), output->info(), info));
153 *c217d954SCole Faust
154 *c217d954SCole Faust _is_prepared = weights_info.retain_internal_weights();
155 *c217d954SCole Faust
156 *c217d954SCole Faust _memory_group.manage(&_scaled_output);
157 *c217d954SCole Faust
158 *c217d954SCole Faust // Find the upsampled dimensions and the padding needed for the convolution with stride 1 in order to match output shape
159 *c217d954SCole Faust unsigned int deconv_pad_x = 0;
160 *c217d954SCole Faust unsigned int deconv_pad_y = 0;
161 *c217d954SCole Faust 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);
162 *c217d954SCole Faust
163 *c217d954SCole Faust unsigned int deconv_pad_left = pad_right > pad_left ? pad_right - pad_left : 0;
164 *c217d954SCole Faust unsigned int deconv_pad_right = pad_left > pad_right ? pad_left - pad_right : 0;
165 *c217d954SCole Faust deconv_pad_x -= deconv_pad_left + deconv_pad_right;
166 *c217d954SCole Faust ARM_COMPUTE_ERROR_ON((deconv_pad_x % 2) != 0);
167 *c217d954SCole Faust deconv_pad_left += deconv_pad_x / 2;
168 *c217d954SCole Faust deconv_pad_right += deconv_pad_x / 2;
169 *c217d954SCole Faust
170 *c217d954SCole Faust unsigned int deconv_pad_top = pad_bottom > pad_top ? pad_bottom - pad_top : 0;
171 *c217d954SCole Faust unsigned int deconv_pad_bottom = pad_top > pad_bottom ? pad_top - pad_bottom : 0;
172 *c217d954SCole Faust deconv_pad_y -= deconv_pad_top + deconv_pad_bottom;
173 *c217d954SCole Faust ARM_COMPUTE_ERROR_ON((deconv_pad_y % 2) != 0);
174 *c217d954SCole Faust deconv_pad_top += deconv_pad_y / 2;
175 *c217d954SCole Faust deconv_pad_bottom += deconv_pad_y / 2;
176 *c217d954SCole Faust
177 *c217d954SCole Faust TensorInfo scale_out_info(scale_out_shape, 1, input->info()->data_type(), input->info()->quantization_info());
178 *c217d954SCole Faust scale_out_info.set_data_layout(data_layout);
179 *c217d954SCole Faust _scaled_output.allocator()->init(scale_out_info);
180 *c217d954SCole Faust
181 *c217d954SCole Faust // configure scale function
182 *c217d954SCole Faust const PadStrideInfo upsample_info(stride_x, stride_y, deconv_pad_left, deconv_pad_right, deconv_pad_top, deconv_pad_bottom, DimensionRoundingType::FLOOR);
183 *c217d954SCole Faust _scale_f.configure(compile_context, input, &_scaled_output, upsample_info);
184 *c217d954SCole Faust
185 *c217d954SCole Faust // Setup the function to convolve the upscaled output
186 *c217d954SCole Faust const PadStrideInfo conv_info(1, 1, 0, 0, 0, 0, DimensionRoundingType::CEIL);
187 *c217d954SCole Faust _conv_f.configure(compile_context, &_scaled_output, &_weights_flipped, bias, output, conv_info, weights_info);
188 *c217d954SCole Faust _scaled_output.allocator()->allocate();
189 *c217d954SCole Faust
190 *c217d954SCole Faust // Setup flip axis data
191 *c217d954SCole Faust _flip_axis.allocator()->allocate();
192 *c217d954SCole Faust _flip_axis.map(true);
193 *c217d954SCole Faust auto axis_data = reinterpret_cast<uint32_t *>(_flip_axis.buffer());
194 *c217d954SCole Faust if(weights->info()->data_layout() == DataLayout::NHWC)
195 *c217d954SCole Faust {
196 *c217d954SCole Faust axis_data[0] = 1;
197 *c217d954SCole Faust axis_data[1] = 2;
198 *c217d954SCole Faust }
199 *c217d954SCole Faust else
200 *c217d954SCole Faust {
201 *c217d954SCole Faust axis_data[0] = 0;
202 *c217d954SCole Faust axis_data[1] = 1;
203 *c217d954SCole Faust }
204 *c217d954SCole Faust _flip_axis.unmap();
205 *c217d954SCole Faust }
206 *c217d954SCole Faust
run()207 *c217d954SCole Faust void CLDirectDeconvolutionLayer::run()
208 *c217d954SCole Faust {
209 *c217d954SCole Faust prepare();
210 *c217d954SCole Faust
211 *c217d954SCole Faust MemoryGroupResourceScope scope_mg(_memory_group);
212 *c217d954SCole Faust
213 *c217d954SCole Faust _scale_f.run();
214 *c217d954SCole Faust _conv_f.run();
215 *c217d954SCole Faust }
216 *c217d954SCole Faust
prepare()217 *c217d954SCole Faust void CLDirectDeconvolutionLayer::prepare()
218 *c217d954SCole Faust {
219 *c217d954SCole Faust if(!_is_prepared)
220 *c217d954SCole Faust {
221 *c217d954SCole Faust ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
222 *c217d954SCole Faust
223 *c217d954SCole Faust // Run weights flipping and mark original weights tensor as unused
224 *c217d954SCole Faust _weights_flipped.allocator()->allocate();
225 *c217d954SCole Faust _flip_weights.run();
226 *c217d954SCole Faust _original_weights->mark_as_unused();
227 *c217d954SCole Faust
228 *c217d954SCole Faust // Prepare convolution
229 *c217d954SCole Faust _conv_f.prepare();
230 *c217d954SCole Faust
231 *c217d954SCole Faust // Free flipped weights
232 *c217d954SCole Faust if(!_weights_flipped.is_used())
233 *c217d954SCole Faust {
234 *c217d954SCole Faust _weights_flipped.allocator()->free();
235 *c217d954SCole Faust }
236 *c217d954SCole Faust _is_prepared = true;
237 *c217d954SCole Faust }
238 *c217d954SCole Faust }
239 *c217d954SCole Faust } // namespace arm_compute
240