Skip to content

vllm.models.deepseek_v41.amd.akr_mla

Routes sparse_mla and wo_a of DeepSeek-V4.1's attention on amd_kernels.

the decode rows of the AITER sparse MLA call (plain per-tensor fp8

KV, attention sink, ragged SWA list plus the ragged top-512 list), with the inverse RoPE and MXFP8 epilogue into the buffer wo_a reads.

wo_a: the grouped MXFP8 wo_a GEMM with an MXFP8 epilogue, so wo_b (stock, or the mHC post-mix chain) reads its codes without a quantize launch. It reads the AITER-preshuffled weight the stock linear keeps (no copy). One split-K workspace per device: default stream only. wo_b: at the rows where the dense entry's decode kernel on wo_a's bf16 output (quantizer launch + GEMM) beats AITER's wo_b on the epilogue's codes, wo_a writes bf16 and the entry runs wo_b (the stock weight, shuffled scales).

Functions:

  • sparse_mla_decode –

    True if the kernel wrote this call's MXFP8 output.

  • wo_a –

    wo_a's MXFP8 output for wo_b, or None, having launched nothing.

  • wo_a_wo_b –

    wo_b(wo_a(attn_out)) with wo_a's bf16 output through the dense entry

_cache_ok(cache)

An fp8 cache [blocks, block, 512] with a power-of-two block and 512-byte rows, the geometry the kernel reads.

Source code in vllm/models/deepseek_v41/amd/akr_mla.py
def _cache_ok(cache: torch.Tensor) -> bool:
    """An fp8 cache `[blocks, block, 512]` with a power-of-two block and
    512-byte rows, the geometry the kernel reads."""
    if cache.dim() != 3 or cache.dtype not in (torch.float8_e4m3fn, torch.uint8):
        return False
    _, block, dim = cache.shape
    return (
        block & (block - 1) == 0
        and cache.stride(2) == 1
        and cache.stride(1) == dim
        and cache.stride(0) % dim == 0
    )

sparse_mla_decode(layer, q, positions, swa_cache, swa_indptr, swa_indices, extra_cache, extra_indptr, extra_indices, output_mxfp8)

True if the kernel wrote this call's MXFP8 output.

Source code in vllm/models/deepseek_v41/amd/akr_mla.py
def sparse_mla_decode(
    layer,
    q: torch.Tensor,
    positions: torch.Tensor,
    swa_cache: torch.Tensor,
    swa_indptr: torch.Tensor,
    swa_indices: torch.Tensor,
    extra_cache: torch.Tensor | None,
    extra_indptr: torch.Tensor | None,
    extra_indices: torch.Tensor | None,
    output_mxfp8: tuple[torch.Tensor, torch.Tensor],
) -> bool:
    """True if the kernel wrote this call's MXFP8 output."""
    st = layer.akr
    if not st.mla or not akr_config.serves("sparse_mla", q.shape[0]):
        return False
    if st.mla_caches_ok is None:
        # the caches are bound once; checked on the first (eager) call
        st.mla_caches_ok = _cache_ok(swa_cache) and (
            extra_cache is None or _cache_ok(extra_cache)
        )
    if not st.mla_caches_ok:
        return False
    return akr_kernels.sparse_mla_decode(
        q,
        swa_cache,
        swa_indptr,
        swa_indices,
        extra_cache,
        extra_indptr,
        extra_indices,
        layer.attn_sink,
        layer.scale,
        layer._flashinfer_fp8_q_scale,
        layer._flashinfer_fp8_kv_scale,
        positions,
        layer.rotary_emb.cos_sin_cache,
        *output_mxfp8,
    )

wo_a(layer, attn_out)

wo_a's MXFP8 output for wo_b, or None, having launched nothing.

Source code in vllm/models/deepseek_v41/amd/akr_mla.py
def wo_a(layer, attn_out: QuantizedActivation) -> QuantizedActivation | None:
    """wo_a's MXFP8 output for wo_b, or None, having launched nothing."""
    x = attn_out.data
    if (
        not wo_a_serves(layer, x.shape[0])
        or not x.is_contiguous()
        or not attn_out.scale.is_contiguous()
    ):
        return None
    codes, scale = akr_kernels.mxfp8_grouped_gemm(attn_out, layer.akr.wo_a)
    return akr_config.quant_activation(codes, scale, attn_out.orig_dtype)

wo_a_wo_b(layer, attn_out)

wo_b(wo_a(attn_out)) with wo_a's bf16 output through the dense entry (its quantizer launch + GEMM), where that beats wo_a's MXFP8 epilogue + AITER's wo_b on the codes; None, having launched nothing, elsewhere.

Source code in vllm/models/deepseek_v41/amd/akr_mla.py
def wo_a_wo_b(layer, attn_out: QuantizedActivation) -> torch.Tensor | None:
    """wo_b(wo_a(attn_out)) with wo_a's bf16 output through the dense entry
    (its quantizer launch + GEMM), where that beats wo_a's MXFP8 epilogue +
    AITER's wo_b on the codes; None, having launched nothing, elsewhere."""
    st = layer.akr
    m = attn_out.data.shape[0]
    if (
        st.wo_b is None
        or st.want_codes
        or layer.gemm_rs is not None
        or not akr_config.serves("wo_b", m)
        or not wo_a_serves(layer, m)
        or not attn_out.data.is_contiguous()
        or not attn_out.scale.is_contiguous()
    ):
        return None
    z = akr_kernels.mxfp8_grouped_gemm_bf16(attn_out, st.wo_a)
    return akr_kernels.mxfp8_dense(z, st.wo_b)