Skip to content

vllm.model_executor.layers.fused_moe.routed_experts_capturer

Classes:

Functions:

RoutedExpertsCapturer

Worker-side capturer for routed experts, lives on GPU.

Layer-level hooks call capture inside the forward pass. Routing rows owned by this DP rank are written into a preallocated device buffer.

The device buffer uses int32. Stable snapshots use the narrowest dtype that can represent every logical expert ID.

Invariants
  • One instance per worker; shape is fixed at init and covers the worst-case step (max_num_batched_tokens tokens).
  • Every routed layer overwrites the current step's token rows.

Methods:

_restore_local_topk_ids(topk_ids, ctx)

Restore this DP rank's token rows, trimming trailing SP padding.

Layout priority: cross-DP batch, local batch, gathered DP/TP shards, then a local TP shard. Gathered rows are DP-major/TP-minor; preserve this priority when layout sizes coincide. PCP is unsupported.

Without DP metadata, use the pre-SP batch descriptor. Unknown counts pass through; known counts must match a full, sharded or padded batch. A full local batch avoids gathering, including zero/one-token batches where only the publishing TP0 rank needs the real rows.

snapshot_routing_data(num_tokens)

Return a stable snapshot of the current routing data.

RoutedExpertsRowRestorer

Bases: Protocol

Methods:

restore_routed_experts_rows(topk_ids, ctx)

Restore local token-ordered logical IDs for all captured MoE layers.

Binding selects the first provider. Read current metadata from ctx; do not depend on the provider's layer-specific state or modify input. Return a 2-D tensor with the input device, dtype and top-k width. Collectives must be rank-consistent and graph-replay safe.

RoutedExpertsSink

Layer-owned buffer and callback for the expert ids a monolithic kernel routes to; it outlives kernel rebuilds on weight reload.

bind_routed_experts_capturer(model, capturer)

Attach capture callbacks to the target model's MoE routers.