Skip to content
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
Fixed the clipping operation in fast gradient clipping with Privacy E…
…ngine (#664)

Summary:
Pull Request resolved: #664

Earlier, privacy engine erroneously wasn't using the max_grad_norm argument which infoms the clipping norm. It is fixed now.

Reviewed By: HuanyuZhang

Differential Revision: D60917356
  • Loading branch information
EnayatUllah authored and facebook-github-bot committed Aug 12, 2024
commit 0ca48db01109b96dbcb5772793b8ba318a697629
23 changes: 17 additions & 6 deletions opacus/privacy_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ def _prepare_model(
module: nn.Module,
*,
batch_first: bool = True,
max_grad_norm: Union[float, List[float]] = 1.0,
loss_reduction: str = "mean",
grad_sample_mode: str = "hooks",
) -> AbstractGradSampleModule:
Expand All @@ -194,12 +195,21 @@ def _prepare_model(

return module
else:
return wrap_model(
module,
grad_sample_mode=grad_sample_mode,
batch_first=batch_first,
loss_reduction=loss_reduction,
)
if grad_sample_mode == "ghost":
return wrap_model(
module,
grad_sample_mode=grad_sample_mode,
batch_first=batch_first,
loss_reduction=loss_reduction,
max_grad_norm=max_grad_norm,
)
else:
return wrap_model(
module,
grad_sample_mode=grad_sample_mode,
batch_first=batch_first,
loss_reduction=loss_reduction,
)

def is_compatible(
self,
Expand Down Expand Up @@ -355,6 +365,7 @@ def make_private(
module = self._prepare_model(
module,
batch_first=batch_first,
max_grad_norm=max_grad_norm,
loss_reduction=loss_reduction,
grad_sample_mode=grad_sample_mode,
)
Expand Down