Skip to content

feat: support sequence packing and context parallelism for DTensor value models - #3320

Merged
yuki-97 merged 15 commits into
mainfrom
feat/dtensor-value-seqpack-cp
Nov 5, 2026
Merged

yuki-97 merged 15 commits into
mainfrom
feat/dtensor-value-seqpack-cp

Conversation

@fujial-code

Copy link
Copy Markdown
Contributor

What does this PR do?

Adds sequence packing and context parallelism support to the DTensor/FSDP2 PPO value-model path.

This follows the DTensor PPO value-model support introduced in #2837 and closes the two missing features tracked in #2951.

The main correctness work is specific to scalar token-level value predictions:

  • Sequence packing must unpack packed scalar scores back to the original batch and sequence layout.
  • Context parallelism must restore the global sequence order before applying the PPO value right shift and computing the value loss.
  • PPO rollout batches must be padded according to the value model's CP topology because the policy and value models may use different parallel configurations.

Sequence packing and context parallelism are supported as independent modes. Their existing mutual-exclusion validation remains in place.

Issues

Closes #2951

Summary of Changes

DTensor Value Sequence Packing

  • Enables sequence packing for token-level regression reward/value models.
  • Keeps sequence packing unsupported for Bradley-Terry and other sequence-level reward models.
  • Passes FlashAttention arguments through the DTensor value training and inference paths when sequence packing is enabled.
  • Extends ScorePostProcessor to reconstruct padded per-sample value tensors from packed outputs using:
    • cu_seqlens
    • original input lengths
    • original batch size and sequence length
  • Requires float16 or bfloat16 precision because FlashAttention does not support float32 inputs.
  • Removes the PPO setup guard that previously rejected DTensor value sequence packing.

DTensor Value Context Parallelism

  • Adds a value-specific loss postprocessor for context-parallel scalar logits.
  • Gathers CP-sharded value logits into the original global token order before:
    • applying the temporal right shift
    • computing PPO value loss and clipping
    • calculating value statistics
  • Updates value inference to gather CP shards before applying the same right-shift convention introduced in feat: Support for dtensor ppo #2837.
  • Corrects backward scaling for the replicated full-sequence value loss.
  • Avoids double-counting metrics replicated across TP/CP ranks.
  • Pads sequence-aligned PPO tensors to a multiple of 2 * context_parallel_size, as required by load-balanced causal CP attention.
  • Restores the original sequence length before returning value predictions.
  • Forces the ring FlashAttention backend for CP to support Qwen2 GQA layouts where:
    • num_attention_heads != num_key_value_heads
  • Requires float16 or bfloat16 precision for CP.
  • Removes the PPO setup guard that previously rejected DTensor value CP.

Configuration and Compatibility

Tests

Added or extended coverage for:

  • Sequence packing configuration validation.
  • Regression value models accepting FlashAttention arguments.
  • Packed scalar score reconstruction.
  • CP gather-before-right-shift behavior.
  • CP sequence-length validation and automatic padding.
  • DTensor value initialization and inference with:
    • TP=2 sequence packing
    • CP=2
  • DTensor value training with sequence packing and CP.
  • Pinned-weight inference equivalence against the feature-OFF reference.
  • Pinned-weight deterministic train-forward/loss equivalence, including:
    • value loss
    • value mean
    • return mean
    • residual squared mean

Validation

Validated with the Qwen2.5-1.5B-Instruct GSM8K PPO setup on one 8-GPU node.

Both comparisons resume from the same value-model checkpoint so differences cannot be attributed to random value-head initialization.

Sequence Packing OFF vs ON

Ran a 100-step PPO comparison with matched settings:

  • Value topology: TP=2
  • Same initial checkpoint
  • Same dataset and training configuration
  • Sequence packing disabled vs enabled

Results:
image
image

  • Both runs completed 100 PPO steps successfully.
  • train/reward follows the same learning trajectory.
  • train/critic/loss closely overlaps across training.
  • Pinned inference and train-forward equivalence tests pass.

Context Parallelism OFF vs ON

Ran a 100-step PPO comparison with matched settings:

  • Same initial checkpoint
  • Same dataset and training configuration
  • CP disabled vs CP=2

Results at step 100:
image
image

  • train/reward
    • CP OFF: 0.8955
    • CP ON: 0.9023
  • train/critic/loss
    • CP OFF: 0.03147
    • CP ON: 0.03260

