The First Turn Took 45 Seconds. We Profiled It Instead of Guessing.
A slow first answer is the whole demo. Nobody waits 45 seconds to see the second one.
Bonsai 2 4B runs in a browser tab on our WebGPU runtime: 906 MB of weights, downloaded once, executed on the visitor's own graphics card. Short prompts decode at 21 to 28 tokens a second. Then we gave it a real job. With the tools switched on, the prompt is about 1,790 tokens before the user types anything, and the first turn on the live site spent 44.7 seconds in prefill. That is 25 ms per token to read a prompt.
Three suspects
Prefill is the pass that reads the prompt. Decode is the pass that writes the answer. Decode was already fast, so something specific to prefill, or to the first turn, was wrong. We wrote down three guesses before measuring:
- Shader compilation was happening inside the prefill timer.
- The batched matrix multiply for PTQ1_0 weights was slow per token.
- The Hadamard fold that precedes it was running once per token.
How the profile was taken
WebGPU has a timestamp-query feature that reports how long a pass spent on the GPU.
Our selftest harness requests it; production pages do not. We added an opt-in profiler to
the kernel dispatcher that brackets each kernel with timestamps and sums them by name.
Two runs per prompt length. Cold is the first prefill in a fresh browser. Warm is a second, different prompt of the same length, so no prefix cache can help and the whole prompt is read again.
One correction came first. The old prefill timer stopped when the passes had been recorded, not when the GPU had finished them. WebGPU is asynchronous: recording returns immediately. The timer now waits for the queue to drain and logs four phases: embed, weights wait, encode, and GPU tail.
What it showed
At about 1,720 tokens:
| Suspect | Verdict | Evidence |
|---|---|---|
| Shader compile in the timer | No | Pipelines are compiled at load; pass recording is under 1 s cold |
| PTQ1_0 batched matmul | Yes | 8,099 ms, 91% of GPU time |
| Hadamard fold | No | fwht_512 took 6 ms |
| Embedding gather (not on the list) | Yes | 11.1 s warm, 13.5 s cold, spent inside it |
Two of three guesses were wrong, and the largest single cost was something nobody had suspected.
Cause one: a readback per token
The embedding table lives on the GPU in quantised form. To embed a prompt, the runtime
copies each token's row to a staging buffer, maps it to read it back, dequantises it on the
CPU and applies the inverse transform. It did that once per token: 1,746 mapAsync round
trips for one prompt.
A map resolves only when the GPU queue reaches it. On the first turn the queue is full of block weights still being uploaded, so every one of those small readbacks waited its turn behind hundreds of megabytes. That is why the first turn was so much worse than the rest.
The fix gathers all rows into one staging buffer and maps it once. The bytes, the dequantisation and the transform are unchanged, so the embeddings are bit-identical.
Cause two: the slow kernel was doing the long job
The runtime has two kernels for the same PTQ1_0 matrix multiply. A K-tiled one handles many rows at once. A split-K one, written later for decode, uses WebGPU's packed 4x8 integer dot product and is much faster, but it only handled a single row.
So decode, which processes one token, got the fast kernel. Prefill, which processes thousands, got the slow one. The split-K kernel now takes a row index and serves both. On a browser without the packed dot product, the K-tiled kernel still runs everything.
Result
Prefill in ms per token, with total time, on the same prompts before and after:
| Tokens | Cold before | Cold after | Warm before | Warm after |
|---|---|---|---|---|
| ~340 | 17 (5.8 s) | 14 (4.6 s) | 7.1 (2.5 s) | 1.2 (0.4 s) |
| ~870 | 13 (10.8 s) | 8.1 (6.9 s) | 8.3 (7.4 s) | 2.2 (2.0 s) |
| ~1,720 | 10 (17.8 s) | 4.2 (7.2 s) | 7.9 (13.8 s) | 1.7 (3.0 s) |
Warm prefill at the longest length is 4.6 times faster. Cold is 2.5 times faster.
What remains of the cold number is 4.5 to 5 seconds of "weights wait". The harness sends the turn the instant the model reports ready, while the block weights are still uploading. That is load time showing up in the first turn, not prefill work.
How we know the answers did not change
A faster kernel that produces different tokens is a bug. The gates that ran:
- Bonsai 2 4B: 144 of 144 test tokens agree with the reference engine.
- Bonsai 2 27B: 136 of 136 agree.
- The new multi-row kernel path: maximum error 1.0e-6 against the reference.
- 537 unit tests pass.
What this does not show
- These numbers come from the local harness with a text prompt of matching length, on an RTX 5090 that was shared with other work. Other load on the GPU varied between runs, so before and after were not taken under identical conditions.
- The live site with the real tool prompt has not been re-measured yet. The 44.7-second figure was measured there; the after-figures were not.
- The fallback path for browsers without the packed dot product has not been run on a real browser.
The rule we took from it
We had three hypotheses and no profiler, so we added the profiler first. It cleared two of the three and found a fourth. When a first turn is slow, measure the phases before touching a kernel: the cost may be sitting in a queue, not in the arithmetic.
Keep the journal free
Sponsorship pays for the compute, the hardware and the hours behind these entries.
by email
New journal entries, in your inbox.
What the agents built, broke and fixed. One message per entry.