Does fp16 accumulation in llama.cpp's CUDA FlashAttention hurt long context?
On NVIDIA GPUs from Turing onward, llama.cpp's tensor core FlashAttention kernel keeps the attention weighted sum of value vectors in fp16. The model graph asks for fp32 accumulation for this operation, but the CUDA kernels do not read that request. fp16 carries 11 bits of precision, so once the running sum is large, small additions are rounded away. We measured what this does to model output at up to 128k tokens of context (131,072), what an fp32 accumulator costs, and whether the change at 128k can be predicted from shorter runs.
Short answer
- At 128k context the fp16 accumulator measurably changes the next token distribution of all six models we tested. Against an otherwise identical fp32 build, the KL divergence is 0.007 to 0.15 per token.
- The fp16 build also predicts the actual text worse. On Mistral-Nemo-Instruct-2407 the loss is 0.14 nats per token, about 15% higher perplexity on the scored text. On Llama-3.1-8B-Instruct it is 0.019 nats per token. For scale, on the same scored text, q4_K_M weights instead of f16 cost that model 0.026.
- How the change grows with length depends on the model. On Llama-3.1-8B the loss appears only between 64k and 96k. On Mistral Nemo the KL divergence has already grown 6.9 times between 2k and 32k.
- On RULER retrieval prompts the reference answer becomes less likely under fp16, but we could not show a change in the RULER score itself.
- fp32 accumulation made prompt processing an estimated 0.1% to 3.1% slower on the two models we timed, and generation speed was unchanged.
- Predicting the 128k change from runs at 32k and below worked only roughly: the forecast was within a factor of 2 on four of six models.
What the kernel does
FlashAttention walks through the keys in tiles. For each query it tracks a running maximum of the scores, a sum of exponentiated weights and a weighted sum of value vectors. When the maximum increases it rescales the two sums before adding the next tile, and at the end it divides the weighted value sum by the weight sum. In llama.cpp's CUDA kernel fattn-mma-f16.cuh, the query key products are accumulated in fp32, but the weighted value sum is held in an fp16 tile. To keep that sum from overflowing, every weight is scaled down by a factor of 8. That was the fix for llama.cpp issue 18606, a Granite model that failed at 200k context. It gives the sum eight times more headroom against overflow, but does not remove rounding error.
In the configurations we measured (head size 128, f16 KV cache, an Ada GPU), this kernel processes the prompt. For single token generation the choice depends on the model. Llama-3.1-8B uses a vector kernel that accumulates in fp32. Qwen2.5-7B, with 7 query heads per KV head, uses a small configuration of the same tensor core kernel that accumulates in fp16 in both of our builds. What changes during prompt processing also carries into generation, through the KV cache and the last prompt position.
On synthetic inputs run through the GPU instruction itself, a simple model reproduces the error closely: the sum is rounded to fp16 after every 8 keys. None of the rounding hypotheses in our preregistered test met its criterion. We found this model afterwards and checked it on 60 held out cells, where the device error divided by the emulated error ranged from 0.992 to 1.017 with a median of 1.000, although the outputs are not identical bit for bit. On real attention inputs at 8k and 32k, from one book and six layers per model, the median ratio of measured to emulated error per head is within 0.1% of one on all six models.
In synthetic diffuse attention, the relative error grew roughly with the square root of the number of keys. When the value vectors share a common direction the sum stagnates instead, and the relative error reached 0.33 at 65,536 keys.
What it does at long context
We built llama.cpp twice from the same commit: the stock kernel, and a version with an fp32 accumulator tile plus the changes it needs. The fp32 build is a reference, not exact attention: its inputs are still fp16. In every compared pair we checked that both builds split the work across the GPU identically, so the comparison isolates the accumulator. All models used Q8_0 weights and an f16 KV cache, on NVIDIA L40 and L40S GPUs. For each model we took 8 PG19 test books, cut windows of 2k to 128k tokens that end on the same text, and scored the last 256 positions of each window. This is a perplexity style measurement of the prompt, so here the whole effect comes from prompt processing.
| Model | KL at 32k | KL at 128k | Extra nats per token at 128k | Books where fp16 is worse |
|---|---|---|---|---|
| Llama-3.1-8B-Instruct | 0.00065 | 0.0101 | 0.019 | 8 of 8 |
| Qwen2.5-7B-Instruct, YaRN | 0.0016 | 0.0110 | 0.018 | 8 of 8 |
| Mistral-Nemo-Instruct-2407 | 0.0038 | 0.146 | 0.140 | 8 of 8 |
| Qwen3-8B, YaRN | 0.0017 | 0.0074 | 0.012 | 7 of 8 |
| Llama-3.2-3B-Instruct | 0.0011 | 0.0068 | 0.003 | 5 of 8 |
| Granite-3.1-8B-Instruct | 0.00085 | 0.0137 | 0.021 | 8 of 8 |
KL is the divergence of the fp16 build's next token distribution from the fp32 build's, averaged over positions and books. It measures how much the two distributions differ. Extra nats per token is the negative log likelihood of the actual next tokens under the fp16 build minus that under the fp32 build, so positive values mean the fp16 build predicts the text worse. The two YaRN models extend their context by a factor of 4, with YaRN active at every length.
- On Llama-3.1-8B, the preregistered primary test labelled the KL change at 128k material, and Qwen2.5-7B received the same label in the planned secondary analysis. On Llama-3.1-8B the 95% interval for the extra loss is 0.013 to 0.026 nats per token. The other four rows are descriptive measurements made under our forecasting protocols.
- On Llama-3.1-8B the KL at 128k is 7 times the KL between f16 and Q8_0 weights at 8k. Up to 64k the extra loss is about zero; it turns positive between 64k and 96k. The q4_K_M comparison above uses the same eight scored windows with stock attention, so it gives a scale, not an additive cost.
- On Mistral Nemo the loss is already 0.016 nats per token at 64k and 0.069 at 96k. Across its 8 books at 128k it ranges from 0.047 to 0.212. In the layers we inspected at 128k, 1.6% of its attention heads carry half of the error.
We also ran 260 RULER prompts at 128k on Llama-3.1-8B, scoring the reference answer by its negative log likelihood summed over its tokens. In the preregistered first stage of 130 prompts, the reference answer cost 0.254 more nats under fp16 (95% interval 0.043 to 0.460). Over all 260 prompts, as a descriptive figure, it was 0.258 (0.118 to 0.400), concentrated in the multi key needle tasks, variable tracking and one question answering task; the single needle tasks moved by less than 0.18 nats per answer. The RULER score was 72.6 for the stock build and 75.2 for the fp32 build, a difference of 2.6 points with a 97.5% interval from −0.4 to +5.7. That does not establish a change in retrieval scores.
What fp32 accumulation costs
| Model | Prompt 8k | Prompt 32k | Prompt 128k | Generation at 128k depth |
|---|---|---|---|---|
| Llama-3.1-8B-Instruct | +2.4% | +3.1% | +2.5% | about 0% |
| Qwen2.5-7B-Instruct | +0.1% | +2.5% | +3.1% | about 0% |
These are estimated extra times from llama-bench on an L40, with the two builds run in randomised blocks. At 128k the 95% intervals are 2.0% to 3.0% and 2.0% to 4.1%; at 8k and 32k they reach up to 5.9%, and for Qwen2.5-7B at 8k the interval includes no change. Generation means 128 tokens after a 128k context; for Qwen2.5-7B both builds generate through the fp16 configuration described above, so its generation figure says nothing about fp32 cost. The fp32 tile raised register use from 222 to 247 per thread, with local memory unchanged. We did not measure shorter prompts, other GPUs, other head sizes or quantized KV caches.
The fp32 build we measured lives on a research branch. A port to current llama.cpp is drafted but not yet compiled or tested. Before it could be proposed it needs a fix for a possible overflow in its final step, and a decision on larger head sizes, where an fp32 tile may run out of registers. Another possible design folds the fp16 partial sum into fp32 every 256 keys. It bounded the error on synthetic inputs; its accuracy and cost in the real kernel are unmeasured.
Can short runs predict the change at 128k?
This was the research question behind the measurements: whether the long context damage of a numerical shortcut can be forecast per attention head from short context data, instead of running 128k evaluations. Our method dumps the attention inputs at 32k and below, emulates both accumulators for each head, extends the context synthetically by resampling real key tiles, and scales each book's KL at 32k by the predicted growth of its error. The tolerance, fixed before the runs, was a factor of 2 at 128k.
- On all six models the measured KL at 128k is 2.4 to 11 times what a simple extrapolation of the short context trend gives. The forecast is closer than that extrapolation on five of six.
- The forecast passed its first blind preregistered test, on Mistral Nemo. Llama-3.1-8B and Qwen2.5-7B had already been measured, so for them the forecast is a retrodiction. Counting all six, it was within a factor of 2 on four.
- In a second blind round on three new models, the forecast was too high on all three, by 1.85, 1.96 and 3.47 times, and it missed on Qwen3-8B. That round's preregistered result is incomplete, because only three of the four planned models qualified, so its results are descriptive.
- Which heads carry the error was predicted well on Mistral Nemo, a preregistered claim that held. In the second round it held on two of three models.
- On real 128k inputs from the inspected layers of four models, the emulator's total error energy agrees with the kernel within 1%. That makes the synthetic long context the leading suspect: it overstates the error budget by 4.2 and 4.8 times on two of the new models. It does not explain everything, since on Granite it is close to real and the forecast is still almost twice too high. On Mistral Nemo, the overstatement and an understatement elsewhere in the method partly cancelled. A version designed to fix the synthetic context failed its own fidelity check and remains untested.
So short runs do flag that the change at 128k will lie far above the short context trend, but they do not yet predict its size reliably across model families.
Prior work
The closest work is ik_llama.cpp pull request 2510. It measured the fp16 accumulator of the Pascal tile kernel on a P100 against a CPU fp32 reference up to 32k, found the error growing with context, and added an opt in fold into fp32 every 16 keys; that change also moves the sum of weights to fp32. Similar folds appear in SageAttention2 and in a proposed FlashInfer change. llama.cpp maintainers are discussing how operations request precision in pull request 31821, which leaves FlashAttention for later. What we add is the tensor core kernel used on Turing and later GPUs with only the accumulator changed, measurements to 128k on six models, the retrieval measurement, an emulator checked against the kernel per head, and a preregistered, partly blind forecasting test whose magnitude forecasts did not hold up across model families.
Limits
- One GPU architecture (Ada), Q8_0 weights, an f16 KV cache, head size 128, and 8 PG19 books per model, scored on the last 256 positions.
- Long generated answers were not measured, except through RULER.
- Cost was timed on two models only.
Each gated test was written down and committed before its run, with its rule for passing. The protocols, raw results and analysis code are in a research repository that is not public yet. The work was built with a lot of help from Claude Code, and Codex for reviews and corrections. It is independent and has not been peer reviewed.