-
Notifications
You must be signed in to change notification settings - Fork 647
fix(jax): write the checkpoint pointer beside save_ckpt #5726
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
wanghan-iapcm
merged 3 commits into
deepmodeling:master
from
wanghan-iapcm:fix-jax-ckpt-pointer
Jul 10, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
| """Test the JAX trainer writes its checkpoint pointer beside save_ckpt. | ||
|
|
||
| JAX training writes checkpoint directories and the stable ``.jax`` link relative | ||
| to ``save_ckpt`` (which may include a directory), but used to always write the | ||
| ``checkpoint`` pointer file to the current working directory with a value that | ||
| still carried the directory prefix. The freeze entrypoint expects the pointer | ||
| inside the folder it is given and resolves its value relative to that folder, so | ||
| a directory-valued ``save_ckpt`` broke freeze/restart tooling. | ||
| """ | ||
|
|
||
| import os | ||
| import tempfile | ||
| import unittest | ||
| from pathlib import ( | ||
| Path, | ||
| ) | ||
| from unittest.mock import ( | ||
| patch, | ||
| ) | ||
|
|
||
| from deepmd.jax.train.trainer import ( | ||
| DPTrainer, | ||
| ) | ||
|
|
||
|
|
||
| class TestCheckpointPointer(unittest.TestCase): | ||
| def setUp(self) -> None: | ||
| self.tmpdir = tempfile.TemporaryDirectory() | ||
| self.cwd = os.getcwd() | ||
| os.chdir(self.tmpdir.name) | ||
|
|
||
| def tearDown(self) -> None: | ||
| os.chdir(self.cwd) | ||
| self.tmpdir.cleanup() | ||
|
|
||
| def _save(self, save_ckpt: str) -> None: | ||
| trainer = DPTrainer.__new__(DPTrainer) | ||
| trainer.save_ckpt = save_ckpt | ||
| with ( | ||
| patch.object(DPTrainer, "_write_checkpoint"), | ||
| patch.object(DPTrainer, "_cleanup_old_checkpoints"), | ||
| patch("deepmd.jax.train.trainer._link_checkpoint"), | ||
| ): | ||
| trainer._save_checkpoint(1) | ||
|
|
||
| def test_pointer_beside_ckpt_for_subdir(self) -> None: | ||
| # save_ckpt with a directory: pointer must land in that directory with a | ||
| # value relative to it (basename only), so freeze(subdir) resolves it. | ||
| self._save("subdir/model.ckpt") | ||
| pointer = Path("subdir") / "checkpoint" | ||
| self.assertTrue(pointer.is_file()) | ||
| self.assertEqual(pointer.read_text(), "model.ckpt.jax") | ||
| self.assertFalse(Path("checkpoint").exists()) | ||
|
|
||
| def test_pointer_in_cwd_for_bare_name(self) -> None: | ||
| # default/bare save_ckpt: pointer stays in the CWD (parent is "."). | ||
| self._save("model.ckpt") | ||
| pointer = Path("checkpoint") | ||
| self.assertTrue(pointer.is_file()) | ||
| self.assertEqual(pointer.read_text(), "model.ckpt.jax") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.