Repository navigation
[PyTorch] Detect THD inter-sequence padding by cu_seqlens values - #4033
Draft
ArchanaChetan07 wants to merge 1 commit into
Draft
ArchanaChetan07 wants to merge 1 commit into
ArchanaChetan07 wants to merge 1 commit into
Conversation
With qkv_format="thd" and pad_between_seqs=None, DotProductAttention inferred inter-sequence padding from tensor identity: padded cu_seqlens that were separate objects from the unpadded ones always selected the padded path, even when the values were equal. Under P2P context parallelism that path recomputes per-rank padded cu_seqlens at every step (NVIDIA#3487). Compare the values in eager mode instead, and cache the result by tensor identity and version so the sync happens once per cu_seqlens set rather than once per layer. During CUDA graph capture, reuse the result of an eager warm-up call with the same unmodified tensors, and otherwise keep the previous conservative answer (padding present). Absent or aliased padded cu_seqlens still imply no padding without reading values. Fixes NVIDIA#3487 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: ArchanaChetan07 <[email protected]>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
With
qkv_format="thd"andpad_between_seqs=None,DotProductAttentioninfers inter-sequence padding from tensor identity (added in 4cd705b). Ifcu_seqlens_*_paddedandcu_seqlens_*are separate tensors, it picks the padded path even when their values are equal. With P2P context parallelism, that path recomputes per-rank paddedcu_seqlenson every step, which #3487 measured as a ~4-6% iteration-time regression.This PR moves the auto-detect into a small helper,
_infer_thd_pad_between_seqs:Falsewithout reading any values, as before.torch.equal. The result is cached by tensor identity and_version, using the same pattern as_thd_policy_validation_cache. So the single device sync happens once per new set ofcu_seqlens, not once per DPA layer. Cache entries hold their tensors, so their ids can't be reused. In-place updates bump_versionand cause a cache miss.True(padding present), which is the current behavior on main. So capture never chooses a less conservative backend than it does today.It compares the full tensors, last entry included. A padded tail on the last sequence still counts as padding, because the CP partitioning of that sequence depends on its padded length.
Fixes #3487
Type of change
Changes
dot_product_attention.py: add_infer_thd_pad_between_seqsand a bounded (64-entry) LRU cache. The forward auto-detect now calls the helper.test_attention_backend_selection.py: add detection tests (no-padded / aliased / equal values / padded / tail-padded / kv-only / q-only). Add a capture test: no cached result → conservative; cached result reused without callingtorch.equal; an in-place update invalidates the cache entry. The tests use CPU tensors and need no GPU or distributed setup.Notes for reviewers
[:-1].OrderedDictcache instead oflru_cacheplus a side store.(id, _version), as suggested in review there.cu_seqlensset (in practice about once per microbatch). Callers that know there is no padding can still passpad_between_seqs=Falseand skip it.Testing
_infer_thd_pad_between_seqsfrom this branch, and for the baseline it runs main's verbatim inline detection block wrapped in a function with the same name:equal-values,q-only-equal-values,detection_cuda_graph), 5 passedtest_attention_backend_selection.py/test_attention.pywith a built TE, and the CP tests (test_attention_with_cp.py, multi-GPU). No performance measurement has been done on H100 with P2P CP.--line-length=100 --preview --enable-unstable-feature=string_processing).Checklist:
Prepared with AI assistance (Claude Code).
🤖 Generated with Claude Code