Skip to content

vllm.model_executor.layers.fused_moe.router.base_router

Classes:

  • BaseRouter –

    Base class providing common functionality for all router implementations.

Functions:

BaseRouter

Bases: FusedMoERouter

Base class providing common functionality for all router implementations.

This class implements the template method pattern where select_experts() handles common pre-processing and post-processing, delegating the actual routing logic to the abstract _compute_routing() method.

Methods:

__init__(top_k, global_num_experts, eplb_state=None)

Args: top_k: Number of experts to select per token global_num_experts: Total number of experts eplb_state: Optional EPLBLayerState for load balancing

_apply_eplb_mapping(topk_ids)

Apply EPLB mapping to convert logical expert IDs to physical expert IDs.

_compute_routing(hidden_states, router_logits, indices_type, *, input_ids=None) abstractmethod

Compute the actual routing logic.

This method must be implemented by subclasses to provide the specific routing algorithm (e.g., grouped_topk, fused_topk, custom routing, etc.).

Parameters:

  • hidden_states

    (Tensor) –

    Input hidden states

  • router_logits

    (Tensor) –

    Router logits for expert selection

  • indices_type

    (dtype | None) –

    Desired dtype for expert indices (may be None)

  • input_ids

    (Tensor | None, default: None ) –

    Token ids, for routers that condition on them

Returns:

_convert_indices_dtype(topk_ids, indices_type)

Convert topk_ids to the desired dtype if needed.

_select_experts(hidden_states, router_logits, topk_indices_dtype=None, *, input_ids=None)

Route the input hidden states to the top-k experts based on the router logits.

This method implements the template method pattern: 1. Validates EPLB state 2. Calls _compute_routing() to get topk_weights and topk_ids 3. Applies EPLB mapping if enabled 4. Converts indices dtype if needed

Returns:

_validate_eplb_state()

Validate that EPLB state is properly initialized if EPLB is enabled.

set_capture_fn(capture_fn)

Set a capture callback for logical routed expert IDs.

get_moe_padding_mask(num_tokens)

Rows of the current batch that are CUDA graph or DP padding.

Routers set these rows' expert ids to -1 so the dispatch and experts skip them; None when VLLM_MOE_SKIP_PADDING is off, the runner does not mark padding, or the router sees more rows than the mask covers (naive DP dispatch all-gathers rows from every DP rank before routing).