Skip to content

Commit 5726d17

Browse files
fix(dataset): skip datasets allotted no samples under max_sample_num (#1095)
* fix(dataset): honor a 0-sample share under max_sample_num * fix(dataset): skip datasets allotted no samples under max_sample_num
1 parent 5593687 commit 5726d17

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

‎data_juicer/core/data/dataset_builder.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ def load_dataset(self, **kwargs) -> DJDataset:
173173
_datasets = []
174174
# load datasets with sample numbers
175175
for stra, weight, sample_num in zip(self.load_strategies, self.weights, self.sample_numbers):
176+
# random_sample treats 0 as "unset" and falls back to the raw weight,
177+
# so a dataset allotted no samples must be skipped, not sampled
178+
if self.max_sample_num and sample_num <= 0:
179+
continue
180+
176181
# load dataset with its load strategy
177182
dataset = stra.load_data(**load_kwargs)
178183

‎tests/core/data/test_dataset_builder.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,26 @@ def test_invalid_max_sample_num(self):
520520
self.assertIn('should be a positive integer',
521521
str(context.exception))
522522

523+
def test_max_sample_num_zero_share(self):
524+
"""Test max_sample_num with datasets allotted 0 samples"""
525+
cfg = Namespace()
526+
cfg.dataset = {
527+
'configs': [
528+
{
529+
'type': 'local',
530+
'path': 'test_data/sample.jsonl',
531+
'weight': 1.0
532+
} for _ in range(4)
533+
],
534+
'max_sample_num': 2
535+
}
536+
537+
builder = DatasetBuilder(cfg)
538+
self.assertEqual(builder.sample_numbers, [1, 1, 0, 0])
539+
540+
ds = builder.load_dataset()
541+
self.assertEqual(len(ds), 2)
542+
523543
@TEST_TAG('ray')
524544
def test_builder_ray_config(self):
525545
"""Test loading Ray configuration from YAML"""

0 commit comments

Comments
 (0)