Skip to main content

Fixed: RuntimeError: expected common dtype to be floating point, yet common dtype is Long in cosine_similarity

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

I compared two vectors of whole numbers with PyTorch's cosine_similarity, and it stopped with RuntimeError: expected common dtype to be floating point, yet common dtype is Long. The function computes in decimal numbers, and both inputs held whole numbers. Converting them with .float() fixed it.

What I Was Doing​

I was writing a guide to cosine_similarity in PyTorch 2.15.0, on Python 3.15 on an Apple M1 Pro, and checked what happens with vectors written as whole numbers, one row each:

cosine_int.py
import torch
from torch.nn.functional import cosine_similarity

a = torch.tensor([[1, 0]])
b = torch.tensor([[1, 1]])
print(cosine_similarity(a, b))
Terminal
uv run python cosine_int.py

What I Expected​

About 0.7071, the cosine of the 45 degrees between the two vectors.

What I Got​

Output
Traceback (most recent call last):
File "/Users/you/source/pytorch-cosine/cosine_int.py", line 6, in <module>
print(cosine_similarity(a, b))
~~~~~~~~~~~~~~~~~^^^^^^
RuntimeError: expected common dtype to be floating point, yet common dtype is Long

The terminal after uv run python cosine_int.py: a traceback pointing at cosine_similarity(a, b) on line 6 and ending in RuntimeError: expected common dtype to be floating point, yet common dtype is Long.

What I Checked​

What the message's words mean. Long is PyTorch's name for whole numbers stored in 64 bits, torch.int64, which torch.tensor() gives numbers written without a decimal point (see How to Check the Data Type of a Tensor in PyTorch). The "common dtype" is the one data type PyTorch uses for an operation on several tensors. Its type promotion rules say that when the inputs' types differ, "we promote by finding the minimum dtype that satisfies the following rules", with "complex > floating > integral > boolean". Here both were Long, so the common type was Long.

Whether one decimal input is enough. I kept a as whole numbers and wrote b as decimals, torch.tensor([[1.0, 1.0]]). The data types were torch.int64 and torch.float32, and cosine_similarity printed tensor([0.7071]): with one decimal input, the common type is decimal, and the function works.

The reference gives the formula: the dot product of the two vectors divided by the product of their lengths. Those lengths are square roots, which are rarely whole numbers, and the message says the function expects the common type to be a decimal type ("floating point").

Why It Happens​

cosine_similarity computes in decimal numbers, and doesn't convert whole numbers on its own. When both inputs are whole numbers, the common data type is Long, and the function refuses it.

The Fix​

Convert the inputs to decimal numbers with .float():

cosine_int_fixed.py
import torch
from torch.nn.functional import cosine_similarity

a = torch.tensor([[1, 0]])
b = torch.tensor([[1, 1]])
print(cosine_similarity(a.float(), b.float()))

The fixed cosine_int_fixed.py in a macOS-style code window: the same two tensors of whole numbers, and a print of cosine_similarity(a.float(), b.float()).

.float() returns a copy of the tensor as torch.float32, PyTorch's usual decimal type.

After the Fix​

Terminal
uv run python cosine_int_fixed.py
Output
tensor([0.7071])

The terminal after the fix: tensor([0.7071]).

0.7071, the expected similarity. The commands work unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Converting only one vector and relying on thatIt works, but the next change to the other vector can bring the error backConvert both, or create both as decimals
Writing data as whole numbers when you'll compare itEvery comparison needs a conversionCreate it as decimals: torch.tensor([[1.0, 0.0]])

Troubleshooting​

SymptomLikely causeFix
yet common dtype is IntBoth tensors hold 32-bit whole numbers, torch.int32: two such tensors gave this messageThe same fix
IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1) after the fixThe vectors are flat, shape [2], not rowsFixed: IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1) in cosine_similarity

FAQ​

Why does the error say "common" dtype?​

Because it's about the two inputs together. PyTorch first works out one data type for both, then checks that one. A decimal b alone was enough to make the common type decimal.

Do embeddings from a model have this problem?​

No. A model computes in decimal numbers, so its embeddings are decimal too: in How to Measure How Similar Two Sentences Are with Hugging Face Embeddings, they go straight into cosine_similarity. The error comes from tensors you write by hand, or from whole-number data such as counts.

Conclusion​

RuntimeError: expected common dtype to be floating point, yet common dtype is Long came from two tensors of whole numbers. cosine_similarity(a.float(), b.float()) returned tensor([0.7071]).

Additional Resources​