We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Not auto add DistributedSampler for DDP training
## 🐛 Bug

<!-- A clear and concise description of what the bug is. -->
in 0.72, even if we don't set sampler, pytorch_lightning will not add  DistributedSampler for us.
### To Reproduce
the reason is in pytorch, if we don't set sampler,  pytorch will add a sampler for us.
in pytorch's dataloader.py:
```
         if sampler is None:  # give default samplers
            if self._dataset_kind == _DatasetKind.Iterable:
                # See NOTE [ Custom Samplers and IterableDataset ]
                sampler = _InfiniteConstantSampler()
            else:  # map-style
                if shuffle:
                    sampler = RandomSampler(dataset)
                else:
                    sampler = SequentialSampler(dataset)
```

but in pytorch_lightning we check whether sampler is None to  decide to add sampler 
in  data_loading.py funciton auto_add_sampler:
```
        no_sampler_added = dataloader.sampler is None
```

because pytorch have default sampler for us, which is not None, pytorch_lighting will not automatically add sampler.





--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `pytorch_lightning/trainer/data_loading.py`
Content:
```
1 import warnings
2 from abc import ABC, abstractmethod
3 from typing import Union, List, Tuple, Callable
4 
5 import torch.distributed as torch_distrib
6 from torch.utils.data import DataLoader
7 from torch.utils.data.distributed import DistributedSampler
8 
9 from pytorch_lightning.core import LightningModule
10 from pytorch_lightning.utilities.exceptions import MisconfigurationException
11 
12 try:
13     from apex import amp
14 except ImportError:
15     APEX_AVAILABLE = False
16 else:
17     APEX_AVAILABLE = True
18 
19 try:
20     import torch_xla
21     import torch_xla.core.xla_model as xm
22     import torch_xla.distributed.xla_multiprocessing as xmp
23 except ImportError:
24     XLA_AVAILABLE = False
25 else:
26     XLA_AVAILABLE = True
27 
28 
29 def _has_len(dataloader: DataLoader) -> bool:
30     """ Checks if a given Dataloader has __len__ method implemented i.e. if
31     it is a finite dataloader or infinite dataloader """
32     try:
33         # try getting the length
34         if len(dataloader) == 0:
35             raise ValueError('Dataloader returned 0 length. Please make sure'
36                              ' that your Dataloader atleast returns 1 batch')
37         return True
38     except TypeError:
39         return False
40 
41 
42 class TrainerDataLoadingMixin(ABC):
43 
44     # this is just a summary on variables used in this abstract class,
45     #  the proper values/initialisation should be done in child class
46     proc_rank: int
47     use_ddp: bool
48     use_ddp2: bool
49     shown_warnings: ...
50     val_check_interval: float
51     use_tpu: bool
52     tpu_local_core_rank: int
53     train_dataloader: DataLoader
54     num_training_batches: Union[int, float]
55     val_check_batch: ...
56     val_dataloaders: List[DataLoader]
57     num_val_batches: Union[int, float]
58     test_dataloaders: List[DataLoader]
59     num_test_batches: Union[int, float]
60     train_percent_check: float
61     val_percent_check: float
62     test_percent_check: float
63 
64     @abstractmethod
65     def is_overriden(self, *args):
66         """Warning: this is just empty shell for code implemented in other class."""
67 
68     def _percent_range_check(self, name: str) -> None:
69         value = getattr(self, name)
70         msg = f'`{name}` must lie in the range [0.0, 1.0], but got {value:.3f}.'
71         if name == 'val_check_interval':
72             msg += ' If you want to disable validation set `val_percent_check` to 0.0 instead.'
73 
74         if not 0. <= value <= 1.:
75             raise ValueError(msg)
76 
77     def _worker_check(self, dataloader: DataLoader, name: str) -> None:
78         if isinstance(dataloader, DataLoader) and dataloader.num_workers <= 2:
79             warnings.warn(f'The dataloader, {name}, does not have many workers which may be a bottleneck.'
80                           ' Consider increasing the value of the `num_workers` argument`'
81                           ' in the `DataLoader` init to improve performance.')
82 
83     def auto_add_sampler(self, dataloader: DataLoader, train: bool) -> DataLoader:
84 
85         # don't do anything if it's not a dataloader
86         if not isinstance(dataloader, DataLoader):
87             return dataloader
88 
89         need_dist_sampler = self.use_ddp or self.use_ddp2 or self.use_tpu
90         no_sampler_added = dataloader.sampler is None
91 
92         if need_dist_sampler and no_sampler_added:
93 
94             skip_keys = ['sampler', 'batch_sampler', 'dataset_kind']
95 
96             dl_args = {
97                 k: v for k, v in dataloader.__dict__.items() if not k.startswith('_') and k not in skip_keys
98             }
99 
100             if self.use_tpu:
101                 sampler = DistributedSampler(
102                     dataloader.dataset,
103                     num_replicas=xm.xrt_world_size(),
104                     rank=xm.get_ordinal()
105                 )
106             else:
107                 sampler = DistributedSampler(dataloader.dataset)
108 
109             dl_args['sampler'] = sampler
110             dataloader = type(dataloader)(**dl_args)
111 
112         return dataloader
113 
114     def reset_train_dataloader(self, model: LightningModule) -> None:
115         """Resets the train dataloader and initialises required variables
116         (number of batches, when to validate, etc.).
117 
118         Args:
119             model: The current `LightningModule`
120         """
121         self.train_dataloader = self.request_dataloader(model.train_dataloader)
122 
123         self.num_training_batches = 0
124 
125         # automatically add samplers
126         self.train_dataloader = self.auto_add_sampler(self.train_dataloader, train=True)
127 
128         self._worker_check(self.train_dataloader, 'train dataloader')
129         self._percent_range_check('train_percent_check')
130 
131         if not _has_len(self.train_dataloader):
132             self.num_training_batches = float('inf')
133         else:
134             # try getting the length
135             self.num_training_batches = len(self.train_dataloader)
136             self.num_training_batches = int(self.num_training_batches * self.train_percent_check)
137 
138         # determine when to check validation
139         # if int passed in, val checks that often
140         # otherwise, it checks in [0, 1.0] % range of a training epoch
141         if isinstance(self.val_check_interval, int):
142             self.val_check_batch = self.val_check_interval
143             if self.val_check_batch > self.num_training_batches:
144                 raise ValueError(
145                     f'`val_check_interval` ({self.val_check_interval}) must be less than or equal '
146                     f'to the number of the training batches ({self.num_training_batches}). '
147                     'If you want to disable validation set `val_percent_check` to 0.0 instead.')
148         else:
149             if not _has_len(self.train_dataloader):
150                 if self.val_check_interval == 1.0:
151                     self.val_check_batch = float('inf')
152                 else:
153                     raise MisconfigurationException(
154                         'When using an infinite DataLoader (e.g. with an IterableDataset or when '
155                         'DataLoader does not implement `__len__`) for `train_dataloader`, '
156                         '`Trainer(val_check_interval)` must be `1.0` or an int. An int k specifies '
157                         'checking validation every k training batches.')
158             else:
159                 self._percent_range_check('val_check_interval')
160 
161                 self.val_check_batch = int(self.num_training_batches * self.val_check_interval)
162                 self.val_check_batch = max(1, self.val_check_batch)
163 
164     def _reset_eval_dataloader(self, model: LightningModule,
165                                mode: str) -> Tuple[int, List[DataLoader]]:
166         """Generic method to reset a dataloader for evaluation.
167 
168         Args:
169             model: The current `LightningModule`
170             mode: Either `'val'` or `'test'`
171 
172         Returns:
173             Tuple (num_batches, dataloaders)
174         """
175         dataloaders = self.request_dataloader(getattr(model, f'{mode}_dataloader'))
176 
177         if not isinstance(dataloaders, list):
178             dataloaders = [dataloaders]
179 
180         # add samplers
181         dataloaders = [self.auto_add_sampler(dl, train=False) for dl in dataloaders if dl]
182 
183         num_batches = 0
184 
185         # determine number of batches
186         # datasets could be none, 1 or 2+
187         if len(dataloaders) != 0:
188             for i, dataloader in enumerate(dataloaders):
189                 self._worker_check(dataloader, f'{mode} dataloader {i}')
190                 if not _has_len(dataloader):
191                     num_batches = float('inf')
192 
193             percent_check = getattr(self, f'{mode}_percent_check')
194 
195             if num_batches != float('inf'):
196                 self._percent_range_check(f'{mode}_percent_check')
197 
198                 num_batches = sum(len(dataloader) for dataloader in dataloaders)
199                 num_batches = int(num_batches * percent_check)
200             elif percent_check not in (0.0, 1.0):
201                 raise MisconfigurationException(
202                     'When using an infinite DataLoader (e.g. with an IterableDataset or when '
203                     f'DataLoader does not implement `__len__`) for `{mode}_dataloader`, '
204                     f'`Trainer({mode}_percent_check)` must be `0.0` or `1.0`.')
205         return num_batches, dataloaders
206 
207     def reset_val_dataloader(self, model: LightningModule) -> None:
208         """Resets the validation dataloader and determines the number of batches.
209 
210         Args:
211             model: The current `LightningModule`
212         """
213         if self.is_overriden('validation_step'):
214             self.num_val_batches, self.val_dataloaders =\
215                 self._reset_eval_dataloader(model, 'val')
216 
217     def reset_test_dataloader(self, model) -> None:
218         """Resets the validation dataloader and determines the number of batches.
219 
220         Args:
221             model: The current `LightningModule`
222         """
223         if self.is_overriden('test_step'):
224             self.num_test_batches, self.test_dataloaders =\
225                 self._reset_eval_dataloader(model, 'test')
226 
227     def request_dataloader(self, dataloader_fx: Callable) -> DataLoader:
228         """Handles downloading data in the GPU or TPU case.
229 
230         Args:
231             dataloader_fx: The bound dataloader getter
232 
233         Returns:
234             The dataloader
235         """
236         dataloader = dataloader_fx()
237 
238         # get the function we'll use to get data
239         if self.use_ddp or self.use_ddp2:
240             # all processes wait until data download has happened
241             torch_distrib.barrier()
242 
243         # data download/load on TPU
244         elif self.use_tpu and XLA_AVAILABLE:
245             # all processes wait until data download has happened
246             torch_xla.core.xla_model.rendezvous('pl.TrainerDataLoadingMixin.get_dataloaders')
247 
248         return dataloader
249 
250     def determine_data_use_amount(self, train_percent_check: float, val_percent_check: float,
251                                   test_percent_check: float, overfit_pct: float) -> None:
252         """Use less data for debugging purposes
253         """
254         self.train_percent_check = train_percent_check
255         self.val_percent_check = val_percent_check
256         self.test_percent_check = test_percent_check
257         if overfit_pct > 0:
258             if overfit_pct > 1:
259                 raise ValueError(
260                     f'`overfit_pct` must be not greater than 1.0, but got {overfit_pct:.3f}.')
261 
262             self.train_percent_check = overfit_pct
263             self.val_percent_check = overfit_pct
264             self.test_percent_check = overfit_pct
265
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/pytorch_lightning/trainer/data_loading.py b/pytorch_lightning/trainer/data_loading.py
--- a/pytorch_lightning/trainer/data_loading.py
+++ b/pytorch_lightning/trainer/data_loading.py
@@ -87,9 +87,8 @@
             return dataloader
 
         need_dist_sampler = self.use_ddp or self.use_ddp2 or self.use_tpu
