Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

IVF indexes and vector quantisation

After this lesson: I can choose IVF and quantisation settings that keep recall high while saving memory.

60 min lesson6 commandsVector database lab: Qdrant and PostgreSQL with pgvector

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

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

WordWhat it means
Centroidthe middle point of a group of vectors
IVFan index of lists, one per centroid
Probeone list searched for a question
Quantisationstoring numbers in fewer bits
Product quantisationstoring each part of a vector as a code
Rescoringchecking 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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

IVF 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

Command
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

Command
docker exec tools python3 steps/pgrecall.py ivfflat 1

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Recall with five probes

Command
docker exec tools python3 steps/pgrecall.py ivfflat 5

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Worked example: 20,000 vectors in 100 lists

ProbesVectors compared, aboutRecall@10, about
1100 centroids and 200 vectors0.6
5100 centroids and 1,000 vectors1.0
100 (every list)100 centroids and 20,000 vectors1.0, as exact

The cells students fill in are left blank in this preview.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A 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

Command
docker exec tools python3 steps/quant.py scalar

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Product 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

Command
docker exec tools python3 steps/pqtoy.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Search product-quantised vectors in Qdrant

Command
docker exec tools python3 steps/quant.py product

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

What quant.py printed in the lab

Stored asBytes a vectorNo rescoreRescore, oversampling 2Rescore, oversampling 8
float32 (no quantisation)1921.0 at hnsw_ef 64not needednot needed
int8, scalar48about 0.95about 1.0about 1.0
Product, x1612about 0.45about 0.65about 0.97

The cells students fill in are left blank in this preview.

Try it: memory for a million vectors

Code: python
# 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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Assignment: 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.

Code: python
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

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.