Created
July 11, 2023 21:38
-
-
Save anijain2305/0472b8a9a255541e6e869b81fd2eb4d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
class MockModule(torch.nn.Module): | |
def __init__(self): | |
super().__init__() | |
self.a = torch.tensor(0) | |
self.b = torch.tensor(0) | |
self.register_buffer("iterations", torch.tensor(0)) | |
def forward(self, x): | |
# Mutation handled in bytecode | |
self.a += 1 | |
# Mutation handled in bytecode | |
setattr(self, "b", self.a * 2) | |
# Mutation handled in the graph module itself | |
self.iterations += 1 | |
return x * self.a * self.b | |
mod = MockModule() | |
x = torch.randn(4) | |
opt_mod = torch.compile(mod, backend="eager", fullgraph=True) | |
print(opt_mod(torch.ones(4))) | |
print(opt_mod(torch.ones(4))) |
Author
anijain2305
commented
Jul 11, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment