Skip to content

vllm.model_executor.layers.fused_moe.router.gate_linear

Classes:

  • GateLinear –

    MoE gate linear layer with multi-tier GEMM dispatch:

Functions:

GateLinear

Bases: ReplicatedLinear

MoE gate linear layer with multi-tier GEMM dispatch:

  1. cuteDSL ll_bf16_gemm (SM90+, M<=16, bf16 in, fp32 out, any shape w/ K divisible by 8)
  2. fp32 specialized kernel (bf16/fp32 in, fp32 out; SM90+ model-specific shapes and batch limits, gfx950 any shape with M<=32)
  3. cuteDSL ll_fp32w_gemm (SM90+, M<=32, bf16/fp32 in, fp32 out, any shape)
  4. bf16x3 CuteDSL kernel (SM90 or SM100, bf16 in, fp32 weight)
  5. ROCm bf16x3 router GEMM (gfx950, bf16 in, fp32 weight, fp32 out, M>=2048)
  6. cuBLAS bf16×bf16→fp32 (SM90+ + bf16 weight + fp32 out_dtype); gfx950 routes low M through the tier 2 kernel
  7. F.linear via ReplicatedLinear (ultimate fallback)

The out_dtype attribute is mutable and can be set after init (e.g. when the required dtype depends on the expert quantization method which is only known later).

A quant_config that actually quantizes the gate disables every specialized tier, leaving plain ReplicatedLinear behavior.

VLLM_BATCH_INVARIANT also disables tiers 1-5: they pick kernels and split-K from the token count and skip the batch-invariant linear path.

Methods:

  • set_out_dtype –

    Set output dtype for the router logits after init.

set_out_dtype(out_dtype)

Set output dtype for the router logits after init.

Useful when the required dtype depends on the expert quantization method which is only known after the gate is constructed.

_GateLinearMethod

Bases: UnquantizedLinearMethod

UnquantizedLinearMethod plus the ROCm bf16x3 weight split.

Building the split here rather than lazily in forward() charges it to the weights memory pool ahead of KV cache profiling, and rebuilds it on weight reload. The latter matters because an in-place param.data.copy_() leaves both data_ptr() and _version untouched, so a stale split cannot be detected from inside forward().

allow_rocm_bf16x3_router_gemm is final by the time this runs: the only caller of set_out_dtype does so during model construction.

_aiter_gemm_a16w32() cached

AITER's gemm_a16w32, or None when the installed AITER does not have it.

bf16x3_router_gemm_module() cached

The CuteDSL bf16x3 router GEMM module for this device (SM90 or SM100).

Only for code that Dynamo does not trace, such as custom op bodies: Dynamo ignores @cache and would trace the device query.

fp32_router_gemm_dispatch_impl(x, weight, allow_bf16x3_router_gemm)

Run the FP32 kernel for small batches, with shape-specific limits.

Larger SM90/SM100 BF16 batches use BF16x3; other inputs fall back to F.linear. This must be wrapped in a custom op because our torch.compile integration does not support runtime dispatching on num_tokens.

rocm_bf16x3_router_gemm_dispatch_impl(x, weight, weight_split)

Run the ROCm bf16x3 router GEMM, or fall back for small batches.

This must be a custom op because our torch.compile integration does not support runtime dispatching on num_tokens: models carrying fp32 router weights (MiniMax-M2, HunYuan-V3) call the gate from @support_torch_compile model code and vLLM drops all Dynamo guards, so a plain Python branch on x.shape[0] would be frozen at first trace.