Skip to content
KernelIndex
Search⌘K

submission 683428

NinoHeather · python · License unknown

Use it

Vendorable · source mirrored · license unknownView source →

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

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

Reported · How evidence levels are derived →

Source and license

sourceavailable
revision digestsha256:a2feb46d933b86724a964ac93050006b4e92d69b25f58aa447541dd4f10ad997
license declaredunknown
license concludedunknown
authorsNinoHeather
imported2026-08-15

Kernel source

my_submission_demo.py133 lines
#!POPCORN leaderboard amd-mixed-mla
#!POPCORN gpu MI355X

"""
Two-stage MLA decode (stage1_asm + reduce_v1) with fp8 Q static quantization.
Per-shape tuned page_size / num_kv_splits. Eager buffer initialization.
All shapes use a8w8 path (fp8 Q + fp8 KV) for maximum throughput.
"""

import torch
from task import input_t, output_t
from aiter import dtypes as aiter_dtypes
from aiter import get_mla_metadata_info_v1, get_mla_metadata_v1
from aiter import mla_decode_stage1_asm_fwd, mla_reduce_v1

_USE_STATIC = False
try:
    from aiter.ops.quant import static_per_tensor_quant
    _USE_STATIC = True
except ImportError:
    pass
try:
    from aiter.ops.quant import dynamic_per_tensor_quant
    _HAS_DYN = True
except ImportError:
    _HAS_DYN = False

FP8 = aiter_dtypes.fp8
BF16 = torch.bfloat16
NH = 16
NKV = 1
DQ = 576
DV = 512
SM = 1.0 / (DQ ** 0.5)
_STATIC_SCALE = torch.tensor([0.1], dtype=torch.float32, device='cuda')

# (page_size, num_splits, is_sparse)
# ps: larger pages amortize page-table overhead for long kv
# ns: fewer splits = less reduce overhead; more splits = better kv parallelism
# sparse: helps with large batch + few splits metadata layout
_CFG = {
    (4, 1024):   (1, 8,  False),
    (4, 8192):   (8, 16, False),
    (32, 1024):  (1, 8,  False),
    (32, 8192):  (8, 12, False),
    (64, 1024):  (2, 4,  False),
    (64, 8192):  (8, 12, False),
    (256, 1024): (2, 1,  True),
    (256, 8192): (8, 8,  False),
}

_meta = {}

for (_bs, _kv), (_ps, _ns, _sp) in _CFG.items():
    _npb = _kv // _ps
    _tp = _bs * _npb

    _qo = torch.arange(_bs + 1, dtype=torch.int32, device='cuda')
    _kip = torch.arange(_bs + 1, dtype=torch.int32, device='cuda') * _npb
    _klp = torch.full((_bs,), _ps, dtype=torch.int32, device='cuda')
    _ki = torch.arange(_tp, dtype=torch.int32, device='cuda')

    _info = get_mla_metadata_info_v1(
        _bs, 1, NH, FP8, FP8,
        is_sparse=_sp, fast_mode=True,
        num_kv_splits=_ns, intra_batch_mode=True)
    _w = [torch.empty(s, dtype=t, device='cuda') for s, t in _info]
    _wm, _wi, _wis, _ri, _rfm, _rpm = _w

    get_mla_metadata_v1(
        _qo, _kip, _klp,
        NH // NKV, NKV, False,
        _wm, _wis, _wi, _ri, _rfm, _rpm,
        page_size=_ps, kv_granularity=max(_ps, 16),
        max_seqlen_qo=1, uni_seqlen_qo=1,
        fast_mode=True, max_split_per_batch=_ns,
        intra_batch_mode=True, dtype_q=FP8, dtype_kv=FP8)

    _np = _rpm.size(0)
    _meta[(_bs, _kv)] = {
        'qo': _qo, 'kip': _kip, 'klp': _klp, 'ki': _ki,
        'wm': _wm, 'wi': _wi, 'wis': _wis,
        'ri': _ri, 'rfm': _rfm, 'rpm': _rpm,
        'logits': torch.empty((_np, 1, NH, DV), dtype=torch.float32, device='cuda'),
        'lse': torch.empty((_np, 1, NH, 1), dtype=torch.float32, device='cuda'),
        'out': torch.empty((_bs, NH, DV), dtype=BF16, device='cuda'),
        'qbuf': torch.empty((_bs, NH, DQ), dtype=FP8, device='cuda'),
        'qscale': _STATIC_SCALE.clone(),
        'ps': _ps, 'ns': _ns,
    }


def custom_kernel(data: input_t) -> output_t:
    q, kv_data, _, _, config = data
    bs = config['batch_size']
    kv = config['kv_seq_len']
    tq = q.shape[0]

    m = _meta[(bs, kv)]
    kvf, kvs = kv_data['fp8']
    ps = m['ps']
    kv4 = kvf.view(kvf.shape[0] // ps, ps, NKV, DQ)

    qbuf = m['qbuf']
    qsc = m['qscale']
    if _USE_STATIC:
        static_per_tensor_quant(qbuf, q, _STATIC_SCALE)
    elif _HAS_DYN:
        dynamic_per_tensor_quant(qbuf, q.view_as(qbuf), qsc)
    else:
        finfo = torch.finfo(FP8)
        amax = q.abs().amax().clamp(min=1e-12)
        scale = amax / finfo.max
        qbuf.copy_((q / scale).clamp(finfo.min, finfo.max).to(FP8))
        qsc.fill_(scale.item())

    mla_decode_stage1_asm_fwd(
        qbuf, kv4,
        m['qo'], m['kip'], m['ki'], m['klp'],
        None,
        m['wm'], m['wi'], m['wis'],
        1, ps, NKV, SM,
        m['logits'], m['lse'], m['out'],
        qsc, kvs)

    if m['ns'] > 1:
        mla_reduce_v1(
            m['logits'], m['lse'],
            m['ri'], m['rfm'], m['rpm'],
            1, m['out'], None)

    return m['out'][:tq]
scrolls · 133 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