[fx] hack __torch_dispatch__ for meta tensor and autograd.#1515
Merged
FrankLeeeee merged 19 commits intohpcaitech:mainfrom Aug 31, 2022
Merged
[fx] hack __torch_dispatch__ for meta tensor and autograd.#1515FrankLeeeee merged 19 commits intohpcaitech:mainfrom
FrankLeeeee merged 19 commits intohpcaitech:mainfrom
Conversation
super-dainiu
commented
Aug 29, 2022
super-dainiu
commented
Aug 29, 2022
Cypher30
reviewed
Aug 30, 2022
Contributor
Cypher30
left a comment
There was a problem hiding this comment.
Great work! As we have debated, you could add the MetaTensor to proxy and try to record the atens inside MetaTensor data structure~
Cypher30
approved these changes
Aug 30, 2022
Contributor
Cypher30
left a comment
There was a problem hiding this comment.
We could pass this PR first~
FrankLeeeee
suggested changes
Aug 30, 2022
Contributor
FrankLeeeee
left a comment
There was a problem hiding this comment.
You should fix the broken unit tests before merging this PR.
FrankLeeeee
reviewed
Aug 30, 2022
FrankLeeeee
reviewed
Aug 30, 2022
FrankLeeeee
reviewed
Aug 30, 2022
FrankLeeeee
reviewed
Aug 30, 2022
FrankLeeeee
approved these changes
Aug 31, 2022
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What's new?
When I tried to run autograd with meta tensor input on
vit_b_16, I discovered that some aten ops are not registered for meta backend. So following the suggestions in Function to automatically calculate Conv shape · Issue #79512 · pytorch/pytorch · GitHub, I tried to patch native_layer_norm.default for meta backend.However, even if patching is successful, the autograd dispatcher refuses to use my patched op for meta backend.
So as discussed in CompositeImplicitAutograd operators should not perform operations that do not dispatch · Issue #61669 · pytorch/pytorch · GitHub, failing due to CompositeImplicitAutograd is inevitable for PyTorch version 1.12.0 and below. I somehow managed to develop another version of autograd with meta tensor.
Since previous works of the PyTorch team have supported aten ops on meta backend, we can simply hack the autograd dispatcher, deceiving it that we are running on CPU. In this way, the dispatcher will not use CompositeImplicitAutograd anymore, and our patched ops can be used for meta backend. So now we can do forward and backward with meta tensor only, and trace a large model with batch_size=1e10 in milliseconds.
With this amazing
__torch_dispatch__, I replaced the previously patched version of tracing into REALLL meta tracing.Concerns
Indeed,
__torch_dispatch__is not compatible with PyTorch 1.11.0 and below.