|
| 1 | +# Copyright 2023–2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Unit tests for input_pipeline_utils.""" |
| 16 | + |
| 17 | +import pytest |
| 18 | +import unittest |
| 19 | + |
| 20 | +from maxtext.input_pipeline.input_pipeline_utils import compute_file_sharding |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.cpu_only |
| 24 | +class ComputeFileShardingNormalCaseTest(unittest.TestCase): |
| 25 | + """file_count >= host_count: disjoint file subsets, no row sharding.""" |
| 26 | + |
| 27 | + def test_even_split(self): |
| 28 | + # 8 files, 4 hosts → interleaved assignment, 2 files each |
| 29 | + file_slice, files_per_host, _ = compute_file_sharding(8, host_index=0, host_count=4) |
| 30 | + self.assertEqual(list(range(8)[file_slice]), [0, 4]) |
| 31 | + self.assertEqual(files_per_host, 2) |
| 32 | + |
| 33 | + file_slice, files_per_host, _ = compute_file_sharding(8, host_index=1, host_count=4) |
| 34 | + self.assertEqual(list(range(8)[file_slice]), [1, 5]) |
| 35 | + self.assertEqual(files_per_host, 2) |
| 36 | + |
| 37 | + file_slice, files_per_host, _ = compute_file_sharding(8, host_index=2, host_count=4) |
| 38 | + self.assertEqual(list(range(8)[file_slice]), [2, 6]) |
| 39 | + self.assertEqual(files_per_host, 2) |
| 40 | + |
| 41 | + file_slice, files_per_host, _ = compute_file_sharding(8, host_index=3, host_count=4) |
| 42 | + self.assertEqual(list(range(8)[file_slice]), [3, 7]) |
| 43 | + self.assertEqual(files_per_host, 2) |
| 44 | + |
| 45 | + def test_uneven_split(self): |
| 46 | + # 5 files, 4 hosts → host 0 gets an extra file |
| 47 | + file_slice, _, _ = compute_file_sharding(5, host_index=0, host_count=4) |
| 48 | + self.assertEqual(list(range(5)[file_slice]), [0, 4]) |
| 49 | + |
| 50 | + file_slice, _, _ = compute_file_sharding(5, host_index=1, host_count=4) |
| 51 | + self.assertEqual(list(range(5)[file_slice]), [1]) |
| 52 | + |
| 53 | + file_slice, _, _ = compute_file_sharding(5, host_index=2, host_count=4) |
| 54 | + self.assertEqual(list(range(5)[file_slice]), [2]) |
| 55 | + |
| 56 | + file_slice, _, _ = compute_file_sharding(5, host_index=3, host_count=4) |
| 57 | + self.assertEqual(list(range(5)[file_slice]), [3]) |
| 58 | + |
| 59 | + def test_single_host_gets_all_files(self): |
| 60 | + file_slice, files_per_host, _ = compute_file_sharding(8, host_index=0, host_count=1) |
| 61 | + self.assertEqual(list(range(8)[file_slice]), [0, 1, 2, 3, 4, 5, 6, 7]) |
| 62 | + self.assertEqual(files_per_host, 8) |
| 63 | + |
| 64 | + def test_no_row_shard_in_normal_case(self): |
| 65 | + for host_index in range(4): |
| 66 | + _, _, row_shard = compute_file_sharding(8, host_index, host_count=4) |
| 67 | + self.assertIsNone(row_shard) |
| 68 | + |
| 69 | + |
| 70 | +class ComputeFileShardingUndersizedCaseTest(unittest.TestCase): |
| 71 | + """file_count < host_count: multiple hosts share a file, split by row.""" |
| 72 | + |
| 73 | + def test_single_file_four_hosts(self): |
| 74 | + # All 4 hosts read the same file, each gets a quarter of the rows |
| 75 | + _, _, row_shard = compute_file_sharding(1, host_index=0, host_count=4) |
| 76 | + self.assertEqual(row_shard, (0, 4)) # row index 0 of 4 |
| 77 | + |
| 78 | + _, _, row_shard = compute_file_sharding(1, host_index=1, host_count=4) |
| 79 | + self.assertEqual(row_shard, (1, 4)) # row index 1 of 4 |
| 80 | + |
| 81 | + _, _, row_shard = compute_file_sharding(1, host_index=2, host_count=4) |
| 82 | + self.assertEqual(row_shard, (2, 4)) # row index 2 of 4 |
| 83 | + |
| 84 | + _, _, row_shard = compute_file_sharding(1, host_index=3, host_count=4) |
| 85 | + self.assertEqual(row_shard, (3, 4)) # row index 3 of 4 |
| 86 | + |
| 87 | + def test_three_files_eight_hosts(self): |
| 88 | + # 8 hosts round-robin across 3 files: |
| 89 | + # hosts 0,3,6 → file 0 (3 readers); hosts 1,4,7 → file 1 (3 readers); hosts 2,5 → file 2 (2 readers) |
| 90 | + expected = { |
| 91 | + # host_index: (file_indices, row_shard) |
| 92 | + 0: ([0], (0, 3)), |
| 93 | + 1: ([1], (0, 3)), |
| 94 | + 2: ([2], (0, 2)), |
| 95 | + 3: ([0], (1, 3)), |
| 96 | + 4: ([1], (1, 3)), |
| 97 | + 5: ([2], (1, 2)), |
| 98 | + 6: ([0], (2, 3)), |
| 99 | + 7: ([1], (2, 3)), |
| 100 | + } |
| 101 | + for host_index, (exp_files, exp_row_shard) in expected.items(): |
| 102 | + file_slice, _, row_shard = compute_file_sharding(3, host_index, host_count=8) |
| 103 | + self.assertEqual(list(range(3)[file_slice]), exp_files, f"host {host_index} file assignment") |
| 104 | + self.assertEqual(row_shard, exp_row_shard, f"host {host_index} row shard") |
| 105 | + |
| 106 | + def test_no_row_shard_when_only_one_reader(self): |
| 107 | + # 2 files, 3 hosts: file 1 has only one reader (host 1) → no row split needed |
| 108 | + _, _, row_shard = compute_file_sharding(2, host_index=1, host_count=3) |
| 109 | + self.assertIsNone(row_shard) |
| 110 | + |
| 111 | + |
| 112 | +if __name__ == "__main__": |
| 113 | + unittest.main() |
0 commit comments