Both runs complete successfully, and the reward and critic-loss behavior remain aligned.

train/loss is the policy loss and is not expected to remain pointwise identical after the stochastic rollout trajectories diverge. The correctness checks for this PR therefore use pinned deterministic value inference/train-loss equivalence together with reward and critic-loss behavior.

Local Checks

  • Ruff formatting passes.
  • Ruff lint checks pass.
  • Focused CPU unit tests for the new setup and post-processing logic pass.
  • Focused GPU worker tests pass for sequence packing and CP inference, training, and equivalence.
  • 100-step pinned PPO comparisons complete successfully for both features.

@fujial-code
fujial-code requested a review from yuki-97 July 23, 2026 08:28
@fujial-code
fujial-code requested review from a team as code owners July 23, 2026 08:28
@copy-pr-bot

copy-pr-bot Bot commented Jul 23, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@fujial-code fujial-code changed the title feat(ppo): support sequence packing and context parallelism for DTensor value models feat: support sequence packing and context parallelism for DTensor value models Jul 23, 2026
@fujial-code fujial-code added CI Relating to CI CI:L1 Run doctests, unit tests, and functional tests labels Jul 23, 2026
@fujial-code

Copy link
Copy Markdown
Contributor Author

/ok to test f778007

@fujial-code
fujial-code force-pushed the feat/dtensor-value-seqpack-cp branch from f778007 to 3789104 Compare July 29, 2026 05:52
@github-actions github-actions Bot removed the CI Relating to CI label Jul 29, 2026
@fujial-code

Copy link
Copy Markdown
Contributor Author

/ok to test 3789104

@fujial-code
fujial-code force-pushed the feat/dtensor-value-seqpack-cp branch from 3789104 to 4142810 Compare July 30, 2026 03:24
@fujial-code

Copy link
Copy Markdown
Contributor Author

/ok to test 4142810

@fujial-code

Copy link
Copy Markdown
Contributor Author

/ok to test b4c8662

@fujial-code

Copy link
Copy Markdown
Contributor Author

/ok to test 94c0444

@yuki-97 yuki-97 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.

thanks @fujial-code for supporting this, nice work!

left some comments. besides, could you also help to add two nightly tests for sequence packing and CP?

Comment thread nemo_rl/algorithms/ppo.py Resolved
Comment thread nemo_rl/models/automodel/setup.py Resolved
Comment thread nemo_rl/models/automodel/train.py Resolved
Comment thread nemo_rl/models/automodel/train.py Resolved
Comment thread nemo_rl/models/automodel/train.py Resolved
Comment thread nemo_rl/models/automodel/train.py Resolved
Comment thread nemo_rl/models/value/lm_value.py Resolved
@yuki-97

yuki-97 commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

@bg51717 could you help to take a review as well?

@yuki-97
yuki-97 requested a review from bg51717 August 4, 2026 14:23

@bg51717 bg51717 left a comment

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.

LGTM. I've left some comments.

Comment thread nemo_rl/models/automodel/setup.py Resolved
Comment thread nemo_rl/models/automodel/setup.py Resolved
@fujial-code
fujial-code requested a review from a team as a code owner August 12, 2026 18:08
@fujial-code

fujial-code commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor Author

@yuki-97 Added the two requested nightly tests in 482380f:

  • DTensor PPO value sequence packing with TP2+SP
  • DTensor PPO value context parallelism with TP2+SP+CP2

Both recipes, launcher scripts, and nightly suite entries are included. I validated the YAML and shell dry runs and also ran the focused 2-GPU worker suite: all 6 CP and sequence-packing training/equivalence tests passed .

@fujial-code

Copy link
Copy Markdown
Contributor Author

/ok to test 482380f

@fujial-code
fujial-code force-pushed the feat/dtensor-value-seqpack-cp branch from 482380f to 9213306 Compare October 9, 2026 20:24
@fujial-code fujial-code added CI:L2 Run doctests, unit tests, functional tests, and convergence tests and removed CI:L1 Run doctests, unit tests, and functional tests labels Oct 9, 2026
@fujial-code

Copy link
Copy Markdown
Contributor Author

/ok to test 9213306

@fujial-code

Copy link
Copy Markdown
Contributor Author

