Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 1 | /* |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2021, 2023 Arm Limited. |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +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/gpu/cl/operators/ClGemmConv2d.h" |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/CL/ICLTensor.h" |
| 27 | #include "arm_compute/core/PixelValue.h" |
| 28 | #include "arm_compute/core/Size2D.h" |
| 29 | #include "arm_compute/core/TensorInfo.h" |
| 30 | #include "arm_compute/core/Utils.h" |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 31 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 32 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 33 | #include "arm_compute/core/Validate.h" |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 34 | #include "arm_compute/runtime/CL/CLScheduler.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 35 | |
| 36 | #include "src/common/utils/Log.h" |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 37 | #include "src/core/helpers/AutoConfiguration.h" |
| 38 | #include "src/core/helpers/MemoryHelpers.h" |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 39 | #include "src/gpu/cl/kernels/ClActivationKernel.h" |
| 40 | #include "src/gpu/cl/kernels/ClCol2ImKernel.h" |
| 41 | #include "src/gpu/cl/kernels/ClIm2ColKernel.h" |
| 42 | #include "src/gpu/cl/kernels/ClWeightsReshapeKernel.h" |
| 43 | #include "src/gpu/cl/operators/ClGemm.h" |
| 44 | #include "src/gpu/cl/operators/ClGemmLowpMatrixMultiplyCore.h" |
| 45 | #include "src/gpu/cl/utils/ClAuxTensorHandler.h" |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 46 | #include "support/Cast.h" |
| 47 | |
| 48 | namespace arm_compute |
| 49 | { |
| 50 | using namespace experimental; |
| 51 | using namespace misc::shape_calculator; |
| 52 | using namespace utils::cast; |
| 53 | namespace opencl |
| 54 | { |
Georgios Pinitas | 1988463 | 2021-08-16 12:38:54 +0100 | [diff] [blame] | 55 | ClGemmConv2d::ClGemmConv2d() |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 56 | : _weights_reshape_kernel(nullptr), |
| 57 | _im2col_kernel(nullptr), |
| 58 | _mm_gemm(nullptr), |
| 59 | _mm_gemmlowp(nullptr), |
| 60 | _col2im_kernel(nullptr), |
| 61 | _activation_kernel(nullptr), |
| 62 | _im2col_output(), |
| 63 | _weights_reshaped(), |
| 64 | _gemm_output(), |
| 65 | _skip_im2col(false), |
| 66 | _skip_col2im(false), |
| 67 | _is_quantized(false), |
| 68 | _fuse_activation(true), |
| 69 | _append_bias(false), |
| 70 | _is_prepared(false), |
| 71 | _aux_mem(AuxTensorIdx::Count) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 72 | { |
| 73 | } |
Georgios Pinitas | 1988463 | 2021-08-16 12:38:54 +0100 | [diff] [blame] | 74 | ClGemmConv2d::~ClGemmConv2d() = default; |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 75 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 76 | void ClGemmConv2d::configure_mm(const ClCompileContext &compile_context, |
| 77 | const ITensorInfo *src, |
| 78 | ITensorInfo *weights, |
| 79 | ITensorInfo *biases, |
| 80 | ITensorInfo *dst, |
Georgios Pinitas | 1988463 | 2021-08-16 12:38:54 +0100 | [diff] [blame] | 81 | const GEMMLowpOutputStageInfo &gemmlowp_output_stage, |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 82 | int gemm_3d_depth, |
| 83 | const ActivationLayerInfo &act_info) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 84 | { |
| 85 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, weights); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 86 | ARM_COMPUTE_ERROR_THROW_ON( |
| 87 | validate_mm(src, weights, biases, dst, gemmlowp_output_stage, gemm_3d_depth, _skip_im2col, act_info)); |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 88 | |
| 89 | const GEMMInfo &gemm_info = GEMMInfo(false, // is_a_reshaped |
| 90 | false, // is_b_reshaped |
| 91 | true, // reshape_b_only_on_first_run |
| 92 | gemm_3d_depth, // depth_output_gemm3d |
| 93 | _skip_im2col, // reinterpret_input_as_3d |
| 94 | false, // retain_internal_weights |
| 95 | gemmlowp_output_stage, // gemmlowp_output_stage |
| 96 | false, // fast_math |
| 97 | false, // fp_mixed_precision |
| 98 | true, // broadcast_bias |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame] | 99 | act_info // activation_info |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 100 | ); |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 101 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 102 | TensorInfo tmp_src{*src}; |
| 103 | if (_is_quantized) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 104 | { |
| 105 | // Since we need negative offsets for computing convolution, we need to change QuantizationInfo() |
| 106 | // Extract and negate input and weights offset |
| 107 | const QuantizationInfo input_quantization_info = src->quantization_info(); |
| 108 | const QuantizationInfo weights_quantization_info = weights->quantization_info(); |
| 109 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 110 | tmp_src.set_quantization_info( |
| 111 | QuantizationInfo(input_quantization_info.uniform().scale, -input_quantization_info.uniform().offset)); |
| 112 | weights->set_quantization_info( |
| 113 | QuantizationInfo(weights_quantization_info.uniform().scale, -weights_quantization_info.uniform().offset)); |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 114 | |
| 115 | _mm_gemmlowp = std::make_unique<ClGemmLowpMatrixMultiplyCore>(); |
| 116 | _mm_gemmlowp->configure(compile_context, &tmp_src, weights, biases, dst, gemm_info); |
| 117 | |
| 118 | // Revert back QuantizatioInfo as weights could be used in other convolution layers |
| 119 | weights->set_quantization_info(weights_quantization_info); |
| 120 | |
| 121 | auto mm_mem_req = _mm_gemmlowp->workspace(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 122 | for (unsigned int cont = 0; cont < mm_mem_req.size(); ++cont) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 123 | { |
| 124 | _aux_mem[cont] = mm_mem_req[cont]; |
| 125 | } |
| 126 | } |
| 127 | else |
| 128 | { |
| 129 | // Configure matrix multiply function |
| 130 | _mm_gemm = std::make_unique<ClGemm>(); |
| 131 | _mm_gemm->configure(compile_context, &tmp_src, weights, biases, dst, 1.0f, 1.0f, gemm_info); |
| 132 | auto mm_mem_req = _mm_gemm->workspace(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 133 | for (unsigned int cont = 0; cont < mm_mem_req.size(); ++cont) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 134 | { |
| 135 | _aux_mem[cont] = mm_mem_req[cont]; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 140 | Status ClGemmConv2d::validate_mm(const ITensorInfo *src, |
| 141 | const ITensorInfo *weights, |
| 142 | const ITensorInfo *biases, |
| 143 | const ITensorInfo *dst, |
| 144 | const GEMMLowpOutputStageInfo &gemmlowp_output_stage, |
| 145 | int gemm_3d_depth, |
| 146 | bool skip_im2col, |
| 147 | const ActivationLayerInfo &act_info) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 148 | { |
| 149 | const bool is_quantized = is_data_type_quantized_asymmetric(src->data_type()); |
| 150 | |
| 151 | const GEMMInfo &gemm_info = GEMMInfo(false, // is_a_reshaped |
| 152 | false, // is_b_reshaped |
| 153 | true, // reshape_b_only_on_first_run |
| 154 | gemm_3d_depth, // depth_output_gemm3d |
| 155 | skip_im2col, // reinterpret_input_as_3d |
| 156 | false, // retain_internal_weights |
| 157 | gemmlowp_output_stage, // gemmlowp_output_stage |
| 158 | false, // fast_math |
| 159 | false, // fp_mixed_precision |
| 160 | true, // broadcast_bias |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame] | 161 | act_info // activation_info |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 162 | ); |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 163 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 164 | if (is_quantized) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 165 | { |
| 166 | // Since we need negative offsets for computing convolution, we need to change QuantizationInfo() |
| 167 | // Extract and negate input and weights offset |
| 168 | const QuantizationInfo input_quantization_info = src->quantization_info(); |
| 169 | const QuantizationInfo weights_quantization_info = weights->quantization_info(); |
| 170 | |
| 171 | std::unique_ptr<ITensorInfo> src_qa = src->clone(); |
| 172 | std::unique_ptr<ITensorInfo> weights_qa = weights->clone(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 173 | src_qa->set_quantization_info( |
| 174 | QuantizationInfo(input_quantization_info.uniform().scale, -input_quantization_info.uniform().offset)); |
| 175 | weights_qa->set_quantization_info( |
| 176 | QuantizationInfo(weights_quantization_info.uniform().scale, -weights_quantization_info.uniform().offset)); |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 177 | |
| 178 | // Perform validation step on GEMMLowp |
| 179 | return ClGemmLowpMatrixMultiplyCore::validate(src_qa.get(), weights_qa.get(), biases, dst, gemm_info); |
| 180 | } |
| 181 | else |
| 182 | { |
| 183 | // Perform validation step on Matrix multiply function |
| 184 | return ClGemm::validate(src, weights, biases, dst, 1.0f, 1.0f, gemm_info); |
| 185 | } |
| 186 | } |
| 187 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 188 | void ClGemmConv2d::configure(const CLCompileContext &compile_context, |
| 189 | ITensorInfo *src, |
| 190 | ITensorInfo *weights, |
| 191 | ITensorInfo *biases, |
| 192 | ITensorInfo *dst, |
| 193 | const Conv2dInfo &conv2d_info, |
| 194 | const WeightsInfo &weights_info) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 195 | { |
| 196 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, weights, dst); |
| 197 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 198 | ARM_COMPUTE_ERROR_THROW_ON(ClGemmConv2d::validate(src, weights, biases, dst, conv2d_info, weights_info)); |
ramelg01 | 2e53f17 | 2021-09-22 10:48:25 +0100 | [diff] [blame] | 199 | ARM_COMPUTE_LOG_PARAMS(src, weights, biases, dst, conv2d_info, weights_info); |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 200 | |
| 201 | const DataType data_type = src->data_type(); |
| 202 | const DataLayout data_layout = src->data_layout(); |
| 203 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 204 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 205 | const int idx_kernels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES); |
| 206 | |
| 207 | const unsigned int kernel_width = weights->dimension(idx_width); |
| 208 | const unsigned int kernel_height = weights->dimension(idx_height); |
| 209 | const unsigned int num_kernels = weights->dimension(idx_kernels); |
| 210 | |
| 211 | const UniformQuantizationInfo iq_info = src->quantization_info().uniform(); |
| 212 | const UniformQuantizationInfo oq_info = dst->quantization_info().uniform(); |
| 213 | |
| 214 | _is_prepared = weights_info.retain_internal_weights(); |
| 215 | _is_quantized = is_data_type_quantized_asymmetric(src->data_type()); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 216 | _skip_im2col = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1 && |
| 217 | conv2d_info.conv_info.stride().first == 1 && conv2d_info.conv_info.stride().second == 1); |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 218 | _skip_col2im = data_layout == DataLayout::NHWC; |
| 219 | |
| 220 | // Only for quantize there are few cases where we cannot fuse the activation function in GEMM |
| 221 | _fuse_activation = true; |
| 222 | |
| 223 | const ITensorInfo *gemm_input_to_use = src; |
| 224 | ITensorInfo *gemm_output_to_use = dst; |
| 225 | |
| 226 | // Get parameters from conv_info |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame] | 227 | unsigned int stride_x = 0; |
| 228 | unsigned int stride_y = 0; |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 229 | std::tie(stride_x, stride_y) = conv2d_info.conv_info.stride(); |
| 230 | |
| 231 | // Get convolved dimensions |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame] | 232 | unsigned int conv_w = 0; |
| 233 | unsigned int conv_h = 0; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 234 | std::tie(conv_w, conv_h) = scaled_dimensions(src->dimension(idx_width), src->dimension(idx_height), kernel_width, |
| 235 | kernel_height, conv2d_info.conv_info, conv2d_info.dilation); |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 236 | |
| 237 | unsigned int mat_weights_cols = num_kernels / conv2d_info.num_groups; |
| 238 | |
| 239 | ITensorInfo *biases_to_use = biases; |
| 240 | _append_bias = false; |
| 241 | |
| 242 | _weights_reshape_kernel = std::make_unique<kernels::ClWeightsReshapeKernel>(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 243 | if (conv2d_info.num_groups != 1 && biases != nullptr) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 244 | { |
| 245 | // num_groups != 1 can only be for NCHW |
| 246 | // Since it is missing an utility function to reshape the biases, we append the biases into the weights tensor |
| 247 | biases_to_use = nullptr; |
| 248 | _append_bias = true; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 249 | _weights_reshape_kernel->configure(compile_context, weights, biases, &_weights_reshaped, |
| 250 | conv2d_info.num_groups); |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 251 | } |
| 252 | else |
| 253 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 254 | _weights_reshape_kernel->configure(compile_context, weights, nullptr, &_weights_reshaped, |
| 255 | conv2d_info.num_groups); |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | // Create tensor to store im2col reshaped inputs |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 259 | if (!_skip_im2col) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 260 | { |
| 261 | // Configure and tune im2col. im2col output shape is auto-initialized |
| 262 | _im2col_kernel = std::make_unique<opencl::kernels::ClIm2ColKernel>(); |
| 263 | |
| 264 | // Set the GPU target for im2col |
| 265 | _im2col_kernel->set_target(CLScheduler::get().target()); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 266 | _im2col_kernel->configure(compile_context, src, &_im2col_output, Size2D(kernel_width, kernel_height), |
| 267 | conv2d_info.conv_info, _append_bias, conv2d_info.dilation, conv2d_info.num_groups); |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 268 | |
| 269 | // Set quantization info |
| 270 | _im2col_output.set_quantization_info(src->quantization_info()); |
| 271 | CLScheduler::get().tune_kernel_static(*_im2col_kernel); |
| 272 | |
| 273 | // Update GEMM input |
| 274 | gemm_input_to_use = &_im2col_output; |
| 275 | } |
| 276 | |
| 277 | // Create GEMM output tensor |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 278 | if (!_skip_col2im) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 279 | { |
| 280 | TensorShape shape_gemm; |
| 281 | |
| 282 | // If we cannot skip col2im it means we run im2col as well |
| 283 | shape_gemm = _im2col_output.tensor_shape(); |
| 284 | shape_gemm.set(0, mat_weights_cols); |
| 285 | shape_gemm.set(1, conv_w * conv_h); |
| 286 | |
| 287 | _gemm_output = TensorInfo(shape_gemm, 1, data_type); |
| 288 | _gemm_output.set_quantization_info(dst->quantization_info()).set_data_layout(src->data_layout()); |
| 289 | |
| 290 | // Update GEMM output |
| 291 | gemm_output_to_use = &_gemm_output; |
| 292 | } |
| 293 | |
| 294 | GEMMLowpOutputStageInfo gemmlowp_output_stage; |
| 295 | gemmlowp_output_stage.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT; |
| 296 | gemmlowp_output_stage.gemmlowp_offset = 0; |
| 297 | |
| 298 | // Configure output stage for quantized case |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 299 | if (_is_quantized) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 300 | { |
| 301 | const auto output_quant_info = (dst->total_size() == 0) ? iq_info : oq_info; |
| 302 | const bool is_quantized_per_channel = is_data_type_quantized_per_channel(weights->data_type()); |
| 303 | const unsigned int num_filters = (is_quantized_per_channel) ? num_kernels : 1; |
| 304 | |
| 305 | gemmlowp_output_stage.is_quantized_per_channel = is_quantized_per_channel; |
| 306 | |
| 307 | gemmlowp_output_stage.gemmlowp_multipliers.resize(num_filters); |
| 308 | gemmlowp_output_stage.gemmlowp_shifts.resize(num_filters); |
| 309 | quantization::compute_quantized_multipliers_and_shifts(src, weights, dst, |
| 310 | gemmlowp_output_stage.gemmlowp_multipliers.data(), |
| 311 | gemmlowp_output_stage.gemmlowp_shifts.data()); |
| 312 | gemmlowp_output_stage.gemmlowp_multiplier = gemmlowp_output_stage.gemmlowp_multipliers[0]; |
| 313 | gemmlowp_output_stage.gemmlowp_shift = gemmlowp_output_stage.gemmlowp_shifts[0]; |
| 314 | |
| 315 | PixelValue min_val{}; |
| 316 | PixelValue max_val{}; |
| 317 | std::tie(min_val, max_val) = get_min_max(dst->data_type()); |
| 318 | |
| 319 | auto min_activation = min_val.get<int32_t>(); |
| 320 | auto max_activation = max_val.get<int32_t>(); |
| 321 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 322 | const std::set<ActivationLayerInfo::ActivationFunction> supported_acts = { |
| 323 | ActivationLayerInfo::ActivationFunction::RELU, ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, |
| 324 | ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU}; |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 325 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 326 | if (conv2d_info.act_info.enabled()) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 327 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 328 | if (supported_acts.count(conv2d_info.act_info.activation()) != 0) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 329 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 330 | std::tie(min_activation, max_activation) = |
| 331 | get_quantized_activation_min_max(conv2d_info.act_info, data_type, output_quant_info); |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 332 | } |
| 333 | else |
| 334 | { |
| 335 | _fuse_activation = false; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | // Set the GEMMLowp output stage info |
| 340 | gemmlowp_output_stage.gemmlowp_offset = output_quant_info.offset; |
| 341 | gemmlowp_output_stage.gemmlowp_min_bound = min_activation; |
| 342 | gemmlowp_output_stage.gemmlowp_max_bound = max_activation; |
| 343 | } |
| 344 | |
| 345 | // Configure and tune GEMM |
| 346 | // In case of NHWC, we need to run GEMM3D (gemm_3d_depth != 0) in order to avoid reshaping the output matrix |
| 347 | const unsigned int gemm_3d_depth = (data_layout == DataLayout::NHWC) ? conv_h : 0; |
| 348 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 349 | configure_mm(compile_context, gemm_input_to_use, &_weights_reshaped, biases_to_use, gemm_output_to_use, |
| 350 | gemmlowp_output_stage, gemm_3d_depth, conv2d_info.act_info); |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 351 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 352 | if (!_skip_col2im) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 353 | { |
| 354 | // Set the GPU target for col2im |
| 355 | _col2im_kernel = std::make_unique<opencl::kernels::ClCol2ImKernel>(); |
| 356 | _col2im_kernel->set_target(CLScheduler::get().target()); |
| 357 | // Configure and tune Col2Im |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 358 | _col2im_kernel->configure(compile_context, gemm_output_to_use, dst, Size2D(conv_w, conv_h), |
| 359 | conv2d_info.num_groups); |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 360 | CLScheduler::get().tune_kernel_static(*_col2im_kernel.get()); |
| 361 | } |
| 362 | |
| 363 | ARM_COMPUTE_ERROR_ON_MSG((dst->dimension(idx_width) != conv_w) || (dst->dimension(idx_height) != conv_h), |
| 364 | "Output shape does not match the expected one"); |
| 365 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 366 | if (!_fuse_activation) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 367 | { |
| 368 | _activation_kernel = std::make_unique<opencl::kernels::ClActivationKernel>(); |
| 369 | _activation_kernel->configure(compile_context, dst, nullptr, conv2d_info.act_info); |
| 370 | } |
| 371 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 372 | _aux_mem[Im2ColOutput] = |
| 373 | MemoryInfo(offset_int_vec(Im2ColOutput), MemoryLifetime::Temporary, _im2col_output.total_size()); |
| 374 | _aux_mem[WeightsReshaped] = |
| 375 | MemoryInfo(offset_int_vec(WeightsReshaped), MemoryLifetime::Persistent, _weights_reshaped.total_size()); |
| 376 | _aux_mem[GemmOutput] = MemoryInfo(offset_int_vec(GemmOutput), MemoryLifetime::Temporary, _gemm_output.total_size()); |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 377 | } |
| 378 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 379 | Status ClGemmConv2d::validate(const ITensorInfo *src, |
| 380 | const ITensorInfo *weights, |
| 381 | const ITensorInfo *biases, |
| 382 | const ITensorInfo *dst, |
| 383 | const Conv2dInfo &conv2d_info, |
Georgios Pinitas | 1988463 | 2021-08-16 12:38:54 +0100 | [diff] [blame] | 384 | const WeightsInfo &weights_info) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 385 | { |
| 386 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, weights, dst); |
| 387 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights_info.are_reshaped(), "Weights already reshaped are not supported!"); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 388 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, |
| 389 | DataType::F16, DataType::F32); |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 390 | const bool is_quantized_per_channel = is_data_type_quantized_per_channel(weights->data_type()); |
| 391 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 392 | if (!is_quantized_per_channel) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 393 | { |
| 394 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, weights); |
| 395 | } |
| 396 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(src, weights); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 397 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((conv2d_info.num_groups != 1) && (src->data_layout() != DataLayout::NCHW), |
| 398 | "Grouping (num_groups != 1) with NHWC data layout is not supported"); |
| 399 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((conv2d_info.num_groups != 1) && (src->data_type() == DataType::QASYMM8), |
| 400 | "Grouping (num_groups != 1) is not supported with QASYMM8"); |
| 401 | ARM_COMPUTE_RETURN_ERROR_ON(((src->dimension(2) / weights->dimension(2)) != conv2d_info.num_groups) && |
| 402 | (src->data_layout() == DataLayout::NCHW)); |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 403 | |
| 404 | const DataLayout data_layout = src->data_layout(); |
| 405 | const DataType data_type = src->data_type(); |
| 406 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 407 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 408 | const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
| 409 | const int idx_kernels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES); |
| 410 | |
| 411 | const unsigned int kernel_width = weights->dimension(idx_width); |
| 412 | const unsigned int kernel_height = weights->dimension(idx_height); |
| 413 | const unsigned int num_kernels = weights->dimension(idx_kernels); |
| 414 | |
| 415 | TensorInfo im2col_reshaped_info{}; |
| 416 | TensorInfo info_gemm{}; |
| 417 | TensorInfo weights_reshaped_info{}; |
| 418 | const ITensorInfo *gemm_input_to_use = src; |
| 419 | const ITensorInfo *gemm_output_to_use = dst; |
| 420 | const ITensorInfo *weights_to_use = weights; |
| 421 | const bool is_quantized = is_data_type_quantized_asymmetric(data_type); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 422 | const bool skip_im2col = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1 && |
| 423 | conv2d_info.conv_info.stride().first == 1 && conv2d_info.conv_info.stride().second == 1); |
| 424 | const bool skip_col2im = data_layout == DataLayout::NHWC; |
| 425 | bool fuse_activation = true; |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 426 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 427 | ARM_COMPUTE_RETURN_ERROR_ON((weights->dimension(idx_channel) * conv2d_info.num_groups) != |
| 428 | src->dimension(idx_channel)); |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 429 | ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4); |
| 430 | |
| 431 | // Validate biases |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 432 | if (biases != nullptr) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 433 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 434 | if (is_quantized) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 435 | { |
| 436 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32); |
| 437 | } |
| 438 | else |
| 439 | { |
| 440 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, biases); |
| 441 | } |
| 442 | ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(idx_kernels)); |
| 443 | ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1); |
| 444 | } |
| 445 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 446 | if (conv2d_info.act_info.enabled()) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 447 | { |
| 448 | ARM_COMPUTE_ERROR_ON(conv2d_info.act_info.b() > conv2d_info.act_info.a()); |
| 449 | } |
| 450 | |
| 451 | // Get convolved dimensions |
| 452 | unsigned int conv_w = 0; |
| 453 | unsigned int conv_h = 0; |
| 454 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 455 | std::tie(conv_w, conv_h) = scaled_dimensions(src->dimension(idx_width), src->dimension(idx_height), kernel_width, |
| 456 | kernel_height, conv2d_info.conv_info, conv2d_info.dilation); |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 457 | |
| 458 | unsigned int mat_weights_cols = num_kernels / conv2d_info.num_groups; |
| 459 | |
| 460 | const ITensorInfo *biases_to_use = biases; |
| 461 | bool append_bias = false; |
| 462 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 463 | if (conv2d_info.num_groups != 1 && biases != nullptr) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 464 | { |
| 465 | // num_groups != 1 can only be for NCHW |
| 466 | // Since it is missing an utility function to reshape the biases, we append the biases into the weights tensor |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 467 | biases_to_use = nullptr; |
| 468 | append_bias = true; |
| 469 | weights_reshaped_info = |
| 470 | TensorInfo(compute_weights_reshaped_shape(*weights, true, conv2d_info.num_groups), 1, data_type); |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 471 | } |
| 472 | else |
| 473 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 474 | weights_reshaped_info = |
| 475 | TensorInfo(compute_weights_reshaped_shape(*weights, false, conv2d_info.num_groups), 1, data_type); |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | weights_to_use = &weights_reshaped_info; |
| 479 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 480 | if (!skip_im2col) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 481 | { |
| 482 | const Size2D kernel_dims(kernel_width, kernel_height); |
| 483 | |
| 484 | // Output tensor auto initialization if not yet initialized |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 485 | TensorShape expected_output_shape = |
| 486 | compute_im2col_conv_shape(src, kernel_dims, conv2d_info.conv_info, append_bias, conv2d_info.dilation, |
| 487 | conv2d_info.num_groups == 1, conv2d_info.num_groups); |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 488 | |
| 489 | auto_init_if_empty(im2col_reshaped_info, src->clone()->set_tensor_shape(expected_output_shape)); |
| 490 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 491 | ARM_COMPUTE_RETURN_ON_ERROR( |
| 492 | opencl::kernels::ClIm2ColKernel::validate(src, &im2col_reshaped_info, kernel_dims, conv2d_info.conv_info, |
| 493 | append_bias, conv2d_info.dilation, conv2d_info.num_groups)); |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 494 | gemm_input_to_use = &im2col_reshaped_info; |
| 495 | } |
| 496 | |
| 497 | // Create GEMM output tensor |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 498 | if (!skip_col2im) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 499 | { |
| 500 | TensorShape shape_gemm; |
| 501 | |
| 502 | shape_gemm = gemm_input_to_use->tensor_shape(); |
| 503 | shape_gemm.set(0, mat_weights_cols); |
| 504 | shape_gemm.set(1, conv_w * conv_h); |
| 505 | |
| 506 | info_gemm = TensorInfo(shape_gemm, 1, data_type); |
| 507 | info_gemm.set_quantization_info(dst->quantization_info()).set_data_layout(src->data_layout()); |
| 508 | gemm_output_to_use = &info_gemm; |
| 509 | } |
| 510 | |
| 511 | GEMMLowpOutputStageInfo gemmlowp_output_stage; |
| 512 | gemmlowp_output_stage.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT; |
| 513 | gemmlowp_output_stage.gemmlowp_offset = 0; |
| 514 | gemmlowp_output_stage.is_quantized_per_channel = is_quantized_per_channel; |
| 515 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 516 | if (is_quantized) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 517 | { |
| 518 | const UniformQuantizationInfo iq_info = src->quantization_info().uniform(); |
| 519 | const UniformQuantizationInfo oq_info = dst->quantization_info().uniform(); |
| 520 | const auto output_quant_info = (dst->total_size() == 0) ? iq_info : oq_info; |
| 521 | const unsigned int num_filters = (is_quantized_per_channel) ? num_kernels : 1; |
| 522 | |
| 523 | gemmlowp_output_stage.gemmlowp_multipliers.resize(num_filters); |
| 524 | gemmlowp_output_stage.gemmlowp_shifts.resize(num_filters); |
| 525 | quantization::compute_quantized_multipliers_and_shifts(src, weights, dst, |
| 526 | gemmlowp_output_stage.gemmlowp_multipliers.data(), |
| 527 | gemmlowp_output_stage.gemmlowp_shifts.data()); |
| 528 | gemmlowp_output_stage.gemmlowp_multiplier = gemmlowp_output_stage.gemmlowp_multipliers[0]; |
| 529 | gemmlowp_output_stage.gemmlowp_shift = gemmlowp_output_stage.gemmlowp_shifts[0]; |
| 530 | |
| 531 | int min_activation = 0; |
| 532 | int max_activation = 0; |
| 533 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 534 | const std::set<ActivationLayerInfo::ActivationFunction> supported_acts = { |
| 535 | ActivationLayerInfo::ActivationFunction::RELU, ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, |
| 536 | ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU}; |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 537 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 538 | if (conv2d_info.act_info.enabled()) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 539 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 540 | if (supported_acts.count(conv2d_info.act_info.activation()) != 0) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 541 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 542 | std::tie(min_activation, max_activation) = |
| 543 | get_quantized_activation_min_max(conv2d_info.act_info, data_type, output_quant_info); |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 544 | } |
| 545 | else |
| 546 | { |
| 547 | fuse_activation = false; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | // Set the GEMMLowp output stage info |
| 552 | gemmlowp_output_stage.gemmlowp_offset = output_quant_info.offset; |
| 553 | gemmlowp_output_stage.gemmlowp_min_bound = min_activation; |
| 554 | gemmlowp_output_stage.gemmlowp_max_bound = max_activation; |
| 555 | } |
| 556 | |
| 557 | // In case of NHWC, we need to run GEMM3D (gemm_3d_depth != 0) in order to avoid reshaping the output matrix |
| 558 | const unsigned int gemm_3d_depth = (data_layout == DataLayout::NHWC) ? conv_h : 0; |
| 559 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 560 | ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(gemm_input_to_use, weights_to_use, biases_to_use, gemm_output_to_use, |
| 561 | gemmlowp_output_stage, gemm_3d_depth, skip_im2col, conv2d_info.act_info)); |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 562 | |
| 563 | // Validate Col2Im |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 564 | if (!skip_col2im) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 565 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 566 | ARM_COMPUTE_RETURN_ON_ERROR( |
| 567 | kernels::ClCol2ImKernel::validate(gemm_output_to_use, dst, Size2D(conv_w, conv_h), conv2d_info.num_groups)); |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 568 | } |
| 569 | |
SiCongLi | 579ca84 | 2021-10-18 09:38:33 +0100 | [diff] [blame] | 570 | // Validate Activation Layer |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 571 | if (!fuse_activation) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 572 | { |
| 573 | ARM_COMPUTE_RETURN_ON_ERROR(kernels::ClActivationKernel::validate(dst, nullptr, conv2d_info.act_info)); |
| 574 | } |
| 575 | |
| 576 | return Status{}; |
| 577 | } |
| 578 | |
Georgios Pinitas | 1988463 | 2021-08-16 12:38:54 +0100 | [diff] [blame] | 579 | void ClGemmConv2d::run(ITensorPack &tensors) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 580 | { |
| 581 | prepare(tensors); |
| 582 | |
| 583 | auto src = tensors.get_const_tensor(ACL_SRC_0); |
| 584 | auto biases = tensors.get_const_tensor(ACL_SRC_2); |
| 585 | auto dst = tensors.get_tensor(ACL_DST); |
| 586 | auto gemm_input_to_use = src; |
| 587 | auto gemm_output_to_use = dst; |
| 588 | |
| 589 | CLAuxTensorHandler im2col_output(offset_int_vec(Im2ColOutput), _im2col_output, tensors, false); |
| 590 | CLAuxTensorHandler gemm_output(offset_int_vec(GemmOutput), _gemm_output, tensors, false); |
| 591 | CLAuxTensorHandler weights_reshaped(offset_int_vec(WeightsReshaped), _weights_reshaped, tensors, false); |
| 592 | |
| 593 | // Run im2col |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 594 | if (!_skip_im2col) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 595 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 596 | ITensorPack pack = {{TensorType::ACL_SRC, src}, {TensorType::ACL_DST, im2col_output.get()}}; |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 597 | CLScheduler::get().enqueue_op(*_im2col_kernel, pack, false); |
| 598 | gemm_input_to_use = im2col_output.get(); |
| 599 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 600 | if (!_skip_col2im) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 601 | { |
| 602 | gemm_output_to_use = gemm_output.get(); |
| 603 | } |
| 604 | ITensorPack pack_mm = tensors; |
| 605 | pack_mm.add_const_tensor(TensorType::ACL_SRC_0, gemm_input_to_use); |
| 606 | pack_mm.add_const_tensor(TensorType::ACL_SRC_1, weights_reshaped.get()); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 607 | if (!_append_bias) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 608 | { |
| 609 | pack_mm.add_const_tensor(TensorType::ACL_SRC_2, biases); |
| 610 | } |
| 611 | pack_mm.add_tensor(TensorType::ACL_DST, gemm_output_to_use); |
| 612 | // Runs ClGemm or ClGemmLowpMatrixMultiplyCore functions |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 613 | if (_is_quantized) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 614 | { |
| 615 | // Run gemmlowp |
| 616 | _mm_gemmlowp->run(pack_mm); |
| 617 | } |
| 618 | else |
| 619 | { |
| 620 | // Run gemm |
| 621 | _mm_gemm->run(pack_mm); |
| 622 | } |
| 623 | |
| 624 | // Reshape output matrix |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 625 | if (!_skip_col2im) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 626 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 627 | ITensorPack pack = {{TensorType::ACL_SRC, gemm_output_to_use}, {TensorType::ACL_DST, dst}}; |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 628 | CLScheduler::get().enqueue_op(*_col2im_kernel.get(), pack, false); |
| 629 | } |
| 630 | |
| 631 | //Run Activation Layer if we cannot fuse in GEMM |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 632 | if (!_fuse_activation) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 633 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 634 | ITensorPack pack = {{TensorType::ACL_SRC, dst}, {TensorType::ACL_DST, dst}}; |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 635 | CLScheduler::get().enqueue_op(*_activation_kernel.get(), pack, false); |
| 636 | } |
| 637 | } |
| 638 | |
Georgios Pinitas | 1988463 | 2021-08-16 12:38:54 +0100 | [diff] [blame] | 639 | void ClGemmConv2d::prepare(ITensorPack &tensors) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 640 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 641 | if (!_is_prepared) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 642 | { |
| 643 | // Run weights reshaping and mark original weights tensor as unused |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 644 | ICLTensor *weights_reshaped_p = |
| 645 | utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(offset_int_vec(WeightsReshaped))); |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 646 | CLAuxTensorHandler weights_reshaped(_weights_reshaped, *weights_reshaped_p); |
| 647 | auto weights = tensors.get_const_tensor(TensorType::ACL_SRC_1); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 648 | ITensorPack pack = {{TensorType::ACL_SRC, weights}, {TensorType::ACL_DST, weights_reshaped.get()}}; |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 649 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 650 | if (_append_bias) |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 651 | { |
| 652 | const auto biases = tensors.get_const_tensor(TensorType::ACL_SRC_2); |
| 653 | pack.add_const_tensor(TensorType::ACL_BIAS, biases); |
| 654 | } |
| 655 | CLScheduler::get().enqueue_op(*_weights_reshape_kernel.get(), pack, true); |
| 656 | tensors.add_const_tensor(TensorType::ACL_SRC_1, weights_reshaped.get()); |
| 657 | |
| 658 | // Prepare GEMM |
| 659 | _is_quantized ? _mm_gemmlowp->prepare(tensors) : _mm_gemm->prepare(tensors); |
| 660 | _is_prepared = true; |
| 661 | } |
| 662 | } |
Georgios Pinitas | 1988463 | 2021-08-16 12:38:54 +0100 | [diff] [blame] | 663 | experimental::MemoryRequirements ClGemmConv2d::workspace() const |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 664 | { |
| 665 | return _aux_mem; |
| 666 | } |
| 667 | } // namespace opencl |
| 668 | } // namespace arm_compute |