-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an option to zero out the gradient before the forward
Summary: Pull Request resolved: #4905 Currently the optimizer zeros the gradients after the forward and before the backward. In a recent PyTorch change, it set all gradients to None by default. This has a benefit of reducing the memory consumption (since all gradients are None). However, doing this after the forward does not provide any memory saving, since the the memory consumption is maximum at the end of forward. It doesn't matter whether the gradient is set to None before the forward or after the forward. So we should set it before the forward the enjoy the memory saving. We add a flag to enable it instead of doing it by default for now. Since people can override the zero_grad function (as the comment indicates), we do not know exactly what is done inside the function. This is to be on the safe side so the current flows may not be broken. Once we have gone through the existing flows, we should make the flag enabled by default. Reviewed By: tglik Differential Revision: D44264848 fbshipit-source-id: a68c7cbd36439faf65801f0f771ae8bc9c130699
- Loading branch information
1 parent
1bc3a33
commit 88217ca
Showing
1 changed file
with
29 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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