submission 616674
aidandonaghey1 · python · License unknown
Use it
Vendorable · source mirrored · license unknownView source →
No package. Vendor the mirrored source: 222 lines, June 9 Researcher Reciprocity License v1.0.
submission.py
curl "https://kernelindex.com/api/v1/implementations/kernelbot-amd-mixed-mla-616674?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
Reported · How evidence levels are derived →
Source and license
sourceavailable
revision digestsha256:5a136838f11b6f474406aaaa9a06e786b237c717117e667edf8f90c93536d78d
license declaredunknown
license concludedunknown
authorsaidandonaghey1
imported2026-08-26
Kernel source
submission.py222 lines
#!POPCORN leaderboard amd-mixed-mla
#!POPCORN gpu MI355X
"""
Optimized MLA decode kernel for AMD MI355X.
Optimizations over reference:
1. Cache metadata buffers — avoid re-allocating per call
2. Cache kv_indices — avoid torch.arange per call
3. Pre-allocate output tensor — reuse across calls
4. Config-adaptive NUM_KV_SPLITS — tune per batch/kv_len
5. Fused Q quantization — fewer kernel launches
"""
import torch
from task import input_t, output_t
from utils import make_match_reference
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
# ---------------------------------------------------------------------------
# DeepSeek R1 latent MQA constants
# ---------------------------------------------------------------------------
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 # 576
V_HEAD_DIM = KV_LORA_RANK # 512
SM_SCALE = 1.0 / (QK_HEAD_DIM ** 0.5)
PAGE_SIZE = 1
FP8_DTYPE = aiter_dtypes.fp8
FP8_FINFO = torch.finfo(FP8_DTYPE)
# ---------------------------------------------------------------------------
# Caches (populated on first call per config)
# ---------------------------------------------------------------------------
_meta_cache: dict[tuple, dict] = {}
_kv_indices_cache: dict[int, torch.Tensor] = {}
_output_cache: dict[tuple, torch.Tensor] = {}
# ---------------------------------------------------------------------------
# Config-adaptive KV splits
# ---------------------------------------------------------------------------
def _pick_num_kv_splits(batch_size: int, kv_len: int) -> int:
total_tokens = batch_size * kv_len
if total_tokens <= 8192: # e.g. bs=4, kv=1024
return 8
elif total_tokens <= 65536: # e.g. bs=32, kv=1024 or bs=4, kv=8192
return 16
elif total_tokens <= 524288: # e.g. bs=64, kv=8192
return 32
else: # e.g. bs=256, kv=8192
return 64
# ---------------------------------------------------------------------------
# Cached metadata builder
# ---------------------------------------------------------------------------
def _get_cached_metadata(
batch_size: int,
max_q_len: int,
nhead: int,
nhead_kv: int,
q_dtype: torch.dtype,
kv_dtype: torch.dtype,
qo_indptr: torch.Tensor,
kv_indptr: torch.Tensor,
num_kv_splits: int,
) -> dict:
kv_last_page_len = (kv_indptr[1:] - kv_indptr[:-1]).to(torch.int32)
cache_key = (batch_size, max_q_len, nhead, nhead_kv, q_dtype, kv_dtype, num_kv_splits)
if cache_key not in _meta_cache:
info = get_mla_metadata_info_v1(
batch_size, max_q_len, nhead, 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]
_meta_cache[cache_key] = {
"work": work,
"meta": {
"work_meta_data": work[0],
"work_indptr": work[1],
"work_info_set": work[2],
"reduce_indptr": work[3],
"reduce_final_map": work[4],
"reduce_partial_map": work[5],
}
}
cached = _meta_cache[cache_key]
(work_metadata, work_indptr, work_info_set,
reduce_indptr, reduce_final_map, reduce_partial_map) = cached["work"]
get_mla_metadata_v1(
qo_indptr, kv_indptr, kv_last_page_len,
nhead // nhead_kv,
nhead_kv,
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 cached["meta"]
# ---------------------------------------------------------------------------
# Cached kv_indices
# ---------------------------------------------------------------------------
def _get_kv_indices(total_kv_len: int) -> torch.Tensor:
if total_kv_len not in _kv_indices_cache:
_kv_indices_cache[total_kv_len] = torch.arange(
total_kv_len, dtype=torch.int32, device="cuda"
)
cached = _kv_indices_cache[total_kv_len]
if cached.shape[0] < total_kv_len:
_kv_indices_cache[total_kv_len] = torch.arange(
total_kv_len, dtype=torch.int32, device="cuda"
)
return _kv_indices_cache[total_kv_len]
# ---------------------------------------------------------------------------
# Cached output buffer
# ---------------------------------------------------------------------------
def _get_output(total_q: int, nq: int, dv: int) -> torch.Tensor:
key = (total_q, nq, dv)
if key not in _output_cache:
_output_cache[key] = torch.empty(
(total_q, nq, dv), dtype=torch.bfloat16, device="cuda"
)
return _output_cache[key]
# ---------------------------------------------------------------------------
# Optimized FP8 quantization
# ---------------------------------------------------------------------------
def _quantize_fp8_fast(tensor: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
amax = tensor.abs().amax().clamp(min=1e-12)
scale = amax / FP8_FINFO.max
fp8_tensor = (tensor / scale).clamp(min=FP8_FINFO.min, max=FP8_FINFO.max).to(FP8_DTYPE)
return fp8_tensor, scale.to(torch.float32).reshape(1)
# ---------------------------------------------------------------------------
# Main kernel
# ---------------------------------------------------------------------------
def custom_kernel(data: input_t) -> output_t:
q, kv_data, qo_indptr, kv_indptr, config = data
batch_size = config["batch_size"]
nq = config["num_heads"]
nkv = config["num_kv_heads"]
dq = config["qk_head_dim"]
dv = config["v_head_dim"]
q_seq_len = config["q_seq_len"]
total_kv_len = int(kv_indptr[-1].item())
# Adaptive KV splits
kv_len_per_seq = total_kv_len // batch_size if batch_size > 0 else total_kv_len
num_kv_splits = _pick_num_kv_splits(batch_size, kv_len_per_seq)
# FP8 Q quantization
q_fp8, q_scale = _quantize_fp8_fast(q)
# FP8 KV
kv_buffer_fp8, kv_scale = kv_data["fp8"]
kv_buffer_4d = kv_buffer_fp8.view(kv_buffer_fp8.shape[0], PAGE_SIZE, nkv, kv_buffer_fp8.shape[-1])
# Cached metadata (buffers reused, but re-populated with current indptrs)
meta = _get_cached_metadata(
batch_size, q_seq_len, nq, nkv,
q_fp8.dtype, kv_buffer_fp8.dtype,
qo_indptr, kv_indptr, num_kv_splits,
)
# Cached kv_indices
kv_indices = _get_kv_indices(total_kv_len)
# kv_last_page_len (recomputed — cheap)
kv_last_page_len = (kv_indptr[1:] - kv_indptr[:-1]).to(torch.int32)
# Output buffer
o = _get_output(q.shape[0], nq, dv)
mla_decode_fwd(
q_fp8.view(-1, nq, dq),
kv_buffer_4d,
o,
qo_indptr,
kv_indptr,
kv_indices,
kv_last_page_len,
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,
kv_scale=kv_scale,
intra_batch_mode=True,
**meta,
)
return o
scrolls · 222 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