submission 709280
Ningning Zhao · python · License unknown
Use it
Vendorable · source mirrored · license unknownView source →
No package. Vendor the mirrored source: 186 lines, June 9 Researcher Reciprocity License v1.0.
submission.py
curl "https://kernelindex.com/api/v1/implementations/kernelbot-amd-mxfp4-mm-709280?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
Reported · How evidence levels are derived →
Source and license
sourceavailable
revision digestsha256:370ba7fcfc3df62de387705f96770d5aa724be4712d972f33495b59a7136fe3a
license declaredunknown
license concludedunknown
authorsNingning Zhao
imported2026-08-26
Techniques
Extracted from the mirrored source by pattern, never inferred. Each row cites its line.
fp4
FP4 quant + FP4 GEMM reference: bf16 A, MXFP4 B -> MXFP4 per-1x32 quant A -> gemm_a4w4 -> bf16 C.Kernel source
submission.py186 lines
"""
FP4 quant + FP4 GEMM reference: bf16 A, MXFP4 B -> MXFP4 per-1x32 quant A -> gemm_a4w4 -> bf16 C.
Quant logic follows aiter op_tests/test_gemm_a4w4.py (get_triton_quant(QuantType.per_1x32)).
NOTE: Explicitly uses dynamic_mxfp4_quant from aiter.ops.triton.quant (patched in #975)
rather than going through aiter.get_triton_quant, which may dispatch to the
unpatched fp4_utils.py kernel. See ROCm/aiter#974, ROCm/aiter#975.
"""
import torch
from task import input_t, output_t
from utils import make_match_reference
from aiter import QuantType,dtypes
import aiter
from aiter.ops.shuffle import shuffle_weight
from aiter.ops.triton.quant import dynamic_mxfp4_quant # #975-patched kernel
from aiter.utility.fp4_utils import e8m0_shuffle
# K must be divisible by 64 (scale group 32 and fp4 pack 2)
SCALE_GROUP_SIZE = 32
def _quant_mxfp4(x, shuffle=True):
x_fp4, bs_e8m0 = dynamic_mxfp4_quant(x)
if shuffle:
bs_e8m0 = e8m0_shuffle(bs_e8m0)
return x_fp4.view(dtypes.fp4x2), bs_e8m0.view(dtypes.fp8_e8m0)
def generate_input(m: int, n: int, k: int, seed: int):# -> input_t:
"""
Generate random bf16 inputs A [m, k], B [n, k] and quantized MXFP4 B, shuffled B and B_scale.
Returns:
Tuple of (A, B), both bf16 on cuda.
"""
assert k % 64 == 0, "k must be divisible by 64 (scale group 32 and fp4 pack 2)"
gen = torch.Generator(device="cuda")
gen.manual_seed(seed)
A = torch.randn((m, k), dtype=torch.bfloat16, device="cuda", generator=gen)
B = torch.randn((n, k), dtype=torch.bfloat16, device="cuda", generator=gen)
B_q, B_scale_sh = _quant_mxfp4(B, shuffle=True)
# shuffle B(weight) to (16,16) tile coalesced
B_shuffle = shuffle_weight(B_q, layout=(16, 16))
return (A, B, B_q, B_shuffle, B_scale_sh)
def run_torch_fp4_mm(
x: torch.Tensor,
w: torch.Tensor,
x_scales: torch.Tensor,
w_scales: torch.Tensor,
dtype: torch.dtype = torch.bfloat16,
) -> torch.Tensor:
"""
PyTorch reference: dequant MXFP4 + E8M0 scale -> f32 -> mm -> dtype.
Same logic as aiter op_tests/test_gemm_a4w4.run_torch.
x: [m, k//2] fp4 packed, w: [n, k//2] fp4 packed
x_scales: [m, k//32] E8M0, w_scales: [n, k//32] E8M0
Returns: [m, n] in dtype
"""
from aiter.utility import fp4_utils
m, _ = x.shape
n, _ = w.shape
# fp4 packed -> f32
x_f32 = fp4_utils.mxfp4_to_f32(x)
w_f32 = fp4_utils.mxfp4_to_f32(w)
# E8M0 scale: [*, k//32] -> repeat 32 along k -> f32
x_scales = x_scales[:m].repeat_interleave(SCALE_GROUP_SIZE, dim=1)
x_scales_f32 = fp4_utils.e8m0_to_f32(x_scales)
x_f32 = x_f32 * x_scales_f32
w_scales = w_scales[:n].repeat_interleave(SCALE_GROUP_SIZE, dim=1)
w_scales_f32 = fp4_utils.e8m0_to_f32(w_scales)
w_f32 = w_f32 * w_scales_f32
return torch.mm(x_f32, w_f32.T).to(dtype)[:m, :n]
def ref_kernel(data: input_t) -> output_t:
"""
Reference: MXFP4 per-1x32 quant on A and B; both PyTorch ref and gemm_a4w4 are given.
Returns gemm_a4w4 for check_implementation.
"""
A, B, B_q, B_shuffle, B_scale_sh = data
A = A.contiguous()
B = B.contiguous()
m, k = A.shape
n, _ = B.shape
# 1) PyTorch impl just for your reference: dequant fp4 + e8m0 -> f32 -> mm -> bf16
# Per-1x32 MXFP4 quant
# A_q, A_scale = _quant_mxfp4(A, shuffle=False)
# B_q, B_scale = _quant_mxfp4(B, shuffle=False)
# gemm_a4w4 expects A [M,K/2], B [N,K/2] as dtypes.fp4x2; A_scale/B_scale [*,K/32] E8M0
# quant_func returns scale as dtypes.fp8_e8m0; gemm_a4w4 accepts E8M0, no view to uint8 needed
# slice to exact shapes [m,k_scale] / [n,k_scale] (quant may return padded scale)
# k_scale = k // SCALE_GROUP_SIZE
# A_scale = A_scale[:m, :k_scale].contiguous()
# B_scale = B_scale[:n, :k_scale].contiguous()
# out_torch = run_torch_fp4_mm(A_q, B_q, A_scale, B_scale, torch.bfloat16)
# 2) aiter.gemm_a4w4 path: needs shuffled B_q and shuffled scales (see test_gemm_a4w4.py:102-105)
A_q, A_scale_sh = _quant_mxfp4(A, shuffle=True)
# to be noted, aiter also has other a4w4 implements using triton, https://github.com/ROCm/aiter/blob/main/aiter/ops/triton/gemm/basic/gemm_afp4wfp4.py
out_gemm = aiter.gemm_a4w4(
A_q,
B_shuffle,
A_scale_sh,
B_scale_sh,
dtype=dtypes.bf16,
bpreshuffle=True,
)
return out_gemm
def custom_kernel(data: input_t) -> output_t:
"""
Optimized FP4 GEMM: MXFP4 per-1x32 quant + gemm_a4w4 for MI355X.
🔑 Key Optimizations:
1. Minimal unpacking - only use required inputs
2. Contiguous memory layout for coalesced access
3. Pre-trim scale tensors to avoid kernel padding overhead
4. bpreshuffle=True to skip redundant weight shuffle (~10% speedup)
5. Avoid redundant dtype conversions
Args:
data: tuple (A, B, B_q, B_shuffle, B_scale_sh) from generate_input
- A: [m, k] bf16 activation (to be quantized)
- B_shuffle: [n, k//2] fp4x2 pre-shuffled weight
- B_scale_sh: [n, k//32] fp8_e8m0 pre-shuffled scales
Returns:
[m, n] bf16 tensor = dequant(A) @ dequant(B).T
"""
# === Step 1: Unpack only needed inputs ===
A, _, _, B_shuffle, B_scale_sh = data # Skip unused B, B_q
m, k = A.shape
# B_shuffle: [n, k//2] fp4x2, B_scale_sh: [n, k//32] fp8_e8m0
# === Opt 1: Ensure contiguous memory for efficient access ===
A = A.contiguous()
B_shuffle = B_shuffle.contiguous()
# === Opt 2: Quantize A with shuffle=True for gemm layout compatibility ===
A_q, A_scale_sh = _quant_mxfp4_opt(A, shuffle=True)
# === Opt 3: Trim scales to exact dimensions (remove quant padding) ===
k_scale = k // SCALE_GROUP_SIZE # = k // 32
# Conditional slice: only copy if actually padded
if A_scale_sh.shape[1] != k_scale:
A_scale_sh = A_scale_sh[:, :k_scale]
if B_scale_sh.shape[1] != k_scale:
B_scale_sh = B_scale_sh[:, :k_scale]
# === Opt 4: Ensure contiguous after slicing ===
A_scale_sh = A_scale_sh.contiguous()
B_scale_sh = B_scale_sh.contiguous()
# === Opt 5: Call optimized kernel with bpreshuffle=True ===
# Critical: B_shuffle already has (16,16) coalesced layout,
# so skip kernel-internal shuffle to save ~10% overhead
out = aiter.gemm_a4w4(
A_q, # [m, k//2] fp4x2
B_shuffle, # [n, k//2] fp4x2, pre-shuffled
A_scale_sh, # [m, k//32] fp8_e8m0
B_scale_sh, # [n, k//32] fp8_e8m0
dtype=dtypes.bf16, # Output in bf16 for compatibility
bpreshuffle=True, # ⚡ Skip internal shuffle (B already shuffled)
)
# === Opt 6: Minimal dtype enforcement (avoid redundant cast) ===
if out.dtype != torch.bfloat16:
out = out.to(torch.bfloat16)
return out
def _quant_mxfp4_opt(x: torch.Tensor, shuffle: bool = True):
"""
Optimized MXFP4 quantization with optional scale shuffle.
Inlined for self-contained submission.
"""
x_fp4, scales = dynamic_mxfp4_quant(x)
if shuffle:
scales = e8m0_shuffle(scales)
return x_fp4.view(dtypes.fp4x2), scales.view(dtypes.fp8_e8m0)
check_implementation = make_match_reference(custom_kernel, rtol=1e-02, atol=1e-02)
scrolls · 186 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