Self-review

Reviewed the rebased 15-file change at 92133062dc42f80890d44c21f07785800563c339 against current main (d8b376aa9f48bebb19e8fab0860a4784a0666847). The rebase conflicts were resolved by adapting the value-model paths to the current Automodel APIs rather than retaining the superseded manual CP plumbing.

What was verified

  • The PPO guards now allow Automodel value sequence packing or CP independently, while preserving the existing CP + packing and dynamic-batching exclusions.
  • Packed token-level regression scores are restored to the original batch/sequence layout before value alignment.
  • CP value scores and LOGIT losses use the current ContextParallelSharder to restore canonical token order; the right shift happens only after that gather.
  • The generic LossPostProcessor supplies the current CP gradient-fanout correction, so the rebased implementation does not duplicate the new main-branch logic.
  • Policy CP retains its float32-capable backend choice, while regression value CP keeps the FlashAttention precision/backend constraint needed for GQA.
  • The new recipes, nightly launchers, suite entries, and shared value-alignment helper remain present after the rebase.

Rebase findings resolved

  • Replaced the removed dtensor_cfg name with automodel_cfg in the new recipes and tests.
  • Replaced the old value-specific CP all-gather/padding helpers with the current Automodel sharder, which now owns padding, token-layout restoration, and trimming.
  • Removed the obsolete CP rejection in the value worker and score postprocessor.
  • Updated tests for the current ProcessedInputs and checkpoint APIs.
  • Preserved all eight commits with DCO sign-offs and confirmed the PR now has a GitHub merge ref against current main.

Verification status

  • Focused CPU tests for value shifting, packed-score reconstruction, CP gather-before-shift, reward-model FlashAttention arguments, and setup validation: 9 passed.
  • Broader Automodel unit run: 28 passed, then stopped at an existing test that directly requires a CUDA driver on the login node.
  • Ruff lint/format, Python compilation, YAML parsing, shell syntax, and git diff --check: passed.
  • Pyrefly reports no errors in the changed files; the project-wide run still reports four unrelated Transfer Queue environment/import errors.
  • GPU/runtime coverage on the rebased SHA is intentionally left to the newly authorized CI:L2 run.

No remaining correctness or mixed-version API findings were identified in the current diff.

@fujial-code

Copy link
Copy Markdown
Contributor Author

/ok to test f30176f

@fujial-code
fujial-code force-pushed the feat/dtensor-value-seqpack-cp branch from aca676f to e429d72 Compare November 3, 2026 08:56
@github-actions github-actions Bot added the Documentation Improvements or additions to documentation label Nov 3, 2026
@fujial-code

Copy link
Copy Markdown
Contributor Author

/ok to test e429d72

@fujial-code

Copy link
Copy Markdown
Contributor Author

/ok to test 00cfdbc

@fujial-code fujial-code added CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) and removed CI:L2 Run doctests, unit tests, functional tests, and convergence tests labels Nov 3, 2026
Comment thread tests/test_suites/nightly.txt Resolved
@yuki-97

yuki-97 commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

/ok to test 7a77c66

yuki-97
yuki-97 previously approved these changes Nov 4, 2026
@yuki-97

yuki-97 commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

/ok to test 1495948

@yuki-97
yuki-97 enabled auto-merge (squash) November 4, 2026 08:47
@fujial-code

Copy link
Copy Markdown
Contributor Author

/ok to test 3448605

@yuki-97
yuki-97 merged commit 6ef5638 into main Nov 5, 2026
94 checks passed
@yuki-97
yuki-97 deleted the feat/dtensor-value-seqpack-cp branch November 5, 2026 07:59
avenkateshha added a commit that referenced this pull request Nov 16, 2026
Main's #3320 removed the is_reward_model parameter from
prepare_model_forward and dropped it at the five existing call sites. The
PR's get_topk_logits_ipc call merged without a textual conflict and still
passed is_reward_model=False, so its first teacher microbatch would raise
"TypeError: prepare_model_forward() got an unexpected keyword argument
'is_reward_model'". That is the CSD path for an Automodel student with
teacher_topk_ipc_k > 0 (128 by default). Drop the keyword so the call
matches the other five.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Adithya Hanasoge <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) Documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PPO Dtensor value model support sequence packing + CP

3 participants