Repository navigation
Add v2 router - #722
Merged
Merged
Add v2 router#722
Conversation
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
AkshitaB
force-pushed
the
akshitab/moe-v2-router
branch
from
June 12, 2026 23:42
702f36d to
10bd2f5
Compare
Adds `nn/moe/v2/router.py`: `MoERouterV2` and `MoERouterConfigV2` — the mixture-of-experts router. Given hidden states it computes per-token expert scores (softmax/sigmoid gating), selects top-k experts (with optional jitter, weight normalization, and auxiliary-loss-free bias balancing), and reports the per-expert token counts plus the inputs needed for the load-balancing / z-loss auxiliary losses. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
- Guard the optional `nvtx` import with the `olmo_core._nvtx` no-op fallback. - Disable the optional fp32-cast recompute path (and its OutputDiscardCheckpoint import), which is provided by a separate change; the router falls back to a plain fp32 cast until it lands. - Use the builtin `tuple` annotation instead of `typing.Tuple`. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Cover config num_params, the forward routing contract (weights/indices/ per-expert-count shapes, valid indices, total assignments = tokens * top_k), the scores-only short circuit, and top-1 / sigmoid gating. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Convert the trailing `#` comments on `restore_weight_scale`, `original_top_k`, and `use_recompute_fp32_cast` into Sphinx attribute docstrings, matching how config fields are documented elsewhere. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
OutputDiscardCheckpoint is now available, so restore the optional `use_recompute_fp32_cast` path (and its import): the fp32 router-input cast is materialized through an OutputDiscardCheckpoint and recomputed in backward. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
AkshitaB
force-pushed
the
akshitab/moe-v2-router
branch
from
June 23, 2026 07:16
10bd2f5 to
099f309
Compare
…cstring Document that, unlike other configs in the codebase (which keep dimensions out of the config and take them only as build() arguments), the v2 configs store dimension fields like d_model/num_experts directly, and that this should be unified to the dimension-agnostic build(d_model, ...) convention in the future. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…op debug cruft - num_params()/build() docstrings listed :param d_model:/:param num_experts: that aren't parameters of those methods; correct to the real signature. - Annotate use_recompute_fp32_cast as bool (was the only untyped __init__ arg). - Remove the commented-out debug block (wrote tensors to /workspace, print) and the now-unused _debug_index field it referenced. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Replace the hardcoded local UES_QUANT_SCORES=False gate with a use_quant_scores config field (default False) plumbed through MoERouterConfigV2 -> MoERouterV2, so the quantized-score top-k path (quantize + tie-break for selection, original scores for weights) is selectable instead of dead-code-gated. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Replace the vague "# TODO: check / # WARN: does not work with PP" with a NOTE explaining why: the expert-index recompute cache assumes a single in-flight microbatch with strict first-forward-then-recompute ordering, so interleaved pipeline-parallel microbatches would corrupt its single slot. Also drop the no-op else: pass and a dangling trailing comment. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Return d_model * num_experts directly instead of zero-initializing and accumulating into a local. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
get_top_k returns a (weights, indices) tuple, but the use_quant_scores branch did `.indices` on it, which would raise AttributeError whenever the flag was enabled (latent while the path was dead). Unpack the tuple instead. Add CPU tests for the previously-uncovered router knobs — uniform/random assignment, restore_weight_scale, original_top_k rescale, expert-weight normalization, bias_gamma score-bias routing, use_quant_scores selection parity, grad flow, and compute_metrics scaling — plus a distributed apply_tp test parametrized over gloo (CPU lane) and NCCL (GPU lane) that checks weight replication and sharded-input forward. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Remove tests that don't exercise real router behavior: compute_metrics (injects loss values and reads them back scaled), the grad-flow smoke (only asserts grads are non-None), and random-expert-assignment (all its assertions are generic top-k invariants that hold for any input). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
With matched weights and default config (no bias/normalize/quant/etc.), MoERouterV2 must route identically to the v1 MoELinearRouter. Parametrized over top_k (1, 2) and gating (softmax, sigmoid); compares expert weights, indices, and per-expert token counts. 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.
Adds
nn/moe/v2/router.py:MoERouterV2andMoERouterConfigV2, the mixture-of-experts router.Given hidden states
(B, S, d_model)it:compute_aux_loss).It depends only on existing pieces (
olmo_core.ops.moe,nn.moe.loss,nn.moe.router,olmo_core.config/distributed.utils);nvtxis an optional import with theolmo_core._nvtxno-op fallback. The optional fp32-cast recompute path (which usesOutputDiscardCheckpoint, provided by a separate change) is disabled for now — the router falls back to a plain fp32 cast. Includes CPU unit tests covering the routing contract, the scores-only path, and top-1 / sigmoid gating.🤖 Generated with Claude Code