forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_unspec.py
624 lines (504 loc) · 20.5 KB
/
test_unspec.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
# Owner(s): ["module: dynamo"]
import math
import random
import unittest
import numpy as np
import torch
import torch._dynamo.test_case
import torch._dynamo.testing
import torch.nn.functional as F
from torch._dynamo.comptime import comptime
from torch._dynamo.testing import CompileCounter, same
from torch.testing._internal.logging_utils import logs_to_string
# The intention of this test file is you should put test cases specifically
# for assume_static_by_default=False, aka you want to YOLO make everything as
# dynamic as possible. If you want to test the more normal situation where
# you assume static by default, put it in a regular test file and
# test_dynamic_shapes will cover both the YOLO and non-YOLO cases.
@torch._dynamo.config.patch(assume_static_by_default=False)
class UnspecTests(torch._dynamo.test_case.TestCase):
def test_numpy_correctness(self):
def fn(x, y, z):
xy = [x + y, y, False]
np_x = x.numpy()
np_y = y.numpy()
return {
"x": x,
"z": z,
"a": np_y.sum(),
"b": xy,
"c": np_y[0][0] / 68,
"d": np_x.sum(),
"e": np_x + np_y,
}, x + np_y.sum() + z
x = torch.tensor([[1.0, 2.0], [3.0, 4.0]], dtype=torch.float64)
y = torch.ones([2, 2], dtype=torch.int64)
z = np.int64(12)
res1 = fn(x, y, z)
cnts = torch._dynamo.testing.CompileCounter()
opt_fn = torch._dynamo.optimize(cnts)(fn)
res2 = opt_fn(x, y, z)
self.assertEqual(res1, res2)
def test_no_recompilations(self):
# no recompilations if passing on different numpy int values
def fn(x, y):
return {"a": x + 1, "b": y / 2}
x = torch.tensor([[1.0, 2.0], [3.0, 4.0]], dtype=torch.float64)
cnts = torch._dynamo.testing.CompileCounter()
opt_fn = torch._dynamo.optimize(cnts)(fn)
for i in range(10):
opt_fn(x, np.int64(i))
self.assertEqual(cnts.frame_count, 1)
self.assertEqual(cnts.op_count, 2)
@unittest.expectedFailure # array scalars decay to 0D arrays
def test_builtin_max_min(self):
# test unspecialized primitive max/min
def fn(x, y, z):
return z + 1, max(x, y), min(x - 4, y)
x = np.int64(12)
y = 10
z = torch.tensor([[1.0, 2.0], [3.0, 4.0]], dtype=torch.float64)
res1 = fn(x, y, z)
cnts = torch._dynamo.testing.CompileCounter()
opt_fn = torch._dynamo.optimize(cnts)(fn)
res2 = opt_fn(x, y, z)
self.assertTrue(same(res1, res2, relax_numpy_equality=True))
def test_feed_random_values_into_graph_only(self):
def fn(shape):
torch.manual_seed(123)
x = torch.randn(shape, device="cpu") * random.randint(30, 100)
return x
shape = [2, 3]
random.seed(1)
res1 = fn(shape)
cnts = torch._dynamo.testing.CompileCounter()
opt_fn = torch._dynamo.optimize(cnts)(fn)
random.seed(1)
res2 = opt_fn(shape)
self.assertTrue(same(res1, res2))
def test_random_values_with_graph_break(self):
def fn(x):
r1 = random.random()
y = x + random.uniform(10, 20)
y.sum().item()
r2 = random.randint(2, 18) # no graph output in this frame
y.sum().item()
return y + r1, r2
x = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
random.seed(1)
res1 = fn(x)
cnts = torch._dynamo.testing.CompileCounter()
opt_fn = torch._dynamo.optimize(cnts)(fn)
random.seed(1)
res2 = opt_fn(x)
self.assertTrue(same(res1, res2))
# Really annoying intersection of specialization and RandomValueSource
# If we get a RandomValueSource with a single element tensor, we should return a ConstantVariable like other
# unspects... but if we do, we break the bytecode assumptions and guards will not work as we will be referring
# to a name from a source that is not there. If we call .item() and take the wrapped_value out, where we do
# wrapped_value = wrapped_value.item() where we send unspec down to wrap_fx_proxy, this test passes and then
# some models fail on missing codegen.tx.output.random_values_var. If we let the tensor value go into wrap as
# it is, this test fails.
# The real solution here is to rewrite RandomValueSource and all the codegen it does from the ground up.
def test_multiple_consecutive_random_calls_before_graph(self):
def fn(x):
dim1 = random.randrange(start=0, stop=5)
dim2 = random.randrange(start=0, stop=5)
dim3 = random.randrange(start=0, stop=5)
y = torch.rand(dim1, dim2, dim3)
return x + 2, y
x = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
random.seed(1)
res1 = fn(x)
cnts = torch._dynamo.testing.CompileCounter()
opt_fn = torch._dynamo.optimize(cnts)(fn)
random.seed(1)
res2 = opt_fn(x)
self.assertTrue(same(res1, res2))
def test_compiled_random_calls_are_random(self):
# For compiled functions with random calls,
# it should return different values for every iteration.
# https://github.com/pytorch/pytorch/issues/95425
@torch.compile(backend="eager", fullgraph=True)
def fn(x):
return (x + 1) * random.uniform(0, 1)
res = []
for _ in range(5):
res.append(fn(torch.ones(2)))
for i in range(1, 5):
self.assertFalse(same(res[i - 1], res[i]))
def test_random_call_with_while_loop(self):
def fn(x):
dim1 = random.randrange(start=0, stop=3)
dim2 = dim1
while dim1 == dim2:
dim2 = random.randrange(start=0, stop=3)
return x * 2
x = torch.randn(4)
random.seed(1)
res1 = fn(x)
opt_fn = torch._dynamo.optimize("eager")(fn)
random.seed(1)
res2 = opt_fn(x)
self.assertTrue(same(res1, res2))
random.seed(10)
res1 = fn(x)
random.seed(10)
res2 = opt_fn(x)
self.assertTrue(same(res1, res2))
def test_builtin_getitem(self):
# builtin getitem args[0] is python list and args[1] is unspec
def fn(x, idx):
return (torch.zeros(idx), x[idx], x[idx:])
x = list(range(50))
ref = fn(x, 48) # 48 is unspecialized
cnts = torch._dynamo.testing.CompileCounter()
opt_fn = torch._dynamo.optimize(cnts)(fn)
res = opt_fn(x, 48)
self.assertTrue(same(ref, res))
def test_use_and_specialize(self):
cnt = CompileCounter()
@torch.compile(backend=cnt, fullgraph=True, dynamic=True)
def fn(x, y):
x = x + y
if y == 2:
return x - 1
else:
return x + 1
self.assertTrue(same(fn(torch.tensor([5]), 2), 6))
self.assertTrue(same(fn(torch.tensor([6]), 2), 7))
self.assertTrue(same(fn(torch.tensor([5]), 3), 9))
self.assertTrue(same(fn(torch.tensor([4]), 3), 8))
self.assertEqual(cnt.frame_count, 2)
def test_no_recompiles(self):
cnt = CompileCounter()
@torch.compile(backend=cnt, fullgraph=True, dynamic=True)
def fn(x, y):
return x + y
self.assertTrue(same(fn(torch.tensor([5]), 100), 105))
self.assertTrue(same(fn(torch.tensor([4]), 200), 204))
self.assertTrue(same(fn(torch.tensor([3]), 300), 303))
self.assertTrue(same(fn(torch.tensor([2]), 400), 402))
self.assertEqual(cnt.frame_count, 1)
self.assertEqual(cnt.op_count, 1)
def test_no_recompiles_prod_backward(self):
# https://github.com/pytorch/pytorch/issues/120608
cnt = CompileCounter()
@torch.compile(backend=cnt, fullgraph=True, dynamic=True)
def fn(t):
return torch.prod(t, 3, keepdim=True)
input_shapes = [(8, 10, 3, 2), (8, 3, 5, 2), (8, 4, 8, 2)]
for s in input_shapes:
t1 = torch.randn(s, requires_grad=True)
h_result = fn(t1)
grad = torch.ones_like(h_result)
h_result.backward(grad)
self.assertEqual(cnt.frame_count, 1)
self.assertEqual(cnt.op_count, 1)
@unittest.skipIf(not torch.cuda.is_available(), "requires cuda")
def test_builtin_functions_on_cuda(self):
def fn(x, scaler):
m = torch.nn.ReLU()
y = m(x) * scaler
return y
x = torch.randn([3, 6], device="cuda")
scaler = 0.23 # 0.23 is unspecialized
ref = fn(x, scaler)
cnts = torch._dynamo.testing.CompileCounter()
opt_fn = torch._dynamo.optimize(cnts)(fn)
res = opt_fn(x, scaler)
self.assertTrue(same(ref, res))
self.assertEqual(ref.device, res.device)
def test_unspec_float_precision(self):
def fn(image, scale_factor):
image = torch.nn.functional.interpolate(
image[None],
size=None,
scale_factor=scale_factor,
mode="bilinear",
recompute_scale_factor=True,
align_corners=False,
)[0]
return image.shape
x = torch.rand([3, 427, 640])
scale_factor = 1.873536229133606
ref = fn(x, scale_factor)
cnts = torch._dynamo.testing.CompileCounter()
opt_fn = torch._dynamo.optimize(cnts)(fn)
res = opt_fn(x, scale_factor)
self.assertTrue(same(ref, res))
@unittest.expectedFailure # fails as long as numpy scalars are 0D arrays
def test_specializing_numpy_float_in_control_flow(self):
# np.float64 is unspecialized by default,
# but it should be specialized when used in control flow.
def fn(x, y):
if y > 1.0:
return x + 1
else:
return x - 1
x = torch.rand(4)
opt_fn = torch._dynamo.optimize("eager", nopython=True)(fn)
for t in [np.float16, np.float32, np.float64]:
y = t(1.23)
ref = fn(x, y)
res = opt_fn(x, y)
self.assertTrue(same(ref, res))
def test_mark_static_inside(self):
def fn(x):
torch._dynamo.mark_static(x, 0)
comptime.assert_static(x.size(0))
return x + 1
opt_fn = torch.compile(fn, dynamic=True, fullgraph=True)
opt_fn(torch.randn(12, 23))
def test_shape_graph_break(self):
from torch._dynamo.comptime import comptime
def fn(x):
x_shape = x.size()
comptime.graph_break()
return x + torch.randn(x_shape)
x = torch.randn(20)
opt_fn = torch._dynamo.optimize("eager")(fn)
opt_fn(x)
def test_isinstance_symint(self):
def fn(x):
assert isinstance(x.size(0), int)
return x * 2
x = torch.randn(20)
opt_fn = torch._dynamo.optimize("eager")(fn)
opt_fn(x)
y = torch.randn(30)
torch._dynamo.mark_dynamic(y, 0)
opt_fn(y)
def test_mark_01_dynamic(self):
def fn(x):
return x * 2
x = torch.randn(1)
torch._dynamo.mark_dynamic(x, 0)
opt_fn = torch._dynamo.optimize("eager")(fn)
# This will fail to compile a generic kernel, but we should not
# complain about it (mark dynamic will try its best but 0/1
# specialization is allowed)
opt_fn(x)
def test_conv1d_symint_padding(self):
kernel = torch.randn(1, 1, 4)
def func(x):
padding = math.ceil((kernel.shape[-1] + x.shape[-1] % 2) / 2) - 1
out = F.conv1d(x, kernel, padding=padding, stride=2)
return out
opt_func = torch.compile(func)
x = torch.randn(1, 1, 175)
opt_func(x) # passes
x = torch.randn(1, 1, 249)
opt_func(x) # crashes
@torch._dynamo.config.patch("assume_static_by_default", True)
def test_propagate_dynamic_dim(self):
x = torch.randn(20)
torch._dynamo.mark_dynamic(x, 0)
@torch.compile()
def fn(x):
y = x * 2
comptime.graph_break()
z = y * 2
return z
z = fn(x)
self.assertEqual(z._dynamo_weak_dynamic_indices, {0})
def test_rshift_dynamic(self):
def shift_right(tensor: torch.Tensor) -> torch.Tensor:
return (tensor >> 2).to(torch.long)
opt_fn = torch.compile(shift_right, fullgraph=True, dynamic=True)
sample_input = torch.tensor([4, 4, 16, 32], dtype=torch.uint8)
opt_fn(sample_input)
@torch._dynamo.config.patch(capture_scalar_outputs=True)
def test_symfloat_to_tensor(self):
def f1(v):
return torch.tensor([v.item()])
def f2(v):
return torch.tensor([[v.item()], [2.0]])
def f3(v):
return torch.tensor(v.item())
def f4(v):
return torch.tensor((v.item(),))
optimize = torch.compile(backend="aot_eager", fullgraph=True)
r = torch.randn(1)
self.assertEqual(f1(r), optimize(f1)(r))
self.assertEqual(f2(r), optimize(f2)(r))
self.assertEqual(f3(r), optimize(f3)(r))
self.assertEqual(f4(r), optimize(f4)(r))
def test_to_tensor(self):
def f1():
a = np.random.uniform(low=-1, high=1, size=(20, 1))
return torch.tensor([a, a, a, a], dtype=torch.float64, device="cpu")
def f2():
a = torch.tensor([[[123]]])
return torch.tensor([a, a])
def f3():
a = torch.tensor(123)
return torch.tensor([a, a])
def f4():
a = torch.tensor(123)
b = torch.tensor([[[456]]])
return torch.tensor([a, b])
def f5():
a = np.array([1, 2])
return torch.tensor([a, a])
optimize = torch.compile(backend="aot_eager", fullgraph=True)
self.assertEqual(f1().shape, optimize(f1)().shape)
self.assertEqual(f2(), optimize(f2)())
self.assertEqual(f3(), optimize(f3)())
self.assertEqual(f4(), optimize(f4)())
self.assertEqual(f5(), optimize(f5)())
def test_sym_int_conversion(self):
def f(x):
y = x.size(0)
return x * int(y == 0)
opt_fn = torch.compile(f, backend="eager", fullgraph=True)
x = torch.randn(2, 3)
opt_fn(x)
def test_sum_dimlist_spec(self):
def fn(inputs, dim):
return torch.sum(inputs, dim)
inputs = torch.randn(128, 5, 24, 24)
dim = (-1, 1, 0, 2)
compl_fn = torch.compile(fn, dynamic=True, backend="eager", fullgraph=True)
self.assertEqual(compl_fn(inputs, dim), fn(inputs, dim))
@torch._dynamo.config.patch(capture_scalar_outputs=True)
def test_item_max(self):
def fn(x):
return torch.ones(max(x.item(), 1024))
x = torch.tensor([1000])
y = torch.tensor([2000])
compl_fn = torch.compile(fn, backend="eager", fullgraph=True)
self.assertEqual(fn(x), compl_fn(x))
self.assertEqual(fn(y), compl_fn(y))
# https://github.com/pytorch/pytorch/issues/104812
def test_argmin_coerces_symint_to_intlist_spec(self):
def fn(x, dim):
# the python arg parser coerces dim into a vector<int>
return torch.amin(x, dim=dim, keepdim=True)
x = torch.randn(4, 4, 4)
dim = 2
compl_fn = torch.compile(fn, dynamic=True, backend="eager", fullgraph=True)
self.assertEqual(compl_fn(x, dim), fn(x, dim))
def test_exponential(self):
def fn(inputs, op_inputs_dict):
res = inputs.exponential_(**op_inputs_dict)
return res
inputs = torch.randn(2, 3, 4)
op_inputs_dict = {"lambd": 10, "generator": None}
compl_fn = torch.compile(fn, dynamic=True, backend="eager", fullgraph=True)
self.assertEqual(compl_fn(inputs, op_inputs_dict), fn(inputs, op_inputs_dict))
def test_symbol_guard_limit_before_specialize(self):
cnts = torch._dynamo.testing.CompileCounter()
@torch._dynamo.optimize(cnts, dynamic=True)
def fn(x):
torch._check(x.size(0) != 3)
torch._check(x.size(0) != 4)
torch._check(x.size(0) != 5)
torch._check(x.size(0) != 6)
return x + 2
# Control test
fn(torch.randn(12))
fn(torch.randn(13))
fn(torch.randn(14))
self.assertExpectedInline(cnts.frame_count, """1""")
cnts.frame_count = 0
torch._dynamo.reset()
with torch.fx.experimental._config.patch(
symbol_guard_limit_before_specialize=3
):
fn(torch.randn(12))
fn(torch.randn(13))
fn(torch.randn(14))
self.assertExpectedInline(cnts.frame_count, """3""")
def test_defaults(self):
def g(x, i=8):
comptime.assert_static(i)
return x * i
def fn(x):
return g(x)
inputs = torch.randn(2, 3, 4)
compl_fn = torch.compile(fn, dynamic=True, backend="eager")
self.assertEqual(compl_fn(inputs), fn(inputs))
@torch._dynamo.config.patch(specialize_float=False, assume_static_by_default=True)
def test_unspec_float_input(self):
cnts = torch._dynamo.testing.CompileCounter()
def f(x, y):
if y == 5.0:
return x + 2
else:
return x + y
cf = torch.compile(backend=cnts, fullgraph=True)(f)
x = torch.randn(3)
self.assertEqual(f(x, 3.0), cf(x, 3.0))
self.assertEqual(f(x, 4.0), cf(x, 4.0))
self.assertExpectedInline(cnts.frame_count, """1""") # no recompile
self.assertEqual(f(x, 5.0), cf(x, 5.0))
self.assertExpectedInline(cnts.frame_count, """2""") # guard worked
self.assertEqual(f(x, math.nan), cf(x, math.nan))
self.assertExpectedInline(cnts.frame_count, """3""") # nan always recompiles
@torch._dynamo.config.patch(specialize_float=False, assume_static_by_default=True)
def test_unspec_float_output(self):
cnts = torch._dynamo.testing.CompileCounter()
def f(x, y):
return x + 1, y * 2
cf = torch.compile(backend=cnts, fullgraph=True)(f)
x = torch.randn(3)
self.assertEqual(f(x, 3.0), cf(x, 3.0))
self.assertEqual(f(x, 4.0), cf(x, 4.0))
self.assertEqual(f(x, 5.0), cf(x, 5.0))
@torch._dynamo.config.patch(capture_scalar_outputs=True)
def test_data_dependent_evaluate_expr_graph_break(self):
cnts = torch._dynamo.testing.CompileCounter()
# To ensure that the continuation frame is compiled,
# have to write the test function in this funny way.
# See https://github.com/pytorch/pytorch/issues/111918
def test(y):
if y > 2:
return True
else:
return False
@torch._dynamo.optimize(cnts)
def fn(x):
x = x + 1
y = x.item()
if test(y):
return x * 2
else:
return x * 3
x = torch.tensor([3.0])
fn(x)
self.assertExpectedInline(cnts.frame_count, """2""")
self.assertExpectedInline(cnts.op_count, """4""")
def test_prune_torch_check(self):
log_stream, ctx = logs_to_string("torch._dynamo.output_graph", "graph_code")
@torch.compile(fullgraph=True, dynamic=True, backend="eager")
def f(x, y):
torch._check(y + 5 == 85)
torch._check(x.size(0) == 80)
with ctx():
f(torch.randn(80, 100), 80)
out = "\n".join(log_stream.getvalue().strip().split("\n")[3:]).strip()
self.assertExpectedInline(
out,
"""\
def forward(self):
return ()""",
)
@torch._dynamo.config.patch(capture_scalar_outputs=True)
def test_split_aot_autograd(self):
@torch.compile(backend="aot_eager", fullgraph=True)
def f(x, i):
y, z = i.tolist()
return torch.split(x, [y, z])
print(f(torch.randn(10, requires_grad=True), torch.tensor([7, 3])))
def test_bool_tensor_ctor(self):
cnts = torch._dynamo.testing.CompileCounter()
@torch.compile(backend=cnts, dynamic=True, fullgraph=True)
def f(x):
y = torch.empty((x.size(0) // 13) * 13)
return torch.tensor(y.numel() == 0)
self.assertTrue(f(torch.empty(8)).item())
self.assertFalse(f(torch.empty(13)).item())
if __name__ == "__main__":
from torch._dynamo.test_case import run_tests
run_tests()