We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
diable val and test shuffling
Fixes part of #1551 

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

{"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@@ -214,7 +214,7 @@\n         # shuffling in val and test set is bad practice\n         for loader in dataloaders:\n             if mode in ('val', 'test') and hasattr(loader, 'sampler') and isinstance(loader.sampler, RandomSampler):\n-                raise MisconfigurationException(\n+                rank_zero_warn(\n                     f'Your {mode}_dataloader has shuffle=True, it is best practice to turn'\n                     ' this off for validation and test dataloaders.')\n", "issue": "diable val and test shuffling\nFixes part of #1551 \n", "before_files": [{"content": "import platform\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, RandomSampler\nfrom torch.utils.data.distributed import DistributedSampler\n\nfrom pytorch_lightning.core import LightningModule\nfrom pytorch_lightning.utilities import rank_zero_warn\nfrom pytorch_lightning.utilities.exceptions import MisconfigurationException\n\ntry:\n    from torch.utils.data import IterableDataset\n    ITERABLE_DATASET_EXISTS = True\nexcept ImportError:\n    ITERABLE_DATASET_EXISTS = False\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\ntry:\n    import horovod.torch as hvd\nexcept ImportError:\n    HOROVOD_AVAILABLE = False\nelse:\n    HOROVOD_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.'\n                             ' Please make sure that your Dataloader at least 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    use_horovod: 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    replace_sampler_ddp: bool\n\n    @abstractmethod\n    def is_overridden(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        on_windows = platform.system() == 'Windows'\n\n        if isinstance(dataloader, DataLoader) and dataloader.num_workers <= 2 and not on_windows:\n            rank_zero_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        # don't manipulate iterable datasets\n        is_dataloader = isinstance(dataloader, DataLoader)\n\n        is_iterable_ds = False\n        if ITERABLE_DATASET_EXISTS and hasattr(dataloader, 'dataset'):\n            is_iterable_ds = isinstance(dataloader.dataset, IterableDataset)\n\n        if not is_dataloader or is_iterable_ds:\n            return dataloader\n        need_dist_sampler = (self.use_ddp or self.use_ddp2 or self.use_horovod or self.use_tpu)\n\n        if self.replace_sampler_ddp and need_dist_sampler:\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            elif self.use_horovod:\n                sampler = DistributedSampler(dataloader.dataset,\n                                             num_replicas=hvd.size(),\n                                             rank=hvd.rank())\n            else:\n                world_size = {\n                    'ddp': self.num_nodes * self.num_processes,\n                    'ddp2': self.num_nodes,\n                    'ddp_cpu': self.num_processes * self.num_nodes\n                }\n                sampler = DistributedSampler(\n                    dataloader.dataset,\n                    num_replicas=world_size[self.distributed_backend],\n                    rank=self.proc_rank,\n                )\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'\n                        ' or when 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, 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        # shuffling in val and test set is bad practice\n        for loader in dataloaders:\n            if mode in ('val', 'test') and hasattr(loader, 'sampler') and isinstance(loader.sampler, RandomSampler):\n                raise MisconfigurationException(\n                    f'Your {mode}_dataloader has shuffle=True, it is best practice to turn'\n                    ' this off for validation and test dataloaders.')\n\n        if any([dl is None for dl in dataloaders]):\n            rank_zero_warn(\"One of given dataloaders is None and it will be skipped.\")\n\n        # add samplers\n        dataloaders = [self.auto_add_sampler(dl, train=False) for dl in dataloaders if dl is not None]\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'\n                    f' or when 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_overridden('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_overridden('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        elif self.use_horovod:\n            # all processes wait until data download has happened\n            hvd.join()\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 platform\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, RandomSampler\nfrom torch.utils.data.distributed import DistributedSampler\n\nfrom pytorch_lightning.core import LightningModule\nfrom pytorch_lightning.utilities import rank_zero_warn\nfrom pytorch_lightning.utilities.exceptions import MisconfigurationException\n\ntry:\n    from torch.utils.data import IterableDataset\n    ITERABLE_DATASET_EXISTS = True\nexcept ImportError:\n    ITERABLE_DATASET_EXISTS = False\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\ntry:\n    import horovod.torch as hvd\nexcept ImportError:\n    HOROVOD_AVAILABLE = False\nelse:\n    HOROVOD_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.'\n                             ' Please make sure that your Dataloader at least 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    use_horovod: 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    replace_sampler_ddp: bool\n\n    @abstractmethod\n    def is_overridden(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        on_windows = platform.system() == 'Windows'\n\n        if isinstance(dataloader, DataLoader) and dataloader.num_workers <= 2 and not on_windows:\n            rank_zero_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        # don't manipulate iterable datasets\n        is_dataloader = isinstance(dataloader, DataLoader)\n\n        is_iterable_ds = False\n        if ITERABLE_DATASET_EXISTS and hasattr(dataloader, 'dataset'):\n            is_iterable_ds = isinstance(dataloader.dataset, IterableDataset)\n\n        if not is_dataloader or is_iterable_ds:\n            return dataloader\n        need_dist_sampler = (self.use_ddp or self.use_ddp2 or self.use_horovod or self.use_tpu)\n\n        if self.replace_sampler_ddp and need_dist_sampler:\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            elif self.use_horovod:\n                sampler = DistributedSampler(dataloader.dataset,\n                                             num_replicas=hvd.size(),\n                                             rank=hvd.rank())\n            else:\n                world_size = {\n                    'ddp': self.num_nodes * self.num_processes,\n                    'ddp2': self.num_nodes,\n                    'ddp_cpu': self.num_processes * self.num_nodes\n                }\n                sampler = DistributedSampler(\n                    dataloader.dataset,\n                    num_replicas=world_size[self.distributed_backend],\n                    rank=self.proc_rank,\n                )\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'\n                        ' or when 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, 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        # shuffling in val and test set is bad practice\n        for loader in dataloaders:\n            if mode in ('val', 'test') and hasattr(loader, 'sampler') and isinstance(loader.sampler, RandomSampler):\n                rank_zero_warn(\n                    f'Your {mode}_dataloader has shuffle=True, it is best practice to turn'\n                    ' this off for validation and test dataloaders.')\n\n        if any([dl is None for dl in dataloaders]):\n            rank_zero_warn(\"One of given dataloaders is None and it will be skipped.\")\n\n        # add samplers\n        dataloaders = [self.auto_add_sampler(dl, train=False) for dl in dataloaders if dl is not None]\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'\n                    f' or when 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_overridden('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_overridden('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        elif self.use_horovod:\n            # all processes wait until data download has happened\n            hvd.join()\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"}]}