Skip to content

Commit

Permalink
Merge pull request #138 from aegkmq/better-rng
Browse files Browse the repository at this point in the history
Replace the rand() with a portable rng
  • Loading branch information
karpathy committed Jul 28, 2023
2 parents 9949c50 + 6ce28fb commit d04336c
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions run.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,24 @@ void transformer(int token, int pos, Config* p, RunState* s, TransformerWeights*
matmul(s->logits, x, w->wcls, p->dim, p->vocab_size);
}

// https://en.wikipedia.org/wiki/Xorshift#xorshift.2A
unsigned long long rng_seed;
unsigned int random_u32() {
rng_seed ^= rng_seed >> 12;
rng_seed ^= rng_seed << 25;
rng_seed ^= rng_seed >> 27;
return (rng_seed * 0x2545F4914F6CDD1Dull) >> 32;
}
float random_f32() {
return (random_u32() >> 8) / 16777216.0f;
}

// ----------------------------------------------------------------------------
// functions to sample the next token from the transformer's predicted distribution

int sample(float* probabilities, int n) {
// sample index from probabilities, they must sum to 1
float r = (float)rand() / (float)RAND_MAX;
float r = random_f32();
float cdf = 0.0f;
for (int i = 0; i < n; i++) {
cdf += probabilities[i];
Expand Down Expand Up @@ -464,8 +476,8 @@ int main(int argc, char *argv[]) {
}

// seed rng with time. if you want deterministic behavior use temperature 0.0
srand((unsigned int)time(NULL));
rng_seed = (unsigned int)time(NULL);

// read in the model.bin file
Config config;
TransformerWeights weights;
Expand Down

0 comments on commit d04336c

Please sign in to comment.