A billion vectors will not fit in memory
An IVF index groups vectors into lists round centres and searches only the nearest lists. Quantisation stores each vector in fewer bytes.
A billion vectors of 768 numbers take about 3 terabytes as plain numbers. Together, these two ideas are how large collections fit on ordinary machines.
By the end of this lesson
Note
I can choose IVF and quantisation settings that keep recall high while saving memory.
Key words
| Word | What it means |
|---|---|
| Centroid | the middle point of a group of vectors |
| IVF | an index of lists, one per centroid |
| Probe | one list searched for a question |
| Quantisation | storing numbers in fewer bits |
| Product quantisation | storing each part of a vector as a code |
| Rescoring | checking the best few again with the full numbers |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsIVF searches only the nearest lists
Building an IVF index runs k-means: it finds a number of centroids and puts each vector in the list of its nearest one. pgvector calls the number of lists "lists".
A search finds the centroids nearest the question and reads only those lists. Each list read is a probe. One probe is fast and misses neighbours just over a border; more probes find them.
An IVF search reads a few lists
flowchart LR
Q["Question"] --> C["Compare with every centroid"]
C --> P1["Nearest list"]
C --> P2["Second nearest list"]
P1 --> R["Compare with the vectors in them"]
P2 --> R
R --> T["Top k"]Build an IVF index with 100 lists
docker exec pg psql -U lab -d shop -c "CREATE INDEX docs_ivf ON docs USING ivfflat (embedding vector_cosine_ops) WITH (lists = 100)"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Recall with one probe
docker exec tools python3 steps/pgrecall.py ivfflat 1Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Recall with five probes
docker exec tools python3 steps/pgrecall.py ivfflat 5Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Worked example: 20,000 vectors in 100 lists
| Probes | Vectors compared, about | Recall@10, about |
|---|---|---|
| 1 | 100 centroids and 200 vectors | 0.6 |
| 5 | 100 centroids and 1,000 vectors | 1.0 |
| 100 (every list) | 100 centroids and 20,000 vectors | 1.0, as exact |
The cells students fill in are left blank in this preview.
Check
Have a go
Check
3 questionsA common mistake
Warning
Many people build an IVF index on an empty table and load the data after. The lists are learnt from the rows present at build time, so empty lists learn nothing. Load the data first, then build, and rebuild when the data changes a lot.
Scalar quantisation: 1 byte instead of 4
A vector's numbers are usually 4-byte floats. Scalar quantisation maps each number to a whole number from -128 to 127, one byte. The vectors take a quarter of the memory.
The search compares the small codes first, which is quick, and loses a little recall. Rescoring then reads the full numbers of the best few and puts them in the right order.
Search int8 vectors in Qdrant
docker exec tools python3 steps/quant.py scalarShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsProduct quantisation stores codes for parts
Product quantisation cuts a vector into parts. Each part has a small codebook of centres, learnt from the data. A part is stored as the number of its nearest centre.
Qdrant's x16 setting keeps 12 bytes for the lab's 192. The loss is larger, so a search reads more candidates than it needs, called oversampling, and rescores them with the full numbers.
Product quantisation of one vector by hand
docker exec tools python3 steps/pqtoy.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Search product-quantised vectors in Qdrant
docker exec tools python3 steps/quant.py productShown here, not run. Press Start lab to type it into your own lab’s terminal.
What quant.py printed in the lab
| Stored as | Bytes a vector | No rescore | Rescore, oversampling 2 | Rescore, oversampling 8 |
|---|---|---|---|---|
| float32 (no quantisation) | 192 | 1.0 at hnsw_ef 64 | not needed | not needed |
| int8, scalar | 48 | about 0.95 | about 1.0 | about 1.0 |
| Product, x16 | 12 | about 0.45 | about 0.65 | about 0.97 |
The cells students fill in are left blank in this preview.
Try it: memory for a million vectors
# Memory for the vectors alone, in megabytes, before any index.
vectors = 1000000
numbers = 768
float32 = vectors * numbers * 4
int8 = vectors * numbers * 1
pq = vectors * (numbers // 4) * 1
print("float32:", float32 // 1000000, "MB")
print("int8:", int8 // 1000000, "MB")
print("product, 1 byte for every 4 numbers:", pq // 1000000, "MB")
# Change vectors to 50,000,000. Which of the three fits in 64 GB?
Make a free account to save your work and track your progress.
Check
Have a go
Check
3 questionsAssignment: lists and codes
Vectors and centroids are lists of numbers. Distance is the straight-line distance, and you may compare squared distances. Write four functions.
nearest_centroid(centroids, v) is the index of the centroid nearest v; on a tie, the lower index. probe_order(centroids, v, probes) is the list of the indexes of the probes nearest centroids, nearest first. pq_encode(v, codebooks) cuts v into as many equal parts as there are codebooks and gives, for each part, the index of its nearest centre in that part's codebook. pq_decode(codes, codebooks) puts the chosen centres back together into one list.
def nearest_centroid(centroids, v):
return 0
def probe_order(centroids, v, probes):
return []
def pq_encode(v, codebooks):
return []
def pq_decode(codes, codebooks):
return []
Make a free account to save your work and track your progress.
Exit quiz
Have a go