Skip to content
KernelIndex
Search⌘K

submission 645776

phoenixdna · python · License unknown

Use it

Vendorable · source mirrored · license unknownView source →

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

submission.py
curl "https://kernelindex.com/api/v1/implementations/kernelbot-amd-mixed-mla-645776?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
92.3µs
#429 of 766
2026-03-27

Reported · How evidence levels are derived →

Source and license

sourceavailable
revision digestsha256:105e13e01745c33f6a1d7a6b6cdf553e35fba42e7e997f58aab8cd817d16a4c5
license declaredunknown
license concludedunknown
authorsphoenixdna
imported2026-08-26

Kernel source

submission.py170 lines
import math
import torch

from task import input_t, output_t


NUM_HEADS = 16
NUM_KV_HEADS = 1
QK_HEAD_DIM = 576
V_HEAD_DIM = 512
SM_SCALE = 1.0 / math.sqrt(QK_HEAD_DIM)
PAGE_SIZE = 1
NUM_KV_SPLITS = 32

_RUNTIME = None
_META_CACHE = {}


def _ensure_runtime():
    global _RUNTIME
    if _RUNTIME is not None:
        return _RUNTIME

    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

    fp8_info = torch.finfo(aiter_dtypes.fp8)
    _RUNTIME = (
        mla_decode_fwd,
        aiter_dtypes.fp8,
        fp8_info.min,
        fp8_info.max,
        get_mla_metadata_info_v1,
        get_mla_metadata_v1,
    )
    return _RUNTIME


def _quantize_fp8(q: torch.Tensor, fp8_dtype: torch.dtype, fp8_min: float, fp8_max: float):
    amax = q.abs().amax().clamp(min=1e-12)
    scale = (amax / fp8_max).to(torch.float32).view(1)
    q_fp8 = (q / scale).clamp(min=fp8_min, max=fp8_max).to(fp8_dtype)
    return q_fp8, scale


def _get_cached_meta(
    batch_size: int,
    kv_seq_len: int,
    q_dtype: torch.dtype,
    kv_dtype: torch.dtype,
    qo_indptr: torch.Tensor,
    kv_indptr: torch.Tensor,
    get_mla_metadata_info_v1,
    get_mla_metadata_v1,
):
    key = (batch_size, kv_seq_len, str(q_dtype), str(kv_dtype), NUM_KV_SPLITS, qo_indptr.device.index)
    cached = _META_CACHE.get(key)
    if cached is not None:
        return cached

    max_q_len = 1
    kv_last_page_len = (kv_indptr[1:] - kv_indptr[:-1]).to(torch.int32)
    info = get_mla_metadata_info_v1(
        batch_size,
        max_q_len,
        NUM_HEADS,
        q_dtype,
        kv_dtype,
        is_sparse=False,
        fast_mode=False,
        num_kv_splits=NUM_KV_SPLITS,
        intra_batch_mode=True,
    )
    work = [torch.empty(shape, dtype=dtype, device="cuda") for shape, dtype in info]
    (
        work_metadata,
        work_indptr,
        work_info_set,
        reduce_indptr,
        reduce_final_map,
        reduce_partial_map,
    ) = work

    get_mla_metadata_v1(
        qo_indptr,
        kv_indptr,
        kv_last_page_len,
        NUM_HEADS // NUM_KV_HEADS,
        NUM_KV_HEADS,
        True,
        work_metadata,
        work_info_set,
        work_indptr,
        reduce_indptr,
        reduce_final_map,
        reduce_partial_map,
        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,
    )

    cached = {
        "kv_indices": torch.arange(batch_size * kv_seq_len, dtype=torch.int32, device="cuda"),
        "kv_last_page_len": kv_last_page_len,
        "work": {
            "work_meta_data": work_metadata,
            "work_indptr": work_indptr,
            "work_info_set": work_info_set,
            "reduce_indptr": reduce_indptr,
            "reduce_final_map": reduce_final_map,
            "reduce_partial_map": reduce_partial_map,
        },
    }
    _META_CACHE[key] = cached
    return cached


def custom_kernel(data: input_t) -> output_t:
    (
        mla_decode_fwd,
        fp8_dtype,
        fp8_min,
        fp8_max,
        get_mla_metadata_info_v1,
        get_mla_metadata_v1,
    ) = _ensure_runtime()
    q, kv_data, qo_indptr, kv_indptr, config = data

    q_input, q_scale = _quantize_fp8(q, fp8_dtype, fp8_min, fp8_max)
    kv_input, kv_scale = kv_data["fp8"]
    meta = _get_cached_meta(
        config["batch_size"],
        config["kv_seq_len"],
        q_input.dtype,
        kv_input.dtype,
        qo_indptr,
        kv_indptr,
        get_mla_metadata_info_v1,
        get_mla_metadata_v1,
    )

    out = torch.empty((q.shape[0], NUM_HEADS, V_HEAD_DIM), dtype=torch.bfloat16, device="cuda")
    mla_decode_fwd(
        q_input,
        kv_input.view(kv_input.shape[0], PAGE_SIZE, NUM_KV_HEADS, kv_input.shape[-1]),
        out,
        qo_indptr,
        kv_indptr,
        meta["kv_indices"],
        meta["kv_last_page_len"],
        1,
        page_size=PAGE_SIZE,
        nhead_kv=NUM_KV_HEADS,
        sm_scale=SM_SCALE,
        logit_cap=0.0,
        num_kv_splits=NUM_KV_SPLITS,
        q_scale=q_scale,
        kv_scale=kv_scale,
        intra_batch_mode=True,
        **meta["work"],
    )
    return out
scrolls · 170 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