-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Add functionalization pass in TorchDynamo #99461
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
Add functionalization pass in TorchDynamo #99461
Conversation
[ghstack-poisoned]
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/99461
Note: Links to docs will display an error until the docs builds have been completed. ❌ 5 New FailuresAs of commit ae41c99: NEW FAILURES - The following jobs have failed:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
torch/_dynamo/config.py
Outdated
guard_nn_modules = False | ||
|
||
# Optionally functionalize the produced export graph module | ||
functionalize = False |
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.
Lets not add more things to config. Can you use **kwargs in export
API for now to do this?
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.
Sure
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.
side note - @anijain2305 what's our stance towards BC on torch._dynamo.export? Are we ok with removing kwargs later?
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.
All the options seem equally bad to me. Imo, export is going through a heavy refactoring, and I would wait for aot_export etc to be finalized before we set the API in stone.
This looks ok to me for short-term. @bdhirsh Can you take a look? |
torch/_dynamo/eval_frame.py
Outdated
torch._enable_functionalization(reapply_views=True) | ||
yield pytree.tree_map(to_fun, args) | ||
finally: | ||
torch._disable_functionalization() |
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 think this will still cause problems, if your Interpreter(graph).run()
call lower down raises an exception. You need that to be wrapped by the try/finally to make sure that functionalization is always properly turned off afterwards.
One way would be to change this context manager into a decorator that wraps your interpreter call. Or you can just not bother with the decorator here, and manually try/finally around the interpreter call.
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.
Good catch! I will just do plain try/finally.
Otherwise look ok to me! |
Fixes: #99000 cc soumith voznesenskym penguinwu anijain2305 EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng Xia-Weiwen wenzhe-nrv jiayisunx desertfire [ghstack-poisoned]
Fixes: #99000 cc soumith voznesenskym penguinwu anijain2305 EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng Xia-Weiwen wenzhe-nrv jiayisunx desertfire [ghstack-poisoned]
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.
lgtm!
@tugsbayasgalan has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
Fixes: #99000 cc soumith voznesenskym penguinwu anijain2305 EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng Xia-Weiwen wenzhe-nrv jiayisunx desertfire Differential Revision: [D45106409](https://our.internmc.facebook.com/intern/diff/D45106409) [ghstack-poisoned]
@tugsbayasgalan has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
Fixes: #99000 cc soumith voznesenskym penguinwu anijain2305 EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng Xia-Weiwen wenzhe-nrv jiayisunx desertfire Differential Revision: [D45106409](https://our.internmc.facebook.com/intern/diff/D45106409) [ghstack-poisoned]
@tugsbayasgalan has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
Fixes: #99000 cc soumith voznesenskym penguinwu anijain2305 EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng Xia-Weiwen wenzhe-nrv jiayisunx desertfire Differential Revision: [D45106409](https://our.internmc.facebook.com/intern/diff/D45106409) [ghstack-poisoned]
Fixes: #99000 cc soumith voznesenskym penguinwu anijain2305 EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng Xia-Weiwen wenzhe-nrv jiayisunx desertfire Differential Revision: [D45106409](https://our.internmc.facebook.com/intern/diff/D45106409) [ghstack-poisoned]
Pull Request resolved: #99461 Fixes: #99000 cc @soumith @voznesenskym @penguinwu @anijain2305 @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @Xia-Weiwen @wenzhe-nrv @jiayisunx @desertfire Differential Revision: [D45106409](https://our.internmc.facebook.com/intern/diff/D45106409/) ghstack-source-id: 187846232
if functionalize and not aten_graph: | ||
raise UserError( | ||
UserErrorType.ANTI_PATTERN, | ||
"TorchDynamo won't functionalize non-aten graphs. Please set `functionalize` to true", |
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.
"TorchDynamo won't functionalize non-aten graphs. Please set `functionalize` to true", | |
"TorchDynamo won't functionalize non-aten graphs. Please set `aten_graph` to true", |
torch/_dynamo/eval_frame.py
Outdated
with torch.fx.traceback.preserve_node_meta(): | ||
return torch.fx.Interpreter(graph).run(*args) | ||
if functionalize: | ||
torch._enable_functionalization(reapply_views=False) |
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 think we concluded to allow views in the graph, should we have reapply_views=True?
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 lol you are right. We will replace them with view_copy later as pass
|
||
new_graph.recompile() | ||
|
||
# TODO remove this once Executorch uses proper functionalization |
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.
Love this deletion!
Fixes: #99000 cc soumith voznesenskym penguinwu anijain2305 EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng Xia-Weiwen wenzhe-nrv jiayisunx desertfire Differential Revision: [D45106409](https://our.internmc.facebook.com/intern/diff/D45106409) [ghstack-poisoned]
Pull Request resolved: #99461 Fixes: #99000 cc @soumith @voznesenskym @penguinwu @anijain2305 @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @Xia-Weiwen @wenzhe-nrv @jiayisunx @desertfire Differential Revision: [D45106409](https://our.internmc.facebook.com/intern/diff/D45106409/) ghstack-source-id: 187851433
@pytorchbot rebase |
@pytorchbot successfully started a rebase job. Check the current status here |
Fixes: #99000 cc soumith voznesenskym penguinwu anijain2305 EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng Xia-Weiwen wenzhe-nrv jiayisunx desertfire Differential Revision: [D45106409](https://our.internmc.facebook.com/intern/diff/D45106409) [ghstack-poisoned]
Successfully rebased |
Fixes: #99000 cc soumith voznesenskym penguinwu anijain2305 EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng Xia-Weiwen wenzhe-nrv jiayisunx desertfire Differential Revision: [D45106409](https://our.internmc.facebook.com/intern/diff/D45106409) [ghstack-poisoned]
Pull Request resolved: #99461 Fixes: #99000 cc @soumith @voznesenskym @penguinwu @anijain2305 @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @Xia-Weiwen @wenzhe-nrv @jiayisunx @desertfire Differential Revision: [D45106409](https://our.internmc.facebook.com/intern/diff/D45106409/) **NOTE FOR REVIEWERS**: This PR has internal Meta-specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D45106409/)! ghstack-source-id: 188007117
Fixes: #99000 cc soumith voznesenskym penguinwu anijain2305 EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng Xia-Weiwen wenzhe-nrv jiayisunx desertfire Differential Revision: [D45106409](https://our.internmc.facebook.com/intern/diff/D45106409) [ghstack-poisoned]
Fixes: #99000 cc soumith voznesenskym penguinwu anijain2305 EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng Xia-Weiwen wenzhe-nrv jiayisunx desertfire Differential Revision: [D45106409](https://our.internmc.facebook.com/intern/diff/D45106409) [ghstack-poisoned]
Pull Request resolved: #99461 Fixes: #99000 cc @soumith @voznesenskym @penguinwu @anijain2305 @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @Xia-Weiwen @wenzhe-nrv @jiayisunx @desertfire Differential Revision: [D45106409](https://our.internmc.facebook.com/intern/diff/D45106409/) **NOTE FOR REVIEWERS**: This PR has internal Meta-specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D45106409/)! ghstack-source-id: 188187442
@pytorchbot merge (Initiating merge automatically since Phabricator Diff has merged) |
@pytorchbot merge (Initiating merge automatically since Phabricator Diff has merged) |
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 |
Stack from ghstack (oldest at bottom):
Fixes: #99000
cc @soumith @voznesenskym @penguinwu @anijain2305 @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @Xia-Weiwen @wenzhe-nrv @jiayisunx @desertfire
Differential Revision: D45106409