-
Notifications
You must be signed in to change notification settings - Fork 25.7k
[dynamo] Properly prune dead cell local variables #136891
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/136891
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 42de084 with merge base 9a957e2 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
|
|
/easycla |
torch/_dynamo/symbolic_convert.py
Outdated
| # 2. In the fast path of `OutputGraph::compile_subgraph`, we populate | ||
| # the tuple of cells _after_ emitting the `MAKE FUNCTION` bytecode, | ||
| # via `STORE DEREF`; these `STORE DEREF` are generated partly based | ||
| # on the current `symbolic_locals`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about another approach where we emit STORE_DEREF as part of NestedUserFunctionVariable::reconstruct, and get rid of the special handling of cellvars here; I might look into that more if I can't fix the existing test failures.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm starting to think there might be a better solution with this approach, by replacing uses of ClosureVariable with NewCellVariable. It feels related to #136814 as well.
37a7f44 to
d75022e
Compare
|
Fix side-effect related issues -- Dynamo must treat captured variables as live (therefore generate bytecode to update these cells), if any of their user functions escapes the lifetime of the function that defined the captured variables. |
d75022e to
08b1c20
Compare
|
Run |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a good change, but tests are failing
08b1c20 to
b7e4ecb
Compare
| # Manually release the self-referential nested function, which | ||
| # captures `self.symbolic_locals` and affects parts of PT test/logic | ||
| # that are sensitive to when certain objects get released. | ||
| del visit |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Attempt to fix failing tests; this is somewhat surprising, or maybe expected?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is surprising to me - which tests are failing because of this? The memleak tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-
These ones, I assume
self.symbolic_localshad a reference tox, so before we run the fullgc.collect(),xwon't be freed (tests do pass if we add agc.collect()afterdel x).
https://github.com/pytorch/pytorch/blob/54e2b2a6d4f6b6d27637b941f35f254ef42edbad/test/dynamo/test_misc.py#L4753-L4797 -
Somewhat similar to the above, but more behind-the-scene logic
https://github.com/pytorch/pytorch/blob/54e2b2a6d4f6b6d27637b941f35f254ef42edbad/test/dynamo/test_misc.py#L10756-L10766 -
This one, which can't seem to be fixed by adding a
gc.collect()in the test itself.
https://github.com/pytorch/pytorch/blob/54e2b2a6d4f6b6d27637b941f35f254ef42edbad/test/test_autograd.py#L5464-L5465
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What exactly is the reference cycle being caused here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quite subtle:
Inside this frame, visit is a cell variable, because visit as a nested function references and captures itself. So you have visit.__closure__[0] being a cell that references visit. Example:
def foo():
def bar():
return bar()
print(bar.__closure__[0].cell_contents is bar)
foo() # prints TrueThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is annoying but I don't have any ideas.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Were you able to try any of the other methods that we discussed on Monday to prevent the refcycle without using del?
b7e4ecb to
54e2b2a
Compare
| # NOTE: Don't trace from the cell locals which aren't explicitly | ||
| # read anymore; if they are indirectly used, they will be reached by | ||
| # other roots. These initially excluded cells are the ones that will | ||
| # hopefully be pruned. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another fix.
54e2b2a to
43a1d59
Compare
|
Rebase to fix a inductor failure which seems unrelated to my change. |
|
@StrongerXi if the test isn't related -- try to rebase? Otherwise you'll need to force merge this when it's been approved |
|
|
||
| # Returning `func` forces dynamo to output `x` in the compiled graph, so | ||
| # that we can store it as `func`'s closure. | ||
| return func, func() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the outputs are (x, x + x) ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm (func, x + x) I'd say?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I meant, what are the 2 outputs in the compiled graph?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yep. Lemme document that.
|
@StrongerXi has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
f290a99 to
7020b9c
Compare
Another 1 line fix in |
|
@StrongerXi has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
|
All green, but I'm still waiting for some internal experiments to guard against potential regression. |
|
Need to investigate compile-time increase and qps drop for internal workflow. |
7020b9c to
c48469e
Compare
|
Still investigating. Update documentation about why we skip |
c48469e to
2dc6537
Compare
|
Internal workflow passed (previous results turned out to be noise). Remove inaccurate documentation on why we skip |
|
@StrongerXi has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
This patch updates the `prune_dead_locals` logic to do slightly more aggressive pruning for cell local variables, in absence of side-effects, e.g., a cell variable can be pruned when its user function(s) will never be used again. See added tests for examples; note that a few tests in `test/dynamo/test_higher_order_ops.py` also got updated because we are no longer returning the unnecessary graph output. Fixes pytorch#127350, pytorch#124653
2dc6537 to
42de084
Compare
Another 1 line fix of this sort, in a test that requires fbgemm. |
|
@StrongerXi has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
|
All green externally and internally. Merging. |
1 similar comment
|
All green externally and internally. Merging. |
|
@pytorchbot merge |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
This patch updates the
prune_dead_localslogic to do slightly more aggressive pruning for cell local variables, in absence of side-effects, e.g., a cell variable can be pruned when its user function(s) will never be used again.See added tests for examples; note that a few tests in
test/dynamo/test_higher_order_ops.pyalso got updated because we are no longer returning the unnecessary graph output.Fixes #127350, #124653
cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @yf225 @chenyang78 @kadeng @muchulee8 @ColinPeppler @amjames @desertfire @chauhang @rec