Skip to content

Commit 3f198a9

Browse files
Merge pull request #4335 from AI-Hypercomputer:aireen/row_sharding_fix
PiperOrigin-RevId: 943457686
2 parents 980af47 + 06a8efc commit 3f198a9

2 files changed

Lines changed: 28 additions & 5 deletions

File tree

src/maxtext/input_pipeline/grain_data_processing.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,11 @@ def create_dataset_from_pattern(pattern):
191191
f"Each shard will be read by at most {math.ceil(dataloading_host_count / len(data_files))} hosts. "
192192
f"Concurrent reading by multiple hosts may cause slow down."
193193
)
194-
min_files_per_host = len(data_files) // dataloading_host_count
195-
if grain_worker_count > min_files_per_host:
194+
if grain_worker_count > files_per_host:
196195
raise ValueError(
197-
f"grain_worker_count ({grain_worker_count}) exceeds the minimum number of {data_file_type} files "
198-
f"per host ({min_files_per_host} = {len(data_files)} files / {dataloading_host_count} hosts). "
199-
f"Lower grain_worker_count to at most {min_files_per_host}."
196+
f"grain_worker_count ({grain_worker_count}) exceeds the number of {data_file_type} files "
197+
f"per host ({files_per_host}). "
198+
f"Lower grain_worker_count to at most {files_per_host}."
200199
)
201200
dataset = grain.MapDataset.source(data_files)
202201
if shuffle:

tests/unit/grain_data_processing_test.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,5 +552,29 @@ def setUp(self):
552552
)
553553

554554

555+
@pytest.mark.cpu_only
556+
class GrainFewerFilesThanHostsTest(_GrainTFRecordSetup, GrainBaseProcessingTest, unittest.TestCase):
557+
"""Tests data loading when file count < dataloading_host_count.
558+
559+
_GrainTFRecordSetup provides a single TFRecord file. Overriding process_indices
560+
to [0, 1] makes make_grain_train_iterator use dataloading_host_count=2, simulating
561+
the undersized scenario without a real multi-host runner. The inherited test_train_ds
562+
then validates the full batch shape through this code path.
563+
"""
564+
565+
def setUp(self):
566+
super().setUp()
567+
# Simulate 2 dataloading hosts with only 1 file to trigger the
568+
# fewer-files-than-hosts path in get_datasets via make_grain_train_iterator.
569+
self.process_indices = [0, 1]
570+
571+
def test_raises_when_grain_worker_count_exceeds_files_per_host(self):
572+
# 1 file, 2 hosts (via process_indices=[0,1]) → files_per_host=1;
573+
# grain_worker_count=2 must raise.
574+
config = self._make_config(grain_worker_count=2)
575+
with self.assertRaises(ValueError):
576+
grain_data_processing.make_grain_train_iterator(config, self.mesh, self.process_indices)
577+
578+
555579
if __name__ == "__main__":
556580
unittest.main()

0 commit comments

Comments
 (0)