Fixed: RuntimeError: expected common dtype to be floating point, yet common dtype is Long in cosine_similarity
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:
import torch
from torch.nn.functional import cosine_similarity
a = torch.tensor([[1, 0]])
b = torch.tensor([[1, 1]])
print(cosine_similarity(a, b))
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
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

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():
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()))

.float() returns a copy of the tensor as torch.float32, PyTorch's usual decimal type.
After the Fix
uv run python cosine_int_fixed.py
tensor([0.7071])
![The terminal after the fix: tensor([0.7071]).](https://strdewiridedevblogsprod.blob.core.windows.net/images/blog/ai/pytorch/fixes/2026/pytorch-cosine-long-03-fixed.webp)
0.7071, the expected similarity. The commands work unchanged in PowerShell.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Converting only one vector and relying on that | It works, but the next change to the other vector can bring the error back | Convert both, or create both as decimals |
| Writing data as whole numbers when you'll compare it | Every comparison needs a conversion | Create it as decimals: torch.tensor([[1.0, 0.0]]) |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
yet common dtype is Int | Both tensors hold 32-bit whole numbers, torch.int32: two such tensors gave this message | The same fix |
IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1) after the fix | The vectors are flat, shape [2], not rows | Fixed: 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
- torch.nn.functional.cosine_similarity — the formula and its arguments.
- Type promotion — how PyTorch chooses one data type for inputs of different types.
