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

RuntimeError: linalg.vector_norm: Expected a floating point or complex tensor as input. Got Long #173

Open
gaussiangit opened this issue Jan 31, 2025 · 0 comments

Comments

@gaussiangit
Copy link

gaussiangit commented Jan 31, 2025

Given code throws me following error.
eva_clip/model.py", line 248, in forward
text = torch.nn.functional.normalize(text, p=2, dim=-1)
File "/site-packages/torch/nn/functional.py", line 5373, in normalize
denom = input.norm(p, dim, keepdim=True).clamp_min(eps).expand_as(input)
File "site-packages/torch/_tensor.py", line 827, in norm
return torch.norm(self, p, dim, keepdim, dtype=dtype)
File "/torch/functional.py", line 1822, in norm
return torch.linalg.vector_norm(input, _p, _dim, keepdim, dtype=dtype)
RuntimeError: linalg.vector_norm: Expected a floating point or complex tensor as input. Got Long

from torch.utils.data import Dataset, DataLoader
from transformers import AutoModel, AutoConfig, AutoTokenizer
from eva_clip import create_model_and_transforms, get_tokenizer
device = 'cuda'
from PIL import Image
 
model_name = "EVA02-CLIP-L-14-336" 
pretrained = "EVA02_CLIP_L_336_psz14_s6B.pt" 
image_path = "CLIP.png"
aption = ["a diagram", "a dog", "a cat"]
device = "cuda" if torch.cuda.is_available() else "cpu"
model, _, preprocess = create_model_and_transforms(model_name, pretrained, force_custom_clip=True)
tokenizer = get_tokenizer(model_name)
model = model.to(device)
image = preprocess(Image.open(image_path)).unsqueeze(0).to(device)
text = tokenizer(["a diagram", "a dog", "a cat"]).to(device)
with torch.no_grad(), torch.cuda.amp.autocast():
    image_features = model.encode_image(image)
    text_features = model.encode_text(text)
    image_features /= image_features.norm(dim=-1, keepdim=True)
    text_features /= text_features.norm(dim=-1, keepdim=True)
    text_probs = (100.0 * image_features @ text_features.T).softmax(dim=-1)
print("Label probs:", text_probs)  # prints: [[0.8275, 0.1372, 0.0352]]


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant