-
Notifications
You must be signed in to change notification settings - Fork 5
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
Fix combined dataloader bug #163
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5d09d14
Fix combined dataloader bug
frostedoyster 9feb92c
Add docstring
frostedoyster 02b145a
Add comment for __len__
frostedoyster c213998
Merge branch 'main' into dataloader-bug
frostedoyster baaf90a
Merge branch 'main' into dataloader-bug
frostedoyster 5c9bf26
Debug regression test
frostedoyster 626094c
Merge branch 'main' into dataloader-bug
frostedoyster d61d2d4
Fix regression tests
frostedoyster 4be826e
Suggestions from code review
frostedoyster 2c0340e
Show __len__ in docs
frostedoyster 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
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
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,57 +1,60 @@ | ||
import itertools | ||
from typing import List | ||
|
||
import numpy as np | ||
import torch | ||
|
||
|
||
class CombinedIterableDataset(torch.utils.data.IterableDataset): | ||
class CombinedDataLoader: | ||
""" | ||
Combines multiple dataloaders into a single iterable dataset. | ||
This is useful for combining multiple dataloaders into a single | ||
dataloader. The new dataloader can be shuffled or not. | ||
Combines multiple dataloaders into a single dataloader. | ||
|
||
This is useful for learning from multiple datasets at the same time, | ||
each of which may have different batch sizes, properties, etc. | ||
|
||
:param dataloaders: list of dataloaders to combine | ||
:param shuffle: whether to shuffle the combined dataloader | ||
:param shuffle: whether to shuffle the combined dataloader (this does not | ||
act on the individual batches, but it shuffles the order in which | ||
they are returned) | ||
|
||
:return: combined dataloader | ||
:return: the combined dataloader | ||
""" | ||
|
||
def __init__(self, dataloaders, shuffle): | ||
def __init__(self, dataloaders: List[torch.utils.data.DataLoader], shuffle: bool): | ||
self.dataloaders = dataloaders | ||
self.shuffle = shuffle | ||
|
||
# Create the indices: | ||
indices = [ | ||
(i, dl_idx) | ||
for dl_idx, dl in enumerate(self.dataloaders) | ||
for i in range(len(dl)) | ||
] | ||
self.indices = list(range(len(self))) | ||
|
||
# Shuffle the indices if requested | ||
if self.shuffle: | ||
np.random.shuffle(indices) | ||
np.random.shuffle(self.indices) | ||
|
||
self.reset() | ||
|
||
self.indices = indices | ||
def reset(self): | ||
self.current_index = 0 | ||
self.full_list = [batch for dl in self.dataloaders for batch in dl] | ||
|
||
def __iter__(self): | ||
for idx, dataloader_idx in self.indices: | ||
yield next(itertools.islice(self.dataloaders[dataloader_idx], idx, None)) | ||
return self | ||
|
||
def __len__(self): | ||
return len(self.indices) | ||
def __next__(self): | ||
if self.current_index >= len(self.indices): | ||
self.reset() # Reset the index for the next iteration | ||
raise StopIteration | ||
|
||
idx = self.indices[self.current_index] | ||
self.current_index += 1 | ||
return self.full_list[idx] | ||
|
||
def combine_dataloaders( | ||
dataloaders: List[torch.utils.data.DataLoader], shuffle: bool = True | ||
): | ||
""" | ||
Combines multiple dataloaders into a single dataloader. | ||
def __len__(self): | ||
"""Returns the total number of batches in all dataloaders. | ||
|
||
:param dataloaders: list of dataloaders to combine | ||
:param shuffle: whether to shuffle the combined dataloader | ||
This returns the total number of batches in all dataloaders | ||
(as opposed to the total number of samples or the number of | ||
individual dataloaders). | ||
|
||
:return: combined dataloader | ||
""" | ||
combined_dataset = CombinedIterableDataset(dataloaders, shuffle) | ||
return torch.utils.data.DataLoader(combined_dataset, batch_size=None) | ||
:return: the total number of batches in all dataloaders | ||
""" | ||
return sum(len(dl) for dl in self.dataloaders) |
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.
Is this working? Shouldn't you return the next dataloader here instead of the full instance?
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.
It does work. It comes from ChatGPT, but intuitively it makes sense:
By iterating, you effectively call
iterable = iter(dataloader)
and thennext(iterable)
(a bunch of times). Next is defined in the class, so it makes sense to return selfThere 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.
Okay then please add a test for iterator.
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.
It's there already from the old function + class