From a13e998a8a458044e35a78f2a447c042524f64e0 Mon Sep 17 00:00:00 2001 From: Ben Williams Date: Tue, 16 May 2023 18:49:16 -0700 Subject: [PATCH] added `--disable-tty` flag for interactive mode --- examples/common.cpp | 15 +++++++++++---- examples/common.h | 3 ++- examples/main/main.cpp | 2 +- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/examples/common.cpp b/examples/common.cpp index 259880a7cc64f..3cf83872f4f77 100644 --- a/examples/common.cpp +++ b/examples/common.cpp @@ -344,6 +344,8 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) { break; } params.input_suffix = argv[i]; + } else if (arg == "--disable-tty") { + params.disable_tty = true; } else { fprintf(stderr, "error: unknown argument: %s\n", arg.c_str()); gpt_print_usage(argc, argv, default_params); @@ -382,6 +384,7 @@ void gpt_print_usage(int /*argc*/, char ** argv, const gpt_params & params) { fprintf(stderr, " run in interactive mode and poll user input upon seeing PROMPT (can be\n"); fprintf(stderr, " specified more than once for multiple prompts).\n"); fprintf(stderr, " --color colorise output to distinguish prompt and user input from generations\n"); + fprintf(stderr, " --disable-tty disable the use of TTY in interactive mode in favor of stderr\n"); fprintf(stderr, " -s SEED, --seed SEED RNG seed (default: -1, use random seed for < 0)\n"); fprintf(stderr, " -t N, --threads N number of threads to use during computation (default: %d)\n", params.n_threads); fprintf(stderr, " -p PROMPT, --prompt PROMPT\n"); @@ -503,7 +506,7 @@ struct llama_context * llama_init_from_gpt_params(const gpt_params & params) { return lctx; } -void console_init(console_state & con_st) { +void console_init(console_state & con_st, bool disable_tty) { #if defined(_WIN32) // Windows-specific console initialization DWORD dwMode = 0; @@ -541,9 +544,13 @@ void console_init(console_state & con_st) { new_termios.c_cc[VTIME] = 0; tcsetattr(STDIN_FILENO, TCSANOW, &new_termios); - con_st.tty = fopen("/dev/tty", "w+"); - if (con_st.tty != nullptr) { - con_st.out = con_st.tty; + if (!disable_tty) { + con_st.tty = fopen("/dev/tty", "w+"); + if (con_st.tty != nullptr) { + con_st.out = con_st.tty; + } + } else { + con_st.out = stderr; } setlocale(LC_ALL, ""); diff --git a/examples/common.h b/examples/common.h index f4e07a2525723..b3b77d1d8d90a 100644 --- a/examples/common.h +++ b/examples/common.h @@ -72,6 +72,7 @@ struct gpt_params { bool use_mlock = false; // use mlock to keep model in memory bool mem_test = false; // compute maximum memory usage bool verbose_prompt = false; // print prompt tokens before generation + bool disable_tty = false; // disable TTY mode }; bool gpt_params_parse(int argc, char ** argv, gpt_params & params); @@ -125,7 +126,7 @@ struct console_state { #endif }; -void console_init(console_state & con_st); +void console_init(console_state & con_st, bool disable_tty); void console_cleanup(console_state & con_st); void console_set_color(console_state & con_st, console_color_t color); bool console_readline(console_state & con_st, std::string & line); diff --git a/examples/main/main.cpp b/examples/main/main.cpp index fe1c847a7f490..d9fa186a98af6 100644 --- a/examples/main/main.cpp +++ b/examples/main/main.cpp @@ -59,7 +59,7 @@ int main(int argc, char ** argv) { // (note for later: this is a slightly awkward choice) con_st.use_color = params.use_color; con_st.multiline_input = params.multiline_input; - console_init(con_st); + console_init(con_st, params.disable_tty); atexit([]() { console_cleanup(con_st); }); if (params.perplexity) {