Skip to content

Add packed THD context parallelism for DSv4 - #7469

Merged
Phlip79 merged 14 commits into
NVIDIA:mainfrom
FDecaYed:codex/dsv4-cp-main-20260917
Oct 9, 2026
Merged

Phlip79 merged 14 commits into
NVIDIA:mainfrom
FDecaYed:codex/dsv4-cp-main-20260917

Conversation

@FDecaYed

@FDecaYed FDecaYed commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor
  • I, the PR author, have personally reviewed every line of this PR.

What does this PR do?

DSv4 attention currently rejects CP > 1. This adds static context parallelism for packed THD inputs with attention_cp_layout="contiguous" and TP=1, covering window attention, ratio-4 CSA, ratio-128 HCA, and MTP. Packed inputs require dsa_kernel_backend="cudnn"; the existing SBHD native backend remains available.

  • Exchange boundary hidden states, preserve document-relative RoPE, and assign compressed groups to their owning CP ranks. Handle packed padding, empty documents, and groups crossing rank boundaries.
  • Connect packed FlashMLA/cuDNN attention and sparse/dense indexer loss, with asynchronous compressed-KV/indexer-K gathers, gradient reduce-scatter overlap, and KV reconstruction for selective recompute.
  • Add contiguous MTP token/mask rolls, batch each phase's field exchanges, and return HSM boundary gradients to their original ranks.
  • Add CP2/CP4 output and gradient comparisons against per-document native SBHD references, boundary/roll coverage, and kernel determinism registrations.

The implementation uses main's existing packing, process-group, RoPE, and static-spec interfaces. It preserves one dense indexer scoring call per CP-local query shard. Compact BF16/MXFP8 indexers and dynamic CP remain separate follow-ups; dependency versions are unchanged. This PR targets main independently of #6403.

Related work

Related to #5795 and #6404. Static CP behavior follows the existing dev CSA implementation, adapted to main's interfaces and pinned cuDNN Frontend.

Validation

  • Passed 96 before/after CPU custom-autograd comparisons for sparse/dense loss, padding, CP1/2/4, and KV reconstruction, plus four pure-attention comparisons. External CUDA kernels and collectives were explicit substitutes; these checks verify adapter and gradient-routing equivalence.
  • Passed CPU checks of the per-document reference outputs/gradients, padding, and global-token normalization for dense/sparse indexer loss.
  • Passed AST, Black/isort, Pylint, Ruff, git diff --check, and kernel determinism coverage checks. Local mypy still reports dependency/type-configuration errors.
  • Real GPU unit tests, full training, peak memory, and throughput have not been run locally. CPU checks do not qualify the external GPU kernels. Existing SM90 dense-indexer-loss and dense empty-compressed-segment restrictions remain.

GPU validation targets:

uv run python -m torch.distributed.run --nproc-per-node 4 -m pytest -q \
  tests/unit_tests/transformer/experimental_attention_variant/test_dsv4_packed_cp.py \
  tests/unit_tests/transformer/test_contiguous_sequence_roll.py \
  tests/unit_tests/determinism/kernels/test_csa_packed_kernels.py \
  tests/unit_tests/determinism/kernels/test_fused_triton_kernels.py

Pre-checks

  • Added relevant unit tests.
  • Added relevant functional tests.
  • Added/updated kernel determinism tests and registered them in the manifest.
  • Completed typing validation.
  • Ran tools/autoformat.sh; blocking formatter/lint checks passed.

Signed-off-by: Deyu Fu <deyuf@nvidia.com>
…260917

Signed-off-by: Deyu Fu <deyuf@nvidia.com>
…260917

Signed-off-by: Deyu Fu <deyuf@nvidia.com>
Signed-off-by: Deyu Fu <deyuf@nvidia.com>
Signed-off-by: Deyu Fu <deyuf@nvidia.com>
@FDecaYed FDecaYed added the deepseekv4 DeepSeek V4 PRs label Sep 18, 2026
@copy-pr-bot

copy-pr-bot Bot commented Sep 18, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@FDecaYed

Copy link
Copy Markdown
Contributor Author

/ok to test e17057c

Signed-off-by: Deyu Fu <deyuf@nvidia.com>
@FDecaYed

Copy link
Copy Markdown
Contributor Author

/ok to test ef78927

Signed-off-by: Deyu Fu <deyuf@nvidia.com>
@FDecaYed

Copy link
Copy Markdown
Contributor Author

/ok to test 8e11d6b

@FDecaYed
FDecaYed marked this pull request as ready for review September 21, 2026 06:48
@FDecaYed
FDecaYed requested review from a team as code owners September 21, 2026 06:48
@svcnvidia-nemo-ci svcnvidia-nemo-ci added the Final Review PR is in the "final review" stage label Oct 1, 2026
@Connor-XY

Copy link
Copy Markdown
Contributor

Hi @FDecaYed, a few notes from stacking #7705/#7708 on this:

  1. Under torch.compile, bucketize(rows, cu[1:]) ignores the slice offset in our PyTorch build, so rows land one document to the right. Fix compiled packed sequence boundary lookup #7705 has the fix plus regressions. Could you adopt it here, or merge Fix compiled packed sequence boundary lookup #7705 right after this?
  2. Rebasing onto main would bring in cuDNN frontend 1.29.0, which Make DSv4 attention and indexer backward deterministic #7708's deterministic backward needs.
  3. The packed path ignores deterministic_mode until Make DSv4 attention and indexer backward deterministic #7708 lands. Should it raise in the meantime?

