Repository: Lightning-AI/pytorch-lightning

Issue:
Relax requirement for DistributedSampler with ddp
**Is your feature request related to a problem? Please describe.**
I have an application where I'm using a custom `BatchSampler` to construct batches for the N-Pairs metric learning loss. I need all of the data to be available on all processes when using `DistributedDataParallel`, so I wouldn't want to use `DistributedSampler`, even if it was compatible with a custom `BatchSampler`. Right now, I've hit a wall because lightning throws this exception:

```
pytorch_lightning.utilities.debugging.MisconfigurationException: 
when using multiple gpus and multiple nodes you must pass
 a DistributedSampler to DataLoader(sampler).

ie: this:
dataset = myDataset()
dataloader = Dataloader(dataset)

becomes:
dataset = myDataset()
dist_sampler = torch.utils.data.distributed.DistributedSampler(dataset)
dataloader = Dataloader(dataset, sampler=dist_sampler)
```

**Describe the solution you'd like**
Could this exception be turned into a warning? I'm all for letting the user know when they're violating best practices, but throwing an exception removes flexibility for advanced users.

**Describe alternatives you've considered**
I looked at using the dp backend, but that's not going to work because the n-pairs loss needs the entire batch to compute the loss. Splitting it into chunks breaks things.

If I'm understanding correctly, this is actually another limitation introduced by Lightning. In a usual `DataParallel` setting, the batch would be merged back together before computing the loss and everything would be fine.


Discussion:
good point. mind changing to a warning and submitting a PR?

Resolving patch:
diff --git a/pytorch_lightning/models/trainer.py b/pytorch_lightning/models/trainer.py
--- a/pytorch_lightning/models/trainer.py
+++ b/pytorch_lightning/models/trainer.py
@@ -444,8 +444,9 @@ def get_dataloaders(self, model):
 
         if self.use_ddp and not isinstance(self.tng_dataloader.sampler, DistributedSampler):
             msg = """
-when using multiple gpus and multiple nodes you must pass
- a DistributedSampler to DataLoader(sampler).
+You're using multiple gpus and multiple nodes without using a DistributedSampler
+to assign a subset of your data to each process. To silence this warning, pass a
+DistributedSampler to your DataLoader.
 
 ie: this:
 dataset = myDataset()
@@ -455,8 +456,10 @@ def get_dataloaders(self, model):
 dataset = myDataset()
 dist_sampler = torch.utils.data.distributed.DistributedSampler(dataset)
 dataloader = Dataloader(dataset, sampler=dist_sampler)
+
+If you want each process to load the full dataset, ignore this warning.
 """
-            raise MisconfigurationException(msg)
+            warnings.warn(msg)
 
     # -----------------------------
     # MODEL TRAINING


Test patch:
diff --git a/tests/test_models.py b/tests/test_models.py
--- a/tests/test_models.py
+++ b/tests/test_models.py
@@ -665,7 +665,7 @@ def test_ddp_sampler_error():
         use_amp=True
     )
 
-    with pytest.raises(MisconfigurationException):
+    with pytest.warns(UserWarning):
         trainer.get_dataloaders(model)
 
     clear_save_dir()