Skip to content
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

LiteDataLoader code improvements and docs #10625

Merged
merged 8 commits into from
Nov 21, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions pytorch_lightning/lite/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,29 +157,27 @@ def _replace_dataloader_init_method() -> Generator:


class _LiteDataLoader:
def __init__(self, dataloader: Union[Iterable, DataLoader], device: Optional[torch.device] = None) -> None:
"""The LiteDataLoader is an extension of an Iterator. It would move the data to the device automatically if
the device is specified.
def __init__(self, dataloader: DataLoader, device: Optional[torch.device] = None) -> None:
awaelchli marked this conversation as resolved.
Show resolved Hide resolved
"""The LiteDataLoader is a wrapper for the :class:`~torch.utils.data.DataLoader`. It moves the data to the
device automatically if the device is specified.

Args:
dataloader: The current dataloader to be used.
dataloader: The dataloader to wrap
device: The device to which the data should be moved. By default the device is `None` and no data
transfers will be made (identical behavior as :class:`~torch.utils.data.DataLoader`).
"""
super().__init__()
awaelchli marked this conversation as resolved.
Show resolved Hide resolved
awaelchli marked this conversation as resolved.
Show resolved Hide resolved
self.__dict__.update(getattr(dataloader, "__dict__", {}))
self.__dict__.update(dataloader.__dict__)
awaelchli marked this conversation as resolved.
Show resolved Hide resolved
self._dataloader = dataloader
self._device = device

def __len__(self) -> Union[int, float]:
if isinstance(self._dataloader, Sized):
return len(self._dataloader)
return float("inf")
awaelchli marked this conversation as resolved.
Show resolved Hide resolved

@property
def device(self) -> Optional[torch.device]:
return self._device

def __len__(self) -> int:
return len(self._dataloader)
kaushikb11 marked this conversation as resolved.
Show resolved Hide resolved

def __iter__(self) -> Union[Iterator[Any], Generator[Any, None, None]]:
iterator = iter(self._dataloader)
if self._device is None:
Expand Down