Skip to content
KernelIndex
Search⌘K

submission 674677

rujutafujuta · python · License unknown

Use it

Vendorable · source mirrored · license unknownView source →

No package. Vendor the mirrored source: 145 lines, June 9 Researcher Reciprocity License v1.0.

mixed-mla-v2.py
curl "https://kernelindex.com/api/v1/implementations/kernelbot-amd-mixed-mla-674677?include=source"
interfacepython
Compatibility
measured onAMD Instinct MI355X
declared hardwareAMD Instinct MI355X
architecturesgfx950
dtypesbf16, int32

Benchmark evidence

1 measurement across 1 GPU, fastest first.

Operation / workload
Hardware
Latency
Rank
Observed
AMD Instinct MI355X
94.0µs
#444 of 766
2026-03-30

Reported · How evidence levels are derived →

Source and license

sourceavailable
revision digestsha256:579acfe0f431fa9f270f0d2540e4a93e8b3fd291201ee6c22e93d79dc09bcd41
license declaredunknown
license concludedunknown
authorsrujutafujuta
imported2026-08-26

Kernel source

mixed-mla-v2.py145 lines
#!POPCORN leaderboard amd-mixed-mla
"""
MLA Decode v2 — CK fp8 + per-shape num_kv_splits tuning.

Key change from v1 (94.420 µs):
  v1 used num_kv_splits=32 for ALL shapes.
  The aiter efficiency model (CU utilization × KV efficiency) shows this is
  badly wrong for high-batch shapes where bs alone already fills all 256 CUs.

  eff(bs, kv, i) = (bs*i / ceil(bs*i/256)*256) * kv / (kv + 84.1*i)
  Optimal splits:
    bs=4,   kv=1024 → 16   (formula max in 1..16 range)
    bs=4,   kv=8192 → 32   (long KV: more splits recover CU util)
    bs=32,  kv=1024 →  8   (bs*8=256 exactly fills all CUs)
    bs=32,  kv=8192 →  8   (same CU fill, KV still large)
    bs=64,  kv=1024 →  4   (bs*4=256 exactly fills all CUs)
    bs=64,  kv=8192 →  4   (same CU fill, KV barely penalized)
    bs=256, kv=1024 →  1   (bs alone fills all CUs; any split adds overhead)
    bs=256, kv=8192 →  1   (same; eff peaks at 0.990 vs 0.276 for splits=32)
"""

import math
import torch
from task import input_t, output_t

from aiter import dtypes as aiter_dtypes
from aiter.mla import mla_decode_fwd
from aiter import get_mla_metadata_info_v1, get_mla_metadata_v1

NUM_HEADS    = 16
NUM_KV_HEADS = 1
QK_HEAD_DIM  = 576
V_HEAD_DIM   = 512
SM_SCALE     = 1.0 / (QK_HEAD_DIM ** 0.5)
PAGE_SIZE    = 1
FP8_DTYPE    = aiter_dtypes.fp8

# ── Per-shape optimal num_kv_splits ─────────────────────────────────────────
# Benchmark shapes are fixed; precompute optimal splits for each.
# Rule: maximize eff = (bs*i / ceil(bs*i/256)*256) * kv/(kv + 84.1*i)
_SPLITS_TABLE: dict[tuple[int, int], int] = {
    (4,   1024): 16,
    (4,   8192): 32,
    (32,  1024):  8,
    (32,  8192):  8,
    (64,  1024):  4,
    (64,  8192):  4,
    (256, 1024):  1,
    (256, 8192):  1,
}
_CU_NUM      = 256   # MI355X: 8 XCDs × 32 CUs
_KV_OVERHEAD = 84.1  # aiter efficiency model overhead constant


def _optimal_splits(batch_size: int, kv_seq_len: int, max_splits: int = 64) -> int:
    if (batch_size, kv_seq_len) in _SPLITS_TABLE:
        return _SPLITS_TABLE[(batch_size, kv_seq_len)]
    best_eff, best_i = -1.0, 1
    for i in range(1, max_splits + 1):
        total      = batch_size * i
        active     = math.ceil(total / _CU_NUM) * _CU_NUM
        cu_util    = total / active
        kv_eff     = kv_seq_len / (kv_seq_len + _KV_OVERHEAD * i)
        eff        = cu_util * kv_eff
        if eff > best_eff:
            best_eff = eff
            best_i   = i
    return best_i


