Skip to content

Commit b8c8dda

Browse files
howard0suggerganov
andauthored
Use unsigned for random seed (#2006)
* Use unsigned for random seed. Keep -1 as the value to use a time based seed. Co-authored-by: Georgi Gerganov <[email protected]>
1 parent 96a712c commit b8c8dda

File tree

10 files changed

+25
-23
lines changed

10 files changed

+25
-23
lines changed

examples/common.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
110110
invalid_param = true;
111111
break;
112112
}
113-
params.seed = std::stoi(argv[i]);
113+
params.seed = std::stoul(argv[i]);
114114
} else if (arg == "-t" || arg == "--threads") {
115115
if (++i >= argc) {
116116
invalid_param = true;

examples/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
int32_t get_num_physical_cores();
2323

2424
struct gpt_params {
25-
int32_t seed = -1; // RNG seed
25+
uint32_t seed = -1; // RNG seed
2626
int32_t n_threads = get_num_physical_cores();
2727
int32_t n_predict = -1; // new tokens to predict
2828
int32_t n_ctx = 512; // context size

examples/embedding/embedding.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ int main(int argc, char ** argv) {
2424

2525
fprintf(stderr, "%s: build = %d (%s)\n", __func__, BUILD_NUMBER, BUILD_COMMIT);
2626

27-
if (params.seed < 0) {
27+
if (params.seed == LLAMA_DEFAULT_SEED) {
2828
params.seed = time(NULL);
2929
}
3030

31-
fprintf(stderr, "%s: seed = %d\n", __func__, params.seed);
31+
fprintf(stderr, "%s: seed = %u\n", __func__, params.seed);
3232

3333
std::mt19937 rng(params.seed);
3434
if (params.random_prompt) {

examples/main/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ Example usage: `--logit-bias 29905-inf`
242242

243243
### RNG Seed
244244

245-
- `-s SEED, --seed SEED`: Set the random number generator (RNG) seed (default: -1, < 0 = random seed).
245+
- `-s SEED, --seed SEED`: Set the random number generator (RNG) seed (default: -1, -1 = random seed).
246246

247247
The RNG seed is used to initialize the random number generator that influences the text generation process. By setting a specific seed value, you can obtain consistent and reproducible results across multiple runs with the same input and settings. This can be helpful for testing, debugging, or comparing the effects of different options on the generated text to see when they diverge. If the seed is set to a value less than 0, a random seed will be used, which will result in different outputs on each run.
248248

examples/main/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ int main(int argc, char ** argv) {
9494

9595
fprintf(stderr, "%s: build = %d (%s)\n", __func__, BUILD_NUMBER, BUILD_COMMIT);
9696

97-
if (params.seed < 0) {
97+
if (params.seed == LLAMA_DEFAULT_SEED) {
9898
params.seed = time(NULL);
9999
}
100100

101-
fprintf(stderr, "%s: seed = %d\n", __func__, params.seed);
101+
fprintf(stderr, "%s: seed = %u\n", __func__, params.seed);
102102

103103
std::mt19937 rng(params.seed);
104104
if (params.random_prompt) {

examples/perplexity/perplexity.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,11 @@ int main(int argc, char ** argv) {
136136

137137
fprintf(stderr, "%s: build = %d (%s)\n", __func__, BUILD_NUMBER, BUILD_COMMIT);
138138

139-
if (params.seed < 0) {
139+
if (params.seed == LLAMA_DEFAULT_SEED) {
140140
params.seed = time(NULL);
141141
}
142142

143-
fprintf(stderr, "%s: seed = %d\n", __func__, params.seed);
143+
fprintf(stderr, "%s: seed = %u\n", __func__, params.seed);
144144

145145
std::mt19937 rng(params.seed);
146146
if (params.random_prompt) {

examples/server/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ node .
152152

153153
`mirostat_eta`: Set the Mirostat learning rate, parameter eta (default: 0.1).
154154

155-
`seed`: Set the random number generator (RNG) seed (default: -1, < 0 = random seed).
155+
`seed`: Set the random number generator (RNG) seed (default: -1, -1 = random seed).
156156

157157
`ignore_eos`: Ignore end of stream token and continue generating (default: false).
158158

examples/train-text-from-scratch/train-text-from-scratch.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2768,7 +2768,7 @@ void train_print_usage(int /*argc*/, char ** argv, const struct train_params * p
27682768
fprintf(stderr, " --checkpoint-in FNAME path from which to load training checkpoint (default '%s')\n", params->fn_checkpoint_in);
27692769
fprintf(stderr, " --checkpoint-out FNAME path to save training checkpoint (default '%s')\n", params->fn_checkpoint_out);
27702770
fprintf(stderr, " --model-out FNAME path to save ggml model (default '%s')\n", params->fn_model_out);
2771-
fprintf(stderr, " -s SEED, --seed SEED RNG seed (default: -1, use random seed for < 0)\n");
2771+
fprintf(stderr, " -s SEED, --seed SEED RNG seed (default: -1, use random seed for -1)\n");
27722772
fprintf(stderr, " -c N, --ctx N Context size used during training (default %d)\n", params->n_ctx);
27732773
fprintf(stderr, " --embd N Embedding size used for new models (default %d)\n", params->n_embd);
27742774
fprintf(stderr, " --mult N Mult size used for new models, influences feedforward size. (default %d)\n", params->n_mult);
@@ -3034,10 +3034,10 @@ int main(int argc, char ** argv) {
30343034
return 1;
30353035
}
30363036

3037-
if (params.seed < 0) {
3037+
if (params.seed == LLAMA_DEFAULT_SEED) {
30383038
params.seed = time(NULL);
30393039
}
3040-
printf("%s: seed: %d\n", __func__, params.seed);
3040+
printf("%s: seed: %u\n", __func__, params.seed);
30413041
srand(params.seed);
30423042

30433043
struct llama_context_params llama_params = llama_context_default_params();

llama.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ static bool kv_cache_init(
777777

778778
struct llama_context_params llama_context_default_params() {
779779
struct llama_context_params result = {
780-
/*.seed =*/ -1,
780+
/*.seed =*/ LLAMA_DEFAULT_SEED,
781781
/*.n_ctx =*/ 512,
782782
/*.n_batch =*/ 512,
783783
/*.gpu_layers =*/ 0,
@@ -2541,7 +2541,7 @@ struct llama_context * llama_new_context_with_model(
25412541

25422542
llama_context * ctx = new llama_context(*model, model->vocab);
25432543

2544-
if (params.seed < 0) {
2544+
if (params.seed == LLAMA_DEFAULT_SEED) {
25452545
params.seed = time(NULL);
25462546
}
25472547

@@ -2974,8 +2974,8 @@ int llama_get_kv_cache_token_count(const struct llama_context * ctx) {
29742974

29752975
#define LLAMA_MAX_RNG_STATE (64*1024)
29762976

2977-
void llama_set_rng_seed(struct llama_context * ctx, int seed) {
2978-
if (seed < 0) {
2977+
void llama_set_rng_seed(struct llama_context * ctx, uint32_t seed) {
2978+
if (seed == LLAMA_DEFAULT_SEED) {
29792979
seed = time(NULL);
29802980
}
29812981
ctx->rng.seed(seed);

llama.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
#define LLAMA_SESSION_MAGIC LLAMA_FILE_MAGIC_GGSN
4747
#define LLAMA_SESSION_VERSION 1
4848

49+
#define LLAMA_DEFAULT_SEED 0xFFFFFFFF
50+
4951
#if defined(GGML_USE_CUBLAS) || defined(GGML_USE_CLBLAST) || defined(GGML_USE_METAL)
5052
// Defined when llama.cpp is compiled with support for offloading model layers to GPU.
5153
#define LLAMA_SUPPORTS_GPU_OFFLOAD
@@ -81,11 +83,11 @@ extern "C" {
8183
typedef void (*llama_progress_callback)(float progress, void *ctx);
8284

8385
struct llama_context_params {
84-
int seed; // RNG seed, -1 for random
85-
int n_ctx; // text context
86-
int n_batch; // prompt processing batch size
87-
int n_gpu_layers; // number of layers to store in VRAM
88-
int main_gpu; // the GPU that is used for scratch and small tensors
86+
uint32_t seed; // RNG seed, -1 for random
87+
int32_t n_ctx; // text context
88+
int32_t n_batch; // prompt processing batch size
89+
int32_t n_gpu_layers; // number of layers to store in VRAM
90+
int32_t main_gpu; // the GPU that is used for scratch and small tensors
8991
float tensor_split[LLAMA_MAX_DEVICES]; // how to split layers across multiple GPUs
9092
// called with a progress value between 0 and 1, pass NULL to disable
9193
llama_progress_callback progress_callback;
@@ -196,7 +198,7 @@ extern "C" {
196198
LLAMA_API int llama_get_kv_cache_token_count(const struct llama_context * ctx);
197199

198200
// Sets the current rng seed.
199-
LLAMA_API void llama_set_rng_seed(struct llama_context * ctx, int seed);
201+
LLAMA_API void llama_set_rng_seed(struct llama_context * ctx, uint32_t seed);
200202

201203
// Returns the maximum size in bytes of the state (rng, logits, embedding
202204
// and kv_cache) - will often be smaller after compacting tokens

0 commit comments

Comments
 (0)