Repository navigation
fix(experience): merge routed_experts when only some samples captured routing - #5
Merged
hijkzzz merged 1 commit intoJul 14, 2026
Conversation
… 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]>
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]>
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.
Problem
Experience._merge_itemandmake_experience_batchdispatch 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 orNone. When a micro-batch mixes the two, the first-element dispatch fails two ways depending on ordering:None→ the merge returnsNone, 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).None→zero_pad_sequencescallsNone.size(-1)and raisesAttributeError, crashingmake_experience.Reachability
A rollout carries
routed_experts=Nonewhenever the engine returns no routing array for a non-empty completion (RouterGenerateClientyieldsNone,absorb_routingearly-returns, the sample staysNone). Withrouting_replayon, sibling rollouts do carry routing, so the mix is reachable, and it lands in thebalance_experiences→make_experience_batchpath that runs every training step. Thepad_value=-1sentinel already wired forrouted_expertsshows mixed-length merges are intended; only theNonecase was unhandled.Fix
Before the field merge (in both
concat_experiencesandmake_experience_batch), fill any missingrouted_expertswith the-1sentinel — "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 withsequencesafter right-pad/concat. No-op unless some but not all samples in the batch carry routing; an all-Nonebatch still merges toNone.Sizing to the sample's own length matters: a zero-length placeholder would let a longer unrouted sample pad
routed_expertsto a shorter seq dim thansequences, 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):None/tensor in both orders → no crash, no silent drop, unrouted sample becomes all-1routed_expertsseq dim trackssequences(the alignment guard)concat_experiencesmixed path with the unrouted sample longestNonestill merges toNoneruff check/ruff format --checkclean on the changed files;python -m compileall -q molt testsclean.