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