vllm.model_executor.layers.fused_moe.runner.moe_runner
¶
Classes:
-
MoERunner–Standard MoE runner implementation for executing Mixture of Experts layers.
MoERunner
¶
Bases: MoERunnerInterface
Standard MoE runner implementation for executing Mixture of Experts layers.
This is the primary concrete implementation of MoE execution logic, providing comprehensive support for standard MoE operations. It handles: - Expert routing and token dispatching using various routing strategies - Shared experts computation with optional parallel execution using CUDA streams - Tensor model parallel and expert parallel operations - Multiple quantization methods and optimized kernel selection - Both monolithic and decomposed expert execution paths - Integration with various parallel execution modes (TP, EP, DP)
The runner orchestrates the complete MoE forward pass including routing tokens to experts, executing expert computations in parallel, and combining results. It supports advanced features like overlapped execution of shared experts, optimized kernels for different parallel configurations, and seamless integration with vLLM's distributed execution framework.
Eventually, this class may be split into more specialized implementations for different configurations (e.g., with/without shared experts, gates, etc.).
Methods:
-
apply_routed_input_transform–Apply transform for routed experts (e.g., latent projection).
-
apply_routed_output_transform–Apply transform to routed expert output (e.g., latent to full dim).
-
forward–Invoke the fused moe layer.
-
process_weights_after_loading–Fuse router and shared expert gate weights.
-
set_eplb_state–Register the EPLB state in this layer.
Attributes:
-
expert_local_to_global(Tensor | None) –Routing table: local expert ID to global expert ID.
-
expert_map_manager–Forward to routed_experts.expert_map_manager for backward compatibility.
-
expert_physical_to_global(Tensor | None) –Routing table: physical expert ID to global expert ID.
expert_local_to_global
property
¶
Routing table: local expert ID to global expert ID.
expert_map_manager
property
¶
Forward to routed_experts.expert_map_manager for backward compatibility.
expert_physical_to_global
property
¶
Routing table: physical expert ID to global expert ID.
_apply_quant_method(hidden_states, router_logits, shared_experts_input, input_ids=None, shared_experts_overlapping=False, hidden_states_scale=None)
¶
Run expert routing and the fused MoE kernel via the quant method.
Orchestrates shared expert execution (before/after), expert selection via the router, and the actual fused MoE computation. Returns (shared_expert_output, fused_expert_output).
shared_experts_overlapping should be True only if using multi-stream
overlap. Then the shared expert was already launched in a separate
stream, so the results only have to be awaited here.
hidden_states_scale is set when hidden_states were quantized before
the PCP all-gather.
_forward_impl(hidden_states, router_logits, shared_experts_input, input_ids=None)
¶
Entry point called by the custom op to run the MoE computation.
Handles pre-dispatch setup (gate application, external shared expert triggering, quant config init) then performs the following steps within the sequence-parallel context.
- Performs expert routing
- fused MoE kernel execution
- shared expert computation.
Returns routed output, optionally paired with shared-expert output. A fused consumer may request the routed output in deferred-finalize form.
_map_global_expert_id_to_local_expert_id(expert_id)
¶
Map global expert ID to local expert ID.
_maybe_add_zero_expert_output(result)
¶
Add the zero expert's contribution to the final result.
When a ZeroExpertRouter is used, it computes a bias-like output from the "zero expert" that is added to the combined routed+shared expert output.
_maybe_apply_routed_scale_to_output(shared_output, fused_output)
¶
Apply routed_scaling_factor to the output.
Scale the fused expert output by routed_scaling_factor. With RoutedScaleMode.OUTPUT_FP16_GUARD, FP16 outputs with shared experts avoid overflow by dividing shared_output by the scale instead; the model's decoder layer must compensate with matching divisions.
_maybe_pad_hidden_states(shared_experts_input, hidden_states)
¶
Pad hidden_states to moe_config.hidden_dim and compute the original dimension for later truncation.
For latent MoE, the routed hidden_states may be smaller than hidden_dim. Padding ensures uniform tensor sizes through the fused MoE kernel. The returned trunc_size is used by _maybe_reduce_final_output to strip the padding from the result.
_maybe_reduce_final_output(states, trunc_size, output_is_reduced=None)
¶
All-reduce the combined output if needed.
This is the "late" all-reduce path. When neither fused nor shared output was individually reduced, the combined sum is all-reduced here. Skipped when sequence-parallel is active (SP handles its own reduction) or when the early path already reduced both outputs.
_maybe_reduce_routed_output_before_transform(fused_output, fused_output_is_reduced)
¶
All-reduce latent routed output before its output transform.
Latent MoE output transforms may contain non-linear ops, e.g. RMSNorm. TP partial routed outputs must be summed in latent space before such transforms are applied.
A transform that commutes with the TP sum is exempt: if
sum_r T(x_r) == T(sum_r x_r), applying the transform to the local
partial output and letting the existing late all-reduce sum the
combined result is equivalent, and costs one collective instead of two.
Such a transform opts out by setting reduce_commutative = True.
The default is False, so transforms that do not declare themselves
keep being reduced early.
_maybe_reduce_shared_expert_output(shared_output, fused_output_is_reduced=None)
¶
All-reduce shared expert output when the combine kernel already reduced fused output.
- If the combine kernel does the reduction for fused_output, reduce shared_output separately. O.w, reduce fused_output+shared_output later.
- If we have SP (TP=N, DP=M, EP), there is a separate AG step handled in the model.
_quantizes_before_pcp_gather()
¶
Whether the routed experts take input quantized row by row by a plain monolithic prepare: dynamic per-token-group fp8, or NVFP4 with a static global scale and unswizzled block scales.
_sequence_parallel_context()
¶
Return a context manager for sequence-parallel token redistribution.
When sequence parallelism is active, returns a context that handles local size tracking for proper token scatter/gather. Otherwise returns a no-op context.
apply_routed_input_transform(hidden_states)
¶
Apply transform for routed experts (e.g., latent projection).
This is called by MoERunner.forward_native. The original hidden_states is saved separately so shared experts get [S, hidden_size] while routed experts get the transformed [S, moe_latent_size].
Returns (possibly transformed) hidden states and the input for shared experts (or None if there are no shared experts).
apply_routed_output_transform(fused_output, residual=None)
¶
Apply transform to routed expert output (e.g., latent to full dim).
Used by latent MoE models (e.g., NemotronH) where routed experts
operate in a compressed latent space and need projection back to
the full hidden dimension before combining with shared expert output.
residual is passed only to transforms that set accumulates_residual.
forward(hidden_states, router_logits, input_ids=None, shared_experts_input=None)
¶
Invoke the fused moe layer.
Input: - hidden_states - router_logits
Output: - The new hidden_states.
Calling sequence - forward - self._forward_entry (_moe_forward or _moe_forward_shared custom op) - _forward_impl
Note: The existence of _moe_forward and _moe_forward_shared custom ops are due to the following reason: 1. pytorch cannot handle union types in custom op signatures so _moe_forward and _moe_forward_shared must be split.
process_weights_after_loading()
¶
Fuse router and shared expert gate weights.
Cannot be done at init because gate weights are loaded after module construction (via weight_loader). The loader runs this after every load or reload, refilling the fused tensor in place so captured CUDA graphs see updated gates.
set_eplb_state(moe_layer_idx, expert_load_view, logical_to_physical_map, logical_replica_count)
¶
Register the EPLB state in this layer.
This is used later in forward pass, where we get the expert mapping
and record the load metrics in expert_load_view.