-
Notifications
You must be signed in to change notification settings - Fork 4
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
Add save submodule plugin #707
Open
ioangatop
wants to merge
1
commit into
main
Choose a base branch
from
706-add-io-plugin-to-save-a-submodule
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 all commits
Commits
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 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 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 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 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 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 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 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 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 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 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 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 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 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,5 @@ | ||
"""Plug-ins API.""" | ||
|
||
from eva.core.plugins.io import SubmoduleTorchCheckpointIO | ||
|
||
__all__ = ["SubmoduleTorchCheckpointIO"] |
This file contains 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,5 @@ | ||
"""IO plug-ins API.""" | ||
|
||
from eva.core.plugins.io.torch_io import SubmoduleTorchCheckpointIO | ||
|
||
__all__ = ["SubmoduleTorchCheckpointIO"] |
This file contains 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,67 @@ | ||
"""Extended version of the IO model checkpoint plugin `TorchCheckpointIO`.""" | ||
|
||
import os | ||
from typing import Any, Dict | ||
|
||
from lightning.fabric.utilities import cloud_io | ||
from lightning.fabric.utilities.types import _PATH | ||
from lightning.pytorch import plugins | ||
from typing_extensions import override | ||
|
||
|
||
class SubmoduleTorchCheckpointIO(plugins.TorchCheckpointIO): | ||
"""IO plugin which allows to additionally save only a sub-part of the full model.""" | ||
|
||
def __init__(self, submodule: str) -> None: | ||
"""Initializes the plugin. | ||
|
||
Args: | ||
submodule: The name of the submodule to additionally save. | ||
""" | ||
super().__init__() | ||
|
||
self._submodule = submodule | ||
|
||
@override | ||
def save_checkpoint( | ||
self, | ||
checkpoint: Dict[str, Any], | ||
path: _PATH, | ||
storage_options: Any | None = None, | ||
) -> None: | ||
super().save_checkpoint(checkpoint, path, storage_options) | ||
self._save_submodule(checkpoint["state_dict"], path) | ||
|
||
@override | ||
def remove_checkpoint(self, path: _PATH) -> None: | ||
super().remove_checkpoint(path) | ||
self._remove_submodule(path) | ||
|
||
def _save_submodule(self, module_checkpoint: Dict[str, Any], module_path: _PATH) -> None: | ||
"""Saves the submodule.""" | ||
path = self._submodule_path(module_path) | ||
state_dict = self._submodule_state_dict(module_checkpoint) | ||
|
||
os.makedirs(os.path.dirname(path), exist_ok=True) | ||
cloud_io._atomic_save(state_dict, path) | ||
|
||
def _remove_submodule(self, module_path: _PATH) -> None: | ||
"""Removes the submodule.""" | ||
path = self._submodule_path(module_path) | ||
fs = cloud_io.get_filesystem(path) | ||
if fs.exists(path): | ||
fs.rm(path, recursive=True) | ||
|
||
def _submodule_path(self, module_path: _PATH) -> str: | ||
"""Constructs and returns the submodule checkpoint path.""" | ||
root, basename = os.path.split(module_path) | ||
return os.path.join(root, self._submodule, basename.replace(".ckpt", ".pth")) | ||
|
||
def _submodule_state_dict(self, module_state_dict: Dict[str, Any]) -> Dict[str, Any]: | ||
"""Returns the submodule `state_dict`.""" | ||
key = self._submodule if self._submodule.endswith(".") else self._submodule + "." | ||
return { | ||
module.replace(key, ""): weights | ||
for module, weights in module_state_dict.items() | ||
if module.startswith(key) | ||
} |
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.
This will also save the checkpoint of the full module to
path
if I understand correctly?For big FMs / encoders those checkpoints can be quite heavy, it'd be nice to have the option to only save the decoder checkpoint.