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 safeglobals to allow metaclip models to load with weights_only=True #967

Merged
merged 1 commit into from
Oct 21, 2024
Merged
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
14 changes: 14 additions & 0 deletions src/open_clip/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@
_MODEL_CONFIGS = {} # directory (model_name: config) of model architecture configs


try:
import _codecs
import numpy as np
# add safe globals, known to be needed for metaclip weights
torch.serialization.add_safe_globals([
_codecs.encode, # now in pytorch main but some pytorch versions w/ weights_only flag don't have it
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if you specify that's not necessary from PyTorch 2.5.0 (I checked this)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For accountability, this is how I checked it's in that release (tag): I opened pytorch/pytorch@1e9bedf, and saw that it contains the tag v2.5.0 (meaning that this commit is included in the tag, thus the release).

np.core.multiarray.scalar,
np.dtype,
np.dtypes.Float64DType,
])
except Exception:
pass


def _natural_key(string_):
return [int(s) if s.isdigit() else s for s in re.split(r'(\d+)', string_.lower())]

Expand Down
19 changes: 19 additions & 0 deletions src/open_clip/model_configs/ViT-bigG-14-quickgelu.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"embed_dim": 1280,
"quick_gelu": true,
"vision_cfg": {
"image_size": 224,
"layers": 48,
"width": 1664,
"head_width": 104,
"mlp_ratio": 4.9231,
"patch_size": 14
},
"text_cfg": {
"context_length": 77,
"vocab_size": 49408,
"width": 1280,
"heads": 20,
"layers": 32
}
}
5 changes: 5 additions & 0 deletions src/open_clip/pretrained.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ def _mccfg(url='', hf_hub='', **kwargs):
laion2b_s39b_b160k=_pcfg(hf_hub='laion/CLIP-ViT-bigG-14-laion2B-39B-b160k/'),
)

_VITbigG14_quickgelu = dict(
metaclip_fullcc=_pcfg(url='https://dl.fbaipublicfiles.com/MMPT/metaclip/G14_fullcc2.5b.pt'),
)

_robertaViTB32 = dict(
laion2b_s12b_b32k=_pcfg(hf_hub='laion/CLIP-ViT-B-32-roberta-base-laion2B-s12B-b32k/'),
)
Expand Down Expand Up @@ -356,6 +360,7 @@ def _mccfg(url='', hf_hub='', **kwargs):
"ViT-H-14-378-quickgelu": _VITH14_378_quickgelu,
"ViT-g-14": _VITg14,
"ViT-bigG-14": _VITbigG14,
"ViT-bigG-14-quickgelu": _VITbigG14_quickgelu,

"roberta-ViT-B-32": _robertaViTB32,
"xlm-roberta-base-ViT-B-32": _xlmRobertaBaseViTB32,
Expand Down
Loading