[Dynamo] Guard serialization for FUNCTION_MATCH (#152727)

Unsupported because it uses unsupported ID_MATCH.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/152727
Approved by: https://github.com/jansel
ghstack dependencies: #152725
This commit is contained in:
Joel Schlosser 2025-05-07 10:57:49 -04:00 committed by PyTorch MergeBot
parent a6f51be2fd
commit a9186ec723
2 changed files with 19 additions and 0 deletions

View File

@ -621,6 +621,22 @@ class TestGuardSerialization(torch._inductor.test_case.TestCase):
):
self._test_serialization("NN_MODULE", fn, m, x)
def test_function_match(self):
def fn(x):
# usage of this context manager installs a FUNCTION_MATCH guard
with torch.no_grad():
y = x * 2
return y
x = torch.randn(3)
# we don't support FUNCTION_MATCH because it adds an ID_MATCH guard, and we don't
# support that in serialization
with self.assertRaisesRegex(
RuntimeError, "FUNCTION_MATCH guard cannot be serialized."
):
self._test_serialization("FUNCTION_MATCH", fn, x)
def test_dict_version(self):
def fn(x):
return pytree.tree_leaves(x)[0] + 1

View File

@ -1761,6 +1761,9 @@ class GuardBuilder(GuardBuilderBase):
def FUNCTION_MATCH(self, guard: Guard):
"""things like torch.add and user defined functions"""
# don't support this in serialization because it uses unsupported ID_MATCH
if self.serialization_mode == "save":
raise RuntimeError("FUNCTION_MATCH guard cannot be serialized.")
return self.ID_MATCH(guard)
def CLOSURE_MATCH(self, guard: Guard):