mirror of
https://github.com/openai/CLIP.git
synced 2026-11-05 01:37:26 +00:00
[GH-ISSUE #209] clip.model.build_model does not work if device is cpu #181
Labels
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
openai/CLIP#181
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @Toekan on GitHub (Jan 17, 2022).
Original GitHub issue: https://github.com/openai/CLIP/issues/209
Hi,
Thanks for providing this really convenient package to use the CLIP model!
I've come across a problem with
build_modelwhen trying to reconstruct the model from a state_dict on my local computer without GPU.Code to reproduce
First I download one of the built-in models and save the state_dict:
model, preprocess = clip.load("ViT-B/32", jit=False, device="cpu") torch.save(model.state_dict(), 'clip_off_the_shelve.pt')Then I load the model using your function and try to use it to infer a text embedding:
model = clip.model.build_model(torch.load('clip_off_the_shelve.pt')) text_tokens = clip.tokenize(["door"]) with torch.no_grad(): text_features = model.encode_text(text_tokens).float() text_features /= text_features.norm(dim=-1, keepdim=True)This unfortunately results in:
Potential fix (?)
From reading around, it seems like the culprit is convert_weights in
build_model, which converts weights to fp16, regardless off the device being used. Pytorch doesn't support fp16 on "cpu" which seems to create the above error. Would it be possible to makebuild_modelconditional on the device?Thanks!
@jongwook commented on GitHub (Apr 11, 2022):
The conditional code can be found here in
clip.load()github.com/openai/CLIP@3482bb6ed3/clip/clip.py (L138-L141)and
clip.load("clip_off_the_shelve.pt")should work; please let me know if it doesn't.@w1redch4d commented on GitHub (Aug 5, 2022):
Facing the same error, and @jongwook
clip.load("clip_off_the_shelve.pt")doesnt work as well@jongwook commented on GitHub (Aug 5, 2022):
By
clip_off_the_shelve.ptI meant the models downloaded under~/.cache/clip. Let me know what the stacktrace looks like if you see an error loading those models withclip.load().@w1redch4d commented on GitHub (Aug 7, 2022):
The stackrace:
any how the error goes away implementing a simple check in the build_model functionality: