Models & Algorithms•SOTAAZ Lab••KR

llama.cpp b12888: 40k idle tokens nearly doubled q8_0 decode time in another slot

On one A100 with -np 2 -kvu --no-cache-idle-slots and a q8_0 KV cache, 40k idle tokens raised decode time in the other slot from 6.16 to 11.88 ms per token on b12888. The measured same-type caches stayed flat from b12889; mixed q8_0/q4_0 still slowed down on the default b12902 build.

llama.cpp b12888: 40k idle tokens nearly doubled q8_0 decode time in another slot

We kept 40,000 tokens from one conversation in a shared q8_0 KV cache while another request generated text. On llama.cpp b12888, that request took 11.88 ms per token, up from 6.16 ms with the other slot empty. The idle tokens made decode 1.9 times slower. On b12889, both cases took 5.79 ms per token. This test kept idle KV data on the GPU with --no-cache-idle-slots.

The q4_0 cache changed more, from 6.22 to 16.68 ms. f16 stayed flat on all three builds. On the latest build we tested, b12902, the mixed q8_0-key/q4_0-value cache still slowed down with idle tokens. Compiling the vector kernel for that pair removed the slowdown in our runs.

All speeds here are from one A100 80GB PCIe. The PR author reported the same pattern on an RTX PRO 6000 with smaller absolute times, which we compare below.

Why an idle slot affected decode

llama-server -np 2 opens two slots, so two conversations can be served at once. Each conversation keeps its keys and values (the KV cache) on the GPU so earlier tokens are not recomputed. With -kvu (unified KV cache), both slots share one cache buffer instead of splitting it in half. A long conversation can then use more than half of the cache while the other slot is short.

A KV cache can be stored in lower precision to save memory: -ctk q8_0 -ctv q8_0 stores keys and values in 8-bit blocks, about half the size of f16, and q4_0 stores them in about a quarter.

The slowdown required a shared cache, idle KV data retained in it, and a K/V type that took the conversion path. When one slot decodes a token, flash attention needs that slot's rows from the shared buffer. Through b12888, the CUDA path for these non-f16 types converted the whole K/V view to f16 on each decode step, including rows belonging to idle slots. A longer idle context therefore added work to every new token.

