Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP][DataLoader] Prototype of SamplerIterableDataset
ghstack-source-id: e2099d04d07f6da90f1a7a5da039a24f12f4d56a Pull Request resolved: #49363
- Loading branch information
Showing
4 changed files
with
54 additions
and
4 deletions.
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
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,4 +1,5 @@ | ||
from .batchdataset import BatchIterableDataset | ||
from .collatedataset import CollateIterableDataset | ||
from .samplerdataset import SamplerIterableDataset | ||
|
||
__all__ = ['BatchIterableDataset', 'CollateIterableDataset'] | ||
__all__ = ['BatchIterableDataset', 'CollateIterableDataset', 'SamplerIterableDataset'] |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from torch.utils.data import IterableDataset, Sampler, SequentialSampler | ||
from typing import TypeVar, Iterator, Sized | ||
|
||
T_co = TypeVar('T_co', covariant=True) | ||
|
||
|
||
class SamplerIterableDataset(IterableDataset[T_co]): | ||
r""" Prototype of :class:`SamplerIterableDataset`. | ||
IterableDataset to generate samples elements. | ||
args: | ||
dataset: IterableDataset being collated | ||
sampler: Sampler to genereate sample elements from input dataset. | ||
Default is :class:`SequentialSampler` for IterableDataset | ||
""" | ||
def __init__(self, | ||
dataset: IterableDataset[T_co], | ||
*, | ||
sampler: Sampler = SequentialSampler, | ||
) -> None: | ||
self.dataset = dataset | ||
self.sampler = sampler(self.dataset) | ||
|
||
def __iter__(self) -> Iterator[T_co]: | ||
return iter(self.sampler) | ||
|
||
def __len__(self) -> int: | ||
if isinstance(self.dataset, Sized) and \ | ||
isinstance(self.sampler, Sized) and \ | ||
len(self.sampler) >= 0: | ||
return len(self.sampler) | ||
else: | ||
raise NotImplementedError |