-
Notifications
You must be signed in to change notification settings - Fork 57
Validate structural arguments (num_bands, truncated sizes) in accounting events #284
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
Open
r1cksync
wants to merge
1
commit into
google-deepmind:main
Choose a base branch
from
r1cksync:harden-accounting-validation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+94
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -233,6 +233,95 @@ def test_use_zcdp_matches_gaussian_privacy(self): | |
| # as the continuous Gaussian with the same sigma. | ||
| self.assertAlmostEqual(eps_continuous, eps_discrete, places=4) | ||
|
|
||
| # -- Structural-argument validation ---------------------------------------- | ||
| # num_bands flows into math.ceil(iterations / num_bands); the truncated sizes | ||
| # flow into TruncatedSubsampledGaussianDpEvent. These were previously | ||
| # unvalidated: num_bands=0 raised an opaque ZeroDivisionError, num_bands<0 | ||
| # raised a misleading "iterations=..." error, and negative sizes were silently | ||
| # accepted and produced a nonsensical event. | ||
|
|
||
| @parameterized.parameters(0, -1, -16) | ||
| def test_amplified_bandmf_event_rejects_nonpositive_num_bands( | ||
| self, num_bands | ||
| ): | ||
| with self.assertRaisesRegex(ValueError, rf"num_bands={num_bands} > 0"): | ||
| accounting.amplified_bandmf_event( | ||
| 1.0, 128, num_bands=num_bands, sampling_prob=0.01 | ||
| ) | ||
|
|
||
| @parameterized.parameters(0, -1, -16) | ||
| def test_truncated_amplified_bandmf_rejects_nonpositive_num_bands( | ||
| self, num_bands | ||
| ): | ||
| with self.assertRaisesRegex(ValueError, rf"num_bands={num_bands} > 0"): | ||
| accounting.truncated_amplified_bandmf_event( | ||
| 1.0, | ||
| 128, | ||
| num_bands=num_bands, | ||
| sampling_prob=0.01, | ||
| largest_group_size=1000, | ||
| truncated_batch_size=16, | ||
| ) | ||
|
|
||
| @parameterized.parameters( | ||
| dict(num_examples=-1, truncated_batch_size=16), | ||
| dict(num_examples=1000, truncated_batch_size=-1), | ||
| dict(num_examples=-5, truncated_batch_size=-3), | ||
| ) | ||
| def test_truncated_dpsgd_event_rejects_negative_sizes( | ||
| self, num_examples, truncated_batch_size | ||
| ): | ||
| with self.assertRaisesRegex(ValueError, r">= 0"): | ||
| accounting.truncated_dpsgd_event( | ||
| 1.0, | ||
| 10, | ||
| sampling_prob=0.1, | ||
| num_examples=num_examples, | ||
| truncated_batch_size=truncated_batch_size, | ||
| ) | ||
|
|
||
| def test_truncated_amplified_bandmf_rejects_negative_sizes(self): | ||
| # largest_group_size / truncated_batch_size are forwarded to | ||
| # truncated_dpsgd_event and validated there. | ||
| with self.assertRaisesRegex(ValueError, r">= 0"): | ||
| accounting.truncated_amplified_bandmf_event( | ||
| 1.0, | ||
| 128, | ||
| num_bands=16, | ||
| sampling_prob=0.01, | ||
| largest_group_size=-1, | ||
| truncated_batch_size=16, | ||
| ) | ||
|
|
||
| def test_amplified_bandmf_event_valid_num_bands_unchanged(self): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think '_unchanged' here and in the below test names is unnecessary and if anything adds confusion. |
||
| # Regression: positive num_bands is unaffected. The mechanism runs | ||
| # rounds = ceil(iterations / num_bands) DP-SGD steps. | ||
| event = accounting.amplified_bandmf_event( | ||
| 1.0, 128, num_bands=16, sampling_prob=0.01 | ||
| ) | ||
| self.assertIsInstance(event, dp_accounting.dp_event.SelfComposedDpEvent) | ||
| self.assertEqual(event.count, 8) # ceil(128 / 16) | ||
|
|
||
| def test_truncated_amplified_bandmf_valid_args_unchanged(self): | ||
| event = accounting.truncated_amplified_bandmf_event( | ||
| 1.0, | ||
| 128, | ||
| num_bands=16, | ||
| sampling_prob=0.01, | ||
| largest_group_size=1000, | ||
| truncated_batch_size=16, | ||
| ) | ||
| self.assertIsInstance(event, dp_accounting.dp_event.SelfComposedDpEvent) | ||
| self.assertEqual(event.count, 8) # ceil(128 / 16) | ||
|
|
||
| def test_truncated_dpsgd_event_valid_args_unchanged(self): | ||
| # Regression: non-negative (incl. zero) sizes still build the event. | ||
| event = accounting.truncated_dpsgd_event( | ||
| 1.0, 10, sampling_prob=0.1, num_examples=1000, truncated_batch_size=16 | ||
| ) | ||
| self.assertIsInstance(event, dp_accounting.dp_event.SelfComposedDpEvent) | ||
| self.assertEqual(event.count, 10) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| absltest.main() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think none of the comments introduced in this CL are necessary, and would suggest removing them all.
From the test names I think it is pretty easy to infer what each test is supposed to do, and the logic can be inferred from reading the file being tested. We generally prefer not to include comments if they only explain something that can be inferred by someone with knowledge of Python.