xref: /aosp_15_r20/external/ComputeLibrary/src/runtime/CL/functions/CLGEMMDeconvolutionLayer.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
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/CLGEMMDeconvolutionLayer.h"
25*c217d954SCole Faust 
26*c217d954SCole Faust #include "arm_compute/core/Helpers.h"
27*c217d954SCole Faust #include "arm_compute/core/Validate.h"
28*c217d954SCole Faust #include "arm_compute/core/utils/misc/ShapeCalculator.h"
29*c217d954SCole Faust #include "arm_compute/core/utils/quantization/AsymmHelpers.h"
30*c217d954SCole Faust #include "arm_compute/runtime/CL/CLScheduler.h"
31*c217d954SCole Faust #include "src/core/CL/kernels/CLDeconvolutionReshapeOutputKernel.h"
32*c217d954SCole Faust #include "src/core/CL/kernels/CLFillBorderKernel.h"
33*c217d954SCole Faust 
34*c217d954SCole Faust #include "src/common/utils/Log.h"
35*c217d954SCole Faust 
36*c217d954SCole Faust #include <tuple>
37*c217d954SCole Faust 
38*c217d954SCole Faust namespace arm_compute
39*c217d954SCole Faust {
40*c217d954SCole Faust namespace
41*c217d954SCole Faust {
compute_start_end_slice_coordinates(const ITensorInfo & output_info,const PadStrideInfo & deconv_info,bool is_nchw)42*c217d954SCole Faust std::pair<Coordinates, Coordinates> compute_start_end_slice_coordinates(const ITensorInfo &output_info, const PadStrideInfo &deconv_info, bool is_nchw)
43*c217d954SCole Faust {
44*c217d954SCole Faust     Coordinates start;
45*c217d954SCole Faust     Coordinates end;
46*c217d954SCole Faust 
47*c217d954SCole Faust     if(is_nchw)
48*c217d954SCole Faust     {
49*c217d954SCole Faust         start.set(0, deconv_info.pad_left());
50*c217d954SCole Faust         start.set(1, deconv_info.pad_top());
51*c217d954SCole Faust         end.set(0, output_info.dimension(0) - deconv_info.pad_right());
52*c217d954SCole Faust         end.set(1, output_info.dimension(1) - deconv_info.pad_bottom());
53*c217d954SCole Faust     }
54*c217d954SCole Faust     else
55*c217d954SCole Faust     {
56*c217d954SCole Faust         start.set(0, 0);
57*c217d954SCole Faust         start.set(1, deconv_info.pad_left());
58*c217d954SCole Faust         start.set(2, deconv_info.pad_top());
59*c217d954SCole Faust 
60*c217d954SCole Faust         end.set(0, output_info.dimension(0));
61*c217d954SCole Faust         end.set(1, output_info.dimension(1) - deconv_info.pad_right());
62*c217d954SCole Faust         end.set(2, output_info.dimension(2) - deconv_info.pad_bottom());
63*c217d954SCole Faust     }
64*c217d954SCole Faust 
65*c217d954SCole Faust     return { start, end };
66*c217d954SCole Faust }
construct_gemmlowp_output_stage(const ITensorInfo * input,const ITensorInfo * weights,const ITensorInfo * output,GEMMLowpOutputStageInfo & output_stage_info)67*c217d954SCole Faust Status construct_gemmlowp_output_stage(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output, GEMMLowpOutputStageInfo &output_stage_info)
68*c217d954SCole Faust {
69*c217d954SCole Faust     const auto data_type = input->data_type();
70*c217d954SCole Faust 
71*c217d954SCole Faust     if(is_data_type_quantized_asymmetric(data_type))
72*c217d954SCole Faust     {
73*c217d954SCole Faust         const UniformQuantizationInfo iq_info = input->quantization_info().uniform();
74*c217d954SCole Faust         const UniformQuantizationInfo wq_info = weights->quantization_info().uniform();
75*c217d954SCole Faust         const UniformQuantizationInfo oq_info = output->quantization_info().uniform();
76*c217d954SCole Faust 
77*c217d954SCole Faust         float multiplier = iq_info.scale * wq_info.scale / oq_info.scale;
78*c217d954SCole Faust         int   output_multiplier(0);
79*c217d954SCole Faust         int   output_shift(0);
80*c217d954SCole Faust         ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift));
81*c217d954SCole Faust 
82*c217d954SCole Faust         output_stage_info.type                = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT;
83*c217d954SCole Faust         output_stage_info.gemmlowp_multiplier = output_multiplier;
84*c217d954SCole Faust         output_stage_info.gemmlowp_shift      = output_shift;
85*c217d954SCole Faust         output_stage_info.gemmlowp_offset     = oq_info.offset;
86*c217d954SCole Faust         const auto min_max_bound              = get_min_max(data_type);
87*c217d954SCole Faust         output_stage_info.gemmlowp_min_bound  = (std::get<0>(min_max_bound)).get<int32_t>();
88*c217d954SCole Faust         output_stage_info.gemmlowp_max_bound  = (std::get<1>(min_max_bound)).get<int32_t>();
89*c217d954SCole Faust         output_stage_info.output_data_type    = data_type;
90*c217d954SCole Faust     }
91*c217d954SCole Faust     return Status{};
92*c217d954SCole Faust }
93*c217d954SCole Faust 
94*c217d954SCole Faust } // namespace
95*c217d954SCole Faust 
CLGEMMDeconvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)96*c217d954SCole Faust CLGEMMDeconvolutionLayer::CLGEMMDeconvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) // NOLINT
97*c217d954SCole Faust     : _memory_group(std::move(memory_manager)),
98*c217d954SCole Faust       _mm_gemm(),
99*c217d954SCole Faust       _mm_gemmlowp(),
100*c217d954SCole Faust       _gemmlowp_output_stage(),
101*c217d954SCole Faust       _permute_input_to_nhwc(),
102*c217d954SCole Faust       _permute_weights_to_nhwc(),
103*c217d954SCole Faust       _reshape_weights(),
104*c217d954SCole Faust       _transpose_weights(),
105*c217d954SCole Faust       _deconv_reshape(std::make_unique<CLDeconvolutionReshapeOutputKernel>()),
106*c217d954SCole Faust       _slice_gemm(),
107*c217d954SCole Faust       _gemmlowp_final(),
108*c217d954SCole Faust       _reshaped_weights(),
109*c217d954SCole Faust       _reshaped_weights_t(),
110*c217d954SCole Faust       _permuted_input(),
111*c217d954SCole Faust       _permuted_weights(),
112*c217d954SCole Faust       _gemm_output(),
113*c217d954SCole Faust       _slice_gemm_input(),
114*c217d954SCole Faust       _original_weights(),
115*c217d954SCole Faust       _is_prepared(false),
116*c217d954SCole Faust       _padded_input(false),
117*c217d954SCole Faust       _is_nchw(false),
118*c217d954SCole Faust       _is_quantized(false)
119*c217d954SCole Faust {
120*c217d954SCole Faust }
121*c217d954SCole Faust 
122*c217d954SCole Faust CLGEMMDeconvolutionLayer::~CLGEMMDeconvolutionLayer() = default;
123*c217d954SCole Faust 
validate(const ITensorInfo * input,const ITensorInfo * weights,const ITensorInfo * bias,const ITensorInfo * output,const PadStrideInfo & deconv_info)124*c217d954SCole Faust Status CLGEMMDeconvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *bias, const ITensorInfo *output, const PadStrideInfo &deconv_info)
125*c217d954SCole Faust {
126*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
127*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32, DataType::F16, DataType::QASYMM8, DataType::QASYMM8_SIGNED);
128*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
129*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, weights);
130*c217d954SCole Faust 
131*c217d954SCole Faust     DataLayout data_layout  = input->data_layout();
132*c217d954SCole Faust     const bool padded_input = deconv_info.pad_bottom() > 0 || deconv_info.pad_left() > 0 || deconv_info.pad_right() > 0 || deconv_info.pad_top() > 0;
133*c217d954SCole Faust     const bool is_nchw      = input->data_layout() == DataLayout::NCHW;
134*c217d954SCole Faust     const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
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     const size_t idx_b = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
139*c217d954SCole Faust 
140*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_w) != deconv_info.stride().first);
141*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_h) != deconv_info.stride().second);
142*c217d954SCole Faust 
143*c217d954SCole Faust     TensorShape nhwc_weights_shape = weights->tensor_shape();
144*c217d954SCole Faust     TensorShape nhwc_input_shape   = input->tensor_shape();
145*c217d954SCole Faust 
146*c217d954SCole Faust     if(is_nchw)
147*c217d954SCole Faust     {
148*c217d954SCole Faust         permute(nhwc_weights_shape, PermutationVector(2, 0, 1));
149*c217d954SCole Faust         permute(nhwc_input_shape, PermutationVector(2, 0, 1));
150*c217d954SCole Faust 
151*c217d954SCole Faust         TensorInfo nhwc_input_info = input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(nhwc_input_shape).set_data_layout(DataLayout::NCHW);
152*c217d954SCole Faust 
153*c217d954SCole Faust         TensorInfo nhwc_weights_info = weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(nhwc_weights_shape).set_data_layout(DataLayout::NCHW);
154*c217d954SCole Faust 
155*c217d954SCole Faust         CLPermute::validate(weights, &nhwc_weights_info, PermutationVector(2, 0, 1));
156*c217d954SCole Faust         CLPermute::validate(input, &nhwc_input_info, PermutationVector(2, 0, 1));
157*c217d954SCole Faust     }
158*c217d954SCole Faust 
159*c217d954SCole Faust     const TensorShape reshaped_shape = TensorShape(nhwc_weights_shape[0], nhwc_weights_shape[1] * nhwc_weights_shape[2] * nhwc_weights_shape[3]);
160*c217d954SCole Faust     const TensorInfo  reshaped_info  = weights->clone()->set_tensor_shape(reshaped_shape).set_data_layout(DataLayout::NCHW).set_is_resizable(true);
161*c217d954SCole Faust     ARM_COMPUTE_RETURN_ON_ERROR(CLReshapeLayer::validate(weights, &reshaped_info));
162*c217d954SCole Faust 
163*c217d954SCole Faust     TensorShape      transposed_shape(reshaped_shape[1], reshaped_shape[0]);
164*c217d954SCole Faust     const TensorInfo reshaped_t_info = reshaped_info.clone()->set_is_resizable(true).set_tensor_shape(transposed_shape);
165*c217d954SCole Faust     ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(&reshaped_info, &reshaped_t_info));
166*c217d954SCole Faust 
167*c217d954SCole Faust     TensorShape gemm_output_shape(weights->dimension(idx_w) * weights->dimension(idx_h) * weights->dimension(idx_b),
168*c217d954SCole Faust                                   input->dimension(idx_w),
169*c217d954SCole Faust                                   input->dimension(idx_h),
170*c217d954SCole Faust                                   input->dimension(idx_b));
171*c217d954SCole Faust 
172*c217d954SCole Faust     TensorInfo gemm_output_info = reshaped_t_info.clone()->set_tensor_shape(gemm_output_shape).set_is_resizable(true);
173*c217d954SCole Faust     GEMMInfo   gemm_info(false, false, true, input->dimension(idx_h), true);
174*c217d954SCole Faust 
175*c217d954SCole Faust     GEMMLowpOutputStageInfo output_stage_info;
176*c217d954SCole Faust 
177*c217d954SCole Faust     if(is_quantized)
178*c217d954SCole Faust     {
179*c217d954SCole Faust         ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyCore::validate(&input->clone()->set_tensor_shape(nhwc_input_shape), &reshaped_t_info, nullptr, &gemm_output_info.set_data_type(DataType::S32),
180*c217d954SCole Faust                                                                            gemm_info));
181*c217d954SCole Faust         ARM_COMPUTE_RETURN_ON_ERROR(construct_gemmlowp_output_stage(input, weights, output, output_stage_info));
182*c217d954SCole Faust     }
183*c217d954SCole Faust     else
184*c217d954SCole Faust     {
185*c217d954SCole Faust         ARM_COMPUTE_RETURN_ON_ERROR(CLGEMM::validate(&input->clone()->set_tensor_shape(nhwc_input_shape).set_is_resizable(true), &reshaped_t_info, nullptr, &gemm_output_info, 1.0f, 0.0f, gemm_info));
186*c217d954SCole Faust     }
187*c217d954SCole Faust 
188*c217d954SCole Faust     const PadStrideInfo stride_info(deconv_info.stride().first, deconv_info.stride().second);
189*c217d954SCole Faust     auto                out_dims           = deconvolution_output_dimensions(input->dimension(idx_w), input->dimension(idx_h), weights->dimension(idx_w), weights->dimension(idx_h), stride_info);
190*c217d954SCole Faust     const TensorShape   deconv_shape       = misc::shape_calculator::compute_deconvolution_output_shape(out_dims, *input, *weights);
191*c217d954SCole Faust     TensorInfo          col2im_output_info = gemm_output_info.clone()->set_tensor_shape(deconv_shape).set_is_resizable(true);
192*c217d954SCole Faust 
193*c217d954SCole Faust     if(padded_input && is_quantized)
194*c217d954SCole Faust     {
195*c217d954SCole Faust         const auto start_end = compute_start_end_slice_coordinates(col2im_output_info, deconv_info, is_nchw);
196*c217d954SCole Faust         ARM_COMPUTE_RETURN_ON_ERROR(CLDeconvolutionReshapeOutputKernel::validate(&gemm_output_info, bias, &col2im_output_info, input, weights, deconv_info));
197*c217d954SCole Faust         ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpOutputStage::validate(&col2im_output_info, nullptr, &col2im_output_info.clone()->set_is_resizable(true).set_data_type(input->data_type()), output_stage_info));
198*c217d954SCole Faust         ARM_COMPUTE_RETURN_ON_ERROR(CLSlice::validate(&col2im_output_info.clone()->set_is_resizable(true).set_data_type(input->data_type()), output, start_end.first, start_end.second));
199*c217d954SCole Faust     }
200*c217d954SCole Faust     else if(padded_input)
201*c217d954SCole Faust     {
202*c217d954SCole Faust         const auto start_end = compute_start_end_slice_coordinates(col2im_output_info, deconv_info, is_nchw);
203*c217d954SCole Faust         ARM_COMPUTE_RETURN_ON_ERROR(CLDeconvolutionReshapeOutputKernel::validate(&gemm_output_info, bias, &col2im_output_info, input, weights, deconv_info));
204*c217d954SCole Faust         ARM_COMPUTE_RETURN_ON_ERROR(CLSlice::validate(&col2im_output_info, output, start_end.first, start_end.second));
205*c217d954SCole Faust     }
206*c217d954SCole Faust     else if(is_quantized)
207*c217d954SCole Faust     {
208*c217d954SCole Faust         ARM_COMPUTE_RETURN_ON_ERROR(CLDeconvolutionReshapeOutputKernel::validate(&gemm_output_info, bias, &col2im_output_info, input, weights, deconv_info));
209*c217d954SCole Faust         ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpOutputStage::validate(&col2im_output_info, nullptr, output, output_stage_info));
210*c217d954SCole Faust     }
211*c217d954SCole Faust     else
212*c217d954SCole Faust     {
213*c217d954SCole Faust         ARM_COMPUTE_RETURN_ON_ERROR(CLDeconvolutionReshapeOutputKernel::validate(&gemm_output_info, bias, output, input, weights, deconv_info));
214*c217d954SCole Faust     }
215*c217d954SCole Faust 
216*c217d954SCole Faust     return Status{};
217*c217d954SCole Faust }
218*c217d954SCole Faust 
configure(const ICLTensor * input,const ICLTensor * weights,const ICLTensor * bias,ICLTensor * output,const PadStrideInfo & deconv_info)219*c217d954SCole Faust void CLGEMMDeconvolutionLayer::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const PadStrideInfo &deconv_info)
220*c217d954SCole Faust {
221*c217d954SCole Faust     configure(CLKernelLibrary::get().get_compile_context(), input, weights, bias, output, deconv_info);
222*c217d954SCole Faust }
223*c217d954SCole Faust 
configure(const CLCompileContext & compile_context,const ICLTensor * input,const ICLTensor * weights,const ICLTensor * bias,ICLTensor * output,const PadStrideInfo & deconv_info)224*c217d954SCole Faust void CLGEMMDeconvolutionLayer::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *bias, ICLTensor *output,
225*c217d954SCole Faust                                          const PadStrideInfo &deconv_info)
226*c217d954SCole Faust {
227*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
228*c217d954SCole Faust     ARM_COMPUTE_ERROR_THROW_ON(CLGEMMDeconvolutionLayer::validate(input->info(),
229*c217d954SCole Faust                                                                   weights->info(),
230*c217d954SCole Faust                                                                   bias != nullptr ? bias->info() : nullptr,
231*c217d954SCole Faust                                                                   output->info(),
232*c217d954SCole Faust                                                                   deconv_info));
233*c217d954SCole Faust     ARM_COMPUTE_LOG_PARAMS(input, weights, bias, output, deconv_info);
234*c217d954SCole Faust 
235*c217d954SCole Faust     _original_weights = weights;
236*c217d954SCole Faust     _padded_input     = deconv_info.pad_bottom() > 0 || deconv_info.pad_left() > 0 || deconv_info.pad_right() > 0 || deconv_info.pad_top() > 0;
237*c217d954SCole Faust     _is_nchw          = input->info()->data_layout() == DataLayout::NCHW;
238*c217d954SCole Faust     _is_quantized     = is_data_type_quantized_asymmetric(input->info()->data_type());
239*c217d954SCole Faust 
240*c217d954SCole Faust     const ICLTensor *input_to_use   = input;
241*c217d954SCole Faust     const ICLTensor *weights_to_use = weights;
242*c217d954SCole Faust 
243*c217d954SCole Faust     // If the data layout is NCHW, transform everything in NHWC. Another alternative could be to
244*c217d954SCole Faust     // do an outer product in NCHW and then an accumulation through a reduction. This would have two
245*c217d954SCole Faust     // drawbacks: first, the outer product is less efficient than a full GEMM. Second, the reduction
246*c217d954SCole Faust     // might be slower than GEMM.
247*c217d954SCole Faust     if(_is_nchw)
248*c217d954SCole Faust     {
249*c217d954SCole Faust         _memory_group.manage(&_permuted_input);
250*c217d954SCole Faust         _permute_input_to_nhwc.configure(compile_context, input, &_permuted_input, PermutationVector(2U, 0U, 1U));
251*c217d954SCole Faust 
252*c217d954SCole Faust         _permute_weights_to_nhwc.configure(compile_context, weights, &_permuted_weights, PermutationVector(2U, 0U, 1U));
253*c217d954SCole Faust 
254*c217d954SCole Faust         input_to_use   = &_permuted_input;
255*c217d954SCole Faust         weights_to_use = &_permuted_weights;
256*c217d954SCole Faust     }
257*c217d954SCole Faust 
258*c217d954SCole Faust     // Reshape the input weights. The weights will be reshaped only once during the call to prepare()
259*c217d954SCole Faust     _reshaped_weights.allocator()->init(TensorInfo(TensorShape(weights_to_use->info()->dimension(0),
260*c217d954SCole Faust                                                                weights_to_use->info()->dimension(1) * weights_to_use->info()->dimension(2) * weights_to_use->info()->dimension(3)),
261*c217d954SCole Faust                                                    1,
262*c217d954SCole Faust                                                    input->info()->data_type(), weights->info()->quantization_info()));
263*c217d954SCole Faust 
264*c217d954SCole Faust     _reshape_weights.configure(compile_context, weights_to_use, &_reshaped_weights);
265*c217d954SCole Faust     _transpose_weights.configure(compile_context, &_reshaped_weights, &_reshaped_weights_t);
266*c217d954SCole Faust 
267*c217d954SCole Faust     const size_t idx_h = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::HEIGHT);
268*c217d954SCole Faust     GEMMInfo     gemm_info(false, false, true, input->info()->dimension(idx_h), true);
269*c217d954SCole Faust 
270*c217d954SCole Faust     // Configure output stage for asymmetric quantized types
271*c217d954SCole Faust     if(_is_quantized)
272*c217d954SCole Faust     {
273*c217d954SCole Faust         // gemmlowp adds the offsets (instead of subtracting them). Thus, we need to negate the original
274*c217d954SCole Faust         // and restore them back to make it work properly.
275*c217d954SCole Faust         QuantizationInfo iq_info = input->info()->quantization_info();
276*c217d954SCole Faust         QuantizationInfo wq_info = weights->info()->quantization_info();
277*c217d954SCole Faust 
278*c217d954SCole Faust         input_to_use->info()->set_quantization_info(QuantizationInfo(iq_info.uniform().scale, -iq_info.uniform().offset));
279*c217d954SCole Faust         _reshaped_weights_t.info()->set_quantization_info(QuantizationInfo(wq_info.uniform().scale, -wq_info.uniform().offset));
280*c217d954SCole Faust 
281*c217d954SCole Faust         _mm_gemmlowp.configure(compile_context, input_to_use, &_reshaped_weights_t, nullptr, &_gemm_output, gemm_info);
282*c217d954SCole Faust 
283*c217d954SCole Faust         input_to_use->info()->set_quantization_info(iq_info);
284*c217d954SCole Faust         _reshaped_weights_t.info()->set_quantization_info(wq_info);
285*c217d954SCole Faust     }
286*c217d954SCole Faust     else
287*c217d954SCole Faust     {
288*c217d954SCole Faust         _mm_gemm.configure(compile_context, input_to_use, &_reshaped_weights_t, nullptr, &_gemm_output, 1.f, 0.0f, gemm_info);
289*c217d954SCole Faust     }
290*c217d954SCole Faust 
291*c217d954SCole Faust     if(_is_nchw)
292*c217d954SCole Faust     {
293*c217d954SCole Faust         _permuted_input.allocator()->allocate();
294*c217d954SCole Faust     }
295*c217d954SCole Faust 
296*c217d954SCole Faust     ICLTensor *deconv_reshape_output = nullptr;
297*c217d954SCole Faust     ICLTensor *slice_output          = nullptr;
298*c217d954SCole Faust     ICLTensor *output_stage_output   = nullptr;
299*c217d954SCole Faust 
300*c217d954SCole Faust     if(_padded_input && _is_quantized)
301*c217d954SCole Faust     {
302*c217d954SCole Faust         _memory_group.manage(&_slice_gemm_input);
303*c217d954SCole Faust         _memory_group.manage(&_gemmlowp_final);
304*c217d954SCole Faust         deconv_reshape_output = &_gemmlowp_final;
305*c217d954SCole Faust         output_stage_output   = &_slice_gemm_input;
306*c217d954SCole Faust         slice_output          = output;
307*c217d954SCole Faust     }
308*c217d954SCole Faust     else if(_padded_input)
309*c217d954SCole Faust     {
310*c217d954SCole Faust         _memory_group.manage(&_slice_gemm_input);
311*c217d954SCole Faust         deconv_reshape_output = &_slice_gemm_input;
312*c217d954SCole Faust         slice_output          = output;
313*c217d954SCole Faust     }
314*c217d954SCole Faust     else if(_is_quantized)
315*c217d954SCole Faust     {
316*c217d954SCole Faust         _memory_group.manage(&_gemmlowp_final);
317*c217d954SCole Faust         deconv_reshape_output = &_gemmlowp_final;
318*c217d954SCole Faust         output_stage_output   = output;
319*c217d954SCole Faust     }
320*c217d954SCole Faust     else
321*c217d954SCole Faust     {
322*c217d954SCole Faust         deconv_reshape_output = output;
323*c217d954SCole Faust     }
324*c217d954SCole Faust 
325*c217d954SCole Faust     // Configure a Col2Im call to reshape the output of GEMM
326*c217d954SCole Faust     _deconv_reshape->configure(compile_context, &_gemm_output, bias, deconv_reshape_output, input->info(), weights->info(), deconv_info);
327*c217d954SCole Faust     _gemm_output.allocator()->allocate();
328*c217d954SCole Faust 
329*c217d954SCole Faust     if(_is_quantized)
330*c217d954SCole Faust     {
331*c217d954SCole Faust         GEMMLowpOutputStageInfo output_stage_info;
332*c217d954SCole Faust         construct_gemmlowp_output_stage(input->info(), weights->info(), output->info(), output_stage_info);
333*c217d954SCole Faust         _gemmlowp_output_stage.configure(compile_context, &_gemmlowp_final, nullptr, output_stage_output, output_stage_info);
334*c217d954SCole Faust         _gemmlowp_final.allocator()->allocate();
335*c217d954SCole Faust     }
336*c217d954SCole Faust 
337*c217d954SCole Faust     // If the input was padded, the output needs to be sliced.
338*c217d954SCole Faust     if(_padded_input)
339*c217d954SCole Faust     {
340*c217d954SCole Faust         const auto start_end = compute_start_end_slice_coordinates(*deconv_reshape_output->info(), deconv_info, _is_nchw);
341*c217d954SCole Faust         _slice_gemm.configure(compile_context, &_slice_gemm_input, slice_output, start_end.first, start_end.second);
342*c217d954SCole Faust         _slice_gemm_input.allocator()->allocate();
343*c217d954SCole Faust     }
344*c217d954SCole Faust }
345*c217d954SCole Faust 
run()346*c217d954SCole Faust void CLGEMMDeconvolutionLayer::run()
347*c217d954SCole Faust {
348*c217d954SCole Faust     prepare();
349*c217d954SCole Faust 
350*c217d954SCole Faust     MemoryGroupResourceScope scope_mg(_memory_group);
351*c217d954SCole Faust 
352*c217d954SCole Faust     if(_is_nchw)
353*c217d954SCole Faust     {
354*c217d954SCole Faust         _permute_input_to_nhwc.run();
355*c217d954SCole Faust     }
356*c217d954SCole Faust 
357*c217d954SCole Faust     if(_is_quantized)
358*c217d954SCole Faust     {
359*c217d954SCole Faust         _mm_gemmlowp.run();
360*c217d954SCole Faust     }
361*c217d954SCole Faust     else
362*c217d954SCole Faust     {
363*c217d954SCole Faust         _mm_gemm.run();
364*c217d954SCole Faust     }
365*c217d954SCole Faust 
366*c217d954SCole Faust     CLScheduler::get().enqueue(*_deconv_reshape, false);
367*c217d954SCole Faust 
368*c217d954SCole Faust     if(_is_quantized)
369*c217d954SCole Faust     {
370*c217d954SCole Faust         _gemmlowp_output_stage.run();
371*c217d954SCole Faust     }
372*c217d954SCole Faust 
373*c217d954SCole Faust     if(_padded_input)
374*c217d954SCole Faust     {
375*c217d954SCole Faust         _slice_gemm.run();
376*c217d954SCole Faust     }
377*c217d954SCole Faust }
378*c217d954SCole Faust 
prepare()379*c217d954SCole Faust void CLGEMMDeconvolutionLayer::prepare()
380*c217d954SCole Faust {
381*c217d954SCole Faust     if(!_is_prepared)
382*c217d954SCole Faust     {
383*c217d954SCole Faust         ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
384*c217d954SCole Faust 
385*c217d954SCole Faust         if(_is_nchw)
386*c217d954SCole Faust         {
387*c217d954SCole Faust             _permuted_weights.allocator()->allocate();
388*c217d954SCole Faust             _permute_weights_to_nhwc.run();
389*c217d954SCole Faust         }
390*c217d954SCole Faust 
391*c217d954SCole Faust         _reshaped_weights.allocator()->allocate();
392*c217d954SCole Faust         _reshape_weights.run();
393*c217d954SCole Faust 
394*c217d954SCole Faust         if(_is_nchw)
395*c217d954SCole Faust         {
396*c217d954SCole Faust             _permuted_weights.allocator()->free();
397*c217d954SCole Faust         }
398*c217d954SCole Faust 
399*c217d954SCole Faust         _reshaped_weights_t.allocator()->allocate();
400*c217d954SCole Faust         _transpose_weights.run();
401*c217d954SCole Faust 
402*c217d954SCole Faust         // Prepare gemm
403*c217d954SCole Faust         if(!_is_quantized)
404*c217d954SCole Faust         {
405*c217d954SCole Faust             _mm_gemm.prepare();
406*c217d954SCole Faust         }
407*c217d954SCole Faust         else
408*c217d954SCole Faust         {
409*c217d954SCole Faust             _mm_gemmlowp.prepare();
410*c217d954SCole Faust         }
411*c217d954SCole Faust 
412*c217d954SCole Faust         // Free resources
413*c217d954SCole Faust         if(!_reshaped_weights_t.is_used())
414*c217d954SCole Faust         {
415*c217d954SCole Faust             _reshaped_weights_t.allocator()->free();
416*c217d954SCole Faust         }
417*c217d954SCole Faust 
418*c217d954SCole Faust         _original_weights->mark_as_unused();
419*c217d954SCole Faust         _is_prepared = true;
420*c217d954SCole Faust     }
421*c217d954SCole Faust }
422*c217d954SCole Faust } // namespace arm_compute
423