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

Utility to test the correctness of per sample gradients #484

Open
ffuuugor opened this issue Aug 26, 2022 · 6 comments
Open

Utility to test the correctness of per sample gradients #484

ffuuugor opened this issue Aug 26, 2022 · 6 comments

Comments

@ffuuugor
Copy link
Contributor

ffuuugor commented Aug 26, 2022

For custom grad_samplers or functorch-based implementation

@ffuuugor
Copy link
Contributor Author

More context.

The first and essential step of DP-SGD implementation is computing per-sample gradients. While Opacus provides various approaches to do so (hooks, ExpandedWeights, functorch) and performs some level of model validation, it is still possible to end up with wrong per-sample gradients which Opacus will not notify you of.

There are multiple potential scenarios why it's possible, but one stands out as the most important. Opacus has an implicit requirement that any input model always maintain batch dimension as the first dimension throughout the forward() method (or second if batch_first=False). Which means simple x.transpose() can break all privacy guarantees.
Detecting such behaviour would require deep introspection of the model and we haven't found a generic enough way to do this for arbitrary input models.

There's, however, one approach that we already use to verify correctness of the per-sample computation: compare against microbatches. By definition, "per-sample gradient" is a gradient you would get if a given sample was the only one in a batch. It is, however, quite easy to split the batch into N micro-batches of size 1 and use these gradients to verify p.grad_sample.

Here's how we do it for our unit test: link

The idea of this new feature is to expose similar utility to an end user to let them check that:

  • Their model doesn't do any funny business with batch dimensions
  • Whatever code opacus uses to compute per-sample gradients works well for their model specifically (we've had interesting edge cases in the past where RNNs or GANs would misbehave)

@aaossa
Copy link

aaossa commented Jan 23, 2023

Hi, any updates on #532 ? (Just lurking but it seems that the PR was left behind for some reason)

@karthikprasad
Copy link
Contributor

@aaossa , looks like the PR has been approved and is pending merge. @psolikov, would you be able to rebase and merge it?

@psolikov
Copy link
Contributor

@aaossa @karthikprasad Thanks for reminding! I'll try to merge soon.

facebook-github-bot pushed a commit that referenced this issue Feb 10, 2023
Summary:
## Types of changes

- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
- [x] Docs change / refactoring / dependency upgrade

## Motivation and Context / Related issue

Implementation of the utility described in #484.

Refactored the code to avoid code duplicates.

## How Has This Been Tested (if it applies)

Added the new utility as a test case for existing tests stored in `tests.grad_samples`.

## Checklist

- [x] The documentation is up-to-date with the changes I made.
- [x] I have read the **CONTRIBUTING** document and completed the CLA (see **CONTRIBUTING**).
- [x] All tests passed, and additional code has been covered with new tests.

Pull Request resolved: #532

Reviewed By: karthikprasad

Differential Revision: D40797432

Pulled By: ffuuugor

fbshipit-source-id: 923009d6f7f6d4c34bce9f4af39945fdf9ff9d57
@aaossa
Copy link

aaossa commented Sep 17, 2024

Seems like this should be closed since #532 contents were already merged (again, just lurking my notifications and found this)

@HuanyuZhang
Copy link
Contributor

Let us keep it, since we may want to pursue a better plan.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants