Skip to content

server: fix checkpoint handling for hybrid memory models - #24797

Closed
AntmanLFEz wants to merge 1 commit into
ggml-org:masterfrom
AntmanLFEz:fix-hybrid-prompt-caching
Closed

AntmanLFEz wants to merge 1 commit into
ggml-org:masterfrom
AntmanLFEz:fix-hybrid-prompt-caching

Conversation

@AntmanLFEz

Copy link
Copy Markdown

Overview

This PR fixes prompt cache invalidation on hybrid models (such as Qwen3.5/Qwen3.6) where any minor prompt variation (thinking content stripped, injected timestamp, etc.) caused a full prompt re-processing to occur, effectively disabling prompt caching.

Thee server logs would consistently show the following with most agent harness's between turns:

    SLT_WRN(slot, "forcing full prompt re-processing due to lack of cache data (likely due to SWA or hybrid/recurrent memory, see %s)\n",
            "https://github.com/ggml-org/llama.cpp/pull/13194#issuecomment-2868343055");

Related issues: #21831, #23013, #22746, #24714, #24055

Additional information

Hybrid memory consists of two independent cache subsystems with fundamentally different positional semantics:

  • Attention cache: pos_min = earliest cached KV position
  • Recurrent cache: pos_min = current state position (one cell per sequence), so pos_min == pos_max == current_position (always the latest)

The previous seq_pos_min() implementation took std::max() of both caches' min positions, which conflated these semantics. This inflated pos_min to the latest position, making it appear as though no prefix was cached and forcing full prompt re-processing on any minor variation.

Solution

  • Return only the attention cache's pos_min, matching non-hybrid memory semantics.
  • seq_pos_max still retains intersection semantics because it bounds valid attention entries by where the recurrent state has advanced.

Additional hardening:

  • When speculative decoding partially accepts draft tokens and the rollback distance exceeds n_rs_seq, the recurrent cache now falls through to normal cell removal instead of returning false. This prevents hybrid seq_rm from failing and leaving the attention cache with stale entries. The trade-off is the recurrent state loses precise snapshot restoration, but the KV cache is correctly trimmed. The model re-processes from the last valid checkpoint, producing correct results with slightly higher latency during speculative decoding only.

Note that other hybrid types with recurrent memory would likely benefit from the same seq_pos_min() change, though I have not explicitly tested those models yet.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: AI was used to investigate and understand how the recurrent memory system works.

@alexhgreen07

Copy link
Copy Markdown

I can confirm from my testing that this has resolved most of the cache invalidation issue I've had with qwen3.6 35B. I'm seeing zero full 'cache re-processing' messages, though I am still seeing occasional large prompts being processed (70%+), maybe just expected with openclaw. I don't understand why this is or what cases cause this, but it's rare enough (1/15 messages or less) I'm no longer concerned. Previously I'd expect at a certain point that every message is fully reprocessing the prompt. I have run initial tests within the following environments:

Environment:

  • OS Windows 11 Home (WSL2)
  • Ryzen 5 7600
  • NVidia RTX 5070 Ti

Software stack:

  • llama.cpp (tag 'b9820' hash 3fc4e10, with above patch applied, clean re-build)
  • OpenClaw 26.6.10

Run command:
./llama-server -m "$MODEL" --port 8080 -c 131070 --jinja --no-mmap -ctk q4_0 -ctv q4_0 --chat-template-kwargs '{"preserve_thinking": true}'

NOTE: Some important command line arguments were initially tested and then removed: --swa-full and --cache-reuse 256. The cache reuse had no observable impact, and SWA full actually prevented reliable cache hits and improved performance.

I had single sessions running contexts up to 80k tokens, and multiple parallel sessions running between 40k and 70k tokens each, some misses when running many parallel sessions which seemed to show the message clearing stale recurrent state beyond n_past = 10047 (recr_pos_max = 15932).

Note I haven't reviewed the code in detail and can't claim to understand it, though I'm willing to validate against various models on my setup as described above.

@ggerganov

Copy link
Copy Markdown
Member

This change is obviously wrong - the recurrent state is valid only for the latest position and using it with any other position is incorrect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants