Binary Embeddings and Hamming Search

medium
Unit 20 · RLHF & Embeddings0/2 to milestone

Storing millions of 1024-dimensional float embeddings is expensive. Binary quantisation keeps just the sign of each dimension, giving 32× less memory, and compares vectors by Hamming distance, which CPUs compute very fast. You then rescore the shortlist with the full-precision vectors.

Implement:

  • binarize(X): rows of floats → a uint8 array of packed bits (1 where the value is > 0), via np.packbits(X > 0, axis=1).
  • hamming(a, b): the number of differing bits between two packed rows.
  • search(query, X, k, rescore=10): binarise everything, take the rescore * k nearest by Hamming distance (ties by index), then re-rank those by cosine similarity with the original float vectors and return the top k indices.
#embeddings#vector-search#quantization2 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Binary Embeddings and Hamming Search · TechGradify