Skip to content

Commit

Permalink
use proxies to nn.Module in dynamo generated GraphModules (#120756)
Browse files Browse the repository at this point in the history
Summary:
Fixes remaining refleaks found when debugging pytorch/pytorch#119607, tests added in pytorch/pytorch#120657.

Also fixes some tests that xfail: pytorch/pytorch#120631 (not entirely sure why), but introduced tests now fail.

X-link: pytorch/pytorch#120756
Approved by: https://github.com/jansel

Reviewed By: huydhn

Differential Revision: D55225127

Pulled By: williamwen42

fbshipit-source-id: 0d402deb4246d981304985761b94f637970fabce
  • Loading branch information
williamwen42 authored and facebook-github-bot committed Mar 22, 2024
1 parent 2196021 commit 9d64293
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions userbenchmark/dynamo/dynamobench/_dynamo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2575,3 +2575,18 @@ class Invalid(dict): # type: ignore[type-arg]
pass

return RemovableHandle(Invalid())


# Returns a "proxy" (new object with the same class and dict) for (non-GraphModule) nn.Module's.
# Attribute changes to the original object/proxy will be reflected in the other.
# This is useful for cases where we want a keep-alive reference to a module without increasing
# its reference count.
def nn_module_proxy(mod):
if not isinstance(mod, torch.nn.Module):
return mod
if isinstance(mod, torch.fx.GraphModule):
# Dynamo-generated GM's shouldn't contain user-created GM's
return mod
proxy = mod.__class__.__new__(mod.__class__)
proxy.__dict__ = mod.__dict__
return proxy

0 comments on commit 9d64293

Please sign in to comment.