|
| 1 | +# Copyright 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 autocheckpoint feature.""" |
| 16 | + |
| 17 | +from unittest import mock |
| 18 | +from absl.testing import absltest |
| 19 | +from maxtext.common import checkpointing |
| 20 | +from maxtext.utils import exceptions |
| 21 | +import orbax.checkpoint as ocp |
| 22 | +from orbax.checkpoint._src.checkpoint_managers import save_decision_policy as save_decision_policy_lib |
| 23 | + |
| 24 | + |
| 25 | +class AutocheckpointTest(absltest.TestCase): |
| 26 | + |
| 27 | + @mock.patch("maxtext.src.maxtext.common.checkpointing.PyTreeCheckpointHandler") |
| 28 | + @mock.patch("maxtext.src.maxtext.common.checkpointing.CheckpointManager") |
| 29 | + @mock.patch("maxtext.src.maxtext.utils.gcs_utils.mkdir_and_check_permissions") |
| 30 | + def test_create_checkpoint_manager_with_autocheckpoint(self, mock_mkdir, mock_manager_cls, mock_handler_cls): |
| 31 | + mock_mkdir.return_value = "/tmp/checkpoint" |
| 32 | + |
| 33 | + manager = checkpointing.create_orbax_checkpoint_manager( |
| 34 | + checkpoint_dir="/tmp/checkpoint", |
| 35 | + enable_checkpointing=True, |
| 36 | + use_async=False, |
| 37 | + save_interval_steps=100, |
| 38 | + enable_autocheckpoint=True, |
| 39 | + ) |
| 40 | + |
| 41 | + self.assertIsNotNone(manager) |
| 42 | + mock_manager_cls.assert_called_once() |
| 43 | + _, kwargs = mock_manager_cls.call_args |
| 44 | + options = kwargs.get("options") |
| 45 | + self.assertIsInstance(options.save_decision_policy, save_decision_policy_lib.AnySavePolicy) |
| 46 | + # AnySavePolicy internally has policies |
| 47 | + policies = options.save_decision_policy.policies |
| 48 | + self.assertTrue(any(isinstance(p, save_decision_policy_lib.PreemptionCheckpointingPolicy) for p in policies)) |
| 49 | + self.assertTrue(any(isinstance(p, save_decision_policy_lib.FixedIntervalPolicy) for p in policies)) |
| 50 | + |
| 51 | + @mock.patch("jax.block_until_ready") |
| 52 | + @mock.patch("maxtext.src.maxtext.common.checkpointing.ocp.args.PyTreeSave") |
| 53 | + def test_save_checkpoint_triggers_on_preemption(self, mock_save_args, mock_block): |
| 54 | + mock_manager = mock.MagicMock(spec=ocp.CheckpointManager) |
| 55 | + mock_manager.reached_preemption.return_value = True |
| 56 | + |
| 57 | + config = mock.MagicMock() |
| 58 | + config.enable_checkpointing = True |
| 59 | + config.checkpoint_period = 1000 |
| 60 | + config.enable_autocheckpoint = True |
| 61 | + config.checkpoint_storage_target_data_file_size_bytes = 1024 |
| 62 | + |
| 63 | + state = mock.MagicMock() |
| 64 | + |
| 65 | + # Step 5 is not a multiple of checkpoint_period (1000), but reached_preemption is True |
| 66 | + checkpointing.save_checkpoint(mock_manager, 5, state, config=config) |
| 67 | + |
| 68 | + mock_manager.save.assert_called_once() |
| 69 | + mock_manager.reached_preemption.assert_called_with(5) |
| 70 | + |
| 71 | + def test_maybe_save_checkpoint_handles_preemption(self): |
| 72 | + mock_manager = mock.MagicMock(spec=ocp.CheckpointManager) |
| 73 | + mock_manager.reached_preemption.return_value = True |
| 74 | + |
| 75 | + config = mock.MagicMock() |
| 76 | + config.checkpoint_period = 1000 |
| 77 | + config.enable_autocheckpoint = True |
| 78 | + |
| 79 | + state = mock.MagicMock() |
| 80 | + state.step = 6 |
| 81 | + |
| 82 | + with self.assertRaisesRegex(exceptions.StopTraining, "Job is preempted."): |
| 83 | + # step=None means it will use state.step - 1 = 5 |
| 84 | + checkpointing.maybe_save_checkpoint(mock_manager, state, config, data_iterator=None, step=None) |
| 85 | + |
| 86 | + mock_manager.wait_until_finished.assert_called_once() |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == "__main__": |
| 90 | + absltest.main() |
0 commit comments