Skip to content
KernelIndex
Search⌘K

submission 592718

Yang Liu · python · License unknown

Use it

Vendorable · source mirrored · license unknownView source →

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

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

Reported · How evidence levels are derived →

Source and license

sourceavailable
revision digestsha256:af5f55a0c731159ca26bdd8cdac32b3005ba28b55c6719526102accac3f60df5
license declaredunknown
license concludedunknown
authorsYang Liu
imported2026-08-26

Techniques

Extracted from the mirrored source by pattern, never inferred. Each row cites its line.

fp4"mxfp4": (kv_buffer_mxfp4, scale_e8m0),

Kernel source

submission.py242 lines
import torch
import torch.nn.functional as F
from task import input_t, output_t
from utils import make_match_reference

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

# MLA constants
NUM_HEADS = 16
NUM_KV_HEADS = 1
KV_LORA_RANK = 512
QK_ROPE_HEAD_DIM = 64
QK_HEAD_DIM = 576
V_HEAD_DIM = 512
SM_SCALE = 1.0 / (QK_HEAD_DIM ** 0.5)
PAGE_SIZE = 1
NUM_KV_SPLITS = 32
FP8_DTYPE = aiter_dtypes.fp8

# Cache for metadata to avoid recomputation
_meta_cache = {}


def _get_cached_metadata(batch_size, max_q_len, kv_indptr, qo_indptr):
    key = (batch_size, max_q_len)
    kv_last_page_len = (kv_indptr[1:] - kv_indptr[:-1]).to(torch.int32)

    if key not in _meta_cache:
        q_dtype = FP8_DTYPE
        kv_dtype = FP8_DTYPE
        info = get_mla_metadata_info_v1(
            batch_size, max_q_len, NUM_HEADS, q_dtype, kv_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]
        _meta_cache[key] = work

    work = _meta_cache[key]
    (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,
        NUM_HEADS // NUM_KV_HEADS, NUM_KV_HEADS,
        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=FP8_DTYPE,
        dtype_kv=FP8_DTYPE,
    )

    return {
        "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,
    }


_output_cache = {}
_kv_indices_cache = {}


def custom_kernel(data: input_t) -> output_t:
    q, kv_data, qo_indptr, kv_indptr, config = data

    batch_size = config["batch_size"]
    q_seq_len = config["q_seq_len"]

    # FP8 quantize Q on-the-fly
    finfo = torch.finfo(FP8_DTYPE)
    q_amax = q.abs().amax().clamp(min=1e-12)
    q_scale = q_amax / finfo.max
    q_fp8 = (q / q_scale).clamp(min=finfo.min, max=finfo.max).to(FP8_DTYPE)
    q_scale = q_scale.to(torch.float32).reshape(1)

    # Get fp8 KV data
    kv_buffer_fp8, kv_scale = kv_data["fp8"]

    # Total KV length and indices
    total_kv_len = int(kv_indptr[-1].item())

    if total_kv_len not in _kv_indices_cache:
        _kv_indices_cache[total_kv_len] = torch.arange(total_kv_len, dtype=torch.int32, device="cuda")
    kv_indices = _kv_indices_cache[total_kv_len]

    # Reshape KV buffer
    kv_buffer_4d = kv_buffer_fp8.view(kv_buffer_fp8.shape[0], PAGE_SIZE, NUM_KV_HEADS, kv_buffer_fp8.shape[-1])

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

    # Get metadata
    meta = _get_cached_metadata(batch_size, q_seq_len, kv_indptr, qo_indptr)

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

    mla_decode_fwd(
        q_fp8.view(-1, NUM_HEADS, QK_HEAD_DIM),
        kv_buffer_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,
        **meta,
    )

    return o


def generate_input(batchsize: int, qseqlen: int, kvseqlen: int, seed: int) -> input_t:
    gen = torch.Generator(device="cuda")
    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="cuda", generator=gen)
    kv_buffer_bf16 = torch.randn((total_kv, NUM_KV_HEADS, QK_HEAD_DIM), dtype=torch.bfloat16, device="cuda", generator=gen)

    # FP8 quantize KV
    finfo = torch.finfo(FP8_DTYPE)
    amax = kv_buffer_bf16.abs().amax().clamp(min=1e-12)
    scale = amax / finfo.max
    kv_buffer_fp8 = (kv_buffer_bf16 / scale).clamp(min=finfo.min, max=finfo.max).to(FP8_DTYPE)
    kv_scale_fp8 = scale.to(torch.float32).reshape(1)

    # MXFP4 quantize KV
    from aiter.utility.fp4_utils import dynamic_mxfp4_quant
    B, M, N = kv_buffer_bf16.shape
    tensor_2d = kv_buffer_bf16.reshape(B * M, N)
    fp4_data_2d, scale_e8m0 = dynamic_mxfp4_quant(tensor_2d)
    kv_buffer_mxfp4 = fp4_data_2d.view(B, M, N // 2)

    kv_data = {
        "bf16": kv_buffer_bf16,
        "fp8": (kv_buffer_fp8, kv_scale_fp8),
        "mxfp4": (kv_buffer_mxfp4, scale_e8m0),
    }

    qo_indptr = torch.arange(0, batchsize + 1, dtype=torch.int32, device="cuda") * qseqlen
    kv_indptr = torch.arange(0, batchsize + 1, dtype=torch.int32, device="cuda") * 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 ref_kernel(data: input_t) -> output_t:
    q, kv_data, qo_indptr, kv_indptr, config = data

    finfo = torch.finfo(FP8_DTYPE)
    q_amax = q.abs().amax().clamp(min=1e-12)
    q_scale = q_amax / finfo.max
    q_fp8 = (q / q_scale).clamp(min=finfo.min, max=finfo.max).to(FP8_DTYPE)
    q_scale = q_scale.to(torch.float32).reshape(1)

    kv_buffer_fp8, kv_scale = kv_data["fp8"]

    batch_size = config["batch_size"]
    q_seq_len = config["q_seq_len"]
    total_kv_len = int(kv_indptr[-1].item())
    kv_indices = torch.arange(total_kv_len, dtype=torch.int32, device="cuda")
    kv_buffer_4d = kv_buffer_fp8.view(kv_buffer_fp8.shape[0], PAGE_SIZE, NUM_KV_HEADS, kv_buffer_fp8.shape[-1])
    kv_last_page_len = (kv_indptr[1:] - kv_indptr[:-1]).to(torch.int32)

    info = get_mla_metadata_info_v1(
        batch_size, q_seq_len, 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]
    (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,
        NUM_HEADS // NUM_KV_HEADS, NUM_KV_HEADS,
        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=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=FP8_DTYPE, dtype_kv=FP8_DTYPE,
    )

    o = torch.empty((q.shape[0], NUM_HEADS, V_HEAD_DIM), dtype=torch.bfloat16, device="cuda")

    mla_decode_fwd(
        q_fp8.view(-1, NUM_HEADS, QK_HEAD_DIM),
        kv_buffer_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=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


check_implementation = make_match_reference(ref_kernel, rtol=1e-01, atol=1e-01)
scrolls · 242 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