06 / Core components

MoE Expert Routing and Router Calibration

Learn MoE expert routing with frozen features, affine heads, hierarchical selection, prompt shortlists, router distillation, and calibrated specialist scores.

Sourcessrc/anchored_moe/routers/README.mdsrc/anchored_moe/branching/generative.pysrc/anchored_moe/routers/base.pysrc/anchored_moe/routers/features.pysrc/anchored_moe/routers/heads.pysrc/anchored_moe/routers/logical.pysrc/anchored_moe/routers/hierarchical.pysrc/anchored_moe/routers/shortlisting.pysrc/anchored_moe/routers/stabilized.pysrc/anchored_moe/routers/stickiness.pysrc/anchored_moe/calibration/global_recalibration.pyexamples/verify_affine_router.pyexamples/verify_logical_router.py

What this part does

Routers turn a token’s pre-MLP hidden state into calibrated evidence for each installed specialist. They preserve stable expert ordering and package ownership, but they do not decide which path runs or execute an MLP.

Integrated

The core identity-feature, independent-head, and logical-router pipeline is integrated into package-driven anchored top-1 composition. Global recalibration and distillation are explicitly composable, while hierarchical, shortlisted, and cross-layer strategies remain research primitives.

Where it fits

For request-level Needle routing, see Needle branches. That owner supports a linear head on shared features or a private router suffix. The preferred browser router generates one select_branch tool call using the current request and bounded recent conversation context. Invalid choices or router failures select the frozen base; generated choices do not carry calibrated probabilities.

The remainder of this page describes the token-level MLP router pipeline.

The router is one stage in a deliberately separated runtime:

  1. a converted layer exposes the hidden state immediately before its MLP;
  2. a frozen feature extractor produces the router feature tensor;
  3. package-owned heads score their experts;
  4. a logical router presents those scores in stable registry order;
  5. a selection policy compares specialist evidence with the zero-score anchor;
  6. inference dispatches the selected experts and combines their outputs.

The routers subpackage owns stages two through four. Calibration fits or adjusts head parameters, policies choose routes, capacity rules handle availability, inference executes sparse paths, and the registry controls package lifecycle.

Implemented approaches

Every row is implemented and validated in the Python framework; the label describes how much explicit assembly is still required.

Router strategies and their current integration boundaries
StrategyFeatureResultCurrent boundaryIntegration label
Identity featuresRTR-001Unchanged pre-MLP hidden statesPackage-compatible feature contractIntegrated
Independent affine headsRTR-002One calibrated score per expertPackage-owned and safely serializedIntegrated
Logical append-only routerRTR-003Frozen layer-local score bankCompiled during explicit top-1 synchronizationIntegrated
Global recalibrationRTR-004New registry-bound logical routerExplicit fit, persistence, and activationComposable
Hierarchical routingRTR-005Package scores plus selected-package local scoresManual hierarchy; no manifest-driven attachmentResearch primitive
Prompt shortlistingRTR-006Full score shape with unselected columns maskedRequires external retrieval scoresResearch primitive
Hard-route distillationRTR-007Frozen affine student routerExplicit assembly; no report-bearing activationComposable
Cross-layer stickinessRTR-008Stabilized anchored top-1 decisionCaller supplies previous routesResearch primitive

Frozen identity features

IdentityRouterFeatureExtractor accepts the pre-MLP hidden state and returns the same tensor object. Version one has no learned projection, allocation, or hidden detachment. This keeps the feature space easy to audit and lets the caller control gradient flow during explicit fitting.

The extractor also exposes an immutable feature specification. Packages bind their heads to that specification so a router trained for one hidden width or feature transformation cannot be attached silently to another.

Independent append-only heads

Each AffineRouterHead owns one expert score at one decoder layer. Its affine weight and bias produce raw evidence; a positive temperature and a threshold turn that evidence into the calibrated score consumed by later policies.

AppendOnlyRouterHeads preserves registration order and rejects duplicate (layer_index, expert_id) ownership. Appending a package does not rewrite old heads, and the safetensors format contains only finite tensors and validated non-executable metadata.

One frozen logical router

compile_logical_router() copies independent package heads into a LogicalAppendOnlyRouter. The runtime stores layer-local affine rows, temperatures, and thresholds as frozen buffers while preserving the same score shape, expert identifiers, and calibrated values.

Compilation is an ownership boundary: later training of a package head cannot change the deployed router. The implementation evaluates columns in a shape-stable order so appending a new column preserves every incumbent score bit-for-bit for identical inputs, dtype, device, and backend.

Explicit global recalibration

recalibrate_global_router() jointly fits copied affine columns against an aligned replay bank of anchor-relative utilities. It returns a new frozen logical router and a report bound to the exact registry and compatibility hashes; source heads, experts, and model state remain unchanged.

This mode intentionally gives up append-only score invariance. It never runs as a hidden installation side effect, and activating its separately persisted registry-router version remains an explicit lifecycle operation.

Hierarchical package and local scoring

HierarchicalRouterScoreBank scores packages first, then evaluates a local logical router only for token rows assigned to the selected non-anchor package. Stable flattened row indices let the policy reconstruct the original token shape without scoring unrelated local experts.

The current hierarchy requires rectangular layer coverage, at least two local experts per package, and globally unique expert identifiers. It is constructed manually from validated logical routers and has no manifest-driven model attachment.

Prompt-level shortlisting

PromptTopMShortlister accepts external finite retrieval scores and selects a stable top-M candidate set for each prompt. A PromptShortlistedLogicalRouter evaluates only those columns, then restores the complete expert dimension with a finite value below the zero-score anchor for every unselected candidate.

The retriever, its training, generation-time reuse, and expert-weight paging are outside this primitive. A retrieval miss therefore cannot be recovered by the token router unless another shortlisted specialist has positive evidence.

Router distillation

distill_frozen_router() takes an immutable teacher score bank, derives anchor-first hard top-1 assignments over cached features, and fits a deterministic affine student. The accepted student compiles into the ordinary frozen logical-router contract, so explicit routed-layer composition needs no distillation-specific dispatcher.

Version one distills assignments rather than score distributions. It does not yet cover top-2 or adaptive targets, persist its report with a registry version, or activate itself on a model.

Cross-layer route stickiness

CrossLayerTop1Stickiness applies a switching margin between the current anchored top-1 winner and a caller-supplied previous-layer route. It compares both candidates using current-layer evidence and retains the incumbent unless the new winner exceeds it by strictly more than the margin.

The stabilizer owns no request state. A model or generation loop must carry the previous route explicitly; zero margin delegates exactly to ordinary anchored top-1 behavior.

How it works

Keep score production separate from route choice

Router output has one specialist column per registered expert. The permanent anchor column is not stored in the router because its evidence is always zero. A selection policy prepends that zero conceptually and applies anchor-first, stable tie-breaking.

This boundary matters: a positive specialist score is evidence, not an execution command. Top-1, top-2, adaptive, compatibility, and capacity policies can interpret the same score bank differently.

Preserve ownership through compilation

Package files retain independently owned affine heads because installation, removal, and versioning operate at package boundaries. The synchronized model uses a compiled logical view because inference needs one stable expert order per layer.

Compilation copies rather than aliases state:

Package heads → validated append order → copied frozen buffers → score bank

The logical architecture is therefore an in-memory runtime representation, not a second package wire format.

Make advanced behavior explicit

Global recalibration may change all scores, hierarchical routing changes which columns are evaluated, shortlisting depends on external prompt evidence, distillation approximates a teacher, and stickiness depends on a previous decision. Each behavior has a separate public entry point and records or returns the metadata needed to audit its boundary.

Mathematical intuition

One affine head converts a feature vector into calibrated expert evidence:

A specialist activates only after its raw affine evidence is adjusted by its own temperature and threshold.
gₗ,ₖ(h) = (wₗ,ₖᵀ φₗ(h) + bₗ,ₖ) / Tₗ,ₖ − τₗ,ₖ
h
Pre-MLP hidden states with shape batch by sequence by model width.
φₗ(h)
Frozen router features; version one is the unchanged hidden state.
wₗ,ₖ, bₗ,ₖ
The affine weight and bias owned by expert k at layer l.
Tₗ,ₖ
A finite positive temperature fitted during calibration.
τₗ,ₖ
A finite activation threshold subtracted from the scaled evidence.

Practical consequenceThe anchor remains at score zero outside the router. A specialist must beat that fixed baseline under the selected policy before it can run.

For K registered experts at one layer, the logical router stacks the calibrated columns without changing their order or meaning:

The runtime router is a stable ordered view of independently owned expert evidence.
Gₗ(h) = [gₗ,₁(h), …, gₗ,K(h)] ∈ ℝᴮˣˢˣᴷ
Gₗ(h)
All specialist scores for one decoder layer.
B
Batch size.
S
Sequence length, or the equivalent token-leading dimensions.
K
Number of registered specialist columns at the layer.

Practical consequenceAppending expert K plus one extends only the final dimension. Existing columns retain their values and stable expert identifiers.

Minimal usage

Compile one package-owned affine head into a frozen logical score bank:

from anchored_moe import AffineRouterHead, AppendOnlyRouterHeads
from anchored_moe import compile_logical_router

heads = AppendOnlyRouterHeads(feature_size=4096)
heads.append(AffineRouterHead(
    4096, layer_index=18, expert_id="code:0"
))
router = compile_logical_router(heads)
scores = router(hidden_states, layer_index=18)

The resulting final dimension follows stable expert order. Comparing these scores with the anchor and executing a path remain separate policy and inference steps.

Verified implementation details

Show append-only score preservation and exact safe reload
original = router(features, layer_index=2)
router.append(AffineRouterHead(
    4, layer_index=2, expert_id="math:0", threshold=0.2
))
appended = router(features, layer_index=2)

assert torch.equal(appended[..., :1], original)
router.save_safetensors(payload_path)
restored = AppendOnlyRouterHeads.from_safetensors(payload_path)
assert torch.equal(restored(features, layer_index=2), appended)

Expected invariant: appending a separately owned head preserves the incumbent column exactly, and strict safetensors reload restores the complete score bank.

Show frozen logical compilation and routed top-1 execution
logical = LogicalAppendOnlyRouter.from_independent_heads(independent)
incumbent = logical(features, layer_index=3)
logical.append(math_head)
appended = logical(features, layer_index=3)

assert torch.equal(appended[..., :1], incumbent)
assert sum(p.numel() for p in logical.parameters()) == 0
output = routed(features)
assert torch.equal(output, expected_output)

Expected invariant: the compiled router owns frozen, storage-independent state; appending preserves old scores, and the shared score-bank interface drives explicit anchored top-1 execution.

Current limitations

  • Version-one runtime features are the identity transform; there is no learned frozen projection.
  • Logical compilation deliberately favors bit-exact append invariance over one fused matrix multiplication and has no independent package format.
  • Global recalibration requires aligned offline replay data and changes every specialist score for one exact registry.
  • Hierarchical routing requires rectangular package topology and explicit construction from logical routers.
  • Prompt shortlisting requires caller-provided retrieval scores and does not retain generation state or page expert weights into memory.
  • Hard-route distillation supports anchored top-1 assignments only and has no report-bearing registry activation.
  • Cross-layer stickiness is caller-sequenced outside the model forward; it does not provide top-2, adaptive, or autoregressive hysteresis.
  • Selection, combination, capacity, dispatch, feature caching, calibration, and package lifecycle remain deliberately separate subsystems.

References

Damai Dai et al. · 2022

StableMoE: Stable Routing Strategy for Mixture of Experts

arXiv:2204.08396
Why it matters here
Motivates separating route discovery from a lightweight frozen deployment router.
How Mutations differs
Mutations distills fixed-zero-anchor top-1 assignments from an arbitrary immutable composed teacher and implements cross-layer switching as a separate deterministic margin rule; it does not reproduce StableMoE's jointly learned balanced teacher or later expert-training stage.

The core feature, affine-head, logical-compilation, hierarchical, shortlisting, and global-recalibration implementations are project-specific framework infrastructure rather than paper-derived router algorithms. Their tensor and module behavior follows the PyTorch module contract, and router payloads use the safetensors format for non-executable state.

The router theory README contains the complete algorithms, invariants, validation coverage, and documented research differences. The architecture blueprint defines the shared fixed-zero anchor, append-only installation, and optional router-only recalibration modes.

  • Read experts for the transformations identified by each router column.
  • Return to configuration and modeling for the current declarative configuration and explicit synchronization boundary.
  • Continue to policies to see how score banks become top-1, top-2, adaptive, or hierarchical route decisions.
  • Read calibration for feature banks, utility labels, ridge fitting, temperatures, thresholds, and replay-based refinement.
  • Read inference for sparse dispatch and route telemetry.