forked from Lightning-AI/lightning-thunder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_interpreter.py
More file actions
3604 lines (2591 loc) · 81.8 KB
/
Copy pathtest_interpreter.py
File metadata and controls
3604 lines (2591 loc) · 81.8 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
from collections.abc import Iterable, Sequence
from contextlib import redirect_stdout
from functools import partial, wraps
import io
import sys
import dis
import weakref
from collections.abc import Callable
import pytest
import torch
from torch.testing import assert_close
from thunder.tests.framework import IS_WINDOWS
import thunder
from thunder.core.interpreter import (
is_jitting_with_raise,
is_jitting,
make_opaque,
interpret,
InterpreterError,
print_interpreter_log,
last_interpreter_log,
last_interpreted_instructions,
)
#
# Test suite for core Python interpreter functionality
#
interpret_no_tracking = interpret
# This wraps the jit call into a tracking one (using a wrapper function
# rather than partial to get a nice test name).
def interpret_tracking(*args, **kwargs):
return interpret(*args, with_provenance_tracking=True, **kwargs)
# This will be called by PyTest and parametrize each test that has
# a jit attribute (all?) with versions that use jit and jit_tracking.
def pytest_generate_tests(metafunc):
if "jit" in metafunc.fixturenames:
metafunc.parametrize("jit", [interpret, interpret_tracking])
def skipif_python_3_11_plus(f):
if sys.version_info >= (3, 11):
return pytest.mark.skip(f, reason=f"not yet implemented for Python 3.11+, got {sys.version_info=}")
return f
def test_no_return(jit):
def foo():
pass
jfoo = jit(foo)
assert jfoo() == foo()
def test_constant_return(jit):
def foo():
return 5
jfoo = jit(foo)
assert jfoo() == foo()
def test_constant_addition(jit):
def foo():
return 3 + 5
jfoo = jit(foo)
assert jfoo() == foo()
def test_input_number_addition(jit):
def foo(a, b):
return a + 2 + b
jfoo = jit(foo)
args = (5, 2)
assert jfoo(*args) == foo(*args)
def test_input_tensor_addition(jit):
def foo(a, b):
return a + 2 + b
jfoo = jit(foo)
args = (4, 3)
thunder_result = jfoo(*args)
python_result = foo(*args)
assert_close(thunder_result, python_result)
def test_dup_top_two(jit):
def foo(a):
a[-1] += a.pop()
return a
if "DUP_TOP_TWO" in dis.opmap.keys():
assert any(i.opname == "DUP_TOP_TWO" for i in dis.get_instructions(foo))
assert jit(foo)([1, 2, 3]) == foo([1, 2, 3])
def test_constant_if(jit):
def foo(a, b):
if 3 < 5:
return a + b
else:
assert False
jfoo = jit(foo)
args = (4, 3)
thunder_result = jfoo(*args)
python_result = foo(*args)
assert_close(thunder_result, python_result)
def test_if(jit):
def foo(a, b):
if a < b:
return a
elif b > a:
return b
else:
return 0
jfoo = jit(foo)
cases = (
(5, 3),
(9, 12),
(2, 2),
)
for case in cases:
assert jfoo(*case) == foo(*case)
def test_while(jit):
# produces POP_JUMP_BACKWARD_IF_TRUE/FALSE in 3.11
def foo(arr):
i = 0
res = []
v = arr[0]
while v:
res.append(i)
i = i + 1
v = arr[i]
return res
def bar(arr):
i = 0
res = []
v = arr[0]
while not v:
res.append(i)
i = i + 1
v = arr[i]
return res
def baz(arr):
i = 0
res = []
v = arr[0]
while v is not None:
res.append(i)
i = i + 1
v = arr[i]
return res
def tom(arr):
i = 0
res = []
v = arr[0]
while v is None:
res.append(i)
i = i + 1
v = arr[i]
return res
foo_arr = [True, True, False, True]
assert foo(foo_arr) == jit(foo)(foo_arr)
bar_arr = [False, False, True]
assert bar(bar_arr) == jit(bar)(bar_arr)
baz_arr = [False, False, None]
assert baz(baz_arr) == jit(baz)(baz_arr)
tom_arr = [None, None, False]
assert tom(tom_arr) == jit(tom)(tom_arr)
def test_and_or(jit):
# JUMP_IF_TRUE/FALSE_OR_POP
def foo(a, b):
return a and b
def bar(a, b):
return a or b
jfoo = jit(foo)
jbar = jit(bar)
cases = (
(True, True),
(True, False),
(False, True),
(False, False),
(object(), True),
(object(), False),
)
for case in cases:
assert jfoo(*case) == foo(*case)
assert jbar(*case) == bar(*case)
def test_dunder_bool(jit):
jitting = False
class mycls:
def __init__(self, value):
self.value = value
# True if self.value is even
def __bool__(self):
assert is_jitting_with_raise() == jitting
return (self.value % 2) == 0
def foo(a):
if a:
return 1
return -1
jfoo = jit(foo)
cases = (
(mycls(4),),
(mycls(5),),
)
for case in cases:
jitting = False
r = foo(*case)
jitting = True
jr = jfoo(*case)
assert r == jr
def test_dunder_bool_instance(jit):
jitting = False
class X:
def __bool__(self):
assert is_jitting_with_raise() == jitting
return False
x = X()
jitting = False
bx = bool(x)
jitting = True
jbx = jit(bool)(x)
assert bx == jbx == False
x.__bool__ = lambda: True # dunder methods use class attribute, not instance attribute.
jitting = False
bx = bool(x)
jitting = True
jbx = jit(bool)(x)
assert bx == jbx == False
def test_function_call(jit):
jitting = False
def fn(fn):
assert is_jitting_with_raise() == jitting
return fn
jitting = False
r = fn(fn)
jitting = True
jr = jit(fn)(fn)
assert r == jr
jitting = False
r = fn(fn=fn)
jitting = True
jr = jit(fn)(fn=fn)
assert r == jr
def test_nested_function_call(jit):
jitting = False
def bar(a, b):
assert is_jitting_with_raise() == jitting
return a + b
def foo(a, b):
assert is_jitting_with_raise() == jitting
return bar(a + 1, b)
jfoo = jit(foo)
args = (4, 3)
python_result = foo(*args)
jitting = True
thunder_result = jfoo(*args)
assert_close(thunder_result, python_result)
def test_call_function_ex(jit):
jitting = False
def foo(a, b):
assert is_jitting_with_raise() == jitting
return a + b
def argsplat(*args):
assert is_jitting_with_raise() == jitting
return foo(*args)
def kwargsplat(**kwargs):
assert is_jitting_with_raise() == jitting
return foo(**kwargs)
if sys.version_info < (3, 14):
# Python 3.14 has no arg in call function ex.
assert any(i.opname == "CALL_FUNCTION_EX" and not i.arg & 1 for i in dis.get_instructions(argsplat))
assert any(i.opname == "CALL_FUNCTION_EX" and i.arg & 1 for i in dis.get_instructions(kwargsplat))
kwargs = {"a": 1, "b": 2}
jitting = False
res1 = argsplat(*kwargs.values())
res2 = kwargsplat(**kwargs)
jitting = True
jres1 = jit(argsplat)(*kwargs.values())
jres2 = jit(kwargsplat)(**kwargs)
assert_close(res1, jres1)
assert_close(res2, jres2)
@pytest.mark.skipif(
sys.version_info >= (3, 14),
reason="Python 3.14+ do not implement BUILD_CONST_KEY_MAP",
)
def test_build_const_key_map(jit):
def fn1(a, b):
return {"a": a, "b": b}
# test order for collisions
def fn2(a, b):
return {"a": a, "a": b} # noqa: F601
assert any(i.opname == "BUILD_CONST_KEY_MAP" for i in dis.get_instructions(fn1))
assert any(i.opname == "BUILD_CONST_KEY_MAP" for i in dis.get_instructions(fn2))
jfn1 = jit(fn1)
jfn2 = jit(fn2)
assert jfn1(1, 2) == fn1(1, 2)
assert jfn2(1, 2) == fn2(1, 2)
def test_build_map_dict_merge(jit):
def addall(*args, **kwargs):
return sum(args) + sum(kwargs.values())
def foo(*args, **kwargs):
return addall(*args, **kwargs)
assert any(i.opname == "BUILD_MAP" for i in dis.get_instructions(foo))
assert any(i.opname == "DICT_MERGE" for i in dis.get_instructions(foo))
jfoo = jit(foo)
args = (4, 3)
kwargs = {"a": 1, "b": 2}
thunder_result = jfoo(*args, **kwargs)
python_result = foo(*args, **kwargs)
with pytest.raises(KeyError, match="got multiple values for keyword argument"):
d = {"a": 3, "b": 4}
def mergefail(**kwargs):
return addall(**kwargs, **d)
jfail = jit(mergefail)
jfail(**kwargs)
assert_close(thunder_result, python_result)
def test_dict_update(jit):
jitting = False
def addall(*args, **kwargs):
assert is_jitting_with_raise() == jitting
return sum(args) + sum(kwargs.values())
def foo(*args, **kwargs):
assert is_jitting_with_raise() == jitting
return addall(*args, **{**kwargs, "x": 1})
assert any(i.opname == "DICT_UPDATE" for i in dis.get_instructions(foo))
args = (4, 3)
kwargs = {"a": 1, "b": 2}
jitting = False
python_result = foo(*args, **kwargs)
jitting = True
thunder_result = jit(foo)(*args, **kwargs)
assert_close(thunder_result, python_result)
def test_inner_function_definition(jit):
def foo(a, b):
def bar(a, b):
return a + b
return bar(a + 1, b)
jfoo = jit(foo)
args = (4, 3)
thunder_result = jfoo(*args)
python_result = foo(*args)
assert_close(thunder_result, python_result)
def foo(a, b):
def bar(a, b=b):
return a + b
return bar(a + 1)
assert_close(foo(*args), jit(foo)(*args))
def foo(a, b):
def bar(a: int, *, b: int = b):
return a + b
return bar(a + 1)
assert_close(foo(*args), jit(foo)(*args))
def test_inner_closure(jit):
# NOTE The addition of closing over value also tests
# the STORE_DEREF opcode
def foo(a, b):
value = 5
def bar(a):
return a + b + value
return bar(a + 1)
jfoo = jit(foo)
args = (4, 3)
thunder_result = jfoo(*args)
python_result = foo(*args)
assert_close(thunder_result, python_result)
def test_delete_deref(jit):
def foo(a, b):
value = 5
def bar(a):
nonlocal value
del value
return a + b + value
return bar(a + 1)
jfoo = jit(foo)
args = (4, 3)
with pytest.raises(NameError, match="'value'"):
foo(*args)
with pytest.raises(NameError, match="'value'"):
jfoo(*args)
def test_locals_globals(jit):
def fn():
funny_name_nowhere_else = True
return locals() | globals()
assert "test_locals_globals" in jit(fn)()
assert "funny_name_nowhere_else" in jit(fn)()
def test_unpack_sequence(jit):
def foo(tup):
a, b = tup
return a + b
def bar(tup):
a, b = map(lambda x: x, tup) # unpack iterable
return a + b
jfoo = jit(foo)
jbar = jit(bar)
args = (4, 3)
thunder_result = jfoo(args)
python_result = foo(args)
assert_close(thunder_result, python_result)
thunder_result = jbar(args)
python_result = bar(args)
assert_close(thunder_result, python_result)
def test_exception_traceback(jit):
def bar(a):
raise ValueError(f"I don't like {a}")
def foo(b):
return bar(b + 1)
jfoo = jit(foo)
args = (4,)
with pytest.raises(ValueError) as excinfo:
jfoo(*args)
tb_string = "".join(str(tbe) for tbe in excinfo.traceback)
assert "in foo\n" in tb_string
assert "in bar\n" in tb_string
def test_finally(jit):
jitting = False
arr = []
def foo():
try:
assert is_jitting_with_raise() == jitting
arr.append(1)
raise ValueError("test")
arr.append(2)
except KeyError:
assert is_jitting_with_raise() == jitting
arr.append(3)
except ValueError:
assert is_jitting_with_raise() == jitting
arr.append(4)
raise
finally:
assert is_jitting_with_raise() == jitting
arr.append(5)
with pytest.raises(ValueError):
jitting = False
foo()
arr_orig = arr
arr = []
with pytest.raises(ValueError):
jitting = True
jit(foo)()
assert arr_orig == arr
def test_raise(jit):
msg = "lorem ipsum"
jitting = False
class ExampleException(ValueError):
def __init__(self):
assert is_jitting_with_raise() == jitting
super().__init__(msg)
def foo():
raise ExampleException # Constructed implicitly
jfoo = jit(foo)
with pytest.raises(ExampleException) as excinfo:
jitting = False
foo()
with pytest.raises(ExampleException) as excinfo:
jitting = True
jfoo()
assert msg in str(excinfo.value)
def test_bare_except(jit):
msg = "lorem ipsum"
jitting = False
def bare_except():
try:
assert is_jitting_with_raise() == jitting
raise ValueError(msg)
except Exception:
assert is_jitting_with_raise() == jitting
return True
assert bare_except() == True
jitting = True
assert jit(bare_except)() == True
def test_trivial_try_finally(jit):
def trivial_try_finally():
try:
pass
finally:
return True
assert jit(trivial_try_finally)() == True
def test_try_finally(jit):
def try_finally():
try:
var = False
raise ValueError
except ValueError:
var = True
finally:
return var
assert jit(try_finally)() == True
def test_match_exception(jit):
def match_exception():
error_set = (ValueError, IndexError)
try:
raise ValueError
except error_set:
return True
assert jit(match_exception)() == True
def test_match_as(jit):
msg = "lorem ipsum"
def match_as():
try:
raise ValueError(msg)
except ValueError as e:
return str(e)
assert msg in jit(match_as)()
def test_list(jit):
def foo():
arr = [1, 2, 3]
arr = arr.copy()
arr[3:] = arr[:2]
arr[0] = arr[-1]
del arr[2]
return arr
assert foo() == jit(foo)()
def test_raise_external(jit):
msg = "lorem ipsum"
def raise_external():
raise ValueError(msg)
with pytest.raises(ValueError) as excinfo:
jit(raise_external)()
assert msg in str(excinfo.value)
def test_raise_from(jit):
msg = "lorem ipsum"
def raise_from():
try:
raise ValueError(msg) from IndexError(msg)
except ValueError as e:
return (str(e), str(e.__cause__))
res = jit(raise_from)()
assert msg in res[0] and msg in res[1]
def test_raise_from_external(jit):
msg = "lorem ipsum"
def raise_from_external():
raise ValueError(msg) from IndexError(msg)
with pytest.raises(ValueError) as excinfo:
jit(raise_from_external)()
e = excinfo.value
assert type(e) == ValueError
assert type(e.__cause__) == IndexError and msg in str(e.__cause__), excinfo.value
def test_nested_try_except(jit):
def nested_try_except():
try:
raise ValueError
except ValueError as e1:
try:
raise IndexError from e1
except IndexError:
pass
return True
assert jit(nested_try_except)() == True
def test_inner_nested_try_except(jit):
def inner_nested_try_except():
try:
try:
raise ValueError
except ValueError:
pass
except Exception:
return False
return True
assert jit(inner_nested_try_except)() == True
def test_cross_function_exceptions(jit):
jitting = False
def foo():
assert is_jitting_with_raise() == jitting
def bar():
assert is_jitting_with_raise() == jitting
raise ValueError
bar()
def cross_function_exceptions():
try:
assert is_jitting_with_raise() == jitting
foo()
except ValueError:
assert is_jitting_with_raise() == jitting
return True
jitting = False
assert cross_function_exceptions() == True
jitting = True
assert jit(cross_function_exceptions)() == True
def test_stop_exception_no_leak(jit):
class Identity(torch.nn.Module):
def forward(self, x):
for p in self.parameters():
pass
return x
def foo():
model = thunder.jit(Identity())
x = torch.randn(16, 16)
model(x)
return weakref.ref(x)
weak_x = foo()
assert weak_x() is None
def test_exception_no_leak(jit):
class Identity(torch.nn.Module):
@staticmethod
def raises():
raise RuntimeError("Exc")
def forward(self, x):
try:
self.raises()
except RuntimeError:
pass
return x
def foo():
model = thunder.jit(Identity())
x = torch.randn(16, 16)
model(x)
return weakref.ref(x)
weak_x = foo()
assert weak_x() is None
def test_uncaught_exception_no_leak():
class Identity(torch.nn.Module):
def forward(self, x):
raise RuntimeError("FOOBAR")
return x
def main():
with torch.device("cpu"):
model = thunder.jit(Identity())
x = torch.randn(16, 16)
try:
model(x)
except Exception:
pass
return weakref.ref(x)
weak_x = main()
assert weak_x() is None
def test_walrus_operator(jit):
def foo(a, b):
c = (_a := b)
return c
if "DUP_TOP" in dis.opmap.keys():
assert any(i.opname == "DUP_TOP" for i in dis.get_instructions(foo))
jfoo = jit(foo)
assert jfoo(3, 8) == foo(3, 8)
def test_build_map(jit):
def foo(a, b):
return {0: a, 1: b, 2: 3, "a": 4, a: 5}
jfoo = jit(foo)
# a, b
cases = (
(-3, 9),
(1, 1),
(0, 1),
(2, 5),
)
for a, b in cases:
assert jfoo(a, b) == foo(a, b)
def test_map_add_set_add(jit):
def fn():
d = {i: i * 2 for i in range(10)}
s = {i * 2 for i in range(10)}
return d, s
jfn = jit(fn)
assert jfn() == fn()
def test_kwargs(jit):
def foo(a, b, *, c=2):
return a + b + c
jfoo = jit(foo)
assert jfoo(2, 3) == foo(2, 3)
assert jfoo(a=2, b=7, c=3) == foo(a=2, b=7, c=3)
# Same case as above except c can be specified positionally
def foo(a, b, c=2):
return a + b + c
jfoo = jit(foo)
assert jfoo(2, 3) == foo(2, 3)
assert jfoo(a=2, b=7, c=3) == foo(a=2, b=7, c=3)
def test_args_kwargs(jit):
def bar(a, b):
return a + b
def foo(a, **kwargs):
return bar(a, **kwargs)
jfoo = jit(foo)
assert jfoo(2, b=3) == foo(2, b=3)
assert jfoo(a=2, b=3) == foo(a=2, b=3)
def test_partials(jit):
def foo(a, b, c):
return a - b * c
pfoo = partial(foo, 2, c=3)
jpfoo = jit(pfoo)
assert jpfoo(4) == pfoo(4)
assert jpfoo(-9) == pfoo(-9)
ppfoo = partial(pfoo, -5)
jppfoo = jit(ppfoo)
assert jppfoo() == ppfoo()
# Tests that keywords "stack" as expected (later partials take precedence)
pfoo = partial(foo, c=2)
ppfoo = partial(pfoo, c=-2)
jppfoo = jit(ppfoo)
assert jppfoo(7, 9) == ppfoo(7, 9)
assert jppfoo(7, 9, c=4) == ppfoo(7, 9, c=4)
# Tests that args "stack" as expected
pfoo = partial(foo, 7)
ppfoo = partial(pfoo, 9)
jppfoo = jit(ppfoo)
assert jppfoo(5) == ppfoo(5)
assert jppfoo(-3) == ppfoo(-3)
def test_using_imported_modules(jit):
import operator
def foo(a, b):
return operator.add(a, b)
jfoo = jit(foo)
assert jfoo(3, 5) == foo(3, 5)
def test_reduce(jit):
import functools
import operator
# Trivial reduce over native types {
def foo(a):
return functools.reduce(operator.add, a, 0)
jfoo = jit(foo)
assert jfoo((1, 2, 3)) == foo((1, 2, 3)) == 6
# }
# Reduce over Tensor.shape {
def foo(a):
return functools.reduce(operator.add, a.shape, 0)
jfoo = jit(foo)
assert jfoo(torch.rand(1, 2, 3)) == foo(torch.rand(1, 2, 3)) == 6
# }
# Custom Iterable over Tensor.shape {
class mycls(object):
def __init__(self, t):
self.t = t