-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Warn user when IterableDataset has __len__ defined #2437
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d00b2d5
add warning when getting checking len
awaelchli 55953eb
added test
awaelchli 1899418
changelog
awaelchli c88ec3e
pep
awaelchli 47d8141
do not show warning below 1.4
awaelchli a4e44cf
try version parse
awaelchli 4f4449b
comments
awaelchli 2647c45
xfail
awaelchli e29f740
Update requirements/base.txt
williamFalcon bdd15fe
Update pytorch_lightning/trainer/data_loading.py
williamFalcon 3e5f673
version
Borda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import multiprocessing | ||
import platform | ||
from abc import ABC, abstractmethod | ||
from distutils.version import LooseVersion | ||
from typing import Union, List, Tuple, Callable, Optional | ||
import multiprocessing | ||
|
||
import torch | ||
import torch.distributed as torch_distrib | ||
from torch.utils.data import DataLoader, RandomSampler, SequentialSampler | ||
from torch.utils.data.distributed import DistributedSampler | ||
|
@@ -41,19 +43,33 @@ | |
HOROVOD_AVAILABLE = True | ||
|
||
|
||
def _has_iterable_dataset(dataloader: DataLoader): | ||
return ITERABLE_DATASET_EXISTS and hasattr(dataloader, 'dataset') \ | ||
and isinstance(dataloader.dataset, IterableDataset) | ||
|
||
|
||
def _has_len(dataloader: DataLoader) -> bool: | ||
""" Checks if a given Dataloader has __len__ method implemented i.e. if | ||
it is a finite dataloader or infinite dataloader """ | ||
it is a finite dataloader or infinite dataloader. """ | ||
|
||
try: | ||
# try getting the length | ||
if len(dataloader) == 0: | ||
raise ValueError('`Dataloader` returned 0 length.' | ||
' Please make sure that your Dataloader at least returns 1 batch') | ||
return True | ||
has_len = True | ||
except TypeError: | ||
return False | ||
has_len = False | ||
except NotImplementedError: # e.g. raised by torchtext if a batch_size_fn is used | ||
return False | ||
has_len = False | ||
Comment on lines
61
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if the resulting action is the same, lets write it in one
|
||
|
||
if has_len and _has_iterable_dataset(dataloader) and LooseVersion(torch.__version__) >= LooseVersion("1.4.0"): | ||
rank_zero_warn( | ||
'Your `IterableDataset` has `__len__` defined.' | ||
' In combination with multi-processing data loading (e.g. batch size > 1),' | ||
' this can lead to unintended side effects since the samples will be duplicated.' | ||
) | ||
return has_len | ||
|
||
|
||
class TrainerDataLoadingMixin(ABC): | ||
|
@@ -128,12 +144,9 @@ def _worker_check(self, dataloader: DataLoader, name: str) -> None: | |
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) | ||
# don't manipulate iterable datasets | ||
is_iterable_ds = _has_iterable_dataset(dataloader) | ||
|
||
if not is_dataloader or is_iterable_ds: | ||
return dataloader | ||
|
@@ -285,11 +298,7 @@ def _reset_eval_dataloader( | |
# datasets could be none, 1 or 2+ | ||
if len(dataloaders) != 0: | ||
for i, dataloader in enumerate(dataloaders): | ||
try: | ||
num_batches = len(dataloader) | ||
except (TypeError, NotImplementedError): | ||
num_batches = float('inf') | ||
|
||
num_batches = len(dataloader) if _has_len(dataloader) else float('inf') | ||
self._worker_check(dataloader, f'{mode} dataloader {i}') | ||
|
||
# percent or num_steps | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you encapsulate the if statement in () you do not need to use \