Skip to content

llama-server: Avoids extra copying of existing checkpoints - #30683

Open
sarahwu185 wants to merge 20 commits into
ggml-org:masterfrom
sarahwu185:sarwu/cache_context
Open

sarahwu185 wants to merge 20 commits into
ggml-org:masterfrom
sarahwu185:sarwu/cache_context

Conversation

@sarahwu185

@sarahwu185 sarahwu185 commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Overview

If an existing checkpoint already exist, reuse it instead of copying the state again.

Also with --cache-reuse, deleting a chunk from the middle of the prompt can restore a checkpoint that still has the deleted tokens and produce wrong output.

Additional information

Tracks checkpoint reusing inside the slot.

Profiling

Same long system prompt with a different user message per request vs. a growing multi-turn chat. Median GPU prompt time per request, RTX PRO 6000 Blackwell.

Model Checkpoint size Shared prefix, new user message (master → branch) Multi-turn chat (master → branch)
Gemma 3 1B Q4_K_M (SWA) 11 MiB 20 → 15.5 ms (−22%) unchanged, ~30 ms
Gemma 3 12B Q4_K_M (SWA) 320 MiB 216–230 → 114–130 ms (−45%) unchanged, ~275–314 ms
Qwen3.6-35B-A3B Q4_K_M (hybrid, MTP draft) 72 MiB 83 → 62 ms (−25%) unchanged, ~110–119 ms
lev (Qwen3.5-4B hybrid) Q4_K_M 50 MiB 48–49 → 35.5–36 ms (−27%) 70–72 → 57–59 ms (−18%)

Multi-turn chat benefits only when the next request goes back to a position that already has a checkpoint. lev restored a checkpoint on every turn, and all 48 re-copies became reuses. Qwen3.6 and Gemma just extended the cached prompt, so there was nothing to reuse.

Future works

an issue with the pos_min_thold clamp (live SWA state with pos_min == 0 is never reused)

Requirements

  • I have read and agree with the contributing guidelines
    Yes
  • AI usage disclosure: Yes, AI was used in identifying the problem as well as drafting the code change.

Comment thread tools/server/server-context.cpp Resolved
Comment thread tools/server/server-context.cpp Outdated
// cache reuse shifted chunks over the tokens this checkpoint covers
if (n_past > n_prefix && cur.n_tokens > n_prefix) {
return false;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes also seem to fix a bug on master where, with --cache-reuse, deleting a chunk from the middle of the prompt can restore a checkpoint that still has the deleted tokens and produce wrong output. This branch gets it right. Repro: tinygemma3 with -c 1024.

No test seems to cover this, could you maybe add one and mention it in the description?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah thanks, I have added a test

@ggerganov ggerganov self-assigned this Nov 3, 2026
@ggerganov

Copy link
Copy Markdown
Member

Please rebase again.

@tdakhran Could you help with review here?

@tdakhran tdakhran left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks right to me, tested with internal harness locally and observed no regressions.

Can confirm that it fixes --cache-reuse case as @praneshgo pointed, worth adding it to commits or title for git history.

Semi-related: there is an issue with the pos_min_thold clamp (live SWA state with pos_min == 0 is never reused), but can be addressed in a separate PR.

@sarahwu185

sarahwu185 commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the comments, branch is rebased, comment is updated

@ORippler ORippler left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logic seems good to me, but I'm not super well-versed in llama-server yet.

Could think about adding a regression test to ensure we continue avoiding extra copies, though that would rely on flushing + parsing of log messages afaik

Comment thread tools/server/server-context.cpp Resolved
@ggerganov

Copy link
Copy Markdown
Member

Let's wait for #30137 to merge and rebase this one.

@sarahwu185

Copy link
Copy Markdown
Contributor Author

Hello @tdakhran do you mind taking a look at 2f4d690? Now that checkpoints are immutable (can't just update id_task) I have to keep track of the reused checkpoint so that it doesn't get affected by the thinning logic.

Comment thread tools/server/tests/utils.py Outdated
server_args.extend(["--cache-ram", self.cache_ram])
if self.n_cache_reuse:
server_args.extend(["--cache-reuse", self.n_cache_reuse])
if self.n_ctx_checkpoints:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will skip when value 0 is provided, shall it be if self.n_ctx_checkpoints is not None?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I have updated


// checkpoint reused by a task - checkpoints are immutable, so its id_task still names the task that created it
const common_prompt_checkpoint * ckpt_reused = nullptr;
int ckpt_reused_task = -1;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These probably require reset logic when the slot is reassigned.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reset logic is added.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setup logic for newly added tests is very similar and can be factored out.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah sounds good, I have put them to the same test, also added SWA variant

return server

@staticmethod
def tinylfm2() -> ServerProcess:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose adding also a model with SWA layers, like tinygemma3.

Comment thread tools/server/tests/utils.py Outdated
server = ServerProcess()
server.offline = True # will be downloaded by load_all()
server.model_hf_file = None
server.model_hf_repo = "ggml-org/LFM2-test-ci-80M"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ngxson @ServeurpersoCom Should we also move this model to the ggml-org-dev?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, since #32096 the CI test models live on ggml-org-dev, so a new one should go there directly. On master, test_infill (Qwen2.5-Coder-1.5B-IQ3_XXS) and test_spec_methods (spec-for-testing) still point to ggml-org.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, how do I move this model? Thanks

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, how do I move this model? Thanks

I copied it to ggml-org-dev/LFM2-test-ci-80M, so you only need to point tinylfm2 at it: server.model_hf_repo = "ggml-org-dev/LFM2-test-ci-80M". If you need other test models there, just ping me and I'll copy them.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I have switched it.

This branch has not been deployed

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants