Skip to content
KernelIndex
Search⌘K

submission 676865

DevSecSmith · python · License unknown

Use it

Vendorable · source mirrored · license unknownView source →

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

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

Reported · How evidence levels are derived →

Source and license

sourceavailable
revision digestsha256:529a33b9e0f4258b26fd2d27f6dec9ff8308b56df8d9b154e9659663048bc837
license declaredunknown
license concludedunknown
authorsDevSecSmith
imported2026-08-26

Kernel source

submission.py175 lines
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
FP8_DTYPE    = aiter_dtypes.fp8
_fp8_max     = torch.finfo(FP8_DTYPE).max
_fp8_min     = torch.finfo(FP8_DTYPE).min

_SPLITS_MAP = {
    (  4,  1024):  8,
    (  4,  8192): 32,
    ( 32,  1024): 16,
    ( 32,  8192): 32,
    ( 64,  1024): 16,
    ( 64,  8192): 16,
    (256,  1024):  8,
    (256,  8192):  8,
}
def _num_splits(batch, kv): return _SPLITS_MAP.get((batch, kv), 16)


# ── Fused fp8 quantisation via torch.compile ─────────────────────────────────
# Compiles abs+amax+scale+mul+clamp+cast into a single fused GPU kernel,
# eliminating 4+ separate kernel launches on every call.
@torch.compile(fullgraph=True, dynamic=False)
def _quantize_fp8_compiled(t: torch.Tensor):
    amax  = t.abs().amax().clamp(min=1e-12)
    scale = amax / _fp8_max
    fp8   = (t * (_fp8_max / amax)).clamp(_fp8_min, _fp8_max).to(FP8_DTYPE)
    return fp8, scale.to(torch.float32).reshape(1)

# Dispatch table: compile separately per shape so dynamic=False can specialise
_compiled_fns: dict = {}

def _quantize_fp8(t: torch.Tensor):
    key = t.shape
    if key not in _compiled_fns:
        # Force a new specialised compile for this shape
        @torch.compile(fullgraph=True, dynamic=False)
        def _fn(x):
            amax  = x.abs().amax().clamp(min=1e-12)
            scale = amax / _fp8_max
            fp8   = (x * (_fp8_max / amax)).clamp(_fp8_min, _fp8_max).to(FP8_DTYPE)
            return fp8, scale.to(torch.float32).reshape(1)
        _compiled_fns[key] = _fn
    return _compiled_fns[key](t)


_meta_cache:   dict = {}
_kv_idx_cache: dict = {}
_out_cache:    dict = {}
_kv_4d_cache:  dict = {}


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

def _get_out(total_q):
    if total_q not in _out_cache:
        _out_cache[total_q] = torch.empty(
            (total_q, NUM_HEADS, V_HEAD_DIM), dtype=torch.bfloat16, device="cuda")
    return _out_cache[total_q]

def _get_kv_4d(kv_fp8):
    ptr = kv_fp8.data_ptr()
    if ptr not in _kv_4d_cache:
        _kv_4d_cache[ptr] = kv_fp8.view(kv_fp8.shape[0], PAGE_SIZE, NUM_KV_HEADS, QK_HEAD_DIM)
    return _kv_4d_cache[ptr]


def _get_metadata(batch_size, q_seq_len, kv_seq_len,
                  q_dtype, kv_dtype, qo_indptr, kv_indptr, num_kv_splits):
    key = (batch_size, q_seq_len, kv_seq_len, str(q_dtype), str(kv_dtype), num_kv_splits)
    if key in _meta_cache:
        return _meta_cache[key]

    info = get_mla_metadata_info_v1(
        batch_size, q_seq_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(s, dtype=t, device="cuda") for s, t in info]
    (work_metadata, work_indptr, work_info_set,
     reduce_indptr, reduce_final_map, reduce_partial_map) = work

    kv_last_page_len = (kv_indptr[1:] - kv_indptr[:-1]).to(torch.int32)
    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=q_seq_len, uni_seqlen_qo=q_seq_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": 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,
        "kv_last_page_len": kv_last_page_len,
    }
    return _meta_cache[key]


def _prewarm():
    for batch, q_seq, kv_seq in [
        (4,1,1024),(4,1,8192),(32,1,1024),(32,1,8192),
        (64,1,1024),(64,1,8192),(256,1,1024),(256,1,8192)]:
        splits    = _num_splits(batch, kv_seq)
        qo_indptr = torch.arange(batch+1, dtype=torch.int32, device="cuda") * q_seq
        kv_indptr = torch.arange(batch+1, dtype=torch.int32, device="cuda") * kv_seq
        _get_kv_indices(batch * kv_seq)
        _get_out(batch * q_seq)
        _get_metadata(batch, q_seq, kv_seq, FP8_DTYPE, FP8_DTYPE,
                      qo_indptr, kv_indptr, splits)
        # Trigger compile for each known q shape
        q_shape = (batch * q_seq, NUM_HEADS, QK_HEAD_DIM)
        dummy = torch.randn(q_shape, dtype=torch.bfloat16, device="cuda")
        _quantize_fp8(dummy)

try:
    _prewarm()
except Exception:
    pass


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

    batch_size = config["batch_size"]
    q_seq_len  = config["q_seq_len"]
    kv_seq_len = config["kv_seq_len"]

    num_kv_splits           = _num_splits(batch_size, kv_seq_len)
    q_fp8, q_scale          = _quantize_fp8(q)
    kv_buffer_fp8, kv_scale = kv_data["fp8"]

    meta         = _get_metadata(batch_size, q_seq_len, kv_seq_len,
                                 q_fp8.dtype, kv_buffer_fp8.dtype,
                                 qo_indptr, kv_indptr, num_kv_splits)
    kv_buffer_4d = _get_kv_4d(kv_buffer_fp8)
    kv_indices   = _get_kv_indices(kv_buffer_fp8.shape[0])
    o            = _get_out(q.shape[0])

    mla_decode_fwd(
        q_fp8, kv_buffer_4d, o,
        qo_indptr, kv_indptr, kv_indices,
        meta["kv_last_page_len"], 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,
        work_meta_data=meta["work_meta_data"],
        work_indptr=meta["work_indptr"],
        work_info_set=meta["work_info_set"],
        reduce_indptr=meta["reduce_indptr"],
        reduce_final_map=meta["reduce_final_map"],
        reduce_partial_map=meta["reduce_partial_map"],
    )
    return o
scrolls · 175 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