Merge pull request #138 from aegkmq/better-rng

Replace the rand() with a portable rng
This commit is contained in:
Andrej
2023-07-27 22:33:14 -07:00
committed by GitHub
+14 -2
View File
@@ -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); 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 // functions to sample the next token from the transformer's predicted distribution
int sample(float* probabilities, int n) { int sample(float* probabilities, int n) {
// sample index from probabilities, they must sum to 1 // sample index from probabilities, they must sum to 1
float r = (float)rand() / (float)RAND_MAX; float r = random_f32();
float cdf = 0.0f; float cdf = 0.0f;
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
cdf += probabilities[i]; cdf += probabilities[i];
@@ -464,7 +476,7 @@ int main(int argc, char *argv[]) {
} }
// seed rng with time. if you want deterministic behavior use temperature 0.0 // 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 // read in the model.bin file
Config config; Config config;