Why store embeddings as integers
A float32 embedding of 768 dimensions takes 3 KB. Ten million of them take 30 GB, before any index. Integers cut that: int16 halves it, int8 quarters it. The question is what the rounding does to the ranking, and the answer depends almost entirely on how you quantise and how you score.
Symmetric quantisation: one scale, zero stays zero
Pick one scale for the whole corpus from the largest magnitude, and round:
s = \frac{2^{b-1} - 1}{\max_{i,j} |x_{ij}|}, \qquad q_{ij} = \operatorname{round}(s\,x_{ij})with b = 16 for int16 (scale to 32767) or b = 8 for int8 (scale to 127). There is no offset, so a dot product of codes is the dot product of the vectors times s^2, plus rounding:
q \cdot k \approx s^2\,(x \cdot y)The common scale cancels in cosine similarity, so the ranking is decided by the rounded vectors alone. With int16 that rounding is about 1/65,000 of the largest value: below what changes a ranking in practice. With int8 it is about 1/255, which keeps the right documents in the top-10 but can swap neighbours.
Per-dimension affine codes break dot products
Many tools calibrate each dimension separately, mapping its own minimum and maximum to the code range:
q_{ij} = \operatorname{round}\!\left(\frac{x_{ij} - m_j}{M_j - m_j}\cdot 255\right) - 128That is a good way to store each number, but it is the wrong way to compare vectors. Each dimension now has its own scale and its own offset, so the integer dot product q \cdot k is no longer proportional to x \cdot y: dimensions with a small range are blown up, and the offsets add terms that depend on each vector. Scoring those codes with a raw dot product changes the ranking a lot. On BEIR FiQA, sentence-transformers’ quantize_embeddings(precision="int8") scored that way changed every query’s top-10 and lowered nDCG@10 from 0.3687 to 0.3382 (MiniLM-L6) and from 0.4008 to 0.3533 (bge-base).
Measured on a small corpus
This run builds clustered unit vectors (like real embeddings), stores them three ways and searches each with Riptorch’s exact index, against the float64 ranking of the original float32 vectors:
import numpy as np
import riptorch as torch
# Clustered unit vectors, like real embeddings: 4,000 documents, 64 queries, 256 dimensions
rng = np.random.default_rng(3)
centres = rng.standard_normal((40, 256)).astype(np.float32)
docs = centres[rng.integers(0, 40, 4000)] + 0.35 * rng.standard_normal((4000, 256)).astype(np.float32)
docs /= np.linalg.norm(docs, axis=1, keepdims=True)
queries = docs[rng.integers(0, 4000, 64)] + 0.2 * rng.standard_normal((64, 256)).astype(np.float32)
queries /= np.linalg.norm(queries, axis=1, keepdims=True)
def top10(scores):
n = scores.shape[1]
return np.lexsort((np.broadcast_to(np.arange(n), scores.shape), -scores), axis=1)[:, :10]
truth = top10(queries.astype(np.float64) @ docs.astype(np.float64).T) # float64 ranking of the float vectors
def report(name, got, nbytes):
order = (got == truth).all(axis=1).mean()
recall = np.mean([len(set(g) & set(t)) / 10 for g, t in zip(got, truth)])
print(f"{name:34} {nbytes:4} bytes/vector same top-10 order {order:6.1%} recall@10 {recall:.4f}")
report("float32", truth, 4 * 256)
for bits, dtype in [(16, np.int16), (8, np.int8)]:
s = (2 ** (bits - 1) - 1) / np.abs(docs).max() # symmetric: one scale, zero stays zero
d, q = np.round(docs * s).astype(dtype), np.round(queries * s).astype(dtype)
_, idx = torch.SimilarityIndex(torch.from_numpy(d), metric="cross", exact=True).search(torch.from_numpy(q), 10)
report(f"symmetric int{bits} + exact search", idx.numpy(), bits // 8 * 256)
# Per-dimension affine int8 (calibrated min/max per dimension), scored by the raw integer dot product
lo, hi = docs.min(axis=0), docs.max(axis=0)
code = lambda x: np.clip(np.round((x - lo) / (hi - lo) * 255 - 128), -128, 127).astype(np.int8)
_, idx = torch.SimilarityIndex(torch.from_numpy(code(docs)), metric="dot", exact=True).search(torch.from_numpy(code(queries)), 10)
report("per-dimension affine int8 + dot", idx.numpy(), 256)float32 1024 bytes/vector same top-10 order 100.0% recall@10 1.0000 symmetric int16 + exact search 512 bytes/vector same top-10 order 100.0% recall@10 1.0000 symmetric int8 + exact search 256 bytes/vector same top-10 order 17.2% recall@10 0.9734 per-dimension affine int8 + dot 256 bytes/vector same top-10 order 0.0% recall@10 0.7484
Symmetric int16 keeps the float ranking exactly here, at half the memory. Symmetric int8 keeps 97% of the right documents in the top-10 at a quarter of the memory, but often in a different order. Per-dimension affine int8 loses a quarter of them.
Score exactly, too
Quantising well is half the job: the scores have to be computed without new rounding. A float16 cosine on the stored int8 or int16 vectors changed the top-10 order for 11–32% of FiQA queries even though the vectors themselves were fine. Riptorch’s exact index computes integer dot products and compares the scores as exact fractions, so the top-10 is the exact ranking of the stored vectors:
import riptorch as torch
index = torch.SimilarityIndex(torch.from_numpy(codes_int16), metric="cross", exact=True)
values, indices = index.search(torch.from_numpy(query_codes_int16), 10)
Which to choose
| storage | bytes per dimension | ranking vs float32 | use it for |
|---|---|---|---|
| float32 | 4 | the reference | small corpora, re-ranking |
| symmetric int16 + exact search | 2 | the same top-10 on 99.5–99.8% of FiQA queries (order only otherwise; recall 1.0) | the default for retrieval |
| symmetric int8 + exact search | 1 | recall@10 0.971–0.991 on FiQA | very large corpora; re-rank the top-100 in int16 or float32 if order matters |
| per-dimension affine int8 + dot product | 1 | every top-10 changed on FiQA | avoid for similarity search |
Keep the scale s next to the index, and quantise queries with the same scale.