Repository navigation
Commit 2a435a2
[PyT] Add per-sequence attention policies to packed THD attention (#3274)
* Add per-sequence causal policy to THD attention
Signed-off-by: Desh Raj <r.desh26@gmail.com>
* Fix mixed THD test CI coverage
Signed-off-by: Desh Raj <r.desh26@gmail.com>
* Use inter-sequence padding for mixed THD masks
Replace the per-policy Q/K/V gather and output scatter dispatcher with the review-suggested inter-sequence-padding design. Keep the original packed Q/K/V storage, derive logical and physical cu-seqlens for each mask policy, run the ordinary scalar-mask DPA path with pad_between_seqs, and sum its disjoint zero-padded outputs.
Expose a generic attn_mask_type_per_seq mapping plus window_size_per_mask_type so callers can combine full-context offline attention with bounded causal attention. Preserve the scalar fast path for uniform batches, avoid policy-dependent host synchronization, and cover forward/backward parity, existing physical padding, cross-attention, per-policy windows, bottom-right masks, invalid inputs, empty batches, and CUDA graph replay.
NRT H100 validation passed all 16 TransformerEngine cases and 3 focused Megatron integration tests. In an 8-node/64-H100 matched-auto comparison over iterations 7-100, grouped averaged 1802.289 ms and pad-between averaged 1779.596 ms (-1.259%); the paired 95% interval crossed zero, indicating practical parity when the backend is held constant.
In the deployable-backend comparison, grouped/Flash averaged 1596.129 ms while pad-between/auto-cuDNN averaged 1766.769 ms: pad-between was 10.691% slower by mean and 11.482% slower by 5%-trimmed mean. The paired penalty was +170.640 ms with a 95% interval of +89.728 to +251.552 ms, with all 16 fully logged workload counters matching across 100 steps.
The implementation removes the original data-movement contention, but the current image cannot run full-context inter-sequence padding through FlashAttention 2 and has no FA3, so the path falls back to cuDNN fused attention. Retain grouped/Flash for production until a competitive padding-capable backend is available; this commit keeps the cleaner generic API for review and future backend support.
Signed-off-by: Desh Raj <r.desh26@gmail.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Mask inactive mixed-THD lanes for FA3
Mixed-mask dispatch represents sequences owned by other policies as inter-sequence padding. FA3 preserves that layout contract, but its padding lanes are unspecified: focused Hopper testing observed NaNs in inactive forward outputs and tile-spill garbage in inactive dQ/dK/dV lanes.
Build a sync-free mask from the physical cu_seqlens, sanitize each policy output with torch.where, and wrap Q/K/V in an identity-forward autograd guard that zeros only inactive gradient lanes. This keeps the pad_between_seqs design, avoids regrouping or copying tokens, and lets disjoint policy results be summed safely across FA2 and FA3.
Validation: NRT H100 source-overlay run passed all 16 focused mixed-THD tests for FP16/BF16 forward and backward, pre-existing padding, cross attention, per-policy windows, CUDA graphs, and validation errors.
Signed-off-by: Desh Raj <r.desh26@gmail.com>
* Select mixed THD implementation by GPU backend
Keep attn_mask_type_per_seq and window_size_per_mask_type invariant at the caller boundary while selecting the physical implementation inside DotProductAttention.
Use inter-sequence padding only on SM90 when FlashAttention 3 is installed and enabled. Fall back to per-policy token compaction, scalar THD attention, and output restoration for FlashAttention 2 and other configurations. Preserve the uniform-policy scalar fast path and restrict CUDA graph coverage to the sync-free FA3 padding path.
Add a runtime dispatch matrix covering architecture, FA3 availability, and backend environment controls.
Validation:
- NRT H100/FA3: 21 mixed-THD tests and 4 Megatron policy/API tests passed
- IAD A100/FA2: 20 mixed-THD tests passed, 1 Hopper-only graph test skipped, and 4 Megatron policy/API tests passed
- Installed-image five-step training smokes were bit-identical within FA3 across NRT/HEL and within FA2 across IAD/ORD; maximum cross-backend loss delta was 1.56e-5
* Centralize mixed THD architecture dispatch
* Fix mixed THD window normalization order
* Refine mixed THD policy API and backend dispatch
* Add grouped override for mixed THD attention
Signed-off-by: Desh Raj <r.desh26@gmail.com>
* Validate mixed THD sequence policies
* Cache mixed THD backend selection
* Expose THD attention policies through transformer layers
* Support per-policy THD diagonal alignment
* Honor grouped THD dispatch through transformer layers
* Fix FlashAttention cross-attention backend selection
Signed-off-by: Desh Raj <r.desh26@gmail.com>
---------
Signed-off-by: Desh Raj <r.desh26@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Kshitij Lakhani <33047503+KshitijLakhani@users.noreply.github.com>1 parent 20f4cec commit 2a435a2
7 files changed
Lines changed: 1746 additions & 108 deletions
File tree
- qa/L0_pytorch_unittest
- tests/pytorch/attention
- transformer_engine/pytorch
- attention
- dot_product_attention
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
| 72 | + | |
72 | 73 | | |
73 | 74 | | |
74 | 75 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
240 | 240 | | |
241 | 241 | | |
242 | 242 | | |
243 | | - | |
| 243 | + | |
244 | 244 | | |
245 | 245 | | |
246 | 246 | | |
| |||
252 | 252 | | |
253 | 253 | | |
254 | 254 | | |
255 | | - | |
| 255 | + | |
256 | 256 | | |
257 | 257 | | |
258 | 258 | | |
| |||
265 | 265 | | |
266 | 266 | | |
267 | 267 | | |
268 | | - | |
| 268 | + | |
269 | 269 | | |
270 | 270 | | |
271 | 271 | | |
| |||
1149 | 1149 | | |
1150 | 1150 | | |
1151 | 1151 | | |
1152 | | - | |
| 1152 | + | |
| 1153 | + | |
| 1154 | + | |
| 1155 | + | |
| 1156 | + | |
| 1157 | + | |
| 1158 | + | |
| 1159 | + | |
| 1160 | + | |
| 1161 | + | |
| 1162 | + | |
| 1163 | + | |
| 1164 | + | |
| 1165 | + | |
| 1166 | + | |
| 1167 | + | |
| 1168 | + | |
| 1169 | + | |
| 1170 | + | |
| 1171 | + | |
| 1172 | + | |
| 1173 | + | |
| 1174 | + | |
| 1175 | + | |
| 1176 | + | |
| 1177 | + | |
| 1178 | + | |
| 1179 | + | |
| 1180 | + | |
| 1181 | + | |
| 1182 | + | |
| 1183 | + | |
| 1184 | + | |
| 1185 | + | |
| 1186 | + | |
| 1187 | + | |
| 1188 | + | |
| 1189 | + | |
| 1190 | + | |
| 1191 | + | |
| 1192 | + | |
1153 | 1193 | | |
1154 | 1194 | | |
1155 | 1195 | | |
| |||
1158 | 1198 | | |
1159 | 1199 | | |
1160 | 1200 | | |
| 1201 | + | |
1161 | 1202 | | |
1162 | 1203 | | |
1163 | 1204 | | |
| |||
1203 | 1244 | | |
1204 | 1245 | | |
1205 | 1246 | | |
1206 | | - | |
1207 | | - | |
1208 | | - | |
1209 | | - | |
| 1247 | + | |
| 1248 | + | |
1210 | 1249 | | |
1211 | 1250 | | |
1212 | 1251 | | |
| |||
1432 | 1471 | | |
1433 | 1472 | | |
1434 | 1473 | | |
1435 | | - | |
1436 | | - | |
1437 | | - | |
1438 | | - | |
1439 | | - | |
1440 | | - | |
1441 | | - | |
1442 | | - | |
1443 | | - | |
| 1474 | + | |
| 1475 | + | |
| 1476 | + | |
| 1477 | + | |
1444 | 1478 | | |
1445 | | - | |
1446 | | - | |
1447 | | - | |
1448 | | - | |
1449 | | - | |
1450 | | - | |
1451 | | - | |
1452 | | - | |
1453 | | - | |
1454 | | - | |
| 1479 | + | |
| 1480 | + | |
1455 | 1481 | | |
1456 | 1482 | | |
1457 | 1483 | | |
| |||
1474 | 1500 | | |
1475 | 1501 | | |
1476 | 1502 | | |
1477 | | - | |
1478 | | - | |
1479 | | - | |
1480 | | - | |
1481 | | - | |
| 1503 | + | |
1482 | 1504 | | |
1483 | 1505 | | |
1484 | 1506 | | |
| |||
1502 | 1524 | | |
1503 | 1525 | | |
1504 | 1526 | | |
| 1527 | + | |
| 1528 | + | |
| 1529 | + | |
1505 | 1530 | | |
1506 | 1531 | | |
1507 | 1532 | | |
| |||
0 commit comments