Skip to content
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

BatchSplittingSampler return wrong length #640

Closed
dwahdany opened this issue Mar 22, 2024 · 5 comments
Closed

BatchSplittingSampler return wrong length #640

dwahdany opened this issue Mar 22, 2024 · 5 comments

Comments

@dwahdany
Copy link
Contributor

🐛 Bug

BatchSplittingSampler reports the length as

expected_batch_size = self.sampler.sample_rate * self.sampler.num_samples
return int(len(self.sampler) * (expected_batch_size / self.max_batch_size))

Converting the result simply to int leads to the resulted number of batches being one too low. Instead, we need to ceil the result first:

expected_batch_size = self.sampler.sample_rate * self.sampler.num_samples
return int(np.ceil(len(self.sampler) * (expected_batch_size / self.max_batch_size)))

Some libraries like pytorch lightning will skip the last batch if this is reported wrong, resulting in no actual step occuring at all.

@HuanyuZhang
Copy link
Contributor

Thanks for contributing to Opacus. Will take a look.

@dwahdany
Copy link
Contributor Author

dwahdany commented May 7, 2024

Thanks for contributing to Opacus. Will take a look.

Have you had the time to look into this? It seems like a straightfoward fix.

@HuanyuZhang
Copy link
Contributor

Thanks for contributing to Opacus! The fix makes sense to me.
Just a qq: what is the point of using int(np,ceil())? How about just using math.ceil()?

@dwahdany
Copy link
Contributor Author

dwahdany commented May 17, 2024

Thanks for contributing to Opacus! The fix makes sense to me. Just a qq: what is the point of using int(np,ceil())? How about just using math.ceil()?

Thanks for pointing that out. Bad habit I guess, math.ceil is better. I changed it to math.ceil

@s-zanella
Copy link

This patch is still computing incorrectly the expected number of batches. See #516.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants