Commit efd6b55
authored
Fix XNNPACK channels-last reshape for per-channel binary ops under dynamic quant (#20376)
### Summary
`ChannelsLastTaggedReshapePass.input_to_nhwc`'s dynamic-quant branch
calls
`input_node.replace_all_uses_with(input_node_nhwc)` on a shared
activation. Because
the main traversal runs in topological order, this can switch a
broadcasting binary
op's activation operand to NHWC *after* that op was already processed,
while its
other operand (e.g. a per-channel constant) stays NCHW. The operands
then have
incompatible shapes (e.g. `[1, H, W, C]` vs `[1, C, 1, 1]`), and XNNPACK
fails at
runtime in `xnn_reshape_binary_elementwise_nd` with
`xnn_status_invalid_parameter`.
Lowering succeeds; only `execute()` fails. Found on a ResNet-50-backbone
detection
model lowered with w8a8 dynamic quantization.
### Fix
After the main traversal, re-converge any broadcasting binary op
(`add`/`mul`/`sub`/
`div`) whose operands ended up in different memory formats, reusing the
existing
`input_to_nhwc`/`input_to_nchw` helpers. It runs after the retrace so it
sees the
settled graph, and retraces again only if something changed; it is a
no-op for
graphs without diverged operands.
### Test
`test_dynamic_quant_per_channel_binary_chain_lowers_and_runs` builds a
graph where a
per-channel binary op is the first consumer of an input activation and
the
convolution chain runs through it. It fails to execute without this fix
and passes
with it.
```
pytest backends/xnnpack/test/passes/test_channels_last_tagged_reshape.py -k per_channel_binary_chain
```
cc @GregoryComer @digantdesai @cbilgin1 parent 42157d7 commit efd6b55
2 files changed
Lines changed: 137 additions & 0 deletions
File tree
- backends/xnnpack
- _passes
- test/passes
Lines changed: 50 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
77 | 89 | | |
78 | 90 | | |
79 | 91 | | |
| |||
567 | 579 | | |
568 | 580 | | |
569 | 581 | | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
570 | 620 | | |
Lines changed: 87 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
665 | 665 | | |
666 | 666 | | |
667 | 667 | | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 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 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
0 commit comments