]> git.djapps.eu Git - pkg/ggml/sources/llama.cpp/commitdiff
Add --ignore-eos parameter (#181)
authorslaren <redacted>
Sun, 19 Mar 2023 18:22:48 +0000 (19:22 +0100)
committerGitHub <redacted>
Sun, 19 Mar 2023 18:22:48 +0000 (20:22 +0200)
Co-authored-by: Georgi Gerganov <redacted>
main.cpp
utils.cpp
utils.h

index 8e95c23d5d678fc0814fb7adeeeb00effb4c3a4e..e18105624d282515757c0bdf49d246fdde2ee9be 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -27,6 +27,8 @@
 #define ANSI_COLOR_RESET   "\x1b[0m"
 #define ANSI_BOLD          "\x1b[1m"
 
+static const int EOS_TOKEN_ID = 2;
+
 // determine number of model parts based on the dimension
 static const std::map<int, int> LLAMA_N_PARTS = {
     { 4096, 1 },
@@ -956,6 +958,11 @@ int main(int argc, char ** argv) {
             {
                 const int64_t t_start_sample_us = ggml_time_us();
 
+                if (params.ignore_eos) {
+                    // set the logit of the eos token to zero to avoid sampling it
+                    logits[logits.size() - n_vocab + EOS_TOKEN_ID] = 0;
+                }
+
                 id = llama_sample_top_p_top_k(vocab, logits.data() + (logits.size() - n_vocab), last_n_tokens, repeat_penalty, top_k, top_p, temp, rng);
 
                 last_n_tokens.erase(last_n_tokens.begin());
@@ -1055,7 +1062,8 @@ int main(int argc, char ** argv) {
         }
 
         // end of text token
-        if (embd.back() == 2) {
+
+        if (embd.back() == EOS_TOKEN_ID) {
             if (params.interactive) {
                 is_interacting = true;
             } else {
index 99cb30bb96c0a68381610904fbfc9943f8bb8052..a4135b9fdbda97960b7f18e6f4b363a67c996ebb 100644 (file)
--- a/utils.cpp
+++ b/utils.cpp
@@ -71,6 +71,8 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
             params.use_color = true;
         } else if (arg == "-r" || arg == "--reverse-prompt") {
             params.antiprompt = argv[++i];
+        } else if (arg == "--ignore-eos") {
+            params.ignore_eos = true;
         } else if (arg == "-h" || arg == "--help") {
             gpt_print_usage(argc, argv, params);
             exit(0);
@@ -106,6 +108,7 @@ void gpt_print_usage(int /*argc*/, char ** argv, const gpt_params & params) {
     fprintf(stderr, "  --repeat_last_n N     last n tokens to consider for penalize (default: %d)\n", params.repeat_last_n);
     fprintf(stderr, "  --repeat_penalty N    penalize repeat sequence of tokens (default: %.1f)\n", params.repeat_penalty);
     fprintf(stderr, "  -c N, --ctx_size N    size of the prompt context (default: %d)\n", params.n_ctx);
+    fprintf(stderr, "  --ignore-eos          ignore end of stream token and continue generating\n");
     fprintf(stderr, "  --memory_f16          use f16 instead of f32 for memory key+value\n");
     fprintf(stderr, "  --temp N              temperature (default: %.1f)\n", params.temp);
     fprintf(stderr, "  -b N, --batch_size N  batch size for prompt processing (default: %d)\n", params.n_batch);
diff --git a/utils.h b/utils.h
index c68e4cba8b40d7277a2a7df1c0bb863f9721b952..21325191a259a63e1c08410bc763a93d3d73c9ad 100644 (file)
--- a/utils.h
+++ b/utils.h
@@ -36,6 +36,8 @@ struct gpt_params {
 
     bool interactive = false; // interactive mode
     bool instruct    = false; // instruction mode (used for Alpaca models)
+    
+    bool ignore_eos = false; // do not stop generating after eos
 };
 
 bool gpt_params_parse(int argc, char ** argv, gpt_params & params);