Skip to content

Commit 910fb8b

Browse files
committed
Fix issue where interactive mode crashes when input exceeds ctx size
Closes ggml-org#1768
1 parent e9b66ee commit 910fb8b

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

examples/common.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,9 @@ void console_set_color(console_state & con_st, console_color_t color) {
632632
case CONSOLE_COLOR_USER_INPUT:
633633
fprintf(con_st.out, ANSI_BOLD ANSI_COLOR_GREEN);
634634
break;
635+
case CONSOLE_COLOR_ERROR:
636+
fprintf(con_st.out, ANSI_BOLD ANSI_COLOR_RED);
637+
break;
635638
}
636639
con_st.color = color;
637640
fflush(con_st.out);

examples/common.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ struct llama_context * llama_init_from_gpt_params(const gpt_params & params);
112112
enum console_color_t {
113113
CONSOLE_COLOR_DEFAULT=0,
114114
CONSOLE_COLOR_PROMPT,
115-
CONSOLE_COLOR_USER_INPUT
115+
CONSOLE_COLOR_USER_INPUT,
116+
CONSOLE_COLOR_ERROR
116117
};
117118

118119
struct console_state {

examples/main/main.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,17 @@ int main(int argc, char ** argv) {
331331
while ((n_remain != 0 && !is_antiprompt) || params.interactive) {
332332
// predict
333333
if (embd.size() > 0) {
334+
auto max_embd_size = n_ctx - 2;
335+
// Ensure the input doesn't exceed the context size by truncating embd if necessary.
336+
if ((int)embd.size() > max_embd_size) {
337+
auto skipped_tokens = embd.size() - max_embd_size;
338+
console_set_color(con_st, CONSOLE_COLOR_ERROR);
339+
printf("<<input too long: skipped %ld token%s>>", skipped_tokens, skipped_tokens != 1 ? "s" : "");
340+
console_set_color(con_st, CONSOLE_COLOR_DEFAULT);
341+
fflush(stdout);
342+
embd.resize(max_embd_size);
343+
}
344+
334345
// infinite text generation via context swapping
335346
// if we run out of context:
336347
// - take the n_keep first tokens from the original prompt (via n_past)

0 commit comments

Comments
 (0)