LLM-written GEMM kernels on an RTX 4090: none beats cuBLAS, and the reference has a trap
Several projects now publish GPU kernels written by LLM agents, with speedups measured on the hardware they were written for. FlashInfer-Trace is one: it ships, among much else, fp16 GEMM solutions for the four linear layers of Llama-3.1-8B, written by claude-opus-4.1, gemini-2.5-pro, gpt-5 and o3, one CUDA and one Triton solution per model and shape, all declared for B200.
I wanted to know whether any of them helps a serving engine on a consumer card. So I ran all 32,
exactly as published, against F.linear (cuBLAS, what vLLM runs for these layers) on an RTX 4090,
at the decode batch sizes where a custom kernel has its best chance: M = 1, 8 and 32.
The short answer: 19 of the 32 compile, run, give correct results and survive CUDA-graph capture. None of them is faster than cuBLAS on any shape at any batch size. The best kernel that is the solution’s own code reaches 0.52x, 0.79x, 0.49x and 0.92x of cuBLAS on the four shapes. And on two shapes at M = 32, the reference itself failed the correctness check, because of a PyTorch default.
The setup
The material is FlashInfer-Trace at commit da915083: 32 solutions, used exactly as published.
Their source bytes are written out and hashed, and nothing is repaired; a missing #include is an
outcome, not a bug for me to fix.
| Shape | Layer | Weight |
|---|---|---|
| N = 6144, K = 4096 | QKV projection | 48 MiB |
| N = 28672, K = 4096 | gate and up projection | 224 MiB |
| N = 4096, K = 4096 | attention output | 32 MiB |
| N = 4096, K = 14336 | down projection | 112 MiB |
Each solution computes C = A @ B.T in fp16, A of shape [M, K] and B of shape [N, K]. One
measurement cell is one solution at one M in one round, in its own process on the GPU:
- Correctness: 20 random fp16 inputs and 5 inputs whose B is a real weight matrix of the model,
each within rtol = atol = 1e-2 of
F.linear(FlashInfer-Bench’s default tolerance). - Time: 200 pairs,
F.linearthen the solution, each call preceded by a 192 MiB write that flushes the 72 MB L2, each call timed with CUDA events. The speedup is s = median cuBLAS time / median solution time. - Capture: the solution runs inside a CUDA graph and the replay must equal the eager output, because that is how vLLM runs decode.
- Own kernel: a solution counts as its own code only if the kernel in its nsys trace is one it defines. Seven solutions define no kernel they launch; they call cuBLAS, cuBLASLt or ATen. I report these wrappers separately.
Five rounds, each running all 96 cells (32 solutions x 3 batch sizes). A solution would count as faster only with s above 1.05 in all five rounds. A round was accepted only if no other process used the GPU, other processes used at most 2% of each CPU socket, and the GPU clock stayed within 3% of the first round’s.
What happened to the 32 solutions
- Seven do not compile for sm_89, all for reasons in their own source:
printfwithout<cstdio>, CUDA stream and guard helpers used without their headers, awmma::store_matrix_syncoverload error, a stray<![CDATA[as the first line, a pybind11 annotation mismatch, a call to a function that does not exist. These are exactly the seven that FlashInfer-Bench itself recorded asCOMPILE_ERRORon B200. - One compiles but cannot start: it asks for more dynamic shared memory per block than the RTX 4090 allows (about 99 KB, against about 227 KB on B200). It passed on B200.
- One gives wrong results at every batch size, as it does in FlashInfer-Bench’s B200 traces.
- Four are correct but cannot be captured in a CUDA graph: two create a cuBLAS handle or
cuBLASLt descriptors on every call, and two call
torch.cuda.synchronize()inside the kernel call. One of the latter ran past the 600-second compile limit from an empty Triton cache in the first two rounds, and compiled in the last three from the cache those rounds left. - Nineteen are correct and capturable: 16 own kernels and 3 wrappers.
None is faster
| Shape | best own kernel, M = 1 / 8 / 32 |
|---|---|
| attention out (32 MiB) | 0.52 / 0.52 / 0.52 |
| QKV (48 MiB) | 0.73 / 0.75 / 0.79 |
| down (112 MiB) | 0.38 / 0.46 / 0.49 |
| gate and up (224 MiB) | 0.80 / 0.92 / 0.92 |
Values are five-round medians of s for own kernels that are correct and capturable (at M = 32 on the first two shapes these kernels are counted as neither correct nor incorrect; see “The trap in the reference”). The highest single-round s of any cell, of any kind, is 1.016, and that is a wrapper calling cuBLAS. The wrappers that run the same cuBLAS kernel measure 1.000, which also shows that the solution’s slot in the timing loop carries no penalty of its own.
FlashInfer-Bench’s B200 traces report 6-107x for some of these solutions at small batch sizes. Those factors are against its own reference timing, not against cuBLAS, so they cannot be compared with the numbers here.
There was room
I expected few wins because cuBLAS would already run these decode-sized GEMMs near the card’s memory bandwidth. It does not:
After the L2 flush, cuBLAS streams the weights at 559 to 866 GB/s, 55% to 86% of the RTX 4090’s 1,008 GB/s specification, and below 64% on the two small shapes. A kernel that streamed those weights at 90% of the specification would be about 1.4x to 1.6x faster than cuBLAS on them. The published kernels did not get there: they were written and tuned for B200 (every solution declares B200 as its target, and 22 of the 32 sources name B200 in their comments). Whether that room can be taken on this card is a separate question this audit does not answer.
The trap in the reference
At M = 32, on the attention-output and QKV shapes, nine kernels failed the random-input checks in
every round: 20 of 20 random inputs out of tolerance, while all 5 real-weight inputs passed. Before
calling nine kernels from four different models wrong, I checked the reference: F.linear and one
of the failing kernels against a float64 product of the same fp16 inputs, 100 random inputs per
condition.
The reference was at fault. At M = 32 on these two shapes, cuBLAS picks a split-K kernel. With
PyTorch’s default, torch.backends.cuda.matmul.allow_fp16_reduced_precision_reduction = True, the
partial sums are reduced in fp16 (the trace shows splitKreduce_kernel reading __half partials),
and F.linear then fails every one of the 100 inputs against float64, with a worst error of 5.8 and
4.8 times the tolerance. With the flag set to False, the attention-output shape keeps split-K but
reduces in fp32, the QKV shape gets an unsplit kernel, and F.linear passes all 100 on both. The kernel I tested passed all 100 against float64 either way. At M = 8
cuBLAS uses a single GEMM kernel and everything passes.
The errors are few and specific: at most 0.22% of the output elements of a failing input, all with a true value near zero, where the partial sums cancel and the fp16 rounding of each partial stays in the result.
This behaviour is documented. PyTorch’s notes on
reduced precision reduction in fp16 GEMMs
describe the flag and its cost in precision, and a maintainer explains the trade-off in
pytorch/pytorch#116870. What it does to a
kernel benchmark is less obvious: if F.linear with default settings is the correctness reference,
a correct kernel can be marked incorrect at exactly the decode batch sizes where cuBLAS switches to
split-K. In this audit I count those nine as neither correct nor incorrect; every one of the nine was 0.17x to 0.79x of cuBLAS, so the conclusion does not depend on them.
For a benchmark: set the flag to False for the reference, or compare against a float64 product.
These numbers hold for the 32 fp16 GEMM solutions of FlashInfer-Trace at da915083, on an RTX
4090 with PyTorch 2.13 and its bundled cuBLAS, at M = 1, 8 and 32.
Reproduce
The audit is at rockyshi-dev/gen-kernel-audit. Its README has the commands for every step above.