Skip to content

fix(experience): merge routed_experts when only some samples captured routing - #5

Merged
hijkzzz merged 1 commit into
NVIDIA-NeMo:mainfrom
khazic:khazic/fix/routed-experts-mixed-none-merge
Jul 14, 2026
Merged

hijkzzz merged 1 commit into
NVIDIA-NeMo:mainfrom
khazic:khazic/fix/routed-experts-mixed-none-merge

Conversation

@khazic

@khazic khazic commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Problem

Experience._merge_item and make_experience_batch dispatch the per-field merge on the first sample's type. routed_experts (R3 rollout routing replay) is per-sample either a (1, num_moe_layers, topk, T) tensor or None. When a micro-batch mixes the two, the first-element dispatch fails two ways depending on ordering:

  • Leading None → the merge returns None, silently discarding the captured routing of every other sample in the micro-batch. R3 replay is defeated for the whole micro-batch, so train and rollout routing diverge again (exactly what R3 exists to prevent).
  • Trailing None → zero_pad_sequences calls None.size(-1) and raises AttributeError, crashing make_experience.

Reachability

A rollout carries routed_experts=None whenever the engine returns no routing array for a non-empty completion (RouterGenerateClient yields None, absorb_routing early-returns, the sample stays None). With routing_replay on, sibling rollouts do carry routing, so the mix is reachable, and it lands in the balance_experiences → make_experience_batch path that runs every training step. The pad_value=-1 sentinel already wired for routed_experts shows mixed-length merges are intended; only the None case was unhandled.

Fix

Before the field merge (in both concat_experiences and make_experience_batch), fill any missing routed_experts with the -1 sentinel — "keep live routing" on every token, which is exactly "no captured routing" — sized to that sample's own sequence length so it stays seq-aligned with sequences after right-pad/concat. No-op unless some but not all samples in the batch carry routing; an all-None batch still merges to None.

Sizing to the sample's own length matters: a zero-length placeholder would let a longer unrouted sample pad routed_experts to a shorter seq dim than sequences, desyncing the token axis the training forward replays against.

Tests

Added to tests/unit/test_routing_replay.py (all pass, plus the pre-existing 10):

  • mixed None/tensor in both orders → no crash, no silent drop, unrouted sample becomes all -1
  • unrouted sample is the longest → routed_experts seq dim tracks sequences (the alignment guard)
  • concat_experiences mixed path with the unrouted sample longest
  • all-None still merges to None
$ python -m pytest -q tests/unit/test_routing_replay.py tests/unit/test_experience.py
20 passed

ruff check / ruff format --check clean on the changed files; python -m compileall -q molt tests clean.

… routing

Experience._merge_item and make_experience_batch dispatched on the first
sample's type, so a micro-batch that mixed a captured-routing sample
(routed_experts tensor) with an unrouted one (routed_experts=None) either
silently returned None — dropping R3 routing for the whole micro-batch and
letting train/rollout routing diverge — or crashed on None.size(-1) in
zero_pad_sequences, depending on which sample came first.

A rollout ships routed_experts=None whenever the engine returns no routing
for a non-empty completion (router yields None -> absorb_routing early-returns),
so under routing_replay this mix is reachable and lands in the unconditional
balance_experiences -> make_experience_batch path every step.

Fill a missing sample's routed_experts with the -1 sentinel (natural routing
on every token) before the field merge, sized to that sample's own sequence
length so it stays seq-aligned with sequences after right-pad/concat. No-op
unless some — but not all — samples in the batch carry routing; all-None
still merges to None.

Signed-off-by: khazic <[email protected]>
@copy-pr-bot

copy-pr-bot Bot commented Jul 14, 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.

@hijkzzz
hijkzzz merged commit a992877 into NVIDIA-NeMo:main Jul 14, 2026
1 check passed
hijkzzz added a commit that referenced this pull request Jul 14, 2026
Rewrite _fill_missing_routed_experts (added in #5) more tightly: a single
next(...) scan instead of a `present` list + two-branch guard, a named
(layers, topk) unpack, and a 3-line "why" docstring. Behaviour is identical
(all-None -> untouched; all-present -> skip; mixed -> fill each None sample
a -1 block sized to its own sequence). 14 routing_replay tests pass.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@khazic
khazic deleted the khazic/fix/routed-experts-mixed-none-merge branch July 14, 2026 11:14
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.

2 participants