# Buggy code
def auto_add_sampler(self, dataloader: DataLoader, train: bool) -> DataLoader:
    # don't do anything if it's not a dataloader
    # don't manipulate iterable datasets
    is_dataloader = isinstance(dataloader, DataLoader)

    is_iterable_ds = False
    if ITERABLE_DATASET_EXISTS and hasattr(dataloader, "dataset"):
        is_iterable_ds = isinstance(dataloader.dataset, IterableDataset)

    if not is_dataloader or is_iterable_ds:
        return dataloader
    need_dist_sampler = (
        self.use_ddp or self.use_ddp2 or self.use_horovod or self.use_tpu
    )

    if self.replace_sampler_ddp and need_dist_sampler:
        if not isinstance(dataloader.sampler, (SequentialSampler, RandomSampler)):
            raise MisconfigurationException(
                "You seem to have configured a sampler in your DataLoader. This will be replaced "
                " by `DistributedSampler` since `replace_sampler_ddp` is True and you are using"
                " distributed training. Either remove the sampler from your DataLoader or set"
                " `replace_sampler_ddp`=False if you want to use your custom sampler."
            )

        # replace with distributed sampler
        sampler = self._get_distributed_sampler(dataloader)
        dataloader = self.replace_sampler(dataloader, sampler)

    return dataloader


# Traceback (TypeError)
python main.py # with bs=1
GPU available: True, used: False
TPU available: False, using: 0 TPU cores

| Name | Type   | Params
--------------------------------
0 | fst  | Linear | 21
1 | snd  | Linear | 40
/home/constantin/.virtualenvs/tensor/lib64/python3.6/site-packages/pytorch_lightning/utilities/distributed.py:25: UserWarning: The dataloader, train dataloader, does not have many workers which may be a bottleneck. Consider increasing the value of the `num_workers` argument` (try 8 which is the number of cpus on this machine) in the `DataLoader` init to improve performance.
warnings.warn(*args, **kwargs)
/home/constantin/.virtualenvs/tensor/lib64/python3.6/site-packages/pytorch_lightning/utilities/distributed.py:25: UserWarning: The dataloader, val dataloader 0, does not have many workers which may be a bottleneck. Consider increasing the value of the `num_workers` argument` (try 8 which is the number of cpus on this machine) in the `DataLoader` init to improve performance.
warnings.warn(*args, **kwargs)
Epoch 1:  50%|████████████████Traceback (most recent call last):████████████████████████████████████████████████████████████████████████                                                                                                                          | 1024/2048 [00:00<00:00, 1337.66it/s, loss=1.032, v_num=19]
File "main.py", line 57, in <module>
trainer.fit(net, train_dataloader=DataLoader(dataset, batch_size=bs), val_dataloaders=DataLoader(dataset, batch_size=bs))
File "/home/constantin/.virtualenvs/tensor/lib64/python3.6/site-packages/pytorch_lightning/trainer/trainer.py", line 954, in fit
self.run_pretrain_routine(model)
File "/home/constantin/.virtualenvs/tensor/lib64/python3.6/site-packages/pytorch_lightning/trainer/trainer.py", line 1093, in run_pretrain_routine
self.train()
File "/home/constantin/.virtualenvs/tensor/lib64/python3.6/site-packages/pytorch_lightning/trainer/training_loop.py", line 375, in train
self.run_training_epoch()
File "/home/constantin/.virtualenvs/tensor/lib64/python3.6/site-packages/pytorch_lightning/trainer/training_loop.py", line 490, in run_training_epoch
self.run_evaluation(test_mode=self.testing)
File "/home/constantin/.virtualenvs/tensor/lib64/python3.6/site-packages/pytorch_lightning/trainer/evaluation_loop.py", line 379, in run_evaluation
eval_results = self._evaluate(self.model, dataloaders, max_batches, test_mode)
File "/home/constantin/.virtualenvs/tensor/lib64/python3.6/site-packages/pytorch_lightning/trainer/evaluation_loop.py", line 281, in _evaluate
output = self.evaluation_forward(model, batch, batch_idx, dataloader_idx, test_mode)
File "/home/constantin/.virtualenvs/tensor/lib64/python3.6/site-packages/pytorch_lightning/trainer/evaluation_loop.py", line 452, in evaluation_forward
output = model.validation_step(*args)
File "main.py", line 39, in validation_step
raise NotImplementedError()
NotImplementedError
Exception ignored in: <object repr() failed>
Traceback (most recent call last):
File "/home/constantin/.virtualenvs/tensor/lib64/python3.6/site-packages/tqdm/std.py", line 1086, in __del__
File "/home/constantin/.virtualenvs/tensor/lib64/python3.6/site-packages/tqdm/std.py", line 1293, in close
File "/home/constantin/.virtualenvs/tensor/lib64/python3.6/site-packages/tqdm/std.py", line 1471, in display
File "/home/constantin/.virtualenvs/tensor/lib64/python3.6/site-packages/tqdm/std.py", line 1089, in __repr__
File "/home/constantin/.virtualenvs/tensor/lib64/python3.6/site-packages/tqdm/std.py", line 1433, in format_dict
TypeError: 'NoneType' object is not iterable

# Fixed code
def auto_add_sampler(self, dataloader: DataLoader, train: bool) -> DataLoader:
    # don't do anything if it's not a dataloader
    is_dataloader = isinstance(dataloader, DataLoader)
    # don't manipulate iterable datasets
    is_iterable_ds = _has_iterable_dataset(dataloader)

    if not is_dataloader or is_iterable_ds:
        return dataloader
    need_dist_sampler = (
        self.use_ddp or self.use_ddp2 or self.use_horovod or self.use_tpu
    )

    if self.replace_sampler_ddp and need_dist_sampler:
        if not isinstance(dataloader.sampler, (SequentialSampler, RandomSampler)):
            raise MisconfigurationException(
                "You seem to have configured a sampler in your DataLoader. This will be replaced "
                " by `DistributedSampler` since `replace_sampler_ddp` is True and you are using"
                " distributed training. Either remove the sampler from your DataLoader or set"
                " `replace_sampler_ddp`=False if you want to use your custom sampler."
            )

        # replace with distributed sampler
        sampler = self._get_distributed_sampler(dataloader)
        dataloader = self.replace_sampler(dataloader, sampler)

    return dataloader