skip to content

To fit a Sentence Transformers retrieval index into 500 GB, how do you produce int8 or binary embeddings and keep recall with rescoring?

level: seniorimportance: should knowfreq 10%

answer

  1. precision on encode
  2. quantize_embeddings after the fact
  3. int8 needs shared ranges
  4. binary packs eight dims per byte
  5. float32 query rescores a shortlist

basics

~20 s

Encode in float32, convert with quantize_embeddings to int8 using ranges calibrated once on a large sample, or to binary, which needs no calibration. Keep queries in float32 and rescore a shortlist of top_k times a multiplier to recover recall.

solid answer

~40 s

`encode` and `quantize_embeddings` accept `precision` of `float32`, `int8`, `uint8`, `binary` or `ubinary`. For 1,024 dimensions that is 4,096 bytes per vector in float32, 1,024 in int8 and 128 in binary, so 400 million passages drop from about 1.6 TB to about 410 GB or 51 GB. Binary thresholds each value at zero and packs eight dimensions per byte, so chunks can be quantized independently. Int8 maps each dimension's min and max into 256 buckets; `encode(..., precision="int8")` computes those ranges from the embeddings in that call, so chunked indexing gives each chunk different buckets. I compute `ranges` once from a large sample and pass them to every `quantize_embeddings` call. Queries stay float32; I fetch `top_k * rescore_multiplier` candidates from the quantized index and rescore them with the float query.

code

python · 18 lines
python
import numpy as np
from sentence_transformers import SentenceTransformer, quantize_embeddings

model = SentenceTransformer("mixedbread-ai/mxbai-embed-large-v1")

# Calibrate int8 buckets once on a large sample of real passages
sample = model.encode_document(sample_passages, normalize_embeddings=True)
ranges = np.vstack((sample.min(axis=0), sample.max(axis=0)))
np.save("int8_ranges.npy", ranges)

for chunk in passage_chunks:  # lists of passage strings
    emb = model.encode_document(chunk, normalize_embeddings=True)
    int8_emb = quantize_embeddings(emb, precision="int8", ranges=ranges)
    ubinary_emb = quantize_embeddings(emb, precision="ubinary")
    store(int8_emb, ubinary_emb)  # your index writer

# Queries stay float32 so the shortlist can be rescored
query_emb = model.encode_query(["rotate an API key"], normalize_embeddings=True)

go deeper

for a junior

Know that encode has a precision argument and that binary vectors are 32 times smaller than float32 ones.

for a middle

Explain how int8 buckets come from per-dimension ranges and how binary packs bits, and size an index for each precision.

for a senior

Build a chunked indexing job with one stored set of int8 ranges, a binary first stage and float32 rescoring, and measure recall.

for a principal

Trade memory, disk and recall across binary, int8 and float32 tiers against the index budget, and own regeneration on library upgrades.

## The precisions Sentence Transformers offers `SentenceTransformer.encode`, `encode_query` and `encode_document` take a `precision` argument, and `quantize_embeddings` (importable from the package top level) converts embeddings you already have. Both accept: | `precision` | Element type | Bytes for a 1,024-dim vector | Calibration | |---|---|---|---| | `float32` (default) | float32 | 4,096 | none | | `int8` / `uint8` | int8 / uint8 | 1,024 | per-dimension min and max ranges | | `binary` / `ubinary` | int8 / uint8, bits packed | 128 | none | **Binary** sets each value to 1 if it is above zero, else 0, then packs eight dimensions per byte with `np.packbits`; `binary` stores the packed bytes as int8 (offset by 128) and `ubinary` as uint8. Which one you need depends on what your vector library accepts. **Scalar int8** computes, for every dimension, a minimum and maximum, splits that range into 256 buckets and stores the bucket index. For a budget of 500 GB and 400 million 1,024-dimension passages: float32 needs about 1.64 TB, int8 about 410 GB, binary about 51 GB. Why quantized vectors lose some recall is a quantization-quality question; the library's job is producing them consistently. ## The calibration trap with int8 `quantize_embeddings` takes the int8 ranges from, in order: `ranges` you pass, else `calibration_embeddings` you pass, else the embeddings being quantized. `encode(..., precision="int8")` has no way to pass ranges, so it uses the third option on whatever that call encoded. Consequences: - Index a corpus in chunks with `precision="int8"` and **every chunk gets its own buckets**; the same float value maps to different integers across chunks. - Encode one query with `precision="int8"` and the ranges come from a single vector; the library warns that buckets computed from fewer than 100 embeddings are unstable. The fix is to encode in float32, compute ranges once, store them, and reuse them: ```python import numpy as np from sentence_transformers import SentenceTransformer, quantize_embeddings model = SentenceTransformer("mixedbread-ai/mxbai-embed-large-v1") sample = model.encode_document(sample_passages, normalize_embeddings=True) ranges = np.vstack((sample.min(axis=0), sample.max(axis=0))) # shape (2, dim) emb = model.encode_document(chunk, normalize_embeddings=True) int8_chunk = quantize_embeddings(emb, precision="int8", ranges=ranges) ``` Binary has no ranges, so `precision="ubinary"` on `encode` is safe chunk by chunk. ## Rescoring: the step that recovers recall Searching quantized vectors with a quantized query compounds the error on both sides. Rescoring keeps the **query in float32** and works in two stages: 1. Search the quantized index for `top_k * rescore_multiplier` candidates. 2. Score those candidates again with the float32 query against their int8 or binary vectors, and keep the best `top_k`. The library's helpers implement this. `semantic_search_faiss` (in `sentence_transformers.util.quantization`) takes `corpus_precision` of `float32`, `uint8` or `ubinary`, `rescore=True` and `rescore_multiplier=2` by default, and rescores only when the query embeddings are not quantized and the corpus is. `semantic_search_usearch` does the same for `int8` and binary corpora. The pattern the docs recommend at scale combines both: - a **binary index in memory** for the first-stage search; - an **int8 copy on disk**, loaded only for the shortlist; - a **float32 query** for rescoring. In the docs' demo over 41 million texts, the binary index took 5.2 GB of memory and the int8 index 47.5 GB of disk, against roughly 200 GB of memory and disk for float32; binary search with rescoring kept up to about 96% of float32 retrieval performance. ## Fitting the 500 GB budget Applying the tiers to 400 million 1,024-dimension passages: | Tier | Where it lives | Size | |---|---|---| | `ubinary` first-stage index | memory | about 51 GB | | `int8` rescoring copy | disk | about 410 GB | | float32 corpus | not stored | (1.64 TB avoided) | | float32 query | per request | 4 KB | The two stored tiers come to about 461 GB, inside the budget, and only the binary tier must sit in memory. If the budget tightens further, the levers are a smaller checkpoint, `truncate_dim` on a model trained for it, or dropping the int8 tier and rescoring against binary vectors, each measured against recall on labelled queries. ## Release note for Sentence Transformers 6 Version 6 changed multi-process encoding: `encode(..., device=[...], precision="int8")` now quantizes once after merging worker results, so its ranges match single-process encoding. Int8 indexes built with v5.x multi-process encoding are not bit-compatible and must be regenerated. Peak memory is higher, because the full float32 matrix exists before quantization. ## Checklist - Store the int8 `ranges` with the index and reuse them for every insert. - Never quantize the query before rescoring. - Tune `rescore_multiplier` against recall on labelled queries; 2 is a default, not a recommendation for every corpus. - Combine with `truncate_dim` only after checking the model was trained for truncation.

  • Why must the query stay float32 when the corpus is quantized?
    Rescoring compares a precise query against the shortlisted quantized documents, which removes the query side of the error. `semantic_search_faiss` only rescores when the query embeddings are not quantized and the corpus is; quantize the query too and you get the shortlist order back unchanged, with both sides' error in it.
  • You upgraded to Sentence Transformers 6 and rebuilt part of an int8 index with multi-process encode. Can you mix it with the old part?
    Not if the old part came from v5.x multi-process int8 encoding: v6 quantizes once after merging worker results, so its buckets match single-process encoding, and the migration guide says older multi-process int8 indexes are not bit-compatible. Regenerate them, or better, quantize with stored `ranges` through `quantize_embeddings` so the release cannot change your buckets.
  • Which precision would you hand a vector library that only accepts unsigned bytes for bit vectors?
    `ubinary`. It packs the same sign bits as `binary` but stores them as uint8; `binary` stores the packed bytes as int8 offset by 128. The bits are identical, so the choice is only about what your library or database accepts.

Rescoring is like shortlisting job applicants from one-line summaries, then reading the full CV of each shortlisted person: the shortlist must be wider than the final hires, and the final read only helps if you read with full attention, not the one-line summary again.

  • Calls encode with precision='int8' chunk by chunk and assumes buckets match
  • Quantizes the query to int8 or binary before rescoring
  • Thinks binary quantization needs a calibration set like int8
  • Expects binary output to keep one value per dimension
  • Believes rescoring recovers documents the quantized search never shortlisted

Assumes Sentence Transformers 6; v6 quantizes multi-process int8 output once after merging, so v5.x multi-process int8 indexes must be rebuilt.