Skip to content

[PyTorch] Detect THD inter-sequence padding by cu_seqlens values - #4033

Draft
ArchanaChetan07 wants to merge 1 commit into
NVIDIA:mainfrom
ArchanaChetan07:fix/thd-pad-between-seqs-value-detect
Draft

ArchanaChetan07 wants to merge 1 commit into
NVIDIA:mainfrom
ArchanaChetan07:fix/thd-pad-between-seqs-value-detect

Conversation

@ArchanaChetan07

Copy link
Copy Markdown
Contributor

Description

With qkv_format="thd" and pad_between_seqs=None, DotProductAttention infers inter-sequence padding from tensor identity (added in 4cd705b). If cu_seqlens_*_padded and cu_seqlens_* are separate tensors, it picks the padded path even when their values are equal. With P2P context parallelism, that path recomputes per-rank padded cu_seqlens on 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:

  • No values read: if the padded tensors are absent or are the same objects as the unpadded ones, it returns False without reading any values, as before.
  • Eager mode: otherwise, it compares values with 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 of cu_seqlens, not once per DPA layer. Cache entries hold their tensors, so their ids can't be reused. In-place updates bump _version and cause a cache miss.
  • CUDA graph capture: values can't be read here. The helper reuses the result of an eager warm-up call on the same unmodified tensors. If there is no such result, it returns 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

  • Documentation change (change only to the documentation, either a fix or a new content)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Infra/Build change
  • Code refactoring

Changes

  • dot_product_attention.py: add _infer_thd_pad_between_seqs and 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 calling torch.equal; an in-place update invalidates the cache entry. The tests use CPU tensors and need no GPU or distributed setup.

Notes for reviewers

  • Earlier attempt: Fix THD P2P pad detection and tail-zero gating #3506 by @RPalmr tried this before. Its author closed it on 2026-09-29 without saying why. This PR uses the same core idea (value comparison in eager mode, reused during capture) and was written independently against current main. Differences from Fix THD P2P pad detection and tail-zero gating #3506:
    • It compares full tensors, not [:-1].
    • It uses one OrderedDict cache instead of lru_cache plus a side store.
    • It keys on (id, _version), as suggested in review there.
    • It leaves the dQ/dK/dV tail-zeroing alone.
  • Out of scope: the issue's second item, skipping the THD gradient tail-zeroing when there is no tail, is not in this PR. The detector is the main contributor in the issue's measurements, and the tail-zeroing change touches capture-sensitive backward code. It can follow separately if wanted.
  • Remaining cost: the eager path adds one sync per new cu_seqlens set (in practice about once per microbatch). Callers that know there is no padding can still pass pad_between_seqs=False and skip it.

Testing

  • New tests, run locally (CPU, torch 2.14) through a stub harness, because no TE build was available on the dev machine. The harness runs the real _infer_thd_pad_between_seqs from this branch, and for the baseline it runs main's verbatim inline detection block wrapped in a function with the same name:
    • main logic: 3 failed (equal-values, q-only-equal-values, detection_cuda_graph), 5 passed
    • this branch: 8 passed
    • On real main the new tests error out, because the helper doesn't exist there.
  • Not yet run: the full test_attention_backend_selection.py / test_attention.py with 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.
  • Formatted with black 24.4.2 (--line-length=100 --preview --enable-unstable-feature=string_processing).

Checklist:

  • I have read and followed the contributing guidelines
  • The functionality is complete
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Prepared with AI assistance (Claude Code).

🤖 Generated with Claude Code

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[PyTorch][Attention] THD P2P context-parallel regression when padded cu_seqlens are value-equal but not object-identical

1 participant