forked from pytorch/opacus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
One backward function for Ghost Clipping (pytorch#661)
Summary: Pull Request resolved: pytorch#661 Simplfied training loop for ghost clipping using only one "double backward" function. Reviewed By: HuanyuZhang Differential Revision: D60427371 fbshipit-source-id: 73c016a31f0692adcfa3f6838e74315fbed26bb1
- Loading branch information
1 parent
a059670
commit 4804a51
Showing
3 changed files
with
53 additions
and
14 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import torch | ||
from opacus.grad_sample.grad_sample_module_fast_gradient_clipping import ( | ||
GradSampleModuleFastGradientClipping, | ||
) | ||
from opacus.optimizers import DPOptimizerFastGradientClipping | ||
|
||
|
||
def double_backward( | ||
module: GradSampleModuleFastGradientClipping, | ||
optimizer: DPOptimizerFastGradientClipping, | ||
loss_per_sample: torch.Tensor, | ||
) -> None: | ||
""" | ||
Packages the training loop for Fast Gradient and Ghost Clipping. It does the two backward passes, as well as the loss rescaling and hook operations in between. | ||
This function also works with DistributedDPOptimizer. | ||
Args: | ||
module: The DP gradient sample module to train | ||
optimizer: The DP optimizer used to train the module | ||
loss_per_sample: loss on each sample in the mini-batch of size [batch_size, 1] | ||
Returns: | ||
None | ||
""" | ||
|
||
torch.mean(loss_per_sample).backward(retain_graph=True) | ||
optimizer.zero_grad() | ||
rescaled_loss_per_sample = module.get_coeff() * loss_per_sample | ||
rescaled_loss = torch.sum(rescaled_loss_per_sample) | ||
module.disable_hooks() | ||
rescaled_loss.backward() | ||
module.enable_hooks() |