submission 128999
cal.culus · python · License unknown
Use it
Vendorable · source mirrored · license unknownView source →
No package. Vendor the mirrored source: 106 lines, June 9 Researcher Reciprocity License v1.0.
nvfp4_gemm.py
curl "https://kernelindex.com/api/v1/implementations/kernelbot-nvfp4-gemm-128999?include=source"interfacepython
Compatibility
measured onNVIDIA B200
declared hardwareNVIDIA B200
architecturessm_100
dtypesfp8_e4m3, nvfp4
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:dd9efef470743a81486f1fb73f05e78911b33f8a635977f017812fbcc6a6ee54
license declaredunknown
license concludedunknown
authorscal.culus
imported2026-08-26
Techniques
Extracted from the mirrored source by pattern, never inferred. Each row cites its line.
fp4
Block-scaled FP4 GEMM using torch._scaled_mmKernel source
nvfp4_gemm.py106 lines
#!POPCORN leaderboard nvfp4_gemm
# This is a submission template for popcorn leaderboard 'nvfp4_gemm'.
# Your task is as follows:
# >
# > You will implement a block scaled matrix-matrix multiplication kernel optimized for NVIDIA B200.
# > To be explicit, you will be given a tuple of tensors:
# > ```
# > (a, b, sfa, sfb, c)
# > ```
# > where:
# > * `a` is M x K x L in K-major order in nvfp4(e2m1)
# > * `b` is N x K x L in K-major order in nvfp4(e2m1)
# > * `sfa` is M x (K // 16) x L in K-major order in fp8(e4m3fnuz)
# > * `sfb` is N x (K // 16) x L in K-major order in fp8(e4m3fnuz)
# > * `c` is M x N x L in fp16
# >
# > Matrix sizes `M` is divisible by mma_tiler_mn[0], `N` is divisible by mma_tiler_mn[1], `K` is divisible by 256.
# > The ranking criteria is the geometric mean of the benchmark results.
# > For the grand price, your kernel will be evaluated against the speed of light analysis
# > and the solution closest to the speed of light will be awarded the grand price.
# > ```
# > The speed of light analysis based on the max(FP4 Tensor Core math throughput, DRAM memory throughput) of B200 and tested under 1.5Ghz clock:
# > M N K L time[us]
# > 128 7168 16384 1 8.994
# > 128 4096 7168 1 2.354
# > 128 7168 2048 1 1.333
# > ```
# The deadline for this leaderboard is 2025-12-20 06:59:00+00:00
# You can automatically route this file to specific GPUs by adding a line
# `#!POPCORN gpus <GPUs>` to the header of this file.
# Happy hacking!
from task import input_t, output_t
import torch
def ceil_div(a, b):
"""Helper for ceiling division"""
return (a + b - 1) // b
def to_blocked(input_matrix):
"""Convert scale factor tensor to blocked format for torch._scaled_mm"""
rows, cols = input_matrix.shape
n_row_blocks = ceil_div(rows, 128)
n_col_blocks = ceil_div(cols, 4)
padded = input_matrix
blocks = padded.view(n_row_blocks, 128, n_col_blocks, 4).permute(0, 2, 1, 3)
rearranged = blocks.reshape(-1, 4, 32, 4).transpose(1, 2).reshape(-1, 32, 16)
return rearranged.flatten()
def custom_kernel(data: input_t) -> output_t:
"""
Block-scaled FP4 GEMM using torch._scaled_mm
Args:
data: Tuple that expands to:
a: torch.Tensor[float4e2m1fn] of shape [m, k, l],
b: torch.Tensor[float4e2m1fn] of shape [n, k, l],
sfa: torch.Tensor[float8_e4m3fnuz] of shape [m, k // 16, l],
sfb: torch.Tensor[float8_e4m3fnuz] of shape [n, k // 16, l],
sfa_permuted: torch.Tensor[float8_e4m3fnuz] of shape [32, 4, rest_m, 4, rest_k, l],
sfb_permuted: torch.Tensor[float8_e4m3fnuz] of shape [32, 4, rest_n, 4, rest_k, l],
c: torch.Tensor[float16] of shape [m, n, l]
Returns:
Tensor containing output in float16
c: torch.Tensor[float16] of shape [m, n, l]
"""
# c: [m, n, l] is pre-allocated memory to avoid timing allocation overhead.
a, b, sfa, sfb, sfa_permuted, sfb_permuted, c = data
# Get dimensions
m, k, l = a.shape
n = b.shape[0]
# Process each layer using torch._scaled_mm
for layer in range(l):
# Convert scale factors to blocked format
scale_a_blocked = to_blocked(sfa[:, :, layer])
scale_b_blocked = to_blocked(sfb[:, :, layer])
# Use torch._scaled_mm for FP4 GEMM
# (m, k) @ (n, k).T -> (m, n)
result = torch._scaled_mm(
a[:, :, layer],
b[:, :, layer].transpose(0, 1),
scale_a_blocked.cuda()
if scale_a_blocked.device.type != "cuda"
else scale_a_blocked,
scale_b_blocked.cuda()
if scale_b_blocked.device.type != "cuda"
else scale_b_blocked,
bias=None,
out_dtype=torch.float16,
)
c[:, :, layer] = result
return c
scrolls · 106 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