Concepts
What is SQ8 scalar quantization?
How SrvDB's SQ8 mode maps each dimension's min-max range to one byte (0-255), scans the codes with cosine similarity, and stores vectors 4 times smaller than flat.
SQ8 (8-bit scalar quantization) stores every dimension of a vector as one byte: the value’s position between that dimension’s minimum and maximum, scaled to 0-255. It makes a SrvDB database 4 times smaller than flat with one simple, fast transformation, at the cost of approximate scores.
How it works
Section titled “How it works”- Train. SrvDB records the minimum and maximum of every dimension
over the training vectors: the sample passed to
srvdb.create(..., index="sq8", training=...), or the first batch added when notrainingwas given. The ranges are saved atomically inscalar_quantizer.bin. - Encode. Value
x_ibecomesfloor(clamp((x_i - min_i) / (max_i - min_i) × 255, 0, 255)). Codes go tosq8_vectors.bin, oneD-byte record per vector, with the same versioned header and CRC32C sidecar asvectors.bin. - Search. The scan scores the query (full f32) against every code
vector, decoding
min_i + code × (max_i - min_i) / 255on the fly and accumulating the dot product and norm in one pass, in parallel blocks of 256 vectors. The score is the cosine similarity between the query and the decoded vector. - No originals. The full vectors are not stored. A quantizer file cannot be retrained over existing codes, because they would no longer decode correctly.
Try it in SrvDB
Section titled “Try it in SrvDB”import randomimport srvdb
rng = random.Random(7)vectors = [[rng.gauss(0, 1) for _ in range(128)] for _ in range(1000)]ids = [f"doc-{i}" for i in range(len(vectors))]
db = srvdb.create("./sq8-db", dimension=128, index="sq8", training=vectors[:200])db.add(ids, vectors, ["{}"] * len(ids))db.save()print(db.search(vectors[42], k=1)[0][0])# doc-42Trade-offs
Section titled “Trade-offs”- Bytes per vector.
D: 128 at D = 128, 384 at D = 384, 768 at D = 768. Four times less than flat. - Recall. Approximate. The step per dimension is
range / 255, so the error depends on how wide the ranges are. Outliers in the training sample widen a range and coarsen every code in that dimension; values beyond the trained range are clamped. Measure recall against flat. - Scores. Cosine with the decoded vector, not with the original.
- Speed. Still a linear scan, but it reads a quarter of the bytes.
- Training. Only a min/max pass: cheap, but the sample must be representative of later data.
When to choose it
Section titled “When to choose it”- Disk or memory is the limit and you can accept approximate scores.
- Your embeddings are stable in range (most text embedding models are).
- When you need exact scores with a faster scan, choose RaBitQ; for more compression, choose PQ.
How much smaller is SQ8 than flat in SrvDB?
Section titled “How much smaller is SQ8 than flat in SrvDB?”4 times: each dimension is stored as 1 byte instead of a 4-byte float, so a 768-dimensional vector takes 768 bytes instead of 3072.
Does SQ8 keep the original vectors?
Section titled “Does SQ8 keep the original vectors?”No. Only the 8-bit codes are stored, so search scores and returned vectors are approximations of the originals.
What is SQ8 trained on?
Section titled “What is SQ8 trained on?”On the per-dimension minimum and maximum of the training vectors: the first
batch added, or the sample passed to new_scalar_quantized. Later values
outside that range are clamped.
Related concepts
Section titled “Related concepts”Next: What is product quantization?
Papers and further reading
Section titled “Papers and further reading”Per-dimension min-max scalar quantization is a standard technique rather than the subject of one paper; the PQ page lists the product-quantization papers that compare against it.
Was this page helpful? Tell us in Discussions.