-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dev(narugo): add model metadata reader and writer
- Loading branch information
1 parent
cc802fc
commit 7540259
Showing
7 changed files
with
739 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from typing import Dict | ||
|
||
try: | ||
import torch | ||
except (ImportError, ModuleNotFoundError): # pragma: no cover | ||
torch = None | ||
|
||
try: | ||
import safetensors.torch | ||
except (ImportError, ModuleNotFoundError): # pragma: no cover | ||
safetensors = None | ||
|
||
|
||
def _check_env(): | ||
if not safetensors: | ||
raise EnvironmentError( | ||
'Safetensors not installed. Please use "pip install dghs-imgutils[model]".') # pragma: no cover | ||
if not torch: | ||
raise EnvironmentError( | ||
'Torch not installed. Please use "pip install dghs-imgutils[model]".') # pragma: no cover | ||
|
||
|
||
def read_metadata(model_file: str) -> Dict[str, str]: | ||
_check_env() | ||
with safetensors.safe_open(model_file, 'pt') as f: | ||
return f.metadata() | ||
|
||
|
||
def save_with_metadata(src_model_file: str, dst_model_file: str, metadata: Dict[str, str], clear: bool = False): | ||
_check_env() | ||
with safetensors.safe_open(src_model_file, framework='pt') as f: | ||
if clear: | ||
new_metadata = {**(metadata or {})} | ||
else: | ||
new_metadata = {**f.metadata(), **(metadata or {})} | ||
safetensors.torch.save_file( | ||
tensors={key: f.get_tensor(key) for key in f.keys()}, | ||
filename=dst_model_file, | ||
metadata=new_metadata, | ||
) |
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,2 @@ | ||
torch | ||
safetensors |
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.