Elffuss BraidHub

kikocis/ncnn public

fix potential fp16s bf16s conflicts on arm vfpv4 (#5578)

* fix potential fp16s bf16s conflicts on armv7 vfpv4

* but prefer fp16 on armv8.2
Changeset97d85537e7363fee53556fccc80217b65786d4cb3e5e6dbad9367eb82982a54c
Authornihui <nihuini@tencent.com> · 2024-07-12 10:25 +08:00
CommitterGitHub <noreply@github.com> · 2024-07-12 10:25 +08:00
Parentsc40b67352970

Browse the files at this changeset

2 files changed

modified src/net.cpp +33 −3
@@ -621,8 +621,17 @@
621621 // clang-format off
622622 // *INDENT-OFF*
623623
624+#if NCNN_ARM82
625+ if (opt.use_fp16_storage && cpu_support_arm_asimdhp() && layer->support_fp16_storage)
626+ {
627+ Mat bottom_blob_fp16;
628+ cast_float32_to_float16(bottom_blob, bottom_blob_fp16, opt);
629+ bottom_blob = bottom_blob_fp16;
630+ }
631+ else
632+#endif // NCNN_ARM82
624633 #if NCNN_VFPV4
625- if (opt.use_fp16_storage && cpu_support_arm_vfpv4() && layer->support_fp16_storage)
634+ if (opt.use_fp16_storage && !opt.use_bf16_storage && cpu_support_arm_vfpv4() && layer->support_fp16_storage)
626635 {
627636 Mat bottom_blob_fp16;
628637 cast_float32_to_float16(bottom_blob, bottom_blob_fp16, opt);
@@ -740,8 +749,17 @@
740749 // clang-format off
741750 // *INDENT-OFF*
742751
752+#if NCNN_ARM82
753+ if (opt.use_fp16_storage && cpu_support_arm_asimdhp() && !layer->support_fp16_storage)
754+ {
755+ Mat bottom_blob_fp32;
756+ cast_float16_to_float32(bottom_blob, bottom_blob_fp32, opt);
757+ bottom_blob = bottom_blob_fp32;
758+ }
759+ else
760+#endif // NCNN_ARM82
743761 #if NCNN_VFPV4
744- if (opt.use_fp16_storage && cpu_support_arm_vfpv4() && !layer->support_fp16_storage)
762+ if (opt.use_fp16_storage && !opt.use_bf16_storage && cpu_support_arm_vfpv4() && !layer->support_fp16_storage)
745763 {
746764 Mat bottom_blob_fp32;
747765 cast_float16_to_float32(bottom_blob, bottom_blob_fp32, opt);
@@ -2719,8 +2737,20 @@
27192737
27202738 // clang-format off
27212739 // *INDENT-OFF*
2740+#if NCNN_ARM82
2741+ if (d->opt.use_fp16_storage && cpu_support_arm_asimdhp() && (type == 0))
2742+ {
2743+ if (feat.elembits() == 16)
2744+ {
2745+ Mat feat_fp32;
2746+ cast_float16_to_float32(feat, feat_fp32, d->opt);
2747+ feat = feat_fp32;
2748+ }
2749+ }
2750+ else
2751+#endif // NCNN_ARM82
27222752 #if NCNN_VFPV4
2723- if (d->opt.use_fp16_storage && cpu_support_arm_vfpv4() && (type == 0))
2753+ if (d->opt.use_fp16_storage && !d->opt.use_bf16_storage && cpu_support_arm_vfpv4() && (type == 0))
27242754 {
27252755 if (feat.elembits() == 16)
27262756 {
modified tests/testutil.cpp +16 −2
@@ -328,8 +328,15 @@
328328 {
329329 // clang-format off
330330 // *INDENT-OFF*
331+#if NCNN_ARM82
332+ if (opt.use_fp16_storage && ncnn::cpu_support_arm_asimdhp() && op->support_fp16_storage && !(flag & TEST_LAYER_DISABLE_AUTO_INPUT_CASTING))
333+ {
334+ ncnn::cast_float32_to_float16(a, a4, opt);
335+ }
336+ else
337+#endif // NCNN_ARM82
331338 #if NCNN_VFPV4
332- if (opt.use_fp16_storage && ncnn::cpu_support_arm_vfpv4() && op->support_fp16_storage && !(flag & TEST_LAYER_DISABLE_AUTO_INPUT_CASTING))
339+ if (opt.use_fp16_storage && !opt.use_bf16_storage && ncnn::cpu_support_arm_vfpv4() && op->support_fp16_storage && !(flag & TEST_LAYER_DISABLE_AUTO_INPUT_CASTING))
333340 {
334341 ncnn::cast_float32_to_float16(a, a4, opt);
335342 }
@@ -449,8 +456,15 @@
449456
450457 // clang-format off
451458 // *INDENT-OFF*
459+#if NCNN_ARM82
460+ if (opt.use_fp16_storage && ncnn::cpu_support_arm_asimdhp() && op->support_fp16_storage && c4_unpacked.elembits() == 16)
461+ {
462+ ncnn::cast_float16_to_float32(c4_unpacked, c, opt);
463+ }
464+ else
465+#endif // NCNN_ARM82
452466 #if NCNN_VFPV4
453- if (opt.use_fp16_storage && ncnn::cpu_support_arm_vfpv4() && op->support_fp16_storage && c4_unpacked.elembits() == 16)
467+ if (opt.use_fp16_storage && !opt.use_bf16_storage && ncnn::cpu_support_arm_vfpv4() && op->support_fp16_storage && c4_unpacked.elembits() == 16)
454468 {
455469 ncnn::cast_float16_to_float32(c4_unpacked, c, opt);
456470 }