KernelBench hard deprecated · H100
W4A16 GEMM GPT-5.5
passdid not score
agent session16mtotal wall22mcheck3mbenchmark3moutput tokens69,846gpu-lock wait5mgpu-lock held15sregimememory
Per-shape vs governing ceilingeach shape graded against whichever binds — bf16 compute or HBM bandwidth
1×12288×40960.081 ms16.3%0.33 TB/s · 16% of 2.0 TB/s HBM · also 1 TFLOPS (0% of compute)
32×12288×40960.113 ms12.1%0.25 TB/s · 12% of 2.0 TB/s HBM · also 29 TFLOPS (4% of compute)
256×12288×40960.200 ms8.6%129 TFLOPS · 17% of 756 TF bf16 peak · also 0.18 TB/s (9% of HBM)
1×4096×40960.071 ms6.2%0.13 TB/s · 6% of 2.0 TB/s HBM · also 0 TFLOPS (0% of compute)
16×14336×40960.095 ms16.4%0.33 TB/s · 16% of 2.0 TB/s HBM · also 20 TFLOPS (3% of compute)
compute-bound memory-bound · bar + right column = official fraction of the ceiling (the geomean input)
geomean(16.3% · 12.1% · 8.6% · 6.2% · 16.4%) = 11.1%
Kernel source (redacted)
from __future__ import annotations
import torch
import torch.nn as nn
import triton
import triton.language as tl
GROUP_SIZE = 128
@triton.jit
def _w4a16_gemv_kernel(
x_ptr,
w_ptr,
scales_ptr,
zeros_ptr,
out_ptr,
N: tl.constexpr,
K: tl.constexpr,
BLOCK_N: tl.constexpr,
):
pid_n = tl.program_id(0)
offs_n = pid_n * BLOCK_N + tl.arange(0, BLOCK_N)
offs_h = tl.arange(0, 64)
acc = tl.zeros((BLOCK_N,), tl.float32)
for g in tl.range(0, K // 128):
base_k = g * 128
x0 = tl.load(x_ptr + base_k + tl.arange(0, 128)).to(tl.float32)
xsum = tl.sum(x0, axis=0)
packed = tl.load(
w_ptr + (g * 64 + offs_h[None, :]) * N + offs_n[:, None],
mask=offs_n[:, None] < N,
other=0,
)
q_lo = (packed & 0x0F).to(tl.float32)
q_hi = ((packed >> 4) & 0x0F).to(tl.float32)
q = tl.interleave(q_lo, q_hi)
prod = tl.sum(q * x0[None, :], axis=1)
s = tl.load(scales_ptr + g * N + offs_n, mask=offs_n < N, other=0.0).to(tl.float32)
z = tl.load(zeros_ptr + g * N + offs_n, mask=offs_n < N, other=0.0).to(tl.float32)
acc += (prod - xsum * z) * s
tl.store(out_ptr + offs_n, acc.to(tl.bfloat16), mask=offs_n < N)
@triton.jit
def _w4a16_matmul_kernel(
x_ptr,
w_ptr,
scales_ptr,
zeros_ptr,
out_ptr,
M: tl.constexpr,
N: tl.constexpr,
K: tl.constexpr,
BLOCK_M: tl.constexpr,
BLOCK_N: tl.constexpr,
):
pid_m = tl.program_id(0)
pid_n = tl.program_id(1)
offs_m = pid_m * BLOCK_M + tl.arange(0, BLOCK_M)
offs_n = pid_n * BLOCK_N + tl.arange(0, BLOCK_N)
offs_k = tl.arange(0, 128)
offs_h = tl.arange(0, 64)
acc = tl.zeros((BLOCK_M, BLOCK_N), tl.float32)
for g in tl.range(0, K // 128):
base_k = g * 128
x = tl.load(
x_ptr + offs_m[:, None] * K + base_k + offs_k[None, :],
mask=offs_m[:, None] < M,
other=0.0,
)
xsum = tl.sum(x.to(tl.float32), axis=1)
packed = tl.load(
w_ptr + (g * 64 + offs_h[None, :]) * N + offs_n[:, None],
mask=offs_n[:, None] < N,
other=0,
)
q_lo = (packed & 0x0F).to(tl.float32)
q_hi = ((packed >> 4) & 0x0F).to(tl.float32)
q = tl.trans(tl.interleave(q_lo, q_hi)).to(tl.bfloat16)
prod = tl.dot(x, q, out_dtype=tl.float32)
s = tl.load(scales_ptr + g * N + offs_n, mask=offs_n < N, other=0.0).to(tl.float32)
z = tl.load(zeros_ptr + g * N + offs_n, mask=offs_n < N, other=0.0).to(tl.float32)
acc += (prod - xsum[:, None] * z[None, :]) * s[None, :]
tl.store(
out_ptr + offs_m[:, None] * N + offs_n[None, :],
acc.to(tl.bfloat16),
mask=(offs_m[:, None] < M) & (offs_n[None, :] < N),
)
class Model(nn.Module):
def __init__(self, M: int, N: int, K: int, group_size: int = GROUP_SIZE):
super().__init__()
assert K % group_size == 0
assert K % 2 == 0
self.M, self.N, self.K = M, N, K
self.group_size = group_size
n_groups = K // group_size
self.register_buffer("w_q", torch.empty((K // 2, N), dtype=torch.uint8))
self.register_buffer("scales", torch.empty((n_groups, N), dtype=torch.bfloat16))
self.register_buffer("zeros", torch.empty((n_groups, N), dtype=torch.bfloat16))
self._out = None
if M == 1:
self._block_m = 8
self._block_n = 16 if N <= 4096 else 32
self._num_warps = 4
self._grid = (1, triton.cdiv(N, self._block_n))
self.forward = self._forward_matmul
elif M == 16:
self._block_m = 64
self._block_n = 64
self._num_warps = 4
self._grid = (triton.cdiv(M, self._block_m), triton.cdiv(N, self._block_n))
self.forward = self._forward_matmul
elif M == 32:
self._block_m = 64
self._block_n = 32
self._num_warps = 4
self._grid = (triton.cdiv(M, self._block_m), triton.cdiv(N, self._block_n))
self.forward = self._forward_matmul
else:
self._block_m = 128
self._block_n = 64
self._num_warps = 4
self._grid = (triton.cdiv(M, self._block_m), triton.cdiv(N, self._block_n))
self.forward = self._forward_matmul
def _forward_gemv(self, x: torch.Tensor) -> torch.Tensor:
M = self.M
N = self.N
K = self.K
out = self._out
if out is None:
out = torch.empty((M, N), device=x.device, dtype=torch.bfloat16)
self._out = out
_w4a16_gemv_kernel[self._grid](
x,
self.w_q,
self.scales,
self.zeros,
out,
N,
K,
BLOCK_N=self._block_n,
num_warps=self._num_warps,
)
return out
def _forward_matmul(self, x: torch.Tensor) -> torch.Tensor:
M = self.M
N = self.N
K = self.K
out = self._out
if out is None:
out = torch.empty((M, N), device=x.device, dtype=torch.bfloat16)
self._out = out
_w4a16_matmul_kernel[self._grid](
x,
self.w_q,
self.scales,
self.zeros,
out,
M,
N,
K,
BLOCK_M=self._block_m,
BLOCK_N=self._block_n,
num_warps=self._num_warps,
num_stages=3,
)
return out
def forward(self, x: torch.Tensor) -> torch.Tensor:
return self._forward_matmul(x)
M = 1
N = 12288
K = 4096
def get_inputs():
x = torch.randn(M, K, dtype=torch.bfloat16)
return [x]
def get_init_inputs():
return [M, N, K]
20260618_044030_codex_gpt-5.5_07_w4a16_gemm