[PyTorch] Add no-load-balance THD all-gather CP - #3221
Merged
sudhakarsingh27 merged 16 commits intoAug 27, 2026
Merged
sudhakarsingh27 merged 16 commits into
sudhakarsingh27 merged 16 commits into
Conversation
sudhakarsingh27
force-pushed
the
sudhakars/relaxed-load-balancing-all-gather
branch
from
July 18, 2026 00:39
50b86ff to
9645f28
Compare
Allow causal THD attention to shard the complete packed token buffer with mirrored context-parallel chunks while retaining document boundaries through sequence metadata. This supports workloads whose individual documents are not divisible by twice the CP size. Keep the existing per-document partition as the default and reject backend or attention combinations that the prototype has not validated, so existing paths remain unchanged. Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com>
Reuse the existing THD CUDA partition and reorder kernels by representing the complete physical token buffer as one partitioning sequence. Preserve CPU and mixed-device dataloader behavior with a reference fallback. Rename the opt-in policy to packed_super_sequence to distinguish physical partitioning from THD packing, and add a narrowly gated matched-input benchmark path so the two policies can be compared without workload or timing asymmetry. Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com>
Allow relaxed THD all-gather to assign one contiguous chunk per CP rank while preserving the mirrored policy for compatibility and comparison. Rank-major ownership needs no KV reorder and reduces each rank to one attention step; keep it opt-in while the performance tradeoffs are evaluated. Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com>
Keep per-document partitioning as the default and packed-contiguous as the only packed opt-in so the experimental API has one global ownership contract. Delete the unused 2*CP global metadata and reorder paths, simplify packed metadata to one chunk and one attention step per rank, and retain a negative test for the retired selector. Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com>
Keep the experimental packed-contiguous selection internal to context parallelism so public TransformerLayer and attention APIs retain their existing signatures. Per-document partitioning remains the default unless NVTE_EXPERIMENTAL_CP_AG_THD_PACKED_CONTIGUOUS=1 is set. Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com>
Keep the initial upstream review focused on the production context-parallel implementation. The test development remains available in the preceding commits while the branch tip restores the existing test suite unchanged. Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com>
sudhakarsingh27
force-pushed
the
sudhakars/relaxed-load-balancing-all-gather
branch
from
August 18, 2026 21:44
9645f28 to
57f3cf8
Compare
sudhakarsingh27
marked this pull request as ready for review
August 18, 2026 23:02
Contributor
Greptile SummaryThe PR adds an explicit context-parallel load-balancing strategy and introduces contiguous THD partitioning for no-load-balance all-gather attention.
Confidence Score: 5/5The PR appears safe to merge. No blocking failure remains. Important Files Changed
Sequence DiagramsequenceDiagram
participant U as Caller
participant TL as TransformerLayer
participant MHA as MultiheadAttention
participant DPA as DotProductAttention
participant CP as CP All-Gather Attention
U->>TL: set_context_parallel_group(strategy)
TL->>MHA: propagate CP configuration
MHA->>DPA: propagate strategy
DPA->>CP: forward(Q, K, V, strategy)
CP->>CP: partition and build THD metadata
CP->>CP: save strategy on autograd context
CP-->>DPA: attention output
Note over CP: Backward uses captured strategy
CP->>CP: restore K/V and select step layout
CP->>CP: unrestore dK/dV for reduce-scatter
Reviews (10): Last reviewed commit: "Limit deterministic THD guard to F16 bac..." | Re-trigger Greptile |
Name the single contiguous-chunk policy after its deliberate lack of causal load balancing so the performance tradeoff is explicit. Remove the CPU reference partitioner and require the existing CUDA path for per-document metadata. Capture the selected layout through backward and add focused helper plus CP2 forward/backward coverage so mutable environment state cannot make the two passes use different token orders. Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com>
sudhakarsingh27
commented
Aug 20, 2026
Validate the experimental policy where context-parallel communication is selected so unsupported combinations fail before entering custom autograd. Passing the captured mode into the internal all-gather call also prevents a second environment lookup from selecting a different layout. Restore the default CPU dataloader slicing behavior, keep one stream dependency per format path, and exercise padded feature execution so the lean experimental path does not regress existing callers. Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com>
cyanguwa
reviewed
Aug 20, 2026
Refresh the PR on current upstream before addressing review feedback. Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com>
Make token partitioning explicit so input slicing and attention cannot diverge through mutable process state. Reuse native THD indices for CUDA while preserving CPU dataloader behavior, and cover the supported FusedAttention and unpadded FlashAttention 3 paths. Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com>
Use the shorter public name because the strategy governs both input partitioning and attention execution. Consolidate redundant low-level partition tests into the existing end-to-end slicing coverage. Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com>
Document the experimental contract at the public selection points. Preserve the legacy child-setter invocation for the default strategy so the additive API does not disrupt existing extension modules. Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com>
Member
Author
|
/te-ci pytorch L1 |
cyanguwa
previously approved these changes
Aug 21, 2026
The focused no-load-balance test uses cp_2_0, which reaches the same known cuDNN deterministic THD backward workspace limit as the generic CP matrix. Apply the equivalent sm90 skip so the standalone coverage does not bypass that guard. Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com>
Member
Author
|
/te-ci pytorch |
Apply the known sm90 cuDNN backward workspace restriction in the common selector so every caller avoids choosing FusedAttention for unsupported large THD training configurations. Keep inference, smaller shapes, nondeterministic execution, and other architectures unaffected. Remove test-local skips and make the focused CP case use normal backend availability preflight so coverage follows the production selection path. Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com>
Refresh the PR with current upstream attention fixes. Resolve the overlapping context-parallel changes by preserving upstream THD padding and stream-safety behavior while retaining strategy-specific single-chunk all-gather handling. Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com>
cyanguwa
reviewed
Aug 27, 2026
cyanguwa
previously approved these changes
Aug 27, 2026
cyanguwa
left a comment
Collaborator
There was a problem hiding this comment.
Two minor comments - please run the local CI at least and I'm happy to approve again. Thanks for the PR!
The empirical large-workspace limitation applies to deterministic F16/BF16 THD backward. Keep the FP8 fused-attention backend available for the same geometry. Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com>
cyanguwa
approved these changes
Aug 27, 2026
fheinecke
pushed a commit
that referenced
this pull request
Aug 31, 2026
* Add packed THD partitioning for all-gather CP Allow causal THD attention to shard the complete packed token buffer with mirrored context-parallel chunks while retaining document boundaries through sequence metadata. This supports workloads whose individual documents are not divisible by twice the CP size. Keep the existing per-document partition as the default and reject backend or attention combinations that the prototype has not validated, so existing paths remain unchanged. Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com> * Refine THD super-sequence partitioning Reuse the existing THD CUDA partition and reorder kernels by representing the complete physical token buffer as one partitioning sequence. Preserve CPU and mixed-device dataloader behavior with a reference fallback. Rename the opt-in policy to packed_super_sequence to distinguish physical partitioning from THD packing, and add a narrowly gated matched-input benchmark path so the two policies can be compared without workload or timing asymmetry. Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com> * Add contiguous THD all-gather partition Allow relaxed THD all-gather to assign one contiguous chunk per CP rank while preserving the mirrored policy for compatibility and comparison. Rank-major ownership needs no KV reorder and reduces each rank to one attention step; keep it opt-in while the performance tradeoffs are evaluated. Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com> * Remove mirrored packed THD partition Keep per-document partitioning as the default and packed-contiguous as the only packed opt-in so the experimental API has one global ownership contract. Delete the unused 2*CP global metadata and reorder paths, simplify packed metadata to one chunk and one attention step per rank, and retain a negative test for the retired selector. Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com> * Gate packed THD partition with env flag Keep the experimental packed-contiguous selection internal to context parallelism so public TransformerLayer and attention APIs retain their existing signatures. Per-document partitioning remains the default unless NVTE_EXPERIMENTAL_CP_AG_THD_PACKED_CONTIGUOUS=1 is set. Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com> * Remove experimental packed THD tests from PR Keep the initial upstream review focused on the production context-parallel implementation. The test development remains available in the preceding commits while the branch tip restores the existing test suite unchanged. Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com> * Rename experimental THD policy and add coverage Name the single contiguous-chunk policy after its deliberate lack of causal load balancing so the performance tradeoff is explicit. Remove the CPU reference partitioner and require the existing CUDA path for per-document metadata. Capture the selected layout through backward and add focused helper plus CP2 forward/backward coverage so mutable environment state cannot make the two passes use different token orders. Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com> * Move THD no-load-balance checks to CP dispatch Validate the experimental policy where context-parallel communication is selected so unsupported combinations fail before entering custom autograd. Passing the captured mode into the internal all-gather call also prevents a second environment lookup from selecting a different layout. Restore the default CPU dataloader slicing behavior, keep one stream dependency per format path, and exercise padded feature execution so the lean experimental path does not regress existing callers. Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com> * Expose CP attention load-balancing strategy Make token partitioning explicit so input slicing and attention cannot diverge through mutable process state. Reuse native THD indices for CUDA while preserving CPU dataloader behavior, and cover the supported FusedAttention and unpadded FlashAttention 3 paths. Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com> * Rename CP load-balancing strategy Use the shorter public name because the strategy governs both input partitioning and attention execution. Consolidate redundant low-level partition tests into the existing end-to-end slicing coverage. Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com> * Mark no-load-balance CP strategy experimental Document the experimental contract at the public selection points. Preserve the legacy child-setter invocation for the default strategy so the additive API does not disrupt existing extension modules. Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com> * Skip known sm90 deterministic THD OOM The focused no-load-balance test uses cp_2_0, which reaches the same known cuDNN deterministic THD backward workspace limit as the generic CP matrix. Apply the equivalent sm90 skip so the standalone coverage does not bypass that guard. Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com> * Move deterministic THD guard to backend selection Apply the known sm90 cuDNN backward workspace restriction in the common selector so every caller avoids choosing FusedAttention for unsupported large THD training configurations. Keep inference, smaller shapes, nondeterministic execution, and other architectures unaffected. Remove test-local skips and make the focused CP case use normal backend availability preflight so coverage follows the production selection path. Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com> * Limit deterministic THD guard to F16 backend The empirical large-workspace limitation applies to deterministic F16/BF16 THD backward. Keep the FP8 fused-attention backend available for the same geometry. Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com> --------- Signed-off-by: Sudhakar Singh <sudhakars@nvidia.com> (cherry picked from commit 902468d)
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.
How to use
Select the token partition strategy explicitly and use the same value for both
context-parallel attention configuration and input partitioning:
CPAttentionLoadBalancingStrategy.DUAL_CHUNK_SWAPremains the default.What changed
CPAttentionLoadBalancingStrategyenum withDUAL_CHUNK_SWAPand experimentalNO_LOAD_BALANCEstrategies.MultiheadAttention, DotProductAttention, and the context-parallel attention
backends.
NO_LOAD_BALANCE, while preserving logical document boundaries through THDmetadata.
same token layout.
slicing while retaining the CPU dataloader fallback.
FlashAttention 3 coverage.
Why
The default per-document DualChunkSwap partition divides every sequence into
2 * cp_sizechunks and performs two attention steps per rank. Partitioningthe complete physical buffer into
cp_sizecontiguous chunks allows oneattention step per rank and supports packed documents whose individual lengths
are not divisible by
2 * cp_size.This strategy intentionally trades causal load balance for fewer attention
calls.
Scope and constraints
The experimental strategy currently requires THD self-attention,
cp_comm_type="all_gather", causal attention without a sliding window(
window_size=(-1, 0)), equal local Q/K/V physical lengths, and eitherFusedAttention or FlashAttention 3. FlashAttention 3 requires
pad_between_seqs=False. FP8 and CUDA graph capture are not supported.Input partitioning and attention configuration must use the same strategy.
Validation
DUAL_CHUNK_SWAPandNO_LOAD_BALANCE.DUAL_CHUNK_SWAPbehavior passed focused smoke checks.git diff --checkpassed.