|
| 1 | +"""Tests for elastic checkpoint.""" |
| 2 | + |
| 3 | +import json |
| 4 | + |
| 5 | +from etils import epath |
| 6 | +from grain._src.core import sharding |
| 7 | +from grain._src.python.checkpoint import elastic_checkpoint |
| 8 | +from grain._src.python.dataset import elastic_iterator |
| 9 | + |
| 10 | +from absl.testing import absltest |
| 11 | + |
| 12 | + |
| 13 | +class MockElasticIterDatasetIterator( |
| 14 | + elastic_iterator.ElasticIterDatasetIterator |
| 15 | +): |
| 16 | + |
| 17 | + def __init__(self, shard_options, total_num_shards, states=None): |
| 18 | + self._shard_options = shard_options |
| 19 | + self._total_num_shards = total_num_shards |
| 20 | + self._states = states if states is not None else {} |
| 21 | + self.updated_states = {} |
| 22 | + |
| 23 | + def get_state(self): |
| 24 | + return { |
| 25 | + "ds_iterator_states": self._states, |
| 26 | + "total_num_shards": self._total_num_shards, |
| 27 | + } |
| 28 | + |
| 29 | + def update_shard_iterator_state(self, shard_index, state): |
| 30 | + self.updated_states[shard_index] = state |
| 31 | + |
| 32 | + |
| 33 | +class ElasticCheckpointTest(absltest.TestCase): |
| 34 | + |
| 35 | + def test_save_and_restore_elastic_iterator(self): |
| 36 | + temp_dir = epath.Path(self.create_tempdir().full_path) |
| 37 | + shard_options = sharding.ShardOptions(shard_index=0, shard_count=1) |
| 38 | + states = { |
| 39 | + 0: {"val": 0}, |
| 40 | + 1: {"val": 1}, |
| 41 | + } |
| 42 | + iterator = MockElasticIterDatasetIterator( |
| 43 | + shard_options=shard_options, total_num_shards=2, states=states |
| 44 | + ) |
| 45 | + elastic_checkpoint.save_elastic_iterator(temp_dir, iterator) |
| 46 | + |
| 47 | + file0 = temp_dir / "shard_state_0-of-2.json" |
| 48 | + self.assertTrue(file0.exists()) |
| 49 | + self.assertEqual( |
| 50 | + file0.read_text(), |
| 51 | + json.dumps({"val": 0, "total_num_shards": 2}, indent=4), |
| 52 | + ) |
| 53 | + file1 = temp_dir / "shard_state_1-of-2.json" |
| 54 | + self.assertTrue(file1.exists()) |
| 55 | + self.assertEqual( |
| 56 | + file1.read_text(), |
| 57 | + json.dumps({"val": 1, "total_num_shards": 2}, indent=4), |
| 58 | + ) |
| 59 | + |
| 60 | + iterator_to_restore = MockElasticIterDatasetIterator( |
| 61 | + shard_options=shard_options, total_num_shards=2 |
| 62 | + ) |
| 63 | + elastic_checkpoint.restore_elastic_iterator(temp_dir, iterator_to_restore) |
| 64 | + self.assertEqual( |
| 65 | + iterator_to_restore.updated_states, |
| 66 | + { |
| 67 | + 0: {"val": 0, "total_num_shards": 2}, |
| 68 | + 1: {"val": 1, "total_num_shards": 2}, |
| 69 | + }, |
| 70 | + ) |
| 71 | + |
| 72 | + def test_restore_elastic_iterator_with_multiple_processes(self): |
| 73 | + temp_dir = epath.Path(self.create_tempdir().full_path) |
| 74 | + # Process 0 |
| 75 | + shard_options_0 = sharding.ShardOptions(shard_index=0, shard_count=2) |
| 76 | + states = { |
| 77 | + 0: {"val": 0}, |
| 78 | + 1: {"val": 1}, |
| 79 | + 2: {"val": 2}, |
| 80 | + } |
| 81 | + iterator_0 = MockElasticIterDatasetIterator( |
| 82 | + shard_options=shard_options_0, total_num_shards=3, states=states |
| 83 | + ) |
| 84 | + # In reality save_elastic_iterator will be called in each process, but |
| 85 | + # get_state() should return all states, so we only need to call it once |
| 86 | + # to create checkpoint files. |
| 87 | + elastic_checkpoint.save_elastic_iterator(temp_dir, iterator_0) |
| 88 | + |
| 89 | + # Check files are written |
| 90 | + self.assertTrue((temp_dir / "shard_state_0-of-3.json").exists()) |
| 91 | + self.assertTrue((temp_dir / "shard_state_1-of-3.json").exists()) |
| 92 | + self.assertTrue((temp_dir / "shard_state_2-of-3.json").exists()) |
| 93 | + |
| 94 | + # Restore for process 0, responsible for shards 0 and 2. |
| 95 | + iterator_to_restore_0 = MockElasticIterDatasetIterator( |
| 96 | + shard_options=shard_options_0, total_num_shards=3 |
| 97 | + ) |
| 98 | + elastic_checkpoint.restore_elastic_iterator(temp_dir, iterator_to_restore_0) |
| 99 | + self.assertEqual( |
| 100 | + iterator_to_restore_0.updated_states, |
| 101 | + { |
| 102 | + 0: {"val": 0, "total_num_shards": 3}, |
| 103 | + 2: {"val": 2, "total_num_shards": 3}, |
| 104 | + }, |
| 105 | + ) |
| 106 | + |
| 107 | + # Restore for process 1, responsible for shard 1. |
| 108 | + shard_options_1 = sharding.ShardOptions(shard_index=1, shard_count=2) |
| 109 | + iterator_to_restore_1 = MockElasticIterDatasetIterator( |
| 110 | + shard_options=shard_options_1, total_num_shards=3 |
| 111 | + ) |
| 112 | + elastic_checkpoint.restore_elastic_iterator(temp_dir, iterator_to_restore_1) |
| 113 | + self.assertEqual( |
| 114 | + iterator_to_restore_1.updated_states, |
| 115 | + { |
| 116 | + 1: {"val": 1, "total_num_shards": 3}, |
| 117 | + }, |
| 118 | + ) |
| 119 | + |
| 120 | + |
| 121 | +if __name__ == "__main__": |
| 122 | + absltest.main() |
0 commit comments