KernelBench hard deprecated · H100

FP8 GEMM Gemini 3.5 Flash

passdid not score
agent session46mtotal wall54mcheck4mbenchmark4moutput tokens—gpu-lock wait8mgpu-lock held17sregimecompute

Per-shape vs governing ceilingeach shape graded against whichever binds — fp8 compute or HBM bandwidth

4096×4096×40960.490 ms18.5%280 TFLOPS · 19% of 1,513 TF fp8 peak · also 0.14 TB/s (7% of HBM)
4096×4096×41273.698 ms2.5%37 TFLOPS · 2% of 1,513 TF fp8 peak · also 0.02 TB/s (1% of HBM)
32×8192×81920.134 ms2.1%0.51 TB/s · 25% of 2.0 TB/s HBM · also 32 TFLOPS (2% of compute)
4096×14336×40961.840 ms17.3%261 TFLOPS · 17% of 1,513 TF fp8 peak · also 0.10 TB/s (5% of HBM)

compute-bound memory-bound · bar + right column = official fraction of the ceiling (the geomean input)

geomean(18.5% · 2.5% · 2.1% · 17.3%) = 6.4%

Kernel source (redacted)
import torch
import torch.nn as nn
import triton
import triton.language as tl

@triton.jit
def fp8_gemm_kernel_fast(
    a_ptr, b_ptr, c_ptr,
    weight_scale_ptr,
    M, N, K,
    stride_am, stride_ak,
    stride_bn, stride_bk,
    stride_cm, stride_cn,
    BLOCK_SIZE_M: tl.constexpr, BLOCK_SIZE_N: tl.constexpr, BLOCK_SIZE_K: tl.constexpr,
    GROUP_SIZE_M: tl.constexpr,
    EVEN_M: tl.constexpr, EVEN_N: tl.constexpr, EVEN_K: tl.constexpr,
):
    pid = tl.program_id(axis=0)
    num_pid_m = tl.cdiv(M, BLOCK_SIZE_M)
    num_pid_n = tl.cdiv(N, BLOCK_SIZE_N)
    
    # Swizzling
    width = GROUP_SIZE_M * num_pid_n
    group_id = pid // width
    row_idx = group_id * GROUP_SIZE_M
    col_idx = pid % num_pid_n
    
    pid_m = row_idx + (pid % width) // num_pid_n
    pid_n = col_idx
    
    if pid_m >= num_pid_m:
        return

    offs_am = pid_m * BLOCK_SIZE_M + tl.arange(0, BLOCK_SIZE_M)
    offs_bn = pid_n * BLOCK_SIZE_N + tl.arange(0, BLOCK_SIZE_N)
    offs_k = tl.arange(0, BLOCK_SIZE_K)

    accumulator = tl.zeros((BLOCK_SIZE_M, BLOCK_SIZE_N), dtype=tl.float32)

    clamped_am = tl.where(offs_am < M, offs_am, 0)
    clamped_bn = tl.where(offs_bn < N, offs_bn, 0)

    if EVEN_K:
        # Fast path: pointers incremented outside loop
        a_ptrs = a_ptr + clamped_am[:, None] * stride_am + offs_k[None, :] * stride_ak
        b_ptrs = b_ptr + clamped_bn[None, :] * stride_bn + offs_k[:, None] * stride_bk
        for k in range(0, tl.cdiv(K, BLOCK_SIZE_K)):
            if not EVEN_M:
                mask_a = offs_am[:, None] < M
                a = tl.load(a_ptrs, mask=mask_a, other=0.0)
            else:
                a = tl.load(a_ptrs)
                
            if not EVEN_N:
                mask_b = offs_bn[None, :] < N
                b = tl.load(b_ptrs, mask=mask_b, other=0.0)
            else:
                b = tl.load(b_ptrs)
                
            accumulator = tl.dot(a, b, accumulator, out_dtype=tl.float32)
            a_ptrs += BLOCK_SIZE_K * stride_ak
            b_ptrs += BLOCK_SIZE_K * stride_bk
    else:
        # Safe path: pointers recomputed with clamping inside loop
        for k in range(0, tl.cdiv(K, BLOCK_SIZE_K)):
            offs_k_current = k * BLOCK_SIZE_K + tl.arange(0, BLOCK_SIZE_K)
            clamped_k = tl.where(offs_k_current < K, offs_k_current, 0)
            a_ptrs = a_ptr + clamped_am[:, None] * stride_am + clamped_k[None, :] * stride_ak
            b_ptrs = b_ptr + clamped_bn[None, :] * stride_bn + clamped_k[:, None] * stride_bk
            
            mask_a = (offs_am[:, None] < M) & (offs_k_current[None, :] < K)
            mask_b = (offs_bn[None, :] < N) & (offs_k_current[:, None] < K)
            a = tl.load(a_ptrs, mask=mask_a, other=0.0)
            b = tl.load(b_ptrs, mask=mask_b, other=0.0)
            accumulator = tl.dot(a, b, accumulator, out_dtype=tl.float32)

    if EVEN_N:
        scales = tl.load(weight_scale_ptr + offs_bn)
    else:
        scales = tl.load(weight_scale_ptr + clamped_bn, mask=offs_bn < N, other=1.0)

    accumulator = accumulator * scales[None, :]
    out = accumulator.to(tl.bfloat16)

    c_ptrs = c_ptr + clamped_am[:, None] * stride_cm + clamped_bn[None, :] * stride_cn
    if EVEN_M and EVEN_N:
        tl.store(c_ptrs, out)
    else:
        mask_c = (offs_am[:, None] < M) & (offs_bn[None, :] < N)
        tl.store(c_ptrs, out, mask=mask_c)

