#53903·milvus

[Bug]: COSINE similarity collapses to ~0 / raw dot product for small-norm vectors (FLAT index, v3.0.1)

Author: JoeyLYZ666Created Sep 29, 2026Updated Sep 29, 2026
Labelskind/bugneeds-triage

Is there an existing issue for this?

  • I have searched the existing issues

Environment

markdown
- Milvus version: v3.0.1 standalone (official Docker image `milvusdb/milvus:v3.0.1`; server reports `3.0-20260902-658cbd1689`)
- Deployment mode(standalone or cluster): standalone
- MQ type(rocksmq, pulsar or kafka): rocksmq (default)
- SDK version(e.g. pymilvus v2.0.0rc2): pymilvus 3.0.1
- OS(Ubuntu or CentOS): server in Docker on Windows 11 host
- CPU/Memory: not load-related
- GPU: none
- Others: index FLAT, metric COSINE, no filters, no search params

Current Behavior

Cosine similarity between collinear nonzero vectors depends on their magnitude once the norm drops below ~1e-20. With a FLAT/COSINE collection containing one 2-D row per case (all values exactly representable in float32):

stored vector query vector true cosine returned score
[1e-1, 0] [1e-1, 0] 1 1.0
[1e-20, 0] [1e-20, 0] 1 1.0
[1e-25, 0] [1e-25, 0] 1 1.0000000811712396e-25
[1e-1, 0] [1e-25, 0] 1 0.0
[1e-25, 0] [1e-1, 0] 1 1.0000000195414814e-25

The tiny stored vector behaves as if it were the zero vector (self-similarity 0), and the tiny query vector behaves as if it were never normalized (score equals the raw dot product). Both operands and their norms are ordinary finite float32 values; nothing underflows in the input.

Expected Behavior

Cosine similarity is scale-invariant: for collinear nonzero vectors the score is 1 (or -1 for opposite directions) regardless of magnitude. A score of 0 means "orthogonal" and 1e-25 is a raw, unnormalized dot product; both change top-k decisions around any score threshold (e.g. radius/range search or score filtering).

Steps To Reproduce

markdown
1. Run the standalone at `127.0.0.1:19530` and `pip install pymilvus==3.0.1 numpy`.
2. Run the attached `reproduce.py` (`python reproduce.py --uri http://127.0.0.1:19530 --output observed.json`). It creates a uniquely named FLAT/COSINE collection, inserts one row per case, flushes, searches with default params, prints the table above, deletes its own collection, and exits non-zero on mismatch. `observed.json` is the output of an actual run; the run is deterministic (repeated twice with identical results).

Milvus Log

No response

Anything else?

The apparent mechanism: the per-vector sum of squares is computed in float32, and (1e-25)^2 = 1e-50 underflows to 0, so the vector takes the same path as a true zero vector (stored side not normalized; query side falls back to the raw dot product). The threshold fits this explanation: [1e-20, 0] (sum of squares 1e-40, a representable float32 subnormal) is still handled correctly, [1e-25, 0] is not. #50408 reports the overflow side of the same computation (1e20-scale queries), so a shared fix — widening the norm accumulation, or treating "sum of squares == 0" differently from "vector is zero" — would likely cover both ends.

I found this while checking that positively scaling the data and query of an exact search preserves all cosine scores (a metamorphic relation). Happy to provide more failing magnitudes if useful.

reproduce.py