[GH-ISSUE #222] clip output is NaN #193

Closed
opened 2026-10-31 21:12:15 +00:00 by bsakaguchi · 6 comments
Owner

Originally created by @chinoll on GitHub (Mar 7, 2022).
Original GitHub issue: https://github.com/openai/CLIP/issues/222

My CLIP will output NaN when using CUDA, but it will output normally when using CPU. How to solve this problem?

import torch
import clip
from PIL import Image
import numpy as np
device = "cuda:0" #use cuda
model, preprocess = clip.load("ViT-B/32", device=device,jit=False)

text = clip.tokenize(["a photo is comics","a photo is illustration","a photo is empty","a photo is white","a photo is black","a photo is gray","a photo is lineart"]).to(device)
with torch.no_grad():
    image = preprocess(image).unsqueeze(0).to(device)
    logits_per_image, logits_per_text = model(image, text)
    print(logits_per_image)
    probs = logits_per_image.softmax(dim=-1).cpu().numpy()[0]
print(probs)

output

tensor([[nan, nan, nan, nan, nan, nan, nan]], device='cuda:0',
       dtype=torch.float16)
[nan nan nan nan nan nan nan]

CLIP can output the probability normally when using CPU.

Originally created by @chinoll on GitHub (Mar 7, 2022). Original GitHub issue: https://github.com/openai/CLIP/issues/222 My CLIP will output NaN when using CUDA, but it will output normally when using CPU. How to solve this problem? ```python import torch import clip from PIL import Image import numpy as np device = "cuda:0" #use cuda model, preprocess = clip.load("ViT-B/32", device=device,jit=False) text = clip.tokenize(["a photo is comics","a photo is illustration","a photo is empty","a photo is white","a photo is black","a photo is gray","a photo is lineart"]).to(device) with torch.no_grad(): image = preprocess(image).unsqueeze(0).to(device) logits_per_image, logits_per_text = model(image, text) print(logits_per_image) probs = logits_per_image.softmax(dim=-1).cpu().numpy()[0] print(probs) ``` output ``` tensor([[nan, nan, nan, nan, nan, nan, nan]], device='cuda:0', dtype=torch.float16) [nan nan nan nan nan nan nan] ``` CLIP can output the probability normally when using CPU.
Author
Owner

@chinoll commented on GitHub (Mar 7, 2022):

torch version:1.10.2
cuda version:11.4
OS:Windows 10
python version:3.9.5

<!-- gh-comment-id:1060729572 --> @chinoll commented on GitHub (Mar 7, 2022): torch version:1.10.2 cuda version:11.4 OS:Windows 10 python version:3.9.5
Author
Owner

@illtellyoulater commented on GitHub (Mar 19, 2022):

same problem here, did you manage to fix it?

<!-- gh-comment-id:1072915583 --> @illtellyoulater commented on GitHub (Mar 19, 2022): same problem here, did you manage to fix it?
Author
Owner

@chinoll commented on GitHub (Mar 19, 2022):

same problem here, did you manage to fix it?

Are you talking about getting the model to produce the correct output? Using model.float() will make the model produce the correct output, but I want to use float16 and the model does not work properly when the input data is in the format float16.

<!-- gh-comment-id:1072950450 --> @chinoll commented on GitHub (Mar 19, 2022): > same problem here, did you manage to fix it? Are you talking about getting the model to produce the correct output? Using model.float() will make the model produce the correct output, but I want to use float16 and the model does not work properly when the input data is in the format float16.
Author
Owner

@illtellyoulater commented on GitHub (Mar 29, 2022):

@chinoll yeah I also wanted to make it work with the GPU (and float16)... and I think I've fixed this by using a different version of cuda toolkit.

Try uninstalling torch & torchvision and then install them again with pip3 install torch==1.11.0+cu115 torchvision==0.12.0+cu115 -f https://download.pytorch.org/whl/torch_stable.html.

If it doesn't work, again uninstall them and reinstall them with pip install torch==1.10.1+cu102 torchvision==0.11.2+cu102 -f https://download.pytorch.org/whl/torch_stable.html.

<!-- gh-comment-id:1082148367 --> @illtellyoulater commented on GitHub (Mar 29, 2022): @chinoll yeah I also wanted to make it work with the GPU (and float16)... and I think I've fixed this by using a different version of cuda toolkit. Try uninstalling torch & torchvision and then install them again with `pip3 install torch==1.11.0+cu115 torchvision==0.12.0+cu115 -f https://download.pytorch.org/whl/torch_stable.html`. If it doesn't work, again uninstall them and reinstall them with `pip install torch==1.10.1+cu102 torchvision==0.11.2+cu102 -f https://download.pytorch.org/whl/torch_stable.html`.
Author
Owner

@SunZhigang7 commented on GitHub (Mar 31, 2022):

pip3 install torch==1.11.0+cu115 torchvision==0.12.0+cu115 -f https://download.pytorch.org/whl/torch_stable.html

useful!! thanks

<!-- gh-comment-id:1084802567 --> @SunZhigang7 commented on GitHub (Mar 31, 2022): > pip3 install torch==1.11.0+cu115 torchvision==0.12.0+cu115 -f https://download.pytorch.org/whl/torch_stable.html useful!! thanks
Author
Owner

@chinoll commented on GitHub (Apr 7, 2022):

@chinoll yeah I also wanted to make it work with the GPU (and float16)... and I think I've fixed this by using a different version of cuda toolkit.

Try uninstalling torch & torchvision and then install them again with pip3 install torch==1.11.0+cu115 torchvision==0.12.0+cu115 -f https://download.pytorch.org/whl/torch_stable.html.

If it doesn't work, again uninstall them and reinstall them with pip install torch==1.10.1+cu102 torchvision==0.11.2+cu102 -f https://download.pytorch.org/whl/torch_stable.html.

Great!

<!-- gh-comment-id:1091028999 --> @chinoll commented on GitHub (Apr 7, 2022): > @chinoll yeah I also wanted to make it work with the GPU (and float16)... and I think I've fixed this by using a different version of cuda toolkit. > > Try uninstalling torch & torchvision and then install them again with `pip3 install torch==1.11.0+cu115 torchvision==0.12.0+cu115 -f https://download.pytorch.org/whl/torch_stable.html`. > > If it doesn't work, again uninstall them and reinstall them with `pip install torch==1.10.1+cu102 torchvision==0.11.2+cu102 -f https://download.pytorch.org/whl/torch_stable.html`. Great!
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
openai/CLIP#193
No description provided.