Scalar Quantisation of Embeddings

medium
Unit 21 · APIs & Retrieval0/2 to milestone

Storing float32 embeddings costs 4 bytes per dimension. Scalar quantisation maps each dimension's calibrated range to uint8 (one byte), a 4× saving with little loss in retrieval quality. It's the default in many vector databases.

Implement:

  • fit_ranges(E): per-dimension (mins, maxs) from calibration embeddings
  • quantize(E, ranges): round((x - min) / (max - min) * 255), clipped to [0, 255], as np.uint8. A dimension with max == min maps to 0.
  • dequantize(Q, ranges): the inverse mapping back to floats
  • topk_overlap(Q, D, ranges, queries, k): for each query, compare the top-k document indices by dot product using the dequantised documents against the exact top-k, and return the mean overlap fraction
#embeddings#quantization#vector-search2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Scalar Quantisation of Embeddings · TechGradify