Skip to content
KernelIndex
Search⌘K

submission 706216

guangxiangdebizi · python · License unknown

Use it

Vendorable · source mirrored · license unknownView source →

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

submission_exp2_v01.py
curl "https://kernelindex.com/api/v1/implementations/kernelbot-amd-mixed-mla-706216?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
91.4µs
#424 of 766
2026-04-03

Reported · How evidence levels are derived →

Source and license

sourceavailable
revision digestsha256:5f16719a5d4c5d8c9470bec0975ca5474375e008f1ac10d5dd052a3d999e3251
license declaredunknown
license concludedunknown
authorsguangxiangdebizi
imported2026-08-26

Kernel source

submission_exp2_v01.py207 lines
#!POPCORN leaderboard amd-mixed-mla
#!POPCORN gpu MI355X

"""Experiment 2 for AMD MLA decode: keep a8w8, trim host-side overhead."""

import torch
from task import input_t, output_t

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


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

_KV_INDICES_CACHE: dict[tuple[int, str], torch.Tensor] = {}
_DECODE_STATE_CACHE: dict[tuple[int, int, int, torch.dtype, torch.dtype, int, str], tuple[torch.Tensor, dict[str, torch.Tensor]]] = {}


def quantize_fp8(tensor: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
    finfo = torch.finfo(FP8_DTYPE)
    amax = tensor.abs().amax().clamp(min=1e-12)
    scale = amax / finfo.max
    fp8_tensor = (tensor / scale).clamp(min=finfo.min, max=finfo.max).to(FP8_DTYPE)
    return fp8_tensor, scale.to(torch.float32).reshape(1)


def choose_num_kv_splits(batch_size: int, kv_seq_len: int) -> int:
    splits = 8 if kv_seq_len <= 1024 else 16
    if batch_size >= 32:
        splits *= 2
    if batch_size >= 128:
        splits *= 2
    return min(splits, 64)


def get_kv_indices(total_kv_len: int, device: torch.device) -> torch.Tensor:
    key = (total_kv_len, str(device))
    kv_indices = _KV_INDICES_CACHE.get(key)
    if kv_indices is None:
        kv_indices = torch.arange(total_kv_len, dtype=torch.int32, device=device)
        _KV_INDICES_CACHE[key] = kv_indices
    return kv_indices


def make_mla_decode_metadata(
    batch_size: int,
    max_q_len: int,
    qo_indptr: torch.Tensor,
    kv_indptr: torch.Tensor,
    kv_last_page_len: torch.Tensor,
    q_dtype: torch.dtype,
    kv_dtype: torch.dtype,
    num_kv_splits: int,
):
    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,
    )

    return {
        "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,
    }


def get_decode_state(
    batch_size: int,
    q_seq_len: int,
    kv_seq_len: int,
    q_dtype: torch.dtype,
    kv_dtype: torch.dtype,
    qo_indptr: torch.Tensor,
    kv_indptr: torch.Tensor,
    device: torch.device,
) -> tuple[torch.Tensor, dict[str, torch.Tensor], int]:
    num_kv_splits = choose_num_kv_splits(batch_size, kv_seq_len)
    cache_key = (
        batch_size,
        q_seq_len,
        kv_seq_len,
        q_dtype,
        kv_dtype,
        num_kv_splits,
        str(device),
    )
    cached = _DECODE_STATE_CACHE.get(cache_key)
    if cached is not None:
        kv_last_page_len, meta = cached
        return kv_last_page_len, meta, num_kv_splits

    # This task only generates uniform decode segments, so the per-batch lengths are shape-derived.
    kv_last_page_len = torch.full((batch_size,), kv_seq_len, dtype=torch.int32, device=device)
    meta = make_mla_decode_metadata(
        batch_size,
        q_seq_len,
        qo_indptr,
        kv_indptr,
        kv_last_page_len,
        q_dtype,
        kv_dtype,
        num_kv_splits,
    )
    _DECODE_STATE_CACHE[cache_key] = (kv_last_page_len, meta)
    return kv_last_page_len, meta, num_kv_splits


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

    q_input, q_scale = quantize_fp8(q)
    kv_buffer_fp8, kv_scale = kv_data["fp8"]

    kv_last_page_len, meta, num_kv_splits = get_decode_state(
        config["batch_size"],
        config["q_seq_len"],
        config["kv_seq_len"],
        q_input.dtype,
        kv_buffer_fp8.dtype,
        qo_indptr,
        kv_indptr,
        q.device,
    )

    total_kv_len = config["batch_size"] * config["kv_seq_len"]
    kv_indices = get_kv_indices(total_kv_len, q.device)
    kv_buffer_4d = kv_buffer_fp8.view(
        kv_buffer_fp8.shape[0],
        PAGE_SIZE,
        NUM_KV_HEADS,
        kv_buffer_fp8.shape[-1],
    )
    out = torch.empty((q.shape[0], NUM_HEADS, V_HEAD_DIM), dtype=torch.bfloat16, device="cuda")

    mla_decode_fwd(
        q_input.view(-1, NUM_HEADS, QK_HEAD_DIM),
        kv_buffer_4d,
        out,
        qo_indptr,
        kv_indptr,
        kv_indices,
        kv_last_page_len,
        config["q_seq_len"],
        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,
    )
    return out
scrolls · 207 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