| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | // |
| 5 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | // in compliance with the License. You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://opensource.org/licenses/BSD-3-Clause |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software distributed |
| 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | // specific language governing permissions and limitations under the License. |
| 14 | |
| 15 | #include "convolution_arm.h" |
| 16 | |
| 17 | #include "benchmark.h" |
| 18 | #include "cpu.h" |
| 19 | #include "layer_type.h" |
| 20 | |
| 21 | #if __ARM_NEON |
| 22 | #include <arm_neon.h> |
| 23 | #endif // __ARM_NEON |
| 24 | |
| 25 | #include "arm_activation.h" |
| 26 | #include "arm_usability.h" |
| 27 | |
| 28 | namespace ncnn { |
| 29 | |
| 30 | #if NCNN_GNU_INLINE_ASM |
| 31 | #include "convolution_1x1.h" |
| 32 | #include "convolution_2x2.h" |
| 33 | #include "convolution_3x3.h" |
| 34 | #include "convolution_4x4.h" |
| 35 | #include "convolution_5x5.h" |
| 36 | #include "convolution_7x7.h" |
| 37 | #endif // NCNN_GNU_INLINE_ASM |
| 38 | |
| 39 | #include "convolution_packed.h" |
| 40 | #include "convolution_3x3_winograd.h" |
| 41 | #include "convolution_im2col_gemm.h" |
| 42 | |
| 43 | #if NCNN_BF16 |
| 44 | #include "convolution_packed_bf16s.h" |
| 45 | #include "convolution_3x3_winograd_bf16s.h" |
| 46 | #include "convolution_im2col_gemm_bf16s_fp16s.h" |
| 47 | #include "convolution_im2col_gemm_bf16s.h" |
| 48 | #endif // NCNN_BF16 |
| 49 | |
| 50 | #if NCNN_INT8 |
| 51 | #include "convolution_packed_int8.h" |
| 52 | #include "convolution_im2col_gemm_int8.h" |
| 53 | #include "convolution_3x3_winograd_int8.h" |
| 54 | |
| 55 | // #include "convolution_3x3_int8.h" |
| 56 | #endif // NCNN_INT8 |
| 57 | |
| 58 | #if __ARM_NEON |
| 59 | #if NCNN_GNU_INLINE_ASM |
| 60 | #include "convolution_3x3_pack1to4.h" |
| 61 | #include "convolution_3x3_pack4.h" |
| 62 | #include "convolution_3x3_pack4to1.h" |
| 63 | #include "convolution_5x5_pack4.h" |
| 64 | #include "convolution_7x7_pack1to4.h" |
| 65 | |
| 66 | #if NCNN_BF16 |
| 67 | #include "convolution_3x3_pack1to4_bf16s.h" |
| 68 | #include "convolution_3x3_pack4_bf16s.h" |
| 69 | #include "convolution_5x5_pack4_bf16s.h" |
| 70 | #include "convolution_7x7_pack1to4_bf16s.h" |
| 71 | #endif // NCNN_BF16 |
| 72 | #endif // NCNN_GNU_INLINE_ASM |
| 73 | #endif // __ARM_NEON |
| 74 | |
| 75 | Convolution_arm::Convolution_arm() |
| 76 | { |
| 77 | #if __ARM_NEON |
| 78 | support_packing = true; |
| 79 | #if NCNN_ARM82 |
| 80 | support_fp16_storage = cpu_support_arm_asimdhp(); |
| 81 | #endif |
| 82 | #endif // __ARM_NEON |
| 83 | |
| 84 | #if NCNN_BF16 |
| 85 | support_bf16_storage = true; |
| 86 | #endif |
| 87 | |
| 88 | activation = 0; |
| 89 | nT = 0; |
| 90 | convolution_dilation1 = 0; |
| 91 | } |
| 92 | |
| 93 | static void convolution_transform_kernel_packed_neon(const Mat& weight_data, Mat& weight_data_tm, int num_input, int num_output, int kernel_w, int kernel_h, int elempack, int out_elempack) |
| 94 | { |
| 95 | const int maxk = kernel_w * kernel_h; |
| 96 | |
| 97 | // src = kw-kh-inch-outch |
| 98 | // dst = pb-pa-kw-kh-inch/pa-outch/pb |
| 99 | { |
| 100 | Mat weight_data_r2 = weight_data.reshape(maxk, num_input, num_output); |
| 101 | |
| 102 | weight_data_tm.create(maxk, num_input / elempack, num_output / out_elempack, (size_t)4u * elempack * out_elempack, elempack * out_elempack); |
| 103 | |
| 104 | for (int q = 0; q + (out_elempack - 1) < num_output; q += out_elempack) |
| 105 | { |
| 106 | float* g00 = weight_data_tm.channel(q / out_elempack); |
| 107 | |
| 108 | for (int p = 0; p + (elempack - 1) < num_input; p += elempack) |
| 109 | { |
| 110 | for (int k = 0; k < maxk; k++) |
| 111 | { |
| 112 | for (int i = 0; i < elempack; i++) |
| 113 | { |
| 114 | for (int j = 0; j < out_elempack; j++) |
| 115 | { |
| 116 | const float* k00 = weight_data_r2.channel(q + j).row(p + i); |
| 117 | |
| 118 | g00[0] = k00[k]; |
| 119 | |
| 120 | g00++; |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | int Convolution_arm::create_pipeline(const Option& opt) |
| 130 | { |
| 131 | if (dynamic_weight) |
| 132 | return 0; |
| 133 | |
| 134 | activation = create_activation_layer(activation_type, activation_params, opt); |
| 135 | nT = opt.num_threads; |
| 136 | |
| 137 | #if NCNN_INT8 |
| 138 | if (opt.use_int8_inference && weight_data.elemsize == (size_t)1u) |
| 139 | { |
| 140 | return create_pipeline_int8_arm(opt); |
| 141 | } |
| 142 | #endif |
| 143 | |
| 144 | #if NCNN_ARM82 |
| 145 | if (support_fp16_storage && opt.use_fp16_storage) |
| 146 | { |
| 147 | return create_pipeline_fp16s(opt); |
| 148 | } |
| 149 | #endif |
| 150 | |
| 151 | #if NCNN_BF16 |
| 152 | if (opt.use_bf16_storage) |
| 153 | { |
| 154 | return create_pipeline_bf16s(opt); |
| 155 | } |
| 156 | #endif |
| 157 | |
| 158 | if ((!support_packing || !opt.use_packing_layout) && !opt.use_bf16_storage && kernel_w == kernel_h && dilation_w != 1 && dilation_h == dilation_w && stride_w == 1 && stride_h == 1) |
| 159 | { |
| 160 | convolution_dilation1 = ncnn::create_layer_cpu(ncnn::LayerType::Convolution); |
| 161 | |
| 162 | // set param |
| 163 | ncnn::ParamDict pd; |
| 164 | pd.set(0, num_output); // num_output |
| 165 | pd.set(1, kernel_w); |
| 166 | pd.set(11, kernel_h); |
| 167 | pd.set(2, 1); |
| 168 | pd.set(12, 1); |
| 169 | pd.set(3, 1); // stride_w |
| 170 | pd.set(13, 1); // stride_h |
| 171 | pd.set(4, 0); // pad_w |
| 172 | pd.set(14, 0); // pad_h |
| 173 | pd.set(5, bias_term); |
| 174 | pd.set(6, weight_data_size); |
| 175 | |
| 176 | convolution_dilation1->load_param(pd); |
| 177 | |
| 178 | // set weights |
| 179 | if (bias_term) |
| 180 | { |
| 181 | ncnn::Mat weights[2]; |
| 182 | weights[0] = weight_data; |
| 183 | weights[1] = bias_data; |
| 184 | |
| 185 | convolution_dilation1->load_model(ModelBinFromMatArray(weights)); |
| 186 | } |
| 187 | else |
| 188 | { |
| 189 | ncnn::Mat weights[1]; |
| 190 | weights[0] = weight_data; |
| 191 | |
| 192 | convolution_dilation1->load_model(ModelBinFromMatArray(weights)); |
| 193 | } |
| 194 | |
| 195 | convolution_dilation1->create_pipeline(opt); |
| 196 | |
| 197 | if (opt.lightmode) |
| 198 | weight_data.release(); |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | const int maxk = kernel_w * kernel_h; |
| 204 | const int num_input = weight_data_size / maxk / num_output; |
| 205 | |
| 206 | int elempack = 1; |
| 207 | int out_elempack = 1; |
| 208 | #if __ARM_NEON |
| 209 | if (opt.use_packing_layout) |
| 210 | { |
| 211 | elempack = num_input % 4 == 0 ? 4 : 1; |
| 212 | out_elempack = num_output % 4 == 0 ? 4 : 1; |
| 213 | } |
| 214 | #endif |
| 215 | |
| 216 | bool prefer_winograd = (opt.use_winograd23_convolution || opt.use_winograd43_convolution || opt.use_winograd63_convolution) && (num_input >= 8 || num_output >= 8); |
| 217 | |
| 218 | if (opt.use_winograd_convolution && prefer_winograd && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 219 | { |
| 220 | // dynamic shape |
| 221 | if (opt.use_winograd63_convolution && (num_input <= 128 && num_output <= 128)) |
| 222 | conv3x3s1_winograd63_transform_kernel(weight_data, weight_winograd63_data, num_input, num_output, opt); |
| 223 | else if (opt.use_winograd43_convolution && (num_input >= 8 && num_output >= 8)) |
| 224 | conv3x3s1_winograd43_transform_kernel(weight_data, weight_winograd43_data, num_input, num_output, opt); |
| 225 | else |
| 226 | conv3x3s1_winograd23_transform_kernel(weight_data, weight_winograd23_data, num_input, num_output, opt); |
| 227 | |
| 228 | if (opt.lightmode) |
| 229 | weight_data.release(); |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | int l2_cache_size_fp32 = get_cpu_level2_cache_size() / sizeof(float); |
| 235 | bool prefer_sgemm = num_input * num_output * kernel_w * kernel_h * dilation_w * dilation_h * stride_w * stride_h * 2 > l2_cache_size_fp32 || (num_input > 16 || num_output > 16); |
| 236 | |
| 237 | #if NCNN_GNU_INLINE_ASM |
| 238 | if (elempack == 4 && out_elempack == 4) |
| 239 | { |
| 240 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2 && (num_input < 4 || num_output < 32)) |
| 241 | { |
| 242 | prefer_sgemm = false; |
| 243 | } |
| 244 | if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 245 | { |
| 246 | prefer_sgemm = false; |
| 247 | } |
| 248 | if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2 && (num_input < 8 || num_output < 44)) |
| 249 | { |
| 250 | prefer_sgemm = false; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | if (elempack == 1 && out_elempack == 4) |
| 255 | { |
| 256 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 257 | { |
| 258 | prefer_sgemm = false; |
| 259 | } |
| 260 | else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 261 | { |
| 262 | prefer_sgemm = false; |
| 263 | } |
| 264 | else if (kernel_w == 7 && kernel_h == 7 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 265 | { |
| 266 | prefer_sgemm = false; |
| 267 | } |
| 268 | } |
| 269 | #endif // NCNN_GNU_INLINE_ASM |
| 270 | |
| 271 | if ((opt.use_sgemm_convolution && prefer_sgemm) || (kernel_w == 1 && kernel_h == 1)) |
| 272 | { |
| 273 | convolution_im2col_gemm_transform_kernel(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h, opt); |
| 274 | |
| 275 | if (opt.lightmode) |
| 276 | weight_data.release(); |
| 277 | |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | #if NCNN_GNU_INLINE_ASM |
| 282 | if ((elempack == 4 && out_elempack == 4 && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 283 | || (elempack == 4 && out_elempack == 4 && kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 284 | || (elempack == 4 && out_elempack == 4 && kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 285 | || (elempack == 1 && out_elempack == 4 && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 286 | || (elempack == 1 && out_elempack == 4 && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 287 | || (elempack == 1 && out_elempack == 4 && kernel_w == 7 && kernel_h == 7 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)) |
| 288 | { |
| 289 | convolution_transform_kernel_packed_neon(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h, elempack, out_elempack); |
| 290 | } |
| 291 | else if (elempack == 1 && out_elempack == 1 && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 292 | { |
| 293 | conv3x3s2_transform_kernel_neon(weight_data, weight_3x3s2_data, num_input, num_output); |
| 294 | } |
| 295 | else if ((elempack == 1 && out_elempack == 1 && kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 296 | || (elempack == 1 && out_elempack == 1 && kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 297 | || (elempack == 1 && out_elempack == 1 && kernel_w == 4 && kernel_h == 4 && dilation_w == 1 && dilation_h == 1 && stride_w == 4 && stride_h == 4) |
| 298 | || (elempack == 1 && out_elempack == 1 && kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 299 | || (elempack == 1 && out_elempack == 1 && kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 300 | || (elempack == 1 && out_elempack == 1 && kernel_w == 7 && kernel_h == 7 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 301 | || (elempack == 1 && out_elempack == 1 && kernel_w == 7 && kernel_h == 7 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)) |
| 302 | { |
| 303 | weight_data_tm = weight_data; |
| 304 | } |
| 305 | else |
| 306 | #endif // NCNN_GNU_INLINE_ASM |
| 307 | { |
| 308 | convolution_transform_kernel_packed(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h); |
| 309 | } |
| 310 | |
| 311 | if (opt.lightmode) |
| 312 | weight_data.release(); |
| 313 | |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | int Convolution_arm::destroy_pipeline(const Option& opt) |
| 318 | { |
| 319 | if (activation) |
| 320 | { |
| 321 | activation->destroy_pipeline(opt); |
| 322 | delete activation; |
| 323 | activation = 0; |
| 324 | } |
| 325 | |
| 326 | if (convolution_dilation1) |
| 327 | { |
| 328 | convolution_dilation1->destroy_pipeline(opt); |
| 329 | delete convolution_dilation1; |
| 330 | convolution_dilation1 = 0; |
| 331 | } |
| 332 | |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | int Convolution_arm::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 337 | { |
| 338 | #if NCNN_INT8 |
| 339 | if (opt.use_int8_inference && int8_scale_term) |
| 340 | { |
| 341 | return forward_int8_arm(bottom_blob, top_blob, opt); |
| 342 | } |
| 343 | #endif |
| 344 | |
| 345 | // flattened blob, implement as InnerProduct |
| 346 | if (bottom_blob.dims == 1 && kernel_w == 1 && kernel_h == 1) |
| 347 | { |
| 348 | Mat bottom_blob_3d; |
| 349 | if (bottom_blob.elemsize % 16 == 0) |
| 350 | { |
| 351 | bottom_blob_3d = bottom_blob; |
| 352 | bottom_blob_3d.dims = 3; |
| 353 | bottom_blob_3d.w = 1; |
| 354 | bottom_blob_3d.h = 1; |
| 355 | bottom_blob_3d.c = bottom_blob.w; |
| 356 | bottom_blob_3d.cstep = 1; |
| 357 | } |
| 358 | else |
| 359 | { |
| 360 | bottom_blob_3d = bottom_blob.reshape(1, 1, bottom_blob.w, opt.workspace_allocator); |
| 361 | } |
| 362 | |
| 363 | Mat top_blob_3d; |
| 364 | int ret = forward(bottom_blob_3d, top_blob_3d, opt); |
| 365 | if (ret != 0) |
| 366 | return ret; |
| 367 | |
| 368 | if (top_blob_3d.elemsize % 16 == 0) |
| 369 | { |
| 370 | top_blob = top_blob_3d; |
| 371 | top_blob.dims = 1; |
| 372 | top_blob.w = top_blob_3d.c; |
| 373 | top_blob.h = 1; |
| 374 | top_blob.c = 1; |
| 375 | bottom_blob_3d.cstep = top_blob_3d.c; |
| 376 | } |
| 377 | else |
| 378 | { |
| 379 | top_blob = top_blob_3d.reshape(top_blob_3d.c, opt.blob_allocator); |
| 380 | } |
| 381 | |
| 382 | return 0; |
| 383 | } |
| 384 | |
| 385 | int elembits = bottom_blob.elembits(); |
| 386 | |
| 387 | #if NCNN_ARM82 |
| 388 | if (support_fp16_storage && opt.use_fp16_storage && elembits == 16) |
| 389 | { |
| 390 | if (opt.use_fp16_arithmetic) |
| 391 | return forward_fp16sa(bottom_blob, top_blob, opt); |
| 392 | else |
| 393 | return forward_fp16s(bottom_blob, top_blob, opt); |
| 394 | } |
| 395 | #endif |
| 396 | |
| 397 | #if NCNN_BF16 |
| 398 | if (opt.use_bf16_storage && elembits == 16) |
| 399 | return forward_bf16s(bottom_blob, top_blob, opt); |
| 400 | #endif |
| 401 | |
| 402 | int w = bottom_blob.w; |
| 403 | int h = bottom_blob.h; |
| 404 | int channels = bottom_blob.c; |
| 405 | size_t elemsize = bottom_blob.elemsize; |
| 406 | int elempack = bottom_blob.elempack; |
| 407 | |
| 408 | // NCNN_LOGE("Convolution input %d x %d pad = %d %d ksize=%d %d stride=%d %d", w, h, pad_w, pad_h, kernel_w, kernel_h, stride_w, stride_h); |
| 409 | |
| 410 | const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1; |
| 411 | const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1; |
| 412 | |
| 413 | Mat bottom_blob_bordered; |
| 414 | make_padding(bottom_blob, bottom_blob_bordered, opt); |
| 415 | if (bottom_blob_bordered.empty()) |
| 416 | return -100; |
| 417 | |
| 418 | w = bottom_blob_bordered.w; |
| 419 | h = bottom_blob_bordered.h; |
| 420 | |
| 421 | int outw = (w - kernel_extent_w) / stride_w + 1; |
| 422 | int outh = (h - kernel_extent_h) / stride_h + 1; |
| 423 | int out_elempack = 1; |
| 424 | #if __ARM_NEON |
| 425 | if (opt.use_packing_layout) |
| 426 | { |
| 427 | out_elempack = num_output % 4 == 0 ? 4 : 1; |
| 428 | } |
| 429 | #endif |
| 430 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 431 | |
| 432 | top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); |
| 433 | if (top_blob.empty()) |
| 434 | return -100; |
| 435 | |
| 436 | if ((!support_packing || !opt.use_packing_layout) && kernel_w == kernel_h && dilation_w != 1 && dilation_h == dilation_w && stride_w == 1 && stride_h == 1) |
| 437 | { |
| 438 | if (outw >= dilation_w && outh >= dilation_h) |
| 439 | { |
| 440 | return forwardDilation_arm(bottom_blob_bordered, top_blob, opt); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | const int num_input = channels * elempack; |
| 445 | |
| 446 | bool prefer_winograd = (opt.use_winograd23_convolution || opt.use_winograd43_convolution || opt.use_winograd63_convolution) && (num_input >= 8 || num_output >= 8); |
| 447 | |
| 448 | if (opt.use_winograd_convolution && prefer_winograd && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 449 | { |
| 450 | bool prefer_winograd63 = false; |
| 451 | bool prefer_winograd23 = false; |
| 452 | bool prefer_winograd43 = !prefer_winograd63 && !prefer_winograd23; |
| 453 | |
| 454 | if (prefer_winograd23 && (!opt.use_winograd23_convolution || weight_winograd23_data.empty())) |
| 455 | { |
| 456 | // f23 fallback to f43 |
| 457 | prefer_winograd23 = false; |
| 458 | prefer_winograd43 = true; |
| 459 | } |
| 460 | |
| 461 | if (prefer_winograd63 && (!opt.use_winograd63_convolution || weight_winograd63_data.empty())) |
| 462 | { |
| 463 | // f63 fallback to f43 |
| 464 | prefer_winograd63 = false; |
| 465 | prefer_winograd43 = true; |
| 466 | } |
| 467 | |
| 468 | if (prefer_winograd43 && (!opt.use_winograd43_convolution || weight_winograd43_data.empty())) |
| 469 | { |
| 470 | // f43 fallback to f63 or f23 |
| 471 | prefer_winograd43 = false; |
| 472 | if (opt.use_winograd63_convolution && !weight_winograd63_data.empty()) |
| 473 | { |
| 474 | prefer_winograd63 = true; |
| 475 | } |
| 476 | else |
| 477 | { |
| 478 | prefer_winograd23 = true; |
| 479 | } |
| 480 | } |
| 481 | // NCNN_LOGE("prefer_winograd %d %d %d", prefer_winograd23, prefer_winograd43, prefer_winograd63); |
| 482 | |
| 483 | int _nT = nT ? nT : opt.num_threads; |
| 484 | if (nT != 0 && opt.num_threads != nT) |
| 485 | { |
| 486 | // force num_threads the same as in create_pipeline |
| 487 | // so we could use pre-packed A/B from the same tile config |
| 488 | NCNN_LOGE("opt.num_threads %d changed, convolution winograd will use load-time value %d", opt.num_threads, nT); |
| 489 | } |
| 490 | |
| 491 | int ret = 0; |
| 492 | if (prefer_winograd23) |
| 493 | { |
| 494 | ret = conv3x3s1_winograd23(bottom_blob_bordered, top_blob, weight_winograd23_data, bias_data, _nT, opt); |
| 495 | } |
| 496 | else if (prefer_winograd43) |
| 497 | { |
| 498 | ret = conv3x3s1_winograd43(bottom_blob_bordered, top_blob, weight_winograd43_data, bias_data, _nT, opt); |
| 499 | } |
| 500 | else if (prefer_winograd63) |
| 501 | { |
| 502 | ret = conv3x3s1_winograd63(bottom_blob_bordered, top_blob, weight_winograd63_data, bias_data, _nT, opt); |
| 503 | } |
| 504 | else |
| 505 | { |
| 506 | // should never reach here |
| 507 | } |
| 508 | if (ret != 0) |
| 509 | return ret; |
| 510 | |
| 511 | if (activation) |
| 512 | { |
| 513 | activation->forward_inplace(top_blob, opt); |
| 514 | } |
| 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | int l2_cache_size_fp32 = get_cpu_level2_cache_size() / sizeof(float); |
| 519 | bool prefer_sgemm = num_input * num_output * kernel_w * kernel_h * dilation_w * dilation_h * stride_w * stride_h * 2 > l2_cache_size_fp32 || (num_input > 16 || num_output > 16); |
| 520 | |
| 521 | #if NCNN_GNU_INLINE_ASM |
| 522 | if (elempack == 4 && out_elempack == 4) |
| 523 | { |
| 524 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2 && (num_input < 4 || num_output < 32)) |
| 525 | { |
| 526 | prefer_sgemm = false; |
| 527 | } |
| 528 | if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 529 | { |
| 530 | prefer_sgemm = false; |
| 531 | } |
| 532 | if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2 && (num_input < 8 || num_output < 44)) |
| 533 | { |
| 534 | prefer_sgemm = false; |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | if (elempack == 1 && out_elempack == 4) |
| 539 | { |
| 540 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 541 | { |
| 542 | prefer_sgemm = false; |
| 543 | } |
| 544 | else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 545 | { |
| 546 | prefer_sgemm = false; |
| 547 | } |
| 548 | else if (kernel_w == 7 && kernel_h == 7 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 549 | { |
| 550 | prefer_sgemm = false; |
| 551 | } |
| 552 | } |
| 553 | #endif // NCNN_GNU_INLINE_ASM |
| 554 | |
| 555 | if ((opt.use_sgemm_convolution && prefer_sgemm) || (kernel_w == 1 && kernel_h == 1)) |
| 556 | { |
| 557 | int _nT = nT ? nT : opt.num_threads; |
| 558 | if (nT != 0 && opt.num_threads != nT) |
| 559 | { |
| 560 | // force num_threads the same as in create_pipeline |
| 561 | // so we could use pre-packed A/B from the same tile config |
| 562 | NCNN_LOGE("opt.num_threads %d changed, convolution gemm will use load-time value %d", opt.num_threads, nT); |
| 563 | } |
| 564 | |
| 565 | int ret = convolution_im2col_gemm(bottom_blob_bordered, top_blob, weight_sgemm_data, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, _nT, opt); |
| 566 | if (ret != 0) |
| 567 | return ret; |
| 568 | |
| 569 | if (activation) |
| 570 | { |
| 571 | activation->forward_inplace(top_blob, opt); |
| 572 | } |
| 573 | return 0; |
| 574 | } |
| 575 | |
| 576 | #if NCNN_GNU_INLINE_ASM |
| 577 | #if __ARM_NEON |
| 578 | if (elempack == 4 && out_elempack == 4) |
| 579 | { |
| 580 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 581 | { |
| 582 | conv3x3s2_pack4_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 583 | |
| 584 | if (activation) |
| 585 | { |
| 586 | activation->forward_inplace(top_blob, opt); |
| 587 | } |
| 588 | } |
| 589 | else if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 590 | { |
| 591 | conv5x5s1_pack4_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 592 | |
| 593 | if (activation) |
| 594 | { |
| 595 | activation->forward_inplace(top_blob, opt); |
| 596 | } |
| 597 | } |
| 598 | else if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 599 | { |
| 600 | conv5x5s2_pack4_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 601 | |
| 602 | if (activation) |
| 603 | { |
| 604 | activation->forward_inplace(top_blob, opt); |
| 605 | } |
| 606 | } |
| 607 | else |
| 608 | { |
| 609 | convolution_packed(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | if (elempack == 1 && out_elempack == 4) |
| 614 | { |
| 615 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 616 | { |
| 617 | conv3x3s1_pack1to4_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 618 | |
| 619 | if (activation) |
| 620 | { |
| 621 | activation->forward_inplace(top_blob, opt); |
| 622 | } |
| 623 | } |
| 624 | else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 625 | { |
| 626 | conv3x3s2_pack1to4_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 627 | |
| 628 | if (activation) |
| 629 | { |
| 630 | activation->forward_inplace(top_blob, opt); |
| 631 | } |
| 632 | } |
| 633 | else if (kernel_w == 7 && kernel_h == 7 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 634 | { |
| 635 | conv7x7s2_pack1to4_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 636 | |
| 637 | if (activation) |
| 638 | { |
| 639 | activation->forward_inplace(top_blob, opt); |
| 640 | } |
| 641 | } |
| 642 | else |
| 643 | { |
| 644 | convolution_packed(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | if (elempack == 4 && out_elempack == 1) |
| 649 | { |
| 650 | { |
| 651 | convolution_packed(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 652 | } |
| 653 | } |
| 654 | #endif // __ARM_NEON |
| 655 | |
| 656 | if (elempack == 1 && out_elempack == 1) |
| 657 | { |
| 658 | if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 659 | { |
| 660 | conv1x1s1_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 661 | |
| 662 | if (activation) |
| 663 | { |
| 664 | activation->forward_inplace(top_blob, opt); |
| 665 | } |
| 666 | } |
| 667 | else if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 668 | { |
| 669 | conv1x1s2_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 670 | |
| 671 | if (activation) |
| 672 | { |
| 673 | activation->forward_inplace(top_blob, opt); |
| 674 | } |
| 675 | } |
| 676 | else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 677 | { |
| 678 | conv3x3s2_packed_neon(bottom_blob_bordered, top_blob, weight_3x3s2_data, bias_data, opt); |
| 679 | |
| 680 | if (activation) |
| 681 | { |
| 682 | activation->forward_inplace(top_blob, opt); |
| 683 | } |
| 684 | } |
| 685 | else if (kernel_w == 4 && kernel_h == 4 && dilation_w == 1 && dilation_h == 1 && stride_w == 4 && stride_h == 4) |
| 686 | { |
| 687 | conv4x4s4_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 688 | |
| 689 | if (activation) |
| 690 | { |
| 691 | activation->forward_inplace(top_blob, opt); |
| 692 | } |
| 693 | } |
| 694 | else if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 695 | { |
| 696 | conv5x5s1_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 697 | |
| 698 | if (activation) |
| 699 | { |
| 700 | activation->forward_inplace(top_blob, opt); |
| 701 | } |
| 702 | } |
| 703 | else if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 704 | { |
| 705 | conv5x5s2_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 706 | |
| 707 | if (activation) |
| 708 | { |
| 709 | activation->forward_inplace(top_blob, opt); |
| 710 | } |
| 711 | } |
| 712 | else if (kernel_w == 7 && kernel_h == 7 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 713 | { |
| 714 | conv7x7s1_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 715 | |
| 716 | if (activation) |
| 717 | { |
| 718 | activation->forward_inplace(top_blob, opt); |
| 719 | } |
| 720 | } |
| 721 | else if (kernel_w == 7 && kernel_h == 7 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 722 | { |
| 723 | conv7x7s2_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 724 | |
| 725 | if (activation) |
| 726 | { |
| 727 | activation->forward_inplace(top_blob, opt); |
| 728 | } |
| 729 | } |
| 730 | else |
| 731 | { |
| 732 | convolution_packed(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 733 | } |
| 734 | } |
| 735 | #else // NCNN_GNU_INLINE_ASM |
| 736 | { |
| 737 | convolution_packed(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 738 | } |
| 739 | #endif // NCNN_GNU_INLINE_ASM |
| 740 | |
| 741 | return 0; |
| 742 | } |
| 743 | |
| 744 | int Convolution_arm::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const |
| 745 | { |
| 746 | const Mat& bottom_blob = bottom_blobs[0]; |
| 747 | const Mat& _weight_data = bottom_blobs[1]; |
| 748 | Mat& top_blob = top_blobs[0]; |
| 749 | |
| 750 | const int _kernel_w = _weight_data.w; |
| 751 | const int _kernel_h = _weight_data.h; |
| 752 | const int _num_output = _weight_data.c * _weight_data.elempack; |
| 753 | |
| 754 | Mat weight_data_flattened; |
| 755 | flatten(_weight_data, weight_data_flattened, opt); |
| 756 | if (weight_data_flattened.empty()) |
| 757 | return -100; |
| 758 | |
| 759 | #if NCNN_ARM82 |
| 760 | if (opt.use_fp16_storage && cpu_support_arm_asimdhp() && weight_data_flattened.elembits() == 16) |
| 761 | { |
| 762 | Mat weight_data_flattened_fp32; |
| 763 | cast_float16_to_float32(weight_data_flattened, weight_data_flattened_fp32, opt); |
| 764 | weight_data_flattened = weight_data_flattened_fp32; |
| 765 | } |
| 766 | #endif // NCNN_ARM82 |
| 767 | #if NCNN_BF16 |
| 768 | if (opt.use_bf16_storage && weight_data_flattened.elembits() == 16) |
| 769 | { |
| 770 | Mat weight_data_flattened_fp32; |
| 771 | cast_bfloat16_to_float32(weight_data_flattened, weight_data_flattened_fp32, opt); |
| 772 | weight_data_flattened = weight_data_flattened_fp32; |
| 773 | } |
| 774 | #endif // NCNN_BF16 |
| 775 | |
| 776 | // weight_data_flattened as pack1 |
| 777 | weight_data_flattened.w *= weight_data_flattened.elempack; |
| 778 | weight_data_flattened.elemsize /= weight_data_flattened.elempack; |
| 779 | weight_data_flattened.elempack = 1; |
| 780 | |
| 781 | Mat bias_data_flattened; |
| 782 | if (bias_term) |
| 783 | { |
| 784 | const Mat& _bias_data = bottom_blobs[2]; |
| 785 | flatten(_bias_data, bias_data_flattened, opt); |
| 786 | if (bias_data_flattened.empty()) |
| 787 | return -100; |
| 788 | |
| 789 | #if NCNN_ARM82 |
| 790 | if (opt.use_fp16_storage && cpu_support_arm_asimdhp() && bias_data_flattened.elembits() == 16) |
| 791 | { |
| 792 | Mat bias_data_flattened_fp32; |
| 793 | cast_float16_to_float32(bias_data_flattened, bias_data_flattened_fp32, opt); |
| 794 | bias_data_flattened = bias_data_flattened_fp32; |
| 795 | } |
| 796 | #endif // NCNN_ARM82 |
| 797 | #if NCNN_BF16 |
| 798 | if (opt.use_bf16_storage && bias_data_flattened.elembits() == 16) |
| 799 | { |
| 800 | Mat bias_data_flattened_fp32; |
| 801 | cast_bfloat16_to_float32(bias_data_flattened, bias_data_flattened_fp32, opt); |
| 802 | bias_data_flattened = bias_data_flattened_fp32; |
| 803 | } |
| 804 | #endif // NCNN_BF16 |
| 805 | |
| 806 | // bias_data_flattened as pack1 |
| 807 | bias_data_flattened.w *= bias_data_flattened.elempack; |
| 808 | bias_data_flattened.elemsize /= bias_data_flattened.elempack; |
| 809 | bias_data_flattened.elempack = 1; |
| 810 | } |
| 811 | |
| 812 | ncnn::Layer* op = ncnn::create_layer_cpu(ncnn::LayerType::Convolution); |
| 813 | |
| 814 | ncnn::ParamDict pd; |
| 815 | pd.set(0, _num_output); |
| 816 | pd.set(1, _kernel_w); |
| 817 | pd.set(11, _kernel_h); |
| 818 | pd.set(2, dilation_w); |
| 819 | pd.set(12, dilation_h); |
| 820 | pd.set(3, stride_w); |
| 821 | pd.set(13, stride_h); |
| 822 | pd.set(4, pad_left); |
| 823 | pd.set(15, pad_right); |
| 824 | pd.set(14, pad_top); |
| 825 | pd.set(16, pad_bottom); |
| 826 | pd.set(18, pad_value); |
| 827 | pd.set(5, bias_term); |
| 828 | pd.set(6, weight_data_flattened.w); |
| 829 | pd.set(8, int8_scale_term); |
| 830 | pd.set(9, activation_type); |
| 831 | pd.set(10, activation_params); |
| 832 | |
| 833 | op->load_param(pd); |
| 834 | |
| 835 | ncnn::Mat weights[2]; |
| 836 | weights[0] = weight_data_flattened; |
| 837 | weights[1] = bias_data_flattened; |
| 838 | |
| 839 | op->load_model(ncnn::ModelBinFromMatArray(weights)); |
| 840 | |
| 841 | op->create_pipeline(opt); |
| 842 | |
| 843 | op->forward(bottom_blob, top_blob, opt); |
| 844 | |
| 845 | op->destroy_pipeline(opt); |
| 846 | |
| 847 | delete op; |
| 848 | |
| 849 | return 0; |
| 850 | } |
| 851 | |
| 852 | #if NCNN_BF16 |
| 853 | static void convolution_transform_kernel_packed_bf16s_neon(const Mat& weight_data, Mat& weight_data_tm, int num_input, int num_output, int kernel_w, int kernel_h, int elempack, int out_elempack) |
| 854 | { |
| 855 | const int maxk = kernel_w * kernel_h; |
| 856 | |
| 857 | // src = kw-kh-inch-outch |
| 858 | // dst = pb-pa-kw-kh-inch/pa-outch/pb |
| 859 | { |
| 860 | Mat weight_data_r2 = weight_data.reshape(maxk, num_input, num_output); |
| 861 | |
| 862 | weight_data_tm.create(maxk, num_input / elempack, num_output / out_elempack, (size_t)2u * elempack * out_elempack, elempack * out_elempack); |
| 863 | |
| 864 | for (int q = 0; q + (out_elempack - 1) < num_output; q += out_elempack) |
| 865 | { |
| 866 | unsigned short* g00 = weight_data_tm.channel(q / out_elempack); |
| 867 | |
| 868 | for (int p = 0; p + (elempack - 1) < num_input; p += elempack) |
| 869 | { |
| 870 | for (int k = 0; k < maxk; k++) |
| 871 | { |
| 872 | for (int i = 0; i < elempack; i++) |
| 873 | { |
| 874 | for (int j = 0; j < out_elempack; j++) |
| 875 | { |
| 876 | const float* k00 = weight_data_r2.channel(q + j).row(p + i); |
| 877 | |
| 878 | g00[0] = float32_to_bfloat16(k00[k]); |
| 879 | |
| 880 | g00++; |
| 881 | } |
| 882 | } |
| 883 | } |
| 884 | } |
| 885 | } |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | int Convolution_arm::create_pipeline_bf16s(const Option& opt) |
| 890 | { |
| 891 | const int maxk = kernel_w * kernel_h; |
| 892 | const int num_input = weight_data_size / maxk / num_output; |
| 893 | |
| 894 | int elempack = 1; |
| 895 | int out_elempack = 1; |
| 896 | #if __ARM_NEON |
| 897 | if (opt.use_packing_layout) |
| 898 | { |
| 899 | elempack = num_input % 4 == 0 ? 4 : 1; |
| 900 | out_elempack = num_output % 4 == 0 ? 4 : 1; |
| 901 | } |
| 902 | #endif |
| 903 | |
| 904 | bool prefer_winograd = (opt.use_winograd23_convolution || opt.use_winograd43_convolution || opt.use_winograd63_convolution) && (num_input >= 8 || num_output >= 8); |
| 905 | |
| 906 | if (opt.use_winograd_convolution && prefer_winograd && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 907 | { |
| 908 | // dynamic shape |
| 909 | if (opt.use_winograd63_convolution && (num_input <= 128 && num_output <= 128)) |
| 910 | conv3x3s1_winograd63_transform_kernel(weight_data, weight_winograd63_data, num_input, num_output, opt); |
| 911 | else if (opt.use_winograd43_convolution && (num_input >= 8 && num_output >= 8)) |
| 912 | conv3x3s1_winograd43_transform_kernel(weight_data, weight_winograd43_data, num_input, num_output, opt); |
| 913 | else |
| 914 | conv3x3s1_winograd23_transform_kernel(weight_data, weight_winograd23_data, num_input, num_output, opt); |
| 915 | |
| 916 | if (opt.lightmode) |
| 917 | weight_data.release(); |
| 918 | |
| 919 | return 0; |
| 920 | } |
| 921 | |
| 922 | int l2_cache_size_bf16 = get_cpu_level2_cache_size() / sizeof(unsigned short); |
| 923 | bool prefer_sgemm = num_input * num_output * kernel_w * kernel_h * dilation_w * dilation_h * stride_w * stride_h * 2 > l2_cache_size_bf16 || (num_input > 16 || num_output > 16); |
| 924 | |
| 925 | #if NCNN_GNU_INLINE_ASM |
| 926 | if (elempack == 4 && out_elempack == 4) |
| 927 | { |
| 928 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2 && (num_input < 4 || num_output < 32)) |
| 929 | { |
| 930 | prefer_sgemm = false; |
| 931 | } |
| 932 | if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 933 | { |
| 934 | prefer_sgemm = false; |
| 935 | } |
| 936 | if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2 && (num_input < 8 || num_output < 44)) |
| 937 | { |
| 938 | prefer_sgemm = false; |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | if (elempack == 1 && out_elempack == 4) |
| 943 | { |
| 944 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 945 | { |
| 946 | prefer_sgemm = false; |
| 947 | } |
| 948 | else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 949 | { |
| 950 | prefer_sgemm = false; |
| 951 | } |
| 952 | else if (kernel_w == 7 && kernel_h == 7 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 953 | { |
| 954 | prefer_sgemm = false; |
| 955 | } |
| 956 | } |
| 957 | #endif // NCNN_GNU_INLINE_ASM |
| 958 | |
| 959 | if ((opt.use_sgemm_convolution && prefer_sgemm) || (kernel_w == 1 && kernel_h == 1)) |
| 960 | { |
| 961 | convolution_im2col_gemm_transform_kernel_bf16s(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h, opt); |
| 962 | |
| 963 | if (opt.lightmode) |
| 964 | weight_data.release(); |
| 965 | |
| 966 | return 0; |
| 967 | } |
| 968 | |
| 969 | #if NCNN_GNU_INLINE_ASM |
| 970 | if ((elempack == 4 && out_elempack == 4 && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 971 | || (elempack == 4 && out_elempack == 4 && kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 972 | || (elempack == 4 && out_elempack == 4 && kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 973 | || (elempack == 1 && out_elempack == 4 && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 974 | || (elempack == 1 && out_elempack == 4 && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 975 | || (elempack == 1 && out_elempack == 4 && kernel_w == 7 && kernel_h == 7 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)) |
| 976 | { |
| 977 | convolution_transform_kernel_packed_bf16s_neon(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h, elempack, out_elempack); |
| 978 | } |
| 979 | else |
| 980 | #endif // NCNN_GNU_INLINE_ASM |
| 981 | { |
| 982 | convolution_transform_kernel_packed_bf16s(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h); |
| 983 | } |
| 984 | |
| 985 | if (opt.lightmode) |
| 986 | weight_data.release(); |
| 987 | |
| 988 | return 0; |
| 989 | } |
| 990 | |
| 991 | int Convolution_arm::forward_bf16s(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 992 | { |
| 993 | int w = bottom_blob.w; |
| 994 | int h = bottom_blob.h; |
| 995 | int channels = bottom_blob.c; |
| 996 | size_t elemsize = bottom_blob.elemsize; |
| 997 | int elempack = bottom_blob.elempack; |
| 998 | |
| 999 | // NCNN_LOGE("Convolution input %d x %d pad = %d %d ksize=%d %d stride=%d %d", w, h, pad_w, pad_h, kernel_w, kernel_h, stride_w, stride_h); |
| 1000 | |
| 1001 | const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1; |
| 1002 | const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1; |
| 1003 | |
| 1004 | Mat bottom_blob_bordered; |
| 1005 | make_padding(bottom_blob, bottom_blob_bordered, opt); |
| 1006 | if (bottom_blob_bordered.empty()) |
| 1007 | return -100; |
| 1008 | |
| 1009 | w = bottom_blob_bordered.w; |
| 1010 | h = bottom_blob_bordered.h; |
| 1011 | |
| 1012 | int outw = (w - kernel_extent_w) / stride_w + 1; |
| 1013 | int outh = (h - kernel_extent_h) / stride_h + 1; |
| 1014 | int out_elempack = 1; |
| 1015 | #if __ARM_NEON |
| 1016 | if (opt.use_packing_layout) |
| 1017 | { |
| 1018 | out_elempack = num_output % 4 == 0 ? 4 : 1; |
| 1019 | } |
| 1020 | #endif |
| 1021 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 1022 | |
| 1023 | top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); |
| 1024 | if (top_blob.empty()) |
| 1025 | return -100; |
| 1026 | |
| 1027 | // TODO dilated conv for bf16s |
| 1028 | // if ((!support_packing || !opt.use_packing_layout) && kernel_w == kernel_h && dilation_w != 1 && dilation_h == dilation_w && stride_w == 1 && stride_h == 1) |
| 1029 | // { |
| 1030 | // return forwardDilation_arm(bottom_blob_bordered, top_blob, opt); |
| 1031 | // } |
| 1032 | |
| 1033 | const int num_input = channels * elempack; |
| 1034 | |
| 1035 | bool prefer_winograd = (opt.use_winograd23_convolution || opt.use_winograd43_convolution || opt.use_winograd63_convolution) && (num_input >= 8 || num_output >= 8); |
| 1036 | |
| 1037 | if (opt.use_winograd_convolution && prefer_winograd && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 1038 | { |
| 1039 | bool prefer_winograd63 = false; |
| 1040 | bool prefer_winograd23 = false; |
| 1041 | bool prefer_winograd43 = !prefer_winograd63 && !prefer_winograd23; |
| 1042 | |
| 1043 | if (prefer_winograd23 && (!opt.use_winograd23_convolution || weight_winograd23_data.empty())) |
| 1044 | { |
| 1045 | // f23 fallback to f43 |
| 1046 | prefer_winograd23 = false; |
| 1047 | prefer_winograd43 = true; |
| 1048 | } |
| 1049 | |
| 1050 | if (prefer_winograd63 && (!opt.use_winograd63_convolution || weight_winograd63_data.empty())) |
| 1051 | { |
| 1052 | // f63 fallback to f43 |
| 1053 | prefer_winograd63 = false; |
| 1054 | prefer_winograd43 = true; |
| 1055 | } |
| 1056 | |
| 1057 | if (prefer_winograd43 && (!opt.use_winograd43_convolution || weight_winograd43_data.empty())) |
| 1058 | { |
| 1059 | // f43 fallback to f63 or f23 |
| 1060 | prefer_winograd43 = false; |
| 1061 | if (opt.use_winograd63_convolution && !weight_winograd63_data.empty()) |
| 1062 | { |
| 1063 | prefer_winograd63 = true; |
| 1064 | } |
| 1065 | else |
| 1066 | { |
| 1067 | prefer_winograd23 = true; |
| 1068 | } |
| 1069 | } |
| 1070 | // NCNN_LOGE("prefer_winograd %d %d %d", prefer_winograd23, prefer_winograd43, prefer_winograd63); |
| 1071 | |
| 1072 | int _nT = nT ? nT : opt.num_threads; |
| 1073 | if (nT != 0 && opt.num_threads != nT) |
| 1074 | { |
| 1075 | // force num_threads the same as in create_pipeline |
| 1076 | // so we could use pre-packed A/B from the same tile config |
| 1077 | NCNN_LOGE("opt.num_threads %d changed, convolution winograd will use load-time value %d", opt.num_threads, nT); |
| 1078 | } |
| 1079 | |
| 1080 | int ret = 0; |
| 1081 | if (prefer_winograd23) |
| 1082 | { |
| 1083 | ret = conv3x3s1_winograd23_bf16s(bottom_blob_bordered, top_blob, weight_winograd23_data, bias_data, _nT, opt); |
| 1084 | } |
| 1085 | else if (prefer_winograd43) |
| 1086 | { |
| 1087 | ret = conv3x3s1_winograd43_bf16s(bottom_blob_bordered, top_blob, weight_winograd43_data, bias_data, _nT, opt); |
| 1088 | } |
| 1089 | else if (prefer_winograd63) |
| 1090 | { |
| 1091 | ret = conv3x3s1_winograd63_bf16s(bottom_blob_bordered, top_blob, weight_winograd63_data, bias_data, _nT, opt); |
| 1092 | } |
| 1093 | else |
| 1094 | { |
| 1095 | // should never reach here |
| 1096 | } |
| 1097 | if (ret != 0) |
| 1098 | return ret; |
| 1099 | |
| 1100 | if (activation) |
| 1101 | { |
| 1102 | activation->forward_inplace(top_blob, opt); |
| 1103 | } |
| 1104 | return 0; |
| 1105 | } |
| 1106 | |
| 1107 | int l2_cache_size_bf16 = get_cpu_level2_cache_size() / sizeof(unsigned short); |
| 1108 | bool prefer_sgemm = num_input * num_output * kernel_w * kernel_h * dilation_w * dilation_h * stride_w * stride_h * 2 > l2_cache_size_bf16 || (num_input > 16 || num_output > 16); |
| 1109 | |
| 1110 | #if NCNN_GNU_INLINE_ASM |
| 1111 | if (elempack == 4 && out_elempack == 4) |
| 1112 | { |
| 1113 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2 && (num_input < 4 || num_output < 32)) |
| 1114 | { |
| 1115 | prefer_sgemm = false; |
| 1116 | } |
| 1117 | if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 1118 | { |
| 1119 | prefer_sgemm = false; |
| 1120 | } |
| 1121 | if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2 && (num_input < 8 || num_output < 44)) |
| 1122 | { |
| 1123 | prefer_sgemm = false; |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | if (elempack == 1 && out_elempack == 4) |
| 1128 | { |
| 1129 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 1130 | { |
| 1131 | prefer_sgemm = false; |
| 1132 | } |
| 1133 | else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 1134 | { |
| 1135 | prefer_sgemm = false; |
| 1136 | } |
| 1137 | else if (kernel_w == 7 && kernel_h == 7 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 1138 | { |
| 1139 | prefer_sgemm = false; |
| 1140 | } |
| 1141 | } |
| 1142 | #endif // NCNN_GNU_INLINE_ASM |
| 1143 | |
| 1144 | if ((opt.use_sgemm_convolution && prefer_sgemm) || (kernel_w == 1 && kernel_h == 1)) |
| 1145 | { |
| 1146 | int _nT = nT ? nT : opt.num_threads; |
| 1147 | if (nT != 0 && opt.num_threads != nT) |
| 1148 | { |
| 1149 | // force num_threads the same as in create_pipeline |
| 1150 | // so we could use pre-packed A/B from the same tile config |
| 1151 | NCNN_LOGE("opt.num_threads %d changed, convolution gemm will use load-time value %d", opt.num_threads, nT); |
| 1152 | } |
| 1153 | |
| 1154 | int ret = convolution_im2col_gemm_bf16s(bottom_blob_bordered, top_blob, weight_sgemm_data, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, _nT, opt); |
| 1155 | if (ret != 0) |
| 1156 | return ret; |
| 1157 | |
| 1158 | if (activation) |
| 1159 | { |
| 1160 | activation->forward_inplace(top_blob, opt); |
| 1161 | } |
| 1162 | return 0; |
| 1163 | } |
| 1164 | |
| 1165 | #if NCNN_GNU_INLINE_ASM |
| 1166 | #if __ARM_NEON |
| 1167 | if (elempack == 4 && out_elempack == 4) |
| 1168 | { |
| 1169 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 1170 | { |
| 1171 | conv3x3s2_pack4_bf16s_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 1172 | |
| 1173 | if (activation) |
| 1174 | { |
| 1175 | activation->forward_inplace(top_blob, opt); |
| 1176 | } |
| 1177 | } |
| 1178 | else if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 1179 | { |
| 1180 | conv5x5s1_pack4_bf16s_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 1181 | |
| 1182 | if (activation) |
| 1183 | { |
| 1184 | activation->forward_inplace(top_blob, opt); |
| 1185 | } |
| 1186 | } |
| 1187 | else if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 1188 | { |
| 1189 | conv5x5s2_pack4_bf16s_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 1190 | |
| 1191 | if (activation) |
| 1192 | { |
| 1193 | activation->forward_inplace(top_blob, opt); |
| 1194 | } |
| 1195 | } |
| 1196 | else |
| 1197 | { |
| 1198 | convolution_packed_bf16s(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | if (elempack == 1 && out_elempack == 4) |
| 1203 | { |
| 1204 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 1205 | { |
| 1206 | conv3x3s1_pack1to4_bf16s_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 1207 | |
| 1208 | if (activation) |
| 1209 | { |
| 1210 | activation->forward_inplace(top_blob, opt); |
| 1211 | } |
| 1212 | } |
| 1213 | else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 1214 | { |
| 1215 | conv3x3s2_pack1to4_bf16s_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 1216 | |
| 1217 | if (activation) |
| 1218 | { |
| 1219 | activation->forward_inplace(top_blob, opt); |
| 1220 | } |
| 1221 | } |
| 1222 | else if (kernel_w == 7 && kernel_h == 7 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 1223 | { |
| 1224 | conv7x7s2_pack1to4_bf16s_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 1225 | |
| 1226 | if (activation) |
| 1227 | { |
| 1228 | activation->forward_inplace(top_blob, opt); |
| 1229 | } |
| 1230 | } |
| 1231 | else |
| 1232 | { |
| 1233 | convolution_packed_bf16s(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 1234 | } |
| 1235 | } |
| 1236 | |
| 1237 | if (elempack == 4 && out_elempack == 1) |
| 1238 | { |
| 1239 | { |
| 1240 | convolution_packed_bf16s(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 1241 | } |
| 1242 | } |
| 1243 | #endif // __ARM_NEON |
| 1244 | |
| 1245 | if (elempack == 1 && out_elempack == 1) |
| 1246 | { |
| 1247 | { |
| 1248 | convolution_packed_bf16s(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 1249 | } |
| 1250 | } |
| 1251 | #else // NCNN_GNU_INLINE_ASM |
| 1252 | { |
| 1253 | convolution_packed_bf16s(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 1254 | } |
| 1255 | #endif // NCNN_GNU_INLINE_ASM |
| 1256 | |
| 1257 | return 0; |
| 1258 | } |
| 1259 | #endif // NCNN_BF16 |
| 1260 | |
| 1261 | #if NCNN_INT8 |
| 1262 | int Convolution_arm::create_pipeline_int8_arm(const Option& opt) |
| 1263 | { |
| 1264 | const int maxk = kernel_w * kernel_h; |
| 1265 | const int num_input = weight_data_size / maxk / num_output; |
| 1266 | |
| 1267 | bool prefer_winograd = (opt.use_winograd23_convolution || opt.use_winograd43_convolution) && (num_input >= 8 && num_output >= 8) && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1; |
| 1268 | #if NCNN_ARM82DOT |
| 1269 | if (ncnn::cpu_support_arm_asimddp()) |
| 1270 | { |
| 1271 | prefer_winograd = false; |
| 1272 | } |
| 1273 | #endif |
| 1274 | |
| 1275 | if (opt.use_winograd_convolution && prefer_winograd) |
| 1276 | { |
| 1277 | if (opt.use_winograd43_convolution) |
| 1278 | conv3x3s1_winograd43_transform_kernel_int8(weight_data, weight_winograd43_data, num_input, num_output, opt); |
| 1279 | else |
| 1280 | conv3x3s1_winograd23_transform_kernel_int8(weight_data, weight_winograd23_data, num_input, num_output, opt); |
| 1281 | } |
| 1282 | else if (opt.use_sgemm_convolution) |
| 1283 | { |
| 1284 | convolution_im2col_gemm_transform_kernel_int8(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h, opt); |
| 1285 | } |
| 1286 | else |
| 1287 | { |
| 1288 | convolution_transform_kernel_packed_int8(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h); |
| 1289 | } |
| 1290 | |
| 1291 | scale_in_data.create(num_output); |
| 1292 | for (int p = 0; p < num_output; p++) |
| 1293 | { |
| 1294 | // requantize and relu |
| 1295 | float scale_in; |
| 1296 | if (weight_data_int8_scales[p] == 0) |
| 1297 | scale_in = 0; |
| 1298 | else |
| 1299 | scale_in = 1.f / (bottom_blob_int8_scales[0] * weight_data_int8_scales[p]); |
| 1300 | |
| 1301 | scale_in_data[p] = scale_in; |
| 1302 | } |
| 1303 | |
| 1304 | if (opt.lightmode) |
| 1305 | weight_data.release(); |
| 1306 | |
| 1307 | return 0; |
| 1308 | } |
| 1309 | |
| 1310 | int Convolution_arm::forward_int8_arm(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 1311 | { |
| 1312 | int elembits = bottom_blob.elembits(); |
| 1313 | |
| 1314 | Mat bottom_blob_int8 = bottom_blob; |
| 1315 | if (elembits != 8) |
| 1316 | { |
| 1317 | Option opt_q = opt; |
| 1318 | opt_q.blob_allocator = opt.workspace_allocator; |
| 1319 | quantize_to_int8(bottom_blob, bottom_blob_int8, bottom_blob_int8_scales, opt_q); |
| 1320 | if (bottom_blob_int8.empty()) |
| 1321 | return -100; |
| 1322 | } |
| 1323 | |
| 1324 | // NCNN_LOGE("Convolution_arm input %d x %d ksize=%d %d stride=%d %d", w, h, kernel_w, kernel_h, stride_w, stride_h); |
| 1325 | |
| 1326 | Mat bottom_blob_bordered; |
| 1327 | make_padding(bottom_blob_int8, bottom_blob_bordered, opt); |
| 1328 | if (bottom_blob_bordered.empty()) |
| 1329 | return -100; |
| 1330 | |
| 1331 | int w = bottom_blob_bordered.w; |
| 1332 | int h = bottom_blob_bordered.h; |
| 1333 | int elempack = bottom_blob_bordered.elempack; |
| 1334 | |
| 1335 | const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1; |
| 1336 | const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1; |
| 1337 | |
| 1338 | int outw = (w - kernel_extent_w) / stride_w + 1; |
| 1339 | int outh = (h - kernel_extent_h) / stride_h + 1; |
| 1340 | |
| 1341 | bool use_int8_requantize = int8_scale_term > 100; |
| 1342 | int out_elempack = 1; |
| 1343 | #if __ARM_NEON |
| 1344 | if (opt.use_packing_layout) |
| 1345 | { |
| 1346 | if (use_int8_requantize) |
| 1347 | out_elempack = num_output % 8 == 0 ? 8 : 1; |
| 1348 | else |
| 1349 | out_elempack = num_output % 4 == 0 ? 4 : 1; |
| 1350 | } |
| 1351 | #endif // __ARM_NEON |
| 1352 | size_t out_elemsize = use_int8_requantize ? 1u * out_elempack : 4u * out_elempack; |
| 1353 | #if NCNN_ARM82 |
| 1354 | if (support_fp16_storage && opt.use_fp16_storage) |
| 1355 | { |
| 1356 | out_elemsize = use_int8_requantize ? 1u * out_elempack : 2u * out_elempack; |
| 1357 | } |
| 1358 | #endif |
| 1359 | if (opt.use_bf16_storage) |
| 1360 | out_elemsize = use_int8_requantize ? 1u * out_elempack : 2u * out_elempack; |
| 1361 | |
| 1362 | // NCNN_LOGE("forward_int8_arm %d %d %d %d %d", w, h, bottom_blob_bordered.c, elempack, out_elempack); |
| 1363 | |
| 1364 | int channels = bottom_blob_bordered.c; |
| 1365 | const int num_input = channels * elempack; |
| 1366 | |
| 1367 | bool prefer_winograd = (opt.use_winograd23_convolution || opt.use_winograd43_convolution) && (num_input >= 8 && num_output >= 8) && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1; |
| 1368 | #if NCNN_ARM82DOT |
| 1369 | if (ncnn::cpu_support_arm_asimddp()) |
| 1370 | { |
| 1371 | prefer_winograd = false; |
| 1372 | } |
| 1373 | #endif |
| 1374 | |
| 1375 | int out_elempack_int32 = 1; |
| 1376 | #if __ARM_NEON |
| 1377 | if (opt.use_packing_layout) |
| 1378 | { |
| 1379 | out_elempack_int32 = num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1; |
| 1380 | } |
| 1381 | #endif // __ARM_NEON |
| 1382 | |
| 1383 | Mat top_blob_int32; |
| 1384 | top_blob_int32.create(outw, outh, num_output / out_elempack_int32, (size_t)(4u * out_elempack_int32), out_elempack_int32, opt.workspace_allocator); |
| 1385 | if (top_blob_int32.empty()) |
| 1386 | return -100; |
| 1387 | |
| 1388 | int _nT = nT ? nT : opt.num_threads; |
| 1389 | if (nT != 0 && opt.num_threads != nT) |
| 1390 | { |
| 1391 | // force num_threads the same as in create_pipeline |
| 1392 | // so we could use pre-packed A/B from the same tile config |
| 1393 | NCNN_LOGE("opt.num_threads %d changed, convolution gemm will use load-time value %d", opt.num_threads, nT); |
| 1394 | } |
| 1395 | |
| 1396 | int ret = 0; |
| 1397 | if (opt.use_winograd_convolution && prefer_winograd) |
| 1398 | { |
| 1399 | if (opt.use_winograd43_convolution && !weight_winograd43_data.empty()) |
| 1400 | ret = conv3x3s1_winograd43_int8(bottom_blob_bordered, top_blob_int32, weight_winograd43_data, _nT, opt); |
| 1401 | else |
| 1402 | ret = conv3x3s1_winograd23_int8(bottom_blob_bordered, top_blob_int32, weight_winograd23_data, _nT, opt); |
| 1403 | } |
| 1404 | else if (opt.use_sgemm_convolution) |
| 1405 | { |
| 1406 | ret = convolution_im2col_gemm_int8(bottom_blob_bordered, top_blob_int32, weight_sgemm_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, _nT, opt); |
| 1407 | } |
| 1408 | else |
| 1409 | { |
| 1410 | convolution_packed_int8(bottom_blob_bordered, top_blob_int32, weight_data_tm, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, opt); |
| 1411 | } |
| 1412 | if (ret != 0) |
| 1413 | return ret; |
| 1414 | |
| 1415 | bottom_blob_bordered.release(); |
| 1416 | |
| 1417 | top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); |
| 1418 | if (top_blob.empty()) |
| 1419 | return -100; |
| 1420 | |
| 1421 | if (use_int8_requantize) |
| 1422 | { |
| 1423 | requantize_from_int32_to_int8(top_blob_int32, top_blob, scale_in_data, top_blob_int8_scales, bias_data, activation_type, activation_params, opt); |
| 1424 | } |
| 1425 | else |
| 1426 | { |
| 1427 | dequantize_from_int32(top_blob_int32, top_blob, scale_in_data, bias_data, opt); |
| 1428 | |
| 1429 | if (activation) |
| 1430 | { |
| 1431 | activation->forward_inplace(top_blob, opt); |
| 1432 | } |
| 1433 | } |
| 1434 | |
| 1435 | return 0; |
| 1436 | } |
| 1437 | #endif // NCNN_INT8 |
| 1438 | |
| 1439 | int Convolution_arm::forwardDilation_arm(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 1440 | { |
| 1441 | int w = bottom_blob.w; |
| 1442 | int h = bottom_blob.h; |
| 1443 | size_t elemsize = bottom_blob.elemsize; |
| 1444 | |
| 1445 | const int kernel_size = kernel_w; |
| 1446 | const int stride = stride_w; |
| 1447 | const int dilation = dilation_w; |
| 1448 | const int kernel_extent = dilation * (kernel_size - 1) + 1; |
| 1449 | |
| 1450 | int outw = (w - kernel_extent) / stride + 1; |
| 1451 | int outh = (h - kernel_extent) / stride + 1; |
| 1452 | |
| 1453 | top_blob.create(outw, outh, num_output, elemsize, opt.blob_allocator); |
| 1454 | if (top_blob.empty()) |
| 1455 | return -100; |
| 1456 | |
| 1457 | // Make (dilation * dilation) batches |
| 1458 | Mat inner_bottom_blob; |
| 1459 | Mat inner_top_blob; |
| 1460 | for (int x = 0; x < dilation; x++) |
| 1461 | { |
| 1462 | for (int y = 0; y < dilation; y++) |
| 1463 | { |
| 1464 | int inner_w = (w - y + dilation - 1) / dilation; |
| 1465 | int inner_h = (h - x + dilation - 1) / dilation; |
| 1466 | |
| 1467 | int inner_outw = (inner_w - kernel_size) / stride + 1; |
| 1468 | int inner_outh = (inner_h - kernel_size) / stride + 1; |
| 1469 | |
| 1470 | inner_bottom_blob.create(inner_w, inner_h, bottom_blob.c, elemsize, opt.workspace_allocator); |
| 1471 | if (inner_bottom_blob.empty()) |
| 1472 | return -100; |
| 1473 | |
| 1474 | inner_top_blob.create(inner_outw, inner_outh, num_output, elemsize, opt.workspace_allocator); |
| 1475 | if (inner_top_blob.empty()) |
| 1476 | return -100; |
| 1477 | |
| 1478 | #pragma omp parallel for num_threads(opt.num_threads) |
| 1479 | for (int c = 0; c < bottom_blob.c; c++) |
| 1480 | { |
| 1481 | float* outptr = inner_bottom_blob.channel(c); |
| 1482 | |
| 1483 | for (int i = 0; i < inner_h; i++) |
| 1484 | { |
| 1485 | const float* ptr = (const float*)bottom_blob.channel(c) + dilation * i * w + x * w + y; |
| 1486 | for (int j = 0; j < inner_w; j++) |
| 1487 | { |
| 1488 | outptr[j] = ptr[j * dilation]; |
| 1489 | } |
| 1490 | outptr += inner_w; |
| 1491 | } |
| 1492 | } |
| 1493 | |
| 1494 | Option opt_g = opt; |
| 1495 | opt_g.blob_allocator = inner_top_blob.allocator; |
| 1496 | convolution_dilation1->forward(inner_bottom_blob, inner_top_blob, opt_g); |
| 1497 | |
| 1498 | #pragma omp parallel for num_threads(opt.num_threads) |
| 1499 | for (int c = 0; c < num_output; c++) |
| 1500 | { |
| 1501 | float* outptr = (float*)top_blob.channel(c) + x * outw + y; |
| 1502 | for (int i = 0; i < inner_outh; i++) |
| 1503 | { |
| 1504 | const float* ptr = (const float*)inner_top_blob.channel(c) + i * inner_outw; |
| 1505 | for (int j = 0; j < inner_outw; j++) |
| 1506 | { |
| 1507 | outptr[j * dilation] = ptr[j]; |
| 1508 | } |
| 1509 | outptr += dilation * outw; |
| 1510 | } |
| 1511 | } |
| 1512 | } |
| 1513 | } |
| 1514 | |
| 1515 | if (activation) |
| 1516 | { |
| 1517 | activation->forward_inplace(top_blob, opt); |
| 1518 | } |
| 1519 | |
| 1520 | return 0; |
| 1521 | } |
| 1522 | |
| 1523 | } // namespace ncnn |