Skip to content

vllm.model_executor.layers.fused_moe.deep_gemm_mega_moe

DeepGEMM MegaMoE experts: fused EP dispatch, expert GEMMs and combine.

Each MEGA_MOE_* format is one row of the feature matrix. MX weights and activations carry one UE8M0 scale per 1x32 block (MEGA_MOE_MXFP4_SM90 activations use one FP32 scale per 1x128 block). NVFP4 weights carry one E4M3 scale per 1x16 block plus a per-expert FP32 global scale; NVFP4 dispatch scales each token by its own FP32 scale. MEGA_MOE_FP8_BLOCK_SM90 weights carry one FP32 scale per 128x128 block.

Classes:

Functions:

DeepGemmMegaMoEExperts

Bases: Module

Routed experts, and optionally the shared expert, as one MegaMoE launch.

Parameters are registered in the kernel layout and every checkpoint shard is written straight into it, so loading (and reloading) needs no post-load transform, except the SM90 requantization of a fused FP8 shared expert and the SM90 layout of FP32 block scales. Whether the shared expert runs inside the launch is decided here, before any weight loads; a fused layer owns the shared-expert weights and the caller drops its shared MLP.

NVFP4 experts also take per-expert weight_scale_2 (w1 and w3 must match: one L1 scale per expert) and w2 input_scale; the kernel quantizes every L2 input with the max of the latter over all loaded experts. Dispatch uses per-token scales, so w1/w3 input_scale are not loaded.

Parameters:

  • num_experts

    (int) –

    Physical experts across EP ranks, redundant included.

  • activation

    (MegaMoEActivation) –

    Activation fused into the L1 epilogue.

  • weight_format

    (MegaMoEWeightFormat) –

    Routed-expert precision, a row of the feature matrix.

  • shared_experts

    (Module | None, default: None ) –

    Shared MLP to fuse if it is unsharded and in one of the weight_format.shared precisions; None never fuses.

  • max_num_tokens

    (int | None, default: None ) –

    Most tokens one rank passes to forward; the symmetric buffer is sized for it. Defaults to max_num_batched_tokens. A caller that hands this layer only its sequence-parallel shard of the batch can pass the shard size.

  • l2_input_scale

    (float | None, default: None ) –

    Fixed w2 input_scale that replaces the checkpoint's, for QAT recipes that quantize the L2 input with a constant scale.

Methods:

  • forward –

    Run the routed and shared experts on this rank's tokens.

  • get_block_m –

    BLOCK_M the MegaMoE scheduler runs num_tokens local tokens with.

  • load_shared_expert_weight –

    Load one shared-expert checkpoint tensor; return the parameter name.

  • load_weights –

    Load per-expert {e}.{w1,w2,w3}.*, fused gate_up_proj/down_proj

  • process_weights_after_loading –

    Requantize newly loaded shared-expert shards to SM90 scales.

_checkpoint_tensor(suffix, shard_id, tensor)

Map compressed-tensors suffixes and quantization scales to the ModelOpt names and dequantization scales the parameters hold.

_create_weights()

Register the routed and fused shared-expert parameters.

Routed parameters are indexed by local expert on dim 0, which EPLB moves.

_expert_shards(name, weight)

(expert_id, shard_id, param suffix, tensor) per shard in weight.

_fusable_shared_experts(shared_experts)

How many shared experts the launch fuses, and in which precision.

_kernel_layout_shard(dst, src, shard_id, is_scale) staticmethod

Pair a checkpoint shard with its destination in the MegaMoE layout.

_load_weight(dst, src, shard_id, is_scale)

Write checkpoint shard src into dst in the kernel layout.

dst is one routed expert's slice or a shared-expert parameter.

_map_global_expert_id(expert_id)

Local slots that logical expert expert_id occupies on this rank.

_mean_num_tokens(num_tokens)

Mean tokens per EP rank of this step, the same on every rank.

CUDA graph steps pad every DP rank to one count, so it is num_tokens.

_pack_scale(sf, rows, cols) staticmethod

Expand block scales to the kernel granularity, four per int32 along K.

_record_global_scale(weight_name, scale, shard_id, expert_id)

Track NVFP4 global scales the kernel takes one of per launch or expert.

Every rank sees the scales of all experts, so ranks agree and EPLB can move experts.

_sm90_fp4_shard(src, is_scale) staticmethod

Repack a routed FP4 shard for the SM90 kernel's dequantization.

Raises:

  • AssertionError –

    If a row's UE8M0 scales are out of the kernel's range.

forward(hidden_states, topk_weights, topk_ids, shared_experts=None)

Run the routed and shared experts on this rank's tokens.

Parameters:

  • hidden_states

    (Tensor) –

    BF16 [num_tokens, hidden_size] activations.

  • topk_weights

    (Tensor) –

    [num_tokens, top_k] routing weights.

  • topk_ids

    (Tensor) –

    [num_tokens, top_k] logical expert ids.

  • shared_experts

    (Module | None, default: None ) –

    Unfused shared MLP, run on hidden_states and added to the output. Must be None when this layer fused the shared expert at construction.

Returns:

  • Tensor –

    BF16 [num_tokens, hidden_size] routed plus shared expert output.

get_block_m(num_tokens)

BLOCK_M the MegaMoE scheduler runs num_tokens local tokens with.

load_shared_expert_weight(name, loaded_weight)

Load one shared-expert checkpoint tensor; return the parameter name.

load_weights(weights)

Load per-expert {e}.{w1,w2,w3}.*, fused gate_up_proj/down_proj (or w13/w2) and shared_experts.* tensors.

process_weights_after_loading()

Requantize newly loaded shared-expert shards to SM90 scales.

Requantization is row-local and leaves requantized rows unchanged, so a reloaded shard is exact when both its weight and scale arrived.

Raises:

  • ValueError –

    If a shard's weight or scale loaded without the other.

MegaMoEActivation dataclass

Gated activation fused into the MegaMoE L1 epilogue.

swiglu computes gate * sigmoid(alpha * gate) * (up + beta) after clamping gate from above and up symmetrically to clamp; this covers plain, DeepSeek-V4 (clamp) and GPT-OSS style (alpha/beta) experts. situ is Kimi K3's SiTU with gate tanh scale alpha and linear tanh scale beta; beta=0 leaves the linear branch unscaled.

MegaMoEWeightFormat dataclass

One routed-expert precision of the MegaMoE feature matrix.

Attributes:

  • uses_nvfp4_api (bool) –

    nvfp4_mega_moe has its own symmetric buffer, BLOCK_M and scales.

uses_nvfp4_api property

nvfp4_mega_moe has its own symmetric buffer, BLOCK_M and scales.

MegaMoEWeightSpec dataclass

Storage of one MegaMoE weight in the kernel layout.

Attributes:

has_global_scale property

E4M3 block scales pair with an FP32 per-tensor scale.

_matches_weight_spec(linear, spec)

Whether a checkpoint linear can be written into a spec weight.

check_deep_gemm_mega_moe_supported(device, hidden_size, intermediate_size, weight_format)

Raise unless DeepGEMM MegaMoE can run on device with these shapes.

Raises: