Skip to content
KernelIndex
Search⌘K

submission 655730

SwordHoly · python · License unknown

Use it

Vendorable · source mirrored · license unknownView source →

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

v1_cache_meta_v3.py
curl "https://kernelindex.com/api/v1/implementations/kernelbot-amd-mixed-mla-655730?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
151.6µs
#527 of 766
2026-03-28

Reported · How evidence levels are derived →

Source and license

sourceavailable
revision digestsha256:820255adfcf6a499a322005b3b5fb9a1bd9d8105bf9645af238e0c34574b078f
license declaredunknown
license concludedunknown
authorsSwordHoly
imported2026-08-26

Kernel source

v1_cache_meta_v3.py96 lines
"""MLA v25: Refined hybrid dispatch — a8w8 only for bs>=64 AND kv>=8192."""
import torch
from task import input_t, output_t
from aiter.mla import mla_decode_fwd
from aiter import dtypes as aiter_dtypes
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
NUM_KV_SPLITS = 32
FP8_DTYPE = aiter_dtypes.fp8

_meta_cache = {}
_indices_cache = {}


def _get_metadata(batch_size, max_q_len, nq, nkv, q_dtype, kv_dtype,
                  qo_indptr, kv_indptr, kv_last_page_len, num_kv_splits):
    key = (batch_size, int(kv_indptr[-1].item()), num_kv_splits, q_dtype, kv_dtype)
    if key not in _meta_cache:
        info = get_mla_metadata_info_v1(
            batch_size, max_q_len, nq, q_dtype, kv_dtype,
            is_sparse=False, fast_mode=False,
            num_kv_splits=num_kv_splits, intra_batch_mode=True,
        )
        work = [torch.empty(s, dtype=t, device="cuda") for s, t in info]
        (wm, wi, wis, ri, rfm, rpm) = work
        get_mla_metadata_v1(
            qo_indptr, kv_indptr, kv_last_page_len,
            nq // nkv, nkv, True,
            wm, wis, wi, ri, rfm, rpm,
            page_size=PAGE_SIZE, kv_granularity=max(PAGE_SIZE, 16),
            max_seqlen_qo=max_q_len, uni_seqlen_qo=max_q_len,
            fast_mode=False, max_split_per_batch=num_kv_splits,
            intra_batch_mode=True, dtype_q=q_dtype, dtype_kv=kv_dtype,
        )
        _meta_cache[key] = {
            "work_meta_data": wm, "work_indptr": wi, "work_info_set": wis,
            "reduce_indptr": ri, "reduce_final_map": rfm, "reduce_partial_map": rpm,
        }
    return _meta_cache[key]


def _get_kv_indices(n):
    if n not in _indices_cache:
        _indices_cache[n] = torch.arange(n, dtype=torch.int32, device="cuda")
    return _indices_cache[n]


def custom_kernel(data: input_t) -> output_t:
    q, kv_data, qo_indptr, kv_indptr, config = data
    bs = config["batch_size"]
    nq, nkv = config["num_heads"], config["num_kv_heads"]
    dq, dv = config["qk_head_dim"], config["v_head_dim"]
    q_seq_len = config["q_seq_len"]

    kv_fp8, kv_scale = kv_data["fp8"]
    total_kv = int(kv_indptr[-1].item())
    kv_last = (kv_indptr[1:] - kv_indptr[:-1]).to(torch.int32)

    # a8w8 only helps when: large batch AND large kv_len
    # bs=64/kv=8k: a8w8 ~224us vs a16w8 ~228us (small win)
    # bs=256/kv=8k: a8w8 ~368us vs a16w8 ~679us (huge win)
    # bs>=64/kv=1k: a8w8 WORSE (Q quant overhead dominates)
    kv_len = total_kv // max(bs, 1)
    use_a8w8 = (bs >= 64) and (kv_len >= 4096)

    if use_a8w8:
        q_cont = q.contiguous()
        finfo = torch.finfo(FP8_DTYPE)
        amax = q_cont.abs().amax().clamp(min=1e-12)
        q_scale_val = (amax / finfo.max).to(torch.float32).reshape(1)
        q_input = (q_cont / q_scale_val).clamp(min=finfo.min, max=finfo.max).to(FP8_DTYPE)
    else:
        q_input = q.contiguous()
        q_scale_val = None

    meta = _get_metadata(bs, q_seq_len, nq, nkv, q_input.dtype, kv_fp8.dtype,
                         qo_indptr, kv_indptr, kv_last, NUM_KV_SPLITS)
    kv_idx = _get_kv_indices(total_kv)
    kv_4d = kv_fp8.view(kv_fp8.shape[0], PAGE_SIZE, nkv, kv_fp8.shape[-1])

    o = torch.empty((q.shape[0], nq, dv), dtype=torch.bfloat16, device="cuda")
    mla_decode_fwd(
        q_input.view(-1, nq, dq), kv_4d, o,
        qo_indptr, kv_indptr, kv_idx, kv_last, q_seq_len,
        page_size=PAGE_SIZE, nhead_kv=nkv, sm_scale=SM_SCALE,
        logit_cap=0.0, num_kv_splits=NUM_KV_SPLITS,
        q_scale=q_scale_val, kv_scale=kv_scale, intra_batch_mode=True, **meta,
    )
    return o
scrolls · 96 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