Skip to content

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.

SQ8: a dimension's range from min to max is split into 256 levels; a value maps to the level below it dimension i, learned from training vectors min_i max_i 0 255 x_i code = floor((x_i - min_i) / range × 255) 1 byte per dimension
Each dimension has its own range. Values outside it are clamped to 0 or 255; a dimension with no range is stored as 0.
  1. 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 no training was given. The ranges are saved atomically in scalar_quantizer.bin.
  2. Encode. Value x_i becomes floor(clamp((x_i - min_i) / (max_i - min_i) × 255, 0, 255)). Codes go to sq8_vectors.bin, one D-byte record per vector, with the same versioned header and CRC32C sidecar as vectors.bin.
  3. Search. The scan scores the query (full f32) against every code vector, decoding min_i + code × (max_i - min_i) / 255 on 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.
  4. No originals. The full vectors are not stored. A quantizer file cannot be retrained over existing codes, because they would no longer decode correctly.
import random
import 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-42
  • 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.
  • 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.

No. Only the 8-bit codes are stored, so search scores and returned vectors are approximations of the originals.

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.

Next: What is product quantization?

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.