Skip to content
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 method to load model from already loaded state_dict #22

Merged
merged 1 commit into from
Nov 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/spandrel/__helpers/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def load_from_file(self, path: str | Path) -> ModelDescriptor:
"""

state_dict = self.load_state_dict_from_file(path)
return self.registry.load(state_dict).to(self.device)
return self.load_from_state_dict(state_dict)

def load_state_dict_from_file(self, path: str | Path) -> StateDict:
"""
Expand All @@ -65,6 +65,15 @@ def load_state_dict_from_file(self, path: str | Path) -> StateDict:
f"Unsupported model file extension {extension}. Please try a supported model type."
)

def load_from_state_dict(self, state_dict: StateDict) -> ModelDescriptor:
"""
Load a model from the given state dict.

Throws an `UnsupportedModelError` if the model architecture is not supported.
"""

return self.registry.load(state_dict).to(self.device)

def _load_pth(self, path: str | Path) -> StateDict:
return torch.load(
path,
Expand Down