zero_grad() between forward and backward passes generates error message #422
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.
Summary:
In case of Poisson sampling, we must check that all
grad_sample
attributes are set toNone
(since we forbit accumulation with Poisson sampling).This was previously done in a forward hook, resulting in an error message being thrown inadequately, in particular when
zero_grad()
is called between the forward and backward steps (see task for more context).Hence, we instead register a backward hook at the
GradSampleModule
level (to ensure it's the first hook to be called, before the hooks that populate thegrad_sample
attribute). Updated the docstring accordingly with more details.Differential Revision: D36067469