-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathextra_opinfo.py
2715 lines (2408 loc) · 88.3 KB
/
extra_opinfo.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
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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
"""
Test data for aten operators which don't exist in PyTorch file:
pytorch/torch/testing/_internal/common_methods_invocations.py.
"""
import functools
import itertools
from typing import Any, List
import torch
import torchvision
from torch import testing as torch_testing
from torch.testing._internal import (
common_device_type,
common_dtype,
common_methods_invocations,
)
from torch.testing._internal.opinfo import core as opinfo_core
S = 5
M = 10
def sample_inputs_scalar_tensor(op_info, device, dtype, requires_grad, **kwargs):
del op_info
del kwargs
del device
del requires_grad
# Not including a scalar tensor in vals because meta tests start failing due to
# lack of meta support for _local_scalar_dense
# torch.tensor(2, device=device)
vals = (-5j, 0j, 1j)
for item in vals:
yield opinfo_core.SampleInput(item, dtype=dtype)
def sample_inputs_bernoulli_p(op_info, device, dtype, requires_grad, **kwargs):
del op_info
shapes = [
[3],
[],
[3, 2],
[2, 3, 2],
]
for shape in shapes:
for p in (0, 0.5, 1):
t = torch_testing.make_tensor(
shape,
low=0,
high=1,
device=device,
dtype=dtype,
requires_grad=requires_grad,
**kwargs,
)
yield opinfo_core.SampleInput(t, args=(p,))
yield opinfo_core.SampleInput(t, kwargs={"p": p})
def sample_inputs_bernoulli_p_deterministic(op_info, device, dtype, requires_grad, **kwargs):
del op_info
shapes = [
[3],
[],
[3, 2],
[2, 3, 2],
]
for shape in shapes:
for p in (0, 1):
t = torch_testing.make_tensor(
shape,
low=0,
high=1,
device=device,
dtype=dtype,
requires_grad=requires_grad,
**kwargs,
)
yield opinfo_core.SampleInput(t, args=(p,))
yield opinfo_core.SampleInput(t, kwargs={"p": p})
def sample_inputs_col2im(op_info, device, dtype, requires_grad, **kwargs):
del op_info
# input_shape, output_size, kernal, dilation, padding, stride
cases = (
(
(1, 12, 12),
(4, 5),
(2, 2),
{"dilation": (1, 1), "padding": (0, 0), "stride": (1, 1)},
),
(
(1, 8, 30),
(4, 5),
(2, 2),
{"dilation": (1, 1), "padding": (1, 1), "stride": (1, 1)},
),
(
(1, 8, 9),
(4, 4),
(2, 2),
{"dilation": (1, 1), "padding": (0, 0), "stride": (1, 1)},
),
(
(1, 8, 25),
(4, 4),
(2, 2),
{"dilation": (1, 1), "padding": (1, 1), "stride": (1, 1)},
),
(
(1, 8, 9),
(4, 4),
(2, 2),
{"dilation": (1, 1), "padding": (1, 1), "stride": (2, 2)},
),
(
(1, 9, 4),
(4, 4),
(3, 3),
{"dilation": (1, 1), "padding": (1, 1), "stride": (2, 2)},
),
(
(1, 18, 16),
(2, 2),
(1, 1),
{"dilation": (2, 2), "padding": (3, 3), "stride": (2, 2)},
),
)
make_arg = functools.partial(
torch_testing.make_tensor, device=device, dtype=dtype, requires_grad=requires_grad
)
for shape, output_size, kernel_size, kwargs in cases:
tensor = make_arg(shape)
yield opinfo_core.SampleInput(tensor, args=(output_size, kernel_size), kwargs=kwargs)
def sample_inputs_conv3d(op_info, device, dtype, requires_grad, **kwargs):
del op_info
make_arg = functools.partial(
torch_testing.make_tensor, device=device, dtype=dtype, requires_grad=requires_grad
)
# Ordered as shapes for input, weight, bias,
# and a dict of values of (stride, padding, dilation, groups)
cases: tuple[tuple[int, ...], tuple[int, ...], tuple[int, ...], dict[str, Any]] = ( # type: ignore[assignment]
(
(1, 3, 3, 224, 224),
(32, 3, 3, 3, 3),
None,
{
"stride": (2, 2, 2),
"padding": (1, 1, 1),
"dilation": (1, 1, 1),
"groups": 1,
},
),
(
(2, 4, 3, 56, 56),
(32, 4, 3, 3, 3),
(32,),
{
"stride": (3, 3, 3),
"padding": (2, 2, 2),
"dilation": (1, 1, 1),
"groups": 1,
},
),
)
for input_shape, weight, bias, kwargs in cases: # type: ignore[assignment]
# Batched
yield opinfo_core.SampleInput(
make_arg(input_shape),
args=(make_arg(weight), make_arg(bias) if bias is not None else bias),
kwargs=kwargs,
)
# Unbatched
yield opinfo_core.SampleInput(
make_arg(input_shape[1:]), # type: ignore[index]
args=(make_arg(weight), make_arg(bias) if bias is not None else bias),
kwargs=kwargs,
)
def sample_inputs_convolution(op_info, device, dtype, requires_grad, **kwargs):
del op_info
make_arg = functools.partial(
torch_testing.make_tensor, device=device, dtype=dtype, requires_grad=requires_grad
)
# Ordered as shapes for input, weight, bias,
# and a dict of values of (stride, padding, dilation, groups)
cases: tuple[tuple[int, ...], tuple[int, ...], tuple[int, ...], dict[str, Any]] = ( # type: ignore[assignment]
(
(1, 3, 4),
(3, 3, 3),
(3,),
{
"stride": (2,),
"padding": (2,),
"dilation": (1,),
"transposed": False,
"output_padding": (0,),
"groups": 1,
},
),
(
(1, 3, 4),
(3, 3, 3),
None,
{
"stride": (2,),
"padding": (2,),
"dilation": (1,),
"transposed": True,
"output_padding": (0,),
"groups": 1,
},
),
(
(1, 3, 224, 224),
(32, 3, 3, 3),
None,
{
"stride": (2, 2),
"padding": (1, 1),
"dilation": (1, 1),
"transposed": False,
"output_padding": (0, 0),
"groups": 1,
},
),
(
(1, 3, 224, 224),
(32, 3, 3, 3),
None,
{
"stride": (2,),
"padding": (1,),
"dilation": (1,),
"transposed": False,
"output_padding": (0, 0),
"groups": 1,
},
),
(
(1, 3, 3, 224, 224),
(32, 3, 3, 3, 3),
(32,),
{
"stride": (2, 2, 2),
"padding": (1, 1, 1),
"dilation": (1, 1, 1),
"transposed": False,
"output_padding": (0, 0, 0),
"groups": 1,
},
),
(
(1, 3, 224, 224, 224),
(32, 3, 3, 3, 3),
(32,),
{
"stride": (2, 2, 2),
"padding": (1, 1, 1),
"dilation": (1, 1, 1),
"transposed": False,
"output_padding": (0, 0, 0),
"groups": 1,
},
),
(
(2, 4, 6, 6),
(4, 1, 3, 3),
(4,),
{
"stride": (3, 2),
"padding": (1, 1),
"dilation": (1, 1),
"transposed": True,
"output_padding": (0, 0),
"groups": 4,
},
),
)
for input_shape, weight, bias, kwargs in cases: # type: ignore[assignment]
yield opinfo_core.SampleInput(
make_arg(input_shape),
args=(make_arg(weight), make_arg(bias) if bias is not None else bias),
kwargs=kwargs,
)
def sample_inputs_embedding_renorm(op_info, device, dtype, requires_grad, **kwargs):
del op_info
del kwargs
def make_input(shape):
return common_methods_invocations.make_tensor(
shape, device=device, dtype=dtype, requires_grad=requires_grad
)
def make_long_input(shape, *, low, high, noncontiguous=False):
return common_methods_invocations.make_tensor(
shape,
device=device,
dtype=torch.long,
low=low,
high=high,
noncontiguous=noncontiguous,
)
for max_norm in (0.5, 1.0, 5.0):
for norm_type in (0.8, 1.0, 2.0, 2.5):
idx = make_long_input((6,), low=0, high=S)
weights = make_input((S, S)) * 2
yield common_methods_invocations.SampleInput(
weights,
args=(idx,),
kwargs={"max_norm": max_norm, "norm_type": norm_type},
)
def sample_inputs_embedding_bag(op_info, device, dtype, requires_grad, **kwargs):
del op_info
del kwargs
def make_input(shape):
return common_methods_invocations.make_tensor(
shape, device=device, dtype=dtype, requires_grad=requires_grad
)
def make_long_input(shape, *, low, high, noncontiguous=False):
return common_methods_invocations.make_tensor(
shape,
device=device,
dtype=torch.long,
low=low,
high=high,
noncontiguous=noncontiguous,
)
def make_per_sample_weight(flag, idx):
# a tensor of float / double weights, or None
# to indicate all weights should be taken to be 1
if flag:
return make_input(idx.reshape(-1).shape)
return None
offsets = [
torch.tensor([0, 2, 3], device=device, dtype=torch.long),
torch.tensor([0, 0, 2], device=device, dtype=torch.long),
torch.tensor([0, 2, 2, 4], device=device, dtype=torch.long),
]
for offset in offsets:
for include_last_offset in (True, False):
for generate_per_sample_weight in (True, False):
for mode in (
0,
1,
2,
): # ('sum', 'mean', 'max')
# per_sample_weights only support mode='sum'
if generate_per_sample_weight and mode in (1, 2): # ('mean', 'max'):
continue
# 1-D index tensor
indices = make_long_input((S,), low=0, high=M)
per_sample_weights = make_per_sample_weight(
generate_per_sample_weight, indices
)
# 0
yield common_methods_invocations.SampleInput(
make_input((M, S)),
args=(indices,),
kwargs={
"offsets": offset,
"mode": mode,
"per_sample_weights": per_sample_weights,
"include_last_offset": include_last_offset,
},
)
indices = make_long_input((S,), low=0, high=M, noncontiguous=True)
per_sample_weights = make_per_sample_weight(
generate_per_sample_weight, indices
)
# 1
yield common_methods_invocations.SampleInput(
make_input((M, S)),
args=(indices,),
kwargs={
"offsets": offset,
"mode": mode,
"per_sample_weights": per_sample_weights,
"include_last_offset": include_last_offset,
},
)
if mode != 2: # "max" mode in 2-D index tensor make aten func crash
# 2-D index tensor
indices = make_long_input((S, S), low=0, high=M)
per_sample_weights = make_per_sample_weight(
generate_per_sample_weight, indices
)
# 2
yield common_methods_invocations.SampleInput(
make_input((M, S)),
args=(indices,),
kwargs={
"offsets": offset,
"mode": mode,
"per_sample_weights": per_sample_weights,
"include_last_offset": include_last_offset,
},
)
indices = make_long_input((S, S), low=0, high=M, noncontiguous=True)
per_sample_weights = make_per_sample_weight(
generate_per_sample_weight, indices
)
# 3
yield common_methods_invocations.SampleInput(
make_input((M, S)),
args=(indices,),
kwargs={
"offsets": offset,
"mode": mode,
"per_sample_weights": per_sample_weights,
"include_last_offset": include_last_offset,
},
)
def sample_inputs_embedding_bag_padding_idx(op_info, device, dtype, requires_grad, **kwargs):
del op_info
del kwargs
def make_input(shape):
return common_methods_invocations.make_tensor(
shape, device=device, dtype=dtype, requires_grad=requires_grad
)
def make_long_input(shape, *, low, high, noncontiguous=False):
return common_methods_invocations.make_tensor(
shape,
device=device,
dtype=torch.long,
low=low,
high=high,
noncontiguous=noncontiguous,
)
def make_per_sample_weight(flag, idx):
# a tensor of float / double weights, or None
# to indicate all weights should be taken to be 1
if flag:
return make_input(idx.reshape(-1).shape)
return None
offsets = [
torch.tensor([0, 2, 3], device=device, dtype=torch.long),
# Below case not work for FullGraph mode, guess due to op.While() bug:
# when the initial condition is False, it still excute the loop body once.
# torch.tensor([0, 0, 2], device=device, dtype=torch.long),
# torch.tensor([0, 2, 2, 4], device=device, dtype=torch.long),
]
for offset in offsets:
for include_last_offset in (True, False):
for generate_per_sample_weight in (True, False):
for mode in (
0,
1,
2,
): # ('sum', 'mean', 'max')
# per_sample_weights only support mode='sum'
if generate_per_sample_weight and mode in (1, 2): # ('mean', 'max'):
continue
for padding_idx in (-1, 0, 1, 2, 3):
# 1-D index tensor
indices = make_long_input((S,), low=0, high=M)
per_sample_weights = make_per_sample_weight(
generate_per_sample_weight, indices
)
# 0
yield common_methods_invocations.SampleInput(
make_input((M, S)),
args=(indices,),
kwargs={
"offsets": offset,
"scale_grad_by_freq": False,
"mode": mode,
"sparse": False,
"per_sample_weights": per_sample_weights,
"include_last_offset": include_last_offset,
"padding_idx": padding_idx,
},
)
indices = make_long_input((S,), low=0, high=M, noncontiguous=True)
per_sample_weights = make_per_sample_weight(
generate_per_sample_weight, indices
)
# 1
yield common_methods_invocations.SampleInput(
make_input((M, S)),
args=(indices,),
kwargs={
"offsets": offset,
"scale_grad_by_freq": False,
"mode": mode,
"sparse": False,
"per_sample_weights": per_sample_weights,
"include_last_offset": include_last_offset,
"padding_idx": padding_idx,
},
)
# if mode != 2: # "max" mode in 2-D index tensor make aten func crash
# # 2-D index tensor
# indices = make_long_input((S, S), low=0, high=M)
# per_sample_weights = make_per_sample_weight(
# generate_per_sample_weight, indices
# )
# # 2
# yield common_methods_invocations.SampleInput(
# make_input((M, S)),
# args=(indices,),
# kwargs={
# "offsets": offset,
# "mode": mode,
# "per_sample_weights": per_sample_weights,
# "include_last_offset": include_last_offset,
# "padding_idx": padding_idx,
# },
# )
# indices = make_long_input((S, S), low=0, high=M, noncontiguous=True)
# per_sample_weights = make_per_sample_weight(
# generate_per_sample_weight, indices
# )
# # 3
# yield common_methods_invocations.SampleInput(
# make_input((M, S)),
# args=(indices,),
# kwargs={
# "offsets": offset,
# "mode": mode,
# "per_sample_weights": per_sample_weights,
# "include_last_offset": include_last_offset,
# "padding_idx": padding_idx,
# },
# )
def sample_inputs__local_scalar_dense(op_info, device, dtype, requires_grad, **kwargs):
del op_info
shapes = (
(),
(1,),
(3,),
(1, 1),
(1, 2),
(2, 1),
(1, 1, 1),
(2, 2, 2),
)
for shape in shapes:
t = torch_testing.make_tensor(
shape,
low=0,
high=1,
device=device,
dtype=dtype,
requires_grad=requires_grad,
**kwargs,
)
yield opinfo_core.SampleInput(t)
def _prepare_data_for_fft_ops(device, dtype, requires_grad=False):
# Adapted from https://github.com/pytorch/pytorch/blob/01069ad4be449f376cf88a56d842b8eb50f6e9b6/torch/testing/_internal/opinfo/core.py#L2448C1-L2541C79
is_fp16_or_chalf = dtype in (torch.complex32, torch.half)
if not is_fp16_or_chalf:
oned_tensor = functools.partial(
opinfo_core.make_tensor,
(31,),
device=device,
dtype=dtype,
requires_grad=requires_grad,
)
nd_tensor = functools.partial(
opinfo_core.make_tensor,
(S, S + 1, S + 2),
device=device,
dtype=dtype,
requires_grad=requires_grad,
)
else:
low = None
high = None
shapes = ((2, 8, 9), (33,))
oned_tensor = functools.partial(
opinfo_core.make_tensor,
shapes[1],
device=device,
low=low,
high=high,
dtype=dtype,
requires_grad=requires_grad,
)
nd_tensor = functools.partial(
opinfo_core.make_tensor,
shapes[0],
device=device,
low=low,
high=high,
dtype=dtype,
requires_grad=requires_grad,
)
return oned_tensor, nd_tensor
def sample_inputs__fft_c2c(self, device, dtype, requires_grad=False, **_):
del self # Unused
oned_tensor, nd_tensor = _prepare_data_for_fft_ops(device, dtype, requires_grad)
for normalization, forward in itertools.product((0, 1, 2), (True, False)):
# 1-D
yield opinfo_core.SampleInput(
oned_tensor(), dim=(0,), normalization=normalization, forward=forward
)
# N-D
for dim in [
(0,),
(1,),
(2,),
(1, 2),
(0, 1),
(0, 1, 2),
]:
yield opinfo_core.SampleInput(
nd_tensor(), dim=dim, normalization=normalization, forward=forward
)
def sample_inputs__fft_r2c(self, device, dtype, requires_grad=False, **_):
del self # Unused
oned_tensor, nd_tensor = _prepare_data_for_fft_ops(device, dtype, requires_grad)
for normalization, one_sided in itertools.product((0, 1, 2), (True, True)):
# 1-D
yield opinfo_core.SampleInput(
oned_tensor(), dim=(0,), normalization=normalization, onesided=one_sided
)
# N-D
for dim in [
(0,),
(1,),
(2,),
(1, 2),
(0, 1),
(0, 1, 2),
]:
yield opinfo_core.SampleInput(
nd_tensor(), dim=dim, normalization=normalization, onesided=one_sided
)
def sample_inputs__fft_c2r(self, device, dtype, requires_grad=False, **_):
del self # Unused
oned_tensor, nd_tensor = _prepare_data_for_fft_ops(device, dtype, requires_grad)
for normalization in (0, 1, 2):
# 1-D
yield opinfo_core.SampleInput(
oned_tensor(), dim=(0,), normalization=normalization, last_dim_size=12
)
# N-D
for dim in [
(0,),
(1,),
(2,),
(1, 2),
(0, 1),
(0, 1, 2),
]:
# Slice
yield opinfo_core.SampleInput(
nd_tensor(), dim=dim, normalization=normalization, last_dim_size=6
)
# Pad
yield opinfo_core.SampleInput(
nd_tensor(), dim=dim, normalization=normalization, last_dim_size=64
)
def _index_variable_bool(shape, max_indices, device):
if not isinstance(shape, tuple):
shape = (shape,)
index = (
torch.rand(*shape, dtype=torch.double, device=device).mul_(max_indices).floor_().bool()
)
return index
def sample_inputs_index_bool(op_info, device, dtype, requires_grad, **kwargs):
del op_info # Unused
del kwargs # Unused
make_arg = functools.partial(
torch_testing.make_tensor, dtype=dtype, device=device, requires_grad=requires_grad
)
s = 5
index_bool = _index_variable_bool(s, s, device=device)
index_bool_2d = _index_variable_bool((s, s), s, device=device)
index_bool_3d = _index_variable_bool((s, s, s), s, device=device)
test_args = [
([index_bool],),
([None, index_bool],),
([None, None, None, index_bool],),
([index_bool, None],),
([index_bool, None, None],),
# Extra index
([None, index_bool, None, index_bool],),
([index_bool, None, index_bool, None],),
([None, index_bool, index_bool, None],),
([index_bool_2d],),
([index_bool_2d, None],),
([index_bool_2d, None, None],),
([None, index_bool_2d],),
([None, None, index_bool_2d],),
([index_bool_3d],),
([index_bool_3d, None],),
([None, index_bool_3d],),
]
for args in test_args:
yield opinfo_core.SampleInput(make_arg((s, s, s, s)), args=args)
def sample_inputs_index(op_info, device, dtype, requires_grad, **kwargs):
del op_info # Unused
del kwargs # Unused
make_arg = functools.partial(
torch_testing.make_tensor, dtype=dtype, device=device, requires_grad=requires_grad
)
s = 5
index_1d = common_methods_invocations.index_variable(2, s, device=device)
index_2d = common_methods_invocations.index_variable((s + 1, 2), s, device=device)
index_3d = common_methods_invocations.index_variable((s + 2, s + 1, 2), s, device=device)
test_args = [
([index_1d],),
([None, index_1d],),
([None, None, None, index_1d],),
([index_1d, None],),
([index_1d, None, None],),
# Extra index
([None, index_1d, None, index_1d],),
([index_1d, None, index_1d, None],),
([None, index_1d, index_1d, None],),
([index_2d],),
([None, index_2d],),
([None, None, None, index_2d],),
([index_2d, None],),
([index_2d, None, None],),
# Extra index
([None, index_2d, None, index_2d],),
([index_2d, None, index_2d, None],),
([None, index_2d, index_2d, None],),
([index_3d],),
([None, index_3d],),
([None, None, None, index_3d],),
([index_3d, None],),
([index_3d, None, None],),
# Extra index
([None, index_3d, None, index_3d],),
([index_3d, None, index_3d, None],),
([None, index_3d, index_3d, None],),
# Mixed indices
([None, index_3d, index_1d, index_2d],),
# All indices are not None
([index_2d, index_3d, index_1d],),
([index_2d, index_3d, index_1d, index_2d],),
]
for args in test_args:
yield opinfo_core.SampleInput(make_arg((s, s, s, s)), args=args)
def sample_inputs_index_put(op_info, device, dtype, requires_grad, **kwargs):
del op_info
del kwargs
make_arg = functools.partial(
torch_testing.make_tensor, device=device, dtype=dtype, requires_grad=requires_grad
)
cases = [
# Cases: one None
((1, 3, 4), [None, torch.arange(2, device=device), None], (1, 2, 4)),
((10, 3, 4), [torch.arange(5, device=device), None, None], (5, 3, 4)),
((10, 3, 4, 6), [None, None, None, torch.arange(3, device=device)], (10, 3, 4, 3)),
# Cases: two None
(
(10, 3, 4),
[None, torch.arange(3, device=device), torch.arange(3, device=device)],
(10, 3),
),
(
(10, 3, 4, 6),
[
torch.arange(2, device=device),
None,
torch.arange(2, device=device),
torch.arange(2, device=device),
],
(2, 3),
),
(
(10, 3, 4),
[torch.arange(2, device=device), torch.arange(2, device=device), None],
(2, 4),
),
# Cases: Single indexing
((10, 3, 4), [None, None, torch.tensor([0], device=device)], (10, 3, 1)),
((10, 3, 4), [torch.tensor([0], device=device), None, None], (1, 3, 4)),
((10, 3, 4, 6), [None, torch.tensor([0], device=device), None, None], (10, 1, 4, 6)),
# Cases: Single element
(
(10, 3, 4),
[
torch.tensor([0], device=device),
torch.tensor([0], device=device),
torch.tensor([0], device=device),
],
(1,),
),
# Cases: Multidimensional index
(
(10, 3),
[torch.arange(8, dtype=torch.int64, device=device).reshape((-1, 4))],
(2, 4, 3),
),
]
for data_shape, indices, values_shape in cases: # type: ignore[misc]
data = make_arg(data_shape)
values = make_arg(values_shape) # type: ignore[has-type]
yield opinfo_core.SampleInput(data, indices, values)
def sample_inputs_layer_norm(op_info, device, dtype, requires_grad, **kwargs):
del op_info # unused
del kwargs
make_arg = functools.partial(
torch_testing.make_tensor, device=device, dtype=dtype, requires_grad=requires_grad
)
# Ordered as input shape, normalized_shape, eps
cases: tuple[tuple[int], tuple[int], float] = ( # type: ignore[assignment]
((1, 2, 3), (1, 2, 3), 0.5),
((2, 2, 3), (2, 3), -0.5),
((1,), (1,), 1e-5),
((1, 2), (2,), 1e-5),
((0, 1), (1,), 1e-5),
)
for input_shape, normalized_shape, eps in cases: # type: ignore[misc]
# Shape of weight and bias should be the same as normalized_shape
weight = make_arg(normalized_shape) # type: ignore[has-type]
bias = make_arg(normalized_shape) # type: ignore[has-type]
yield opinfo_core.SampleInput(
make_arg(input_shape), # type: ignore[has-type]
args=(normalized_shape, weight, bias, eps), # type: ignore[has-type]
)
yield opinfo_core.SampleInput(
make_arg(input_shape), # type: ignore[has-type]
args=(normalized_shape, None, bias, eps), # type: ignore[has-type]
)
yield opinfo_core.SampleInput(
make_arg(input_shape), # type: ignore[has-type]
args=(normalized_shape, weight, None, eps), # type: ignore[has-type]
)
yield opinfo_core.SampleInput(
make_arg(input_shape), # type: ignore[has-type]
args=(normalized_shape, None, None, eps), # type: ignore[has-type]
)
def sample_inputs_like_fns(self, device, dtype, requires_grad, **kwargs):
del self # Unused
inputs = [
((), {}),
((S, S), {}),
((0, S, 0), {}),
((S,), {}),
((S,), {"dtype": dtype}),
# Hard-code some dtypes/devices. We want to test cases where the
# (dtype, device) is different from the input's (dtype, device)
((S,), {"dtype": torch.double}),
]
for shape, kwargs in inputs:
t = torch_testing.make_tensor(
shape, dtype=dtype, device=device, low=None, high=None, requires_grad=requires_grad
)
yield opinfo_core.SampleInput(t, **kwargs)
def sample_inputs__log_softmax(
op_info,
device,
dtype,
requires_grad,
**kwargs,
):
del op_info # Unused
make_arg = functools.partial(
torch_testing.make_tensor, device=device, dtype=dtype, requires_grad=requires_grad
)
cases = [
((S,), (0,)),
((S, S), (0,)),
((S, S), (1,)),
((S, S), (-1,)),
((S, M, S), (2,)),
((S, 0, 0), (-1,)),
]
for (shape, dim), half_to_float in itertools.product(cases, (False,)):
# NOTE: softmax with half to float conversion is not supported on CPU
# So we don't test it here
kwargs = dict(half_to_float=half_to_float)
yield opinfo_core.SampleInput(make_arg(shape), args=dim, kwargs=kwargs)
def sample_inputs_max_pool_empty_strides(op_info, device, dtype, requires_grad, **kwargs):
make_arg = functools.partial(
torch_testing.make_tensor, device=device, dtype=dtype, requires_grad=False
)
# FIXME: (RuntimeError: non-empty 3D or 4D (batch mode) tensor expected for input)
params_generator_type_dict = {
"ops.aten.max_pool1d": _TestParamsMaxPool1dEmptyStride,
"ops.aten.max_pool2d": _TestParamsMaxPool2dEmptyStride,
"ops.aten.max_pool3d": _TestParamsMaxPool3dEmptyStride,
}
params_generator = params_generator_type_dict[op_info.name]()
for (shape, memory_format), kwargs in params_generator.gen_input_params():
arg = make_arg(shape).to(memory_format=memory_format).requires_grad_(requires_grad)
yield opinfo_core.SampleInput(arg, kwargs=kwargs)
def sample_inputs_max_pool1d_with_indices(op_info, device, dtype, requires_grad, **kwargs):
del op_info
make_arg = functools.partial(
torch_testing.make_tensor, device=device, dtype=dtype, requires_grad=False
)
params_generator = (
common_methods_invocations._TestParamsMaxPool1d() # pylint: disable=protected-access
)
for (shape, memory_format), kwargs in params_generator.gen_input_params():
arg = make_arg(shape).to(memory_format=memory_format).requires_grad_(requires_grad)
yield opinfo_core.SampleInput(arg, kwargs=kwargs)
def sample_inputs_max_pool2d_with_indices(op_info, device, dtype, requires_grad, **kwargs):
del op_info
make_arg = functools.partial(
torch_testing.make_tensor, device=device, dtype=dtype, requires_grad=False
)
params_generator = (
common_methods_invocations._TestParamsMaxPool2d() # pylint: disable=protected-access
)
for (shape, memory_format), kwargs in params_generator.gen_input_params():
arg = make_arg(shape).to(memory_format=memory_format).requires_grad_(requires_grad)
yield opinfo_core.SampleInput(arg, kwargs=kwargs)
def sample_inputs_max_pool3d_with_indices(op_info, device, dtype, requires_grad, **kwargs):
del op_info