4
4
# This source code is licensed under the license found in the
5
5
# LICENSE file in the root directory of this source tree.
6
6
7
- import random
8
-
9
7
import torch
8
+ from facto .inputgen .utils .random_manager import random_manager as rm
10
9
from facto .inputgen .variable .type import ScalarDtype
11
10
from facto .inputgen .variable .utils import nextdown , nextup
12
11
@@ -166,11 +165,11 @@ def factorize(n, length):
166
165
factor_list = []
167
166
prod = 1
168
167
for _ in range (length ):
169
- x = random .choice (range (10 ))
168
+ x = rm . get_random () .choice (range (10 ))
170
169
factor_list .append (x )
171
170
prod *= x
172
171
if prod != 0 :
173
- i = random .choice (range (length ))
172
+ i = rm . get_random () .choice (range (length ))
174
173
factor_list [i ] = 0
175
174
return {tuple (factor_list )}
176
175
@@ -180,29 +179,29 @@ def factorize(n, length):
180
179
factors = factorize_into_primes (n )
181
180
factor_list = [1 ] * length
182
181
for factor in factors :
183
- x = random .choice (range (length ))
182
+ x = rm . get_random () .choice (range (length ))
184
183
factor_list [x ] *= factor
185
184
return {tuple (factor_list )}
186
185
187
186
188
187
def valid_view_copy_size (tensor , length ):
189
188
n = tensor .numel ()
190
189
valids = factorize (n , length )
191
- factors = random .choice (list (factorize (n , length )))
190
+ factors = rm . get_random () .choice (list (factorize (n , length )))
192
191
if length >= 1 :
193
192
if n > 0 :
194
- x = random .choice (range (length ))
193
+ x = rm . get_random () .choice (range (length ))
195
194
factor_list = list (factors )
196
195
factor_list [x ] = - 1
197
196
valids |= {tuple (factor_list )}
198
197
else :
199
198
zeros = [i for i in range (length ) if factors [i ] == 0 ]
200
- z = random .choice (zeros )
199
+ z = rm . get_random () .choice (zeros )
201
200
factor_list = list (factors )
202
201
factor_list [z ] = - 1
203
202
for i in range (length ):
204
203
if i != z and factors [i ] == 0 :
205
- factor_list [i ] = random .choice (range (1 , 10 ))
204
+ factor_list [i ] = rm . get_random () .choice (range (1 , 10 ))
206
205
valids |= {tuple (factor_list )}
207
206
return valids
208
207
@@ -221,39 +220,39 @@ def invalid_view_copy_size(tensor, length):
221
220
if n > 2 :
222
221
invalids |= factorize (n - 1 , length )
223
222
if n > 3 :
224
- x = random .choice (range (2 , n - 1 ))
223
+ x = rm . get_random () .choice (range (2 , n - 1 ))
225
224
invalids |= factorize (x , length )
226
225
invalids |= factorize (n + 1 , length )
227
226
if n > 0 :
228
227
invalids |= factorize (2 * n , length )
229
228
invalids |= factorize (3 * n , length )
230
- factors = random .choice (list (factorize (n , length )))
229
+ factors = rm . get_random () .choice (list (factorize (n , length )))
231
230
potential_negative = []
232
231
for ix , factor in enumerate (factors ):
233
232
if factor > 1 :
234
233
potential_negative .append (ix )
235
234
if len (potential_negative ) >= 1 :
236
- x = random .choice (potential_negative )
235
+ x = rm . get_random () .choice (potential_negative )
237
236
factor_list = list (factors )
238
237
factor_list [x ] = - factors [x ]
239
238
invalids |= {tuple (factor_list )}
240
239
if len (potential_negative ) >= 2 :
241
- x , y = random .sample (potential_negative , 2 )
240
+ x , y = rm . get_random () .sample (potential_negative , 2 )
242
241
factor_list = list (factors )
243
242
factor_list [x ] = - factors [x ]
244
243
factor_list [y ] = - factors [y ]
245
244
invalids |= {tuple (factor_list )}
246
245
if length >= 2 :
247
- x , y = random .sample (range (length ), 2 )
246
+ x , y = rm . get_random () .sample (range (length ), 2 )
248
247
factor_list = list (factors )
249
248
factor_list [x ], factor_list [y ] = - 1 , - 1
250
249
invalids |= {tuple (factor_list )}
251
250
if length >= 1 and n == 0 :
252
251
zeros = [i for i in range (length ) if factors [i ] == 0 ]
253
- z = random .choice (zeros )
252
+ z = rm . get_random () .choice (zeros )
254
253
non_z = [i for i in range (length ) if i != z ]
255
254
if len (non_z ) >= 1 :
256
- x = random .choice ([i for i in range (length ) if i != z ])
255
+ x = rm . get_random () .choice ([i for i in range (length ) if i != z ])
257
256
factor_list = list (factors )
258
257
factor_list [x ] = - 1
259
258
invalids |= {tuple (factor_list )}
@@ -289,9 +288,9 @@ def valid_dim_list_helper(tensor, pool, length):
289
288
290
289
n = max (tensor .dim (), 1 )
291
290
292
- sample = tuple (random .sample (pool , length ))
291
+ sample = tuple (rm . get_random () .sample (pool , length ))
293
292
neg_sample = tuple (s - n for s in sample )
294
- mix_sample = tuple (random .choice ([s , s - n ]) for s in sample )
293
+ mix_sample = tuple (rm . get_random () .choice ([s , s - n ]) for s in sample )
295
294
296
295
return {sample , neg_sample , mix_sample }
297
296
0 commit comments