core.inference.contexts.moe_routing_replay#

Module Contents#

Classes#

MoERoutingReplayCache

Per-block storage for MoE routing replay.

API#

class core.inference.contexts.moe_routing_replay.MoERoutingReplayCache(block_size_tokens: int, dummy_block_idx: int)#

Per-block storage for MoE routing replay.

Records the expert choices made during forward passes so a finished request’s full routing history may be reconstructed for downstream customers, if so desired. The storage is host-only: a Python dict keyed by block ID, with ndarray values shaped [block_size_tokens, num_layers, topk].

The class owns only the dict-keyed storage and has no reference to the inference context. scatter_routing_to_blocks (write) and reconstruct_routing_from_blocks (read) are the bulk operations; both take the per-step values they need (token-to-block mappings, block IDs) as explicit arguments rather than reading them off a stored context.

Owned by KVBlockAllocator as allocator.routing_replay.

Initialization

has_data() → bool#

Return True when at least one block has stored routing data.

reset() → None#

Clear all stored routing data.

clear_for_reallocated(block_ids: List[int]) → None#

Drop routing data for blocks that were just popped from the free pool.

scatter_routing_to_blocks(
flat_routing: numpy.ndarray,
block_ids: numpy.ndarray,
positions: numpy.ndarray,
) → None#

Scatter flat routing indices into per-block storage.

Matched prefix-cache blocks already contain routing from the original request and are absent from this forward’s token layout.

Parameters:
  • flat_routing – ndarray of shape [active_token_count, num_layers, topk] aligned with the supplied token layout.

  • block_ids – KV-block ID for each routing row.

  • positions – Position within the KV block for each routing row.

reconstruct_routing_from_blocks(
block_ids: List[int],
total_routing_tokens: int,
) → Optional[numpy.ndarray]#

Reconstruct routing indices from per-block storage.

Parameters:
  • block_ids – Ordered list of block IDs for the request.

  • total_routing_tokens – Expected number of routing tokens.

Returns:

ndarray of shape [total_routing_tokens, num_layers, topk] or None.

store_block_routing(
block_id: int,
positions: numpy.ndarray,
routing: numpy.ndarray,
) → None#

Store routing indices for specific token positions in a block.

Parameters:
  • block_id – The block ID.

  • positions – ndarray of token positions within the block (1D, int).

  • routing – ndarray of routing data [num_positions, num_layers, topk].

get_block_routing(block_id: int) → Optional[numpy.ndarray]#

Get routing indices for a block.

Parameters:

block_id – The block ID.

Returns:

ndarray [block_size_tokens, num_layers, topk] or None if not stored.