submission 661453
Henry.Shi · python · License unknown
Use it
Vendorable · source mirrored · license unknownView source →
No package. Vendor the mirrored source: 204 lines, June 9 Researcher Reciprocity License v1.0.
submession_v6.py
curl "https://kernelindex.com/api/v1/implementations/kernelbot-amd-mixed-mla-661453?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:abf686682bb79a3cbbafa3add766d5d43ba98f2b185eeb12a61430a9edf5fdeb
license declaredunknown
license concludedunknown
authorsHenry.Shi
imported2026-08-26
Kernel source
submession_v6.py204 lines
import math
from typing import Dict, Tuple
import torch
import torch.nn.functional as F
from task import input_t, output_t
from utils import make_match_reference
if not torch.cuda.is_available():
raise RuntimeError("MI355x/Rocm GPU is required")
DEVICE = "cuda"
gpu_name = torch.cuda.get_device_name(0)
print(f"GPU: {gpu_name}")
_IS_ROCM = hasattr(torch.version, "hip") and torch.version.hip is not None
if _IS_ROCM:
print(f"Rocm version: {torch.version.hip}")
else:
print(f"CUDA version: {torch.version.cuda}")
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)
def _pick_fp8_dtype() -> torch.dtype:
if _IS_ROCM and hasattr(torch, "float8_e4m3fnuz"):
dt = torch.float8_e4m3fnuz
try:
_ = torch.tensor([0.0], device=DEVICE, dtype=dt)
return dt
except Exception:
pass
if hasattr(torch, "float8_e4m3fn"):
dt = torch.float8_e4m3fn
try:
_ = torch.tensor([0.0], device=DEVICE, dtype=dt)
return dt
except Exception:
pass
return torch.bfloat16
FP8_DTYPE = _pick_fp8_dtype()
FP8_MAX = torch.finfo(FP8_DTYPE).max
FP8_MIN = torch.finfo(FP8_DTYPE).min
def quantize_fp8(tensor: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
amax = tensor.abs().amax().clamp(min=1e-12)
scale = amax / FP8_MAX
fp8_tensor = (tensor / scale).clamp(FP8_MIN, FP8_MAX).to(FP8_DTYPE)
return fp8_tensor, scale.to(torch.float32).reshape(1)
_static_bufs = {}
_graph_cache = {}
_SDPA_OK = None
def _try_capture_graph(key):
bufs = _static_bufs[key]
q_s, kt_s, v_s, out_s = bufs["q"], bufs["kt"], bufs["v"], bufs["out"]
try:
torch.cuda.synchronize()
for _ in range(3):
s = torch.bmm(q_s,kt_s)
s.mul_(SM_SCALE)
a = torch.softmax(s, dim=-1)
torch.bmm(a, v_s, out=out_s)
torch.cuda.synchronize()
g = torch.cuda.CUDAGraph()
with torch.cuda.graph(g):
s = torch.bmm(q_s,kt_s)
s.mul_(SM_SCALE)
a = torch.softmax(s, dim=-1)
torch.bmm(a, v_s, out=out_s)
_graph_cache[key] = g
except Exception:
_graph_cache[key] = None
def generate_input(batchsize: int, qseqlen: int, kvseqlen: int, seed: int) -> input_t:
gen = torch.Generator(device=DEVICE)
gen.manual_seed(seed)
total_q = batchsize * qseqlen
total_kv = batchsize * kvseqlen
q = torch.randn(
(total_q, NUM_HEADS, QK_HEAD_DIM),
dtype=torch.bfloat16, device=DEVICE, generator=gen,
)
kv_buffer_bf16 = torch.randn(
(total_kv, NUM_KV_HEADS, QK_HEAD_DIM),
dtype=torch.bfloat16, device=DEVICE, generator=gen,
)
kv_buffer_fp8, kv_scale_fp8 = quantize_fp8(kv_buffer_bf16)
kv_3d = kv_buffer_bf16.view(batchsize, kvseqlen, QK_HEAD_DIM)
key = (batchsize, kvseqlen)
if key not in _static_bufs:
_static_bufs[key] = {
"q": torch.empty((batchsize, NUM_HEADS, QK_HEAD_DIM),
dtype=torch.bfloat16, device=DEVICE),
"kt": torch.empty((batchsize, QK_HEAD_DIM, kvseqlen),
dtype=torch.bfloat16, device=DEVICE),
"v": torch.empty((batchsize, kvseqlen, V_HEAD_DIM),
dtype=torch.bfloat16, device=DEVICE),
"out": torch.empty((batchsize, NUM_HEADS, V_HEAD_DIM),
dtype=torch.bfloat16, device=DEVICE),
}
bufs = _static_bufs[key]
bufs["q"].copy_(q.view(batchsize, NUM_HEADS, QK_HEAD_DIM))
bufs["kt"].copy_(kv_3d.transpose(1, 2))
bufs["v"].copy_(kv_3d[:, :, :V_HEAD_DIM])
if key not in _graph_cache:
_try_capture_graph(key)
kv_data = {
"bf16": kv_buffer_bf16,
"fp8": (kv_buffer_fp8, kv_scale_fp8),
"_key": key,
"_kt": bufs["kt"],
"_v": bufs["v"],
"_kv4d": kv_3d.unsqueeze(1),
}
qo_indptr = torch.arange(0, batchsize + 1, dtype=torch.int32, device=DEVICE) * qseqlen
kv_indptr = torch.arange(0, batchsize + 1, dtype=torch.int32, device=DEVICE) * kvseqlen
config = {
"batch_size": batchsize,
"num_heads": NUM_HEADS,
"num_kv_heads": NUM_KV_HEADS,
"qk_head_dim": QK_HEAD_DIM,
"kv_lora_rank": KV_LORA_RANK,
"qk_rope_head_dim": QK_ROPE_HEAD_DIM,
"v_head_dim": V_HEAD_DIM,
"q_seq_len": qseqlen,
"kv_seq_len": kvseqlen,
"sm_scale": SM_SCALE,
}
return (q, kv_data, qo_indptr, kv_indptr, config)
def custom_kernel(data: input_t) -> output_t:
global _SDPA_OK
q, kv_data, qo_indptr, kv_indptr, config = data
batch_size = config["batch_size"]
kv_seq_len = config["kv_seq_len"]
nq = config["num_heads"]
dv = config["v_head_dim"]
dq = config["qk_head_dim"]
sm_scale = config["sm_scale"]
key = kv_data.get("_key")
if key and _graph_cache.get(key) is not None:
_graph_cache[key].replay()
return _static_bufs[key]["out"].clone()
if "_kv4d" in kv_data and _SDPA_OK is not False:
try:
q_4d = q.view(batch_size, nq, 1, dq)
kv_4d = kv_data["_kv4d"]
out = F.scaled_dot_product_attention(
q_4d, kv_4d, kv_4d,
scale=sm_scale,
enable_gqa=True,
)
_SDPA_OK = True
return out.squeeze(2)[:, :, :dv].contiguous()
except Exception:
_SDPA_OK = False
q_batch = q.view(batch_size, nq, dq)
if "_kt" in kv_data:
kt = kv_data["_kt"]
v = kv_data["_v"]
scores = torch.bmm(q_batch, kt)
scores.mul_(sm_scale)
attn = torch.softmax(scores, dim=-1)
return torch.bmm(attn,v)
if "bf16" in kv_data:
kv_buffer = kv_data["bf16"]
elif "fp8" in kv_data:
kv_fp8, kv_scale = kv_data["fp8"]
kv_buffer = (kv_fp8.to(torch.float32) * kv_scale).to(torch.bfloat16)
else:
key = next(iter(kv_data))
val = kv_data[key]
kv_buffer = (val[0] if isinstance(val, tuple) else val).to(torch.bfloat16)
kv_batch = kv_buffer.view(batch_size, kv_seq_len, dq)
scores = torch.bmm(q_batch, kv_batch.transpose(1, 2))
scores.mul_(sm_scale)
attn = torch.softmax(scores, dim=-1)
out = torch.bmm(attn, kv_batch)
return out[:, :, :dv].contiguous()
check_implementation = make_match_reference(custom_kernel, rtol=1e-1, atol=1e-1)scrolls · 204 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