Skip to content
KernelIndex
Search⌘K

submission 623446

Arkadip Maitra · 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.

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

Reported · How evidence levels are derived →

Source and license

sourceavailable
revision digestsha256:a9ee8f5f07d09456aae08c6dfb92d2902c9e2d9951c43b3d6f13b1a8a02cd38f
license declaredunknown
license concludedunknown
authorsArkadip Maitra
imported2026-08-26

Techniques

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

persistent-kernel2. Persistent-mode metadata caching: get_mla_metadata_info_v1 +

Kernel source

submission.py204 lines
"""
Optimized MLA decode attention kernel.

Optimizations over reference (fp8 Q + fp8 KV, a8w8):

1. bf16 Q + fp8 KV (a16w8) path: eliminates Q quantization entirely on
   gfx950/gfx942. The decode workload is KV-memory-bound, so skipping
   the Q quant overhead (~5us of kernel launches) helps at all batch sizes.

2. Persistent-mode metadata caching: get_mla_metadata_info_v1 +
   get_mla_metadata_v1 allocate 6 GPU tensors and fill them with scheduling
   data. We cache these keyed by (batch_size, total_kv, dtypes) and reuse
   across calls with the same config, saving ~10us per call.

3. kv_indices / kv_last_page_len caching: eliminates redundant torch.arange
   and tensor subtraction per call (~1-2us savings).

4. Output tensor caching: reuse pre-allocated output buffer for the same
   total_q, avoiding torch.empty allocation overhead (~1us savings).

5. Fused FP8 quantization fallback: uses aiter.scaled_fp8_quant (single CUDA
   kernel) instead of the manual 5-kernel implementation when a16w8 is
   unavailable.
"""

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

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
V_HEAD_DIM = KV_LORA_RANK
SM_SCALE = 1.0 / (QK_HEAD_DIM ** 0.5)
PAGE_SIZE = 1
NUM_KV_SPLITS = 32
FP8_DTYPE = aiter_dtypes.fp8

_meta_cache = {}
_kv_indices_cache = {}
_kv_last_page_cache = {}
_output_cache = {}
_quant_fn = None
_mode_detected = False
_use_a16w8 = False


def _init_quant_fn():
    global _quant_fn
    if _quant_fn is not None:
        return _quant_fn
    try:
        import aiter
        if hasattr(aiter, 'scaled_fp8_quant'):
            _t = torch.ones(1, 1, device='cuda', dtype=torch.bfloat16)
            _r = aiter.scaled_fp8_quant(_t)
            if isinstance(_r, tuple) and len(_r) == 2:
                _quant_fn = aiter.scaled_fp8_quant
                return _quant_fn
    except Exception:
        pass

    def _manual_quant(tensor):
        finfo = torch.finfo(FP8_DTYPE)
        amax = tensor.abs().amax().clamp(min=1e-12)
        scale = amax / finfo.max
        fp8_out = (tensor / scale).clamp(min=finfo.min, max=finfo.max).to(FP8_DTYPE)
        return fp8_out, scale.to(torch.float32).reshape(1)

    _quant_fn = _manual_quant
    return _quant_fn


def _detect_mode():
    global _mode_detected, _use_a16w8
    if _mode_detected:
        return
    _mode_detected = True
    try:
        from aiter.jit.utils.chip_info import get_gfx
        if get_gfx() in ("gfx950", "gfx942"):
            _use_a16w8 = True
    except Exception:
        pass


def _get_cached_meta(batch_size, q_seq_len, total_kv, q_dtype, kv_dtype,
                     qo_indptr, kv_indptr, kv_last_page_len):
    cache_key = (batch_size, q_seq_len, total_kv, q_dtype, kv_dtype)
    if cache_key not in _meta_cache:
        info = get_mla_metadata_info_v1(
            batch_size, q_seq_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]
        get_mla_metadata_v1(
            qo_indptr, kv_indptr, kv_last_page_len,
            NUM_HEADS // NUM_KV_HEADS, NUM_KV_HEADS, True,
            work[0], work[2], work[1],
            work[3], work[4], work[5],
            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_dtype,
            dtype_kv=kv_dtype,
        )
        _meta_cache[cache_key] = {
            "work_meta_data": work[0],
            "work_indptr": work[1],
            "work_info_set": work[2],
            "reduce_indptr": work[3],
            "reduce_final_map": work[4],
            "reduce_partial_map": work[5],
        }
    return _meta_cache[cache_key]


def _get_cached_kv_indices(total_kv):
    if total_kv not in _kv_indices_cache:
        _kv_indices_cache[total_kv] = torch.arange(
            total_kv, dtype=torch.int32, device="cuda"
        )
    return _kv_indices_cache[total_kv]


def _get_cached_kv_last_page(batch_size, total_kv, kv_indptr):
    key = (batch_size, total_kv)
    if key not in _kv_last_page_cache:
        _kv_last_page_cache[key] = (kv_indptr[1:] - kv_indptr[:-1]).to(torch.int32)
    return _kv_last_page_cache[key]


def _get_cached_output(total_q):
    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"
        )
    return _output_cache[total_q]


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"]
    total_kv = int(kv_indptr[-1])

    _detect_mode()

    kv_buffer_fp8, kv_scale = kv_data["fp8"]
    kv_buffer_4d = kv_buffer_fp8.view(
        kv_buffer_fp8.shape[0], PAGE_SIZE, NUM_KV_HEADS, kv_buffer_fp8.shape[-1]
    )

    if _use_a16w8:
        q_input = q
        q_scale_val = None
        q_dt = torch.bfloat16
    else:
        quant_fn = _init_quant_fn()
        q_input, q_scale_val = quant_fn(q)
        q_dt = FP8_DTYPE

    kv_indices = _get_cached_kv_indices(total_kv)
    kv_last_page_len = _get_cached_kv_last_page(batch_size, total_kv, kv_indptr)
    o = _get_cached_output(q.shape[0])

    meta = _get_cached_meta(
        batch_size, q_seq_len, total_kv,
        q_dt, kv_buffer_fp8.dtype,
        qo_indptr, kv_indptr, kv_last_page_len,
    )

    mla_decode_fwd(
        q_input.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_val,
        kv_scale=kv_scale,
        intra_batch_mode=True,
        **meta,
    )
    return o
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