|
| 1 | +# Owner(s): ["module: dynamo"] |
| 2 | + |
| 3 | +import torch |
| 4 | +import torch._dynamo.test_case |
| 5 | +import torch._dynamo.testing |
| 6 | + |
| 7 | + |
| 8 | +def my_custom_function(x): |
| 9 | + return x + 1 |
| 10 | + |
| 11 | + |
| 12 | +class RunDiffGuardTests(torch._dynamo.test_case.TestCase): |
| 13 | + def test_bool_recompile(self): |
| 14 | + def fn(x, y, c): |
| 15 | + if c: |
| 16 | + return x * y |
| 17 | + else: |
| 18 | + return x + y |
| 19 | + |
| 20 | + opt_fn = torch.compile(fn, backend="inductor") |
| 21 | + x = 2 * torch.ones(4) |
| 22 | + y = 3 * torch.ones(4) |
| 23 | + |
| 24 | + ref1 = opt_fn(x, y, True) |
| 25 | + ref2 = opt_fn(x, y, False) |
| 26 | + |
| 27 | + with torch.compiler.set_stance(skip_guard_eval_unsafe=True): |
| 28 | + res2 = opt_fn(x, y, False) |
| 29 | + res1 = opt_fn(x, y, True) |
| 30 | + |
| 31 | + self.assertEqual(ref1, res1) |
| 32 | + self.assertEqual(ref2, res2) |
| 33 | + |
| 34 | + def test_tensor_recompile(self): |
| 35 | + def fn(x, y): |
| 36 | + return x * y |
| 37 | + |
| 38 | + opt_fn = torch.compile(fn, backend="eager") |
| 39 | + x = torch.randn(4, dtype=torch.float32) |
| 40 | + y = torch.randn(4, dtype=torch.float32) |
| 41 | + |
| 42 | + ref1 = opt_fn(x, y) |
| 43 | + |
| 44 | + x64 = torch.randn(4, dtype=torch.float64) |
| 45 | + y64 = torch.randn(4, dtype=torch.float64) |
| 46 | + ref2 = opt_fn(x64, y64) |
| 47 | + |
| 48 | + with torch.compiler.set_stance(skip_guard_eval_unsafe=True): |
| 49 | + res1 = opt_fn(x, y) |
| 50 | + res2 = opt_fn(x64, y64) |
| 51 | + |
| 52 | + self.assertEqual(ref1, res1) |
| 53 | + self.assertEqual(ref2, res2) |
| 54 | + |
| 55 | + def test_post_recompile(self): |
| 56 | + class Foo: |
| 57 | + a = 4 |
| 58 | + b = 5 |
| 59 | + |
| 60 | + foo = Foo() |
| 61 | + |
| 62 | + def fn(x): |
| 63 | + return x + foo.a + foo.b |
| 64 | + |
| 65 | + cnts = torch._dynamo.testing.CompileCounter() |
| 66 | + opt_fn = torch.compile(fn, backend=cnts) |
| 67 | + |
| 68 | + x = torch.randn(4) |
| 69 | + ref = fn(x) |
| 70 | + res = opt_fn(x) |
| 71 | + self.assertEqual(ref, res) |
| 72 | + self.assertEqual(cnts.frame_count, 1) |
| 73 | + |
| 74 | + foo.a = 11 |
| 75 | + ref = fn(x) |
| 76 | + res = opt_fn(x) |
| 77 | + self.assertEqual(ref, res) |
| 78 | + self.assertEqual(cnts.frame_count, 2) |
| 79 | + |
| 80 | + with torch.compiler.set_stance(skip_guard_eval_unsafe=True): |
| 81 | + # Set it back to original value |
| 82 | + foo.a = 4 |
| 83 | + ref = fn(x) |
| 84 | + res = opt_fn(x) |
| 85 | + self.assertEqual(ref, res) |
| 86 | + |
| 87 | + foo.a = 11 |
| 88 | + ref = fn(x) |
| 89 | + res = opt_fn(x) |
| 90 | + self.assertEqual(ref, res) |
| 91 | + |
| 92 | + # Check that we are back to original behavior |
| 93 | + foo.b = 8 |
| 94 | + ref = fn(x) |
| 95 | + res = opt_fn(x) |
| 96 | + self.assertEqual(ref, res) |
| 97 | + self.assertEqual(cnts.frame_count, 3) |
| 98 | + |
| 99 | + def test_fail_on_tensor_shape_change(self): |
| 100 | + def fn(dt): |
| 101 | + return dt["x"] + 1 |
| 102 | + |
| 103 | + x = torch.randn(4) |
| 104 | + dt = {} |
| 105 | + dt["x"] = x |
| 106 | + opt_fn = torch.compile(fn, backend="eager") |
| 107 | + opt_fn(dt) |
| 108 | + |
| 109 | + with self.assertRaisesRegex( |
| 110 | + RuntimeError, "Recompilation triggered with skip_guard_eval_unsafe stance" |
| 111 | + ): |
| 112 | + with torch.compiler.set_stance(skip_guard_eval_unsafe=True): |
| 113 | + x = torch.randn(4, 4) |
| 114 | + dt["x"] = x |
| 115 | + opt_fn(dt) |
| 116 | + |
| 117 | + def test_cache_line_pickup(self): |
| 118 | + def fn(x, a=None, b=None): |
| 119 | + x = x * 3 |
| 120 | + if a: |
| 121 | + x = x * 5 |
| 122 | + if b: |
| 123 | + x = x * 7 |
| 124 | + return x |
| 125 | + |
| 126 | + opt_fn = torch.compile(fn, backend="eager") |
| 127 | + x = torch.ones(4) |
| 128 | + |
| 129 | + ref1 = opt_fn(x, a=None, b=None) |
| 130 | + ref2 = opt_fn(x, a=1, b=None) |
| 131 | + ref3 = opt_fn(x, a=1, b=1) |
| 132 | + |
| 133 | + with torch.compiler.set_stance(skip_guard_eval_unsafe=True): |
| 134 | + res1 = opt_fn(x, a=None, b=None) |
| 135 | + res2 = opt_fn(x, a=1, b=None) |
| 136 | + res3 = opt_fn(x, a=1, b=1) |
| 137 | + |
| 138 | + self.assertEqual(ref1, res1) |
| 139 | + self.assertEqual(ref2, res2) |
| 140 | + self.assertEqual(ref3, res3) |
| 141 | + |
| 142 | + |
| 143 | +if __name__ == "__main__": |
| 144 | + from torch._dynamo.test_case import run_tests |
| 145 | + |
| 146 | + run_tests() |
0 commit comments