Commit 5b1ba66 (b12889, PR #32520) lets the vector flash-attention kernel read the listed rows directly for K/V pairs with a compiled vector kernel. The default build includes q4_0/q4_0, q8_0/q8_0, f16/f16 and bf16/bf16. A mixed pair without a compiled kernel retains the conversion path. A later commit, 386e414 (b12902, PR #33427), limits f16 conversion to the listed rows for q8_0 data.

How we measured it

We built llama-server at three points: b12888 (the last build before the fix), b12889 (the fix) and b12902 (the follow-up). We also made a fourth build of b12902 with the q8_0/q4_0 pair added to GGML_CUDA_FA_QUANTS. The model was Qwen3-4B at Q4_K_M, the model named in PR #32520's own measurement, fully on one A100.

Every server ran with -c 81920 -np 2 -kvu -fa on -ngl 99, with -ctk and -ctv set to the cache type under test. We started a fresh server for each build and cache type.

  • Slot 0, the idle conversation: a fixed text of 0, 16,000, 34,000 and then 40,000 tokens, sent with one generated token so it stays in the cache.
  • Slot 1, the measured request: a 164-token prompt that generates 256 tokens at temperature 0, three times at each idle level after one discarded warm-up.
  • The number: the server's own decode time per token for slot 1 (timings.predicted_per_token_ms), as the median of three. Within a level the three runs differed by at most 0.16 ms.

We passed --no-cache-idle-slots, as in the measurement command in PR #32520. Without it, llama-server moves an idle slot's KV data to its RAM prompt cache and clears those cells from the unified GPU cache when another request arrives. In one check without the flag, q8_0 on b12888 changed from 6.19 to 6.51 ms at 34,000 idle tokens. With the flag, it changed from 6.18 to 11.11 ms. Every result below uses the flag.

The server caps each slot at the model's training length, 40,960 tokens for Qwen3-4B, so 40,000 idle tokens was our top level.

Before and after, by cache type

Four line panels, decode ms per token in slot 1 against idle tokens in slot 0 (0, 16k, 34k, 40k), b12888 in orange and b12902 in blue. f16: both flat at 5.5. q8_0: b12888 rises from 6.2 to 11.9, b12902 stays at 5.8. q4_0: b12888 rises from 6.2 to 16.7, b12902 stays at 5.8. bf16: b12888 rises from 5.8 to 12.7, b12902 stays at 5.3.
K/V cacheb12888, 0 idleb12888, 40k idleb12902, 0 idleb12902, 40k idle
f165.495.455.475.47
q8_06.1611.885.785.79
q4_06.2216.685.795.82
bf165.7812.695.345.35

Decode milliseconds per token in slot 1, median of three, one A100 80GB PCIe.

On b12888, every 1,000 idle tokens added about the same amount to each new token in slot 1: 0.14 ms for q8_0, 0.17 ms for bf16 and 0.26 ms for q4_0. That is the 40k value minus the 0 value, divided by 40. The line is close to straight in the figure. f16 never paid this cost because its path did not need a conversion.

From b12889 on, the same three cache types stayed within 2% of their own 0-idle value at every level we measured. b12889 and b12902 matched within 2% for these types.

The fix also helped with an empty idle slot. At 0 idle tokens, q8_0 went from 6.16 to 5.78 ms (-6.2%), q4_0 from 6.22 to 5.79 ms (-7.0%), and bf16 from 5.78 to 5.34 ms (-7.6%).

After the fix, bf16 was the fastest cache at 5.34 ms, ahead of f16 at 5.47 ms. q8_0 and q4_0, at 5.78-5.79 ms, were about 6% slower than f16, in exchange for about half or a quarter of the cache memory.

In PR #32520, the author reports the same model on an RTX PRO 6000 with 34,000 idle tokens and 128 generated tokens. Their q8_0 numbers were 3.14 ms empty and 7.97 ms idle before the fix, and 3.02 ms idle after it. Our A100 shows the same pattern at larger absolute values: q8_0 took 6.16 ms with an empty slot 0 and 11.02 ms at 34,000 idle tokens on b12888, then 5.80 ms on b12902.

Mixed q8_0 keys and q4_0 values

Some setups keep keys at q8_0 and values at q4_0 to save more memory, since keys feed the attention scores and values do not. This pair is not in the default kernel list, so b12889 left it on the old path.

Line chart for -ctk q8_0 -ctv q4_0, decode ms per token against idle tokens. b12888 and b12889 overlap, rising from 6.2 to 14.0-14.1 at 40k. b12902 with default kernels rises from 6.2 to 11.7. b12902 built with the q8_0-q4_0 kernel stays at 5.8 at every level.

On b12888 and b12889, the mixed cache went from 6.2 to 14.0 and 14.1 ms per token at 40,000 idle tokens. The two builds were within 0.3% of each other at every level. b12902 lowered the cost but did not remove it: 6.21 to 11.71 ms. The PR for b12902 says it converts only the listed rows for a q8_0 cache. Our reading of that description is that the q8_0 keys stop converting the whole view while the q4_0 values still do, which fits a partial drop.

When we built b12902 with -DGGML_CUDA_FA_QUANTS="q4_0-q4_0;q8_0-q8_0;f16-f16;bf16-bf16;q8_0-q4_0", the mixed cache stayed at 5.79 to 5.80 ms at every idle level. The same build ran q8_0/q8_0 and f16/f16 at the same speeds as the default b12902 (within 0.4%).

Three idle slots or one

We also ran -np 4 with the same 34,000 idle tokens split across three slots, about 11,300 each, with a q8_0 cache. On b12888, the fourth slot decoded at 11.01 ms per token, against 11.02 ms when all 34,000 sat in one slot under -np 2. On b12902 it was 5.79 ms with three idle slots and 5.80 ms with one. In this check, the cost followed the total idle tokens in the shared cache, not how many conversations held them.

Running it yourself

Check the build number with llama-server --version. The same-type fix landed in b12889. For a custom build, check that the K/V pair you use has a compiled vector kernel.

bash
llama-server --version
# version: 1.1.0-dev (build 12902, commit 386e41466)

The server command we measured, with a q8_0 cache:

bash
llama-server -m Qwen3-4B-Q4_K_M.gguf -c 81920 -np 2 -kvu -fa on -ngl 99 \
  -ctk q8_0 -ctv q8_0 --no-cache-idle-slots --port 18571

To compile the mixed q8_0/q4_0 kernel, add the pair to the CUDA build:

bash
cmake -B build -DGGML_CUDA=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_CUDA_ARCHITECTURES=80 \
  "-DGGML_CUDA_FA_QUANTS=q4_0-q4_0;q8_0-q8_0;f16-f16;bf16-bf16;q8_0-q4_0"
cmake --build build --target llama-server -j

-DCMAKE_CUDA_ARCHITECTURES=80 targets the A100; set your own card's value or leave it out. Our build machine also had an older NCCL, so we added -DGGML_CUDA_NCCL=OFF. That flag only matters for multi-GPU runs.

To see the effect on your own server, start it with --no-cache-idle-slots (without it, slot 0 is cleared when the next request arrives), fill one slot with a long prompt ("id_slot": 0, "n_predict": 1, "cache_prompt": true), then send a short request to the other slot with "id_slot": 1, "n_predict": 256 and "ignore_eos": true, and read timings.predicted_per_token_ms from the response. Compare it with the same request while slot 0 is empty.

What to do on your server

If your server uses -kvu and retains idle KV data on the GPU with --no-cache-idle-slots, check its build and K/V pair. On our A100, b12889 removed the 1.9x to 2.7x slowdown at 40,000 idle tokens for q8_0/q8_0, q4_0/q4_0 and bf16/bf16; their decode times stayed within 2% of the empty-slot value at every level tested. The default b12902 build still slowed down with mixed q8_0/q4_0. Compiling that pair in GGML_CUDA_FA_QUANTS, or choosing a same-type pair, avoided the measured slowdown.

The next llama.cpp measurement will go out through the newsletter below.