Week 10 • Day 4Week 10

Day 4:Week 10, Day 4: Vector Compression — Scalar (`SQ8`) & Binary (`1-Bit`) Quantization with Oversampled Rescoring

Battle-tested daily lesson note • 100% runnable code, interview Q&A, test suite & interactive quiz

Week 10, Day 4: Vector Compression — Scalar (SQ8) & Binary (1-Bit) Quantization with Oversampled Rescoring


1. Slashing Vector Database RAM by 4x to 32x

In Week 10 Day 1, we saw that 10M vectors of dimension 1536 require 61.44 GB of RAM in FP32. How do production engines (Qdrant, Weaviate, Milvus, Lucene, FAISS) shrink that to 1.92 GB while retaining > 98% recall?

1. Scalar Quantization (SQ8 / INT8 — 4x Compression)

Map each 32-bit float vi∈[vmin⁡,vmax⁡]v_i \in [v_{\min}, v_{\max}] linearly into a signed 8-bit integer [-127, +127]:

qi=round(127⋅vimax⁡∣v∣)∈[−127,127]q_i = \text{round}\left(127 \cdot \frac{v_i}{\max |\mathbf{v}|}\right) \in [-127, 127]

  • Memory Reduction: 4 bytes -> 1 byte (4x smaller).
  • Accuracy: Typically retains 99%+ of FP32 Recall@10 immediately without rescoring!

2. Binary Quantization (BQ / 1-Bit — 32x Compression + XOR/popcount Speedup!)

Map every positive coordinate to 1 and every negative coordinate to 0:

bi={1,if vi>00,if vi≤0b_i = \begin{cases} 1, & \text{if } v_i > 0 \\ 0, & \text{if } v_i \le 0 \end{cases}

  • Memory Reduction: 32 bits -> 1 bit (32x smaller!).
  • Ultra-Fast Distance Computation: The distance between two binary vectors is simply their Hamming Distance (number of differing bits), computed in a single CPU instruction via XOR + POPCNT!
  • Two-Stage Oversampled Rescoring Pattern:
    1. Search the 1-bit Binary Index in RAM with an oversampling factor (e.g., fetch Top-40 candidates for a Top-5 request).
    2. Fetch the exact FP32 (or SQ8) vectors from disk for just those 40 candidates and rescore them   ⟹  \implies 32x less RAM with ~97-99% recall!

In Simple English: Compressing Vectors 4x (SQ8) to 32x (1-Bit Binary)

How can you store 10 million vectors without buying a 64 GB RAM server?

  • Scalar Quantization (SQ8 — 4x smaller): Round smooth 32-bit decimals (0.7341928) into a neat -127..+127 integer score (1 byte instead of 4 bytes!). Memory drops from 64 GB to 16 GB with ~99% accuracy!
  • Binary Quantization (1-Bit — 32x smaller): Keep ONLY the sign bit (+ becomes 1, - becomes 0)! Memory shrinks from 64 GB down to 2 GB!
  • Pro Trick (Oversampled Rescoring): Use blazing-fast 1-Bit search to grab the Top 100 rough candidates in 0.5ms, then load full-precision vectors ONLY for those 100 candidates to rank the exact Top 5!
Week 10 Day 4: Week 10, Day 4: Vector Compression — Scalar (`SQ8`) & Binary (`1-Bit`) Quantization with Oversampled Rescoring | 6-Month AI Engineer Roadmap · DataArena