Skip to content

Commit 89febfe

Browse files
authored
examples : do not assume BOS when shifting context (ggml-org#5622)
1 parent 5022cf2 commit 89febfe

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

examples/main/main.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,8 @@ int main(int argc, char ** argv) {
334334
// number of tokens to keep when resetting context
335335
if (params.n_keep < 0 || params.n_keep > (int) embd_inp.size() || params.instruct || params.chatml) {
336336
params.n_keep = (int)embd_inp.size();
337+
} else {
338+
params.n_keep += add_bos; // always keep the BOS token
337339
}
338340

339341
// prefix & suffix for instruct mode
@@ -383,8 +385,8 @@ int main(int argc, char ** argv) {
383385
}
384386
}
385387

386-
if (params.n_keep > 0) {
387-
LOG_TEE("%s: static prompt based on n_keep: '", __func__);
388+
if (params.n_keep > add_bos) {
389+
LOG_TEE("%s: static prompt based on n_keep: '", __func__);
388390
for (int i = 0; i < params.n_keep; i++) {
389391
LOG_TEE("%s", llama_token_to_piece(ctx, embd_inp[i]).c_str());
390392
}
@@ -540,14 +542,14 @@ int main(int argc, char ** argv) {
540542
break;
541543
}
542544

543-
const int n_left = n_past - params.n_keep - 1;
545+
const int n_left = n_past - params.n_keep;
544546
const int n_discard = n_left/2;
545547

546548
LOG("context full, swapping: n_past = %d, n_left = %d, n_ctx = %d, n_keep = %d, n_discard = %d\n",
547549
n_past, n_left, n_ctx, params.n_keep, n_discard);
548550

549-
llama_kv_cache_seq_rm (ctx, 0, params.n_keep + 1 , params.n_keep + n_discard + 1);
550-
llama_kv_cache_seq_shift(ctx, 0, params.n_keep + 1 + n_discard, n_past, -n_discard);
551+
llama_kv_cache_seq_rm (ctx, 0, params.n_keep , params.n_keep + n_discard);
552+
llama_kv_cache_seq_shift(ctx, 0, params.n_keep + n_discard, n_past, -n_discard);
551553

552554
n_past -= n_discard;
553555

examples/server/server.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,14 +1487,15 @@ struct llama_server_context
14871487
if (slot.is_processing() && system_tokens.size() + slot.cache_tokens.size() >= (size_t) slot.n_ctx)
14881488
{
14891489
// Shift context
1490-
const int n_left = system_tokens.size() + slot.n_past - slot.params.n_keep - 1;
1490+
const int n_keep = slot.params.n_keep + add_bos_token;
1491+
const int n_left = system_tokens.size() + slot.n_past - n_keep;
14911492
const int n_discard = n_left / 2;
14921493

1493-
LOG_TEE("slot %d: context shift - n_keep = %d, n_left = %d, n_discard = %d\n", slot.id, slot.params.n_keep, n_left, n_discard);
1494-
llama_kv_cache_seq_rm (ctx, slot.id, slot.params.n_keep + 1 , slot.params.n_keep + n_discard + 1);
1495-
llama_kv_cache_seq_shift(ctx, slot.id, slot.params.n_keep + 1 + n_discard, system_tokens.size() + slot.n_past, -n_discard);
1494+
LOG_TEE("slot %d: context shift - n_keep = %d, n_left = %d, n_discard = %d\n", slot.id, n_keep, n_left, n_discard);
1495+
llama_kv_cache_seq_rm (ctx, slot.id, n_keep , n_keep + n_discard);
1496+
llama_kv_cache_seq_shift(ctx, slot.id, n_keep + n_discard, system_tokens.size() + slot.n_past, -n_discard);
14961497

1497-
for (size_t i = slot.params.n_keep + 1 + n_discard; i < slot.cache_tokens.size(); i++)
1498+
for (size_t i = n_keep + n_discard; i < slot.cache_tokens.size(); i++)
14981499
{
14991500
slot.cache_tokens[i - n_discard] = slot.cache_tokens[i];
15001501
}
@@ -1507,7 +1508,7 @@ struct llama_server_context
15071508

15081509
LOG_VERBOSE("context shift", {
15091510
{ "n_ctx", n_ctx },
1510-
{ "n_keep", params.n_keep },
1511+
{ "n_keep", n_keep },
15111512
{ "n_left", n_left },
15121513
});
15131514
}

0 commit comments

Comments
 (0)