Commit 7c23098
SwinUNETR: optional flash attention (scaled_dot_product_attention) in WindowAttention (#8977)
Fixes #8973 .
### Description
SwinUNETR's WindowAttention builds the full (nWindows*heads, N, N) score
matrix by hand before softmax. This adds an opt-in use_flash_attention
flag (default False, so existing behaviour is unchanged) that routes
attention through torch.nn.functional.scaled_dot_product_attention,
folding the relative position bias and, for shifted windows, the
attention mask into one additive attn_mask cast to the query dtype. This
mirrors the flash-attention option already in MONAI's SelfAttention,
CrossAttention and CABlock.
Measured on the SwinUNETR encoder (SwinViT) forward, inference, single
GPU, best-of-5. Float32 output matches the default path to within 3e-6
and is bit-exact in float64, verified across 2D and 3D, batch sizes 1 to
4, and non-cubic inputs.
| ROI | dtype | default | flash | speedup |
|---|---|---|---|---|
| 96^3 | fp32 | 12.59 ms | 8.34 ms | 1.51x |
| 96^3 | bf16 | 10.71 ms | 5.46 ms | 1.96x |
| 128^3 | fp32 | 34.27 ms | 21.74 ms | 1.58x |
| 128^3 | bf16 | 29.45 ms | 14.12 ms | 2.09x |
| 160^3 | fp32 | 59.05 ms | 37.66 ms | 1.57x |
| 160^3 | bf16 | 51.25 ms | 25.33 ms | 2.02x |
The flag is threaded through SwinTransformer, BasicLayer and
SwinTransformerBlock to WindowAttention, exactly as use_v2 and
use_checkpoint are. No parameters or buffers change, so pretrained
weights load unchanged. The flash path is used only when autograd is
disabled and the module is not scripted, so training and TorchScript
keep the original path byte-for-byte; this is a deliberate choice to
leave training numerics untouched.
### Types of changes
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).
- [x] New tests added to cover the changes.
- [x] In-line docstrings updated.
---------
Signed-off-by: Soumya Snigdha Kundu <soumya_snigdha.kundu@kcl.ac.uk>
Signed-off-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>
Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>1 parent 605611b commit 7c23098
2 files changed
Lines changed: 47 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
98 | 98 | | |
99 | 99 | | |
100 | 100 | | |
| 101 | + | |
101 | 102 | | |
102 | 103 | | |
103 | 104 | | |
| |||
149 | 150 | | |
150 | 151 | | |
151 | 152 | | |
| 153 | + | |
152 | 154 | | |
153 | 155 | | |
154 | 156 | | |
| |||
224 | 226 | | |
225 | 227 | | |
226 | 228 | | |
| 229 | + | |
227 | 230 | | |
228 | 231 | | |
229 | 232 | | |
| |||
513 | 516 | | |
514 | 517 | | |
515 | 518 | | |
| 519 | + | |
516 | 520 | | |
517 | 521 | | |
518 | 522 | | |
| |||
522 | 526 | | |
523 | 527 | | |
524 | 528 | | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
525 | 533 | | |
526 | 534 | | |
527 | 535 | | |
528 | 536 | | |
529 | 537 | | |
530 | 538 | | |
| 539 | + | |
531 | 540 | | |
532 | 541 | | |
533 | 542 | | |
| |||
584 | 593 | | |
585 | 594 | | |
586 | 595 | | |
587 | | - | |
588 | | - | |
589 | 596 | | |
590 | 597 | | |
591 | 598 | | |
592 | 599 | | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
593 | 616 | | |
594 | 617 | | |
595 | 618 | | |
| |||
628 | 651 | | |
629 | 652 | | |
630 | 653 | | |
| 654 | + | |
631 | 655 | | |
632 | 656 | | |
633 | 657 | | |
| |||
643 | 667 | | |
644 | 668 | | |
645 | 669 | | |
| 670 | + | |
646 | 671 | | |
647 | 672 | | |
648 | 673 | | |
| |||
660 | 685 | | |
661 | 686 | | |
662 | 687 | | |
| 688 | + | |
663 | 689 | | |
664 | 690 | | |
665 | 691 | | |
| |||
924 | 950 | | |
925 | 951 | | |
926 | 952 | | |
| 953 | + | |
927 | 954 | | |
928 | 955 | | |
929 | 956 | | |
| |||
946 | 973 | | |
947 | 974 | | |
948 | 975 | | |
| 976 | + | |
949 | 977 | | |
950 | 978 | | |
951 | 979 | | |
| |||
996 | 1024 | | |
997 | 1025 | | |
998 | 1026 | | |
| 1027 | + | |
999 | 1028 | | |
1000 | 1029 | | |
1001 | 1030 | | |
| |||
1079 | 1108 | | |
1080 | 1109 | | |
1081 | 1110 | | |
| 1111 | + | |
1082 | 1112 | | |
1083 | 1113 | | |
1084 | 1114 | | |
| |||
1112 | 1142 | | |
1113 | 1143 | | |
1114 | 1144 | | |
| 1145 | + | |
1115 | 1146 | | |
1116 | 1147 | | |
1117 | 1148 | | |
| |||
1193 | 1224 | | |
1194 | 1225 | | |
1195 | 1226 | | |
| 1227 | + | |
1196 | 1228 | | |
1197 | 1229 | | |
1198 | 1230 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
111 | 111 | | |
112 | 112 | | |
113 | 113 | | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
114 | 127 | | |
115 | 128 | | |
116 | 129 | | |
| |||
0 commit comments