feat: Add public utility for per-sample gradient validation (#484)#810
Conversation
|
Hi @chidoziemanagwu! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
|
hi @iden-kalemaj @alexandresablayrolles the CLA has been signed and all import checks are passing. This PR adds a public diagnostic utility for per-sample gradient verification (Issue #484). Would you be able to approve the workflows and review when you have a moment? Thank you. |
|
@facebook-github-bot has imported this pull request. If you are a Meta employee, you can view this in D98158224. (Because this pull request was imported automatically, there will not be any future comments.) |
|
Hi @HuanyuZhang, I noticed this PR was imported into Meta's internal system (D98158224) and assigned a few weeks ago. Just checking in to see if there is any feedback from the internal review or if there are any additional changes needed on my end to move this toward a merge. This utility will be a great help for anyone building custom layers in Opacus! Thanks for your time. |
|
Thx @chidoziemanagwu for the reminder. Just left some comments. |
|
|
||
| __all__ = [ | ||
| "check_per_sample_gradients_are_correct", | ||
| "get_per_sample_gradient_diagnostics", |
There was a problem hiding this comment.
Any reason why we need to expose check_per_sample_gradients_are_correct? I thought get_per_sample_gradient_diagnostics should have contained all the information needed.
| ``` | ||
|
|
||
| The simpler `check_per_sample_gradients_are_correct` function is also available | ||
| if you only need a boolean pass/fail result. |
There was a problem hiding this comment.
Let us hide check_per_sample_gradients_are_correct for simplicity.
| report = get_per_sample_gradient_diagnostics(x, model) | ||
| self.assertTrue(report["passed"]) | ||
|
|
||
| def test_public_import_path(self): |
There was a problem hiding this comment.
Any chance we could add a test diagnosing mismatched gradients (i.e., assertFalse rather than assertTrue)?
…gradients_are_correct - Remove check_per_sample_gradients_are_correct from public opacus.utils API - Drop README mention of the boolean helper for simplicity - Add diagnostics test exercising the mismatched-gradient (failing) path Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
…gradients_are_correct - Remove check_per_sample_gradients_are_correct from public opacus.utils API - Drop README mention of the boolean helper for simplicity - Add diagnostics test exercising the mismatched-gradient (failing) path
fcaf563 to
69253de
Compare
|
Hello @HuanyuZhang I made an update pls review :) |
Types of changes
Motivation and Context / Related issue
Fixes #484: Utility to test correctness of per sample gradients
Context:
Currently, users building custom privacy models or extending Opacus with custom
grad_samplershave no public-facing mechanism to verify that Opacus computes their per-sample gradients mathematically correctly. The internal validation methodcheck_per_sample_gradients_are_correctswallows all diagnostic data and only returns a boolean flag, making it impossible for engineers to debug why their Layer gradient failed.The Approach:
I extracted the core comparison logic between Opacus's reference micro-batch computation and the optimized hook computation into a newly exported public API:
get_per_sample_gradient_diagnostics.meanandsumloss reduction paradigms.opacus/utils/__init__.py.Community Impact (Scalability & Architecture):
By exposing this tool, external researchers and security engineers can independently verify their custom mechanisms against the Opacus framework. This lowers the technical barrier to entry for developing novel Differential Privacy architectures, directly shifting the validation and debugging burden away from project maintainers.
How Has This Been Tested (if it applies)
I expanded the
opacus.tests.per_sample_gradients_utils_test.pysuite. AlongsideConv1dandLineartests, I introduced coverage forLayerNormarrays, validated the exact structured dictionary returned by the new diagnostic tool, and tested the public import routing. I also updated theREADME.mdto reflect these public enhancements.Usage Example:
Test Execution & Visual Proof (Terminal Log):
Checklist