|
| 1 | +From 200892a3a54323eb65ca9c8d8afb6043ca2d8944 Mon Sep 17 00:00:00 2001 |
| 2 | + |
| 3 | +Date: Fri, 16 Jun 2023 23:43:36 +0200 |
| 4 | +Subject: [PATCH] Pass pointer to params in llama_init_from_file |
| 5 | + |
| 6 | +Especially with golang bindings, calling by value has the side-effect of |
| 7 | +values not being copied correctly. This has been observed with the |
| 8 | +bindings in https://github.com/go-skynet/go-llama.cpp/pull/105. |
| 9 | +--- |
| 10 | + examples/common.cpp | 2 +- |
| 11 | + examples/quantize-stats/quantize-stats.cpp | 2 +- |
| 12 | + examples/save-load-state/save-load-state.cpp | 4 ++-- |
| 13 | + examples/train-text-from-scratch/train-text-from-scratch.cpp | 2 +- |
| 14 | + llama.cpp | 3 ++- |
| 15 | + llama.h | 2 +- |
| 16 | + tests/test-tokenizer-0.cpp | 2 +- |
| 17 | + 7 files changed, 9 insertions(+), 8 deletions(-) |
| 18 | + |
| 19 | +diff --git a/examples/common.cpp b/examples/common.cpp |
| 20 | +index 055383beff9..7cf48e82158 100644 |
| 21 | +--- a/examples/common.cpp |
| 22 | ++++ b/examples/common.cpp |
| 23 | +@@ -555,7 +555,7 @@ struct llama_context * llama_init_from_gpt_params(const gpt_params & params) { |
| 24 | + lparams.logits_all = params.perplexity; |
| 25 | + lparams.embedding = params.embedding; |
| 26 | + |
| 27 | +- llama_context * lctx = llama_init_from_file(params.model.c_str(), lparams); |
| 28 | ++ llama_context * lctx = llama_init_from_file(params.model.c_str(), &lparams); |
| 29 | + |
| 30 | + if (lctx == NULL) { |
| 31 | + fprintf(stderr, "%s: error: failed to load model '%s'\n", __func__, params.model.c_str()); |
| 32 | +diff --git a/examples/quantize-stats/quantize-stats.cpp b/examples/quantize-stats/quantize-stats.cpp |
| 33 | +index 6b8018ee284..a7c1e873a92 100644 |
| 34 | +--- a/examples/quantize-stats/quantize-stats.cpp |
| 35 | ++++ b/examples/quantize-stats/quantize-stats.cpp |
| 36 | +@@ -330,7 +330,7 @@ int main(int argc, char ** argv) { |
| 37 | + lparams.f16_kv = false; |
| 38 | + lparams.use_mlock = false; |
| 39 | + |
| 40 | +- ctx = llama_init_from_file(params.model.c_str(), lparams); |
| 41 | ++ ctx = llama_init_from_file(params.model.c_str(), &lparams); |
| 42 | + |
| 43 | + if (ctx == NULL) { |
| 44 | + fprintf(stderr, "%s: error: failed to load model '%s'\n", __func__, params.model.c_str()); |
| 45 | +diff --git a/examples/save-load-state/save-load-state.cpp b/examples/save-load-state/save-load-state.cpp |
| 46 | +index da4d37ad03d..07ee6750d4c 100644 |
| 47 | +--- a/examples/save-load-state/save-load-state.cpp |
| 48 | ++++ b/examples/save-load-state/save-load-state.cpp |
| 49 | +@@ -35,7 +35,7 @@ int main(int argc, char ** argv) { |
| 50 | + auto last_n_tokens_data = std::vector<llama_token>(params.repeat_last_n, 0); |
| 51 | + |
| 52 | + // init |
| 53 | +- auto ctx = llama_init_from_file(params.model.c_str(), lparams); |
| 54 | ++ auto ctx = llama_init_from_file(params.model.c_str(), &lparams); |
| 55 | + auto tokens = std::vector<llama_token>(params.n_ctx); |
| 56 | + auto n_prompt_tokens = llama_tokenize(ctx, params.prompt.c_str(), tokens.data(), int(tokens.size()), true); |
| 57 | + |
| 58 | +@@ -95,7 +95,7 @@ int main(int argc, char ** argv) { |
| 59 | + llama_free(ctx); |
| 60 | + |
| 61 | + // load new model |
| 62 | +- auto ctx2 = llama_init_from_file(params.model.c_str(), lparams); |
| 63 | ++ auto ctx2 = llama_init_from_file(params.model.c_str(), &lparams); |
| 64 | + |
| 65 | + // Load state (rng, logits, embedding and kv_cache) from file |
| 66 | + { |
| 67 | +diff --git a/examples/train-text-from-scratch/train-text-from-scratch.cpp b/examples/train-text-from-scratch/train-text-from-scratch.cpp |
| 68 | +index 7ec85951adc..1c7a06c21be 100644 |
| 69 | +--- a/examples/train-text-from-scratch/train-text-from-scratch.cpp |
| 70 | ++++ b/examples/train-text-from-scratch/train-text-from-scratch.cpp |
| 71 | +@@ -3054,7 +3054,7 @@ int main(int argc, char ** argv) { |
| 72 | + struct llama_context_params llama_params = llama_context_default_params(); |
| 73 | + llama_params.vocab_only = true; |
| 74 | + |
| 75 | +- struct llama_context * lctx = llama_init_from_file(params.fn_vocab_model, llama_params); |
| 76 | ++ struct llama_context * lctx = llama_init_from_file(params.fn_vocab_model, &llama_params); |
| 77 | + |
| 78 | + struct llama_vocab vocab; |
| 79 | + { |
| 80 | +diff --git a/llama.cpp b/llama.cpp |
| 81 | +index 81f047ed298..0629e873886 100644 |
| 82 | +--- a/llama.cpp |
| 83 | ++++ b/llama.cpp |
| 84 | +@@ -2618,8 +2618,9 @@ static void llama_model_quantize_internal(const std::string & fname_inp, const s |
| 85 | + |
| 86 | + struct llama_context * llama_init_from_file( |
| 87 | + const char * path_model, |
| 88 | +- struct llama_context_params params) { |
| 89 | ++ const struct llama_context_params * params_ptr) { |
| 90 | + ggml_time_init(); |
| 91 | ++ struct llama_context_params params = *params_ptr; |
| 92 | + |
| 93 | + llama_context * ctx = new llama_context; |
| 94 | + |
| 95 | +diff --git a/llama.h b/llama.h |
| 96 | +index 1241ba6c0ec..faf2675f125 100644 |
| 97 | +--- a/llama.h |
| 98 | ++++ b/llama.h |
| 99 | +@@ -142,7 +142,7 @@ extern "C" { |
| 100 | + // Return NULL on failure |
| 101 | + LLAMA_API struct llama_context * llama_init_from_file( |
| 102 | + const char * path_model, |
| 103 | +- struct llama_context_params params); |
| 104 | ++ const struct llama_context_params * params); |
| 105 | + |
| 106 | + // Frees all allocated memory |
| 107 | + LLAMA_API void llama_free(struct llama_context * ctx); |
| 108 | +diff --git a/tests/test-tokenizer-0.cpp b/tests/test-tokenizer-0.cpp |
| 109 | +index ab1538a0cf3..b405df8e687 100644 |
| 110 | +--- a/tests/test-tokenizer-0.cpp |
| 111 | ++++ b/tests/test-tokenizer-0.cpp |
| 112 | +@@ -36,7 +36,7 @@ int main(int argc, char **argv) { |
| 113 | + |
| 114 | + lparams.vocab_only = true; |
| 115 | + |
| 116 | +- ctx = llama_init_from_file(fname.c_str(), lparams); |
| 117 | ++ ctx = llama_init_from_file(fname.c_str(), &lparams); |
| 118 | + |
| 119 | + if (ctx == NULL) { |
| 120 | + fprintf(stderr, "%s: error: failed to load vocab '%s'\n", __func__, fname.c_str()); |
0 commit comments