Skip to content

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

@gaussiangit

Description

@gaussiangit

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]]


Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions