Skip to content
KernelIndex
Search⌘K

submission 624003

dannywillowliu-uchi · python · License unknown

Use it

Vendorable · source mirrored · license unknownView source →

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

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

Reported · How evidence levels are derived →

Source and license

sourceavailable
revision digestsha256:85d4469e12c8016d50a170ba17d4bb2ee561a81eb6ea352120a2107d6ccd136a
license declaredunknown
license concludedunknown
authorsdannywillowliu-uchi
imported2026-08-26

Kernel source

submission.py106 lines
import torch
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
NUM_KV_SPLITS = 16

# Dispatch threshold: bf16-NP for small shapes, fp8-PS for large shapes
# fp8-PS saves bandwidth (1 byte vs 2 bytes per element) but has metadata + Q quantization overhead
# Breakeven on eval server: total_kv ~65K
FP8_PS_THRESHOLD = 65536

_cache = {}


def _init_fp8_ps(bs, kv, total_kv, total_q, qo_indptr, kv_indptr):
	"""Initialize fp8-PS with cached metadata for large shapes."""
	kv_lpl = (kv_indptr[1:] - kv_indptr[:-1]).to(torch.int32)
	kv_idx = torch.arange(total_kv, dtype=torch.int32, device="cuda")
	o_buf = torch.empty((total_q, NUM_HEADS, V_HEAD_DIM), dtype=torch.bfloat16, device="cuda")

	info = get_mla_metadata_info_v1(
		bs, 1, NUM_HEADS, FP8_DTYPE, FP8_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]
	wm, wi, wis, ri, rfm, rpm = work
	get_mla_metadata_v1(
		qo_indptr, kv_indptr, kv_lpl,
		NUM_HEADS // NUM_KV_HEADS, NUM_KV_HEADS, True,
		wm, wis, wi, ri, rfm, rpm,
		page_size=PAGE_SIZE, kv_granularity=max(PAGE_SIZE, 16),
		max_seqlen_qo=1, uni_seqlen_qo=1,
		fast_mode=False, max_split_per_batch=NUM_KV_SPLITS,
		intra_batch_mode=True,
		dtype_q=FP8_DTYPE, dtype_kv=FP8_DTYPE,
	)

	return {
		"mode": "fp8-PS", "o": o_buf, "kv_lpl": kv_lpl, "kv_idx": kv_idx,
		"meta": {
			"work_meta_data": wm, "work_indptr": wi, "work_info_set": wis,
			"reduce_indptr": ri, "reduce_final_map": rfm, "reduce_partial_map": rpm,
		},
	}


def custom_kernel(data):
	q, kv_data, qo_indptr, kv_indptr, config = data
	bs = config["batch_size"]
	kv = config["kv_seq_len"]
	total_kv = bs * kv
	total_q = q.shape[0]
	key = (bs, kv)

	if key not in _cache:
		if total_kv <= FP8_PS_THRESHOLD:
			# bf16-NP: simple, no quantization overhead, fast for small shapes
			kv_lpl = (kv_indptr[1:] - kv_indptr[:-1]).to(torch.int32)
			kv_idx = torch.arange(total_kv, dtype=torch.int32, device="cuda")
			o_buf = torch.empty((total_q, NUM_HEADS, V_HEAD_DIM), dtype=torch.bfloat16, device="cuda")
			_cache[key] = {"mode": "bf16-NP", "o": o_buf, "kv_lpl": kv_lpl, "kv_idx": kv_idx}
		else:
			_cache[key] = _init_fp8_ps(bs, kv, total_kv, total_q, qo_indptr, kv_indptr)

	c = _cache[key]

	if c["mode"] == "bf16-NP":
		kv_buf = kv_data["bf16"]
		mla_decode_fwd(
			q.view(-1, NUM_HEADS, QK_HEAD_DIM),
			kv_buf.view(total_kv, PAGE_SIZE, NUM_KV_HEADS, -1),
			c["o"], qo_indptr, kv_indptr, c["kv_idx"], c["kv_lpl"], 1,
			page_size=PAGE_SIZE, nhead_kv=NUM_KV_HEADS,
			sm_scale=SM_SCALE, logit_cap=0.0,
		)
		return c["o"]

	# fp8-PS path
	amax = q.abs().amax().clamp(min=1e-12)
	q_scale = (amax / FP8_MAX).to(torch.float32).reshape(1)
	q_fp8 = (q / q_scale).clamp(min=FP8_MIN, max=FP8_MAX).to(FP8_DTYPE)
	kv_buf, kv_scale = kv_data["fp8"]
	mla_decode_fwd(
		q_fp8.view(-1, NUM_HEADS, QK_HEAD_DIM),
		kv_buf.view(total_kv, PAGE_SIZE, NUM_KV_HEADS, -1),
		c["o"], qo_indptr, kv_indptr, c["kv_idx"], c["kv_lpl"], 1,
		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,
		**c["meta"],
	)
	return c["o"]
scrolls · 106 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