]> git.djapps.eu Git - pkg/ggml/sources/llama.cpp/commitdiff
Allow setting the rng seed after initialization. (#1184)
authorÁsgeir Bjarni Ingvarsson <redacted>
Wed, 26 Apr 2023 20:08:43 +0000 (20:08 +0000)
committerGitHub <redacted>
Wed, 26 Apr 2023 20:08:43 +0000 (22:08 +0200)
The llama_set_state_data function restores the rng state to what it
was at the time llama_copy_state_data was called. But users may want
to restore the state and proceed with a different seed.

llama.cpp
llama.h

index 25203c9e90b28f9fb58eff7ea45da6b60c864374..8334553a591dcae6559387239332e094fee4eb09 100644 (file)
--- a/llama.cpp
+++ b/llama.cpp
@@ -2082,6 +2082,13 @@ int llama_get_kv_cache_token_count(struct llama_context * ctx) {
 
 #define LLAMA_MAX_RNG_STATE 64*1024
 
+void llama_set_rng_seed(struct llama_context * ctx, int seed) {
+    if (seed <= 0) {
+        seed = time(NULL);
+    }
+    ctx->rng.seed(seed);
+}
+
 // Returns the size of the state
 size_t llama_get_state_size(struct llama_context * ctx) {
     // we don't know size of rng until we actually serialize it. so reserve more than enough memory for its serialized state.
diff --git a/llama.h b/llama.h
index ab41798d8b71226bab0e21ad25da646a2cd563b9..24c48cce624d8c43dbfb5d8c772d60a936a131af 100644 (file)
--- a/llama.h
+++ b/llama.h
@@ -116,6 +116,9 @@ extern "C" {
     // Returns the number of tokens in the KV cache
     LLAMA_API int llama_get_kv_cache_token_count(struct llama_context * ctx);
 
+    // Sets the current rng seed.
+    LLAMA_API void llama_set_rng_seed(struct llama_context * ctx, int seed);
+
     // Returns the size in bytes of the state (rng, logits, embedding and kv_cache)
     LLAMA_API size_t llama_get_state_size(struct llama_context * ctx);