You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Eval bug: cache-reuse not effective in qwen3-next #18497
then a request with a context of 4096 tokens is sent, kv looks like this with each letter being 512 tokens long block:
AAAABBBB
Then a new request is sent with the same content except for an extra block of 512 tokens inserted in the center:
AAAACBBBB
Result with Qwen3-Next:
Context gets fully reporccesed:
slot update_slots: id 3 | task 173 | forcing full prompt re-processing due to lack of cache data (likely due to SWA or hybrid/recurrent memory, see https://github.com/ggml-org/llama.cpp/pull/13194#issuecomment-2868343055)
Result with Qwen3:
512 tokens get processed, the rest is filled from shfited kv cache.
I also tried with --ctx-checkpoints 128 but this did not change the behavior, im not sure how this feature is supposed to work.
The problem is the recurrent state, not the KV cache. You will get checkpoints after each processed prompt, so if a checkpoint matches, content will get reprocessed from there. But the recurrent state doesn't work like the KV cache. It only keeps a single state that gets updated with each token. Therefore, the only way this could work if we added arbitrary snapshot checkpoints during prompt processing, for example each N tokens.
This seems to be the issue I'm experiencing too. I send 100k tokens of context (which stays the same) + and additional 3k tokens of instruction at the end (which changes on each run), and it always rebuilds the whole thing rather than the last 3k. I switched back to using another model for the task because its taking so long to do the prompt processing on each pass.
The --cache-reuse and --swa-flag flags do not do anything for recurrent models, so you can remove them for Qwen3 Next.
If you have control over the client, you can chunk the input prompt in smaller pieces so that the server can create checkpoints and reduce the amount of recomputation for shared prefixes.
Therefore, the only way this could work if we added arbitrary snapshot checkpoints during prompt processing, for example each N tokens.
Yes, this is quite easy to implement. It will likely increase the host-memory cache size, but no way around this. Contributions are welcome.
If you have control over the client, you can chunk the input prompt in smaller pieces so that the server can create checkpoints and reduce the amount of recomputation for shared prefixes.
I've tried chunking with a python script and it still no luck. This script adds 5 chunks, followed by an instruction, and the cache works. When the instruction is modified, the cache has to rebuild.
The problem is the recurrent state, not the KV cache. You will get checkpoints after each processed prompt, so if a checkpoint matches, content will get reprocessed from there. But the recurrent state doesn't work like the KV cache. It only keeps a single state that gets updated with each token. Therefore, the only way this could work if we added arbitrary snapshot checkpoints during prompt processing, for example each N tokens.
I see, so the clue I was missing is that qwen3-next has recurrent elements, I just red a paper on deltanet. The behavior makes sense with that in mind. I think better diagnostics would be good when non-applicable flags are used, its a bit mutch to expect the user to know the architecture details of eatch model used.
Reacted by Piotr Wilkin (ilintar), porterchild and KeenZ
I experience similar issues with LM Studio which currently uses llama.cpp b7721 and qwen3-next-80b. Cache reuse is very random, and it frequently discards the entire cache and recomputes it, which makes using large contexts almost impossible.
For example, during my recent test I used a system prompt 1071 tokens long, followed by 40 tokens for the first user prompt. The initial prompt processing worked as expected when the cache is empty:
When I tried to regenerate that reply (so the same system prompt and user prompt), I got this:
2026-01-16 14:09:49 [DEBUG]
Looking for non-prefix contiguous prompt sequences of size >= 256 to reuse from cache
Cache reuse summary: 1124/1124 of prompt (100%), 1124 prefix, 0 non-prefix
Failed to remove tokens from cache, clearing cache instead.
Then after reply regeneration, which produced 2566 tokens, I enter a new 9 token prompt. When generating the next reply, it doesn't match the entire cache, possibly due tokenization issues like those I reported in the past for DeepSeek models. But even the part that matches, the first 1781 tokens out of 3759, can't be used, because the entire cache is discarded again:
This doesn't happen all the time though. As I progress with the conversation, sometimes it does reuse the cache before generating replies, which start instantly with no delay.
However, even in those situations, if I try to regenerate that reply again it will usually discard the entire cache and reprocess the entire conversation, including the system prompt, from scratch, even though it used the cache just fine with the same prompt to generate the original reply that I'm regenerating.
I didn't try to reproduce this without LM Studio yet. But, from what I read here, it's unlikely I would get a different behavior.
So, my question is, is this particular behavior expected for this type of model? Because the longer the conversation progresses, the more painful it becomes to have the entire cache discarded.
The "cache reuse" functionality is different from what you describe.
You are thinking about prefix/prompt caching. This works correctly with llama-server, so if you experience any issue with it, feel free to create a bug report with repro steps.
Reacted by Vladimir Nicolici and Piotr Wilkin (ilintar)
@vnicolici Again: Qwen3Next is a hybrid model. It has recurrent (linear) layers. Recurrent layers don't have a KV cache. They use a single recurrent state which is updated for each token. Therefore, besides the snapshotting which I mentioned above, recurrent states can't do "prefix" cache'ing because they don't keep any sequences :)
@ggerganov Since linear cache takes so much less space than normal KV-cache, it might be reasonable to add a separate snapshot option just for linear layers, because non-linear layers can do normal prefix cache reuse. The logic might be a bit more complicated (since we'd have to check for "existing snapshot + proper prefix"), but it would actually enable something like say an LRU-cache of 2000 linear snapshots done every 100 generated tokens (parametrizable with say --linear-snapshot-cache-size and --linear-snapshot-cache-frequency). Then, for hybrid models, we could use this hybrid snapshot + prefix caching mechanism.
@othermod Regarding your earlier comment - I missed the part that you performed your test using LM Studio. If you run the same test with llama-server, you will see that it already works as expected:
So my #18497 (comment) that chunking does not work with /v1/chat/completions is not fully correct. Just make sure that your "chunked" requests have max_tokens=0 to avoid generating stuff during the chunking process and it should work as I described earlier in #18497 (comment).
The llama.cpp server has advanced prefix caching techniques that are not yet available in downstream projects.
@pwilkin Yes, this is quite easy to achieve. The logic for deciding when to make a checkpoint is here:
Same error here too with Qwen3.5 model. Tried to use --swa-full option, not worked for me as comments above already said.
I did flash-attn off since I'm using 140 iGPU. Flash attention makes operation slower on intel's iGPU.
I'm now experiencing the same issue with unsloth/qwen3.5-397b-a17b-gguf/qwen3.5-397b-a17b-mxfp4_moe-00001-of-00006.gguf .
The --swa-full flag seems to have no effect, the behavior is the same both before and after adding the flag, so I'm guessing the model is similar to qwen3-next in that regard.
This is how the log looks when it happens:
600.39.716.877 I srv params_from_: Chat format: peg-constructed
600.39.718.796 I slot get_availabl: id 3 | task -1 | selected slot by LCP similarity, sim_best = 0.904 (> 0.100 thold), f_keep = 0.896
600.39.719.348 I slot launch_slot_: id 3 | task -1 | sampler chain: logits -> ?penalties -> ?dry -> ?top-n-sigma -> top-k -> ?typical -> top-p -> min-p -> ?xtc -> temp-ext -> dist
600.39.719.352 I slot launch_slot_: id 3 | task 20986 | processing task, is_child = 0
600.39.719.357 I slot update_slots: id 3 | task 20986 | new prompt, n_ctx_slot = 262144, n_keep = 0, task.n_tokens = 116515
600.39.719.405 W slot update_slots: id 3 | task 20986 | n_past = 105331, slot.prompt.tokens.size() = 117565, seq_id = 3, pos_min = 117564, n_swa = 1
600.39.719.596 W slot update_slots: id 3 | task 20986 | forcing full prompt re-processing due to lack of cache data (likely due to SWA or hybrid/recurrent memory, see https://github.com/ggml-org/llama.cpp/pull/13194#issuecomment-2868343055)
600.39.719.633 W slot update_slots: id 3 | task 20986 | erased invalidated context checkpoint (pos_min = 109415, pos_max = 109415, n_tokens = 109416, n_swa = 1, size = 186.329 MiB)
600.39.720.796 W slot update_slots: id 3 | task 20986 | erased invalidated context checkpoint (pos_min = 109992, pos_max = 109992, n_tokens = 109993, n_swa = 1, size = 186.329 MiB)
600.39.722.255 W slot update_slots: id 3 | task 20986 | erased invalidated context checkpoint (pos_min = 110282, pos_max = 110282, n_tokens = 110283, n_swa = 1, size = 186.329 MiB)
600.39.723.368 W slot update_slots: id 3 | task 20986 | erased invalidated context checkpoint (pos_min = 110906, pos_max = 110906, n_tokens = 110907, n_swa = 1, size = 186.329 MiB)
600.39.724.851 W slot update_slots: id 3 | task 20986 | erased invalidated context checkpoint (pos_min = 111615, pos_max = 111615, n_tokens = 111616, n_swa = 1, size = 186.329 MiB)
600.39.726.093 W slot update_slots: id 3 | task 20986 | erased invalidated context checkpoint (pos_min = 112114, pos_max = 112114, n_tokens = 112115, n_swa = 1, size = 186.329 MiB)
600.39.727.501 W slot update_slots: id 3 | task 20986 | erased invalidated context checkpoint (pos_min = 113710, pos_max = 113710, n_tokens = 113711, n_swa = 1, size = 186.329 MiB)
600.39.742.056 W slot update_slots: id 3 | task 20986 | erased invalidated context checkpoint (pos_min = 116903, pos_max = 116903, n_tokens = 116904, n_swa = 1, size = 186.329 MiB)
600.39.754.232 I slot update_slots: id 3 | task 20986 | n_tokens = 0, memory_seq_rm [0, end)
I'm using the model with Qwen Code, and losing an ~110k context is very painful, it takes almost an hour and a half to reprocess it. This issue usually occurs after a couple of hours of coding, then it works fine again for a while, then it happens again, and so on.
Other information that might be related, this is logged while the model is loading:
0.06.469.907 I print_info: n_swa = 0
0.06.469.907 I print_info: is_swa_any = 0
Anyway, I'll try using increasing --ctx-checkpoints the next time this happens, maybe it will help, since --swa-full seems to be unsupported for this model.
Also, it would be nice if a warning would be displayed when attempting to use the --swa-full flag with models that aren't compatible with it.
Im going to close this, because there is no real bug here and #20087 makes llamacpp essentially do the best possible thing available to an RNN to avoid this
Reacted by Piotr Wilkin (ilintar) and Vladimir Nicolici
Activity
pwilkin commentedon Dec 31, 2025
The problem is the recurrent state, not the KV cache. You will get checkpoints after each processed prompt, so if a checkpoint matches, content will get reprocessed from there. But the recurrent state doesn't work like the KV cache. It only keeps a single state that gets updated with each token. Therefore, the only way this could work if we added arbitrary snapshot checkpoints during prompt processing, for example each N tokens.
othermod commentedon Dec 31, 2025
This seems to be the issue I'm experiencing too. I send 100k tokens of context (which stays the same) + and additional 3k tokens of instruction at the end (which changes on each run), and it always rebuilds the whole thing rather than the last 3k. I switched back to using another model for the task because its taking so long to do the prompt processing on each pass.
ggerganov commentedon Dec 31, 2025
The
--cache-reuseand--swa-flagflags do not do anything for recurrent models, so you can remove them for Qwen3 Next.If you have control over the client, you can chunk the input prompt in smaller pieces so that the server can create checkpoints and reduce the amount of recomputation for shared prefixes.
Yes, this is quite easy to implement. It will likely increase the host-memory cache size, but no way around this. Contributions are welcome.
othermod commentedon Dec 31, 2025
test_chunking.py
I've tried chunking with a python script and it still no luck. This script adds 5 chunks, followed by an instruction, and the cache works. When the instruction is modified, the cache has to rebuild.
Chunk 1... 3.65s
Chunk 2... 4.18s
Chunk 3... 3.57s
Chunk 4... 4.20s
Chunk 5... 3.51s
First instruction... 0.78s
Modified instruction... 15.60s
This issue only happens on this model.
Here is what I'm seeing in LM Studio when the modified instruction runs. Including in case it helps.
2025-12-31 01:58:00 [INFO]
[qwen3-next-80b-a3b-thinking] Running chat completion on conversation with 11 messages.
2025-12-31 01:58:00 [DEBUG]
Sampling params: repeat_last_n = 64, repeat_penalty = 1.100, frequency_penalty = 0.000, presence_penalty = 0.000
dry_multiplier = 0.000, dry_base = 1.750, dry_allowed_length = 2, dry_penalty_last_n = -1
top_k = 40, top_p = 0.950, min_p = 0.050, xtc_probability = 0.000, xtc_threshold = 0.100, typical_p = 1.000, top_n_sigma = -1.000, temp = 0.800
mirostat = 0, mirostat_lr = 0.100, mirostat_ent = 5.000
2025-12-31 01:58:00 [DEBUG]
Sampling:
logits -> penalties -> dry -> top-n-sigma -> top-k -> typical -> top-p -> min-p -> xtc -> temp-ext -> dist
Generate: n_ctx = 262144, n_batch = 512, n_predict = 20, n_keep = 805
2025-12-31 01:58:00 [DEBUG]
Looking for non-prefix contiguous prompt sequences of size >= 256 to reuse from cache
Cache reuse summary: 4133/4145 of prompt (99.7105%), 4133 prefix, 0 non-prefix
Failed to remove tokens from cache, clearing cache instead.
2025-12-31 01:58:00 [DEBUG]
Total prompt tokens: 4145
Prompt tokens to decode: 4145
BeginProcessingPrompt
ggerganov commentedon Dec 31, 2025
Chunking over
/v1/chat/completionsdoes not work because the chat templates appends various tokens to each chunks so the prefix will never match.IMbackK commentedon Dec 31, 2025
I see, so the clue I was missing is that qwen3-next has recurrent elements, I just red a paper on deltanet. The behavior makes sense with that in mind. I think better diagnostics would be good when non-applicable flags are used, its a bit mutch to expect the user to know the architecture details of eatch model used.
vnicolici commentedon Jan 16, 2026
I experience similar issues with LM Studio which currently uses llama.cpp b7721 and qwen3-next-80b. Cache reuse is very random, and it frequently discards the entire cache and recomputes it, which makes using large contexts almost impossible.
For example, during my recent test I used a system prompt 1071 tokens long, followed by 40 tokens for the first user prompt. The initial prompt processing worked as expected when the cache is empty:
It generated 643 tokens as the assistant reply.
When I tried to regenerate that reply (so the same system prompt and user prompt), I got this:
Then after reply regeneration, which produced 2566 tokens, I enter a new 9 token prompt. When generating the next reply, it doesn't match the entire cache, possibly due tokenization issues like those I reported in the past for DeepSeek models. But even the part that matches, the first 1781 tokens out of 3759, can't be used, because the entire cache is discarded again:
This doesn't happen all the time though. As I progress with the conversation, sometimes it does reuse the cache before generating replies, which start instantly with no delay.
However, even in those situations, if I try to regenerate that reply again it will usually discard the entire cache and reprocess the entire conversation, including the system prompt, from scratch, even though it used the cache just fine with the same prompt to generate the original reply that I'm regenerating.
I didn't try to reproduce this without LM Studio yet. But, from what I read here, it's unlikely I would get a different behavior.
So, my question is, is this particular behavior expected for this type of model? Because the longer the conversation progresses, the more painful it becomes to have the entire cache discarded.
ggerganov commentedon Jan 16, 2026
The "cache reuse" functionality is different from what you describe.
You are thinking about prefix/prompt caching. This works correctly with
llama-server, so if you experience any issue with it, feel free to create a bug report with repro steps.pwilkin commentedon Jan 17, 2026
@vnicolici Again: Qwen3Next is a hybrid model. It has recurrent (linear) layers. Recurrent layers don't have a KV cache. They use a single recurrent state which is updated for each token. Therefore, besides the snapshotting which I mentioned above, recurrent states can't do "prefix" cache'ing because they don't keep any sequences :)
@ggerganov Since linear cache takes so much less space than normal KV-cache, it might be reasonable to add a separate snapshot option just for linear layers, because non-linear layers can do normal prefix cache reuse. The logic might be a bit more complicated (since we'd have to check for "existing snapshot + proper prefix"), but it would actually enable something like say an LRU-cache of 2000 linear snapshots done every 100 generated tokens (parametrizable with say
--linear-snapshot-cache-sizeand--linear-snapshot-cache-frequency). Then, for hybrid models, we could use this hybrid snapshot + prefix caching mechanism.ggerganov commentedon Jan 17, 2026
@othermod Regarding your earlier comment - I missed the part that you performed your test using LM Studio. If you run the same test with
llama-server, you will see that it already works as expected:So my #18497 (comment) that chunking does not work with
/v1/chat/completionsis not fully correct. Just make sure that your "chunked" requests havemax_tokens=0to avoid generating stuff during the chunking process and it should work as I described earlier in #18497 (comment).The llama.cpp server has advanced prefix caching techniques that are not yet available in downstream projects.
@pwilkin Yes, this is quite easy to achieve. The logic for deciding when to make a checkpoint is here:
llama.cpp/tools/server/server-context.cpp
Lines 2561 to 2593 in a89002f
Currently, it is configured to make a checkpoint at 64 tokens before the full prompt:
llama.cpp/tools/server/server-context.cpp
Lines 2534 to 2539 in a89002f
scottix commentedon Feb 19, 2026
llama.cpp/src/llama-memory-hybrid.cpp
Line 179 in 10b26ee
llama.cpp/src/llama-memory-hybrid-iswa.cpp
Line 185 in 10b26ee
I think it is writing and reading partial caches but with this model it needs the full cache.
sammcj commentedon Feb 26, 2026
Confirming this is an issue with the Qwen 3.5 MoE models as well, not sure if it's helpful but as another data point here's the crash logs:
Config:
--cache-reuse 256resolves the crashForestHouse2316 commentedon Mar 6, 2026
Same error here too with Qwen3.5 model. Tried to use --swa-full option, not worked for me as comments above already said.
I did flash-attn off since I'm using 140 iGPU. Flash attention makes operation slower on intel's iGPU.
Options I used
Log
llama | slot launch_slot_: id 3 | task 66 | processing task, is_child = 0
llama | slot update_slots: id 3 | task 66 | new prompt, n_ctx_slot = 65536, n_keep = 0, task.n_tokens = 44172
llama | slot update_slots: id 3 | task 66 | n_past = 14242, slot.prompt.tokens.size() = 44015, seq_id = 3, pos_min = 44014, n_swa = 1
llama | slot update_slots: id 3 | task 66 | forcing full prompt re-processing due to lack of cache data (likely due to SWA or hybrid/recurrent memory, see #13194 (comment))
llama | slot update_slots: id 3 | task 66 | erased invalidated context checkpoint (pos_min = 43285, pos_max = 43285, n_tokens = 43286, n_swa = 1, size = 50.251 MiB)
llama | slot update_slots: id 3 | task 66 | erased invalidated context checkpoint (pos_min = 43464, pos_max = 43464, n_tokens = 43465, n_swa = 1, size = 50.251 MiB)
llama | slot update_slots: id 3 | task 66 | n_tokens = 0, memory_seq_rm [0, end)
llama | slot update_slots: id 3 | task 66 | prompt processing progress, n_tokens = 2048, batch.n_tokens = 2048, progress = 0.046364
llama | slot update_slots: id 3 | task 66 | n_tokens = 2048, memory_seq_rm [2048, end)
... (Then continue to the end of the context size)
vnicolici commentedon Mar 6, 2026
I'm now experiencing the same issue with
unsloth/qwen3.5-397b-a17b-gguf/qwen3.5-397b-a17b-mxfp4_moe-00001-of-00006.gguf.The
--swa-fullflag seems to have no effect, the behavior is the same both before and after adding the flag, so I'm guessing the model is similar to qwen3-next in that regard.This is how the log looks when it happens:
I'm using the model with Qwen Code, and losing an ~110k context is very painful, it takes almost an hour and a half to reprocess it. This issue usually occurs after a couple of hours of coding, then it works fine again for a while, then it happens again, and so on.
I'm currently using
b8196and this command line:Other information that might be related, this is logged while the model is loading:
Anyway, I'll try using increasing
--ctx-checkpointsthe next time this happens, maybe it will help, since--swa-fullseems to be unsupported for this model.Also, it would be nice if a warning would be displayed when attempting to use the
--swa-fullflag with models that aren't compatible with it.IMbackK commentedon Mar 6, 2026
Im going to close this, because there is no real bug here and #20087 makes llamacpp essentially do the best possible thing available to an RNN to avoid this