Command Palette

Search for a command to run...

Hectal
Phase 3: Vector Search: Exact, IVF, HNSW and Smaller Vectors
RAG and Vector Search · Exercise 5 of 6 PreviousNext
Intermediate

Scalar quantisation and memory

Write quantise(vec, levels) that maps each number from the range -1 to 1 onto a whole number from 0 to levels: floor((x + 1) / 2 * levels + 0.5) (import math for floor).

Write restore(codes, levels) that maps the codes back to numbers in -1 to 1: code / levels * 2 - 1.

Write memory_mb(n, dim, bits) that returns the megabytes (1 MB = 1,000,000 bytes, integer division) needed for n vectors of dim numbers stored with bits bits per number.

What Check tests

  1. 8-bit codes
  2. 1-bit codes keep only the sign
  3. The extremes restore exactly
  4. The error is at most half a step
  5. Memory at three precisions

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.