Skip to content
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

zero_grad() between forward and backward passes generates error message #423

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions opacus/privacy_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,20 @@
from torch.utils.data import DataLoader


def forbid_accumulation_hook(module: GradSampleModule, _grad_input: torch.Tensor, _grad_output: torch.Tensor):
def forbid_accumulation_hook(
module: GradSampleModule, _grad_input: torch.Tensor, _grad_output: torch.Tensor
):
"""
Model hook that detects repetitive forward/backward passes between optimizer steps.

This is a backward hook that will be wrapped around the whole model using
`register_full_backward_hook`. Hence, this hook will be *the first* to be called
among all backward hooks. In particular, it will be called *before* all hooks
present in the `autograd_grad_sample_hooks` attribute of `GradSampleModule`.
Hence, if `optimizer.zero_grad()` is not called before the backward hook and if
some `p.grad_sample` is not None, it means that `p.grad_sample` was updated in
a *previous* iteration.
`register_backward_hook`. We wish to detect a case where:
- `optimizer.zero_grad()` is not called before the backward pass; and
- `p.grad_sample` was updated in a *previous* iteration.

To do so, we attach a backward hook to the model that runs *after* the computation
of `grad_sample` for the current step. We compute the number of accumulated iterations
like on `optimizers/optimizer.py` and check whether it's strictly larger than one.

Args:
module: input module
Expand All @@ -55,11 +58,17 @@ def forbid_accumulation_hook(module: GradSampleModule, _grad_input: torch.Tensor

for p in module.parameters():
if p.grad_sample is not None:
raise ValueError(
"Poisson sampling is not compatible with grad accumulation. "
"You need to call optimizer.step() after every forward/backward pass "
"or consider using BatchMemoryManager"
)
if isinstance(p.grad_sample, torch.Tensor):
accumulated_iterations = 1
elif isinstance(p.grad_sample, list):
accumulated_iterations = len(p.grad_sample)

if accumulated_iterations > 1:
raise ValueError(
"Poisson sampling is not compatible with grad accumulation. "
"You need to call optimizer.step() after every forward/backward pass "
"or consider using BatchMemoryManager"
)


class PrivacyEngine:
Expand Down Expand Up @@ -354,7 +363,7 @@ def make_private(
module, batch_first=batch_first, loss_reduction=loss_reduction
)
if poisson_sampling:
module.register_full_backward_hook(forbid_accumulation_hook)
module.register_backward_hook(forbid_accumulation_hook)

data_loader = self._prepare_data_loader(
data_loader, distributed=distributed, poisson_sampling=poisson_sampling
Expand Down