When samples don't stack cleanly
The default collate function stacks every field of each sample into a tensor. That works when all your samples have the same shape — image batches, fixed-feature regression. It breaks when sequences have variable length, when samples carry per-item metadata, or when you want to batch in some custom way.
Custom collate
A collate function takes List[Sample] and returns a batch. Most common variant: pad variable-length sequences to the longest in the batch.
IterableDataset — streaming data
For datasets that are too big to fit on disk (giant text corpora, network-streamed data, online sensor feeds), implement IterableDataset instead of Dataset. You only define __iter__(self); PyTorch will iterate batches from your iterator.
The catch with multi-worker IterableDatasets: each worker receives the full iterator unless you split it manually. Use torch.utils.data.get_worker_info() inside __iter__ to slice the stream by worker id.