Skip to content

Commit 360a9c9

Browse files
authored
server : fix cache_tokens bug with no cache_prompt (#13533)
1 parent 09d13d9 commit 360a9c9

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

tools/server/server.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2951,7 +2951,8 @@ struct server_context {
29512951
llama_kv_self_seq_rm (ctx, slot.id, n_keep , n_keep + n_discard);
29522952
llama_kv_self_seq_add(ctx, slot.id, n_keep + n_discard, slot.n_past, -n_discard);
29532953

2954-
if (slot.params.cache_prompt) {
2954+
// add generated tokens to cache
2955+
{
29552956
llama_tokens new_tokens = slot.cache_tokens.get_text_tokens(); // copy
29562957
for (size_t i = n_keep + n_discard; i < new_tokens.size(); i++) {
29572958
new_tokens[i - n_discard] = new_tokens[i];
@@ -2996,10 +2997,7 @@ struct server_context {
29962997
common_batch_add(batch, slot.sampled, slot.n_past, { slot.id }, true);
29972998

29982999
slot.n_past += 1;
2999-
3000-
if (slot.params.cache_prompt) {
3001-
slot.cache_tokens.push_back(slot.sampled);
3002-
}
3000+
slot.cache_tokens.push_back(slot.sampled);
30033001

30043002
SLT_DBG(slot, "slot decode token, n_ctx = %d, n_past = %d, n_cache_tokens = %d, truncated = %d\n",
30053003
slot.n_ctx, slot.n_past, (int) slot.cache_tokens.size(), slot.truncated);
@@ -3171,6 +3169,11 @@ struct server_context {
31713169

31723170
SLT_DBG(slot, "after context reuse, new slot.n_past = %d\n", slot.n_past);
31733171
}
3172+
} else {
3173+
// if we don't cache the prompt, we have to remove the entire KV cache
3174+
llama_kv_self_seq_rm(ctx, slot.id, 0, -1);
3175+
slot.n_past = 0;
3176+
slot.cache_tokens.clear();
31743177
}
31753178
}
31763179

@@ -3204,7 +3207,7 @@ struct server_context {
32043207
SLT_INF(slot, "kv cache rm [%d, end)\n", slot.n_past);
32053208

32063209
// remove the non-common part from the cache
3207-
slot.cache_tokens.resize(slot.n_past);
3210+
slot.cache_tokens.keep_first(slot.n_past);
32083211

32093212
// check if we should process the image
32103213
if (slot.n_past < slot.n_prompt_tokens
@@ -3221,7 +3224,8 @@ struct server_context {
32213224
continue;
32223225
}
32233226

3224-
if (slot.params.cache_prompt) {
3227+
// add the image chunk to cache
3228+
{
32253229
const auto & chunk = slot.prompt_tokens.find_chunk(slot.n_past);
32263230
slot.cache_tokens.push_back(chunk.get()); // copy
32273231
}
@@ -3242,9 +3246,7 @@ struct server_context {
32423246
const bool need_embd = slot.task_type == SERVER_TASK_TYPE_EMBEDDING && llama_pooling_type(slot.ctx) == LLAMA_POOLING_TYPE_NONE;
32433247

32443248
common_batch_add(batch, cur_tok, slot.n_past, { slot.id }, need_embd);
3245-
if (slot.params.cache_prompt) {
3246-
slot.cache_tokens.push_back(cur_tok);
3247-
}
3249+
slot.cache_tokens.push_back(cur_tok);
32483250

32493251
slot.n_prompt_tokens_processed++;
32503252
slot.n_past++;

tools/server/tests/unit/test_completion.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,18 @@ def test_cache_vs_nocache_prompt():
196196
assert res_cache.body["content"] == res_no_cache.body["content"]
197197

198198

199+
def test_nocache_long_input_prompt():
200+
global server
201+
server.start()
202+
res = server.make_request("POST", "/completion", data={
203+
"prompt": "I believe the meaning of life is"*32,
204+
"seed": 42,
205+
"temperature": 1.0,
206+
"cache_prompt": False,
207+
})
208+
assert res.status_code == 200
209+
210+
199211
def test_completion_with_tokens_input():
200212
global server
201213
server.temperature = 0.0

tools/server/utils.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1153,7 +1153,7 @@ struct server_tokens {
11531153
tokens.clear();
11541154
}
11551155

1156-
void resize(size_t n) {
1156+
void keep_first(size_t n) {
11571157
GGML_ASSERT(n <= tokens.size());
11581158
if (has_mtmd) {
11591159
// we throw an error if we try to remove a token in the middle of an image

0 commit comments

Comments
 (0)