submission 754460
musicofhel · python · License unknown
Use it
Vendorable · source mirrored · license unknownView source →
No package. Vendor the mirrored source: 158 lines, June 9 Researcher Reciprocity License v1.0.
mla_v4.py
curl "https://kernelindex.com/api/v1/implementations/kernelbot-amd-mixed-mla-754460?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:f0aac43f27d9da7f422dcedd1e940b9b68e814625b87d58d7a8f0fcfe25914e9
license declaredunknown
license concludedunknown
authorsmusicofhel
imported2026-08-26
Kernel source
mla_v4.py158 lines
"""
MLA v4: Hybrid bf16/fp8 Q approach.
- Small workloads: bf16 Q + fp8 KV (skip quantize_fp8, use a16w8 kernel) — 36% faster
- Large workloads: fp8 Q + fp8 KV (a8w8 kernel) — avoids 2x Q bandwidth penalty
From v3 benchmarks:
bf16 Q wins on bs<=64 (92-224µs vs baseline 145-260µs)
fp8 Q wins on bs=256,kv=8192 (401µs vs bf16's 662µs)
Metadata buffer caching, output pre-allocation, kv_indices caching.
"""
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
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
NUM_KV_SPLITS = 32
FP8_DTYPE = aiter_dtypes.fp8
# Threshold: if total_kv_len > this, use fp8 Q (a8w8 kernel)
# bs=256 * kv=8192 = 2M → fp8 is better
# bs=64 * kv=8192 = 524K → bf16 is still better
_FP8_Q_THRESHOLD = 1000000
# Caches
_meta_cache = {}
_out_cache = {}
_idx_cache = {}
def _quantize_fp8(tensor):
"""Dynamic per-tensor FP8 quantization."""
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 _get_metadata_cached(batch_size, max_q_len, nq, nkv, q_dtype, kv_dtype,
qo_indptr, kv_indptr, kv_last_page_len):
key = (batch_size, int(kv_indptr[-1].item()), q_dtype, kv_dtype)
if key not in _meta_cache:
info = get_mla_metadata_info_v1(
batch_size, max_q_len, nq, q_dtype, kv_dtype,
is_sparse=False, fast_mode=False,
num_kv_splits=NUM_KV_SPLITS, intra_batch_mode=True,
)
_meta_cache[key] = [torch.empty(s, dtype=t, device="cuda") for s, t in info]
work = _meta_cache[key]
(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,
nq // nkv, nkv, 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 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())
# Decide Q dtype based on workload size
use_fp8_q = total_kv_len > _FP8_Q_THRESHOLD
if use_fp8_q:
# Large workload: fp8 Q + fp8 KV (a8w8 kernel)
q_input, q_scale = _quantize_fp8(q)
else:
# Small workload: bf16 Q + fp8 KV (a16w8 kernel, skip quant)
q_input = q
q_scale = None
# fp8 KV always
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 kv_indices
idx_key = total_kv_len
if idx_key not in _idx_cache:
_idx_cache[idx_key] = torch.arange(total_kv_len, dtype=torch.int32, device="cuda")
kv_indices = _idx_cache[idx_key]
kv_last_page_len = (kv_indptr[1:] - kv_indptr[:-1]).to(torch.int32)
meta = _get_metadata_cached(
batch_size, q_seq_len, nq, nkv,
q_input.dtype, kv_buffer_fp8.dtype,
qo_indptr, kv_indptr, kv_last_page_len,
)
# Cached output tensor
out_key = (q.shape[0], nq, dv)
if out_key not in _out_cache:
_out_cache[out_key] = torch.empty(out_key, dtype=torch.bfloat16, device="cuda")
o = _out_cache[out_key]
mla_decode_fwd(
q_input.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 · 158 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