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

safetensors support when loading from hf_hub #820

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ regex
ftfy
tqdm
huggingface_hub
safetensors
sentencepiece
protobuf
timm
4 changes: 4 additions & 0 deletions src/open_clip/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ def load_checkpoint(model, checkpoint_path, strict=True):
from .big_vision import load_big_vision_weights
load_big_vision_weights(model, checkpoint_path)
return {}
elif Path(checkpoint_path).suffix in ('.safetensors',):
from safetensors.torch import load_model
load_model(model, checkpoint_path)
return {}

state_dict = load_state_dict(checkpoint_path)
# detect old format and make compatible with new format
Expand Down
26 changes: 20 additions & 6 deletions src/open_clip/pretrained.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,14 +543,28 @@ def has_hf_hub(necessary=False):


def download_pretrained_from_hf(
model_id: str,
filename: str = 'open_clip_pytorch_model.bin',
revision=None,
cache_dir: Union[str, None] = None,
model_id: str,
filename: Union[str, None] = None,
revision=None,
cache_dir: Union[str, None] = None,
):
has_hf_hub(True)
cached_file = hf_hub_download(model_id, filename, revision=revision, cache_dir=cache_dir)
return cached_file

# List of filenames to try downloading --> try safetensors if .bin file is missing
filenames_to_try = ['open_clip_pytorch_model.bin', 'open_clip_pytorch_model.safetensors'] if filename is None else [filename]

last_exception = None # Variable to store the last exception
for fname in filenames_to_try:
try:
# Attempt to download the file
cached_file = hf_hub_download(model_id, fname, revision=revision, cache_dir=cache_dir)
return cached_file # Return the path to the downloaded file if successful
except Exception as e:
last_exception = e # Store the last exception encountered
continue # Try the next file

# If the loop completes without returning, raise the last encountered exception
raise FileNotFoundError(f"Failed to download any files for {model_id}. Last error: {last_exception}") from last_exception


def download_pretrained(
Expand Down
Loading