Skip to content

Adding StratifiedBatchSampler for guaranteed subgroup representation - #3817

Open
avishkarsonni wants to merge 2 commits into
pytorch:masterfrom
avishkarsonni:master
Open

Adding StratifiedBatchSampler for guaranteed subgroup representation#3817
avishkarsonni wants to merge 2 commits into
pytorch:masterfrom
avishkarsonni:master

Conversation

@avishkarsonni

@avishkarsonni avishkarsonni commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Fixes #3653

Description:
This PR adds StratifiedBatchSampler as a guaranteed way to add fixed number of samples of every group in every batch for the methods where missing groups causes in omission of loss terms (Group DRO and Contrastive learning) which adds another method to existing methods like WeightedRandomSampler, DistributedSampler , auto_dataloader
Check list:

  • New tests are added (if a new feature is added)
  • New doc strings: description and/or example code are in RST format
  • Documentation is updated (if required)

avishkarsonni and others added 2 commits July 23, 2026 21:35
- Adding this batch sampler as a feature so that sample of every grp is added to every batch for Group DRo and contrastive learning (methods where missing group zeroes a loss term)

- 'oversample' is by default and 'undersample' to discard the smallest group

In a distributed configuration, groups are sharded across ranks
individually, so that every batch of every rank still contains every
group

Subclasses torch BatchSampler rather than Sampler, so that a
DeterministicEngine can wrap it in ReproducibleBatchSampler.
@github-actions github-actions Bot added docs module: distributed Distributed module labels Jul 23, 2026
return iter(indices)


class StratifiedBatchSampler(BatchSampler):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@vfdev-5 I am not sure if this is the correct place to put the sampler class. However, we really dont have a samplers/ folder either in ignite. Would it make sense to have ignite/samplers? like we have for metrics, handlers and so on.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think since DistributedProxySampler broke this rule years ago and creating new ignite/samplers directory will basically break the public path and create all new boilerplate, so I don't think it works well and it is for only 2-3 sampler as of now

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

Labels

docs module: distributed Distributed module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: StratifiedBatchSampler for Guaranteed Subgroup Representation

2 participants