Skip to content

Commit f986e50

Browse files
author
zeljko
committed
Separate RAM cache restore prefix threshold
1 parent 5c53d13 commit f986e50

6 files changed

Lines changed: 12 additions & 2 deletions

File tree

common/common.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2689,6 +2689,11 @@ bool gpt_params_find_arg(int argc, char ** argv, const std::string & arg, gpt_pa
26892689
params.cache_ram_n_min = std::stoi(argv[i]);
26902690
return true;
26912691
}
2692+
if (arg == "--cache-ram-reuse-n-min") {
2693+
CHECK_ARG
2694+
params.cache_ram_reuse_n_min = std::stoi(argv[i]);
2695+
return true;
2696+
}
26922697
if (arg == "--pos") {
26932698
CHECK_ARG
26942699
params.i_pos = std::stoi(argv[i]);
@@ -2887,6 +2892,7 @@ void gpt_params_print_usage(int /*argc*/, char ** argv, const gpt_params & param
28872892
options.push_back({ "*", "-cram, --cache-ram N", "set the maximum cache size in MiB (default: %d, -1 - no limit, 0 - disable)",params.cache_ram_mib });
28882893
options.push_back({ "*", "-crs, --cache-ram-similarity N", "max of similarity of prompt tokens to cache tokens that triggers prompt cache (default: %.2f).",params.cache_ram_similarity });
28892894
options.push_back({ "*", "-cram-n-min --cache-ram-n-min N", "minimum number of the cached tokens that triggers prompt cache (default: %d).", params.cache_ram_n_min });
2895+
options.push_back({ "*", "--cache-ram-reuse-n-min N", "minimum reusable common-prefix tokens required to restore from prompt cache (default: %d).", params.cache_ram_reuse_n_min });
28902896
options.push_back({ "*", "-n, --predict N", "number of tokens to predict (default: %d, -1 = infinity, -2 = until context filled)", params.n_predict });
28912897
options.push_back({ "*", "-b, --batch-size N", "logical maximum batch size (default: %d)", params.n_batch });
28922898
options.push_back({ "*", "-ub, --ubatch-size N", "physical maximum batch size (default: %d)", params.n_ubatch });

common/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,7 @@ struct gpt_params {
519519
int32_t ctx_checkpoints_tolerance = 5; // the number of tokens before the full prompt to create the checkpoint
520520
int32_t cache_ram_mib = 8192; // -1 = no limit, 0 - disable, 1 = 1 MiB, etc.
521521
int32_t cache_ram_n_min = 0; // min number of tokens required to save in the ram
522+
int32_t cache_ram_reuse_n_min = 0; // min reusable common-prefix tokens required to restore from ram cache
522523
float cache_ram_similarity = 0.5f; // similarity of tokens to cached tokens
523524

524525
// batched-bench params

docs/parameters.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,9 @@ Note: When the available memory is very limited, turn this option off (`-cram 0`
150150
| `-cram, --cache-ram N` | Set the maximum cache size in MiB | 8192 | -1 = no limit, 0 = disable Very useful when the variations of the same prompt are re-sent to the model (coding agents, etc.). [PR 954](https://github.com/ikawrakow/ik_llama.cpp/pull/954) |
151151
| `-crs, --cache-ram-similarity N` | Max similarity of prompt tokens to cache tokens that triggers prompt cache | 0.50 | |
152152
| `-cram-n-min, --cache-ram-n-min N` | Minimum number of cached tokens that triggers prompt cache | 0 | |
153+
| `--cache-ram-reuse-n-min N` | Minimum reusable common-prefix tokens required to restore from prompt cache | 0 | |
153154

154-
When restoring a prompt from RAM cache, the server ranks candidates by reusable common prefix first and uses similarity as a tie-breaker. A candidate is skipped if its reusable prefix is below `--cache-ram-n-min`, if the reusable fraction is below `--cache-ram-similarity`, or if the cached KV range cannot be rewound safely for SWA/sliding-window attention.
155+
When restoring a prompt from RAM cache, the server ranks candidates by reusable common prefix first and uses similarity as a tie-breaker. A candidate is skipped if its reusable prefix is below `--cache-ram-reuse-n-min`, if the reusable fraction is below `--cache-ram-similarity`, or if the cached KV range cannot be rewound safely for SWA/sliding-window attention.
155156

156157
For SWA models, cached prompt entries track the KV position range saved with the state. If the active KV window starts after the reusable prefix, restore is only allowed when an earlier context checkpoint can rewind to that prefix. Otherwise the candidate is rejected before loading state, so a bad RAM-cache hit does not mutate the slot and then force full prompt re-processing. When context checkpoints are capped, the server keeps the earliest checkpoint as a rewind anchor when possible.
157158

examples/server/server-context.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1148,7 +1148,7 @@ server_slot* server_context::get_available_slot(const server_task& task) {
11481148
const int64_t t_start = ggml_time_us();
11491149
copy_data_to_cached_prompt(tokens, *ret);
11501150

1151-
ret->prompt_load(*prompt_cache, task.tokens, (size_t) std::max(0, cache_ram_n_min), cache_ram_similarity);
1151+
ret->prompt_load(*prompt_cache, task.tokens, (size_t) std::max(0, cache_ram_reuse_n_min), cache_ram_similarity);
11521152
prompt_cache->update();
11531153

11541154
ret->cache_tokens = ret->server_cached_prompt.tokens.clone(); // recover cache tokens

examples/server/server-context.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ struct server_context {
297297
// Necessary similarity of prompt for slot selection
298298
float slot_prompt_similarity = 0.0f;
299299
int32_t cache_ram_n_min = 0;
300+
int32_t cache_ram_reuse_n_min = 0;
300301
float cache_ram_similarity = 0.5f;
301302

302303
~server_context();

examples/server/server.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,7 @@ int main(int argc, char ** argv) {
553553
// Necessary similarity of prompt for slot selection
554554
ctx_server.slot_prompt_similarity = params.slot_prompt_similarity;
555555
ctx_server.cache_ram_n_min = params.cache_ram_n_min;
556+
ctx_server.cache_ram_reuse_n_min = params.cache_ram_reuse_n_min;
556557
ctx_server.cache_ram_similarity = params.cache_ram_similarity;
557558
#ifdef SQLITE3_MODERN_CPP_SUPPORT
558559
auto db_handle = std::make_shared<DatabaseHandle>(params.sql_save_file);

0 commit comments

Comments
 (0)