xref: /aosp_15_r20/external/ComputeLibrary/src/cpu/operators/CpuGemmConv2d.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1*c217d954SCole Faust /*
2*c217d954SCole Faust  * Copyright (c) 2021-2023 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 "src/cpu/operators/CpuGemmConv2d.h"
25*c217d954SCole Faust 
26*c217d954SCole Faust #include "arm_compute/core/Size2D.h"
27*c217d954SCole Faust #include "arm_compute/core/TensorInfo.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/core/utils/quantization/AsymmHelpers.h"
32*c217d954SCole Faust #include "arm_compute/runtime/NEON/NEScheduler.h"
33*c217d954SCole Faust 
34*c217d954SCole Faust #include "src/common/utils/Log.h"
35*c217d954SCole Faust #include "src/core/helpers/MemoryHelpers.h"
36*c217d954SCole Faust #include "src/cpu/kernels/CpuCol2ImKernel.h"
37*c217d954SCole Faust #include "src/cpu/kernels/CpuIm2ColKernel.h"
38*c217d954SCole Faust #include "src/cpu/kernels/CpuReshapeKernel.h"
39*c217d954SCole Faust #include "src/cpu/kernels/CpuWeightsReshapeKernel.h"
40*c217d954SCole Faust #include "src/cpu/operators/CpuGemm.h"
41*c217d954SCole Faust #include "src/cpu/operators/CpuGemmLowpMatrixMultiplyCore.h"
42*c217d954SCole Faust #include "src/cpu/operators/CpuGemmLowpOutputStage.h"
43*c217d954SCole Faust #include "src/cpu/utils/CpuAuxTensorHandler.h"
44*c217d954SCole Faust 
45*c217d954SCole Faust #include <set>
46*c217d954SCole Faust #include <tuple>
47*c217d954SCole Faust 
48*c217d954SCole Faust using namespace arm_compute::misc::shape_calculator;
49*c217d954SCole Faust using namespace arm_compute::experimental;
50*c217d954SCole Faust 
51*c217d954SCole Faust namespace arm_compute
52*c217d954SCole Faust {
53*c217d954SCole Faust namespace cpu
54*c217d954SCole Faust {
skip_im_col_info(const ITensorInfo * src,const ITensorInfo * weights,const PadStrideInfo & conv_info,const Size2D & dilation,const ActivationLayerInfo & act_info)55*c217d954SCole Faust CpuGemmConv2d::SkipInfo CpuGemmConv2d::skip_im_col_info(const ITensorInfo *src, const ITensorInfo *weights, const PadStrideInfo &conv_info,
56*c217d954SCole Faust                                                         const Size2D &dilation, const ActivationLayerInfo &act_info)
57*c217d954SCole Faust {
58*c217d954SCole Faust     const DataLayout   data_layout   = src->data_layout();
59*c217d954SCole Faust     const int          idx_width     = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
60*c217d954SCole Faust     const int          idx_height    = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
61*c217d954SCole Faust     const unsigned int kernel_width  = weights->dimension(idx_width);
62*c217d954SCole Faust     const unsigned int kernel_height = weights->dimension(idx_height);
63*c217d954SCole Faust     unsigned int       conv_w        = 0;
64*c217d954SCole Faust     unsigned int       conv_h        = 0;
65*c217d954SCole Faust     std::tie(conv_w, conv_h) = scaled_dimensions(src->dimension(idx_width),
66*c217d954SCole Faust                                                  src->dimension(idx_height),
67*c217d954SCole Faust                                                  kernel_width,
68*c217d954SCole Faust                                                  kernel_height,
69*c217d954SCole Faust                                                  conv_info,
70*c217d954SCole Faust                                                  dilation);
71*c217d954SCole Faust     const bool skip_im2col = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1 && conv_info.stride().first == 1 && conv_info.stride().second == 1);
72*c217d954SCole Faust 
73*c217d954SCole Faust     if(skip_im2col)
74*c217d954SCole Faust     {
75*c217d954SCole Faust         const bool skip_col2im = (data_layout == DataLayout::NHWC && (bool(CpuGemmConv2d::validate_gemm3d(src, weights, act_info, conv_h, /*skip_im2col*/ true))));
76*c217d954SCole Faust         if(skip_col2im)
77*c217d954SCole Faust         {
78*c217d954SCole Faust             return { true, true };
79*c217d954SCole Faust         }
80*c217d954SCole Faust     }
81*c217d954SCole Faust     else
82*c217d954SCole Faust     {
83*c217d954SCole Faust         const bool skip_col2im = (data_layout == DataLayout::NHWC && (bool(CpuGemmConv2d::validate_gemm3d(src, weights, act_info, conv_h, /*skip_im2col*/ false))));
84*c217d954SCole Faust         if(skip_col2im)
85*c217d954SCole Faust         {
86*c217d954SCole Faust             return { false, true };
87*c217d954SCole Faust         }
88*c217d954SCole Faust     }
89*c217d954SCole Faust 
90*c217d954SCole Faust     // Default case when we cannot reinterpret the input and output as 3D.
91*c217d954SCole Faust     return { false, false };
92*c217d954SCole Faust }
93*c217d954SCole Faust 
CpuGemmConv2d()94*c217d954SCole Faust CpuGemmConv2d::CpuGemmConv2d()
95*c217d954SCole Faust     : _weights_reshape_kernel(nullptr), _im2col_kernel(), _mm_gemm(), _mm_gemmlowp(), _col2im_kernel(), _reshape_kernel(), _im2col_output(), _weights_reshaped(), _gemm_output(), _gemm_output_3d(),
96*c217d954SCole Faust       _data_layout(DataLayout::NCHW), _skip_im2col(false), _skip_col2im(false), _is_quantized(false), _is_prepared(false), _aux_mem(AuxTensorIdx::Count)
97*c217d954SCole Faust {
98*c217d954SCole Faust }
99*c217d954SCole Faust CpuGemmConv2d::~CpuGemmConv2d() = default;
100*c217d954SCole Faust 
configure_mm(const ITensorInfo * src,const ITensorInfo * weights,const ITensorInfo * biases,ITensorInfo * dst,const ActivationLayerInfo & act_info,bool enable_fast_math,int gemm_3d_depth,bool fixed_format,arm_compute::WeightFormat weight_format)101*c217d954SCole Faust void CpuGemmConv2d::configure_mm(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, ITensorInfo *dst, const ActivationLayerInfo &act_info,
102*c217d954SCole Faust                                  bool enable_fast_math, int gemm_3d_depth, bool fixed_format, arm_compute::WeightFormat weight_format)
103*c217d954SCole Faust {
104*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON_NULLPTR(src, weights);
105*c217d954SCole Faust     ARM_COMPUTE_ERROR_THROW_ON(validate_mm(src, weights, biases, dst, act_info, enable_fast_math, gemm_3d_depth, _skip_im2col, fixed_format, weight_format));
106*c217d954SCole Faust 
107*c217d954SCole Faust     // Create GEMMInfo structure
108*c217d954SCole Faust     const GEMMInfo &gemm_info = GEMMInfo(false, false, true /* Reshape weights only for the first run */,
109*c217d954SCole Faust                                          gemm_3d_depth, _skip_im2col /* Reinterpret the input as 3D if im2col is skipped */,
110*c217d954SCole Faust                                          false, GEMMLowpOutputStageInfo(), false, enable_fast_math, false, act_info, experimental::PostOpList<ITensorInfo *>(), fixed_format, weight_format);
111*c217d954SCole Faust 
112*c217d954SCole Faust     // Supported activations in GEMM
113*c217d954SCole Faust     const std::set<ActivationLayerInfo::ActivationFunction> supported_acts = { ActivationLayerInfo::ActivationFunction::RELU,
114*c217d954SCole Faust                                                                                ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
115*c217d954SCole Faust                                                                                ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU
116*c217d954SCole Faust                                                                              };
117*c217d954SCole Faust 
118*c217d954SCole Faust     if(_is_quantized)
119*c217d954SCole Faust     {
120*c217d954SCole Faust         TensorInfo tmp_src{ *src };
121*c217d954SCole Faust         TensorInfo tmp_weights{ *weights };
122*c217d954SCole Faust         // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
123*c217d954SCole Faust         // Extract and negate input and weights offset
124*c217d954SCole Faust         const QuantizationInfo        iqinfo    = src->quantization_info();
125*c217d954SCole Faust         const QuantizationInfo        wqinfo    = weights->quantization_info();
126*c217d954SCole Faust         const QuantizationInfo        oqinfo    = (dst->total_size() == 0) ? iqinfo : dst->quantization_info();
127*c217d954SCole Faust         const UniformQuantizationInfo uiqinfo   = iqinfo.uniform();
128*c217d954SCole Faust         const UniformQuantizationInfo uoqinfo   = oqinfo.uniform();
129*c217d954SCole Faust         const DataType                data_type = src->data_type();
130*c217d954SCole Faust 
131*c217d954SCole Faust         tmp_src.set_quantization_info(QuantizationInfo(uiqinfo.scale, -uiqinfo.offset));
132*c217d954SCole Faust         if(!is_data_type_quantized_per_channel(tmp_weights.data_type()))
133*c217d954SCole Faust         {
134*c217d954SCole Faust             const UniformQuantizationInfo uwqinfo = wqinfo.uniform();
135*c217d954SCole Faust             tmp_weights.set_quantization_info(QuantizationInfo(uwqinfo.scale, -uwqinfo.offset));
136*c217d954SCole Faust         }
137*c217d954SCole Faust 
138*c217d954SCole Faust         // Merge activation with output stage
139*c217d954SCole Faust         PixelValue type_min{};
140*c217d954SCole Faust         PixelValue type_max{};
141*c217d954SCole Faust         std::tie(type_min, type_max) = get_min_max(data_type);
142*c217d954SCole Faust         int32_t min_activation = type_min.get<int32_t>();
143*c217d954SCole Faust         int32_t max_activation = type_max.get<int32_t>();
144*c217d954SCole Faust 
145*c217d954SCole Faust         if(supported_acts.count(act_info.activation()) != 0)
146*c217d954SCole Faust         {
147*c217d954SCole Faust             std::tie(min_activation, max_activation) = get_quantized_activation_min_max(act_info, data_type, uoqinfo);
148*c217d954SCole Faust         }
149*c217d954SCole Faust 
150*c217d954SCole Faust         GEMMLowpOutputStageInfo output_info;
151*c217d954SCole Faust         output_info.type                     = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT;
152*c217d954SCole Faust         output_info.gemmlowp_offset          = uoqinfo.offset;
153*c217d954SCole Faust         output_info.gemmlowp_min_bound       = min_activation;
154*c217d954SCole Faust         output_info.gemmlowp_max_bound       = max_activation;
155*c217d954SCole Faust         output_info.is_quantized_per_channel = (tmp_weights.data_type() == DataType::QSYMM8_PER_CHANNEL);
156*c217d954SCole Faust         quantization::calculate_quantized_multipliers(iqinfo, wqinfo, oqinfo, output_info);
157*c217d954SCole Faust 
158*c217d954SCole Faust         _mm_gemmlowp = std::make_unique<CpuGemmLowpMatrixMultiplyCore>();
159*c217d954SCole Faust         _mm_gemmlowp->configure(&tmp_src, &tmp_weights, biases, dst, GEMMInfo(false, false, true, gemm_3d_depth, _skip_im2col, false, output_info, false, enable_fast_math, false, act_info,
160*c217d954SCole Faust                                                                               experimental::PostOpList<ITensorInfo *>(), fixed_format, weight_format));
161*c217d954SCole Faust 
162*c217d954SCole Faust         auto mm_mem_req = _mm_gemmlowp->workspace();
163*c217d954SCole Faust         for(unsigned int cont = 0; cont < mm_mem_req.size(); ++cont)
164*c217d954SCole Faust         {
165*c217d954SCole Faust             _aux_mem[cont] = mm_mem_req[cont];
166*c217d954SCole Faust         }
167*c217d954SCole Faust     }
168*c217d954SCole Faust     else
169*c217d954SCole Faust     {
170*c217d954SCole Faust         // Configure matrix multiply function
171*c217d954SCole Faust         _mm_gemm = std::make_unique<CpuGemm>();
172*c217d954SCole Faust         _mm_gemm->configure(src, weights, biases, dst, 1.0f, 0.0f, gemm_info);
173*c217d954SCole Faust         auto mm_mem_req = _mm_gemm->workspace();
174*c217d954SCole Faust         for(unsigned int cont = 0; cont < mm_mem_req.size(); ++cont)
175*c217d954SCole Faust         {
176*c217d954SCole Faust             _aux_mem[cont] = mm_mem_req[cont];
177*c217d954SCole Faust         }
178*c217d954SCole Faust     }
179*c217d954SCole Faust }
180*c217d954SCole Faust 
validate_mm(const ITensorInfo * src,const ITensorInfo * weights,const ITensorInfo * biases,const ITensorInfo * dst,const ActivationLayerInfo & act_info,bool enable_fast_math,int gemm_3d_depth,bool skip_im2col,bool fixed_format,arm_compute::WeightFormat weight_format)181*c217d954SCole Faust Status CpuGemmConv2d::validate_mm(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst,
182*c217d954SCole Faust                                   const ActivationLayerInfo &act_info, bool enable_fast_math, int gemm_3d_depth, bool skip_im2col, bool fixed_format, arm_compute::WeightFormat weight_format)
183*c217d954SCole Faust {
184*c217d954SCole Faust     const DataType data_type             = src->data_type();
185*c217d954SCole Faust     const bool     is_quantized          = is_data_type_quantized_asymmetric(data_type);
186*c217d954SCole Faust     const bool     is_activation_enabled = act_info.enabled();
187*c217d954SCole Faust 
188*c217d954SCole Faust     // Create GEMMInfo structure
189*c217d954SCole Faust     const GEMMInfo gemm_info = GEMMInfo(false, false, true /* Reshape weights only for the first run */,
190*c217d954SCole Faust                                         gemm_3d_depth, skip_im2col /* Reinterpret the input as 3D if im2col is skipped */,
191*c217d954SCole Faust                                         false, GEMMLowpOutputStageInfo(), false, enable_fast_math, false, act_info, experimental::PostOpList<ITensorInfo *>(), fixed_format, weight_format);
192*c217d954SCole Faust 
193*c217d954SCole Faust     if(is_quantized)
194*c217d954SCole Faust     {
195*c217d954SCole Faust         // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
196*c217d954SCole Faust         // Extract and negate input and weights offset
197*c217d954SCole Faust         const QuantizationInfo       &iqinfo  = src->quantization_info();
198*c217d954SCole Faust         const QuantizationInfo       &wqinfo  = weights->quantization_info();
199*c217d954SCole Faust         const QuantizationInfo       &oqinfo  = (dst->total_size() == 0) ? iqinfo : dst->quantization_info();
200*c217d954SCole Faust         const UniformQuantizationInfo uoqinfo = oqinfo.uniform();
201*c217d954SCole Faust 
202*c217d954SCole Faust         // Merge activation with output stage
203*c217d954SCole Faust         PixelValue type_min{};
204*c217d954SCole Faust         PixelValue type_max{};
205*c217d954SCole Faust         std::tie(type_min, type_max) = get_min_max(data_type);
206*c217d954SCole Faust         int32_t min_activation = type_min.get<int32_t>();
207*c217d954SCole Faust         int32_t max_activation = type_max.get<int32_t>();
208*c217d954SCole Faust 
209*c217d954SCole Faust         const std::set<ActivationLayerInfo::ActivationFunction> supported_acts = { ActivationLayerInfo::ActivationFunction::RELU,
210*c217d954SCole Faust                                                                                    ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
211*c217d954SCole Faust                                                                                    ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU
212*c217d954SCole Faust                                                                                  };
213*c217d954SCole Faust         if(is_activation_enabled && supported_acts.count(act_info.activation()) != 0)
214*c217d954SCole Faust         {
215*c217d954SCole Faust             std::tie(min_activation, max_activation) = get_quantized_activation_min_max(act_info, data_type, uoqinfo);
216*c217d954SCole Faust         }
217*c217d954SCole Faust 
218*c217d954SCole Faust         GEMMLowpOutputStageInfo output_info;
219*c217d954SCole Faust         output_info.type                     = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT;
220*c217d954SCole Faust         output_info.gemmlowp_offset          = uoqinfo.offset;
221*c217d954SCole Faust         output_info.gemmlowp_min_bound       = min_activation;
222*c217d954SCole Faust         output_info.gemmlowp_max_bound       = max_activation;
223*c217d954SCole Faust         output_info.is_quantized_per_channel = (weights->data_type() == DataType::QSYMM8_PER_CHANNEL);
224*c217d954SCole Faust         ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multipliers(iqinfo, wqinfo, oqinfo, output_info));
225*c217d954SCole Faust 
226*c217d954SCole Faust         // Perform validation step on GEMMLowp
227*c217d954SCole Faust         std::unique_ptr<ITensorInfo> input_qa   = src->clone();
228*c217d954SCole Faust         std::unique_ptr<ITensorInfo> weights_qa = weights->clone();
229*c217d954SCole Faust         input_qa->set_quantization_info(QuantizationInfo(iqinfo.uniform().scale, -iqinfo.uniform().offset));
230*c217d954SCole Faust         weights_qa->set_quantization_info(QuantizationInfo(wqinfo.uniform().scale, -wqinfo.uniform().offset));
231*c217d954SCole Faust 
232*c217d954SCole Faust         return CpuGemmLowpMatrixMultiplyCore::validate(input_qa.get(), weights_qa.get(), biases, dst, GEMMInfo(false, false, true, gemm_3d_depth, skip_im2col, false, output_info, false, enable_fast_math,
233*c217d954SCole Faust                                                                                                                false, act_info));
234*c217d954SCole Faust     }
235*c217d954SCole Faust     else
236*c217d954SCole Faust     {
237*c217d954SCole Faust         // Perform validation step on Matrix multiply function
238*c217d954SCole Faust         return CpuGemm::validate(src, weights, nullptr, dst, 1.0f, 0.0f, gemm_info);
239*c217d954SCole Faust     }
240*c217d954SCole Faust }
241*c217d954SCole Faust 
validate_gemm3d(const ITensorInfo * input_info,const ITensorInfo * weights_info,const ActivationLayerInfo & act_info,int gemm_3d_depth,bool skip_im2col)242*c217d954SCole Faust Status CpuGemmConv2d::validate_gemm3d(const ITensorInfo *input_info, const ITensorInfo *weights_info, const ActivationLayerInfo &act_info, int gemm_3d_depth, bool skip_im2col)
243*c217d954SCole Faust {
244*c217d954SCole Faust     const DataType     data_type = input_info->data_type();
245*c217d954SCole Faust     const unsigned int mult_y    = skip_im2col ? 1U : gemm_3d_depth;
246*c217d954SCole Faust     const unsigned int mult_z    = skip_im2col ? gemm_3d_depth : 1U;
247*c217d954SCole Faust 
248*c217d954SCole Faust     // Set dummy tensor shapes for the validation
249*c217d954SCole Faust     const TensorInfo dummy_input_info(TensorShape(4U, 4U * mult_y, 1U * mult_z), 1, data_type, input_info->quantization_info());
250*c217d954SCole Faust     const TensorInfo dummy_weights_info(TensorShape(4U, 4U), 1, data_type, weights_info->quantization_info());
251*c217d954SCole Faust     const TensorInfo dummy_output_info(TensorShape(4U, 4U, gemm_3d_depth), 1, data_type, input_info->quantization_info());
252*c217d954SCole Faust 
253*c217d954SCole Faust     return validate_mm(&dummy_input_info, &dummy_weights_info, nullptr, &dummy_output_info, act_info, false, gemm_3d_depth, skip_im2col);
254*c217d954SCole Faust }
255*c217d954SCole Faust 
configure(const ITensorInfo * src,const ITensorInfo * weights,const ITensorInfo * biases,ITensorInfo * dst,const PadStrideInfo & conv_info,const WeightsInfo & weights_info,const Size2D & dilation,const ActivationLayerInfo & act_info,bool enable_fast_math,unsigned int num_groups)256*c217d954SCole Faust void CpuGemmConv2d::configure(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, ITensorInfo *dst, const PadStrideInfo &conv_info, const WeightsInfo &weights_info,
257*c217d954SCole Faust                               const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math, unsigned int num_groups)
258*c217d954SCole Faust {
259*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON_NULLPTR(src, weights, dst);
260*c217d954SCole Faust     ARM_COMPUTE_UNUSED(num_groups, weights_info);
261*c217d954SCole Faust     ARM_COMPUTE_ERROR_THROW_ON(CpuGemmConv2d::validate(src,
262*c217d954SCole Faust                                                        weights,
263*c217d954SCole Faust                                                        biases,
264*c217d954SCole Faust                                                        dst,
265*c217d954SCole Faust                                                        conv_info,
266*c217d954SCole Faust                                                        weights_info,
267*c217d954SCole Faust                                                        dilation,
268*c217d954SCole Faust                                                        act_info,
269*c217d954SCole Faust                                                        enable_fast_math,
270*c217d954SCole Faust                                                        num_groups));
271*c217d954SCole Faust     ARM_COMPUTE_LOG_PARAMS(src, weights, biases, dst, conv_info, weights_info, dilation, act_info, enable_fast_math, num_groups);
272*c217d954SCole Faust 
273*c217d954SCole Faust     const DataType   data_type   = src->data_type();
274*c217d954SCole Faust     const DataLayout data_layout = src->data_layout();
275*c217d954SCole Faust     const int        idx_width   = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
276*c217d954SCole Faust     const int        idx_height  = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
277*c217d954SCole Faust     const int        idx_kernels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
278*c217d954SCole Faust 
279*c217d954SCole Faust     const unsigned int kernel_width  = weights->dimension(idx_width);
280*c217d954SCole Faust     const unsigned int kernel_height = weights->dimension(idx_height);
281*c217d954SCole Faust 
282*c217d954SCole Faust     _is_prepared  = weights_info.retain_internal_weights();
283*c217d954SCole Faust     _is_quantized = is_data_type_quantized_asymmetric(src->data_type());
284*c217d954SCole Faust     _data_layout  = data_layout;
285*c217d954SCole Faust     _skip_im2col  = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1 && conv_info.stride().first == 1 && conv_info.stride().second == 1);
286*c217d954SCole Faust 
287*c217d954SCole Faust     const ITensorInfo *gemm_input_to_use  = src;
288*c217d954SCole Faust     ITensorInfo       *gemm_output_to_use = dst;
289*c217d954SCole Faust 
290*c217d954SCole Faust     // Get convolved dimensions
291*c217d954SCole Faust     unsigned int conv_w = 0;
292*c217d954SCole Faust     unsigned int conv_h = 0;
293*c217d954SCole Faust     std::tie(conv_w, conv_h) = scaled_dimensions(src->dimension(idx_width),
294*c217d954SCole Faust                                                  src->dimension(idx_height),
295*c217d954SCole Faust                                                  kernel_width,
296*c217d954SCole Faust                                                  kernel_height,
297*c217d954SCole Faust                                                  conv_info,
298*c217d954SCole Faust                                                  dilation);
299*c217d954SCole Faust 
300*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON_MSG((dst->dimension(idx_width) != conv_w) || (dst->dimension(idx_height) != conv_h),
301*c217d954SCole Faust                              "Output shape does not match the expected one");
302*c217d954SCole Faust 
303*c217d954SCole Faust     // Check if GEMM3D is supported
304*c217d954SCole Faust     const CpuGemmConv2d::SkipInfo skip_info = CpuGemmConv2d::skip_im_col_info(src, weights, conv_info, dilation, act_info);
305*c217d954SCole Faust     _skip_im2col                            = skip_info.skip_im2col;
306*c217d954SCole Faust     _skip_col2im                            = skip_info.skip_col2im;
307*c217d954SCole Faust 
308*c217d954SCole Faust     // Get parameters from conv_info
309*c217d954SCole Faust     unsigned int stride_x = 0;
310*c217d954SCole Faust     unsigned int stride_y = 0;
311*c217d954SCole Faust     std::tie(stride_x, stride_y) = conv_info.stride();
312*c217d954SCole Faust 
313*c217d954SCole Faust     unsigned int mat_weights_cols = weights->dimension(idx_kernels);
314*c217d954SCole Faust 
315*c217d954SCole Faust     // _weights_reshaped will be auto configured in the kernel.
316*c217d954SCole Faust     // Just append biases and do not transpose 1xW as it will be reshaped in CpuGemm
317*c217d954SCole Faust     _weights_reshape_kernel = std::make_unique<kernels::CpuWeightsReshapeKernel>();
318*c217d954SCole Faust     _weights_reshape_kernel->configure(weights, nullptr, &_weights_reshaped);
319*c217d954SCole Faust     _weights_reshaped.set_quantization_info(weights->quantization_info());
320*c217d954SCole Faust 
321*c217d954SCole Faust     // Create tensor to store im2col reshaped inputs
322*c217d954SCole Faust     if(!_skip_im2col)
323*c217d954SCole Faust     {
324*c217d954SCole Faust         // Configure
325*c217d954SCole Faust         _im2col_kernel = std::make_unique<kernels::CpuIm2ColKernel>();
326*c217d954SCole Faust         _im2col_kernel->configure(src, &_im2col_output, Size2D(kernel_width, kernel_height), conv_info, false, dilation);
327*c217d954SCole Faust 
328*c217d954SCole Faust         // Update GEMM input
329*c217d954SCole Faust         gemm_input_to_use = &_im2col_output;
330*c217d954SCole Faust     }
331*c217d954SCole Faust 
332*c217d954SCole Faust     // Create temporary GEMM output tensor in case we cannot skip col2im
333*c217d954SCole Faust     const DataType output_data_type = data_type == DataType::BFLOAT16 ? DataType::F32 : data_type;
334*c217d954SCole Faust     if(!_skip_col2im)
335*c217d954SCole Faust     {
336*c217d954SCole Faust         TensorShape shape_gemm;
337*c217d954SCole Faust 
338*c217d954SCole Faust         // Calculate GEMM output shape
339*c217d954SCole Faust         shape_gemm = _im2col_output.tensor_shape();
340*c217d954SCole Faust         shape_gemm.set(0, mat_weights_cols);
341*c217d954SCole Faust         shape_gemm.set(1, conv_w * conv_h);
342*c217d954SCole Faust 
343*c217d954SCole Faust         _gemm_output = TensorInfo(shape_gemm, 1, output_data_type);
344*c217d954SCole Faust         _gemm_output.set_quantization_info(dst->quantization_info()).set_data_layout(src->data_layout());
345*c217d954SCole Faust         _gemm_output_3d = TensorInfo(_gemm_output);
346*c217d954SCole Faust 
347*c217d954SCole Faust         // Update GEMM output
348*c217d954SCole Faust         gemm_output_to_use = &_gemm_output;
349*c217d954SCole Faust     }
350*c217d954SCole Faust     else
351*c217d954SCole Faust     {
352*c217d954SCole Faust         _gemm_output_3d = TensorInfo(*dst);
353*c217d954SCole Faust         _gemm_output_3d.set_data_type(output_data_type).set_data_layout(src->data_layout()).set_is_resizable(true);
354*c217d954SCole Faust         _gemm_output = TensorInfo(_gemm_output_3d);
355*c217d954SCole Faust 
356*c217d954SCole Faust         // Update GEMM output
357*c217d954SCole Faust         gemm_output_to_use = &_gemm_output_3d;
358*c217d954SCole Faust     }
359*c217d954SCole Faust 
360*c217d954SCole Faust     // Configure GEMM
361*c217d954SCole Faust     // In case we need to skip col2im, GEMM3D (gemm_3d_depth != 0) must be called in order to avoid reshaping the output matrix
362*c217d954SCole Faust     const unsigned int gemm_3d_depth = _skip_col2im ? conv_h : 0;
363*c217d954SCole Faust     const bool         fixed_format  = weights_info.weight_format() != arm_compute::WeightFormat::UNSPECIFIED;
364*c217d954SCole Faust     configure_mm(gemm_input_to_use, &_weights_reshaped, biases, gemm_output_to_use, act_info, enable_fast_math, gemm_3d_depth, fixed_format, weights_info.weight_format());
365*c217d954SCole Faust 
366*c217d954SCole Faust     if(!_skip_col2im && _data_layout == DataLayout::NCHW)
367*c217d954SCole Faust     {
368*c217d954SCole Faust         // Configure col2im
369*c217d954SCole Faust         _col2im_kernel = std::make_unique<kernels::CpuCol2ImKernel>();
370*c217d954SCole Faust         _col2im_kernel->configure(gemm_output_to_use, dst, Size2D(conv_w, conv_h));
371*c217d954SCole Faust     }
372*c217d954SCole Faust     else
373*c217d954SCole Faust     {
374*c217d954SCole Faust         // Configure reshape layer
375*c217d954SCole Faust         _reshape_kernel = std::make_unique<kernels::CpuReshapeKernel>();
376*c217d954SCole Faust         _reshape_kernel->configure(gemm_output_to_use, dst);
377*c217d954SCole Faust     }
378*c217d954SCole Faust 
379*c217d954SCole Faust     // Check if GEMM transforms weights
380*c217d954SCole Faust     // Modernise through COMPMID-4535
381*c217d954SCole Faust     bool gemm_trans_wei = _aux_mem[1].size > 0;                                            // Asm Pretranspose
382*c217d954SCole Faust     gemm_trans_wei      = _mm_gemm != nullptr ? _aux_mem[3].size > 0 : gemm_trans_wei;     // Tranpose RHS
383*c217d954SCole Faust     gemm_trans_wei      = _mm_gemmlowp != nullptr ? _aux_mem[5].size > 0 : gemm_trans_wei; // Transpose RHS
384*c217d954SCole Faust 
385*c217d954SCole Faust     // Check lifetime
386*c217d954SCole Faust     _aux_mem[Im2ColOutput]    = MemoryInfo(offset_int_vec(Im2ColOutput), MemoryLifetime::Temporary, _im2col_output.total_size());
387*c217d954SCole Faust     _aux_mem[WeightsReshaped] = MemoryInfo(offset_int_vec(WeightsReshaped), gemm_trans_wei ? MemoryLifetime::Prepare : MemoryLifetime::Persistent, _weights_reshaped.total_size());
388*c217d954SCole Faust     _aux_mem[GemmOutput]      = MemoryInfo(offset_int_vec(GemmOutput), MemoryLifetime::Temporary, _gemm_output.total_size());
389*c217d954SCole Faust }
390*c217d954SCole Faust 
has_opt_impl(arm_compute::WeightFormat & expected_weight_format,const ITensorInfo * src,const ITensorInfo * weights,const ITensorInfo * biases,const ITensorInfo * dst,const PadStrideInfo & conv_info,const WeightsInfo & weights_info,const Size2D & dilation,const ActivationLayerInfo & act_info,const bool enable_fast_math)391*c217d954SCole Faust Status CpuGemmConv2d::has_opt_impl(arm_compute::WeightFormat &expected_weight_format, const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst,
392*c217d954SCole Faust                                    const PadStrideInfo &conv_info,
393*c217d954SCole Faust                                    const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, const bool enable_fast_math)
394*c217d954SCole Faust {
395*c217d954SCole Faust     const DataLayout   data_layout   = src->data_layout();
396*c217d954SCole Faust     const int          idx_width     = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
397*c217d954SCole Faust     const int          idx_height    = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
398*c217d954SCole Faust     const unsigned int kernel_width  = weights->dimension(idx_width);
399*c217d954SCole Faust     const unsigned int kernel_height = weights->dimension(idx_height);
400*c217d954SCole Faust     unsigned int       conv_w        = 0;
401*c217d954SCole Faust     unsigned int       conv_h        = 0;
402*c217d954SCole Faust     std::tie(conv_w, conv_h) = scaled_dimensions(src->dimension(idx_width),
403*c217d954SCole Faust                                                  src->dimension(idx_height),
404*c217d954SCole Faust                                                  kernel_width,
405*c217d954SCole Faust                                                  kernel_height,
406*c217d954SCole Faust                                                  conv_info,
407*c217d954SCole Faust                                                  dilation);
408*c217d954SCole Faust 
409*c217d954SCole Faust     const CpuGemmConv2d::SkipInfo skip_info = CpuGemmConv2d::skip_im_col_info(src, weights, conv_info,
410*c217d954SCole Faust                                                                               dilation, act_info);
411*c217d954SCole Faust 
412*c217d954SCole Faust     const bool         skip_im2col   = skip_info.skip_im2col;
413*c217d954SCole Faust     const bool         skip_col2im   = skip_info.skip_col2im;
414*c217d954SCole Faust     const unsigned int gemm_3d_depth = skip_col2im ? conv_h : 0;
415*c217d954SCole Faust     const bool         fixed_format  = weights_info.weight_format() != arm_compute::WeightFormat::UNSPECIFIED;
416*c217d954SCole Faust     const GEMMInfo     gemm_info     = GEMMInfo(false, false, true /* Reshape weights only for the first run */,
417*c217d954SCole Faust                                                 gemm_3d_depth, skip_im2col /* Reinterpret the input as 3D if im2col is skipped */,
418*c217d954SCole Faust                                                 false, GEMMLowpOutputStageInfo(), false, enable_fast_math, false, act_info, experimental::PostOpList<ITensorInfo *>(), fixed_format, weights_info.weight_format());
419*c217d954SCole Faust 
420*c217d954SCole Faust     return CpuGemm::has_opt_impl(expected_weight_format, src, weights, biases, dst, gemm_info);
421*c217d954SCole Faust }
422*c217d954SCole Faust 
validate(const ITensorInfo * src,const ITensorInfo * weights,const ITensorInfo * biases,const ITensorInfo * dst,const PadStrideInfo & conv_info,const WeightsInfo & weights_info,const Size2D & dilation,const ActivationLayerInfo & act_info,bool enable_fast_math,unsigned int num_groups)423*c217d954SCole Faust Status CpuGemmConv2d::validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst, const PadStrideInfo &conv_info,
424*c217d954SCole Faust                                const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math, unsigned int num_groups)
425*c217d954SCole Faust {
426*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, weights, dst);
427*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights_info.are_reshaped(), "Weights already reshaped are not supported!");
428*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::BFLOAT16, DataType::F16, DataType::F32);
429*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM8_PER_CHANNEL, DataType::BFLOAT16, DataType::F16, DataType::F32);
430*c217d954SCole Faust 
431*c217d954SCole Faust     if (!is_fixed_format(weights_info.weight_format()))
432*c217d954SCole Faust     {
433*c217d954SCole Faust         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(src, weights);
434*c217d954SCole Faust     }
435*c217d954SCole Faust 
436*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_MSG(num_groups > 1, "Grouping (num_groups != 1) is not supported");
437*c217d954SCole Faust 
438*c217d954SCole Faust     const DataLayout data_layout = src->data_layout();
439*c217d954SCole Faust     const DataType   data_type   = src->data_type();
440*c217d954SCole Faust     const int        idx_width   = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
441*c217d954SCole Faust     const int        idx_height  = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
442*c217d954SCole Faust     const int        idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
443*c217d954SCole Faust     const int        idx_kernels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
444*c217d954SCole Faust 
445*c217d954SCole Faust     const unsigned int kernel_width  = weights->dimension(idx_width);
446*c217d954SCole Faust     const unsigned int kernel_height = weights->dimension(idx_height);
447*c217d954SCole Faust 
448*c217d954SCole Faust     TensorInfo         im2col_reshaped_info{};
449*c217d954SCole Faust     TensorInfo         info_gemm{};
450*c217d954SCole Faust     TensorInfo         tmp_info{};
451*c217d954SCole Faust     TensorInfo         weights_reshaped_info{};
452*c217d954SCole Faust     const ITensorInfo *gemm_input_to_use  = src;
453*c217d954SCole Faust     const ITensorInfo *gemm_output_to_use = dst;
454*c217d954SCole Faust     const ITensorInfo *weights_to_use     = weights;
455*c217d954SCole Faust 
456*c217d954SCole Faust     const bool append_bias  = false;
457*c217d954SCole Faust     const bool is_quantized = is_data_type_quantized_asymmetric(data_type);
458*c217d954SCole Faust     const bool is_bf16      = data_type == DataType::BFLOAT16;
459*c217d954SCole Faust 
460*c217d954SCole Faust     // Get convolved dimensions
461*c217d954SCole Faust     unsigned int conv_w = 0;
462*c217d954SCole Faust     unsigned int conv_h = 0;
463*c217d954SCole Faust 
464*c217d954SCole Faust     std::tie(conv_w, conv_h) = scaled_dimensions(src->dimension(idx_width),
465*c217d954SCole Faust                                                  src->dimension(idx_height),
466*c217d954SCole Faust                                                  kernel_width,
467*c217d954SCole Faust                                                  kernel_height,
468*c217d954SCole Faust                                                  conv_info,
469*c217d954SCole Faust                                                  dilation);
470*c217d954SCole Faust 
471*c217d954SCole Faust     // Check if GEMM3D is supported
472*c217d954SCole Faust     const CpuGemmConv2d::SkipInfo skip_info = CpuGemmConv2d::skip_im_col_info(src, weights, conv_info,
473*c217d954SCole Faust                                                                               dilation, act_info);
474*c217d954SCole Faust     const bool skip_im2col = skip_info.skip_im2col, skip_col2im = skip_info.skip_col2im;
475*c217d954SCole Faust 
476*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_channel) != src->dimension(idx_channel));
477*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
478*c217d954SCole Faust 
479*c217d954SCole Faust     // Validate biases
480*c217d954SCole Faust     if(biases != nullptr)
481*c217d954SCole Faust     {
482*c217d954SCole Faust         if(is_quantized)
483*c217d954SCole Faust         {
484*c217d954SCole Faust             ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
485*c217d954SCole Faust         }
486*c217d954SCole Faust         else if(is_bf16)
487*c217d954SCole Faust         {
488*c217d954SCole Faust             ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::F32);
489*c217d954SCole Faust         }
490*c217d954SCole Faust         else
491*c217d954SCole Faust         {
492*c217d954SCole Faust             ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, biases);
493*c217d954SCole Faust         }
494*c217d954SCole Faust         ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != dst->dimension(idx_channel));
495*c217d954SCole Faust         ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
496*c217d954SCole Faust     }
497*c217d954SCole Faust 
498*c217d954SCole Faust     unsigned int mat_weights_cols = weights->dimension(idx_kernels);
499*c217d954SCole Faust     unsigned int mat_weights_rows = weights->dimension(idx_width) * weights->dimension(idx_height) * weights->dimension(idx_channel);
500*c217d954SCole Faust 
501*c217d954SCole Faust     weights_reshaped_info = TensorInfo(compute_weights_reshaped_shape(*weights, append_bias), 1, weights->data_type());
502*c217d954SCole Faust     weights_reshaped_info.set_quantization_info(weights->quantization_info());
503*c217d954SCole Faust     weights_to_use = &weights_reshaped_info;
504*c217d954SCole Faust 
505*c217d954SCole Faust     if(!skip_im2col)
506*c217d954SCole Faust     {
507*c217d954SCole Faust         // Create tensor info for im2col reshaped inputs
508*c217d954SCole Faust         // For CPU, the batch size is on the fourth dimension
509*c217d954SCole Faust         TensorShape shape_im2col = src->tensor_shape();
510*c217d954SCole Faust         shape_im2col.set(0, mat_weights_rows);
511*c217d954SCole Faust         shape_im2col.set(1, conv_w * conv_h);
512*c217d954SCole Faust         shape_im2col.set(2, 1);
513*c217d954SCole Faust 
514*c217d954SCole Faust         im2col_reshaped_info = TensorInfo(shape_im2col, 1, data_type);
515*c217d954SCole Faust         im2col_reshaped_info.set_quantization_info(src->quantization_info());
516*c217d954SCole Faust         ARM_COMPUTE_RETURN_ON_ERROR(kernels::CpuIm2ColKernel::validate(src, &im2col_reshaped_info, Size2D(kernel_width, kernel_height), conv_info, append_bias, dilation, 1));
517*c217d954SCole Faust         gemm_input_to_use = &im2col_reshaped_info;
518*c217d954SCole Faust     }
519*c217d954SCole Faust 
520*c217d954SCole Faust     // Create temporary GEMM output tensor in case we cannot skip col2im
521*c217d954SCole Faust     const DataType output_data_type = data_type == DataType::BFLOAT16 ? DataType::F32 : data_type;
522*c217d954SCole Faust     if(!skip_col2im)
523*c217d954SCole Faust     {
524*c217d954SCole Faust         TensorShape shape_gemm = gemm_input_to_use->tensor_shape();
525*c217d954SCole Faust         shape_gemm.set(0, mat_weights_cols);
526*c217d954SCole Faust         shape_gemm.set(1, conv_w * conv_h);
527*c217d954SCole Faust         info_gemm = TensorInfo(shape_gemm, 1, output_data_type);
528*c217d954SCole Faust     }
529*c217d954SCole Faust     else
530*c217d954SCole Faust     {
531*c217d954SCole Faust         info_gemm = TensorInfo(dst->tensor_shape(), 1, output_data_type);
532*c217d954SCole Faust     }
533*c217d954SCole Faust     info_gemm.set_quantization_info(dst->quantization_info()).set_data_layout(src->data_layout());
534*c217d954SCole Faust     gemm_output_to_use      = &info_gemm;
535*c217d954SCole Faust     const bool fixed_format = weights_info.weight_format() != arm_compute::WeightFormat::UNSPECIFIED;
536*c217d954SCole Faust 
537*c217d954SCole Faust     ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(gemm_input_to_use, weights_to_use, biases, gemm_output_to_use, act_info, enable_fast_math, skip_col2im ? conv_h : 0, skip_im2col, fixed_format,
538*c217d954SCole Faust                                             weights_info.weight_format()));
539*c217d954SCole Faust 
540*c217d954SCole Faust     // Validate Col2Im/ReshapeLayer
541*c217d954SCole Faust     if(!skip_col2im && (data_layout == DataLayout::NCHW))
542*c217d954SCole Faust     {
543*c217d954SCole Faust         ARM_COMPUTE_RETURN_ON_ERROR(kernels::CpuCol2ImKernel::validate(gemm_output_to_use, dst, Size2D(conv_w, conv_h)));
544*c217d954SCole Faust     }
545*c217d954SCole Faust 
546*c217d954SCole Faust     return Status{};
547*c217d954SCole Faust }
548*c217d954SCole Faust 
run(ITensorPack & tensors)549*c217d954SCole Faust void CpuGemmConv2d::run(ITensorPack &tensors)
550*c217d954SCole Faust {
551*c217d954SCole Faust     prepare(tensors);
552*c217d954SCole Faust 
553*c217d954SCole Faust     auto src               = tensors.get_const_tensor(ACL_SRC_0);
554*c217d954SCole Faust     auto dst               = tensors.get_tensor(ACL_DST);
555*c217d954SCole Faust     auto gemm_input_to_use = src;
556*c217d954SCole Faust 
557*c217d954SCole Faust     CpuAuxTensorHandler im2col_output(offset_int_vec(Im2ColOutput), _im2col_output, tensors, false);
558*c217d954SCole Faust     CpuAuxTensorHandler gemm_output(offset_int_vec(GemmOutput), _gemm_output, tensors, false);
559*c217d954SCole Faust     CpuAuxTensorHandler reshaped_wei(offset_int_vec(WeightsReshaped), _weights_reshaped, tensors, false);
560*c217d954SCole Faust 
561*c217d954SCole Faust     bool out_has_padding = _skip_col2im && (dst->info()->padding().bottom != 0 || dst->info()->padding().top != 0);
562*c217d954SCole Faust     if(!_skip_im2col)
563*c217d954SCole Faust     {
564*c217d954SCole Faust         // Run input reshaping
565*c217d954SCole Faust         unsigned int y_dim = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
566*c217d954SCole Faust         ITensorPack  pack =
567*c217d954SCole Faust         {
568*c217d954SCole Faust             { TensorType::ACL_SRC, src },
569*c217d954SCole Faust             { TensorType::ACL_DST, im2col_output.get() }
570*c217d954SCole Faust         };
571*c217d954SCole Faust         NEScheduler::get().schedule_op(_im2col_kernel.get(), y_dim, _im2col_kernel->window(), pack);
572*c217d954SCole Faust         gemm_input_to_use = im2col_output.get();
573*c217d954SCole Faust     }
574*c217d954SCole Faust 
575*c217d954SCole Faust     // Handle the case where output has top/bottom padding
576*c217d954SCole Faust     const ITensor *out_to_use = out_has_padding ? gemm_output.get() : dst;
577*c217d954SCole Faust     Tensor         gemm3d;
578*c217d954SCole Faust     _gemm_output_3d.extend_padding(out_to_use->info()->padding());
579*c217d954SCole Faust     gemm3d.allocator()->soft_init(_gemm_output_3d);
580*c217d954SCole Faust     gemm3d.allocator()->import_memory(out_to_use->buffer());
581*c217d954SCole Faust     auto gemm_output_to_use = gemm_output.get();
582*c217d954SCole Faust 
583*c217d954SCole Faust     if(_skip_im2col)
584*c217d954SCole Faust     {
585*c217d954SCole Faust         gemm_output_to_use = &gemm3d;
586*c217d954SCole Faust     }
587*c217d954SCole Faust     if(_skip_col2im && !out_has_padding)
588*c217d954SCole Faust     {
589*c217d954SCole Faust         gemm_output_to_use = dst;
590*c217d954SCole Faust     }
591*c217d954SCole Faust 
592*c217d954SCole Faust     // Runs CpuGemm or CpuGemmLowpMatrixMultiplyCore functions
593*c217d954SCole Faust     ITensorPack pack_mm = tensors;
594*c217d954SCole Faust     pack_mm.add_const_tensor(TensorType::ACL_SRC_0, gemm_input_to_use);
595*c217d954SCole Faust     if(!this->isVarWeightsKernel())
596*c217d954SCole Faust     {
597*c217d954SCole Faust         pack_mm.add_const_tensor(TensorType::ACL_SRC_1, reshaped_wei.get());
598*c217d954SCole Faust     }
599*c217d954SCole Faust     pack_mm.add_tensor(TensorType::ACL_DST, gemm_output_to_use);
600*c217d954SCole Faust     if(_is_quantized)
601*c217d954SCole Faust     {
602*c217d954SCole Faust         // Run gemmlowp
603*c217d954SCole Faust         _mm_gemmlowp->run(pack_mm);
604*c217d954SCole Faust     }
605*c217d954SCole Faust     else
606*c217d954SCole Faust     {
607*c217d954SCole Faust         // Run gemm
608*c217d954SCole Faust         _mm_gemm->run(pack_mm);
609*c217d954SCole Faust     }
610*c217d954SCole Faust 
611*c217d954SCole Faust     // Reshape output matrix
612*c217d954SCole Faust     if(!_skip_col2im)
613*c217d954SCole Faust     {
614*c217d954SCole Faust         if(_data_layout == DataLayout::NCHW)
615*c217d954SCole Faust         {
616*c217d954SCole Faust             ITensorPack pack =
617*c217d954SCole Faust             {
618*c217d954SCole Faust                 { TensorType::ACL_SRC, gemm_output.get() },
619*c217d954SCole Faust                 { TensorType::ACL_DST, dst }
620*c217d954SCole Faust             };
621*c217d954SCole Faust             NEScheduler::get().schedule_op(_col2im_kernel.get(), Window::DimY, _col2im_kernel->window(), pack);
622*c217d954SCole Faust         }
623*c217d954SCole Faust         else
624*c217d954SCole Faust         {
625*c217d954SCole Faust             ITensorPack pack =
626*c217d954SCole Faust             {
627*c217d954SCole Faust                 { TensorType::ACL_SRC, gemm_output_to_use },
628*c217d954SCole Faust                 { TensorType::ACL_DST, dst }
629*c217d954SCole Faust             };
630*c217d954SCole Faust             NEScheduler::get().schedule_op(_reshape_kernel.get(), Window::DimY, _reshape_kernel->window(), pack);
631*c217d954SCole Faust         }
632*c217d954SCole Faust     }
633*c217d954SCole Faust     else if(out_has_padding)
634*c217d954SCole Faust     {
635*c217d954SCole Faust         ITensorPack pack =
636*c217d954SCole Faust         {
637*c217d954SCole Faust             { TensorType::ACL_SRC, gemm_output_to_use },
638*c217d954SCole Faust             { TensorType::ACL_DST, dst }
639*c217d954SCole Faust         };
640*c217d954SCole Faust         NEScheduler::get().schedule_op(_reshape_kernel.get(), Window::DimY, _reshape_kernel->window(), pack);
641*c217d954SCole Faust     }
642*c217d954SCole Faust }
643*c217d954SCole Faust 
prepare(ITensorPack & tensors)644*c217d954SCole Faust void CpuGemmConv2d::prepare(ITensorPack &tensors)
645*c217d954SCole Faust {
646*c217d954SCole Faust     if(!_is_prepared)
647*c217d954SCole Faust     {
648*c217d954SCole Faust         // Variable weights executions that use fixed-format kernels
649*c217d954SCole Faust         // need no reshaping of the weights.
650*c217d954SCole Faust         if(this->isVarWeightsKernel())
651*c217d954SCole Faust         {
652*c217d954SCole Faust             _is_quantized ? _mm_gemmlowp->prepare(tensors) : _mm_gemm->prepare(tensors);
653*c217d954SCole Faust             _is_prepared = true;
654*c217d954SCole Faust             return;
655*c217d954SCole Faust         }
656*c217d954SCole Faust 
657*c217d954SCole Faust         // Run weights reshaping and mark original weights tensor as unused
658*c217d954SCole Faust         CpuAuxTensorHandler weights_reshaped(offset_int_vec(WeightsReshaped), _weights_reshaped, tensors);
659*c217d954SCole Faust         auto                weights = tensors.get_const_tensor(TensorType::ACL_SRC_1);
660*c217d954SCole Faust         ITensorPack         pack =
661*c217d954SCole Faust         {
662*c217d954SCole Faust             { TensorType::ACL_SRC, weights },
663*c217d954SCole Faust             { TensorType::ACL_DST, weights_reshaped.get() }
664*c217d954SCole Faust         };
665*c217d954SCole Faust         NEScheduler::get().schedule_op(_weights_reshape_kernel.get(), 3, _weights_reshape_kernel->window(), pack);
666*c217d954SCole Faust         weights->mark_as_unused();
667*c217d954SCole Faust         ITensorPack gemm_pack = tensors;
668*c217d954SCole Faust         gemm_pack.add_const_tensor(TensorType::ACL_SRC_1, weights_reshaped.get());
669*c217d954SCole Faust         _is_quantized ? _mm_gemmlowp->prepare(gemm_pack) : _mm_gemm->prepare(gemm_pack);
670*c217d954SCole Faust         _is_prepared = true;
671*c217d954SCole Faust     }
672*c217d954SCole Faust }
workspace() const673*c217d954SCole Faust experimental::MemoryRequirements CpuGemmConv2d::workspace() const
674*c217d954SCole Faust {
675*c217d954SCole Faust     return _aux_mem;
676*c217d954SCole Faust }
isVarWeightsKernel() const677*c217d954SCole Faust bool CpuGemmConv2d::isVarWeightsKernel() const
678*c217d954SCole Faust {
679*c217d954SCole Faust     return _mm_gemm && _mm_gemm->isVarWeightsKernel();
680*c217d954SCole Faust }
681*c217d954SCole Faust } // namespace cpu
682*c217d954SCole Faust } // namespace arm_compute
683