-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding DataLoader2 Adapter API and shuffle adapter (#484)
Summary: Pull Request resolved: #484 Test Plan: Imported from OSS Reviewed By: NivekT, Miiira Differential Revision: D36866291 Pulled By: VitalyFedyunin fbshipit-source-id: 1df16c3c45518a069dd87a5565d418ced750ae09
- Loading branch information
1 parent
6f1f81d
commit 5aac88f
Showing
3 changed files
with
104 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import warnings | ||
|
||
from unittest import TestCase | ||
|
||
from torchdata.dataloader2 import DataLoader2, MultiProcessingReadingService, ReadingServiceInterface | ||
from torchdata.dataloader2.adapter import Shuffle | ||
from torchdata.datapipes.iter import IterableWrapper, IterDataPipe | ||
|
||
|
||
class AdapterTest(TestCase): | ||
def test_shuffle(self) -> None: | ||
size = 500 | ||
dp = IterableWrapper(range(size)) | ||
|
||
dl = DataLoader2(datapipe=dp) | ||
self.assertEqual(list(range(size)), list(dl)) | ||
|
||
with warnings.catch_warnings(record=True) as wa: | ||
dl = DataLoader2(datapipe=dp, datapipe_adapter_fn=Shuffle(True)) | ||
self.assertNotEqual(list(range(size)), list(dl)) | ||
self.assertEqual(1, len(wa)) | ||
|
||
dp = IterableWrapper(range(size)).shuffle() | ||
|
||
dl = DataLoader2(datapipe=dp) | ||
self.assertNotEqual(list(range(size)), list(dl)) | ||
|
||
dl = DataLoader2(dp, Shuffle(True)) | ||
self.assertNotEqual(list(range(size)), list(dl)) | ||
|
||
dl = DataLoader2(dp, [Shuffle(None)]) | ||
self.assertNotEqual(list(range(size)), list(dl)) | ||
|
||
dl = DataLoader2(dp, [Shuffle(False)]) | ||
self.assertEqual(list(range(size)), list(dl)) |
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,47 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
from abc import abstractmethod | ||
|
||
import torch | ||
|
||
from torchdata.datapipes.iter import IterDataPipe | ||
|
||
__all__ = [ | ||
"Adapter", | ||
"Shuffle", | ||
] | ||
|
||
assert __all__ == sorted(__all__) | ||
|
||
|
||
class Adapter: | ||
@abstractmethod | ||
def __call__(self, datapipe: IterDataPipe) -> IterDataPipe: | ||
pass | ||
|
||
|
||
class Shuffle(Adapter): | ||
r""" | ||
Shuffle DataPipes adapter allows control over all existing Shuffler (``shuffle``) DataPipes in the graph. | ||
Args: | ||
enable: Optional[Boolean] = True | ||
Shuffle(enable = True) - enables all previously disabled Shuffler DataPipes. If none exists, it will add a new `shuffle` at the end of the graph. | ||
Shuffle(enable = False) - disables all Shuffler DataPipes in the graph. | ||
Shuffle(enable = None) - Is noop. Introduced for backward compatibility. | ||
Example: | ||
>>> dp = IterableWrapper(range(size)).shuffle() | ||
>>> dl = DataLoader2(dp, [Shuffle(False)]) | ||
>>> self.assertEqual(list(range(size)), list(dl)) | ||
""" | ||
|
||
def __init__(self, enable=True): | ||
self.enable = enable | ||
|
||
def __call__(self, datapipe: IterDataPipe) -> IterDataPipe: | ||
return torch.utils.data.graph_settings.apply_shuffle_settings(datapipe, shuffle=self.enable) |
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