-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun_instrumented.py
More file actions
1207 lines (1111 loc) · 41.2 KB
/
run_instrumented.py
File metadata and controls
1207 lines (1111 loc) · 41.2 KB
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import sys
from numpy import dtype
from instrumentation.data_tracing_receiver import DataTracingReceiver
from instrumentation.module_loader import PatchingPathFinder
from gnn.predict import evaluate
from instrumentation.helper import IntToClassMapping
import sys
sys.path.append("/home/aayan/Desktop/Research")
sys.path.append("/Users/aayan/Desktop/Research")
patcher = PatchingPathFinder()
############################################################################
# After patcher.install() is called, all subsequently imported libraries
# would be instrumented to collect the dynamic dataflow and control flow
# dependency information to which would enable the DataTracingReceiver()
# below to construct the execution graph.
############################################################################
patcher.install()
import random
# random.seed(1)
receiver = DataTracingReceiver()
lower = 10
upper = 30
############################################################################
# This file contains input generators which would generate inputs for
# generating the `train`, `test` etc. datasets. This dataset contains code
# which is used for generating graphs which is used for training the ML
# model used in our framework to actually process the generated execution
# graphs.
############################################################################
############################################################################
# The functions below were implemented to test out a preliminary version of
# the model. The functions used for training the predictor for the NumPy
# APIs are below the `NumPy APIs` heading.
############################################################################
def testInsertionSort():
global lower, upper
from demos.insertsort import insertion_sort
l = random.randint(lower, upper)
arr = [random.randint(0,10) for i in range(l)]
print(arr)
with receiver:
arr = insertion_sort(arr)
print("InsertionSorted:", arr)
def testSelectionSort():
global lower, upper
from demos.selectsort import selection_sort
l = random.randint(lower, upper)
arr = [random.randint(0,10) for i in range(l)]
print(arr)
with receiver:
arr = selection_sort(arr)
print("SelectSorted:", arr)
def testMergeSort():
global lower, upper
from demos.mergesort import merge2
l = random.randint(lower, upper)
arr = [random.randint(0,10) for i in range(l)]
print(arr)
with receiver:
arr = merge2(arr)
print("MergeSorted:", arr)
def testArgMergeSort():
global lower, upper
from demos.mergesort import argmerge
l = random.randint(lower, upper)
arr = [random.randint(0,10) for i in range(l)]
print(arr)
with receiver:
indices = argmerge(arr)
print("MergeSorted:", indices)
def testQuickSort():
global lower, upper
from demos.quicksort import quicksort_return
l = random.randint(lower, upper)
arr = [random.randint(0,10) for i in range(l)]
print(arr)
with receiver:
arr = quicksort_return(arr)
print("QuickSorted:", arr)
def testBubbleSort():
global lower, upper
from demos.bubblesort import bubble, bubble_for
l = random.randint(lower, upper)
arr = [random.randint(0,10) for i in range(l)]
print(arr)
with receiver:
arr = bubble_for(arr)
print("BubbleSorted:", arr)
def testHeapSort():
global lower, upper
from demos.heapsort import heapsort
l = random.randint(lower, upper)
arr = [random.randint(0,10) for i in range(l)]
print(arr)
with receiver:
arr = heapsort(arr)
print("HeapSorted:", arr)
def testMatrixMultiplication():
from demos.matmul import matmul_for, matmul_recursive, matmul_strassen, matmul2_for
I = 4
J = 4
K = 4
a = [[random.randint(0, 10) for i in range(J)] for j in range(I)]
b = [[random.randint(0, 10) for i in range(K)] for j in range(J)]
c = [[0 for i in range(K)] for j in range(I)]
print(a, b, c)
with receiver:
# c = matmul_strassen(a, b)
c = matmul_for(a, b, c)
# matmul2_for(a, b, c)
# c = matmul_recursive(a, b)
print("Multiplication: ", a, b, c)
def testDp():
from demos.dp import coinChange
coins = [1,2,5]
amount = 11
with receiver:
ans = coinChange(coins, amount)
print("Coin change: ", coins, amount, ans)
def testTrial():
from demos.simple import trial
a = [1,2,3]
with receiver:
trial(a)
############################################################################
# NUMPY APIs #
############################################################################
############################################################################
# The following are some helper methods which are used to create inputs
# which explore different behaviors of the NumPy APIs. They are individually
# described as they come.
############################################################################
############################################################################
# This function generates a shape which is broadcast compatible to the
# argument shapes `original1` and `original2`. This function assumes
# `original1` and `original2` themselves are broadcast compatible.
# Broadcasting is a NumPy operation where one can perform element wise
# computations of tensors of different shapes, as long as the shapes satisfy
# certain conditions, enabling the NumPy kernel to repeat the tensors along
# certain dimensions to ensure the resultant operands have some common shape.
############################################################################
def get_broadcast_compatible_shape_2(original1, original2, max_dim):
import numpy as np
or1 = list(original1)
or2 = list(original2)
original = []
original_ref = []
while len(or1) > 0 or len(or2) > 0:
if len(or1) > 0 and len(or2) > 0:
a1 = or1.pop()
a2 = or2.pop()
el = min(a1, a2)
el2 = max(a1, a2)
elif len(or1) > 0:
el2 = el = or1.pop()
else:
el2 = el = or2.pop()
original.append(el)
original_ref.append(el2)
original = original[::-1]
original_ref = original_ref[::-1]
if np.random.randint(0, 2):
return original
new_shape = []
for i, i_ in zip(original[::-1], original_ref[::-1]):
if i == 1:
if np.random.randint(0, 2):
if i_ == 1:
new_shape.append(np.random.randint(2, max_dim + 1))
else:
new_shape.append(i_)
else:
new_shape.append(i)
else:
if np.random.randint(0, 2):
new_shape.append(1)
else:
if np.random.randint(0, 2):
new_shape.append(i)
else:
new_shape.append(i_)
if np.random.randint(0, 2):
return new_shape[::-1]
if np.random.randint(0, 2):
new_shape.append(np.random.randint(1, max_dim + 1))
return new_shape[::-1]
############################################################################
# This function generates a shape which is broadcast compatible to the
# argument shape original.
############################################################################
def get_broadcast_compatible_shape(original, max_dim):
import numpy as np
if np.random.randint(0, 2):
return original
new_shape = []
for i in original[::-1]:
if i == 1:
if np.random.randint(0, 2):
new_shape.append(np.random.randint(2, max_dim + 1))
else:
new_shape.append(i)
else:
if np.random.randint(0, 2):
new_shape.append(1)
else:
new_shape.append(i)
if np.random.randint(0, 2):
return new_shape[::-1]
if np.random.randint(0, 2):
new_shape.append(np.random.randint(1, max_dim + 1))
return new_shape[::-1]
############################################################################
# This function generates a shape which is broadcast compatible to the
# argument shape original, while also ensuring that the new shape results
# in a smaller tensor than `original`.
############################################################################
def get_broadcast_compatible_shape_small(original, max_dim):
import numpy as np
if np.random.randint(0, 2):
return original
new_shape = []
for i in original[::-1]:
if i == 1:
new_shape.append(i)
else:
if np.random.randint(0, 2):
new_shape.append(1)
else:
new_shape.append(i)
if np.random.randint(0, 2):
return new_shape[::-1]
return new_shape[::-1]
############################################################################
# This function generates 2 random tensors with have at max `dims`
# dimensions and each dimension can have at most `dim_size_max` elements.
# Hence the maximum size tensor can be
# `dim_size_max * dim_size_max * dim_size_max`
############################################################################
def get_random_init(dims, dim_size_max):
import numpy as np
size_a = tuple([np.random.randint(1,dim_size_max+1) for i in range(np.random.randint(1, dims+1))])
size_b = tuple([np.random.randint(1,dim_size_max+1) for i in range(np.random.randint(1, dims+1))])
a = np.random.uniform(low=-10., high=10., size=size_a).tolist()
b = np.random.uniform(low=-10., high=10., size=size_b).tolist()
return size_a, size_b, a, b
############################################################################
# This function generates two random tensors like `get_random_init`, while
# ensuring the shapes of the two tensors are broadcast compatible
############################################################################
def get_random_init_element_wise(dims, dim_size_max):
import numpy as np
size_a = tuple([np.random.randint(1,dim_size_max+1) for i in range(np.random.randint(1, dims+1))])
size_b = get_broadcast_compatible_shape(size_a, dim_size_max)
a = np.random.uniform(low=-10., high=10., size=size_a).tolist()
b = np.random.uniform(low=-10., high=10., size=size_b).tolist()
return size_a, size_b, a, b
############################################################################
# This function generates 3 random tensors with have at max `dims`
# dimensions and each dimension can have at most `dim_size_max` elements.
# Hence the maximum size tensor can be
# `dim_size_max * dim_size_max * dim_size_max`.
# This function also ensures the shape of all 3 tensors are broadcast
# compatible.
############################################################################
def get_random_init_element_wise_3(dims, dim_size_max):
import numpy as np
size_a = tuple([np.random.randint(1,dim_size_max+1) for i in range(np.random.randint(1, dims+1))])
size_b = get_broadcast_compatible_shape(size_a, dim_size_max)
size_c = get_broadcast_compatible_shape_2(size_a, size_b, dim_size_max)
a = np.random.uniform(low=-10., high=10., size=size_a).tolist()
b = np.random.uniform(low=-10., high=10., size=size_b).tolist()
c = np.random.uniform(low=-10., high=10., size=size_c).tolist()
return size_a, size_b, size_c, a, b, c
############################################################################
# This function generates two random tensors like `get_random_init`, while
# ensuring the shapes of the two tensors are broadcast compatible. However
# it also initializes parameters like `axis`, `keepdims` etc. which are
# commonly used in functions which perform reductions (like sum, max etc.).
############################################################################
def get_random_init_reduction(dims, dim_size_max):
import numpy as np
size = tuple([np.random.randint(1,dim_size_max+1) for i in range(np.random.randint(1, dims+1))])
size_where = get_broadcast_compatible_shape_small(size, dim_size_max)
a = np.random.uniform(low=-10., high=10., size=size).tolist()
if len(size) == 1:
axis = None
else:
axis = np.random.choice([None, np.random.choice(len(size), size=np.random.randint(1, len(size)), replace=False).tolist()])
keepdims = np.random.choice([None, True])
initial = np.random.choice([None, 1])
where = np.random.choice([None, np.random.choice([True, False], p=[0.8, 0.2], size=size_where).tolist()])
return size, size_where, a, axis, keepdims, initial, where
############################################################################
# This function generates two random tensors like `get_random_init`, while
# ensuring the shapes of the two tensors are broadcast compatible. However
# it also initializes parameters like `axis`, `keepdims` etc. which are
# commonly used in functions which perform reductions (like cumsum etc.).
# It initializes `axis` differently and has a slightly different set of
# parameters from `get_random_init_reduction`.
############################################################################
def get_random_init_complex_reduction(dims, dim_size_max):
import numpy as np
size = tuple([np.random.randint(1,dim_size_max+1) for i in range(np.random.randint(1, dims+1))])
a = np.random.uniform(low=-10., high=10., size=size).tolist()
axis = np.random.choice([None, np.random.choice(len(size))])
keepdims = np.random.choice([None, True])
return size, a, axis, keepdims
############################################################################
# DATASET GENERATORS #
############################################################################
### REDUCTION
def testReductionSum(dims, dim_size_max):
import numpy as np
import APIs.sum as sum
size, size_where, a, axis, keepdims, initial, where = get_random_init_reduction(dims, dim_size_max)
print(a, size)
print(axis)
print(keepdims)
print(initial)
print(where, size_where)
with receiver:
ans = sum.sum_1(a, axis=axis, keepdims=keepdims, initial=initial, where=where)
print("reduction sum: ", ans)
def testReductionProd(dims, dim_size_max):
import numpy as np
import APIs.prod as prod
size, size_where, a, axis, keepdims, initial, where = get_random_init_reduction(dims, dim_size_max)
print(a, size)
print(axis)
print(keepdims)
print(initial)
print(where, size_where)
with receiver:
ans = prod.prod_1(a, axis=axis, keepdims=keepdims, initial=initial, where=where)
print("reduction prod: ", ans)
def testReductionMax(dims, dim_size_max):
import numpy as np
import APIs.max as max
size, size_where, a, axis, keepdims, initial, where = get_random_init_reduction(dims, dim_size_max)
print(a, size)
print(axis)
print(keepdims)
print(initial)
print(where, size_where)
with receiver:
ans = max.max_1(a, axis=axis, keepdims=keepdims, initial=initial, where=where)
print("reduction max: ", ans)
def testReductionMin(dims, dim_size_max):
import numpy as np
import APIs.min as min
size, size_where, a, axis, keepdims, initial, where = get_random_init_reduction(dims, dim_size_max)
print(a, size)
print(axis)
print(keepdims)
print(initial)
print(where, size_where)
with receiver:
ans = min.min_1(a, axis=axis, keepdims=keepdims, initial=initial, where=where)
print("reduction min: ", ans)
def testReductionMean(dims, dim_size_max):
import numpy as np
import APIs.mean as mean
size, size_where, a, axis, keepdims, _, where = get_random_init_reduction(dims, dim_size_max)
print(a, size)
print(axis)
print(keepdims)
print(where, size_where)
with receiver:
ans = mean.mean_1(a, axis=axis, keepdims=keepdims, where=where)
print("reduction mean: ", ans)
def testReductionAll(dims, dim_size_max):
import numpy as np
import APIs.all as all
size, size_where, a, axis, keepdims, _, where = get_random_init_reduction(dims, dim_size_max)
a = np.asarray(a).astype(int).tolist()
print(a, size)
print(axis)
print(keepdims)
print(where, size_where)
with receiver:
ans = all.all_1(a, axis=axis, keepdims=keepdims, where=where)
print("reduction all: ", ans)
def testReductionAny(dims, dim_size_max):
import numpy as np
import APIs.any as any
size, size_where, a, axis, keepdims, _, where = get_random_init_reduction(dims, dim_size_max)
a = np.asarray(a).astype(int).tolist()
print(a, size)
print(axis)
print(keepdims)
print(where, size_where)
with receiver:
ans = any.any_1(a, axis=axis, keepdims=keepdims, where=where)
print("reduction any: ", ans)
def testReductionCountNonzero(dims, dim_size_max):
import numpy as np
import APIs.count_nonzero as count_nonzero
size, _, a, axis, keepdims, _, _ = get_random_init_reduction(dims, dim_size_max)
print(a, size)
print(axis)
print(keepdims)
with receiver:
ans = count_nonzero.count_nonzero_1(a, axis=axis, keepdims=keepdims)
print("reduction count_nonzero: ", ans)
def testReductionStd(dims, dim_size_max):
import numpy as np
import APIs.std as std
size, size_where, a, axis, keepdims, _, where = get_random_init_reduction(dims, dim_size_max)
print(a, size)
print(axis)
print(keepdims)
print(where, size_where)
with receiver:
ans = std.std_1(a, axis=axis, keepdims=keepdims, where=where)
print("reduction std: ", ans)
### ELEMENT-WISE
def testAdd(dims, dim_size_max):
import numpy as np
import APIs.add as add
size_a, size_b, a, b = get_random_init_element_wise(dims, dim_size_max)
print(a, size_a)
print(b, size_b)
with receiver:
ans = add.add_1(a, b)
print("add: ", ans)
def testWhere(dims, dim_size_max):
import numpy as np
import APIs.where as where
size_a, size_b, size_c, _, b, c = get_random_init_element_wise_3(dims, dim_size_max)
a = np.random.choice([0, 1], size=size_a).tolist()
print(a, size_a)
print(b, size_b)
print(c, size_c)
with receiver:
ans = where.where_1(a, b, c)
print("where: ", ans)
### COMPLEX REDUCTION
def testReductionCumsum(dims, dim_size_max):
import numpy as np
import APIs.cumsum as cumsum
size, a, axis, _ = get_random_init_complex_reduction(dims, dim_size_max)
print(a, size)
print(axis)
with receiver:
ans = cumsum.cumsum_1(a, axis=axis)
print("reduction cumsum: ", ans)
def testReductionArgmax(dims, dim_size_max):
import numpy as np
import APIs.argmax as argmax
size, a, axis, keepdims = get_random_init_complex_reduction(dims, dim_size_max)
print(a, size)
print(axis)
print(keepdims)
with receiver:
ans = argmax.argmax_1(a, axis=axis, keepdims=keepdims)
print("reduction argmax: ", ans)
def testReductionArgmin(dims, dim_size_max):
import numpy as np
import APIs.argmin as argmin
size, a, axis, keepdims = get_random_init_complex_reduction(dims, dim_size_max)
print(a, size)
print(axis)
print(keepdims)
with receiver:
ans = argmin.argmin_1(a, axis=axis, keepdims=keepdims)
print("reduction argmin: ", ans)
### MATH OPERATIONS
def testDot(dims, dim_size_max):
import numpy as np
import APIs.dot as dot
size_a = ([np.random.randint(1,dim_size_max+1) for i in range(np.random.randint(1, dims+1))])
size_b = ([np.random.randint(1,dim_size_max+1) for i in range(np.random.randint(1, dims+1))])
if len(size_b) == 1:
size_b[-1] = size_a[-1]
elif len(size_b) >= 2:
size_b[-2] = size_a[-1]
size_a = tuple(size_a)
size_b = tuple(size_b)
a = np.random.uniform(low=-10., high=10., size=size_a).tolist()
b = np.random.uniform(low=-10., high=10., size=size_b).tolist()
print(a, size_a)
print(b, size_b)
with receiver:
ans = dot.dot_1(a, b)
print("dot: ", ans)
def testMatmul(dims, dim_size_max):
import numpy as np
import APIs.matmul as matmul
size_a = ([np.random.randint(1,dim_size_max+1) for i in range(np.random.randint(1, dims+1))])
size_b = ([np.random.randint(1,dim_size_max+1) for i in range(np.random.randint(1, dims+1))])
if len(size_b) == 1:
size_b[-1] = size_a[-1]
elif len(size_b) >= 2:
size_b[-2] = size_a[-1]
size_b[:-2] = get_broadcast_compatible_shape(size_a[:-2], dim_size_max)
size_a = tuple(size_a)
size_b = tuple(size_b)
a = np.random.uniform(low=-10., high=10., size=size_a).tolist()
b = np.random.uniform(low=-10., high=10., size=size_b).tolist()
print(a, size_a)
print(b, size_b)
with receiver:
ans = matmul.matmul_1(a, b)
print("matmul: ", ans)
def testOuter(dims, dim_size_max):
import numpy as np
import APIs.outer as outer
size_a, size_b, a, b = get_random_init(dims, dim_size_max)
print(a, size_a)
print(b, size_b)
with receiver:
ans = outer.outer_1(a, b)
print("outer: ", ans)
def testClip(dims, dim_size_max):
import numpy as np
import APIs.clip as clip
size_a, _, a, _ = get_random_init(dims, dim_size_max)
lim1 = np.random.choice([-5, -15])
lim2 = np.random.choice([5, 15])
print(a, size_a)
with receiver:
ans = clip.clip_1(a, lim1, lim2)
print("clip: ", ans)
def testDiff(dims, dim_size_max):
import numpy as np
import APIs.diff as diff
size, a, _, _ = get_random_init_complex_reduction(dims, dim_size_max)
axis = np.random.choice(len(size))
if size[axis] > 2:
n = np.random.choice([1,2])
else:
n = 1
print(a, size)
print(axis)
print(n)
with receiver:
ans = diff.diff_1(a, n, axis)
print("diff: ", ans)
def testArange(dims, dim_size_max):
import numpy as np
import APIs.arange as arange
start = np.random.randint(dim_size_max * 5)
stop = np.random.choice([None, np.random.randint(dim_size_max * 5)])
stop = stop + start if stop is not None else None
step = np.random.choice([None] + [i for i in range(1, dim_size_max)])
print(start)
print(stop)
print(step)
with receiver:
ans = arange.arange_1(start, stop, step)
print("arange: ", ans)
def testLinspace(dims, dim_size_max):
import numpy as np
import APIs.linspace as linspace
size_start = tuple([np.random.randint(1,dim_size_max+1) for i in range(np.random.randint(1, dims+1))])
size_stop = get_broadcast_compatible_shape_small(size_start, dim_size_max)
start = np.random.uniform(low=-10., high=10., size=size_start).tolist()
stop = np.random.uniform(low=-10., high=10., size=size_stop).tolist()
num = np.random.choice([2, 3, dim_size_max])
endpoint = np.random.choice([True, False])
retstep = np.random.choice([True, False])
axis = np.random.choice([np.random.randint(len(size_start))])
print(start)
print(stop)
print(num)
print(endpoint)
print(retstep)
print(axis)
with receiver:
ans = linspace.linspace_1(start, stop, num, endpoint, retstep, axis)
print("linspace: ", ans)
def testAllclose(dims, dim_size_max):
import numpy as np
import APIs.allclose as allclose
size_a, size_b, a, b = get_random_init_element_wise(dims, dim_size_max)
rtol = np.random.choice([1e-5, 1e-4])
atol = np.random.choice([1e-8, 1e-7])
if np.random.randint(0, 2):
b = a
size_b = size_a
equal_nan = np.random.choice([True, False])
print(size_a, a)
print(size_b, b)
print(rtol, atol, equal_nan)
with receiver:
ans = allclose.allclose_1(a, b, rtol, atol, equal_nan)
print("allclose: ", ans)
### SHAPE MANIPULATION
def testTranspose(dims, dim_size_max):
import numpy as np
import APIs.transpose as transpose
size = tuple([np.random.randint(1,dim_size_max+1) for i in range(np.random.randint(2, dims+1))])
a = np.random.uniform(low=-10., high=10., size=size).tolist()
axis = None
print(a, size)
print(axis)
with receiver:
ans = transpose.transpose_1(a, axis)
print("transpose: ", ans)
def testRavel(dims, dim_size_max):
import numpy as np
import APIs.ravel as ravel
size = tuple([np.random.randint(1,dim_size_max+1) for i in range(np.random.randint(2, dims+1))])
a = np.random.uniform(low=-10., high=10., size=size).tolist()
print(a, size)
with receiver:
ans = ravel.ravel_1(a)
print("ravel: ", ans)
def testMoveaxis(dims, dim_size_max):
import numpy as np
import APIs.moveaxis as moveaxis
size = tuple([np.random.randint(1,dim_size_max+1) for i in range(np.random.randint(3, dims+1))])
a = np.random.uniform(low=-10., high=10., size=size).tolist()
source = np.random.choice(len(size) - 1)
destination = (source + 1)
print(a, size)
print(source)
print(destination)
with receiver:
ans = moveaxis.moveaxis_1(a, source, destination)
print("moveaxis: ", ans)
def testSqueeze(dims, dim_size_max):
import numpy as np
import APIs.squeeze as squeeze
size = ([np.random.randint(2,dim_size_max+1) for i in range(np.random.randint(2, dims+1))])
l = len(size)
for i in range(l):
ind = np.random.randint(len(size))
size.insert(ind, 1)
size = tuple(size)
a = np.random.uniform(low=-10., high=10., size=size).tolist()
print(a, size)
with receiver:
ans = squeeze.squeeze_1(a)
print("squeeze: ", ans)
def testReshape(dims, dim_size_max):
import numpy as np
import APIs.reshape as reshape
size = tuple([np.random.randint(2,dim_size_max+1) for i in range(np.random.randint(2, dims+1))])
a = np.random.uniform(low=-10., high=10., size=np.prod(size)).tolist()
newshape = np.random.permutation(size)
print(a, size)
print(newshape)
with receiver:
ans = reshape.reshape_1(a, newshape)
print("reshape: ", ans)
def testAtleast1D(dims, dim_size_max):
import numpy as np
import APIs.atleast_1d as atleast_1d
size = tuple([np.random.randint(1,dim_size_max*dims+1) for i in range(np.random.randint(0, 2))])
a = np.random.uniform(low=-10., high=10., size=size).tolist()
print(a, size)
with receiver:
ans = atleast_1d.atleast_1d_1(a)
print("atleast 1d: ", ans)
### LINEAR OPERATIONS: MULTIPLE ARGUMENTS
def testConcatenate(dims, dim_size_max):
import numpy as np
import APIs.concatenate as concatenate
size = tuple([np.random.randint(1,dim_size_max+1) for i in range(np.random.randint(1, dims+1))])
possibilities = list(range(2, len(size)))
if possibilities == []:
axis = None
else:
axis = np.random.choice([None, np.random.choice(possibilities).tolist()])
N = np.random.randint(2, dims+1) # Number of arrays to concatenate, using dims variable
arrays = []
print(axis)
for i in range(N):
if axis is not None:
size_ = list(size)
size_[axis] = np.random.randint(1, dim_size_max+1) # Concatenated axis may be different in all operands
else:
size_ = tuple([np.random.randint(1,dim_size_max+1) for i in range(np.random.randint(1, dims+1))])
a = np.random.uniform(low=-10., high=10., size=size_).tolist()
print(a, size_)
arrays.append(a)
with receiver:
ans = concatenate.concatenate_1(axis, *arrays)
print("concatenate: ", ans)
def testVstack(dims, dim_size_max):
import numpy as np
import APIs.vstack as vstack
size = tuple([np.random.randint(1,dim_size_max+1) for i in range(np.random.randint(1, dims+1))])
N = np.random.randint(2, dims+1) # Number of arrays to concatenate, using dims variable
arrays = []
for i in range(N):
if len(size) != 1:
size_ = list(size)
size_[0] = np.random.randint(1, dim_size_max+1) # Concatenated axis may be different in all operands
else:
size_ = list(size)
a = np.random.uniform(low=-10., high=10., size=size_).tolist()
print(a, size_)
arrays.append(a)
with receiver:
ans = vstack.vstack_1(*arrays)
print("vstack: ", ans)
def testHstack(dims, dim_size_max):
import numpy as np
import APIs.hstack as hstack
size = tuple([np.random.randint(1,dim_size_max+1) for i in range(np.random.randint(1, dims+1))])
N = np.random.randint(2, dims+1) # Number of arrays to concatenate, using dims variable
arrays = []
for i in range(N):
if len(size) != 1:
size_ = list(size)
size_[1] = np.random.randint(1, dim_size_max+1) # Concatenated axis may be different in all operands
else:
size_ = tuple([np.random.randint(1,dim_size_max+1)])
a = np.random.uniform(low=-10., high=10., size=size_).tolist()
print(a, size_)
arrays.append(a)
with receiver:
ans = hstack.hstack_1(*arrays)
print("vstack: ", ans)
def testStack(dims, dim_size_max):
import numpy as np
import APIs.stack as stack
size = tuple([np.random.randint(1,dim_size_max+1) for i in range(np.random.randint(1, dims+1))])
axis = len(size)
N = np.random.randint(2, dims+1) # Number of arrays to concatenate, using dims variable
arrays = []
print(axis)
for i in range(N):
a = np.random.uniform(low=-10., high=10., size=size).tolist()
print(a, size)
arrays.append(a)
with receiver:
ans = stack.stack_1(axis, *arrays)
print("stack: ", ans)
def testTile(dims, dim_size_max):
import numpy as np
import APIs.tile as tile
size = tuple([np.random.randint(1,dim_size_max+1) for i in range(np.random.randint(1, dims+1))])
a = np.random.uniform(low=-10., high=10., size=size).tolist()
reps = tuple(np.random.choice(np.arange(1,dim_size_max+1), size=np.random.choice(dims)).tolist())
if np.prod(reps) == 1:
reps = (2,)
print(a, size)
print(reps)
with receiver:
ans = tile.tile_1(a, reps)
print("tile: ", ans)
def testAppend(dims, dim_size_max):
import numpy as np
import APIs.append as append
size, arr, _, _ = get_random_init_complex_reduction(dims, dim_size_max)
axis = np.random.choice(len(size))
size_values = list(size)
size_values[axis] = 1 # Concatenated axis may be different in all operands
values = np.random.uniform(low=-10., high=10., size=size_values).tolist()
print(arr, size)
print(values, size_values)
print(axis)
with receiver:
ans = append.append_1(arr, values, axis)
print("append: ", ans)
def testInsert(dims, dim_size_max):
import numpy as np
import APIs.insert as insert
size, arr, axis, _ = get_random_init_complex_reduction(dims, dim_size_max)
if axis is None:
index = np.random.choice(np.prod(size))
else:
index = np.random.choice(size[axis])
value = np.random.randint(-10, 10)
print(arr, size)
print(index)
print(value)
print(axis)
with receiver:
ans = insert.insert_1(arr, index, value, axis)
print("insert: ", ans)
def testRepeat(dims, dim_size_max):
import numpy as np
import APIs.repeat as repeat
size, a, axis, _ = get_random_init_complex_reduction(dims, dim_size_max)
repeats = np.random.randint(2, dim_size_max+1)
print(a, size)
print(repeats)
print(axis)
with receiver:
ans = repeat.repeat_1(a, repeats, axis)
print("repeats: ", ans)
### INITIALIZERS
def testEye(dims, dim_size_max):
import numpy as np
import APIs.eye as eye
N = np.random.randint(1, dim_size_max+1)
M = np.random.choice([None, np.random.randint(1, dim_size_max+1)])
mx = max(N, M) if M is not None else N
k = np.random.choice([0, np.random.randint(-mx, mx+1)]).item() # Weird typecasting being done by np.random.choice
print(N)
print(M)
print(k)
with receiver:
ans = eye.eye_1(N, M, k)
print("eye: ", ans)
def testDiag(dims, dim_size_max):
import numpy as np
import APIs.diag as diag
dim_size_max_2 = (dims * dim_size_max) // 2
# l = np.random.randint(2,dim_size_max_2+1)
# size = tuple([l for i in range(np.random.randint(1, 3))])
# v = np.random.uniform(low=-10., high=10., size=size).tolist()
size, v, _, _ = get_random_init_complex_reduction(2, dim_size_max_2)
k = np.random.choice(np.min(size))
print(v, size)
print(k)
with receiver:
ans = diag.diag_1(v, k)
print("diag: ", ans)
def testFull(dims, dim_size_max):
import numpy as np
import APIs.full as full
size = tuple([np.random.randint(1,dim_size_max+1) for i in range(np.random.randint(1, dims+1))])
val = np.random.choice(dim_size_max)
print(size)
print(val)
with receiver:
ans = full.full_1(size, val)
print("full: ", ans)
### INVOLVE SORTING
def testSort(dims, dim_size_max):
import numpy as np
import APIs.sort as sort
size, a, axis, _ = get_random_init_complex_reduction(dims, dim_size_max)
print(a, size)
print(axis)
with receiver:
ans = sort.sort_1(a, axis)
print("sort: ", ans)
def testArgSort(dims, dim_size_max):
import numpy as np
import APIs.argsort as argsort
size, a, axis, _ = get_random_init_complex_reduction(dims, dim_size_max)
print(a, size)
print(axis)
with receiver:
ans = argsort.argsort_1(a, axis)
print("argsort: ", ans)
def testPercentile(dims, dim_size_max):
import numpy as np
import APIs.percentile as percentile
size, a, axis, keepdims = get_random_init_complex_reduction(dims, dim_size_max)
per = np.random.choice([25, 75, 5, 95]).tolist()
print(a, size)
print(per)
print(axis)
print(keepdims)
with receiver:
ans = percentile.percentile_1(a, per, axis, keepdims)
print("percentile", ans)
def testMedian(dims, dim_size_max):
import numpy as np
import APIs.median as median
size, a, axis, keepdims = get_random_init_complex_reduction(dims, dim_size_max)
print(a, size)
print(axis)
print(keepdims)
with receiver:
ans = median.median_1(a, axis, keepdims)
print("median: ", ans)
def testUnique(dims, dim_size_max):
import numpy as np
import APIs.unique as unique
size, a, _, _ = get_random_init_complex_reduction(dims, dim_size_max)
a = np.asarray(a).astype(int).tolist()
print(a, size)
with receiver:
ans = unique.unique_1(a)
print("unique: ", ans)
def testSearchsorted(dims, dim_size_max):
import numpy as np
import APIs.searchsorted as searchsorted
size_a, a, _, _ = get_random_init_complex_reduction(1, dims * dim_size_max)
size_v, v, _, _ = get_random_init_complex_reduction(1, dim_size_max)
print(a, size_a)
print(v, size_v)
a = np.sort(a).tolist()
with receiver:
ans = searchsorted.searchsorted_1(a, v)
print("searchsorted", ans)
def testTake(dims, dim_size_max):
import numpy as np
import APIs.take as take
size_a, a, axis, _ = get_random_init_complex_reduction(dims, dim_size_max)
size_i, _, _, _ = get_random_init_complex_reduction(dims, dim_size_max)
if axis is not None:
i = np.random.randint(low=0., high=size_a[axis], size=size_i).tolist()
else:
i = np.random.randint(low=0., high=np.prod(size_a), size=size_i).tolist()
i = np.asarray(i).astype(int).tolist()
print(a, size_a)
print(i, size_i)
with receiver:
ans = take.take_1(a, i, axis)
print("take: ", ans)
############################################################################
# Deprecated method used in earlier experiments using a dataset of only
# sorting algorithms.
############################################################################
def generateDataset(mode, num_datapoints):
global receiver
labels = [-1]
import numpy as np
from time import time
def flatten(lol):
return [i for l in lol for i in l]
for i in range(num_datapoints):
st = time()
choice = random.randint(0,5)