-
Notifications
You must be signed in to change notification settings - Fork 25.6k
[export] Fix wrap_with_set_grad_enabled retracing #163295
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/163295
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit ec1548e with merge base bb7c9a2 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
@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 |
@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 |
Fixes pytorch#163294 The code `with torch.set_grad_enabled(enable_grad)` calls `torch._C._set_grad_enabled` three times -- (1) when [initializing set_grad_enabled](https://github.com/pytorch/pytorch/blob/bb7c9a2d4127ff178fe8787caf070d7072d015b6/torch/autograd/grad_mode.py#L187C9-L187C35), (2) when [entering the context](https://github.com/pytorch/pytorch/blob/bb7c9a2d4127ff178fe8787caf070d7072d015b6/torch/autograd/grad_mode.py#L194), and (3) when [exiting the context](https://github.com/pytorch/pytorch/blob/bb7c9a2d4127ff178fe8787caf070d7072d015b6/torch/autograd/grad_mode.py#L197). This results in the the retraced export module to have a duplicate `torch._C._set_grad_enabled` like: ``` def forward(self, arg0_1): add = torch.ops.aten.add.Tensor(arg0_1, 1); arg0_1 = None _set_grad_enabled = torch._C._set_grad_enabled(False); _set_grad_enabled = None _set_grad_enabled = torch._C._set_grad_enabled(False); _set_grad_enabled = None add_1 = torch.ops.aten.add.Tensor(add, 2); add = None _set_grad_enabled_1 = torch._C._set_grad_enabled(True); _set_grad_enabled_1 = None add_2 = torch.ops.aten.add.Tensor(add_1, 3); add_1 = None return (add_2,) ``` When export runs the `replace_set_grad_with_hop_pass`, it will look through the graph for `torch._C._set_grad_enabled` and create subgraphs. The duplicate `torch._C._set_grad_enabled` results in an empty submod in the graph, which resulted in an error in [this post](https://fb.workplace.com/groups/1028545332188949/posts/1844720036398281/?comment_id=1862175381319413). Pull Request resolved: pytorch#163295 Approved by: https://github.com/yushangdi
Fixes pytorch#163294 The code `with torch.set_grad_enabled(enable_grad)` calls `torch._C._set_grad_enabled` three times -- (1) when [initializing set_grad_enabled](https://github.com/pytorch/pytorch/blob/bb7c9a2d4127ff178fe8787caf070d7072d015b6/torch/autograd/grad_mode.py#L187C9-L187C35), (2) when [entering the context](https://github.com/pytorch/pytorch/blob/bb7c9a2d4127ff178fe8787caf070d7072d015b6/torch/autograd/grad_mode.py#L194), and (3) when [exiting the context](https://github.com/pytorch/pytorch/blob/bb7c9a2d4127ff178fe8787caf070d7072d015b6/torch/autograd/grad_mode.py#L197). This results in the the retraced export module to have a duplicate `torch._C._set_grad_enabled` like: ``` def forward(self, arg0_1): add = torch.ops.aten.add.Tensor(arg0_1, 1); arg0_1 = None _set_grad_enabled = torch._C._set_grad_enabled(False); _set_grad_enabled = None _set_grad_enabled = torch._C._set_grad_enabled(False); _set_grad_enabled = None add_1 = torch.ops.aten.add.Tensor(add, 2); add = None _set_grad_enabled_1 = torch._C._set_grad_enabled(True); _set_grad_enabled_1 = None add_2 = torch.ops.aten.add.Tensor(add_1, 3); add_1 = None return (add_2,) ``` When export runs the `replace_set_grad_with_hop_pass`, it will look through the graph for `torch._C._set_grad_enabled` and create subgraphs. The duplicate `torch._C._set_grad_enabled` results in an empty submod in the graph, which resulted in an error in [this post](https://fb.workplace.com/groups/1028545332188949/posts/1844720036398281/?comment_id=1862175381319413). Pull Request resolved: pytorch#163295 Approved by: https://github.com/yushangdi
Fixes pytorch#163294 The code `with torch.set_grad_enabled(enable_grad)` calls `torch._C._set_grad_enabled` three times -- (1) when [initializing set_grad_enabled](https://github.com/pytorch/pytorch/blob/bb7c9a2d4127ff178fe8787caf070d7072d015b6/torch/autograd/grad_mode.py#L187C9-L187C35), (2) when [entering the context](https://github.com/pytorch/pytorch/blob/bb7c9a2d4127ff178fe8787caf070d7072d015b6/torch/autograd/grad_mode.py#L194), and (3) when [exiting the context](https://github.com/pytorch/pytorch/blob/bb7c9a2d4127ff178fe8787caf070d7072d015b6/torch/autograd/grad_mode.py#L197). This results in the the retraced export module to have a duplicate `torch._C._set_grad_enabled` like: ``` def forward(self, arg0_1): add = torch.ops.aten.add.Tensor(arg0_1, 1); arg0_1 = None _set_grad_enabled = torch._C._set_grad_enabled(False); _set_grad_enabled = None _set_grad_enabled = torch._C._set_grad_enabled(False); _set_grad_enabled = None add_1 = torch.ops.aten.add.Tensor(add, 2); add = None _set_grad_enabled_1 = torch._C._set_grad_enabled(True); _set_grad_enabled_1 = None add_2 = torch.ops.aten.add.Tensor(add_1, 3); add_1 = None return (add_2,) ``` When export runs the `replace_set_grad_with_hop_pass`, it will look through the graph for `torch._C._set_grad_enabled` and create subgraphs. The duplicate `torch._C._set_grad_enabled` results in an empty submod in the graph, which resulted in an error in [this post](https://fb.workplace.com/groups/1028545332188949/posts/1844720036398281/?comment_id=1862175381319413). Pull Request resolved: pytorch#163295 Approved by: https://github.com/yushangdi
Fixes #163294
The code
with torch.set_grad_enabled(enable_grad)
callstorch._C._set_grad_enabled
three times -- (1) when initializing set_grad_enabled, (2) when entering the context, and (3) when exiting the context.This results in the the retraced export module to have a duplicate
torch._C._set_grad_enabled
like:When export runs the
replace_set_grad_with_hop_pass
, it will look through the graph fortorch._C._set_grad_enabled
and create subgraphs. The duplicatetorch._C._set_grad_enabled
results in an empty submod in the graph, which resulted in an error in this post.