opacus icon indicating copy to clipboard operation
opacus copied to clipboard

Add support for 'same' and 'valid' for padding in conv

Open ashkan-software opened this issue 3 years ago • 11 comments

Differential Revision: D37811390

ashkan-software avatar Jul 13 '22 07:07 ashkan-software

This pull request was exported from Phabricator. Differential Revision: D37811390

facebook-github-bot avatar Jul 13 '22 07:07 facebook-github-bot

This pull request was exported from Phabricator. Differential Revision: D37811390

facebook-github-bot avatar Jul 14 '22 08:07 facebook-github-bot

So the reason for the failing tests in that padding="same" introduces uneven padding, which previously wasn't supported by either PyTorch or opacus.

All numerical values of padding in convolution layers pad equally from both sides, which we assumed to be the case when writing grad sampler for opacus. With padding="same" it's possible to have uneven padding: if total padding length is odd number, the padding on the right will be larger by one.

I've prototyped the solution in #456 - it's based on your commit, I've fixed 1d and 2d cases. Still pending is:

  1. Fix 3d case (similar to 2d)
  2. Run benchmarks. I call for F.pad() in all cases, regardless of the input parameters. This is the most straightforward option, but might not be the most efficient. It's possible that doing padding inside conv layer is more efficient - if that's true, we'd want to add an extra if-statement and only do padding manually if required (F.pad allows uneven padding, conv layer doesn't)
  3. Ideally, we'd like this to be supported by ExpandedWeights too. So we'd need to talk to @samdow about adapting this changes in core pytorch code

ffuuugor avatar Jul 22 '22 17:07 ffuuugor

This pull request was exported from Phabricator. Differential Revision: D37811390

facebook-github-bot avatar Jul 29 '22 06:07 facebook-github-bot

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

facebook-github-bot avatar Jul 29 '22 06:07 facebook-github-bot

@ashkan-software do you know why commit CircleCI tests weren't triggered? LGTM overall, I can accept if all the tests and linters pass

ffuuugor avatar Jul 29 '22 16:07 ffuuugor

@ashkan-software For ExpandedWeights tests failures, I don't think they should be blocking for merging this. For now you can disable this particular combination of input arguments (we have ew_compatible parameter in _run method for this). And add a #TODO to the code so we remember to enable it later when EW starts supporting it. Additionally, it'd be great if you add this info to grad_sample/README.md, where we document discrepancies between hook-based and ew-based GradSampleModules.

@samdow, do we have an issue created for this, so we can link it for future tracking?

ffuuugor avatar Aug 10 '22 12:08 ffuuugor

This pull request was exported from Phabricator. Differential Revision: D37811390

facebook-github-bot avatar Aug 11 '22 01:08 facebook-github-bot

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

facebook-github-bot avatar Aug 11 '22 01:08 facebook-github-bot

@ffuuugor thanks a lot for your recommendations. I applied them all and we are good to go!

ashkan-software avatar Aug 11 '22 03:08 ashkan-software

@samdow, do we have an issue created for this, so we can link it for future tracking?

No but I'm planning to add this today thanks to talk with @ashkan-software! If I don't get to it today, I'll file an issue 😄 Thanks for flagging this and doing the heavy lifting of the implementation

samdow avatar Aug 11 '22 15:08 samdow

This pull request was exported from Phabricator. Differential Revision: D37811390

facebook-github-bot avatar Sep 01 '22 15:09 facebook-github-bot