|
| 1 | +# Copyright 2026 The Orbax Authors. |
| 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 | +# http://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 | +from unittest import mock |
| 16 | + |
| 17 | +from absl.testing import absltest |
| 18 | +from absl.testing import flagsaver |
| 19 | +from etils import epath |
| 20 | +import jax |
| 21 | +from orbax.checkpoint.experimental.v1._src.layout import orbax_layout |
| 22 | +from orbax.checkpoint.experimental.v1._src.partial import merging |
| 23 | +from orbax.checkpoint.experimental.v1._src.partial import run_merging |
| 24 | + |
| 25 | + |
| 26 | +class RunMergingTest(absltest.TestCase): |
| 27 | + |
| 28 | + def setUp(self): |
| 29 | + super().setUp() |
| 30 | + self.out_path = self.create_tempdir().full_path |
| 31 | + self.in_paths = [self.create_tempdir().full_path] |
| 32 | + |
| 33 | + @mock.patch.object( |
| 34 | + orbax_layout.OrbaxLayout, 'validate', new_callable=mock.AsyncMock |
| 35 | + ) |
| 36 | + @mock.patch.object(merging, 'merge_checkpoints', autospec=True) |
| 37 | + @mock.patch.object(jax, 'process_index', return_value=0) |
| 38 | + def test_main_success(self, _, mock_merge, mock_validate): |
| 39 | + with flagsaver.flagsaver( |
| 40 | + in_paths=self.in_paths, |
| 41 | + out_path=self.out_path, |
| 42 | + per_host_memory_limit_bytes=1024, |
| 43 | + ): |
| 44 | + run_merging.main([]) |
| 45 | + |
| 46 | + mock_validate.assert_called() |
| 47 | + mock_merge.assert_called_once() |
| 48 | + |
| 49 | + @mock.patch.object( |
| 50 | + orbax_layout.OrbaxLayout, 'validate', new_callable=mock.AsyncMock |
| 51 | + ) |
| 52 | + @mock.patch.object(jax, 'process_index', return_value=0) |
| 53 | + def test_main_invalid_output_not_empty(self, _): |
| 54 | + out_path = epath.Path(self.out_path) |
| 55 | + (out_path / 'some_file').write_text('content') |
| 56 | + |
| 57 | + with flagsaver.flagsaver( |
| 58 | + in_paths=self.in_paths, |
| 59 | + out_path=self.out_path, |
| 60 | + per_host_memory_limit_bytes=1024, |
| 61 | + ): |
| 62 | + with self.assertRaisesRegex(ValueError, 'not empty'): |
| 63 | + run_merging.main([]) |
| 64 | + |
| 65 | + @mock.patch.object( |
| 66 | + orbax_layout.OrbaxLayout, 'validate', new_callable=mock.AsyncMock |
| 67 | + ) |
| 68 | + @mock.patch.object(jax, 'process_index', return_value=0) |
| 69 | + def test_main_invalid_input(self, _, mock_validate): |
| 70 | + mock_validate.side_effect = ValueError('Invalid checkpoint') |
| 71 | + |
| 72 | + with flagsaver.flagsaver( |
| 73 | + in_paths=self.in_paths, |
| 74 | + out_path=self.out_path, |
| 75 | + per_host_memory_limit_bytes=1024, |
| 76 | + ): |
| 77 | + with self.assertRaisesRegex(ValueError, 'is not a valid checkpoint'): |
| 78 | + run_merging.main([]) |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == '__main__': |
| 82 | + absltest.main() |
0 commit comments