def run_triton_gemm(x, weight, weight_scale):
    M, K = x.shape
    N, K_w = weight.shape
    c = torch.empty((M, N), dtype=torch.bfloat16, device=x.device)
    
    if M <= 64:
        BLOCK_SIZE_M = 32
        BLOCK_SIZE_N = 128
        BLOCK_SIZE_K = 64
        GROUP_SIZE_M = 1
    else:
        BLOCK_SIZE_M = 128
        BLOCK_SIZE_N = 128
        BLOCK_SIZE_K = 64
        GROUP_SIZE_M = 8

    EVEN_M = (M % BLOCK_SIZE_M == 0)
    EVEN_N = (N % BLOCK_SIZE_N == 0)
    EVEN_K = (K % BLOCK_SIZE_K == 0)

    grid = lambda META: (triton.cdiv(M, BLOCK_SIZE_M) * triton.cdiv(N, BLOCK_SIZE_N),)

    fp8_gemm_kernel_fast[grid](
        x, weight, c,
        weight_scale,
        M, N, K,
        x.stride(0), x.stride(1),
        weight.stride(0), weight.stride(1),
        c.stride(0), c.stride(1),
        BLOCK_SIZE_M=BLOCK_SIZE_M,
        BLOCK_SIZE_N=BLOCK_SIZE_N,
        BLOCK_SIZE_K=BLOCK_SIZE_K,
        GROUP_SIZE_M=GROUP_SIZE_M,
        EVEN_M=EVEN_M,
        EVEN_N=EVEN_N,
        EVEN_K=EVEN_K,
    )
    return c

class Model(nn.Module):
    """y = ((x @ w.T) * weight_scale).to(bf16).

    x: fp8_e4m3 (M, K).  w: fp8_e4m3 (N, K) normalized to the e4m3 range.
    weight_scale: (N,) per-output-channel dequant scale.
    """

    def __init__(self, M: int, N: int, K: int):
        super().__init__()
        self.M, self.N, self.K = M, N, K
        self.register_buffer("weight", torch.empty(N, K, dtype=torch.float8_e4m3fn))
        self.register_buffer("weight_scale", torch.empty(N, dtype=torch.float32))

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return run_triton_gemm(x, self.weight, self.weight_scale)

def get_inputs():
    x = (torch.rand(4096, 4096) * 8 - 4).to(torch.float8_e4m3fn)
    return [x]

def get_init_inputs():
    return [4096, 4096, 4096]

20260618_050815_gemini_gemini-3.5-flash_01_fp8_gemm