Search Tutorials
--Ads--Embedding Dimensions, Matryoshka and Quantization
Shrink vectors with Matryoshka truncation and int8 or binary quantization.
Embeddings take space. One million chunks embedded with a 1,024-dimensional model, stored as 32-bit floats, need about 4 GB of memory, and search time grows with vector size. This lesson shows three ways to shrink vectors while keeping most of their retrieval quality: Matryoshka truncation, int8 quantization and binary quantization.
How much space do embeddings need?
storage = number of vectors × dimensions × bytes per value
| Setup (1 million vectors) | Bytes per vector | Total |
|---|---|---|
| 1,024 dims, float32 | 4,096 | ~4.1 GB |
| 384 dims, float32 | 1,536 | ~1.5 GB |
| 1,024 dims truncated to 256, float32 | 1,024 | ~1.0 GB |
| 1,024 dims, int8 | 1,024 | ~1.0 GB |
| 1,024 dims, binary | 128 | ~0.13 GB |
Index structures such as HNSW add more memory on top of the raw vectors (see Vector Index Types).
Matryoshka embeddings: shorter vectors from the same model
Models trained with Matryoshka Representation Learning pack the most important information into the first dimensions, like Russian nesting dolls. You can cut such a vector to its first 256 or 128 dimensions, re-normalise it, and still get good retrieval. With an ordinary model, truncation destroys quality, so check that your model supports it (for example nomic-embed-text-v1.5, mxbai-embed-large-v1, many recent open models and OpenAI's text-embedding-3 family, which takes a dimensions parameter).
from sentence_transformers import SentenceTransformer
# truncate_dim makes the model return only the first 256 dimensions
model = SentenceTransformer("nomic-ai/nomic-embed-text-v1.5", trust_remote_code=True, truncate_dim=256)
vectors = model.encode(["search_document: Employees get 18 days of paid leave."],
normalize_embeddings=True)
print(vectors.shape) # (1, 256)
Or truncate vectors you already have:
import numpy as np
def truncate(vectors: np.ndarray, dims: int) -> np.ndarray:
short = vectors[:, :dims]
return short / np.linalg.norm(short, axis=1, keepdims=True) # re-normalise!
Scalar (int8) quantization
Int8 quantization maps each float to one of 256 integer levels, using the value range seen in a calibration sample. It cuts memory by 4× with typically very small quality loss. Many vector databases (Qdrant, Milvus, Weaviate, pgvector's halfvec for 16-bit) can do this for you internally.
from sentence_transformers.quantization import quantize_embeddings
float_vectors = model.encode(texts, normalize_embeddings=True)
int8_vectors = quantize_embeddings(float_vectors, precision="int8",
calibration_embeddings=float_vectors)
print(float_vectors.nbytes, "->", int8_vectors.nbytes)
Binary quantization
Binary quantization keeps only the sign of each dimension: positive becomes 1, negative becomes 0. A 1,024-dimension vector shrinks from 4,096 bytes to 128 bytes (32× smaller), and similarity becomes a Hamming distance (count of differing bits), which CPUs compute extremely fast.
import numpy as np
def to_binary(vectors: np.ndarray) -> np.ndarray:
return np.packbits(vectors > 0, axis=1) # 8 dimensions per byte
def hamming_search(query_bits: np.ndarray, doc_bits: np.ndarray, k: int) -> np.ndarray:
differing = np.unpackbits(np.bitwise_xor(doc_bits, query_bits), axis=1).sum(axis=1)
return np.argsort(differing)[:k] # fewer differing bits = more similar
Rescoring: binary speed, float accuracy
Binary search alone loses some accuracy. The standard fix is a two-step search: use the binary index to fetch a generous candidate list (say 4× k), then re-rank those candidates with the full-precision vectors kept on disk or in cheaper storage.
def search_with_rescoring(query_vec, doc_bits, doc_floats, k=10, oversample=4):
candidates = hamming_search(to_binary(query_vec[None, :])[0], doc_bits, k * oversample)
exact_scores = doc_floats[candidates] @ query_vec
return candidates[np.argsort(-exact_scores)[:k]]
Published experiments with this approach report keeping well over 90% of the original retrieval quality, at a fraction of the memory. Results depend on the model, so measure on your own data.
Which technique when?
| Technique | Memory saving | Quality impact | Requirement |
|---|---|---|---|
| Smaller model | Depends | Can be large | None |
| Matryoshka truncation | 2–8× | Small to moderate | A Matryoshka-trained model |
| int8 | 4× | Very small | Calibration data |
| Binary + rescoring | 32× (in memory) | Small | Full vectors kept elsewhere for rescoring |
The techniques combine: Matryoshka truncation to 512 dimensions plus int8 is 16× smaller than the original float32 vectors.
Key takeaways
- Storage is vectors × dimensions × bytes per value; it grows quickly at scale.
- Matryoshka models can be truncated to fewer dimensions; always re-normalise afterwards.
- int8 quantization gives 4× savings with little loss; binary gives 32× and works best with rescoring.
- Below a few hundred thousand vectors, optimisation is rarely needed; plain float32 is fine.