Skip to content

Feat: Support dynamic batch size in BatchSamplerShard with even_batches#3969

Open
yuxinyuan wants to merge 2 commits into
huggingface:mainfrom
yuxinyuan:batch_sampler_shard_even_batches
Open

Feat: Support dynamic batch size in BatchSamplerShard with even_batches#3969
yuxinyuan wants to merge 2 commits into
huggingface:mainfrom
yuxinyuan:batch_sampler_shard_even_batches

Conversation

@yuxinyuan
Copy link
Copy Markdown
Contributor

@yuxinyuan yuxinyuan commented Mar 10, 2026

What does this PR do?

BatchSamplerShard previously forces uneven batches (even_batches=False) for dynamic batch sizes (e.g. batch_size is None). This PR extends it to work correctly with samplers that yield variable-length batches (i.e., batch_size=None), while preserving all existing behavior.

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@yuxinyuan
Copy link
Copy Markdown
Contributor Author

@SunMarc Can you take a look

@github-actions
Copy link
Copy Markdown
Contributor

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the contributing guidelines are likely to be ignored.

@yuxinyuan
Copy link
Copy Markdown
Contributor Author

Anyone has time to look at this ?

@github-actions
Copy link
Copy Markdown
Contributor

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the contributing guidelines are likely to be ignored.

Copy link
Copy Markdown
Member

@SunMarc SunMarc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks ! I'm just curious when you happened to need this

@yuxinyuan
Copy link
Copy Markdown
Contributor Author

Usually batch_size is None means the batch sampler produces dynamic sized batch of data.

The BatchSamplerShard already supports batch_size is None case. But it requires even_batches to be False (no loop back at the end of iteration). This PR just removes this constraint (even_batches can be True after merging this PR).

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 this pull request may close these issues.

2 participants