Skip to content

feat(forest): keep execution padding out of router statistics - #5380

Open
bg51717 wants to merge 68 commits into
mainfrom
biguo/forest-router-stats
Open

bg51717 wants to merge 68 commits into
mainfrom
biguo/forest-router-stats

Conversation

@bg51717

@bg51717 bg51717 commented Nov 23, 2026 •

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

What and why

Stacked on #5379.

Forest training (#5047) adds rows that carry no loss: padding rows that fill a batch up to the row multiple, and rows of failed rollouts that borrow another row's input. On an MoE model these rows still reach the router, so their tokens count toward expert load and shift the next expert-bias updates.

Under Forest training these rows are now always left out of expert-load counts:

  • The reassembler marks rows of failed rollouts, which borrow another row's input, in a new router_excluded_rows column, and the per-batch padding copies from feat(forest): pad Forest rows once per training batch #5379 set it too.
  • When a model's router statistics need a padding mask (live expert-bias updates), the policy and a Megatron PPO critic count those rows as router padding. The critic had no router padding mask before, so it now also leaves sequence padding out.
  • Setup rejects a policy or critic with live expert-bias updates that cannot take a padding mask, such as HybridBlock chunkwise context parallelism. No config key.
  • The router-mask helpers move from the policy worker to nemo_rl/models/megatron/common.py, so the critic can use them.
  • New GB200 nightly ppo-nanov3-30BA3B-3n4g-megatron-gym-single-controller-forest: Forest PPO on Nemotron-3 Nano 30B-A3B with expert-bias updates on the policy and the critic. Every other Forest test trains dense Qwen3-0.6B, where no mask is built.

Issues

Follow-up to #5047.

Convergence / perf / functionality proof

Forest PPO on Nemotron-3 Nano 30B-A3B (the new nightly's recipe, 2 steps), on GB200 3 nodes x 4 GPUs, once on #5379 and once on this PR. A temporary log printed, for each expert-bias update, the tokens the router counted (summed over ranks, divided by top-k 6) and the real tokens of each rank's rows:

Run Update Rows (padding or borrowed) All tokens Of those rows Counted by the router
#5379 step 1, critic 24 (2) 187,388 15,102 225,384
#5379 step 2, policy 32 (7) 252,601 65,695 252,601
#5379 step 2, critic 32 (7) 252,601 65,695 300,320
this PR step 1, critic 24 (1) 223,027 10,824 212,203
this PR step 2, policy 32 (4) 248,651 33,232 215,419
this PR step 2, critic 32 (4) 248,651 33,232 215,419
  • On feat(forest): pad Forest rows once per training batch #5379 the policy counts every token, padding rows included, and the critic also counts sequence padding.
  • With this PR both count exactly the real tokens (all tokens minus the excluded rows) on every update.
  • Training is unaffected: reward 0.75 then 1.0 (feat(forest): pad Forest rows once per training batch #5379: 0.75, 0.875), critic loss 0.378 / 0.376 (0.391 / 0.381), token_mult_prob_error 1.073 (1.074), the same step time.
  • The new nightly at the PR head passes all its metric checks over both runs (2 steps, then resume to 4) in 42 minutes.
  • Ordinary sequence packing works with any dispatcher. With live expert-bias updates, setup rejects model-owned packing and HybridBlock chunkwise context parallelism.
  • The nightly itself does not show whether rows were excluded, since no metric reports it; the counts above come from a temporary log.
  • Unit tests on GB200: default lane 3673 passed, NeMo-Gym lane 909 passed, Megatron lane 104 passed.

Needs upstream changes

None

bg51717 and others added 30 commits November 3, 2026 07:13
Forest training was GRPO-only. A PPO run now runs GAE on every physical
row with that row's own critic values; the logical owner still decides
validity, so a filtered segment masks all of its owner's rows.

- ppo.adv_estimator.normalize_over: kept_rows (default) or all_rows,
  which also whitens over rows masked from the loss. Execution padding
  never counts.
- The critic takes every Forest row as one optimizer step.
- Forest row padding covers the critic's DP and microbatch sizes too.
- The complete-group check now runs on the PPO path as well.
- Forest requires ppo.value_training_mode=whole_batch: its row padding
  covers the whole step, while split mode shards each chunk on its own.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: bg51717 <[email protected]>
…tics

Execution padding and failed owners' borrowed inputs run through the
model, so with expert-bias updates enabled their tokens count toward the
router's expert loads. Forest keeps counting them by default, as
ordinary training does.

token_capture.exclude_padding_from_router_stats opts into masking them.
The finalizer publishes router_excluded_rows for Forest rows; the policy
and a PPO critic then fetch it, and the Megatron microbatch builder
gives those rows no router-visible tokens in the existing padding mask,
packed or not. The critic also masks sequence-packing padding.

Both workers check the mask only with the option on, and raise an error
naming it when the model cannot honour it, such as an expert-bias MoE
with HybridBlock chunkwise context parallelism.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: bg51717 <[email protected]>
Forest pads every published group to the policy and critic row multiple.
With one rollout per group, most rows a step trains on are then padding:
a DP of 4 turns ~154 real rows into ~512.

token_capture.forest_padding_scope: step pads each training batch once
instead. Pad rows copy real rows under new sample IDs with no loss,
borrowed-input tags, and router exclusion flags;
token_capture.forest_pad_source picks row0 or cyclic copies.
ppo.adv_estimator.whiten_execution_padding lets those copies join
all_rows whitening. Defaults are unchanged.

A rollout snapshot taken during a step accepts that step's padding copies
(<sample_id>_padN) of replayed rows, and restore deletes them before
checking the replay inventory. Full trainer checkpoints run after the step
clears its padding and stay strict.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: bg51717 <[email protected]>
SingleController PPO rejects a rollout-failure drop budget because the
critic expected a full step. A Forest critic trains on the step's actual
rows, so a short step fits and Forest PPO now accepts the budget.

Streaming PPO selects chunks in multiples of whole prompt groups so the
rows split evenly across the policy and critic data-parallel ranks.
Forest groups vary in row count and execution padding already aligns each
chunk to both models, so that alignment only held back ready groups,
rejected some num_prompts_per_step values, and made the sampler raise once
a dropped rollout shrank the step. Forest streaming PPO now selects any
group count, as streaming GRPO does. With forest_padding_scope=step, each
chunk is padded.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: bg51717 <[email protected]>
Unit tests, as PPO counterparts of the Forest GRPO tests:
- Full-batch PPO trains every segment row per epoch and the critic takes
  the step's rows (gbs = rows); streaming PPO steps the policy once and
  trains the critic on all chunks after the refit.
- Invalid logical batches and all-invalid owners never train either model;
  a partial step trains the critic on the surviving rows.
- Whole-owner overlong filtering; the GAE advantage stage is never sharded.
- Forest PPO startup rejections, policy/critic padding validation order,
  checkpoint restore with a critic, the critic row multiple pinned in
  data-plane checkpoints, and value-loss equivalence across packing bins
  and segments.

Functional tests, both in L1_Functional_Tests_SingleController_3:
- ppo_async_gym_single_controller_context_compaction.sh: PPO (GAE,
  Megatron critic) on the context-compaction Forest rows, two steps with
  checkpoints, then a resume to step 4. Its config,
  examples/nemo_gym/ppo_qwen3_0_6b_single_controller.yaml, composes the PPO
  SingleController exemplar with the Gym GRPO config.
- grpo_async_gym_single_controller_context_compaction_resume.sh: the same
  checkpoint/resume for Forest GRPO.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: bg51717 <[email protected]>
whiten_execution_padding only changes whitening under Forest step padding,
normalize_over=all_rows only with normalize_advantages=true, and the legacy
PPO entrypoint reads neither. Reject each of these at startup.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: bg51717 <[email protected]>
…on note

PPO advantages are per physical row, each row a GAE episode, and Forest PPO
needs whole_batch critic training. Restore the truncation and
unsupported-features paragraph, which also applies to PPO overlong filtering.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: bg51717 <[email protected]>
…ler exemplars

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: bg51717 <[email protected]>
…sets

The critic's method also sets its router padding-mask flag, so call it
configure_forest_router_padding; setup calls the policy's check and the
critic's setup by their own names.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: bg51717 <[email protected]>
…loss

With every microbatch size 1 the run never padded. A critic microbatch of 2
and step-scope padding pad steps with an odd row count, in the checkpointed
run and the resumed one. The critic-loss check gets an upper bound.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: bg51717 <[email protected]>
Full-batch and streaming PPO: the padding copies reach the policy and the
critic, the critic gbs counts them, and the step deletes them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: bg51717 <[email protected]>
Main moved rollout checkpointing into nemo_rl/algorithms/rollout_checkpointing
(#5011). Step padding handling of the replay inventory check now lives in
rollout_checkpointing.inventory.validate_replay_inventory, called from the
restorer ("clear") and the snapshotter ("allow"). Forest's estimator check
keeps main's new seq_logprob_error_in_loss rejection.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: bg51717 <[email protected]>
The stage zeroed advantages outside the mask by multiplying with it, so a
dataset loss weight of 0.5 scaled the stored advantage again on top of GAE
and the loss. Zero the tokens outside the mask without rescaling the rest.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: bg51717 <[email protected]>
The inventory exception for forest_padding_scope=step also applied to full
trainer checkpoints, so an extra _pad row passed the save check and restore
deleted it. A full checkpoint never owns a streamed step's rows, so gate the
exception on a rollout snapshot on save and from_rollout_snapshot on restore.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: bg51717 <[email protected]>
raw_reward reads neither normalize_over nor whiten_execution_padding, so
setting them promised a whitening change that never happened. Fail at
config validation instead.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: bg51717 <[email protected]>
The PPO exemplar and three guide summaries said every logical rollout keeps
one advantage, which only holds for GRPO.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: bg51717 <[email protected]>
Remove token_capture.forest_padding_scope and its step mode: the
controller's once-per-batch padding, the rollout snapshot's tolerance for
step padding copies, the config, docs and tests. Forest groups keep padding
themselves to the policy and critic row multiple.

Signed-off-by: bg51717 <[email protected]>
Remove token_capture.exclude_padding_from_router_stats: the
router_excluded_rows field the finalizer publishes, the policy and critic
masks built from it, the critic's extra train fields, the config, docs and
tests. Both models keep the Forest router check, which rejects routing
that execution padding could perturb.

Signed-off-by: bg51717 <[email protected]>
Both PPO estimators now return zero advantages where the mask is zero, so the advantage stage no longer clears them a second time.

Signed-off-by: bg51717 <[email protected]>
Trains Forest PPO on Nemotron-3 Nano 30B-A3B with expert-bias updates on the
policy and the critic, so the router padding mask that keeps Forest's
artificial rows out of expert-load counts is exercised.

Signed-off-by: bg51717 <[email protected]>
@bg51717
bg51717 changed the base branch from main to biguo/forest-step-padding November 30, 2026 10:43
@svcnvidia-nemo-ci svcnvidia-nemo-ci added complexity:high Large or cross-cutting change. Set by Autobot PR classify (skills/pr-classify) convergence-risk:high Likely to change training results. Set by Autobot PR classify (skills/pr-classify) proof:functionality Needs proof that it works. Set by Autobot PR classify (skills/pr-classify) proof:convergence Needs convergence proof (W&B runs). Set by Autobot PR classify (skills/pr-classify) labels Nov 30, 2026
…embler

Since padding moved to the controller, the reassembler builds no padding rows;
the per-batch padding copies set the column themselves.

Signed-off-by: bg51717 <[email protected]>
Per-group padding reported finalize/execution_padding_rows; per-batch padding
reported nothing. The step now logs forest_padding_rows, summed over the
step's chunks. Tests cover two padded streaming chunks and the GRPO path.

Signed-off-by: bg51717 <[email protected]>
The old message (saved=4, current=1) read like a config mismatch; per-group
padded checkpoints cannot resume under any config.

Signed-off-by: bg51717 <[email protected]>
@bg51717

bg51717 commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

/ok to test 269be6e

…ding

Signed-off-by: bg51717 <[email protected]>

# Conflicts:
#	nemo_rl/algorithms/single_controller.py
#	nemo_rl/algorithms/single_controller_utils/setup.py
#	nemo_rl/data_plane/schema.py
…est_utils

pad_forest_step_rows only needs the data-plane client, the checkpoint barrier
and the row multiple, so it becomes a module-level function in the new
nemo_rl/algorithms/forest_utils package, next to logical_advantage.py moved
from single_controller_utils.

Signed-off-by: bg51717 <[email protected]>
…se helper

validate_forest_checkpoint_metadata derives the current row multiple from
forest_training, since restore always passed 1 under Forest; the replay
inventory folds its one-expression padding helper in and requires
step_padding. The guide no longer says padding IDs are fixed before
publication.

Signed-off-by: bg51717 <[email protected]>
The step padder moved to forest_utils.utils.pad_forest_step_rows; its
router exclusion of the pad copies moves with it.

Signed-off-by: bg51717 <[email protected]>
@bg51717

bg51717 commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

/ok to test af85133

Base automatically changed from biguo/forest-step-padding to main December 2, 2026 08:12
…tats

Signed-off-by: bg51717 <[email protected]>

# Conflicts:
#	nemo_rl/algorithms/forest_utils/utils.py
#	nemo_rl/algorithms/single_controller.py
#	tests/unit/experience/test_cc_cpu_pipeline.py
#	tests/unit/experience/test_selected_forest_boundaries.py
#	tests/unit/single_controller/test_logical_advantage.py
#	tests/unit/single_controller/test_setup.py
…tats

Signed-off-by: bg51717 <[email protected]>

# Conflicts:
#	tests/unit/experience/test_selected_forest_boundaries.py
Main renamed token_capture.forest_training to token_capture.forest.enabled.
The router-statistics setup and its test still used the old name.

Signed-off-by: bg51717 <[email protected]>
@bg51717

bg51717 commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

/ok to test 0522291

@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 for resolving the previous comments: the option is gone and exclusion is always on under Forest, the step-padding copies are marked, and the MoE nightly with its before/after comparison is in the description.

Scope: the whole PR at 0522291, including the nemo_rl/ changes the first review did not cover.

Checked in the original review:

  • Mask shape and polarity match the pinned Megatron-LM, and expert-load counts accumulate only with grad enabled, so fetching the column for train steps is enough.
  • Every Forest row carries the flag, and it stays with its row through sorting, packing and DP sharding.
  • The non-Forest path is unchanged, and the three helpers moved to common.py are identical to the originals.
  • The new unit tests ran and passed in CI at the head; the nightly does not run in CI.

Remaining comments

  • Remove the unreachable prepacked-input check in data.py and its mention in the PR description.
  • Remove the unused moe_aux_loss_coeff setting from both the policy and critic recipe blocks.

Nits

  • Use the Forest router-padding flag directly on the critic. Every Forest critic training batch fetches the column.
  • Mark step-padding copies unconditionally. Every Forest batch carries the exclusion column.
  • Combine the two router checks and give the policy and critic setup methods the same name.
  • Drop the leading underscores from the three helpers now imported from common.py.
  • Reuse _unwrapped_chunks in _model_self_packs_for_cp.
  • Rename _router_excluded_fields to _forest_train_fields to make clear that both workers fetch these fields.

nemo-rl-review: kind=expert harness=codex/0.173.0 model=gpt-6.2-astra effort=medium review-pr-team=v3 reviewed=0522291

Comment thread nemo_rl/models/megatron/data.py Resolved
Comment thread nemo_rl/models/megatron/common.py Resolved
Comment thread nemo_rl/models/megatron/common.py Resolved
Comment thread nemo_rl/algorithms/single_controller.py Resolved
@fujial-code

Copy link
Copy Markdown
Contributor

Addressed all eight outstanding review threads in da4ead9.

  • Removed the unreachable prepacked-input guard and the two unused recipe settings; updated the PR description.
  • Consolidated Forest router validation, unified the policy/critic setup RPC, simplified exclusion flags, and updated helper names and affected tests.
  • Validation: 841 controller cases passed (822 initially; the remaining 19 passed after setting HF_HOME), plus 64 targeted Megatron/router/worker cases. Recipe minimization, Ruff lint/import/format checks, and git diff --check passed.
  • Full Pyrefly checking in the test container reports one diagnostic in the untouched nemo_rl/models/generation/megatron/megatron_worker.py:1617: ProcessGroupGloo is missing a timeout argument. No diagnostics were reported in the changed files. The MoE nightly was not rerun for this cleanup.

@fujial-code

Copy link
Copy Markdown
Contributor

/ok to test da4ead9

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

CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) complexity:high Large or cross-cutting change. Set by Autobot PR classify (skills/pr-classify) convergence-risk:high Likely to change training results. Set by Autobot PR classify (skills/pr-classify) proof:convergence Needs convergence proof (W&B runs). Set by Autobot PR classify (skills/pr-classify) proof:functionality Needs proof that it works. Set by Autobot PR classify (skills/pr-classify)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants