Skip to content

Fix "to_standard_module" for Ghost Clipping #754

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
18 changes: 10 additions & 8 deletions opacus/grad_sample/gsm_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@

logger = logging.getLogger(__name__)

OPACUS_PARAM_MONKEYPATCH_ATTRS = ["_forward_counter", "_current_grad_sample"]
OPACUS_PARAM_MONKEYPATCH_ATTRS = [
"grad_sample",
"_forward_counter",
"_current_grad_sample",
"_norm_sample",
]


class AbstractGradSampleModule(nn.Module, ABC):
Expand Down Expand Up @@ -131,18 +136,15 @@ def to_standard_module(self) -> nn.Module:
return self._module

def _close(self):
self.del_grad_sample()
self._clean_up_attributes()

def __repr__(self):
return f"{type(self).__name__}({self._module.__repr__()})"

def _clean_up_attributes(self):
# Clean up attributes
for attr in OPACUS_PARAM_MONKEYPATCH_ATTRS:
for p in self.parameters():
if hasattr(p, attr):
delattr(p, attr)

def __repr__(self):
return f"{type(self).__name__}({self._module.__repr__()})"

def forbid_grad_accumulation(self):
"""
Sets a flag to detect gradient accumulation (multiple forward/backward passes
Expand Down
2 changes: 1 addition & 1 deletion opacus/tests/grad_sample_module_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def test_to_standard_module(self):
self.original_model.state_dict(),
strict=True,
)
new_grad_sample_module = GradSampleModule(
new_grad_sample_module = self.CLS(
copy_of_original_model, batch_first=True, loss_reduction="mean"
)

Expand Down