submission 593911
wsxhjnb1 · python · License unknown
Use it
Vendorable · source mirrored · license unknownView source →
No package. Vendor the mirrored source: 373 lines, June 9 Researcher Reciprocity License v1.0.
submission_mixed_mla.py
curl "https://kernelindex.com/api/v1/implementations/kernelbot-amd-mixed-mla-593911?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:620c605d0590f0c3badfb8474927604a97ee425534087322eea31c788f4cf96d
license declaredunknown
license concludedunknown
authorswsxhjnb1
imported2026-08-26
Techniques
Extracted from the mirrored source by pattern, never inferred. Each row cites its line.
fp4
kv_buffer_mxfp4, kv_scale_mxfp4 = kv_data["mxfp4"]Kernel source
submission_mixed_mla.py373 lines
from __future__ import annotations
import os
from typing import Dict, Hashable, Tuple
import torch
from aiter import dtypes as aiter_dtypes
from aiter import get_mla_metadata_info_v1, get_mla_metadata_v1
from aiter.mla import mla_decode_fwd
from aiter.utility.fp4_utils import dynamic_mxfp4_quant, e8m0_to_f32, mxfp4_to_f32
from task import input_t, output_t
# -----------------------------------------------------------------------------
# MLA decode skeleton for AMD MI355X qualification round.
#
# Safe default:
# AITER FP8 decode + persistent metadata cache.
#
# Where to optimize next:
# 1) _custom_mxfp4_decode
# 2) _dispatch_bucket
# 3) metadata-key strategy (if you introduce more varied varlen batches)
# -----------------------------------------------------------------------------
PAGE_SIZE = 1
FP8_DTYPE = aiter_dtypes.fp8
_METADATA_CACHE: Dict[Hashable, Dict[str, torch.Tensor]] = {}
_WARMED_BUCKETS: set[Tuple[int, int, int]] = set()
_MLA_IMPL_OVERRIDE = os.getenv("MLA_IMPL")
_MLA_NUM_KV_SPLITS_OVERRIDE = os.getenv("MLA_NUM_KV_SPLITS")
_MLA_ENABLE_WARMUP_OVERRIDE = os.getenv("MLA_ENABLE_WARMUP")
def _env_flag(name: str, default: bool = False) -> bool:
value = os.getenv(name)
if value is None:
return default
return value.lower() in {"1", "true", "yes", "on"}
def _env_int(name: str, default: int) -> int:
value = os.getenv(name)
return default if value is None else int(value)
def _env_str(name: str, default: str) -> str:
value = os.getenv(name)
return default if value is None else value
def _default_num_kv_splits(config: dict) -> int:
batch_size = int(config["batch_size"])
kv_seq_len = int(config["kv_seq_len"])
# MLA decode is memory-bound. Short-context cases usually do better with
# enough split parallelism to keep the persistent kernel busy, while the
# heaviest long-context / large-batch cases can use more split parallelism
# without paying as much fixed overhead.
if kv_seq_len <= 1024:
return 32
return 32 if batch_size < 64 else 64
def _bucket(config: dict) -> Tuple[int, int, int]:
return (
int(config["batch_size"]),
int(config["q_seq_len"]),
int(config["kv_seq_len"]),
)
def _quantize_fp8(tensor: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
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 _dequantize_mxfp4(
fp4_data: torch.Tensor,
scale_e8m0: torch.Tensor,
orig_shape: tuple[int, int, int],
dtype: torch.dtype = torch.bfloat16,
) -> torch.Tensor:
# Debug-only fallback path. This is not the optimized submission path.
bsz, nheads_kv, dim = orig_shape
assert nheads_kv == 1, "This skeleton expects the DeepSeek-style MLA layout."
num_rows = bsz * nheads_kv
block_size = 32
num_blocks = dim // block_size
fp4_2d = fp4_data.reshape(num_rows, dim // 2)
vals_f32 = mxfp4_to_f32(fp4_2d)
scales_f32 = e8m0_to_f32(scale_e8m0)[:num_rows, :num_blocks]
vals_f32 = vals_f32.view(num_rows, num_blocks, block_size) * scales_f32.unsqueeze(-1)
return vals_f32.view(bsz, nheads_kv, dim).to(dtype)
def _metadata_key(
config: dict,
qo_indptr: torch.Tensor,
kv_indptr: torch.Tensor,
q_dtype: torch.dtype,
kv_dtype: torch.dtype,
num_kv_splits: int,
) -> Hashable:
# Keying by data_ptr() keeps the steady-state path cheap during repeated
# benchmark runs, because the evaluator reuses the same input tensors after
# the first correctness call.
return (
int(config["batch_size"]),
int(config["q_seq_len"]),
int(config["kv_seq_len"]),
int(config["num_heads"]),
int(config["num_kv_heads"]),
str(q_dtype),
str(kv_dtype),
int(num_kv_splits),
int(qo_indptr.data_ptr()),
int(kv_indptr.data_ptr()),
)
def _get_or_create_metadata(
config: dict,
qo_indptr: torch.Tensor,
kv_indptr: torch.Tensor,
q_dtype: torch.dtype,
kv_dtype: torch.dtype,
num_kv_splits: int,
) -> Dict[str, torch.Tensor]:
key = _metadata_key(config, qo_indptr, kv_indptr, q_dtype, kv_dtype, num_kv_splits)
cached = _METADATA_CACHE.get(key)
if cached is not None:
return cached
batch_size = int(config["batch_size"])
max_q_len = int(config["q_seq_len"])
nhead = int(config["num_heads"])
nhead_kv = int(config["num_kv_heads"])
kv_last_page_len = (kv_indptr[1:] - kv_indptr[:-1]).to(torch.int32)
kv_indices = torch.arange(int(kv_indptr[-1].item()), dtype=torch.int32, device="cuda")
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(shape, dtype=dtype, device="cuda") for shape, dtype in info]
(
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,
nhead // nhead_kv,
nhead_kv,
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,
)
cached = {
"call_meta": {
"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": kv_last_page_len,
"kv_indices": kv_indices,
}
_METADATA_CACHE[key] = cached
return cached
def _run_aiter_decode(
q: torch.Tensor,
kv_buffer: torch.Tensor,
qo_indptr: torch.Tensor,
kv_indptr: torch.Tensor,
config: dict,
q_scale: torch.Tensor | None,
kv_scale: torch.Tensor | None,
) -> torch.Tensor:
num_kv_splits = (
int(_MLA_NUM_KV_SPLITS_OVERRIDE)
if _MLA_NUM_KV_SPLITS_OVERRIDE is not None
else _default_num_kv_splits(config)
)
nq = int(config["num_heads"])
nkv = int(config["num_kv_heads"])
dq = int(config["qk_head_dim"])
dv = int(config["v_head_dim"])
max_q_len = int(config["q_seq_len"])
sm_scale = float(config["sm_scale"])
kv_buffer_4d = kv_buffer.view(kv_buffer.shape[0], PAGE_SIZE, nkv, kv_buffer.shape[-1])
meta = _get_or_create_metadata(
config,
qo_indptr,
kv_indptr,
q.dtype,
kv_buffer.dtype,
num_kv_splits,
)
kv_indices = meta["kv_indices"]
kv_last_page_len = meta["kv_last_page_len"]
call_meta = meta["call_meta"]
out = torch.empty((q.shape[0], nq, dv), dtype=torch.bfloat16, device="cuda")
mla_decode_fwd(
q.view(-1, nq, dq),
kv_buffer_4d,
out,
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,
**call_meta,
)
return out
def _custom_mxfp4_decode(
q: torch.Tensor,
kv_buffer_mxfp4: torch.Tensor,
kv_scale_mxfp4: torch.Tensor,
qo_indptr: torch.Tensor,
kv_indptr: torch.Tensor,
config: dict,
) -> torch.Tensor | None:
# TODO(user): replace this placeholder with your actual fused
# dequant + MLA decode Triton / asm kernel.
_ = (q, kv_buffer_mxfp4, kv_scale_mxfp4, qo_indptr, kv_indptr, config)
return None
def _dispatch_bucket(
q: torch.Tensor,
kv_data: dict,
qo_indptr: torch.Tensor,
kv_indptr: torch.Tensor,
config: dict,
) -> torch.Tensor:
impl = _MLA_IMPL_OVERRIDE or "aiter_fp8"
if impl == "aiter_bf16":
kv_bf16 = kv_data["bf16"]
if not kv_bf16.is_contiguous():
kv_bf16 = kv_bf16.contiguous()
return _run_aiter_decode(q, kv_bf16, qo_indptr, kv_indptr, config, q_scale=None, kv_scale=None)
if impl == "naive_mxfp4_debug":
kv_buffer_mxfp4, kv_scale_mxfp4 = kv_data["mxfp4"]
kv_bf16 = _dequantize_mxfp4(
kv_buffer_mxfp4,
kv_scale_mxfp4,
orig_shape=tuple(int(x) for x in kv_data["bf16"].shape),
)
q_fp8, q_scale = _quantize_fp8(q)
return _run_aiter_decode(q_fp8, kv_bf16, qo_indptr, kv_indptr, config, q_scale=q_scale, kv_scale=None)
if impl == "custom_mxfp4":
kv_buffer_mxfp4, kv_scale_mxfp4 = kv_data["mxfp4"]
candidate = _custom_mxfp4_decode(
q,
kv_buffer_mxfp4,
kv_scale_mxfp4,
qo_indptr,
kv_indptr,
config,
)
if candidate is not None:
return candidate
# Fallback stays performant and correct while your custom kernel is under construction.
# Default / fallback: AITER FP8 decode.
q_fp8, q_scale = _quantize_fp8(q)
kv_fp8, kv_scale = kv_data["fp8"]
if not kv_fp8.is_contiguous():
kv_fp8 = kv_fp8.contiguous()
return _run_aiter_decode(
q_fp8,
kv_fp8,
qo_indptr,
kv_indptr,
config,
q_scale=q_scale,
kv_scale=kv_scale,
)
def _maybe_warmup(
q: torch.Tensor,
kv_data: dict,
qo_indptr: torch.Tensor,
kv_indptr: torch.Tensor,
config: dict,
) -> None:
if _MLA_ENABLE_WARMUP_OVERRIDE is None:
warmup_enabled = True
else:
warmup_enabled = _MLA_ENABLE_WARMUP_OVERRIDE.lower() in {"1", "true", "yes", "on"}
if not warmup_enabled:
return
bucket = _bucket(config)
if bucket in _WARMED_BUCKETS:
return
_ = _dispatch_bucket(q, kv_data, qo_indptr, kv_indptr, config)
torch.cuda.synchronize()
_WARMED_BUCKETS.add(bucket)
@torch.inference_mode()
def custom_kernel(data: input_t) -> output_t:
q, kv_data, qo_indptr, kv_indptr, config = data
if not q.is_contiguous():
q = q.contiguous()
if not qo_indptr.is_contiguous():
qo_indptr = qo_indptr.contiguous()
if not kv_indptr.is_contiguous():
kv_indptr = kv_indptr.contiguous()
_maybe_warmup(q, kv_data, qo_indptr, kv_indptr, config)
return _dispatch_bucket(q, kv_data, qo_indptr, kv_indptr, config)
scrolls · 373 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