Skip to content

Add v2 router - #722

Merged
AkshitaB merged 13 commits into
akshitab/moe-v2-corefrom
akshitab/moe-v2-router
Jun 23, 2026
Merged

AkshitaB merged 13 commits into
akshitab/moe-v2-corefrom
akshitab/moe-v2-router

Conversation

@AkshitaB

Copy link
Copy Markdown
Contributor

Adds nn/moe/v2/router.py: MoERouterV2 and MoERouterConfigV2, the mixture-of-experts router.

Given hidden states (B, S, d_model) it:

  • computes per-token expert scores via softmax or sigmoid gating,
  • selects the top-k experts (with optional input jitter, expert-weight normalization, and the auxiliary-loss-free bias-balancing strategy),
  • returns the expert weights, expert indices, the per-expert token counts, and the inputs needed for the load-balancing / z-loss auxiliary losses (the losses themselves are reduced separately in compute_aux_loss).

It depends only on existing pieces (olmo_core.ops.moe, nn.moe.loss, nn.moe.router, olmo_core.config/distributed.utils); nvtx is an optional import with the olmo_core._nvtx no-op fallback. The optional fp32-cast recompute path (which uses OutputDiscardCheckpoint, 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

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Credits must be used to enable repository wide code reviews.

@AkshitaB AkshitaB changed the title Add MoE-v2 router Add v2 router Jun 11, 2026
@AkshitaB
AkshitaB force-pushed the akshitab/moe-v2-router branch from 702f36d to 10bd2f5 Compare June 12, 2026 23:42
TianhuaTao and others added 5 commits June 23, 2026 00:16
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
AkshitaB force-pushed the akshitab/moe-v2-router branch from 10bd2f5 to 099f309 Compare June 23, 2026 07:16
AkshitaB and others added 8 commits June 23, 2026 00:20
…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]>
@AkshitaB
AkshitaB merged commit 4f377ef into akshitab/moe-v2-core Jun 23, 2026
23 of 24 checks passed
@AkshitaB
AkshitaB deleted the akshitab/moe-v2-router branch June 23, 2026 09:42
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