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

Fix: Resolve the default weights of the combined dataset #188

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 7 additions & 5 deletions src/litdata/streaming/combined.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ def __init__(
self._weights = weights
self._iterate_over_all = iterate_over_all

num_datasets = len(datasets)

if iterate_over_all and weights:
raise ValueError(
"When `iterate_over_all` is set to True, the weights argument shouldn't be provided.",
Expand All @@ -70,10 +68,14 @@ def __init__(
self._iterate_over_all = iterate_over_all

if weights is None:
# Inversely weighted based on length
self._weights = [1 / float(num_datasets)] * num_datasets
# Weighted based on the dataset length
dataset_lens = [len(d) for d in datasets]
total_len = sum(dataset_lens)
assert total_len > 0
self._weights = [len / total_len for len in dataset_lens]
else:
self._weights = [w / sum(weights) for w in weights]
weights_sum = sum(weights)
self._weights = [w / weights_sum for w in weights]

self._iterator: Optional[_CombinedDatasetIterator] = None
self._use_streaming_dataloader = False
Expand Down
7 changes: 6 additions & 1 deletion tests/streaming/test_combined.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ def set_epoch(self, epoch):
def __iter__(self):
yield from self.values

def __len__(self):
return len(self.values)


def test_combined_dataset_iterate_over_all_4_datasets():
dataset = TestCombinedStreamingDataset(
Expand All @@ -82,6 +85,8 @@ def test_combined_dataset_num_samples_yield_iterate_over_all():
def test_drop_last_and_shuffle():
dataset_mock_1 = MagicMock()
dataset_mock_2 = MagicMock()
dataset_mock_1.__len__.return_value = 1
dataset_mock_2.__len__.return_value = 1

dataset = TestCombinedStreamingDataset([dataset_mock_1, dataset_mock_2], 42, iterate_over_all=True)
StreamingDataLoader(dataset, shuffle=True, drop_last=True)
Expand Down Expand Up @@ -193,7 +198,7 @@ def test_combined_dataset_state_dict():
([2, 0.5], [0.8, 0.2]),
([1, 1, 1], [1 / 3, 1 / 3, 1 / 3]),
([0.3, 0, 0], [1.0, 0, 0]),
(None, [0.5, 0.5]),
(None, [1 / 3, 2 / 3]),
],
)
def test_combined_dataset_normalizes_weights(weights, expected):
Expand Down
Loading