-
Notifications
You must be signed in to change notification settings - Fork 60
Add checkpoint manager to save latest or top k checkpoints in history #279
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
Merged
Changes from all commits
Commits
Show all changes
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
"""checkpoint manager """ | ||
import os | ||
import stat | ||
|
||
import numpy as np | ||
|
||
import mindspore as ms | ||
from mindspore import log as logger | ||
|
||
|
||
class CheckpointManager: | ||
""" | ||
Manage checkpoint files according to ckpt_save_policy of checkpoint. | ||
Args: | ||
ckpt_save_dir (str): directory to save the checkpoints | ||
ckpt_save_policy (str): Checkpoint saving strategy. Option: None, "top_k", or "latest_k". | ||
None means to save each checkpoint, top_k means to save K checkpoints with the best performance, | ||
and latest_k means saving the latest K checkpoint. Default: top_k. | ||
k (int): top k value | ||
prefer_low_perf (bool): standard for selecting the top k performance. If False, pick top k checkpoints with highest performance e.g. accuracy. If True, pick top k checkpoints with the lowest performance, e.g. loss. | ||
|
||
""" | ||
|
||
def __init__(self, ckpt_save_dir, ckpt_save_policy='top_k', k=10, prefer_low_perf=False, del_past=True): | ||
self.ckpt_save_dir = ckpt_save_dir | ||
self._ckpt_filelist = [] | ||
self.ckpt_save_policy = ckpt_save_policy | ||
self.k = k | ||
|
||
self.ckpt_queue = [] | ||
self.del_past = del_past | ||
self.prefer_low_perf = prefer_low_perf | ||
|
||
def get_ckpt_queue(self): | ||
"""Get all the related checkpoint files managed here.""" | ||
return self.ckpt_queue | ||
|
||
@property | ||
def ckpt_num(self): | ||
"""Get the number of the related checkpoint files managed here.""" | ||
return len(self.ckpt_queue) | ||
|
||
def remove_ckpt_file(self, file_name): | ||
"""Remove the specified checkpoint file from this checkpoint manager and also from the directory.""" | ||
try: | ||
if os.path.exists(file_name): | ||
os.chmod(file_name, stat.S_IWRITE) | ||
os.remove(file_name) | ||
except OSError: | ||
logger.warning("OSError, failed to remove the older ckpt file %s.", file_name) | ||
except ValueError: | ||
logger.warning("ValueError, failed to remove the older ckpt file %s.", file_name) | ||
|
||
|
||
def save_top_k(self, network, perf, ckpt_name, verbose=True): | ||
"""Save and return Top K checkpoint address and accuracy.""" | ||
self.ckpt_queue.append((perf, ckpt_name)) | ||
self.ckpt_queue = sorted(self.ckpt_queue, key=lambda x: x[0], reverse=not self.prefer_low_perf) # by default, reverse is True for descending order | ||
if len(self.ckpt_queue) > self.k: | ||
to_del = self.ckpt_queue.pop(-1) | ||
# save if the perf is better than the minimum in the heap | ||
if to_del[1] != ckpt_name: | ||
ms.save_checkpoint(network, os.path.join(self.ckpt_save_dir, ckpt_name)) | ||
# del minimum | ||
self.remove_ckpt_file(os.path.join(self.ckpt_save_dir, to_del[1])) | ||
else: | ||
ms.save_checkpoint(network, os.path.join(self.ckpt_save_dir, ckpt_name)) | ||
|
||
def save_latest_k(self, network, ckpt_name): | ||
"""Save latest K checkpoint.""" | ||
ms.save_checkpoint(network, os.path.join(self.ckpt_save_dir, ckpt_name)) | ||
self.ckpt_queue.append(ckpt_name) | ||
if len(self.ckpt_queue) > self.k: | ||
to_del = self.ckpt_queue.pop(0) | ||
if self.del_past: | ||
self.remove_ckpt_file(os.path.join(self.ckpt_save_dir, to_del)) | ||
|
||
def save_single(self, network, ckpt_path): | ||
ms.save_checkpoint(network, ckpt_path) | ||
|
||
def save(self, network, perf=None, ckpt_name=None): | ||
"""Save checkpoint according to different save strategy. | ||
""" | ||
if self.ckpt_save_policy is None: | ||
ms.save_checkpoint(network, os.path.join(self.ckpt_save_dir, ckpt_name)) | ||
elif self.ckpt_save_policy == "top_k": | ||
if perf is None: | ||
raise ValueError(f"The expected 'metric' is not None, but got: {metric}.") | ||
self.save_top_k(network, perf, ckpt_name) | ||
return self.ckpt_queue | ||
elif self.ckpt_save_policy == "latest_k": | ||
self.save_latest_k(network, ckpt_name) | ||
return self.ckpt_queue | ||
else: | ||
raise ValueError( | ||
f"The expected 'ckpt_save_policy' is None, top_k or latest_k, but got: {self.ckpt_save_policy}." | ||
) | ||
|
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
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.
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.
I think using python's queue and heap to manage checkpoints will be easier, but this is not critical.
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.
Also thought about it, but heap doesn't sort.