Feature Request: StratifiedBatchSampler for Guaranteed Subgroup Representation
🚀 Feature
When training on datasets with class imbalance, it is critical to have every subgroup represented in every batch. PyTorch's built-in WeightedRandomSampler allows us to assign weights to each subgroup but still relies on random chance, meaning it does not strictly guarantee that rare classes will appear in a specific batch.
For algorithms like Group Distributionally Robust Optimization (DRO) or Contrastive Learning, this is critical since if a group is not represented in a batch, the loss component can potentially become zero and can destabilize training.
If this makes sense, we also have to take care of this question:
- Discarding or Oversampling:
StratifiedBatchSamplercan either oversample the group with the least datapoints or it can create the total number of batches such that it exhausts the subgroup with the least amount of datapoints, which leads to wastage of datapoints of highly-represented classes. We should discuss whether we want to keep both options or not, and which option would act as the default then.
I already have a working prototype of this from a recent project and can modify it for this.
@vfdev-5, any thoughts regarding this?
Hi maintainers! I'd love to take this on and implement the StratifiedBatchSampler for Ignite.
Regarding the oversampling vs. discarding trade-off mentioned in the description, I propose we add an explicit argument, for example, strategy="oversample" or strategy="downsample" (or a boolean like drop_last_groups), with a sensible default like oversampling to ensure no data is wasted out of the box, while giving users full control depending on their Group DRO or Contrastive Learning setup.
I can structure this cleanly by inheriting from torch.utils.data.Sampler and adding comprehensive tests. Could you please assign this to me so I can get started?
@GAUTAMSANSWAL Thanks for the interest! How would this work in distributed environments? It can be useful to ignite depending on its scope on if it would work distributed enviornments, etc.
On the above comment from @GAUTAMSANSWAL there can be a few refinements, strategy="oversample" | "undersample" extends better than drop_last_groups=True/False, at least not better than having a string used at call site.
If the dropped set is fixed, that majority data becomes permanent dead weight. Rotating it per epoch means the full majority set is still seen across training, so no data is truly wasted.
Guardrails should be set so batch_size >= num_groups and raise a clear error on imbalance or a group not getting equal representation than underfilling batches.
and to answer @TahaZahid05 The clean approach is
sharding within each group across ranks, so every rank's every batch still
contains all groups, naively wrapping in DistributedSampler can split strata
across ranks and break the per-batch guarantee. Even if implemented in stages,
the API should be designed so distributed support drops in without breaking
changes. Happy to align on the approach.
and if this issue is not active I can take up on this feature if any suggestions are made.
@avishkarsonni Sounds good to me! If you would like to take it up, go ahead. Thanks!
is the prototype of the project you mentioned in the issue is available publicly? can be good for reference?
You can use this as reference: https://github.com/TahaZahid05/SemEval26-Task-11/blob/3fe25cb94dd561b608c1b5674dd17dc666571b13/training_scripts/st1.ipynb#L1095
Search for StratifiedBatchSampler in the notebook.
@vfdev-5 can you check on this PR?