Add per-sequence causal policy to packed THD attention - #3274
Conversation
Signed-off-by: Desh Raj <r.desh26@gmail.com>
| @pytest.mark.parametrize( | ||
| "sequence_is_causal", | ||
| ( | ||
| (False, True, False, True), |
There was a problem hiding this comment.
The PyTorch QA job explicitly enumerates attention test modules and does not include this new file, so its forward, gradient, validation, and empty-batch coverage never runs in CI and regressions in the new dispatch path go undetected.
Knowledge Base Used: Tests and QA
Greptile SummaryAdds per-sequence causal dispatch for packed THD self-attention.
Confidence Score: 4/5The implementation appears safe to merge, with the non-blocking exception that its new regression tests are not currently executed by repository CI. The mixed-policy dispatcher validates unsupported configurations and preserves the existing scalar attention path, while the only accepted concern is that the newly added test module is absent from the explicitly enumerated PyTorch QA suite. Files Needing Attention: tests/pytorch/attention/test_mixed_thd_attention.py and qa/L0_pytorch_unittest/test.sh Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Packed THD Q/K/V and per-sequence policy] --> B{All policies equal?}
B -->|Yes| C[Invoke scalar-policy attention once]
B -->|No| D[Group token indices by sequence policy]
D --> E[Run full-context scalar attention]
D --> F[Run causal scalar attention]
E --> G[Restore original packed token order]
F --> G
C --> H[Return attention output]
G --> H
Reviews (1): Last reviewed commit: "Add per-sequence causal policy to THD at..." | Re-trigger Greptile |
Description
Add an optional per-sequence causal policy to
DotProductAttentionfor packed THD self-attention. A scalarattn_mask_typecurrently applies one policy to the entire packed invocation, while mixed offline/causal training needs each packed sequence to retain its own policy.When
thd_sequence_is_causalis supplied, Transformer Engine groups whole sequences by policy, invokes the existing scalar-policy attention path once per non-empty group, and restores the original packed token order. Uniform all-causal and all-offline batches retain the existing one-call path.This is a logical dispatcher over existing attention calls, not a new heterogeneous-mask fused kernel. Initial support is intentionally limited to plain FP16/BF16 THD self-attention; options whose tensors or semantics cannot be safely regrouped are rejected explicitly.
Type of change
Changes
thd_sequence_is_causal, a boolean tensor with one value per packed sequence, toDotProductAttention.forward.Validation
python -m pytest --import-mode=importlib tests/pytorch/attention/test_mixed_thd_attention.py -q -vvattention
python -m py_compile, andgit diff --checkChecklist:
The focused GPU suite and repository Python lint pass as described above; the full repository unit-test suite was not run locally.