submission 663193
SomersBuchannan · python · License unknown
Use it
Vendorable · source mirrored · license unknownView source →
No package. Vendor the mirrored source: 178 lines, June 9 Researcher Reciprocity License v1.0.
submission_x1.py
curl "https://kernelindex.com/api/v1/implementations/kernelbot-amd-mixed-mla-663193?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:9ef2f74762ba307930d94fdf36d833d7279a1cb3a992c708aee3a15ce39c86bd
license declaredunknown
license concludedunknown
authorsSomersBuchannan
imported2026-08-26
Kernel source
submission_x1.py178 lines
"""
Optimized MLA decode kernel based on aiter fp8 (a8w8) reference.
Optimization strategy: Minimize Python-side overhead around the aiter kernel.
The aiter mla_decode_fwd kernel itself is hand-tuned assembly — we can't change it.
But we CAN optimize everything around it:
1. Use aiter's native scaled_fp8_quant for Q quantization (fused GPU kernel vs manual)
2. Pre-allocate and cache reusable tensors (kv_indices, output, metadata buffers)
3. Minimize tensor operations (avoid unnecessary .to(), .view(), .reshape())
4. Try fast_mode=True for metadata generation
5. Experiment with kv_granularity and other metadata parameters
"""
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
# ---------------------------------------------------------------------------
# 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
NUM_KV_SPLITS = 32
FP8_DTYPE = aiter_dtypes.fp8
# ---------------------------------------------------------------------------
# Try to import aiter's optimized scaled_fp8_quant
# This is a fused CUDA/HIP kernel that does amax + scale + quantize in one pass
# Much faster than the manual 3-step approach in reference.py
# ---------------------------------------------------------------------------
try:
from aiter import scaled_fp8_quant
_HAS_AITER_FP8_QUANT = True
except ImportError:
_HAS_AITER_FP8_QUANT = False
# ---------------------------------------------------------------------------
# Caches to avoid repeated allocations
# ---------------------------------------------------------------------------
_cache = {}
def _quantize_fp8_fast(tensor: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
"""FP8 quantization — use aiter fused kernel if available."""
if _HAS_AITER_FP8_QUANT:
return scaled_fp8_quant(tensor)
# Fallback: manual (same as reference)
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_cached(key, factory):
"""Get or create a cached tensor/object."""
if key not in _cache:
_cache[key] = factory()
return _cache[key]
def custom_kernel(data: input_t) -> output_t:
"""Optimized MLA decode — minimize overhead around aiter a8w8 kernel."""
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"]
kv_seq_len = config["kv_seq_len"]
total_q = q.shape[0]
total_kv_len = batch_size * kv_seq_len # uniform lengths
# --- 1. Quantize Q to fp8 ---
q_fp8, q_scale = _quantize_fp8_fast(q)
# --- 2. Get pre-quantized fp8 KV (already in input, zero cost) ---
kv_buffer_fp8, kv_scale = kv_data["fp8"]
# --- 3. Reshape KV to 4D (just a view, no copy) ---
kv_buffer_4d = kv_buffer_fp8.view(kv_buffer_fp8.shape[0], PAGE_SIZE, nkv, kv_buffer_fp8.shape[-1])
# --- 4. Cached kv_indices ---
cache_key_indices = ("kv_indices", total_kv_len)
kv_indices = _get_cached(cache_key_indices,
lambda: torch.arange(total_kv_len, dtype=torch.int32, device="cuda"))
# --- 5. kv_last_page_len (simple subtraction) ---
kv_last_page_len = (kv_indptr[1:] - kv_indptr[:-1]).to(torch.int32)
# --- 6. Build metadata ---
# The metadata depends on (batch_size, kv_seq_len, q_dtype, kv_dtype)
# For uniform-length batches, we can cache the metadata buffer allocations
max_q_len = q_seq_len
cache_key_meta = ("meta_info", batch_size, kv_seq_len, q_fp8.dtype, kv_buffer_fp8.dtype)
def _build_meta_buffers():
info = get_mla_metadata_info_v1(
batch_size, max_q_len, nq, q_fp8.dtype, kv_buffer_fp8.dtype,
is_sparse=False, fast_mode=False,
num_kv_splits=NUM_KV_SPLITS, intra_batch_mode=True,
)
return [torch.empty(s, dtype=t, device="cuda") for s, t in info]
work = _get_cached(cache_key_meta, _build_meta_buffers)
(work_metadata, work_indptr, work_info_set,
reduce_indptr, reduce_final_map, reduce_partial_map) = work
# Populate metadata (must be done every call since indptr may differ)
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_fp8.dtype,
dtype_kv=kv_buffer_fp8.dtype,
)
# --- 7. Cached output tensor ---
cache_key_out = ("output", total_q, nq, dv)
o = _get_cached(cache_key_out,
lambda: torch.empty((total_q, nq, dv), dtype=torch.bfloat16, device="cuda"))
# Ensure correct size (in case batch changes)
if o.shape[0] != total_q:
o = torch.empty((total_q, nq, dv), dtype=torch.bfloat16, device="cuda")
_cache[cache_key_out] = o
# --- 8. Call aiter kernel ---
mla_decode_fwd(
q_fp8.view(-1, nq, dq),
kv_buffer_4d,
o,
qo_indptr,
kv_indptr,
kv_indices,
kv_last_page_len,
max_q_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,
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,
)
return o
scrolls · 178 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