-
Notifications
You must be signed in to change notification settings - Fork 287
feat: add --save-total-limit to auto-cleanup old checkpoints #590
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
Open
jxiaof
wants to merge
4
commits into
sgl-project:main
Choose a base branch
from
jxiaof:feat/save-total-limit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 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
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
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,151 @@ | ||
| """Tests for checkpoint cleanup utility.""" | ||
|
|
||
| import os | ||
| import tempfile | ||
| import unittest | ||
| from unittest import mock | ||
|
|
||
| import specforge.utils as utils | ||
| from specforge.utils import cleanup_checkpoints | ||
|
|
||
|
|
||
| def create_checkpoint_dir(output_dir, epoch, step): | ||
| """Create a mock checkpoint directory.""" | ||
| dirname = f"epoch_{epoch}_step_{step}" | ||
| path = os.path.join(output_dir, dirname) | ||
| os.makedirs(path, exist_ok=True) | ||
| # Add a dummy file so the dir is non-empty | ||
| with open(os.path.join(path, "config.json"), "w") as f: | ||
| f.write("{}") | ||
| return path | ||
|
|
||
|
|
||
| class TestCleanupCheckpoints(unittest.TestCase): | ||
| def setUp(self): | ||
| self.tmpdir = tempfile.mkdtemp() | ||
|
|
||
| def tearDown(self): | ||
| import shutil | ||
|
|
||
| shutil.rmtree(self.tmpdir, ignore_errors=True) | ||
|
|
||
| def test_no_cleanup_when_limit_none(self): | ||
| """No checkpoints removed when save_total_limit is None.""" | ||
| for i in range(5): | ||
| create_checkpoint_dir(self.tmpdir, 0, (i + 1) * 1000) | ||
| removed = cleanup_checkpoints(self.tmpdir, None) | ||
| self.assertEqual(removed, 0) | ||
| self.assertEqual(len(os.listdir(self.tmpdir)), 5) | ||
|
|
||
| def test_no_cleanup_when_limit_zero(self): | ||
| """No checkpoints removed when save_total_limit is 0.""" | ||
| for i in range(5): | ||
| create_checkpoint_dir(self.tmpdir, 0, (i + 1) * 1000) | ||
| removed = cleanup_checkpoints(self.tmpdir, 0) | ||
| self.assertEqual(removed, 0) | ||
| self.assertEqual(len(os.listdir(self.tmpdir)), 5) | ||
|
|
||
| def test_no_cleanup_when_limit_negative(self): | ||
| """No checkpoints removed when save_total_limit is negative.""" | ||
| for i in range(5): | ||
| create_checkpoint_dir(self.tmpdir, 0, (i + 1) * 1000) | ||
| removed = cleanup_checkpoints(self.tmpdir, -1) | ||
| self.assertEqual(removed, 0) | ||
|
|
||
| def test_removes_oldest_checkpoints(self): | ||
| """Older checkpoints removed when limit exceeded.""" | ||
| for i in range(5): | ||
| create_checkpoint_dir(self.tmpdir, 0, (i + 1) * 1000) | ||
| removed = cleanup_checkpoints(self.tmpdir, 2) | ||
| self.assertEqual(removed, 3) | ||
| remaining = sorted(os.listdir(self.tmpdir)) | ||
| self.assertEqual(remaining, ["epoch_0_step_4000", "epoch_0_step_5000"]) | ||
|
|
||
| def test_no_removal_when_under_limit(self): | ||
| """No checkpoints removed when count <= limit.""" | ||
| for i in range(3): | ||
| create_checkpoint_dir(self.tmpdir, 0, (i + 1) * 1000) | ||
| removed = cleanup_checkpoints(self.tmpdir, 5) | ||
| self.assertEqual(removed, 0) | ||
| self.assertEqual(len(os.listdir(self.tmpdir)), 3) | ||
|
|
||
| def test_removes_oldest_across_epochs(self): | ||
| """Correct removal ordering across multiple epochs.""" | ||
| steps = [ | ||
| (0, 1000), | ||
| (0, 2000), | ||
| (1, 3000), | ||
| (1, 4000), | ||
| (2, 5000), | ||
| ] | ||
| for epoch, step in steps: | ||
| create_checkpoint_dir(self.tmpdir, epoch, step) | ||
| removed = cleanup_checkpoints(self.tmpdir, 2) | ||
| self.assertEqual(removed, 3) | ||
| remaining = sorted(os.listdir(self.tmpdir)) | ||
| self.assertEqual(remaining, ["epoch_1_step_4000", "epoch_2_step_5000"]) | ||
|
|
||
| def test_nonexistent_dir(self): | ||
| """No error when output_dir does not exist.""" | ||
| removed = cleanup_checkpoints("/nonexistent/path", 3) | ||
| self.assertEqual(removed, 0) | ||
|
|
||
| def test_empty_dir(self): | ||
| """No error when output_dir is empty.""" | ||
| removed = cleanup_checkpoints(self.tmpdir, 3) | ||
| self.assertEqual(removed, 0) | ||
|
|
||
| def test_ignores_non_checkpoint_dirs(self): | ||
| """Non-checkpoint directories are not affected.""" | ||
| create_checkpoint_dir(self.tmpdir, 0, 1000) | ||
| create_checkpoint_dir(self.tmpdir, 0, 2000) | ||
| create_checkpoint_dir(self.tmpdir, 0, 3000) | ||
| # Create non-checkpoint dirs/files | ||
| os.makedirs(os.path.join(self.tmpdir, "some_other_dir"), exist_ok=True) | ||
| with open(os.path.join(self.tmpdir, "random_file.txt"), "w") as f: | ||
| f.write("test") | ||
| removed = cleanup_checkpoints(self.tmpdir, 1) | ||
| self.assertEqual(removed, 2) | ||
| remaining = sorted(os.listdir(self.tmpdir)) | ||
| self.assertIn("epoch_0_step_3000", remaining) | ||
| self.assertIn("some_other_dir", remaining) | ||
| self.assertIn("random_file.txt", remaining) | ||
|
|
||
| def test_nonzero_rank_skips_cleanup(self): | ||
| """Non-zero ranks should not delete checkpoints in distributed runs.""" | ||
| for i in range(5): | ||
| create_checkpoint_dir(self.tmpdir, 0, (i + 1) * 1000) | ||
|
|
||
| with mock.patch.object(utils, "dist") as mock_dist: | ||
| mock_dist.is_available.return_value = True | ||
| mock_dist.is_initialized.return_value = True | ||
| mock_dist.get_rank.return_value = 1 | ||
|
|
||
| removed = cleanup_checkpoints(self.tmpdir, 2) | ||
|
|
||
| self.assertEqual(removed, 0) | ||
| self.assertEqual(len(os.listdir(self.tmpdir)), 5) | ||
|
|
||
| def test_missing_checkpoint_during_delete_is_ignored(self): | ||
| """Cleanup should continue if a checkpoint disappears before deletion.""" | ||
| for i in range(5): | ||
| create_checkpoint_dir(self.tmpdir, 0, (i + 1) * 1000) | ||
|
|
||
| real_rmtree = utils.shutil.rmtree | ||
|
|
||
| def flaky_rmtree(path): | ||
| if path.endswith("epoch_0_step_1000"): | ||
| real_rmtree(path) | ||
| raise FileNotFoundError(path) | ||
| return real_rmtree(path) | ||
|
|
||
| with mock.patch.object(utils.shutil, "rmtree", side_effect=flaky_rmtree): | ||
| removed = cleanup_checkpoints(self.tmpdir, 2) | ||
|
|
||
| self.assertEqual(removed, 2) | ||
| remaining = sorted(os.listdir(self.tmpdir)) | ||
| self.assertEqual(remaining, ["epoch_0_step_4000", "epoch_0_step_5000"]) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In distributed training settings with a shared filesystem (e.g., NFS, Lustre), having all ranks attempt to delete the same checkpoint directories simultaneously will cause race conditions, leading to
FileNotFoundErrororOSErroron non-zero ranks and crashing the training run.Although the docstring mentions that only rank 0 performs the deletion, the implementation does not actually enforce this check. Adding a check for
dist.is_initialized() and dist.get_rank() != 0ensures that only rank 0 executes the cleanup, while other ranks return early and wait at the subsequentdist.barrier()in the training scripts.