Fix incorrect sample_rate with WeightedRandomSampler (Issue #813)#816
Open
intagliated wants to merge 2 commits into
Open
Fix incorrect sample_rate with WeightedRandomSampler (Issue #813)#816intagliated wants to merge 2 commits into
intagliated wants to merge 2 commits into
Conversation
…rch#813) WeightedRandomSampler caused sample_rate to be computed from num_samples instead of dataset size, burning epsilon 781x faster than expected silently. Fix: compute sample_rate as batch_size / len(dataset) which is correct for all sampler types. Also adds UserWarning when WeightedRandomSampler is detected. Same fix applied to DPDataLoader.from_data_loader().
|
@facebook-github-bot has imported this pull request. If you are a Meta employee, you can view this in D102646860. (Because this pull request was imported automatically, there will not be any future comments.) |
Author
|
Hi @HuanyuZhang, I noticed this PR was imported into Meta's internal system (D98158224) and assigned a month ago. Thanks for your time. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem Statement
When using
WeightedRandomSamplerwith aDataLoader, Opacus'smake_private()andmake_private_with_epsilon()were computing an incorrectsample_rate.The original logic derived the rate from
1 / len(data_loader). However, for weighted samplers,len(data_loader)returns the number of batches, not the dataset size. This caused the privacy budget to be consumed significantly faster than reported, effectively breaking the Differential Privacy (DP) guarantees.Solution
The calculation was refactored to be mathematically consistent by grounding the rate in the absolute dataset length and explicit batch size.
Key Changes:
metadata_epsilon, allowing Laplace noise injection into the dataset sizebatch_sizefrom either theDataLoaderorBatchSamplerto handleNoneTypeedge cases in newer PyTorch versions.UserWarningwhenWeightedRandomSampleris detected to ensure transparency in how the privacy rate is derived.Verification
Validated using a dedicated audit script (
verify_randomness.py) comparing the expected privacy ratio against the actual consumption.