Repository navigation
feat(trainer): measure how often replayed routes agree with the trainer's routing - #3989
Closed
faresobeid wants to merge 7 commits into
Closed
faresobeid wants to merge 7 commits into
faresobeid wants to merge 7 commits into
Conversation
…ync in the agreement counts
faresobeid
marked this pull request as ready for review
October 13, 2026 17:49
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 04d7d8b. Configure here.
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.

Adds a router-replay check: each MoE layer compares the replayed routes with the router's own top-k and logs how often they agree. A content check (no holes, ids in range) cannot see routes that are misaligned with their tokens, for example a one-token shift in packing, CP sharding or concatenated multi-turn payloads. miles caught such a bug this way: miles#3146, where under BSHD 3 of 4 tokens got the wrong experts.
What
TokenChoiceTopKRouter.forward, when it getsrouted_experts, also returns its own top-k, taken from the scores it already computed (underno_grad; one extra top-k per layer, no extra gate GEMM or router all-gather).MoE.forwardaccumulates, per layer:learned_selection = False), because inference does not pick their experts from the scores.get_load_balance_statsreduces the counts with the other MoE stats:router_replay/agreement: mean shared fraction, averaged over layers and ranks;router_replay/zero_overlap: fraction of tokens sharing no expert, max over layers and ranks.Replay Agreement.zero_overlapexceeds 1 % (MAX_REPLAY_ZERO_OVERLAP, as miles suggests).enable_router_replayis every micro batch on every rank, so all ranks agree on the collective.Checks
pytest tests/unit/train/models/test_moe.py tests/unit/orchestrator tests/unit/test_configs.py -m "not gpu": 361 passed. ruff is clean.CPU check of the counts after moving the own top-k into the router: 6 replayed rows, one with a repeated id, gives
[6, 5, 0, 5].GPU, 1 node,
configs/ci/integration/reverse-text-moe(samsja/mini-glm-moe) with--trainer.enable-router-replay, 10 steps:Replay Agreement 99.4%on every step, and no warning.Replay Agreement 59.3%. Every step logged3.7% of replayed tokens in some MoE layer share no expert ..., and Mismatch KL went from 0.0001 to 0.027.GPU, Qwen3-30B-A3B reverse-text, 1 node (EP 4, compile on),
max_off_policy_steps = 0, 6 steps:Replay Agreementis 98.9-99.0% andzero_overlapis 0 on every step. Before the placeholder skip, the same setup showed 98.4% and 1.0-1.2%, and the warning fired on every step. A per-token dump of steps 1-8 found all zero-overlap tokens at the final token of a sample; that row equals the previous row in all 48 layers (605/605).Likely conflicts
Note
Medium Risk
Extends the MoE forward and distributed stat packing/reduction paths when router replay is enabled, but only adds metrics and logging without changing routing or loss computation.
Overview
Adds router replay agreement metrics so training can detect when replayed expert routes are misaligned with the tokens they are applied to (e.g. packing or sharding bugs), beyond simple validity checks on route tensors.
When
routed_expertsis supplied, learned-selection routers also compute their own top-k underno_gradandMoEaccumulates per-layer counts (replayed/checked tokens, zero-overlap tokens, shared-expert fraction), skipping padding rows and hash-routed DeepSeek-V4 layers (learned_selection = False). These roll into existing MoE load-balance reduction asrouter_replay/agreement(mean over layers/ranks) androuter_replay/zero_overlap(max over layers/ranks). The step log shows replay agreement and warns if any layer exceeds 1% zero overlap.No behavior change when replay is off; overhead is one extra top-k per layer only during replay.
Reviewed by Cursor Bugbot for commit 798da15. Bugbot is set up for automated code reviews on this repo. Configure here.