Commit b3345b3
committed
Compute fp16 batch norm in fp32 instead of downcasting the params
`nn.BatchNorm2d` keeps gamma/beta/running_mean/running_var in fp32 even
when the activation is fp16. ATen handles that mixed-precision case by
promoting the input to fp32, using the params and eps at full precision,
and narrowing only the final result (`native_batch_norm_helper` in
torch/_decomp/decompositions.py).
The composite body did the opposite: it cast the fp32 params and eps down
to the input element type and ran every op in fp16. That loses mantissa
bits on the params (eps 1e-5 becomes 1.00136e-05), and any channel whose
running_var or running_mean exceeds the fp16 max of 65504 becomes inf --
sqrt(inf) then divides the activation to a silent zero.
Use the existing `prepare_compute_type_for_norm` helper, as layer_norm
and group_norm already do, so the fp16 input is promoted, the arithmetic
runs in fp32, and there is a single narrowing cast before the output.
fp32 inputs are unaffected apart from dropping now-redundant f32->f32
casts, which the optimizer already folded.
Tests: a numerical case with large and tiny running_var (max abs error
4.945 and 0.5 against eager before the fix) and an IR case pinning the
promote/compute/narrow shape of the fp16 composite body.1 parent c89f6a4 commit b3345b3
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