Skip to content

Commit 7f5ff55

Browse files
authored
server: stop generation at n_ctx_train if n_predict is not set (ggml-org#6638)
* server: cap n_predict if not set to n_ctx_train * server: fix infinite loop * server: infinite loop, move in process_token server: infinite loop: set stop limit to true * minor: spaces * minor: spaces * server: include prompt tokens in the EOS limit
1 parent 9e4e077 commit 7f5ff55

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

examples/server/server.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,27 @@ struct server_context {
12071207
LOG_VERBOSE("eos token found", {});
12081208
}
12091209

1210+
auto n_ctx_train = llama_n_ctx_train(model);
1211+
if (slot.params.n_predict < 1 && slot.ga_n == 1
1212+
&& slot.n_prompt_tokens + slot.n_decoded >= n_ctx_train) {
1213+
LOG_WARNING("n_predict is not set and self-context extend is disabled."
1214+
" Limiting generated tokens to n_ctx_train to avoid EOS-less generation infinite loop", {
1215+
{ "id_slot", slot.id },
1216+
{ "params.n_predict", slot.params.n_predict },
1217+
{ "slot.n_prompt_tokens", slot.n_prompt_tokens },
1218+
{ "slot.n_decoded", slot.n_decoded },
1219+
{ "slot.n_predict", slot.n_predict },
1220+
{ "n_slots", params.n_parallel },
1221+
{ "slot.n_ctx", slot.n_ctx },
1222+
{ "n_ctx", n_ctx },
1223+
{ "n_ctx_train", n_ctx_train },
1224+
{ "ga_n", slot.ga_n },
1225+
});
1226+
slot.truncated = true;
1227+
slot.stopped_limit = true;
1228+
slot.has_next_token = false; // stop prediction
1229+
}
1230+
12101231
LOG_VERBOSE("next token", {
12111232
{"id_slot", slot.id},
12121233
{"id_task", slot.id_task},
@@ -2141,7 +2162,7 @@ struct server_context {
21412162
});
21422163

21432164
// process the created batch of tokens
2144-
for (int32_t i = 0; i < (int32_t) batch.n_tokens; i += n_batch) {
2165+
for (int32_t i = 0; i < batch.n_tokens; i += n_batch) {
21452166
const int32_t n_tokens = std::min(n_batch, batch.n_tokens - i);
21462167

21472168
for (auto & slot : slots) {

0 commit comments

Comments
 (0)