falls back to standard rolling behavior.
packed_seq_params (PackedSeqParams): Parameters for packed sequence processing.
If provided, respects sequence boundaries.
return_sum (bool): Whether to calculate and return the rolled tensor sum.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add docstring

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documented cp_layout, fill_value and the contiguous-only shift/fill contract in 0f2ced4.

@Phlip79

Phlip79 commented Oct 6, 2026

Copy link
Copy Markdown
Member

/review model=claude mode=strict

@svcnvidia-nemo-ci svcnvidia-nemo-ci left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

This is a strict review of the DSv4 packed-THD static context-parallel PR. I read all 17 changed files in the head tree. I also followed the paths that cross components:

  • the hybrid attention, and the CSA compressor and indexer;
  • the packed layout and the async all-gather/reduce-scatter (AG/RS) helpers;
  • the MTP rolls, including the zigzag, CP1 and HSM paths;
  • the config validation.

Findings: 0 CRITICAL · 0 IMPORTANT · 7 SUGGESTION (all inline)

  • Four new args or branches that never take effect. The "every new arg has a real use path" check fails for each:
    • fill_value=True in the zigzag MTP padding_mask roll does nothing.
    • compressed_base and compressed_is_sequence_major in build_attention_indices are never passed.
    • The packed branches of _apply_unfused_rope cannot be reached.
    • The second operand of the guard in compute_cp_indexer_topk is always false.
  • Missing docs: the roll_tensor docstring does not cover cp_layout or fill_value. This matches Phlip79's open request.
  • Lost diagnostic: fused_csa_teacher_lse now raises a generic ValueError. The base raised a RuntimeError that included the specific unsupported reason.
  • Untested path: every packed CP test fixes apply_rope_fusion=True, so none runs the new unfused-RoPE code.

Risk: moderate. This is a large new CP surface. Neither the author nor I could run its GPU kernels or collectives. Reading the source, I found no correctness, process-group, CPU-GPU sync, CUDA-graph or backward-compatibility defect. Since nothing above is significant, the verdict is approve. Please check the suggestions before merging.

What I checked by reading the source (not by running it):

  • Compressed-group ownership: each compressed group belongs to the rank that holds its last token. The 8-row halo always contains the ratio-4 overlap predecessor, so is_first = group_id == 0 is correct.
  • Row mapping: seq_to_rank_row maps correctly into the capacity-padded, rank-major gathered buffer. Halo copies never enter the global KV domain.
  • Deferred reduce-scatter edges: they return each compressed-KV and indexer-K gradient exactly once. The gathered buffers are detached on the loss path.
  • Loss scaling: real_row_scale matches loss_divisor for the sparse loss, the dense loss and per-token loss.
  • Roll backward: _RollLeft and _LeftBoundaryExchange send boundary gradients back to the rank that owns the token.
  • Process groups: every P2P call and collective passes the explicit CP group.
  • Host syncs: none were added. The code uses torch._assert_async, and its boolean-mask scalar fills dispatch to masked_fill.
  • Unchanged behavior: zigzag and CP1 MTP behave as before. For CP1, real and physical boundaries are the same tensor.

Not assessed: the PR discussion reports that bucketize(rows, cu[1:]) ignores the slice offset under torch.compile on some PyTorch builds. I cannot confirm this from source. _compiled_compactor and _compiled_attention_indices both use this pattern, so the 4-GPU test_dsv4_packed_cp.py run is what will show whether it holds here.

Review generated by claude

Comment thread megatron/core/transformer/multi_token_prediction.py
Comment thread megatron/core/transformer/multi_token_prediction.py Outdated
Comment thread megatron/core/transformer/experimental_attention_variant/csa_utils/cp_utils.py Outdated
Comment thread megatron/core/transformer/experimental_attention_variant/csa.py Outdated
Adopt the full-prefix bucketize fix and metadata regressions from NVIDIA#7705. Simplify unused paths, document roll semantics, retain teacher diagnostics, and cover unfused RoPE. Explicitly reject packed deterministic_mode pending NVIDIA#7921.

Co-authored-by: Yan Xu <yxu1@nvidia.com>
Signed-off-by: Deyu Fu <deyuf@nvidia.com>
@FDecaYed

FDecaYed commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

@Connor-XY Adopted #7705's boundary fix and metadata regressions in 0f2ced4, with co-author credit. Its scoped changes are now included here. Packed deterministic_mode now raises explicitly pending #7921, with #7708 as the shared/SBHD prerequisite. I kept the existing dependency pins in this PR; the deterministic follow-up can use main's newer runtime. New CI is running.

@svcnvidia-nemo-ci svcnvidia-nemo-ci added Approved All necessary approvals have been made and removed Final Review PR is in the "final review" stage labels Oct 8, 2026
@Phlip79
Phlip79 added this pull request to the merge queue Oct 8, 2026
@nemo-automation-bot

Copy link
Copy Markdown

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/37944069001

Merged via the queue into NVIDIA:main with commit 6605602 Oct 9, 2026
116 of 118 checks passed

This branch was successfully deployed

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

Labels

26.10 Approved All necessary approvals have been made complexity: high deepseekv4 DeepSeek V4 PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants