Skip to content
KernelIndex
Search⌘K

submission 689612

MatrixGod-max · python · License unknown

Use it

Vendorable · source mirrored · license unknownView source →

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

mixed_mla.py
curl "https://kernelindex.com/api/v1/implementations/kernelbot-amd-mixed-mla-689612?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
168.3µs
#542 of 766
2026-04-01

Reported · How evidence levels are derived →

Source and license

sourceavailable
revision digestsha256:5b2e2a14ccab737b73ae16e3bcfe6ae2c0e00497b3ccf016a43a2f7082c5eaf9
license declaredunknown
license concludedunknown
authorsMatrixGod-max
imported2026-08-26

Kernel source

mixed_mla.py118 lines
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

# Try multiple import paths for fast fp8 quant
_fast_fp8 = None
for _path in [
    ("aiter", "scaled_fp8_quant"),
    ("aiter.ops.triton.quant", "dynamic_per_tensor_fp8_quant"),
]:
    try:
        _mod = __import__(_path[0], fromlist=[_path[1]])
        _fast_fp8 = getattr(_mod, _path[1])
        break
    except (ImportError, AttributeError):
        continue

# Constants
NUM_HEADS = 16
NUM_KV_HEADS = 1
QK_HEAD_DIM = 576
V_HEAD_DIM = 512
SM_SCALE = 1.0 / (576 ** 0.5)
PAGE_SIZE = 1
NUM_KV_SPLITS = 32
FP8_DTYPE = aiter_dtypes.fp8
_FP8_FINFO = torch.finfo(FP8_DTYPE)

# Caches
_meta_cache = {}
_kv_indices = None
_output_cache = {}


def _quantize_fp8(tensor):
    if _fast_fp8 is not None:
        return _fast_fp8(tensor)
    amax = tensor.abs().amax().clamp(min=1e-12)
    scale = amax / _FP8_FINFO.max
    fp8 = (tensor / scale).clamp(min=_FP8_FINFO.min, max=_FP8_FINFO.max).to(FP8_DTYPE)
    return fp8, scale.float().reshape(1)


def custom_kernel(data: input_t) -> output_t:
    global _kv_indices

    q, kv_data, qo_indptr, kv_indptr, config = data
    batch_size = config["batch_size"]
    q_seq_len = config["q_seq_len"]

    # FP8 Q quantization
    q_fp8, q_scale = _quantize_fp8(q)

    # Pre-quantized FP8 KV
    kv_fp8, kv_scale = kv_data["fp8"]
    kv_4d = kv_fp8.view(kv_fp8.shape[0], 1, 1, kv_fp8.shape[-1])

    # kv_last_page_len
    kv_last_page_len = (kv_indptr[1:] - kv_indptr[:-1]).to(torch.int32)

    # kv_indices (cached, grow-only)
    total_kv = int(kv_indptr[-1].item())
    if _kv_indices is None or _kv_indices.shape[0] < total_kv:
        _kv_indices = torch.arange(max(total_kv, 2097152), dtype=torch.int32, device="cuda")
    kv_indices = _kv_indices[:total_kv]

    # Metadata buffers (cached by batch_size)
    cache_key = batch_size
    if cache_key not in _meta_cache:
        info = get_mla_metadata_info_v1(
            batch_size, q_seq_len, NUM_HEADS, q_fp8.dtype, kv_fp8.dtype,
            is_sparse=False, fast_mode=False,
            num_kv_splits=NUM_KV_SPLITS, intra_batch_mode=True,
        )
        _meta_cache[cache_key] = [torch.empty(s, dtype=t, device="cuda") for s, t in info]

    wm, wi, wis, ri, rfm, rpm = _meta_cache[cache_key]

    get_mla_metadata_v1(
        qo_indptr, kv_indptr, kv_last_page_len,
        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=q_seq_len,
        uni_seqlen_qo=q_seq_len,
        fast_mode=False,
        max_split_per_batch=NUM_KV_SPLITS,
        intra_batch_mode=True,
        dtype_q=q_fp8.dtype,
        dtype_kv=kv_fp8.dtype,
    )

    # Output (cached by total_q)
    total_q = q.shape[0]
    if total_q not in _output_cache:
        _output_cache[total_q] = torch.empty(
            (total_q, NUM_HEADS, V_HEAD_DIM), dtype=torch.bfloat16, device="cuda"
        )
    o = _output_cache[total_q]

    mla_decode_fwd(
        q_fp8.view(-1, NUM_HEADS, QK_HEAD_DIM),
        kv_4d, o,
        qo_indptr, kv_indptr, kv_indices, kv_last_page_len,
        q_seq_len,
        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,
        work_meta_data=wm, work_indptr=wi, work_info_set=wis,
        reduce_indptr=ri, reduce_final_map=rfm, reduce_partial_map=rpm,
    )
    return o
scrolls · 118 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