forked from karpathy/llm.c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrand.h
223 lines (196 loc) · 6.72 KB
/
rand.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
Mersenne Twisters implementation, numerically identical to torch.
Example usage:
mt19937_state state;
manual_seed(&state, 137);
printf("%u\n", randint32(&state));
printf("%u\n", randint32(&state));
printf("%u\n", randint32(&state));
printf("%u\n", randint32(&state));
printf("%u\n", randint32(&state));
float t8[8];
normal_(t8, 8, 0, 1, &state);
for (int i = 0; i < 8; i++) {
printf("%f\n", t8[i]);
}
printf("%u\n", randint32(&state));
float t16[16];
normal_(t16, 16, 0, 1, &state);
for (int i = 0; i < 16; i++) {
printf("%f\n", t16[i]);
}
printf("%u\n", randint32(&state));
PyTorch reference (producing identical results):
import torch
torch.manual_seed(137)
print(torch.randint(0, 0xFFFFFFFF, [1]).item())
print(torch.randint(0, 0xFFFFFFFF, [1]).item())
print(torch.randint(0, 0xFFFFFFFF, [1]).item())
print(torch.randint(0, 0xFFFFFFFF, [1]).item())
print(torch.randint(0, 0xFFFFFFFF, [1]).item())
t = torch.zeros(8);
t.normal_()
for i in range(len(t)) :
print(t[i].item())
print(torch.randint(0, 0xFFFFFFFF, [1]).item())
t = torch.zeros(16);
t.normal_()
for i in range(len(t)) :
print(t[i].item())
print(torch.randint(0, 0xFFFFFFFF, [1]).item())
Both output:
4053805790
2173880614
380293709
1237255315
2986595568
0.7947664260864258
1.4369317293167114
- 0.2292192131280899
0.47556325793266296
- 0.6334410905838013
- 0.5791953802108765
- 0.0925704762339592
- 0.8659197092056274
2186503452
- 1.2813878059387207
- 2.646395683288574
- 0.06569503247737885
0.2180829495191574
- 0.46536165475845337
- 0.33108410239219666
2.5485482215881348
0.10425379872322083
0.8460659980773926
0.9462448358535767
- 0.2913765013217926
0.34313806891441345
- 1.1186704635620117
- 0.18305328488349915
- 2.3153159618377686
0.3961987793445587
2756748748
*/
#ifndef RAND_H
#define RAND_H
#include <math.h>
#define MERSENNE_STATE_M 397u
#define MERSENNE_STATE_N 624u
#define LMASK 0x7ffffffful
#define UMASK 0x80000000ul
// Copyright(c) Makoto Matsumoto and Takuji Nishimura
// This implementation follows PyTorch so that we are numerically identical when running verification tests.
typedef struct {
unsigned long long seed_;
int left_;
unsigned int next_;
unsigned int state_[MERSENNE_STATE_N];
unsigned int MATRIX_A[2];
} mt19937_state;
void manual_seed(mt19937_state* state, unsigned int seed) {
state->MATRIX_A[0] = 0x0u;
state->MATRIX_A[1] = 0x9908b0df;
state->state_[0] = seed & 0xffffffff;
for (unsigned int j = 1; j < MERSENNE_STATE_N; j++) {
state->state_[j] = 1812433253 * (state->state_[j - 1] ^ (state->state_[j - 1] >> 30)) + j;
state->state_[j] &= 0xffffffff;
}
state->left_ = 1;
state->next_ = 0;
}
void next_state(mt19937_state* state) {
state->left_ = MERSENNE_STATE_N;
state->next_ = 0;
unsigned int y, j;
for (j = 0; j < MERSENNE_STATE_N - MERSENNE_STATE_M; j++) {
y = (state->state_[j] & UMASK) | (state->state_[j + 1] & LMASK);
state->state_[j] = state->state_[j + MERSENNE_STATE_M] ^ (y >> 1) ^ state->MATRIX_A[y & 0x1];
}
for (; j < MERSENNE_STATE_N - 1; j++) {
y = (state->state_[j] & UMASK) | (state->state_[j + 1] & LMASK);
state->state_[j] = state->state_[j + (MERSENNE_STATE_M - MERSENNE_STATE_N)] ^ (y >> 1) ^ state->MATRIX_A[y & 0x1];
}
y = (state->state_[MERSENNE_STATE_N - 1] & UMASK) | (state->state_[0] & LMASK);
state->state_[MERSENNE_STATE_N - 1] = state->state_[MERSENNE_STATE_M - 1] ^ (y >> 1) ^ state->MATRIX_A[y & 0x1];
}
unsigned int randint32(mt19937_state* state) {
if (!state) return 0;
if (state->MATRIX_A[0] != 0 || state->MATRIX_A[1] != 0x9908b0df) manual_seed(state, 5489); // auto-initialize
if (--state->left_ <= 0) {
next_state(state);
}
unsigned int y = state->state_[state->next_++];
y ^= y >> 11;
y ^= (y << 7) & 0x9d2c5680;
y ^= (y << 15) & 0xefc60000;
y ^= y >> 18;
return y;
}
inline unsigned long long randint64(mt19937_state* state) {
return (((unsigned long long)(randint32(state)) << 32) | randint32(state));
}
inline float randfloat32(mt19937_state* state) {
return (randint32(state) & ((1ull << 24) - 1)) * (1.0f / (1ull << 24));
}
inline double randfloat64(mt19937_state* state) {
return (randint64(state) & ((1ull << 53) - 1)) * (1.0 / (1ull << 53));
}
void uniform_(float* data, unsigned int numel, float from, float to, mt19937_state* state) {
for (unsigned int t = 0; t < numel; t++) {
data[t] = randfloat32(state) * (to - from) + from;
}
}
// Box�Muller transform
void normal_fill_16(float* data, float mean, float std, mt19937_state* state) {
#define EPSILONE 1e-12
for (unsigned int t = 0; t < 8; t++) {
float u1 = 1 - data[t];
float u2 = data[t + 8];
float radius = sqrtf(-2 * logf(u1 + EPSILONE));
float theta = 2.0 * M_PI * u2;
data[t] = (radius * cosf(theta) * std + mean);
data[t + 8] = (radius * sinf(theta) * std + mean);
}
}
void normal_fill(float* data, unsigned int numel, float mean, float std, mt19937_state* state) {
for (unsigned int t = 0; t < numel; t++) {
data[t] = randfloat32(state);
}
for (unsigned int i = 0; i < numel - 15; i += 16) {
normal_fill_16(data + i, mean, std, state);
}
if (numel % 16 != 0) {
// recompute the last 16 values
data = data + numel - 16;
for (unsigned int i = 0; i < 16; i++) {
data[i] = randfloat32(state);
}
normal_fill_16(data, mean, std, state);
}
}
void normal_(float* data, unsigned int numel, float mean, float std, mt19937_state* state) {
#define EPSILONE 1e-12
if (numel >= 16) {
normal_fill(data, numel, mean, std, state);
}
else {
double next_double_normal_sample = 0.0; // make compiler warning happy, won't be used
int has_next_double_normal_sample = 0;
for (unsigned int t = 0; t < numel; t++) {
if (has_next_double_normal_sample) {
data[t] = (float)(next_double_normal_sample * std + mean);
has_next_double_normal_sample = 0;
continue;
}
// for numel < 16 we draw a double (float64)
float u1 = randfloat64(state);
float u2 = randfloat64(state);
float radius = sqrtf(-2 * logf(1 - u2 + EPSILONE));
float theta = 2.0 * M_PI * u1;
next_double_normal_sample = radius * sinf(theta);
has_next_double_normal_sample = 1;
data[t] = (radius * cosf(theta) * std + mean);
}
}
}
#endif