Skip to content

Commit a9186ec

Browse files
jbschlosserpytorchmergebot
authored andcommitted
[Dynamo] Guard serialization for FUNCTION_MATCH (pytorch#152727)
Unsupported because it uses unsupported ID_MATCH. Pull Request resolved: pytorch#152727 Approved by: https://github.com/jansel ghstack dependencies: pytorch#152725
1 parent a6f51be commit a9186ec

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

test/dynamo/test_guard_serialization.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,22 @@ def fn(m, x):
621621
):
622622
self._test_serialization("NN_MODULE", fn, m, x)
623623

624+
def test_function_match(self):
625+
def fn(x):
626+
# usage of this context manager installs a FUNCTION_MATCH guard
627+
with torch.no_grad():
628+
y = x * 2
629+
return y
630+
631+
x = torch.randn(3)
632+
633+
# we don't support FUNCTION_MATCH because it adds an ID_MATCH guard, and we don't
634+
# support that in serialization
635+
with self.assertRaisesRegex(
636+
RuntimeError, "FUNCTION_MATCH guard cannot be serialized."
637+
):
638+
self._test_serialization("FUNCTION_MATCH", fn, x)
639+
624640
def test_dict_version(self):
625641
def fn(x):
626642
return pytree.tree_leaves(x)[0] + 1

torch/_dynamo/guards.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,6 +1761,9 @@ def NN_MODULE(self, guard: Guard):
17611761

17621762
def FUNCTION_MATCH(self, guard: Guard):
17631763
"""things like torch.add and user defined functions"""
1764+
# don't support this in serialization because it uses unsupported ID_MATCH
1765+
if self.serialization_mode == "save":
1766+
raise RuntimeError("FUNCTION_MATCH guard cannot be serialized.")
17641767
return self.ID_MATCH(guard)
17651768

17661769
def CLOSURE_MATCH(self, guard: Guard):

0 commit comments

Comments
 (0)