Skip to content
KernelIndex
Search⌘K

submission 531565

josusanmartin · python · License unknown

Use it

Vendorable · source mirrored · license unknownView source →

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

mxfp4_v144_v143_m32_bm8_bn64.py
curl "https://kernelindex.com/api/v1/implementations/kernelbot-amd-mxfp4-mm-531565?include=source"
interfacepython
Compatibility
measured onAMD Instinct MI355X
declared hardwareAMD Instinct MI355X
architecturesgfx950
dtypesbf16, mxfp4

Benchmark evidence

1 measurement across 1 GPU, fastest first.

Operation / workload
Hardware
Latency
Rank
Observed
AMD MXFP4 GEMMsuite of 6 cases
AMD Instinct MI355X
10.1µs
#248 of 1143
2026-03-11

Reported · How evidence levels are derived →

Source and license

sourceavailable
revision digestsha256:60df4dbd1cbf01c05ece36b736ed71229488c7c5fd655cd148bb57ab6b9731cc
license declaredunknown
license concludedunknown
authorsjosusanmartin
imported2026-08-15

Techniques

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

split-k- Goal: raise M=32 workgroup count without adding split-K overhead.
tile-m = 8- Public M=32 shapes use BM=8, BN=64, waves_per_eu=1, KSPLIT=1.
tile-n = 64- Public M=32 shapes use BM=8, BN=64, waves_per_eu=1, KSPLIT=1.

Kernel source

mxfp4_v144_v143_m32_bm8_bn64.py162 lines
#!POPCORN leaderboard amd-mxfp4-mm
#!POPCORN gpu MI355X

"""
Version 144: v143 + smaller M tiles for the two public M=32 shapes.
- Keep v143 everywhere else.
- Public M=32 shapes use BM=8, BN=64, waves_per_eu=1, KSPLIT=1.
- Goal: raise M=32 workgroup count without adding split-K overhead.
"""
import torch
import triton
import triton.language as tl
import aiter
from aiter import dtypes
from aiter.ops.gemm_op_a4w4 import gemm_a4w4_asm
from aiter.utility.fp4_utils import _dynamic_mxfp4_quant_kernel_asm_layout
from task import input_t, output_t


@triton.jit
def _mxfp4_quant_op_asm_exact(
    x,
    BLOCK_SIZE_N,
    BLOCK_SIZE_M,
    MXFP4_QUANT_BLOCK_SIZE,
):
    E8_BIAS: tl.constexpr = 127
    E2_BIAS: tl.constexpr = 1
    NUM_QUANT_BLOCKS: tl.constexpr = BLOCK_SIZE_N // MXFP4_QUANT_BLOCK_SIZE
    x = x.reshape(BLOCK_SIZE_M, NUM_QUANT_BLOCKS, MXFP4_QUANT_BLOCK_SIZE)
    amax = tl.max(tl.abs(x), axis=-1, keep_dims=True)
    amax = amax.to(tl.int32, bitcast=True)
    amax = (amax + 0x200000).to(tl.uint32, bitcast=True) & 0xFF800000
    amax = amax.to(tl.float32, bitcast=True)
    scale_e8m0_unbiased = tl.log2(amax).floor() - 2
    scale_e8m0_unbiased = tl.clamp(scale_e8m0_unbiased, min=-127, max=127)
    bs_e8m0 = scale_e8m0_unbiased.to(tl.uint8) + 127
    quant_scale = tl.exp2(-scale_e8m0_unbiased)
    qx = x * quant_scale
    qx = qx.to(tl.uint32, bitcast=True)
    s = qx & 0x80000000
    e = (qx >> 23) & 0xFF
    m = qx & 0x7FFFFF
    adjusted_exponents = tl.core.sub(E8_BIAS, e + 1, sanitize_overflow=False)
    m = tl.where(e < E8_BIAS, (0x400000 | (m >> 1)) >> adjusted_exponents, m)
    e = tl.maximum(e, E8_BIAS - E2_BIAS) - (E8_BIAS - E2_BIAS)
    e2m1_tmp = tl.minimum((((e << 2) | (m >> 21)) + 1) >> 1, 0x7)
    e2m1_value = ((s >> 28) | e2m1_tmp).to(tl.uint8)
    e2m1_value = tl.reshape(
        e2m1_value, [BLOCK_SIZE_M, NUM_QUANT_BLOCKS, MXFP4_QUANT_BLOCK_SIZE // 2, 2]
    )
    evens, odds = tl.split(e2m1_value)
    x_fp4 = evens | (odds << 4)
    x_fp4 = x_fp4.reshape(BLOCK_SIZE_M, BLOCK_SIZE_N // 2)
    return x_fp4, bs_e8m0.reshape(BLOCK_SIZE_M, NUM_QUANT_BLOCKS)


import aiter.ops.triton._triton_kernels.gemm.basic.gemm_a16wfp4 as _kernel_module
_kernel_module._mxfp4_quant_op = _mxfp4_quant_op_asm_exact

from aiter.ops.triton.gemm.basic.gemm_a16wfp4 import gemm_a16wfp4_preshuffle

_bf16 = dtypes.bf16
_fp4x2 = dtypes.fp4x2
_fp8_e8m0 = dtypes.fp8_e8m0

_kernel_32x128 = "_ZN5aiter41f4gemm_bf16_per1x32Fp4_BpreShuffle_32x128E"

_ASM_SPLITK = {
    (64, 7168, 2048): 2,
    (256, 3072, 1536): 1,
}

_FUSED_CONFIGS = {
    # M=4,N=2880: BLOCK_SIZE_N=64 for exact N fit (45 tiles), num_stages=2
    (4, 2880, 512): {
        "BLOCK_SIZE_M": 8, "BLOCK_SIZE_N": 64, "BLOCK_SIZE_K": 512,
        "GROUP_SIZE_M": 1, "num_warps": 4, "num_stages": 2,
        "waves_per_eu": 2, "matrix_instr_nonkdim": 16,
        "cache_modifier": ".cg", "NUM_KSPLIT": 1,
    },
    # M=16: BLOCK_SIZE_N=64 (exact fit for N=2112), num_stages=2, KSPLIT=7
    (16, 2112, 7168): {
        "BLOCK_SIZE_M": 16, "BLOCK_SIZE_N": 64, "BLOCK_SIZE_K": 512,
        "GROUP_SIZE_M": 1, "num_warps": 4, "num_stages": 2,
        "waves_per_eu": 1, "matrix_instr_nonkdim": 16,
        "cache_modifier": ".cg", "NUM_KSPLIT": 7,
    },
    (32, 4096, 512): {
        "BLOCK_SIZE_M": 8, "BLOCK_SIZE_N": 64, "BLOCK_SIZE_K": 512,
        "GROUP_SIZE_M": 1, "num_warps": 4, "num_stages": 2,
        "waves_per_eu": 1, "matrix_instr_nonkdim": 16,
        "cache_modifier": ".cg", "NUM_KSPLIT": 1,
    },
    (32, 2880, 512): {
        "BLOCK_SIZE_M": 8, "BLOCK_SIZE_N": 64, "BLOCK_SIZE_K": 512,
        "GROUP_SIZE_M": 1, "num_warps": 4, "num_stages": 2,
        "waves_per_eu": 1, "matrix_instr_nonkdim": 16,
        "cache_modifier": ".cg", "NUM_KSPLIT": 1,
    },
}

_QUANT_BLOCK = 32
_QUANT_TILE = 128
_bufs = {}


def _get_asm_bufs(m, k, n, device):
    x_fp4 = torch.empty((m, k >> 1), dtype=torch.uint8, device=device)
    sN = (k + _QUANT_BLOCK - 1) // _QUANT_BLOCK
    sN_pad = ((sN + 7) >> 3) << 3
    sM_pad = ((m + 255) >> 8) << 8
    scale = torch.empty((sM_pad, sN_pad), dtype=torch.uint8, device=device)
    padded_m = ((m + 31) >> 5) << 5
    out = torch.empty((padded_m, n), dtype=_bf16, device=device)
    return x_fp4, scale, sN, sN_pad, sM_pad, out, padded_m


@torch.inference_mode()
def custom_kernel(data: input_t) -> output_t:
    A, B, B_q, B_shuffle, B_scale_sh = data
    m, k = A.shape
    n = B.shape[0]
    key = (m, n, k)

    if key in _ASM_SPLITK:
        if key not in _bufs:
            _bufs[key] = ('asm', _get_asm_bufs(m, k, n, A.device))
        _, (x_fp4, scale, sN, sN_pad, sM_pad, out, padded_m) = _bufs[key]

        grid = ((m + _QUANT_TILE - 1) // _QUANT_TILE, sN_pad)
        _dynamic_mxfp4_quant_kernel_asm_layout[grid](
            A, x_fp4, scale,
            A.stride(0), A.stride(1),
            x_fp4.stride(0), x_fp4.stride(1),
            scale.stride(0), scale.stride(1),
            M=m, N=k, scaleN=sN,
            scaleM_pad=sM_pad, scaleN_pad=sN_pad,
            BLOCK_SIZE=_QUANT_TILE,
            MXFP4_QUANT_BLOCK_SIZE=_QUANT_BLOCK,
            SCALING_MODE=0, SHUFFLE=True,
        )

        splitK = _ASM_SPLITK[key]
        gemm_a4w4_asm(
            x_fp4.view(_fp4x2), B_shuffle, scale.view(_fp8_e8m0), B_scale_sh,
            out, _kernel_32x128,
            bpreshuffle=True, log2_k_split=splitK,
        )
        return out[:m]
    else:
        if key not in _bufs:
            _bufs[key] = ('fused', torch.empty((m, n), dtype=torch.bfloat16, device=A.device))
        _, out = _bufs[key]

        w = B_shuffle.view(torch.uint8).reshape(n // 16, k // 2 * 16)
        sm, sn = B_scale_sh.shape
        w_scales = B_scale_sh.view(torch.uint8).reshape(sm // 32, sn * 32)

        config = _FUSED_CONFIGS.get(key)
        return gemm_a16wfp4_preshuffle(A, w, w_scales, prequant=True, y=out, config=config)
scrolls · 162 lines total

Source code from GPU Mode and the KernelBot dataset · June 9 Researcher Reciprocity License v1.0

Changes from previous submission

Against this author's previous submission submission 531060.

⋯ 1 unchanged lines
#!POPCORN gpu MI355X
"""
- Version 103: Best combined - v99 + v101's M=4 BLOCK_SIZE_N=64 improvement.
- - M=4: BLOCK_SIZE_N=64 (exact fit for N=2880), num_stages=2 → 6.64µs (was 7.24)
- - M=16: BLOCK_SIZE_N=64, num_stages=2, KSPLIT=7 → 14.2µs
- - M=32: Default fused (no explicit config) → 9.5µs
- - M=64: ASM splitK=2 → 13.7µs
- - M=256: ASM splitK=1 → 12.5µs
- Expected geomean: ~10.5µs
+ Version 144: v143 + smaller M tiles for the two public M=32 shapes.
+ - Keep v143 everywhere else.
+ - Public M=32 shapes use BM=8, BN=64, waves_per_eu=1, KSPLIT=1.
+ - Goal: raise M=32 workgroup count without adding split-K overhead.
"""
import torch
import triton
⋯ 74 unchanged lines
"waves_per_eu": 1, "matrix_instr_nonkdim": 16,
"cache_modifier": ".cg", "NUM_KSPLIT": 7,
},
+ (32, 4096, 512): {
+ "BLOCK_SIZE_M": 8, "BLOCK_SIZE_N": 64, "BLOCK_SIZE_K": 512,
+ "GROUP_SIZE_M": 1, "num_warps": 4, "num_stages": 2,
+ "waves_per_eu": 1, "matrix_instr_nonkdim": 16,
+ "cache_modifier": ".cg", "NUM_KSPLIT": 1,
+ },
+ (32, 2880, 512): {
+ "BLOCK_SIZE_M": 8, "BLOCK_SIZE_N": 64, "BLOCK_SIZE_K": 512,
+ "GROUP_SIZE_M": 1, "num_warps": 4, "num_stages": 2,
+ "waves_per_eu": 1, "matrix_instr_nonkdim": 16,
+ "cache_modifier": ".cg", "NUM_KSPLIT": 1,
+ },
}
_QUANT_BLOCK = 32
scrolls · 37 diff lines total

Best evidence level for this revision: reported

JSON