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

Better validator for matching parameters #489

Closed
wants to merge 4 commits into from
Closed

Conversation

ffuuugor
Copy link
Contributor

This PR resolves #477 and addresses various issues regarding parameter validation

Background

As identified in #432, we want to warn the user if optimizer parameters are different from model parameters (e.g. because the module has been replaced by the ModuleValidator). Optimizer expects the weights to have .grad_sample attribute populated by the GradSampleModule, so there's no sense in having optimizer with different set of params to the input model

Issue 1: Comparing values

One problem with how we do it currently is that we're looking for the wrong thing. We use torch.eq, which performs elementwise comparison and will output True for identical tensors. The problem is - we don't need identical, we need the exact same object - so that optimizer can read per-sample gradients computed by the GradSampleModule.

Issue 2: Frozen layers

When freezing some model layers, people are free to choose how to initialize their optimizer: with all model.parameters() or only those which are not frozen - it doesn't have any material difference on the training process.
However, if they do the latter, opacus will complain that model and optimizer weights are mismatched.

This PR

In this PR we address both issues by:

  1. Checking if the optimizer parameters is a subset of model parameters (not including some model params in the optimizer is perfectly fine)
  2. using in operator, which checks object and not its value
  3. Adding extra tests to cover issues described above

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 31, 2022
@facebook-github-bot
Copy link
Contributor

@ffuuugor has updated the pull request. You must reimport the pull request before landing.

@facebook-github-bot
Copy link
Contributor

@facebook-github-bot has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

Copy link
Contributor

@romovpa romovpa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.
Thank you for the detailed description!

):
model = self._init_model()
model = PrivacyEngine.get_compatible_module(model)
optimizer = torch.optim.SGD(model.parameters(), lr=self.LR, momentum=0)
optimizer = torch.optim.SGD(
[
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest something like this for readability

model.parameters() 
if not opt_exclude_frozen 
else [p for p in model.parameters() if p.requires_grad]

Same in the _init_vanilla_training

Copy link
Contributor

@karthikprasad karthikprasad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clean and quick fix!

@facebook-github-bot
Copy link
Contributor

@ffuuugor has updated the pull request. You must reimport the pull request before landing.

@facebook-github-bot
Copy link
Contributor

@facebook-github-bot has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@ffuuugor has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@ffuuugor ffuuugor deleted the ffuuugor_477 branch September 14, 2022 13:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Model finetuning
4 participants