Skip to content

server: add strict prompt cache RAM limit - #25070

Merged
ngxson merged 1 commit into
ggml-org:masterfrom
tarruda:cache-ram-strict
Jul 7, 2026
Merged

ngxson merged 1 commit into
ggml-org:masterfrom
tarruda:cache-ram-strict

Conversation

@tarruda

@tarruda tarruda commented Jun 27, 2026 •

Copy link
Copy Markdown
Contributor

Overview

Modify the caching code to ensure --cache-ram limit is respected.

Additional information

Before this commit, --cache-ram was not a hard limit:

  • The cache always kept at least one entry, even if that entry exceeded the
    RAM/token limits.
  • Old entries were only evicted for the RAM/token limits after saving the new
    one, which could cause the cache to temporarily exceed the RAM/token limits
    even if individual entries were below the limit.

Now, ensure that the RAM limit is strict with these changes:

  • Skip saving state to cache if by itself it exceeds the RAM limit.
  • Evict old entries as necessary to make the new entry fit.

Additionally, token-limit cleanup may now evict the last remaining cache entry instead of always preserving one.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: Minor codex assistance to get familiar with the relevants part of the code.

@tarruda
tarruda requested review from a team as code owners June 27, 2026 02:13
@tarruda
tarruda force-pushed the cache-ram-strict branch from 7702d13 to 4782510 Compare June 28, 2026 10:15
@tarruda
tarruda force-pushed the cache-ram-strict branch from 4782510 to 8035912 Compare July 6, 2026 13:46
@ngxson

ngxson commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

IMO amount specified by --cache-ram should already the strict max. if this limit is not respected, it should be a bug, solution is to fix it rather than having another flag

@tarruda

tarruda commented Jul 6, 2026 •

Copy link
Copy Markdown
Contributor Author

@ngxson as explained in the PR description, it is definitely not a max right now.

If I set --cache-ram to 1G and my kv + checkpoint data sums to 10G, llama.cpp will happily store that when switching to another prefix conversation. The main reason I noticed this is because I run models very close to my device limit (with -np 1) and I was seeing it exceeding my limits causing it to swap.

I can remove the --cache-ram-strict flag and make its behavior the default. Just note that this could break the expectation of some users that previously had at least 1 conversation slot stored, but now will result in full prompt reprocessing if the data exceeds --cache-ram.

Should I amend the PR?

@ngxson

ngxson commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

I haven't looked deep into the current implementation, but I assume that kv cache should not be counted toward this --cache-ram limit

on top of that, its help message says:

set the maximum cache size in MiB (default: %d, -1 - no limit, 0 - disable)

so it should be the hard limit for the cache or checkpoints

@tarruda
tarruda force-pushed the cache-ram-strict branch from 8035912 to 37b0eb1 Compare July 6, 2026 22:47
@tarruda

tarruda commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

@ngxson amended the commit/PR

One note though: I ran git blame on the code that ensured always one entry was kept in the cache and it turns out you did that 8 months ago: https://github.com/ggml-org/llama.cpp/blame/master/tools/server/server-task.cpp#L1755C11-L1755C11

Just thought I'd mention in case you want to review if there was some reason for doing that.

Before this commit, --cache-ram was not a hard limit:

- The cache always kept at least one entry, even if that entry exceeded the
  RAM/token limits.
- Old entries were only evicted for the RAM/token limits after saving the new
  one, which could cause the cache to temporarily exceed the RAM/token limits
  even if individual entries were below the limit.

Now, ensure that the RAM limit is strict with these changes:

- Skip saving state to cache if by itself it exceeds the RAM limit.
- Evict old entries as necessary to make the new entry fit.

Additionally, token-limit cleanup may now evict the last remaining cache entry
instead of always preserving one.
@tarruda
tarruda force-pushed the cache-ram-strict branch from 37b0eb1 to 13c21c0 Compare July 7, 2026 09:21

@ggerganov ggerganov left a comment

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.

The "always keep at least 1 prompt" logic has been there ever since the feature was introduced in #16391. I don't remember why at that time I thought it's needed, but now it seems unnecessary.

@ngxson
ngxson merged commit 6c487e2 into ggml-org:master Jul 7, 2026
25 checks passed
@tarruda
tarruda deleted the cache-ram-strict branch July 7, 2026 13:26
tarruda added a commit to tarruda/llama.cpp that referenced this pull request Jul 13, 2026
Before this commit, --cache-ram was not a hard limit:

- The cache always kept at least one entry, even if that entry exceeded the
  RAM/token limits.
- Old entries were only evicted for the RAM/token limits after saving the new
  one, which could cause the cache to temporarily exceed the RAM/token limits
  even if individual entries were below the limit.

Now, ensure that the RAM limit is strict with these changes:

- Skip saving state to cache if by itself it exceeds the RAM limit.
- Evict old entries as necessary to make the new entry fit.

Additionally, token-limit cleanup may now evict the last remaining cache entry
instead of always preserving one.
satindergrewal pushed a commit to satindergrewal/llama.cpp that referenced this pull request Aug 12, 2026
Before this commit, --cache-ram was not a hard limit:

- The cache always kept at least one entry, even if that entry exceeded the
  RAM/token limits.
- Old entries were only evicted for the RAM/token limits after saving the new
  one, which could cause the cache to temporarily exceed the RAM/token limits
  even if individual entries were below the limit.

Now, ensure that the RAM limit is strict with these changes:

- Skip saving state to cache if by itself it exceeds the RAM limit.
- Evict old entries as necessary to make the new entry fit.

Additionally, token-limit cleanup may now evict the last remaining cache entry
instead of always preserving one.
zbrad pushed a commit to zbrad/llama.cpp that referenced this pull request Sep 10, 2026
Before this commit, --cache-ram was not a hard limit:

- The cache always kept at least one entry, even if that entry exceeded the
  RAM/token limits.
- Old entries were only evicted for the RAM/token limits after saving the new
  one, which could cause the cache to temporarily exceed the RAM/token limits
  even if individual entries were below the limit.

Now, ensure that the RAM limit is strict with these changes:

- Skip saving state to cache if by itself it exceeds the RAM limit.
- Evict old entries as necessary to make the new entry fit.

Additionally, token-limit cleanup may now evict the last remaining cache entry
instead of always preserving one.
SRV_WRN(" - making room for prompt cache entry, removing oldest entry (size = %.3f MiB)\n",
states.front().size() / (1024.0 * 1024.0));

states.pop_front();

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 removes previous entries that match the current prompt; i.e. they would have been restored otherwise. Particularly painful when you have 160k+ token in the context, and - boom - fully reprocessed.

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.

5 participants