Skip to content

Routed Experts Return (MoE Routing Replay / R3)

--enable-return-routed-experts makes vLLM return the routed experts (the expert IDs each MoE layer's router selected) for every token of a request. RL trainers use it for MoE routing replay (R3): the training forward pass reuses the rollout's routing.

vllm serve Qwen/Qwen3-30B-A3B --enable-return-routed-experts
# offline: LLM("Qwen/Qwen3-30B-A3B", enable_return_routed_experts=True)

The flag sets AuxOutputConfig.enable_return_routed_experts (full form: --aux-output-config '{"enable_return_routed_experts": true, "max_bytes": N}'). There is no per-request on/off switch.

Requirements

Startup fails with a ValueError unless:

  • the runner is generate and the model is MoE;
  • prefix caching is enabled (the default). --no-enable-prefix-caching fails; the old v0.21 advice to disable it is wrong.

Rejected at startup: adaptive speculative verification, PP > 1 and DCP/PCP > 1. Regular speculative decoding works.

KV connectors are not rejected, including the prefill/decode ones (NixlConnector, MoRIIOConnector, MooncakeConnector, ...). In a prefill/decode deployment, however, the decode engine holds no rows for the KV the prefill engine computed. It accepts that KV only up to routed_experts_prompt_start and computes the rest of the prompt again, so the output is correct but the prefill work past that position is lost.

Backend row restoration

A backend's MoERunner or RoutedExpertsCaptureSource can optionally implement restore_routed_experts_rows(topk_ids, ctx) -> Tensor. During binding, the capturer selects the first such module in model.modules() order and uses its callback for every captured layer. Without a provider, the existing DP/TP recovery is used. Existing capture sources need no changes.

The callback must restore this DP rank's token-ordered logical expert IDs for all captured layers, using the supplied ForwardContext rather than the provider's layer-specific state. It must preserve the input, device, dtype and top-k width, and keep collective participation consistent across ranks, including empty ranks. Operations must support graph replay. Models requiring different per-layer restorers cannot use this hook. Repeated layer IDs do not select a different restorer; the existing buffer-write semantics are unchanged.

Output format

API Field
Offline (LLM, AsyncLLM) CompletionOutput.routed_experts: np.ndarray or None
/v1/completions, /v1/chat/completions choices[i].routed_experts: base64 .npy, non-streaming only
/inference/v1/generate routed_experts per choice (base64 .npy); with stream: true, on the final chunk
  • Shape: (num_tokens - 1 - routed_experts_prompt_start, num_layer_visits, num_experts_per_tok), num_tokens = prompt + output. The layer axis has one entry per decoder layer visit, in execution order; the rows of dense layers are 0. The last sampled token has no row.
  • Layer visits. For most models a token visits each decoder layer once: num_layer_visits is num_hidden_layers and the layer axis is indexed by layer id. A loop transformer (Inkling 2 loop checkpoints) runs its core layers once per pass, so the axis follows layer_visit_ids: the prelude layers, every core layer of pass 0, of pass 1, and so on, then the coda. loop_num_passes is set for the engine, not per request: every request, and every object the engine writes, has the same number of visits, and objects written with a different pass count do not match the rows of this engine.
  • Rows: row i is position routed_experts_prompt_start + i of prompt_token_ids + output_token_ids.
  • dtype: uint8 for at most 256 experts, else uint16; logical expert IDs.
  • Filled only when the request finishes. None if the flag is off or the request aborted before any forward pass.
import base64, io
import numpy as np

experts = np.load(io.BytesIO(base64.b64decode(choice["routed_experts"])))

routed_experts_prompt_start

Per-request, default 0: drops the first N prompt-token rows (e.g. routing already received in earlier turns). Must be 0 <= value <= prompt_len, else HTTP 400. Pass it top-level on /v1/*completions, inside sampling_params on /inference/v1/generate, or as SamplingParams(routed_experts_prompt_start=...).

Row storage

The aux output connector (vllm/distributed/aux_output_connector/) stores routing rows per engine, keyed by KV block hash, in the host memory of the engine's first tensor-parallel worker. Each row exists once, in the engine that computed it; inline rows need no separate object store. Every data-parallel rank is a separate engine with its own rows.

The scheduler holds the rows of every block it keeps in the GPU prefix cache, and, when a KV connector is configured, of blocks the connector may load back, up to the store capacity. It reuses a cached block only while it holds that block's rows and otherwise computes the block again, producing new rows. Positions before routed_experts_prompt_start need no rows: a request reuses cached KV up to that position even when their rows are gone.

Replica affinity

A later turn of a session reuses its prefix only on the engine that holds the prefix's KV and rows. Sent to any other engine, the request computes the whole prefix again:

  • time to first token and throughput vary with where each turn lands, and multi-turn rollouts pay for their context once per turn;
  • the prefix-cache hit rate falls as replicas or load grow and is not reproducible across runs;
  • a KV store shared between replicas does not fill the gap: the scheduler refuses an external KV hit that reaches past routed_experts_prompt_start unless this engine holds the rows of every loaded block.

Route every request of a session (a multi-turn rollout, or a group of samples sharing a prompt) to the same engine:

  • Put a session ID on each request and have the router map it to an engine with consistent hashing (a hash ring or rendezvous hashing), so adding or removing an engine remaps only its share of sessions.
  • Fall back to another engine only when the target is down or saturated, and accept that the next turn of that session recomputes its prefix.
  • Set routed_experts_prompt_start to the number of tokens whose rows the client already holds. A hit then needs rows only for the positions it returns, and a shared KV store can serve the earlier turns on another engine.

When requests of a session cannot be pinned to one engine, or replicas share KV through a store, deliver the rows through a shared object store instead (see Delivering routed experts through a shared object store).

Weight updates and cache resets

Any reset of the prefix cache drops every row of the engine. That includes sleep at level 1 or 2, pause_generation(clear_cache=True) and reset_prefix_cache. A turn sent after the reset has rows only for what it computes again.

What happens to the KV is your choice, through reset_connector:

Call GPU prefix cache KV connector tier (e.g. CPU offload) Rows
pause_generation(clear_cache=False), weights updated in place, resume_generation() kept kept kept
sleep(level=1, reset_connector=False) cleared kept cleared
sleep(level=1) (default reset_connector=True) cleared cleared cleared
  • No reset. Updating weights while paused with clear_cache=False keeps the cache and the rows, so later turns of a session keep their hits. The reused KV and rows were computed by the weights in place when each position was first computed.
  • Sleep, keeping the connector. With reset_connector=False, the KV of earlier turns can still be loaded back from the connector tier, but its rows are gone. Set routed_experts_prompt_start to the number of tokens whose rows the client already holds: positions before it need no rows, so they load from the tier and only the new part of the prompt is computed. Without it, the hit reaches past routed_experts_prompt_start into blocks whose rows this engine no longer holds, so it is refused and the prompt is computed again.
  • Sleep with the default. Every session computes its whole prefix again on the next turn.

Keeping the cache or the connector tier across an update means later turns reuse KV, and rows, computed by the previous weights. This is a training decision, not a correctness requirement of vLLM: keep them if your algorithm accepts that staleness, reset with the defaults if it does not. The defaults reset because vLLM cannot know which one you want.

Sizing max_bytes

Rows per token take num_layer_visits × num_experts_per_tok × itemsize bytes, with itemsize 1 for at most 256 experts and 2 above; every layer visit counts, dense ones included.

  • Default (max_bytes unset). One block of rows per KV block: the KV cache token capacity, logged at startup as KV cache size: N tokens, times the bytes per token, doubled when a KV connector is configured. The arena is anonymous memory, committed as rows are written.
  • A smaller max_bytes does not save memory. Rows of blocks in the GPU prefix cache are never evicted. When they outgrow max_bytes, the store copies its whole arena into one at least twice as large and logs a warning, a latency spike for the steps that wait on the store. The LRU drops only rows of blocks the scheduler no longer holds, so the rows of a session last as long as its KV does.
  • Size the KV cache for the sessions. Later turns hit only while sessions per engine × average context ≤ KV cache token capacity, with headroom for the peaks; past that, KV evicts and turns recompute whatever max_bytes is.
  • Set max_bytes only with a KV offload tier larger than the GPU cache. Use (GPU KV tokens + offload tier tokens) × bytes per token × 1.25, so rows stay for every block the tier can load back.
Model Bytes per token Sessions × context per engine Rows
Qwen3-30B-A3B (48 layers, top-8 of 128) 48 × 8 × 1 = 384 B 32 × 16K = 524K tokens 201 MB
DeepSeek-V4.1-Flash (40 layers, top-6 of 384) 40 × 6 × 2 = 480 B 51 × 200K = 10.2M tokens 4.9 GB
DeepSeek-V4.1-Flash 480 B 51 × 1M = 51M tokens 24.5 GB

For Qwen3-30B-A3B the rows are 0.4% of the bfloat16 KV of the same tokens (96 KiB per token), so host memory is never the limit; the same holds for every row of the table on a node with hundreds of GB of host memory. What limits reuse is the KV cache capacity and replica affinity, not the row store.

Example: examples/rl/routed_experts_e2e.py.

Delivering routed experts through a shared object store

With inline return the client must hold every row it received and hand it to each request or trainer worker that needs the same prefix. An engine started with object_key_prefix instead writes every request's rows once to a shared object store, and the response lists object keys; any process that can reach the store reads them. The engine delivers one way for all requests: with object_key_prefix every request delivers objects and none returns inline rows, without it every request returns inline rows as above. The design, its guarantees and its limits are in Aux Outputs for RL.

Start the server with an object store and a new key prefix per start. The default store is Mooncake in standalone-store mode ("mode": "standalone-store" and "global_segment_size": 0 in its configuration file), separate from any Mooncake KV store; object_store names another implementation of AuxObjectStore (the store):

vllm serve <model> --aux-output-config \
  '{"enable_return_routed_experts": true, "object_key_prefix": "<run>",
    "object_store_extra_config": {"config_path": "<mooncake config>"}}'

Send cache_salt (top-level) and routed_experts_prompt_start (inside sampling_params) on /inference/v1/generate, or offline cache_salt in the prompt and SamplingParams(routed_experts_prompt_start=...):

  • Scope. On this engine cache_salt is the sharing and the deletion scope: set it to the session, group or trajectory id, chosen per rollout as in choosing a cache salt. Requests share KV and objects only within one salt, and no KV hit crosses salts. Requests without a salt share one scope, the directory @none. The salt appears, percent-encoded, in the object keys, so this mode is for single-tenant RL deployments, not for salts used as tenant secrets.
  • Restrictions. Every request to this engine is rejected with a validation error with n > 1, with stop strings (use stop_token_ids), and as a streaming-input request.
  • Response. The final output carries aux_objects: [key, start, end] entries, the rows of positions [start, end) being the first end - start rows of object key. They cover [routed_experts_prompt_start, num_tokens - 1), the prefix-cache hits included.
  • Turns. Send routed_experts_prompt_start = min(positions held, longest common token prefix of the new prompt and the held tokens, prompt length - 1), and splice each listing onto the trajectory with the earlier copy winning (turns and splicing).
  • Reading. object_store.read_aux_rows(store, keys, "routed_experts") from vllm.distributed.aux_output_connector, with store = object_store.create_aux_object_store("mooncake", {"config_path": ...}), reads the objects in order and concatenates their rows. Writes have no barrier: wait for a training batch's keys no shorter than the write deadline, 80% of VLLM_EXECUTE_MODEL_TIMEOUT_SECONDS; a key still missing then is lost, and the samples that need it are masked. Once the engine has written the drop marker of a key it dropped, read_aux_rows raises at once while that key is missing, with the cause the marker names: the store was full until the deadline (the engine also warns while it waits; raise VLLM_EXECUTE_MODEL_TIMEOUT_SECONDS to wait longer), the write was still queued behind another one, or the store failed the write. It raises also while a rewrite of the key is still queued; the writer removes the marker when it takes the rewrite.
  • Release. Once every sample of a scope is settled and none of its requests runs or will be sent, remove it with store.remove_prefix(object_store.aux_salt_prefix(object_key_prefix, cache_salt)), that is <object_key_prefix>/<directory>/ with the directory urllib.parse.quote(cache_salt, safe=""), @empty for "", or @none without a salt; it is also the part of any listed key before its last /. The removal takes no object of another salt, also not of one that extends this salt. To release every salt that starts with one string at once, such as all samples of group g1, remove f"{object_key_prefix}/{urllib.parse.quote('g1/', safe='')}" instead. Never send a released salt again.
  • KV cache layout. Hybrid layouts are supported. With object_key_prefix and enable_return_routed_experts set, the engine refuses to start without prefix caching or with prefix replay (pass --no-swa-bounded-replay on DeepSeek-V4.1).

Choosing a cache salt

The cache salt is the unit of both sharing and release. Requests with one salt reuse each other's KV and objects; requests with different salts share nothing, also when one salt is a prefix of the other; and one prefix removal releases one salt, or every salt that starts with a given string. Choose it by what the samples share and by when the trainer is done with them:

Rollout cache_salt Shared Released
Several samples per prompt, trained once the group is complete (GRPO, RLOO, best-of-n) the group id, g1 the prompt, computed and stored once for the group, and each sample's earlier turns once every sample of the group is settled
Several samples per prompt, each released on its own before its group completes (asynchronous or partial rollouts) group and sample, g1/s0 only the requests of one sample: every sample computes and stores the prompt again once the sample is settled, one removal per sample; the encoded prefix of g1/ releases the whole group
One sample per prompt (multi-turn agent rollouts) the trajectory id, t1 the trajectory's earlier turns once the trajectory is settled

Per-sample scopes give up the prompt hits of the other samples. In a GRPO rollout of DeepSeek-V4.1-Flash with 4 samples per group and multi-turn tool use, group scopes left generated tokens per second unchanged (0.997 of the run without objects, standard error 0.021), and per-sample scopes lowered them to 0.907 (standard error 0.017), the first-turn hit rate falling from 82% to 0%. Use them only when samples must be released before their group is done. Requests without a salt all share one scope, so they can be released only together.