Skip to content

vllm.models.deepseek_v41.amd.akr_moe

DeepSeek-V4.1 MoE block on amd_kernels: routes router, draft_router and shared_expert.

router / draft_router: gate GEMM, sqrt-softplus, correction bias, top-k, renormalize and routed scaling in one entry, for the target layers (384 experts, top 6) and the DSpark draft layers (128 experts, top 3). shared_expert: the shared expert's gate_up and clamped SiLU-mul as MXFP8 for the mhc_ffn epilogue (shared down + routed add + FFN post-mix); SHARED_PLAN picks per static row count which gate_up runs. Standalone the stock chain on the FFN seam's MXFP8 copy wins at every row count, so the route declines there. The stock linears' preshuffled weights are read as they are; only their scales get a copy (about 0.5 MB per layer).

While any of these routes is on, a MegaMoE layer runs its shared expert after the routed experts on the same stream: beside MegaMoE's persistent, spin-waiting stage-1 kernel the overlapped gate_up GEMM stalled both for 1.3-13.7 ms per occurrence on DEP8 decode.

Classes:

  • Router –

    DeepseekV4MoE.akr: the router route of one MoE layer (gate None when

  • SharedExpert –

    DeepseekV4MLP.akr: the shared-expert route of one MoE layer. The down

Functions:

  • forward_parts –

    The routed output and the shared expert's MXFP8 activation, for a caller

  • shared_takes_codes –

    Whether the shared expert reads the FFN seam's MXFP8 codes at m rows.

  • wants_x_quant –

    Whether the FFN seam should also emit MXFP8 codes for moe at m rows:

Router

DeepseekV4MoE.akr: the router route of one MoE layer (gate None when the layer's router is not eligible: only the serial shared expert).

Methods:

  • route –

    fp32 top-k weights and int32 expert ids, or None for the stock router.

Source code in vllm/models/deepseek_v41/amd/akr_moe.py
class Router:
    """`DeepseekV4MoE.akr`: the router route of one MoE layer (`gate` None when
    the layer's router is not eligible: only the serial shared expert)."""

    def __init__(self, route: str, gate: torch.Tensor | None, moe) -> None:
        self.route_name = route
        self.gate = gate
        if gate is not None:
            self.bias = moe.gate.e_score_correction_bias.data
            self.scale = float(moe.routed_scaling_factor)
            self.top_k = moe.n_activated_experts

    def route(self, moe, x: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor] | None:
        """fp32 top-k weights and int32 expert ids, or None for the stock router."""
        if (
            self.gate is None
            or x.dtype != torch.bfloat16
            or x.dim() != 2
            or x.stride(1) != 1
            or not akr_config.serves(self.route_name, x.shape[0])
        ):
            return None
        from vllm.model_executor.layers.fused_moe.router.base_router import (
            get_moe_padding_mask,
        )

        pad = get_moe_padding_mask(x.shape[0])
        return akr_kernels.router_topk(
            x,
            self.gate,
            self.bias,
            self.scale,
            self.top_k,
            None if pad is None else pad.contiguous(),
        )

route(moe, x)

fp32 top-k weights and int32 expert ids, or None for the stock router.

Source code in vllm/models/deepseek_v41/amd/akr_moe.py
def route(self, moe, x: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor] | None:
    """fp32 top-k weights and int32 expert ids, or None for the stock router."""
    if (
        self.gate is None
        or x.dtype != torch.bfloat16
        or x.dim() != 2
        or x.stride(1) != 1
        or not akr_config.serves(self.route_name, x.shape[0])
    ):
        return None
    from vllm.model_executor.layers.fused_moe.router.base_router import (
        get_moe_padding_mask,
    )

    pad = get_moe_padding_mask(x.shape[0])
    return akr_kernels.router_topk(
        x,
        self.gate,
        self.bias,
        self.scale,
        self.top_k,
        None if pad is None else pad.contiguous(),
    )

SharedExpert

DeepseekV4MLP.akr: the shared-expert route of one MoE layer. The down projection stays the stock linear, on the MXFP8 activation.

Methods:

  • activation –

    gate_up and the clamped SiLU-mul as MXFP8, or None for the stock

  • chain –

    activation and the stock down projection (the unfused form of the

  • forward –

    None: standalone, the stock chain on the seam's MXFP8 copy wins or

Source code in vllm/models/deepseek_v41/amd/akr_moe.py
class SharedExpert:
    """`DeepseekV4MLP.akr`: the shared-expert route of one MoE layer. The down
    projection stays the stock linear, on the MXFP8 activation."""

    def __init__(self, mlp, gate_up: akr_kernels.Mxfp8Weight) -> None:
        self.gate_up = gate_up
        self.limit = mlp.act_fn.swiglu_limit

    def activation(
        self, mlp, x: torch.Tensor, x_quant: QuantizedActivation | None
    ) -> QuantizedActivation | None:
        """gate_up and the clamped SiLU-mul as MXFP8, or None for the stock
        chain."""
        m = x.shape[0]
        if x.dtype != torch.bfloat16 or x.dim() != 2 or not x.is_contiguous():
            return None
        plan = shared_plan(m) if akr_config.serves("shared_expert", m) else None
        if plan is None:
            return None
        out = None
        if plan == "swiglu":
            codes_in = x_quant is not None and m <= SWIGLU_CODES_MAX_ROWS
            out = akr_kernels.mxfp8_gate_up_swiglu(
                x_quant if codes_in else x, self.gate_up
            )
        if out is None:
            gate_up, _ = mlp.gate_up_proj(x if x_quant is None else x_quant)
            out = akr_kernels.clamp_silu_mul_mxfp8(gate_up, self.limit)
        codes, scale = out
        return akr_config.quant_activation(codes, scale, x.dtype)

    def forward(
        self, mlp, x: torch.Tensor, x_quant: QuantizedActivation | None
    ) -> torch.Tensor | None:
        """None: standalone, the stock chain on the seam's MXFP8 copy wins or
        ties (within 0.2 us) at every row count from 16 to 768 on MI355X
        (dev1133), by up to 3.7 us."""
        return None

    def chain(
        self, mlp, x: torch.Tensor, x_quant: QuantizedActivation | None
    ) -> torch.Tensor | None:
        """`activation` and the stock down projection (the unfused form of the
        `mhc_ffn` epilogue's input; tests and warm-up)."""
        act = self.activation(mlp, x, x_quant)
        if act is None:
            return None
        out, _ = mlp.down_proj(act)
        return out

    def warmup(self, mlp) -> None:
        dev = self.gate_up.weight.device
        hidden = self.gate_up.k
        akr_kernels.warmup_clamp_silu_mul(self.gate_up.n // 2, dev)
        for bound, _ in SHARED_PLAN:
            x = torch.zeros(bound, hidden, dtype=torch.bfloat16, device=dev)
            self.chain(mlp, x, None)
            if bound <= SWIGLU_CODES_MAX_ROWS:
                codes = torch.zeros(
                    bound, hidden, dtype=torch.float8_e4m3fn, device=dev
                )
                scale = torch.full(
                    (bound, hidden // 32), 127, dtype=torch.uint8, device=dev
                )
                self.chain(mlp, x, akr_config.quant_activation(codes, scale, x.dtype))

activation(mlp, x, x_quant)

gate_up and the clamped SiLU-mul as MXFP8, or None for the stock chain.

Source code in vllm/models/deepseek_v41/amd/akr_moe.py
def activation(
    self, mlp, x: torch.Tensor, x_quant: QuantizedActivation | None
) -> QuantizedActivation | None:
    """gate_up and the clamped SiLU-mul as MXFP8, or None for the stock
    chain."""
    m = x.shape[0]
    if x.dtype != torch.bfloat16 or x.dim() != 2 or not x.is_contiguous():
        return None
    plan = shared_plan(m) if akr_config.serves("shared_expert", m) else None
    if plan is None:
        return None
    out = None
    if plan == "swiglu":
        codes_in = x_quant is not None and m <= SWIGLU_CODES_MAX_ROWS
        out = akr_kernels.mxfp8_gate_up_swiglu(
            x_quant if codes_in else x, self.gate_up
        )
    if out is None:
        gate_up, _ = mlp.gate_up_proj(x if x_quant is None else x_quant)
        out = akr_kernels.clamp_silu_mul_mxfp8(gate_up, self.limit)
    codes, scale = out
    return akr_config.quant_activation(codes, scale, x.dtype)

chain(mlp, x, x_quant)

activation and the stock down projection (the unfused form of the mhc_ffn epilogue's input; tests and warm-up).

Source code in vllm/models/deepseek_v41/amd/akr_moe.py
def chain(
    self, mlp, x: torch.Tensor, x_quant: QuantizedActivation | None
) -> torch.Tensor | None:
    """`activation` and the stock down projection (the unfused form of the
    `mhc_ffn` epilogue's input; tests and warm-up)."""
    act = self.activation(mlp, x, x_quant)
    if act is None:
        return None
    out, _ = mlp.down_proj(act)
    return out

forward(mlp, x, x_quant)

None: standalone, the stock chain on the seam's MXFP8 copy wins or ties (within 0.2 us) at every row count from 16 to 768 on MI355X (dev1133), by up to 3.7 us.

Source code in vllm/models/deepseek_v41/amd/akr_moe.py
def forward(
    self, mlp, x: torch.Tensor, x_quant: QuantizedActivation | None
) -> torch.Tensor | None:
    """None: standalone, the stock chain on the seam's MXFP8 copy wins or
    ties (within 0.2 us) at every row count from 16 to 768 on MI355X
    (dev1133), by up to 3.7 us."""
    return None

_stock_takes_codes(mlp)

Whether the stock shared expert's gate_up reads an attached MXFP8 copy.

Source code in vllm/models/deepseek_v41/amd/akr_moe.py
def _stock_takes_codes(mlp) -> bool:
    """Whether the stock shared expert's gate_up reads an attached MXFP8 copy."""
    takes = getattr(mlp, "_akr_gate_up_codes", None)
    if takes is None:
        takes = mlp._gateup_scale is None and akr_config.takes_mxfp8_codes(
            mlp.gate_up_proj
        )
        mlp._akr_gate_up_codes = takes
    return takes

forward_parts(moe, x, input_ids, x_quant)

The routed output and the shared expert's MXFP8 activation, for a caller that runs the shared down projection in its own epilogue. None, having done nothing, where the shared-expert route does not serve.

Source code in vllm/models/deepseek_v41/amd/akr_moe.py
def forward_parts(
    moe,
    x: torch.Tensor,
    input_ids: torch.Tensor | None,
    x_quant: QuantizedActivation | None,
) -> tuple[torch.Tensor, QuantizedActivation] | None:
    """The routed output and the shared expert's MXFP8 activation, for a caller
    that runs the shared down projection in its own epilogue. None, having done
    nothing, where the shared-expert route does not serve."""
    shared = moe.shared_experts
    if not moe.use_mega_moe or shared is None or shared.akr is None:
        return None
    act = shared.akr.activation(shared, x, x_quant)
    if act is None:
        return None
    return moe._forward_mega_moe_routed(x, input_ids, x_quant), act

shared_takes_codes(m)

Whether the shared expert reads the FFN seam's MXFP8 codes at m rows.

Source code in vllm/models/deepseek_v41/amd/akr_moe.py
def shared_takes_codes(m: int) -> bool:
    """Whether the shared expert reads the FFN seam's MXFP8 codes at `m` rows."""
    if not shared_serves(m):
        return False
    plan = shared_plan(m)
    return plan == "stock" or (plan == "swiglu" and m <= SWIGLU_CODES_MAX_ROWS)

wants_x_quant(moe, m)

Whether the FFN seam should also emit MXFP8 codes for moe at m rows: the fused EP8 experts read them, and so does the shared expert where its gate_up runs on codes (the route's plan, or the stock linear where the route does not serve).

Source code in vllm/models/deepseek_v41/amd/akr_moe.py
def wants_x_quant(moe, m: int) -> bool:
    """Whether the FFN seam should also emit MXFP8 codes for `moe` at `m` rows:
    the fused EP8 experts read them, and so does the shared expert where its
    gate_up runs on codes (the route's plan, or the stock linear where the
    route does not serve)."""
    fused = getattr(getattr(moe, "experts", None), "akr", None)
    if fused is not None and fused.serves(m):
        return True
    shared = getattr(moe, "shared_experts", None)
    if shared is None:
        return False
    if getattr(shared, "akr", None) is not None and shared_serves(m):
        return shared_takes_codes(m)
    return _stock_takes_codes(shared)