Skip to content

git commit -m "Fix sample_rate calculation with WeightedRandomSampler#814

Open
MN-NR wants to merge 2 commits into
meta-pytorch:mainfrom
MN-NR:fix-weighted-sampler-accounting
Open

git commit -m "Fix sample_rate calculation with WeightedRandomSampler#814
MN-NR wants to merge 2 commits into
meta-pytorch:mainfrom
MN-NR:fix-weighted-sampler-accounting

Conversation

@MN-NR

@MN-NR MN-NR commented Apr 9, 2026

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 WeightedRandomSampler, privacy accounting was silently broken. The sample_rate was computed from len(data_loader) (number of batches) instead of the true dataset size, causing privacy budget to burn 100x-1000x faster than expected.

Root Cause

  • sample_rate = 1 / len(data_loader) was computed AFTER _prepare_data_loader() replaced the sampler
  • For WeightedRandomSampler with num_samples=128 and batch_size=16 on a 100k dataset:
    • Buggy: sample_rate = 1/8 = 0.125 (8 batches)
    • Correct: sample_rate = 16/100000 = 0.00016
    • Result: 781x overestimate → epsilon burns 781x faster than user expects

Fix

Capture true_dataset_size and original_batch_size BEFORE _prepare_data_loader() modifies the sampler, ensuring privacy accounting uses the actual dataset size.

How Has This Been Tested

Checklist

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

When DataLoader uses WeightedRandomSampler, sample_rate was incorrectly
computed from len(data_loader) instead of the true dataset size, causing
privacy budget to burn 100x-1000x faster than expected.

Fixed by capturing dataset size and batch_size before _prepare_data_loader()
replaces the sampler, ensuring privacy accounting uses actual dataset size.

Fixes #[813]"
@meta-cla

meta-cla Bot commented Apr 9, 2026

Copy link
Copy Markdown

Hi @MN-Noor!

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 Apr 10, 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 Apr 10, 2026
@meta-codesync

meta-codesync Bot commented Apr 10, 2026

Copy link
Copy Markdown

@facebook-github-bot has imported this pull request. If you are a Meta employee, you can view this in D100263104. (Because this pull request was imported automatically, there will not be any future comments.)

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