Commit 46d15b7
authored
Compute fp16 batch norm in fp32 instead of downcasting the params (#54)
BatchNorm keeps its params in fp32 even when the activation is fp16. ATen promotes the input to fp32, uses the params and eps at full precision, and narrows only the result (native_batch_norm_helper). The composite body did the opposite: it cast the params down to fp16, losing mantissa bits and turning any running_var above the fp16 max of 65504 into inf, which silently zeroes the output.
Fix: use prepare_compute_type_for_norm, as layer_norm and group_norm already do. fp32 inputs are unchanged apart from dropping redundant f32 -> f32 casts.
Tests: numerical case with large/tiny running_var (fails on main with max abs error 4.945 and 0.5), plus an IR case pinning the promote/compute/narrow body.1 parent c89f6a4 commit 46d15b7
3 files changed
Lines changed: 107 additions & 27 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
706 | 706 | | |
707 | 707 | | |
708 | 708 | | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
709 | 715 | | |
710 | 716 | | |
711 | | - | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
712 | 723 | | |
713 | 724 | | |
714 | 725 | | |
715 | 726 | | |
716 | 727 | | |
717 | 728 | | |
718 | | - | |
| 729 | + | |
719 | 730 | | |
720 | 731 | | |
721 | 732 | | |
| |||
724 | 735 | | |
725 | 736 | | |
726 | 737 | | |
727 | | - | |
| 738 | + | |
728 | 739 | | |
729 | 740 | | |
730 | | - | |
| 741 | + | |
731 | 742 | | |
732 | 743 | | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
733 | 747 | | |
734 | 748 | | |
735 | 749 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
692 | 692 | | |
693 | 693 | | |
694 | 694 | | |
695 | | - | |
696 | | - | |
697 | | - | |
698 | | - | |
699 | | - | |
700 | | - | |
701 | | - | |
702 | | - | |
703 | | - | |
704 | | - | |
705 | | - | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
706 | 698 | | |
707 | 699 | | |
708 | 700 | | |
| |||
711 | 703 | | |
712 | 704 | | |
713 | 705 | | |
714 | | - | |
715 | | - | |
716 | | - | |
717 | | - | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
718 | 747 | | |
719 | 748 | | |
720 | 749 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
577 | 577 | | |
578 | 578 | | |
579 | 579 | | |
580 | | - | |
581 | | - | |
582 | | - | |
583 | | - | |
584 | | - | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
585 | 584 | | |
586 | | - | |
587 | | - | |
| 585 | + | |
| 586 | + | |
588 | 587 | | |
589 | | - | |
| 588 | + | |
| 589 | + | |
590 | 590 | | |
591 | 591 | | |
592 | 592 | | |
| |||
616 | 616 | | |
617 | 617 | | |
618 | 618 | | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
619 | 656 | | |
620 | 657 | | |
621 | 658 | | |
| |||
0 commit comments