Repository: https://github.com/meshapplied/goethe
TLDR: we have the best kernels (with a straight face)
Last week, ThinkingMachines released the Inkling-2-Mini model (26B total and 5.4B active params). We thought, based on some (surface level, and absolutely untrustworthy) benchmark, should be fun to run it at 1000 tok/s and try it out for kernel generation (quite ironically), also some software engineering and math theorem proving in Lean (also a hot topic these days
with Navier-Stokes and Riemann, but also increasingly relevant in software!). We reached only about half of that in the end (at just above 500 tok/s) with heavy speculative decoder tuning, but our kernels took plain decoding to 212 tok/s (71% of that limit), and speculation took us past it. We released the kernels as a pack in our open source inference stack, Goethe (which runs any inference engine under the hood, including our lightweight, but quite robust engine, goethe-engine).
Figure 1: Main results: Decode tokens/sec at temperature=0.7 on one RTX 5090 32 GB. Full table with both temperatures and conditions at the end. For Code the input prompt was to develop algorithmic and data structure primitives like LRU cache, topological sort, a ring buffer, B-trees against LSM-trees benchmark, both in Python and C++; for Chat we asked it to generate a summary of the first 10 chapters of The Lord of the Rings.
We think of this model as a successor of the Qwen 3.8 27B which in NVFP4 format leaves space for quite comfortable context on an RTX5090 (32GB VRAM and 1.67 TB/s bandwidth). So we kept this card and optimized for it (in the end we released these kernels for many Nvidia cards in one single pack, available in Goethe). Decoding a token means reading every active weight once, about 5.5 GB in the layout we ended up with, so bandwidth alone caps plain decoding near 299 tokens/sec for one user. That arithmetic sets the speed limit. Our kernels took plain decoding to 212 tok/s (71% of that limit)
The first surprise came early. Programmatic dependent launch (PDL == CUDA feature that lets the next kernel launch and run its setup before the previous kernel has finished) is the usual answer to idle gaps between kernels. On an early build of our decode step it saved 0.09 ms (under 3%). Nearly everything else came from the kernels themselves, and from the shape of this particular model.
But back to Inkling, if you just care about the results and want to use try it out, try Inkling-2-Mini in Goethe:
goethe install thinkingmachines/Inkling-2-Mini
goethe run
We track one token (one step) through the model (a computational graph) and stop wherever we had to tweak a kernel, based on profiling and building models.
This writeup also contains many of our expectations and then the results. We hope it gives some of you insight into which relationships and primitives determine kernel performance and that in the end it comes down to exploiting structure of GPU and this will also should inform how we profile and determine the hyperparams. In other words it comes down to math. In theory we should be able to calculate what algorithmic primitives and operations, and in what orders yield to the lowest possible time and verifying shouldn’t be that hard right?
Small retrospective of kernel development
Actually we were quite naive when we first learned about GPUs and wrote small kernels for machine learning models. The time of a kernel run depends on parameters the source code doesn't show and even if you can measure them, you need to measure multiple times, but these params even depend on each other meaning nothing like linear independence (like the case in basis vectors) back from linear algebra class.We dont have other option
(more technical but keep reading!)
We dont have other option than to look at the cache layout and setup equations based on cache sizes and speeds (hierarchically) and to measure as much as possible (and as fast as possible) to setup e.g. the relationship between copy speed and the size of the data (flat while it fits in the 96 MB L2 cache, then down to the 1.67 TB/s of the DRAM). And the curves are coupled. A token streams 5.5 GB of weights through that 96 MB of cache, which throws out the KV cache and activations the next kernel needs, unless the weight loads are marked to leave the cache first. The memory only reaches full speed with about 750 KB of loads in flight, and how much a kernel keeps in flight depends on its registers, its number of CTAs and whether the previous kernel still occupies the SM (the goal with PDL will be to saturate this latter)! Even the number of experts a step reads is a curve (8 of 128 for one token, about 52 for 8 tokens and 112 for 32, because new tokens increasingly pick experts an earlier forward pass of 1 token already picked).
These params include latency, waves, the L2 cache, the power, the timings, SM count etc. Wins also don't add up (we saw this when moving one PDL trigger sped up one kernel and slowed the next one by the same amount). And every rigorous measurement takes minutes, while sampled output moves by up to 2x between seeds. This symbolically means an agent iterating in seconds mostly iterates on noise (inside the confidence intervals).
So we made the agents work against hard numbers (micro baselines == target per kernel derived from the card itself, a deliberately broken twin that every test has to catch, timings only at sustained clocks, and many small changes tried in parallel, of which only the faster and bit-identical (or within KL divergence tolerance, but that is input dependent!) ones stay).
### GPU essentials #
Cores
An RTX 5090 has 170 SMs (streaming multiprocessors == independent cores of the GPU). An SM runs threads in groups of 32 warps (warp == set of threads computing completely synchronized), and all threads of a warp execute the same instruction at the same time. A kernel is launched as a grid of CTAs (cooperative thread arrays == groups of up to 32 warps that share one SM’s registers and on-chip shared memory), and each CTA stays on its SM until it finishes.
Waves
The SMs take CTAs in waves (one round in which every SM receives its share). 170 CTAs that each fill an SM make exactly one wave. 200 such CTAs make two, and in the second one 30 SMs work while 140 wait. Occupancy (how many warps an SM keeps resident to hide latency) says how many CTAs fit on one SM. It says nothing about how many waves a grid needs.
Bandwidth and latency
Bandwidth (max VRAM copy speed) is 1,674 GB/s on our card, measured by copying 384 MB
(four times the 96 MB L2 cache). That is 93% of the 1,792 GB/s datasheet value, and every percentage in this writeup is against this measured number. Latency (the time a single load takes to return) is about 450 ns.
To reach full bandwidth, a kernel needs bandwidth * latency of loads in flight (by Little's law; 753 KB on 5090 card). A kernel that keeps less in flight cannot reach full bandwidth (a kind of lower limit), however fast its arithmetic is.
Roofline
Arithmetic intensity (floating point operations PER byte read (copied) from memory) decides which limit applies. Below roughly about 100 operations per byte the 5090 is limited by bandwidth, above it by arithmetic. Decode for one user does one multiply-add per weight, about one operation per byte of BF16 weight, so it sits far down on the bandwidth side. Verifying 8 tokens with the same weights does 8 times the arithmetic for the same bytes, which is why speculative decoding and batching cost far less than their token count suggests.
Tensor cores
Each SM has tensor cores (compute units that execute matrix multiplication on tiles (== sub matrices of the two to-be-multiplied large matrices == small number of rows * small number of cols) in one instruction, called MMA). They read BF16, FP8 (8-bit floats) and on this generation NVFP4 (4 bit floats with one shared 8 bit scale per 16 values). Inkling’s expert weights are NVFP4 and the rest of the model stays in BF16 (as the official weights).
One graph per token
Inkling has 24 layers and runs through them twice, so a token visits 48 layers before it reaches the head. At batch size 1 each visit is a dozen small kernels, and launching them one at a time from the CPU costs more than some of them take to run! We capture the whole step, all 48 visits, as one CUDA graph (a recorded sequence of kernels that the driver replays without the CPU) of about 550 nodes and replay it for every token.
Inside a graph, the gap between two kernels is already a few microseconds. That is all PDL can win back, which explains the 0.09 ms.
Launch overhead comes down to the CPU, so just minimize orchestration by the CPU.
A decode step at B=1 is hundreds of kernels of a few microseconds each, and the launch cost from the host is of the same size. Any model decoded at B=1 has this problem. A CUDA graph of the whole step removed it here, and PDL then had not much left to win us.
Projections
In a canonical transformer, each layer visit starts and ends with a projection. QKV multiplies the token's hidden state (one vector) by three weight matrices to get its query, key and value for attention. O-proj (output projection) maps the attention result back into the hidden state. For a single token both are GEMVs (matrix times vector), so every weight is loaded once and used for one multiply-add. Their time is the time it takes to read the weights.
Expectation. 90% or more of the measured 1,674 GB/s (our microbenchmarks of the same shapes reached that.Result. The O-projection stopped at 78%.
A very essential theorem describes how much the memory can hand back per latency. It's Little's law from queueing theory (on average, bytes in flight = bandwidth * latency, since each requested byte stays in flight for one latency). So delivered bandwidth is the bytes in flight divided by the load latency, capped at the peak (min(peak, in flight / latency)). Below the cap it grows linearly with what is in flight, and the peak is reachable only with at least peak * latency outstanding (1,674 GB/s x 450 ns, about 753 KB on the 5090 card). Our O-projection kept 589 KB in flight, which caps it at 589 / 753 = 78% of the peak, exactly what we measured (latency grows as the memory fills up, so 753 KB is a lower bound).
Two fixes looked plausible. More CTAs (576 CTAs of 2 warps) changed nothing (0.60 ms against 0.61 ms), because the total in flight stayed at 589 KB. Deeper prefetch worked. Each CTA now requests the next two tiles (the chunks of weights it multiplies next) while it computes the current one, which raised the amount in flight to 1.2 MB, above the 753 KB the memory needs.
QKV is now the largest single part of the step (1.33 ms of 4.72 ms), as expected for the largest dense weights of each visit.
Bytes in flight decide bandwidth. A memory-bound kernel runs at full speed only when bandwidth * latency (753 KB on the 5090) is outstanding at every moment. It is the first number to check when any GEMV, copy or attention kernel stalls below the ceiling, on any GPU (only the 753 KB changes). More CTAs do not help when the total in flight stays the same. Here a two-tile prefetch took the O-projection from 589 KB to 1.2 MB.
Attention
Verify attention (the 8 drafted tokens attending over the cached keys and values, the KV cache) went through seven versions. The times are the sum over all 48 layer visits, measured as a standalone kernel.
Figure 2: Verify attention for 8 tokens summed over 48 layer visits (standalone kernel with synthetic shapes: 4,096-token context and a 1,024-token sliding window. CTA: cooperative thread array.)
v1 (23.17 ms). One CTA per attention head. Inkling has 24 heads, so the grid had 24 CTAs, and 146 of the 170 SMs (86%) had no work.
v2 (2.85 ms). The KV cache of each head is split into chunks (16 for global attention, 8 for the sliding window), so several CTAs share one head, and a small second kernel combines their partial softmax results. More CTAs keep more SMs busy (8.1x).
v3 (not measured). One load of keys and values serves the 3 query heads that use it (grouped-query attention). It was folded into v4 before it was timed.
v4 (1.75 ms). Keys and values are stored in FP8. One 32-bit read now loads two of them and converts them to half2 (two 16-bit floats in one register), and one HFMA2 instruction (two fused multiply-adds at once) uses both. Each instruction does twice the work, and the three query heads still share the load.
v5 (1.45 ms). Two details of v4 forced the compiler to synchronize the warp. Its shuffles (register exchanges between the threads of a warp) used a mask that differed between the two halves of the warp, and they sat behind a branch (warp < T) that nvcc could not prove was the same for every thread. Together they produced 42 warp-wide barriers (WARPSYNC in the machine code, SASS). v5 keeps all 32 lanes converged. Out-of-range lanes compute a dummy score (-1e30) and stay in the shuffle with a constant full mask, and the guard became a warp-uniform vote (__all_sync). All 42 barriers disappeared.
The softmax changed in the same version. v4 computed two exponentials per key (one to rescale the running result and one for the new score) and rescaled the output at every step. v5 folds log2(e) into the score scale, so a score below the running maximum (the common case) costs one exp2f and no rescaling, and only a new maximum pays for a rescale.
v6 (reverted). Expectation. Letting the last CTA of each head do the combine would save a kernel launch and a round trip through memory. Result. Slower inside the full step at 8 tokens, because the combine became a serial tail (one CTA working after all the others had finished).
v7 (1.01 ms). The grid is sized to whole waves (8 KV heads x 21 splits = 168 CTAs, one wave on 170 SMs, for global attention and 336, two waves, for the sliding window). A grid of one and a half waves spends its last half-wave with most of the card idle, however high its occupancy.
In the shipped model, 40 of the 48 visits use a 128-token window (8 KV tiles, small enough for registers), so those visits need no split and no combine at all. In the shipped decode step attention takes 0.20 ms of 4.72 ms (4%).
We extracted three key relationships using our profile. How many CTAs run against how many SMs, what the SASS does that the source does not show, and how many waves the grid takes. Every attention version above came from one of them. A fusion pays only when the fused kernel keeps the parallelism of the kernels it replaces (v6 lost it).
Experts
Each token picks 8 of 128 experts (MoE, mixture of experts), and the expert weights are NVFP4. Our expert kernels store the 4-bit weights in the order the tensor cores consume them, so a load goes straight into an MMA without reshuffling. Measured on cold memory (no cache reuse between experts), they run at 96 to 98% of the card's measured bandwidth.
Speculative verify for several users changes the shape of the problem. With 8 users verifying 4 drafted tokens each, one step routes 32 tokens (256 expert assignments over 128 experts, about two tokens per expert).
Expectation. The grouped kernel used for prefill (64 token rows per tile) covers any token count.Result. With two tokens per expert, 62 of its 64 rows per tile were padding.
For up to 48 tokens we now route through a persistent kernel (a fixed set of resident CTAs that loop over the active experts until none are left). That made the 8-user verify 7.6% faster with unchanged outputs.
Tile size follows tokens per expert and tokens per expert collapse at small batch. A MoE layer that is compute-shaped at prefill is memory-shaped and sparse at decode, so one kernel cannot serve both well. We kept the prefill kernel above 48 tokens and a persistent kernel below.
Batching
Expectation. 8 users should cost little more than one, because the weights are read once per step (they can be reused on the go most of the time).
Initially we ran one of our kernels in our engine at B=8 and we got about 199 tokens/sec in total (slower than one user alone). Very bad feeling at first, but we patched it in about 2 hours of microbenchmarking first and then developing some missing primitives.
It was that each user ran its own forward pass, so one step read the active weights (about 5.5 GB) eight times, once per user. Almost every kernel already handled several tokens at once. Only four places needed to know which user a token belongs to (i) the short convolutions on keys and values, (ii) attention (each token reads its own cache up to its own position), (iii) the convolution after the O-projection and (iv) the convolution in the MLP. Each token now carries its slot (which user's KV cache and conv states it uses) and its position (how far that user's sequence has got). With that, the four state ops find each user's data, all 8 tokens run in one forward pass, and every other op reads its weights once for all 8. Aggregated decode at B=8 is at 712 tokens/sec without speculation and 1,041 tokens/sec at B=16, while every user gets exactly the tokens it would get alone.
One cliff remained. The dense projections tiled tokens in groups of 8, so between 9 and 16 tokens per step they launched two groups and read every weight twice. The new kernels load each weight tile once and issue two MMAs on it (tokens 0 to 7 and 8 to 15), which keeps QKV at 28.6 microseconds per visit at 16 tokens against 26 at 1 token.
Batching pays because a step reads every weight once, however many users share it. Weights are the same for every user and state is not (each user's KV cache, conv states and position). So only the few ops that touch per-user state need to know whose token is whose (slot and position), and every other op just sees more rows. The limit sits in the kernels (each one has a token count where it starts reading its weights twice, 8 for our first projections). MoE is the exception (remember the curve from the start, 8 of 128 experts for one token and already 52 for eight), so there more users always means more bytes to be moved.
Speculation
Inkling comes with its own drafter (a small multi-token prediction block in the official weights) that guesses several tokens ahead. Its head screens the 201K-token vocabulary in 2 bits first and scores only a shortlist of 1,024 candidates in full. The guesses form a short chain, the main model verifies them all in one step, and the round repeats.
Three mechanisms affect the performance of speculation dominately.
- No CPU in the loop. Drafting, verifying, accepting and the setup of the next round stay on the GPU, chained as a conditional CUDA graph (a graph that picks its next branch from a value on the device). A round never waits for the host.
- Short verifies when guesses will not survive. The drafter's probabilities predict how far a chain will be accepted, and we verify 1, 2, 4, 6 or 8 tokens accordingly. A hard sentence costs a short verify and an easy one gets the long one.
- Batches speculate too. With several concurrent requests, each drafts 4 tokens and all of them are verified in one step. At 8 users and temperature 0.7 that is 855 tokens/sec against 712 without the drafter.
We achieved smaller acceptance rates than SGLang's and vLLM's, so fewer drafted tokens survive per round (2.4 against 3.0 to 3.4 on chat at temperature 0.7).[1] Still, we come out ahead because a round is cheap. An avg round (inclusive drafting) costs 7.80 ms against 4.72 ms for a single step (measured at chat at temp=0.7) and delivers 2.4 tokens (instead of 1), because verifying more tokens reuses the same weight reads (QKV, O-proj and the LM head barely change).
Figure 3: Exclusive GPU time per part in milliseconds for 1 user at temperature 0.7. Kernels launched early with PDL mostly wait, so their raw durations add up to 7.8 ms inside a 4.7 ms step.
Figure 4: Total decode tokens/sec against concurrent users == batch size B (invariant numerics, mixed code and chat prompts). From B=2 on, for each request it drafts 4 (2.6 accepted at B=) fewer drafted tokens each than one user does, hence the decrease. Sixteen run without the drafter.
A very fast round of speculative drafter PLUS verification beats a long acceptance. Tok/s is accepted tokens divided by round time, and on a memory-bound model the round time grows slowly with the number of verified tokens. Measure the round before tuning the drafter. An average round of ours takes 1.65 times a plain step on chat and delivers 2.4 tokens.
Long context and invariance
The whole 256K context fits in 32 GB. The sliding-window visits keep a small ring per sequence, the global visits keep keys and values in FP8, and prefill runs in chunks with FlashAttention-style tiles. Prefill reaches 13,969 tokens/sec at 1K, 10,240 at 64K and 5,777 at 254K (a 254K prompt in 44 seconds, 27.6 GB peak).
A conversation's next turn reuses the prefix it already computed. With 4K tokens of history and a 64-token question, the first token comes after 16.6 ms instead of 345 ms. With 8K of history it comes after 22.9 ms instead of 660 ms.
Prefill in chunks normally changes the order of floating-point sums, and with it the output. Our default numerics are invariant (a prompt gives the same tokens whether it is prefilled whole or in pieces, at batch 1 or batch 8, and with the same seed sampling gives the same tokens). Every kernel keeps its reductions in the same order whatever the chunk or batch size, and each build is checked for it on the device before it ships.
Everything above runs the official Inkling-2-Mini weights with invariant numerics. Allowing reordered reductions (tolerance numerics) lifts the same workloads by 3 to 28% (at temperature 0.7, 529 tok/sec on code, 381 on chat and 910 for batch size of 8). We keep this as a secondary option in our kernels. An MXFP8 variant of the dense weights is on the way.
Make exactness something a test proves. Batch invariance, chunk invariance and seed-identical speculation are checked on the device for every build. That made it safe (and we could forget about it) to batch, chunk and speculate aggressively.
Benchmarks
| Workload | Temperature | Goethe | SGLang | vLLM | llama.cpp |
|---|---|---|---|---|---|
| Code (B=1) | 0.7 | 513 | 363 (1.43x) | 325 (1.58x) | 150 (3.44x) |
| Chat (B=1) | 0.7 | 314 | 269 (1.18x) | 212 (1.49x) | 95 (3.33x) |
| Mixed (B=8) | 0.7 | 855 | 767 (1.12x) | 730 (1.19x) | 261 (3.28x) |
| Code (B=1) | 1.0 | 455 | 359 (1.26x) | 346 (1.30x) | 142 (2.94x) |
| Chat (B=1) | 1.0 | 268 | 218 (1.24x) | 182 (1.48x) | 82 (3.15x) |
| Mixed (B=8) | 1.0 | 768 | 721 (1.07x) | 721 (1.07x) | 249 (2.98x) |
Table 1: Decode tokens/sec (mean over seeds, 1,024 generated tokens, top-p 1) on one RTX 5090 32 GB at sustained clocks. In brackets our speedup (the geometric mean of the per-seed ratios, with the same seeds for every engine).
Every engine runs its best speculative setup with the model's own drafter. SGLang and vLLM run the official weights. llama.cpp cannot read them and runs the closest published GGUF (4-bit UD-Q4_K_XL, with its drafter in Q8_0). SGLang and vLLM were capped at a 16K context and Goethe ran at its full 256K.
Each cell compares the engines on the same seeds, so a hard prompt is hard for every engine. Every speedup lies outside its 95% interval except one (8 users at temperature 1.0 against vLLM, 7% ahead on average and within noise). The chat cells and code at temperature 1.0 use 24 seeds, the 8-user cells 12 batches of 8 and code at temperature 0.7 6 seeds. Goethe ran its first and last repetitions on identical seeds, and the two agree within 0.34% (no thermal drift).
For one user (B=1), Goethe's first token arrives in 14 to 16 ms (against 23-29 ms for vLLM, 69-74 ms for SGLang and 141-159 ms for llama.cpp). It is behind on two axes. With 8 users at temperature 1.0 its first token takes 181 ms against 56 ms for vLLM, because prefill runs the sequences one after another to stay chunk-invariant. Its peak memory is 30.5 GB against 27.5 GB for vLLM.
Compare on the same seeds and say what does not win. Sampled throughput varies by up to a factor of two between seeds, so unpaired means hide real differences and invent others. Paired ratios with intervals separated the cells we win from the one we only lead.
Versions (goethe-engine 0.3.13, SGLang commit 4817721, vLLM 0.34.0rc1.dev (inkling2-mini branch), llama.cpp build 115deee, model revision ed40e7da, NVIDIA driver 580.178.04, CUDA 12.8).
Disclosure:
Every number here was measured on the card, with our own profiling, kernel generation and benchmarking tools. Gemini 4 Neon and Claude Opus 5.5 were involved in planning phases and code generation.
The optimised kernels work, but were not tested on, NVIDIA GPUs from Ampere onwards: Ampere (A100, A30, A40, A10, RTX A6000, RTX 3090), Ada (RTX 4090, RTX 6000 Ada, L40S, L40, L4), Hopper (H100, H200, GH200) and Blackwell (B200, GB200, B300, GB300, RTX PRO 6000 Blackwell.
Goethe accepts a drafted token only if the model would have produced it with the same seed, so two runs with the same seed always match. SGLang and vLLM use rejection sampling (accept a drafted token with probability min(1, p/q)), which keeps the output distribution without per-seed identity and accepts more drafted tokens on high entropy text. ↩︎