Skip to content

Fix sample rate calculation for WeightedRandomSampler and custom samp… - #849

Open
laasyapriya2007 wants to merge 1 commit into
meta-pytorch:mainfrom
laasyapriya2007:fix-sampler-sample-rate-calculation
Open

laasyapriya2007 wants to merge 1 commit into
meta-pytorch:mainfrom
laasyapriya2007:fix-sampler-sample-rate-calculation

Conversation

@laasyapriya2007

Copy link
Copy Markdown

Types of changes

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

Motivation and Context / Related issue

Fixes #813.

When DataLoader uses samplers where len(sampler) != len(dataset) (such as WeightedRandomSampler), computing sample_rate = 1 / len(data_loader) causes sample_rate to be computed against len(sampler) rather than the full dataset size. This leads to an over-estimated sample_rate and an exploded expected_batch_size, burning the privacy budget prematurely.

This PR introduces:

  1. _get_batch_size and _get_sample_rate helper functions in opacus/data_loader.py to derive sample_rate = batch_size / len(dataset) when batch size is available.
  2. Accepts optional sample_rate in DPDataLoader.from_data_loader, make_private, and make_private_with_epsilon.
  3. Unit tests in dpdataloader_test.py and privacy_engine_test.py verifying correct sample_rate and expected_batch_size with WeightedRandomSampler and BatchSampler.

How Has This Been Tested

  • Added test_from_data_loader_sample_rate_weighted_sampler, test_from_data_loader_custom_sample_rate, and test_from_data_loader_with_batch_sampler in dpdataloader_test.py.
  • Added test_make_private_with_epsilon_weighted_sampler in privacy_engine_test.py.
  • Ran black, isort, and flake8 according to CONTRIBUTING.md.

Checklist

  • The documentation is up-to-date with the changes I made.
  • I have read the CONTRIBUTING document and completed the CLA.
  • All tests passed, and additional code has been covered with new tests.

@meta-cla

meta-cla Bot commented Oct 18, 2026

Copy link
Copy Markdown

Hi @laasyapriya2007!

Thank you for your pull request and welcome to our community.

Action Required

In 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.

Process

In 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 CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla

meta-cla Bot commented Oct 18, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 18, 2026
@laasyapriya2007

Copy link
Copy Markdown
Author

Gentle ping for review when someone has a moment — this corrects the sample-rate computation for WeightedRandomSampler and custom samplers (#813). Checks pass and the CLA is signed. Happy to make any changes.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WeightedRandomSampler causes silent epsilon miscalculation)

1 participant