From e3d780d8335c8cb486a25580da21b608b9cd7af2 Mon Sep 17 00:00:00 2001 From: Justin Date: Wed, 15 Mar 2023 01:47:51 +0000 Subject: [PATCH 1/3] added ctx_size parameter --- main.cpp | 3 +-- utils.cpp | 5 ++++- utils.h | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/main.cpp b/main.cpp index 6dc9ae98036ed..9bbdd11532623 100644 --- a/main.cpp +++ b/main.cpp @@ -818,8 +818,7 @@ int main(int argc, char ** argv) { // load the model { const int64_t t_start_us = ggml_time_us(); - - if (!llama_model_load(params.model, model, vocab, 512)) { // TODO: set context from user input ?? + if (!llama_model_load(params.model, model, vocab, params.n_ctx)) { fprintf(stderr, "%s: failed to load model from '%s'\n", __func__, params.model.c_str()); return 1; } diff --git a/utils.cpp b/utils.cpp index 54217f02f5c36..189ec231ce2e8 100644 --- a/utils.cpp +++ b/utils.cpp @@ -37,7 +37,9 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) { params.n_predict = std::stoi(argv[++i]); } else if (arg == "--top_k") { params.top_k = std::stoi(argv[++i]); - } else if (arg == "--top_p") { + } else if (arg == "-c" || arg == "--ctx_size") { + params.n_ctx = std::stoi(argv[++i]); + } else if (arg == "--top_p") { params.top_p = std::stof(argv[++i]); } else if (arg == "--temp") { params.temp = std::stof(argv[++i]); @@ -92,6 +94,7 @@ void gpt_print_usage(int argc, char ** argv, const gpt_params & params) { fprintf(stderr, " --top_p N top-p sampling (default: %.1f)\n", params.top_p); 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, " --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); fprintf(stderr, " -m FNAME, --model FNAME\n"); diff --git a/utils.h b/utils.h index 4f98011cf257c..36c1153e65f7f 100644 --- a/utils.h +++ b/utils.h @@ -17,7 +17,7 @@ struct gpt_params { int32_t n_threads = std::min(4, (int32_t) std::thread::hardware_concurrency()); int32_t n_predict = 128; // new tokens to predict int32_t repeat_last_n = 64; // last n tokens to penalize - + int32_t n_ctx = 512; //context size // sampling parameters int32_t top_k = 40; float top_p = 0.95f; From 9eb4598fa3a9313d756604f0512b02d8fa65728f Mon Sep 17 00:00:00 2001 From: Justin Date: Wed, 15 Mar 2023 02:33:56 +0000 Subject: [PATCH 2/3] added it in more places --- main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.cpp b/main.cpp index 9bbdd11532623..feb9edade737f 100644 --- a/main.cpp +++ b/main.cpp @@ -218,7 +218,7 @@ bool llama_model_load(const std::string & fname, llama_model & model, gpt_vocab ctx_size += n_ctx*n_layer*n_embd*ggml_type_sizef(GGML_TYPE_F32); // memory_k ctx_size += n_ctx*n_layer*n_embd*ggml_type_sizef(GGML_TYPE_F32); // memory_v - ctx_size += (5 + 10*n_layer)*256; // object overhead + ctx_size += (5 + 10*n_layer)*hparams.n_ctx; // object overhead fprintf(stderr, "%s: ggml ctx size = %6.2f MB\n", __func__, ctx_size/(1024.0*1024.0)); } @@ -547,7 +547,7 @@ bool llama_eval( const int d_key = n_embd/n_head; - static size_t buf_size = 512u*1024*1024; + static size_t buf_size = hparams.n_ctx*1024*1024; static void * buf = malloc(buf_size); if (mem_per_token > 0 && mem_per_token*N > buf_size) { From f056beb3840492e182a50052c25ecbd46b8343c6 Mon Sep 17 00:00:00 2001 From: Georgi Gerganov Date: Wed, 15 Mar 2023 21:35:48 +0200 Subject: [PATCH 3/3] Apply suggestions from code review --- main.cpp | 6 ++++-- utils.cpp | 4 ++-- utils.h | 1 + 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/main.cpp b/main.cpp index feb9edade737f..e29f94b5723b8 100644 --- a/main.cpp +++ b/main.cpp @@ -218,7 +218,7 @@ bool llama_model_load(const std::string & fname, llama_model & model, gpt_vocab ctx_size += n_ctx*n_layer*n_embd*ggml_type_sizef(GGML_TYPE_F32); // memory_k ctx_size += n_ctx*n_layer*n_embd*ggml_type_sizef(GGML_TYPE_F32); // memory_v - ctx_size += (5 + 10*n_layer)*hparams.n_ctx; // object overhead + ctx_size += (5 + 10*n_layer)*256; // object overhead fprintf(stderr, "%s: ggml ctx size = %6.2f MB\n", __func__, ctx_size/(1024.0*1024.0)); } @@ -547,7 +547,9 @@ bool llama_eval( const int d_key = n_embd/n_head; - static size_t buf_size = hparams.n_ctx*1024*1024; + // TODO: check if this size scales with n_ctx linearly and remove constant. somehow I feel it wasn't the case + // static size_t buf_size = hparams.n_ctx*1024*1024; + static size_t buf_size = 512u*1024*1024; static void * buf = malloc(buf_size); if (mem_per_token > 0 && mem_per_token*N > buf_size) { diff --git a/utils.cpp b/utils.cpp index 189ec231ce2e8..aa3ad1053da02 100644 --- a/utils.cpp +++ b/utils.cpp @@ -38,8 +38,8 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) { } else if (arg == "--top_k") { params.top_k = std::stoi(argv[++i]); } else if (arg == "-c" || arg == "--ctx_size") { - params.n_ctx = std::stoi(argv[++i]); - } else if (arg == "--top_p") { + params.n_ctx = std::stoi(argv[++i]); + } else if (arg == "--top_p") { params.top_p = std::stof(argv[++i]); } else if (arg == "--temp") { params.temp = std::stof(argv[++i]); diff --git a/utils.h b/utils.h index 36c1153e65f7f..021120b0513c7 100644 --- a/utils.h +++ b/utils.h @@ -18,6 +18,7 @@ struct gpt_params { int32_t n_predict = 128; // new tokens to predict int32_t repeat_last_n = 64; // last n tokens to penalize int32_t n_ctx = 512; //context size + // sampling parameters int32_t top_k = 40; float top_p = 0.95f;