MCPcopy Create free account
hub / github.com/pytorch/executorch / patch_forward

Function patch_forward

exir/emit/test/test_emit.py:80–96  ·  view source on GitHub ↗

Helper method to make it easier to cleanly torch.export() a method on a module that is not `forward`. TODO(suo): upstream this to torch.export.wrapper.

(obj: torch.nn.Module, new_method)

Source from the content-addressed store, hash-verified

78
79@contextmanager
80def patch_forward(obj: torch.nn.Module, new_method):
81 """Helper method to make it easier to cleanly torch.export() a method on a
82 module that is not `forward`.
83
84 TODO(suo): upstream this to torch.export.wrapper.
85 """
86 # Save the original method
87 original_method = obj.forward
88
89 # Patch the method
90 obj.forward = new_method.__get__(obj, obj.__class__)
91
92 try:
93 yield
94 finally:
95 # Restore the original method
96 obj.forward = original_method
97
98
99class TestEmit(unittest.TestCase):

Calls

no outgoing calls

Tested by

no test coverage detected