submission 608379
N-45div · python · License unknown
Use it
Vendorable · source mirrored · license unknownView source →
No package. Vendor the mirrored source: 123 lines, June 9 Researcher Reciprocity License v1.0.
submission.py
curl "https://kernelindex.com/api/v1/implementations/kernelbot-amd-mixed-mla-608379?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:35ac586d607b607d0198100589d7b1e4676a67dd26570f2d73369439c0ec7df1
license declaredunknown
license concludedunknown
authorsN-45div
imported2026-08-26
Techniques
Extracted from the mirrored source by pattern, never inferred. Each row cites its line.
persistent-kernel
- Cache persistent-mode metadata across calls (same shape → reuse buffers)Kernel source
submission.py123 lines
"""
Optimized MLA decode kernel for MI355X.
Key optimizations over reference:
- Cache persistent-mode metadata across calls (same shape → reuse buffers)
- Precompute constants at module level
- Minimize Python overhead in hot path
"""
from task import input_t, output_t
import torch
from aiter.mla import mla_decode_fwd
from aiter import dtypes as aiter_dtypes, get_mla_metadata_info_v1, get_mla_metadata_v1
# DeepSeek R1 MLA constants
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)
NUM_KV_HEADS = 1
PAGE_SIZE = 1
NUM_KV_SPLITS = 32
FP8_DTYPE = aiter_dtypes.fp8
# Cache metadata and work buffers per shape
_meta_cache = {}
def _get_cached_metadata(batch_size, max_q_len, nhead, q_dtype, kv_dtype, qo_indptr, kv_indptr):
key = (batch_size, max_q_len, nhead, q_dtype, kv_dtype)
if key not in _meta_cache:
nkv = NUM_KV_HEADS
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[key] = work
work = _meta_cache[key]
(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,
nhead // 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,
}, kv_last_page_len
def custom_kernel(data: input_t) -> output_t:
q, kv_data, qo_indptr, kv_indptr, config = data
batch_size = config["batch_size"]
nhead = config["num_heads"]
q_seq_len = config["q_seq_len"]
total_kv_len = int(kv_indptr[-1].item())
# FP8 quantize Q (dynamic per-tensor)
finfo = torch.finfo(FP8_DTYPE)
amax = q.abs().amax().clamp(min=1e-12)
q_scale = (amax / finfo.max).to(torch.float32).reshape(1)
q_fp8 = (q / q_scale).clamp(min=finfo.min, max=finfo.max).to(FP8_DTYPE)
# FP8 KV
kv_buffer_fp8, kv_scale = kv_data["fp8"]
# Reshape KV to 4D: (total_kv, page_size=1, nkv=1, 576)
kv_4d = kv_buffer_fp8.view(kv_buffer_fp8.shape[0], PAGE_SIZE, NUM_KV_HEADS, kv_buffer_fp8.shape[-1])
# Cached metadata + KV indices
meta, kv_last_page_len = _get_cached_metadata(
batch_size, q_seq_len, nhead, q_fp8.dtype, kv_buffer_fp8.dtype,
qo_indptr, kv_indptr,
)
kv_indices = torch.arange(total_kv_len, dtype=torch.int32, device="cuda")
o = torch.empty((q.shape[0], nhead, V_HEAD_DIM), dtype=torch.bfloat16, device="cuda")
mla_decode_fwd(
q_fp8.view(-1, nhead, QK_HEAD_DIM),
kv_4d,
o,
qo_indptr,
kv_indptr,
kv_indices,
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,
**meta,
)
return o
scrolls · 123 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