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

Function patch_forward

exir/capture/_capture.py:139–155  ·  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

137
138@contextmanager
139def patch_forward(obj: torch.nn.Module, new_method):
140 """Helper method to make it easier to cleanly torch.export() a method on a
141 module that is not `forward`.
142
143 TODO(suo): upstream this to torch.export.wrapper.
144 """
145 # Save the original method
146 original_method = obj.forward
147
148 # Patch the method
149 obj.forward = new_method.__get__(obj, obj.__class__)
150
151 try:
152 yield
153 finally:
154 # Restore the original method
155 obj.forward = original_method
156
157
158class WrapperModule(torch.nn.Module):

Callers 4

test_multi_state_planMethod · 0.90
exportMethod · 0.90
captureFunction · 0.70

Calls

no outgoing calls

Tested by 2

test_multi_state_planMethod · 0.72