-
Notifications
You must be signed in to change notification settings - Fork 46
/
rng_mrg.py
290 lines (237 loc) · 8.99 KB
/
rng_mrg.py
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
"""
Implementation of MRG31k3p random number generator for Myia.
Based on Theano implementation (2019/11/05):
https://github.com/Theano/Theano/blob/master/theano/sandbox/rng_mrg.py
While Theano implementation uses many parallel streams in a matrix to
generate numbers, current myia implementation below uses only
1 stream vector for simplicity.
Generator code in SSJ package (L'Ecuyer & Simard):
http://www.iro.umontreal.ca/~simardr/ssj/indexe.html
Page up-to-date (2019/11/05):
http://simul.iro.umontreal.ca/ssj/indexe.html
Original Github project page (2019/11/05):
https://github.com/umontreal-simul/ssj
Original JAVA implementation on Github (2019/11/05):
https://github.com/umontreal-simul/ssj/blob/master/src/main/java/umontreal/ssj/rng/MRG31k3p.java
The MRG31k3p algorithm was published in:
P. L'Ecuyer and R. Touzin, Fast Combined Multiple Recursive Generators with
Multipliers of the form a = +/- 2^d +/- 2^e, Proceedings of the 2000 Winter
Simulation Conference, Dec. 2000, 683-689.
The conception of the multi-stream from MRG31k3p was published in:
P. L'Ecuyer and R. Simard and E. Jack Chen and W. David Kelton,
An Object-Oriented Random-Number Package with Many Long Streams
and Substreams, Operations Research, volume 50, number 6, 2002, 1073-1075.
"""
from dataclasses import dataclass
import numpy as np
# ----------
# Constants.
# ----------
M1 = np.int32(2147483647) # 2^31 - 1
M2 = np.int32(2147462579) # 2^31 - 21069
MASK12 = np.int32(511) # 2^9 - 1
MASK13 = np.int32(16777215) # 2^24 - 1
MASK2 = np.int32(65535) # 2^16 - 1
MULT2 = np.int32(21069)
A1p72 = np.asarray([[1516919229, 758510237, 499121365],
[1884998244, 1516919229, 335398200],
[601897748, 1884998244, 358115744]],
dtype='int64')
A2p72 = np.asarray([[1228857673, 1496414766, 954677935],
[1133297478, 1407477216, 1496414766],
[2002613992, 1639496704, 1407477216]],
dtype='int64')
A1p134 = np.asarray([[1702500920, 1849582496, 1656874625],
[828554832, 1702500920, 1512419905],
[1143731069, 828554832, 102237247]],
dtype='int64')
A2p134 = np.asarray([[796789021, 1464208080, 607337906],
[1241679051, 1431130166, 1464208080],
[1401213391, 1178684362, 1431130166]],
dtype='int64')
i0 = np.int32(0)
i7 = np.int32(7)
i9 = np.int32(9)
i15 = np.int32(15)
i16 = np.int32(16)
i22 = np.int32(22)
i24 = np.int32(24)
# Hashable versions of useful nd-arrays above
# (to avoid hash error during myia compilation).
a_1_p_134 = tuple(
tuple(np.int64(A1p134[i, j]) for j in range(3)) for i in range(3))
a_2_p_134 = tuple(
tuple(np.int64(A2p134[i, j]) for j in range(3)) for i in range(3))
# Tuple of d-types supported by RNG, and index for each type in tuple.
SUPP_TYPES = (np.float16, np.float32, np.float64)
FLOAT16, FLOAT32, FLOAT64 = 0, 1, 2
# ------------------
# Private functions.
# ------------------
def _mat_vec_mod_m(a, i, j, k, m):
"""
Specific matrix-vector operation used to increment random state.
Given 3x3 matrix `a` and vector v = (i, j, k), compute:
output = numpy.sum((a * v) % m, axis=1) % m
:param a: a 2-d tensor (matrix) with shape (3, 3)
:param i: a scalar
:param j: a scalar
:param k: a scalar
:param m: a scalar to use for modulo
:return: a vector with 3 values
"""
i = np.int64(i)
j = np.int64(j)
k = np.int64(k)
m = np.int64(m)
o0 = (((a[0][0] * i) % m) + ((a[0][1] * j) % m) + ((a[0][2] * k) % m)) % m
o1 = (((a[1][0] * i) % m) + ((a[1][1] * j) % m) + ((a[1][2] * k) % m)) % m
o2 = (((a[2][0] * i) % m) + ((a[2][1] * j) % m) + ((a[2][2] * k) % m)) % m
return np.int32(o0), np.int32(o1), np.int32(o2)
def _mrg_new_stream(seed=12345):
"""
Create and return a new random state (vector of 6 32-bits integers).
Seed can be an integer or a vector of 6 integers.
:type seed: int | tuple
:return: a tuple with 6 32-bits integers.
"""
if isinstance(seed, int):
if seed == 0:
raise Exception('seed should not be 0')
elif seed >= M2:
raise Exception('seed should be less than M2')
rstate = (np.int32(seed), np.int32(seed), np.int32(seed),
np.int32(seed), np.int32(seed), np.int32(seed))
elif len(seed) == 6:
if seed[0] == 0 and seed[1] == 0 and seed[2] == 0:
raise Exception('First 3 values of seed should not be all 0')
if seed[3] == 0 and seed[4] == 0 and seed[5] == 0:
raise Exception('Last 3 values of seed should not be all 0')
if seed[0] >= M1 or seed[1] >= M1 or seed[2] >= M1:
raise Exception('First 3 values of seed should be less than M1')
if seed[3] >= M2 or seed[4] >= M2 or seed[5] >= M2:
raise Exception('Last 3 values of seed should be less than M2')
rstate = (np.int32(seed[0]), np.int32(seed[1]), np.int32(seed[2]),
np.int32(seed[3]), np.int32(seed[4]), np.int32(seed[5]))
else:
raise Exception("seed should be 1 integer or 6 integers")
return rstate
def _mrg_next_value(rstate, norm, mask, offset, dtype):
"""
Generate next value from given MRG stream, norm, mask, offset and dtype.
Return new value and updated MRG stream.
"""
x11 = rstate[0]
x12 = rstate[1]
x13 = rstate[2]
x21 = rstate[3]
x22 = rstate[4]
x23 = rstate[5]
y1 = (((x12 & MASK12) << i22) + (x12 >> i9) +
((x13 & MASK13) << i7) + (x13 >> i24))
if y1 < 0 or y1 >= M1:
y1 = y1 - M1
y1 = y1 + x13
if y1 < 0 or y1 >= M1:
y1 = y1 - M1
x13 = x12
x12 = x11
x11 = y1
y1 = ((x21 & MASK2) << i15) + (MULT2 * (x21 >> i16))
if y1 < 0 or y1 >= M2:
y1 = y1 - M2
y2 = ((x23 & MASK2) << i15) + (MULT2 * (x23 >> i16))
if y2 < 0 or y2 >= M2:
y2 = y2 - M2
y2 = y2 + x23
if y2 < 0 or y2 >= M2:
y2 = y2 - M2
y2 = y2 + y1
if y2 < 0 or y2 >= M2:
y2 = y2 - M2
x23 = x22
x22 = x21
x21 = y2
new_rstate = x11, x12, x13, x21, x22, x23
if x11 <= x21:
value = SUPP_TYPES[dtype](((x11 - x21 + M1) & mask) + offset) * norm
else:
value = SUPP_TYPES[dtype](((x11 - x21) & mask) + offset) * norm
return value, new_rstate
# ---------------
# RNG public API.
# ---------------
@dataclass
class MyiaRandomState:
"""
Data class to represent a random state specialized for a d-type.
Supported dtypes: float16, float32, float64
"""
rstate: tuple
mask: object
offset: object
norm: object
dtype: object
def myia_random_state(dtype, seed=12345):
"""
Generate a new random state for given dtype and seed.
:param dtype: a string: either 'float16', ´float32' or 'float64'.
:param seed: an integer or a tuple of 6 integers.
:return: a MyiaRandomState object.
"""
rstate = _mrg_new_stream(seed)
if dtype == 'float16':
mask = 0x7fff
offset = 1
norm = np.float16(3.0458e-05)
dtype = FLOAT16
elif dtype == 'float32':
mask = 0xffffffff
offset = 0
norm = np.float32(4.6566126e-10)
dtype = FLOAT32
elif dtype == 'float64':
mask = 0xffffffff
offset = 0
norm = np.float64(4.656612873077392578125e-10) # 1./2^31
dtype = FLOAT64
else:
raise Exception('Unsupported type for random state')
return MyiaRandomState(rstate, mask, offset, norm, dtype)
def myia_increment_state(state: MyiaRandomState):
"""
Increment a random state and return new updated random state.
Useful to create a new random state from a previous existing one.
Not useful in the random generation process.
:param state: an existing MyiaRandomState object.
:return: a MyiaRandomState object.
"""
rs = state.rstate
new_rstate = (_mat_vec_mod_m(a_1_p_134, rs[0], rs[1], rs[2], M1) +
_mat_vec_mod_m(a_2_p_134, rs[3], rs[4], rs[5], M2))
return MyiaRandomState(
new_rstate, state.mask, state.offset, state.norm, state.dtype)
def myia_next_value(state: MyiaRandomState):
"""
Generate a scalar using given state.
:param state: a MyiaRandomState object.
:return: a tuple: (new scalar value, updated state).
"""
sample, new_rstate = _mrg_next_value(
state.rstate, state.norm, state.mask, state.offset, state.dtype)
return sample, MyiaRandomState(
new_rstate, state.mask, state.offset, state.norm, state.dtype)
# --------------------------
# MRG public high-level API.
# --------------------------
def myia_uniform(state: MyiaRandomState, low=0.0, high=1.0):
"""
Generate a uniform scalar between low and high.
:param state: a MyiaRandomState object.
:param low: lower bound for uniform value.
:param high: upper bound for uniform value.
:return: a tuple: (new uniform scalar value, updated state).
"""
u, next_state = myia_next_value(state)
r = u * (high - low) + low
return r, next_state