_META_CACHE: dict = {}


def _quantize_fp8(t: torch.Tensor):
    fi    = torch.finfo(FP8_DTYPE)
    amax  = t.abs().amax().clamp(min=1e-12)
    scale = (amax / fi.max).to(torch.float32).reshape(1)
    return (t / scale).clamp(min=fi.min, max=fi.max).to(FP8_DTYPE), scale


def _get_meta(batch_size: int, kv_seq_len: int, q_dtype, kv_dtype, kv_indptr):
    nsp = _optimal_splits(batch_size, kv_seq_len)
    key = (batch_size, kv_seq_len, nsp, q_dtype, kv_dtype)
    if key in _META_CACHE:
        return _META_CACHE[key]

    total_kv = int(kv_indptr[-1].item())
    kv_i     = torch.arange(total_kv, dtype=torch.int32, device="cuda")
    kv_lpl   = (kv_indptr[1:] - kv_indptr[:-1]).to(torch.int32)
    kv_ip    = kv_indptr.clone()
    qo_ip    = torch.arange(0, batch_size + 1, dtype=torch.int32, device="cuda")

    info = get_mla_metadata_info_v1(
        batch_size, 1, NUM_HEADS, q_dtype, kv_dtype,
        is_sparse=False, fast_mode=True,
        num_kv_splits=nsp, intra_batch_mode=True,
    )
    work = [torch.empty(s, dtype=t, device="cuda") for s, t in info]
    wm, wi, wis, ri, rf, rp = work

    get_mla_metadata_v1(
        qo_ip, kv_ip, kv_lpl,
        NUM_HEADS // NUM_KV_HEADS, NUM_KV_HEADS, True,
        wm, wis, wi, ri, rf, rp,
        page_size=PAGE_SIZE, kv_granularity=max(PAGE_SIZE, 16),
        max_seqlen_qo=1, uni_seqlen_qo=1, fast_mode=True,
        max_split_per_batch=nsp, intra_batch_mode=True,
        dtype_q=q_dtype, dtype_kv=kv_dtype,
    )

    entry = dict(
        qo_indptr=qo_ip, kv_indptr=kv_ip, kv_indices=kv_i,
        kv_last_page_len=kv_lpl, num_kv_splits=nsp,
        work_meta_data=wm, work_indptr=wi, work_info_set=wis,
        reduce_indptr=ri, reduce_final_map=rf, reduce_partial_map=rp,
    )
    _META_CACHE[key] = entry
    return entry


def custom_kernel(data: input_t) -> output_t:
    q, kv_data, qo_indptr, kv_indptr, config = data

    q_fp8, q_scale = _quantize_fp8(q)
    batch_size = config["batch_size"]
    kv_seq_len = config["kv_seq_len"]

    kv_fp8, kv_scale = kv_data["fp8"]
    c = _get_meta(batch_size, kv_seq_len, q_fp8.dtype, kv_fp8.dtype, kv_indptr)

    kv4 = kv_fp8.view(kv_fp8.shape[0], PAGE_SIZE, NUM_KV_HEADS, kv_fp8.shape[-1])
    o   = torch.empty((batch_size, NUM_HEADS, V_HEAD_DIM), dtype=torch.bfloat16, device="cuda")

    mla_decode_fwd(
        q_fp8.view(-1, NUM_HEADS, QK_HEAD_DIM), kv4, o,
        c["qo_indptr"], c["kv_indptr"], c["kv_indices"], c["kv_last_page_len"],
        max_seqlen_q=1, page_size=PAGE_SIZE, nhead_kv=NUM_KV_HEADS,
        sm_scale=SM_SCALE, logit_cap=0.0, num_kv_splits=c["num_kv_splits"],
        q_scale=q_scale, kv_scale=kv_scale, intra_batch_mode=True,
        work_meta_data=c["work_meta_data"], work_indptr=c["work_indptr"],
        work_info_set=c["work_info_set"], reduce_indptr=c["reduce_indptr"],
        reduce_final_map=c["reduce_final_map"], reduce_partial_map=c["reduce_partial_map"],
    )
    return o
scrolls · 145 lines total

Source code from GPU Mode and the KernelBot dataset · June 9 Researcher Reciprocity License v1.0

Best evidence level for this revision: reported

JSON