-        no_sampler_added = dataloader.sampler is None
 
-        if need_dist_sampler and no_sampler_added:
+        if need_dist_sampler:
 
             skip_keys = ['sampler', 'batch_sampler', 'dataset_kind']

{"golden_diff": "diff --git a/pytorch_lightning/trainer/data_loading.py b/pytorch_lightning/trainer/data_loading.py\n--- a/pytorch_lightning/trainer/data_loading.py\n+++ b/pytorch_lightning/trainer/data_loading.py\n@@ -87,9 +87,8 @@\n             return dataloader\n \n         need_dist_sampler = self.use_ddp or self.use_ddp2 or self.use_tpu\n-        no_sampler_added = dataloader.sampler is None\n \n-        if need_dist_sampler and no_sampler_added:\n+        if need_dist_sampler:\n \n             skip_keys = ['sampler', 'batch_sampler', 'dataset_kind']\n", "issue": "Not auto add DistributedSampler for DDP training\n## \ud83d\udc1b Bug\r\n\r\n<!-- A clear and concise description of what the bug is. -->\r\nin 0.72, even if we don't set sampler, pytorch_lightning will not add  DistributedSampler for us.\r\n### To Reproduce\r\nthe reason is in pytorch, if we don't set sampler,  pytorch will add a sampler for us.\r\nin pytorch's dataloader.py:\r\n```\r\n         if sampler is None:  # give default samplers\r\n            if self._dataset_kind == _DatasetKind.Iterable:\r\n                # See NOTE [ Custom Samplers and IterableDataset ]\r\n                sampler = _InfiniteConstantSampler()\r\n            else:  # map-style\r\n                if shuffle:\r\n                    sampler = RandomSampler(dataset)\r\n                else:\r\n                    sampler = SequentialSampler(dataset)\r\n```\r\n\r\nbut in pytorch_lightning we check whether sampler is None to  decide to add sampler \r\nin  data_loading.py funciton auto_add_sampler:\r\n```\r\n        no_sampler_added = dataloader.sampler is None\r\n```\r\n\r\nbecause pytorch have default sampler for us, which is not None, pytorch_lighting will not automatically add sampler.\r\n\r\n\r\n\r\n\n", "before_files": [{"content": "import warnings\nfrom abc import ABC, abstractmethod\nfrom typing import Union, List, Tuple, Callable\n\nimport torch.distributed as torch_distrib\nfrom torch.utils.data import DataLoader\nfrom torch.utils.data.distributed import DistributedSampler\n\nfrom pytorch_lightning.core import LightningModule\nfrom pytorch_lightning.utilities.exceptions import MisconfigurationException\n\ntry:\n    from apex import amp\nexcept ImportError:\n    APEX_AVAILABLE = False\nelse:\n    APEX_AVAILABLE = True\n\ntry:\n    import torch_xla\n    import torch_xla.core.xla_model as xm\n    import torch_xla.distributed.xla_multiprocessing as xmp\nexcept ImportError:\n    XLA_AVAILABLE = False\nelse:\n    XLA_AVAILABLE = True\n\n\ndef _has_len(dataloader: DataLoader) -> bool:\n    \"\"\" Checks if a given Dataloader has __len__ method implemented i.e. if\n    it is a finite dataloader or infinite dataloader \"\"\"\n    try:\n        # try getting the length\n        if len(dataloader) == 0:\n            raise ValueError('Dataloader returned 0 length. Please make sure'\n                             ' that your Dataloader atleast returns 1 batch')\n        return True\n    except TypeError:\n        return False\n\n\nclass TrainerDataLoadingMixin(ABC):\n\n    # this is just a summary on variables used in this abstract class,\n    #  the proper values/initialisation should be done in child class\n    proc_rank: int\n    use_ddp: bool\n    use_ddp2: bool\n    shown_warnings: ...\n    val_check_interval: float\n    use_tpu: bool\n    tpu_local_core_rank: int\n    train_dataloader: DataLoader\n    num_training_batches: Union[int, float]\n    val_check_batch: ...\n    val_dataloaders: List[DataLoader]\n    num_val_batches: Union[int, float]\n    test_dataloaders: List[DataLoader]\n    num_test_batches: Union[int, float]\n    train_percent_check: float\n    val_percent_check: float\n    test_percent_check: float\n\n    @abstractmethod\n    def is_overriden(self, *args):\n        \"\"\"Warning: this is just empty shell for code implemented in other class.\"\"\"\n\n    def _percent_range_check(self, name: str) -> None:\n        value = getattr(self, name)\n        msg = f'`{name}` must lie in the range [0.0, 1.0], but got {value:.3f}.'\n        if name == 'val_check_interval':\n            msg += ' If you want to disable validation set `val_percent_check` to 0.0 instead.'\n\n        if not 0. <= value <= 1.:\n            raise ValueError(msg)\n\n    def _worker_check(self, dataloader: DataLoader, name: str) -> None:\n        if isinstance(dataloader, DataLoader) and dataloader.num_workers <= 2:\n            warnings.warn(f'The dataloader, {name}, does not have many workers which may be a bottleneck.'\n                          ' Consider increasing the value of the `num_workers` argument`'\n                          ' in the `DataLoader` init to improve performance.')\n\n    def auto_add_sampler(self, dataloader: DataLoader, train: bool) -> DataLoader:\n\n        # don't do anything if it's not a dataloader\n        if not isinstance(dataloader, DataLoader):\n            return dataloader\n\n        need_dist_sampler = self.use_ddp or self.use_ddp2 or self.use_tpu\n        no_sampler_added = dataloader.sampler is None\n\n        if need_dist_sampler and no_sampler_added:\n\n            skip_keys = ['sampler', 'batch_sampler', 'dataset_kind']\n\n            dl_args = {\n                k: v for k, v in dataloader.__dict__.items() if not k.startswith('_') and k not in skip_keys\n            }\n\n            if self.use_tpu:\n                sampler = DistributedSampler(\n                    dataloader.dataset,\n                    num_replicas=xm.xrt_world_size(),\n                    rank=xm.get_ordinal()\n                )\n            else:\n                sampler = DistributedSampler(dataloader.dataset)\n\n            dl_args['sampler'] = sampler\n            dataloader = type(dataloader)(**dl_args)\n\n        return dataloader\n\n    def reset_train_dataloader(self, model: LightningModule) -> None:\n        \"\"\"Resets the train dataloader and initialises required variables\n        (number of batches, when to validate, etc.).\n\n        Args:\n            model: The current `LightningModule`\n        \"\"\"\n        self.train_dataloader = self.request_dataloader(model.train_dataloader)\n\n        self.num_training_batches = 0\n\n        # automatically add samplers\n        self.train_dataloader = self.auto_add_sampler(self.train_dataloader, train=True)\n\n        self._worker_check(self.train_dataloader, 'train dataloader')\n        self._percent_range_check('train_percent_check')\n\n        if not _has_len(self.train_dataloader):\n            self.num_training_batches = float('inf')\n        else:\n            # try getting the length\n            self.num_training_batches = len(self.train_dataloader)\n            self.num_training_batches = int(self.num_training_batches * self.train_percent_check)\n\n        # determine when to check validation\n        # if int passed in, val checks that often\n        # otherwise, it checks in [0, 1.0] % range of a training epoch\n        if isinstance(self.val_check_interval, int):\n            self.val_check_batch = self.val_check_interval\n            if self.val_check_batch > self.num_training_batches:\n                raise ValueError(\n                    f'`val_check_interval` ({self.val_check_interval}) must be less than or equal '\n                    f'to the number of the training batches ({self.num_training_batches}). '\n                    'If you want to disable validation set `val_percent_check` to 0.0 instead.')\n        else:\n            if not _has_len(self.train_dataloader):\n                if self.val_check_interval == 1.0:\n                    self.val_check_batch = float('inf')\n                else:\n                    raise MisconfigurationException(\n                        'When using an infinite DataLoader (e.g. with an IterableDataset or when '\n                        'DataLoader does not implement `__len__`) for `train_dataloader`, '\n                        '`Trainer(val_check_interval)` must be `1.0` or an int. An int k specifies '\n                        'checking validation every k training batches.')\n            else:\n                self._percent_range_check('val_check_interval')\n\n                self.val_check_batch = int(self.num_training_batches * self.val_check_interval)\n                self.val_check_batch = max(1, self.val_check_batch)\n\n    def _reset_eval_dataloader(self, model: LightningModule,\n                               mode: str) -> Tuple[int, List[DataLoader]]:\n        \"\"\"Generic method to reset a dataloader for evaluation.\n\n        Args:\n            model: The current `LightningModule`\n            mode: Either `'val'` or `'test'`\n\n        Returns:\n            Tuple (num_batches, dataloaders)\n        \"\"\"\n        dataloaders = self.request_dataloader(getattr(model, f'{mode}_dataloader'))\n\n        if not isinstance(dataloaders, list):\n            dataloaders = [dataloaders]\n\n        # add samplers\n        dataloaders = [self.auto_add_sampler(dl, train=False) for dl in dataloaders if dl]\n\n        num_batches = 0\n\n        # determine number of batches\n        # datasets could be none, 1 or 2+\n        if len(dataloaders) != 0:\n            for i, dataloader in enumerate(dataloaders):\n                self._worker_check(dataloader, f'{mode} dataloader {i}')\n                if not _has_len(dataloader):\n                    num_batches = float('inf')\n\n            percent_check = getattr(self, f'{mode}_percent_check')\n\n            if num_batches != float('inf'):\n                self._percent_range_check(f'{mode}_percent_check')\n\n                num_batches = sum(len(dataloader) for dataloader in dataloaders)\n                num_batches = int(num_batches * percent_check)\n            elif percent_check not in (0.0, 1.0):\n                raise MisconfigurationException(\n                    'When using an infinite DataLoader (e.g. with an IterableDataset or when '\n                    f'DataLoader does not implement `__len__`) for `{mode}_dataloader`, '\n                    f'`Trainer({mode}_percent_check)` must be `0.0` or `1.0`.')\n        return num_batches, dataloaders\n\n    def reset_val_dataloader(self, model: LightningModule) -> None:\n        \"\"\"Resets the validation dataloader and determines the number of batches.\n\n        Args:\n            model: The current `LightningModule`\n        \"\"\"\n        if self.is_overriden('validation_step'):\n            self.num_val_batches, self.val_dataloaders =\\\n                self._reset_eval_dataloader(model, 'val')\n\n    def reset_test_dataloader(self, model) -> None:\n        \"\"\"Resets the validation dataloader and determines the number of batches.\n\n        Args:\n            model: The current `LightningModule`\n        \"\"\"\n        if self.is_overriden('test_step'):\n            self.num_test_batches, self.test_dataloaders =\\\n                self._reset_eval_dataloader(model, 'test')\n\n    def request_dataloader(self, dataloader_fx: Callable) -> DataLoader:\n        \"\"\"Handles downloading data in the GPU or TPU case.\n\n        Args:\n            dataloader_fx: The bound dataloader getter\n\n        Returns:\n            The dataloader\n        \"\"\"\n        dataloader = dataloader_fx()\n\n        # get the function we'll use to get data\n        if self.use_ddp or self.use_ddp2:\n            # all processes wait until data download has happened\n            torch_distrib.barrier()\n\n        # data download/load on TPU\n        elif self.use_tpu and XLA_AVAILABLE:\n            # all processes wait until data download has happened\n            torch_xla.core.xla_model.rendezvous('pl.TrainerDataLoadingMixin.get_dataloaders')\n\n        return dataloader\n\n    def determine_data_use_amount(self, train_percent_check: float, val_percent_check: float,\n                                  test_percent_check: float, overfit_pct: float) -> None:\n        \"\"\"Use less data for debugging purposes\n        \"\"\"\n        self.train_percent_check = train_percent_check\n        self.val_percent_check = val_percent_check\n        self.test_percent_check = test_percent_check\n        if overfit_pct > 0:\n            if overfit_pct > 1:\n                raise ValueError(\n                    f'`overfit_pct` must be not greater than 1.0, but got {overfit_pct:.3f}.')\n\n            self.train_percent_check = overfit_pct\n            self.val_percent_check = overfit_pct\n            self.test_percent_check = overfit_pct\n", "path": "pytorch_lightning/trainer/data_loading.py"}], "after_files": [{"content": "import warnings\nfrom abc import ABC, abstractmethod\nfrom typing import Union, List, Tuple, Callable\n\nimport torch.distributed as torch_distrib\nfrom torch.utils.data import DataLoader\nfrom torch.utils.data.distributed import DistributedSampler\n\nfrom pytorch_lightning.core import LightningModule\nfrom pytorch_lightning.utilities.exceptions import MisconfigurationException\n\ntry:\n    from apex import amp\nexcept ImportError:\n    APEX_AVAILABLE = False\nelse:\n    APEX_AVAILABLE = True\n\ntry:\n    import torch_xla\n    import torch_xla.core.xla_model as xm\n    import torch_xla.distributed.xla_multiprocessing as xmp\nexcept ImportError:\n    XLA_AVAILABLE = False\nelse:\n    XLA_AVAILABLE = True\n\n\ndef _has_len(dataloader: DataLoader) -> bool:\n    \"\"\" Checks if a given Dataloader has __len__ method implemented i.e. if\n    it is a finite dataloader or infinite dataloader \"\"\"\n    try:\n        # try getting the length\n        if len(dataloader) == 0:\n            raise ValueError('Dataloader returned 0 length. Please make sure'\n                             ' that your Dataloader atleast returns 1 batch')\n        return True\n    except TypeError:\n        return False\n\n\nclass TrainerDataLoadingMixin(ABC):\n\n    # this is just a summary on variables used in this abstract class,\n    #  the proper values/initialisation should be done in child class\n    proc_rank: int\n    use_ddp: bool\n    use_ddp2: bool\n    shown_warnings: ...\n    val_check_interval: float\n    use_tpu: bool\n    tpu_local_core_rank: int\n    train_dataloader: DataLoader\n    num_training_batches: Union[int, float]\n    val_check_batch: ...\n    val_dataloaders: List[DataLoader]\n    num_val_batches: Union[int, float]\n    test_dataloaders: List[DataLoader]\n    num_test_batches: Union[int, float]\n    train_percent_check: float\n    val_percent_check: float\n    test_percent_check: float\n\n    @abstractmethod\n    def is_overriden(self, *args):\n        \"\"\"Warning: this is just empty shell for code implemented in other class.\"\"\"\n\n    def _percent_range_check(self, name: str) -> None:\n        value = getattr(self, name)\n        msg = f'`{name}` must lie in the range [0.0, 1.0], but got {value:.3f}.'\n        if name == 'val_check_interval':\n            msg += ' If you want to disable validation set `val_percent_check` to 0.0 instead.'\n\n        if not 0. <= value <= 1.:\n            raise ValueError(msg)\n\n    def _worker_check(self, dataloader: DataLoader, name: str) -> None:\n        if isinstance(dataloader, DataLoader) and dataloader.num_workers <= 2:\n            warnings.warn(f'The dataloader, {name}, does not have many workers which may be a bottleneck.'\n                          ' Consider increasing the value of the `num_workers` argument`'\n                          ' in the `DataLoader` init to improve performance.')\n\n    def auto_add_sampler(self, dataloader: DataLoader, train: bool) -> DataLoader:\n\n        # don't do anything if it's not a dataloader\n        if not isinstance(dataloader, DataLoader):\n            return dataloader\n\n        need_dist_sampler = self.use_ddp or self.use_ddp2 or self.use_tpu\n\n        if need_dist_sampler:\n\n            skip_keys = ['sampler', 'batch_sampler', 'dataset_kind']\n\n            dl_args = {\n                k: v for k, v in dataloader.__dict__.items() if not k.startswith('_') and k not in skip_keys\n            }\n\n            if self.use_tpu:\n                sampler = DistributedSampler(\n                    dataloader.dataset,\n                    num_replicas=xm.xrt_world_size(),\n                    rank=xm.get_ordinal()\n                )\n            else:\n                sampler = DistributedSampler(dataloader.dataset)\n\n            dl_args['sampler'] = sampler\n            dataloader = type(dataloader)(**dl_args)\n\n        return dataloader\n\n    def reset_train_dataloader(self, model: LightningModule) -> None:\n        \"\"\"Resets the train dataloader and initialises required variables\n        (number of batches, when to validate, etc.).\n\n        Args:\n            model: The current `LightningModule`\n        \"\"\"\n        self.train_dataloader = self.request_dataloader(model.train_dataloader)\n\n        self.num_training_batches = 0\n\n        # automatically add samplers\n        self.train_dataloader = self.auto_add_sampler(self.train_dataloader, train=True)\n\n        self._worker_check(self.train_dataloader, 'train dataloader')\n        self._percent_range_check('train_percent_check')\n\n        if not _has_len(self.train_dataloader):\n            self.num_training_batches = float('inf')\n        else:\n            # try getting the length\n            self.num_training_batches = len(self.train_dataloader)\n            self.num_training_batches = int(self.num_training_batches * self.train_percent_check)\n\n        # determine when to check validation\n        # if int passed in, val checks that often\n        # otherwise, it checks in [0, 1.0] % range of a training epoch\n        if isinstance(self.val_check_interval, int):\n            self.val_check_batch = self.val_check_interval\n            if self.val_check_batch > self.num_training_batches:\n                raise ValueError(\n                    f'`val_check_interval` ({self.val_check_interval}) must be less than or equal '\n                    f'to the number of the training batches ({self.num_training_batches}). '\n                    'If you want to disable validation set `val_percent_check` to 0.0 instead.')\n        else:\n            if not _has_len(self.train_dataloader):\n                if self.val_check_interval == 1.0:\n                    self.val_check_batch = float('inf')\n                else:\n                    raise MisconfigurationException(\n                        'When using an infinite DataLoader (e.g. with an IterableDataset or when '\n                        'DataLoader does not implement `__len__`) for `train_dataloader`, '\n                        '`Trainer(val_check_interval)` must be `1.0` or an int. An int k specifies '\n                        'checking validation every k training batches.')\n            else:\n                self._percent_range_check('val_check_interval')\n\n                self.val_check_batch = int(self.num_training_batches * self.val_check_interval)\n                self.val_check_batch = max(1, self.val_check_batch)\n\n    def _reset_eval_dataloader(self, model: LightningModule,\n                               mode: str) -> Tuple[int, List[DataLoader]]:\n        \"\"\"Generic method to reset a dataloader for evaluation.\n\n        Args:\n            model: The current `LightningModule`\n            mode: Either `'val'` or `'test'`\n\n        Returns:\n            Tuple (num_batches, dataloaders)\n        \"\"\"\n        dataloaders = self.request_dataloader(getattr(model, f'{mode}_dataloader'))\n\n        if not isinstance(dataloaders, list):\n            dataloaders = [dataloaders]\n\n        # add samplers\n        dataloaders = [self.auto_add_sampler(dl, train=False) for dl in dataloaders if dl]\n\n        num_batches = 0\n\n        # determine number of batches\n        # datasets could be none, 1 or 2+\n        if len(dataloaders) != 0:\n            for i, dataloader in enumerate(dataloaders):\n                self._worker_check(dataloader, f'{mode} dataloader {i}')\n                if not _has_len(dataloader):\n                    num_batches = float('inf')\n\n            percent_check = getattr(self, f'{mode}_percent_check')\n\n            if num_batches != float('inf'):\n                self._percent_range_check(f'{mode}_percent_check')\n\n                num_batches = sum(len(dataloader) for dataloader in dataloaders)\n                num_batches = int(num_batches * percent_check)\n            elif percent_check not in (0.0, 1.0):\n                raise MisconfigurationException(\n                    'When using an infinite DataLoader (e.g. with an IterableDataset or when '\n                    f'DataLoader does not implement `__len__`) for `{mode}_dataloader`, '\n                    f'`Trainer({mode}_percent_check)` must be `0.0` or `1.0`.')\n        return num_batches, dataloaders\n\n    def reset_val_dataloader(self, model: LightningModule) -> None:\n        \"\"\"Resets the validation dataloader and determines the number of batches.\n\n        Args:\n            model: The current `LightningModule`\n        \"\"\"\n        if self.is_overriden('validation_step'):\n            self.num_val_batches, self.val_dataloaders =\\\n                self._reset_eval_dataloader(model, 'val')\n\n    def reset_test_dataloader(self, model) -> None:\n        \"\"\"Resets the validation dataloader and determines the number of batches.\n\n        Args:\n            model: The current `LightningModule`\n        \"\"\"\n        if self.is_overriden('test_step'):\n            self.num_test_batches, self.test_dataloaders =\\\n                self._reset_eval_dataloader(model, 'test')\n\n    def request_dataloader(self, dataloader_fx: Callable) -> DataLoader:\n        \"\"\"Handles downloading data in the GPU or TPU case.\n\n        Args:\n            dataloader_fx: The bound dataloader getter\n\n        Returns:\n            The dataloader\n        \"\"\"\n        dataloader = dataloader_fx()\n\n        # get the function we'll use to get data\n        if self.use_ddp or self.use_ddp2:\n            # all processes wait until data download has happened\n            torch_distrib.barrier()\n\n        # data download/load on TPU\n        elif self.use_tpu and XLA_AVAILABLE:\n            # all processes wait until data download has happened\n            torch_xla.core.xla_model.rendezvous('pl.TrainerDataLoadingMixin.get_dataloaders')\n\n        return dataloader\n\n    def determine_data_use_amount(self, train_percent_check: float, val_percent_check: float,\n                                  test_percent_check: float, overfit_pct: float) -> None:\n        \"\"\"Use less data for debugging purposes\n        \"\"\"\n        self.train_percent_check = train_percent_check\n        self.val_percent_check = val_percent_check\n        self.test_percent_check = test_percent_check\n        if overfit_pct > 0:\n            if overfit_pct > 1:\n                raise ValueError(\n                    f'`overfit_pct` must be not greater than 1.0, but got {overfit_pct:.3f}.')\n\n            self.train_percent_check = overfit_pct\n            self.val_percent_check = overfit_pct\n            self.test_percent_check = overfit_pct\n", "path": "pytorch_lightning/trainer/data_loading.py"}]}