Add functionalization pass in TorchDynamo#99461
Add functionalization pass in TorchDynamo#99461tugsbayasgalan wants to merge 19 commits intogh/tugsbayasgalan/110/basefrom
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.
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.
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.
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.
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.
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]
|
@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.
| "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.
I think we concluded to allow views in the graph, should we have reapply_views=True?
There was a problem hiding this comment.
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 |
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