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