-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
[inductor] TorchInductor does not correctly recognize the grad status of model code #125474
Comments
This is reported by a torchbench user: pytorch/benchmark#2253 |
The simplest workaround is to move the
But I think we should be able to fix this. The problem is that your compiled region enables AOTAutograd has some similar logic to recognize when it is compiling for inference vs training here It's a bit annoying though, since inductor is choosing what fw_compiler to use before invoking AOTAutograd. So we would have to change the "fw_compiler" that we pass in from inductor, to dynamically switch between the inference or fw variant, depending on what AOTAutograd discovered. |
Thanks @bdhirsh for the response. I will keep this issue open since it is a valid issue with simple script to reproduce. On the torchbench side, we will workaround it by moving the |
Summary: Related Inductor issue: pytorch/pytorch#125474. Inductor cannot correctly handle `torch.no_grad()` context when it is inside the model code. To best leverage inductor code on inference, we are removing the `torch.no_grad()` context requirement from the model code. Instead, use the `pick_grad` helper function to manage the grad context from the framework level. Fixes #2253 Pull Request resolved: #2256 Reviewed By: aaronenyeshi Differential Revision: D57009780 Pulled By: xuzhao9 fbshipit-source-id: 08998e0da99105e1b4fcd5ca2cb3ba2764513f69
馃悰 Describe the bug
At
pytorch/torch/_inductor/compile_fx.py
Line 1371 in 3061025
torch.is_grad_enabled()
isTrue
even if the compiled code setswith torch.no_grad()
.Reproduction script:
Versions
The latest nightly release (20240503).
cc @ezyang @msaroufim @bdhirsh @anijain2305 @chauhang @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @peterbell10 @ipiszy @yf225 @chenyang78 @kadeng @muchulee8 @ColinPeppler @amjames @desertfire
The text was updated successfully, but these errors were encountered: