Lightning-AI / Lightning-AI/pytorch-lightning
Using overfit_batches does not work with BatchSampler as Sampler
- Dominant language
- Python
- Stars
- 31.4k
- Forks
- 3.8k
- Avg merge
- 6d 7h
- Merged PRs (30d)
- 6
Description
### Bug description
I'm using `BatchSampler` so that a list of indexes are passed to my Dataset so I can do more efficient slicing. The dataloader is constructed like
```py
sampler = BatchSampler(SequentialSampler(dataset), batch_size=self.batch_size, drop_last=False)
dataloader = DataLoader(dataset,
batch_size=None, #disable automatic batching, use sampler for batching
sampler=sampler,
...)
```
This works as expected normally, but when I call `trainer.fit` with `overfit_batches`, the Dataset is only passed a single index as if automatic batching was enabled. A single example only makes it to the collate function and so on.
I don't follow exactly what's going on, but I think the dataloader's sampler (which is a BatchSampler) is being replaced by a SequentialSampler [here](https://github.com/Lightning-AI/lightning/blob/a86584d6dd4d50388c7dcef4f3854b0e8355b346/src/pytorch_lightning/trainer/connectors/data_connector.py#L476). I think this is in order to make sure the dataloader is not shuffling? Maybe a partial solution would be to only replace the sampler if it's a RandomSampler (rather than when it's ~not~ a SequentialSampler), and if something other than RandomSampler or SequentialSampler is found, print a warning that the user needs to make sure thye're not shuffling.
### How to reproduce the bug
_No response_
### Error messages and logs
```
# Error messages and logs here please
```
### Environment
```
#- Lightning Component (e.g. Trainer, LightningModule, LightningApp, LightningWork, LightningFlow):
#- PyTorch Lightning Version (e.g., 1.5.0): '1.8.3.post1'
#- Lightning App Version (e.g., 0.5.2): NA
#- PyTorch Version (e.g., 1.10): '1.12.1'
#- Python version (e.g., 3.9): 3.7.12
#- OS (e.g., Linux):Debian 4.19.260-1
#- CUDA/cuDNN version: 11.6
#- GPU models and configuration:
#- How you installed Lightning(`conda`, `pip`, source): pip
#- Running environment of LightningApp (e.g. local, cloud): GCP Workbench
```
### More info
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.