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)
| 78 | |
| 79 | @contextmanager |
| 80 | def 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 | |
| 99 | class TestEmit(unittest.TestCase): |
no outgoing calls
no test coverage detected