Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 3.5 — Making Vectors Smaller
RAG and Vector Search · Example 1 of 2

8-bit scalar quantisation of a vector

The numbers are random values between -1 and 1 from the course's generator. Real embeddings are usually concentrated nearer zero, which makes a per-dimension range (learned from the data) better than a fixed -1 to 1.

Quantisation stores each number of a vector with fewer bits, which shrinks memory and speeds up comparisons while the ranking barely changes. Re-scoring a small shortlist with the full vectors wins back most of the accuracy that heavy compression loses.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write quantise(vec, levels) and restore(codes, levels) for numbers between -1 and 1 (round with math.floor(x + 0.5)). For the vector [-1.0, 0.0, 0.5, 1.0] print the 8-bit codes (levels 255), the restored values with 3 decimals, and the 1-bit codes (levels 1).

02

A collection has 2,000,000 vectors of 768 numbers. Print the memory in megabytes (1 MB = 1,000,000 bytes) as 32-bit floats, 8-bit codes and 1-bit codes.

Explain it without notes

01

What is 8-bit scalar quantisation, how much memory does it save, and what is the largest error on a number between -1 and 1?

02

Why can a ranking stay nearly unchanged when every number in the vectors is slightly wrong?

03

What is re-scoring a shortlist, and why does it recover accuracy lost to compression?

04

Give the memory for 5 million vectors of 768 numbers as float32, 8-bit and binary.

8-bit scalar quantisation of a vector
Sign in to run this example in your browser.

Expected output

original: -0.998 0.143 -0.486 -0.105 0.308 0.934 0.258 0.433
codes:    0 146 65 114 167 247 160 183
restored: -1.000 0.145 -0.490 -0.106 0.310 0.937 0.255 0.435
biggest error 0.0038 (one step is 0.0078, so the error is at most half of that)
cosine(a, b) = -0.0760   cosine(restored a, b) = -0.0785
1000000 vectors of 384 numbers: 1536 MB as 32-bit floats, 384 MB as 8-bit codes
10000000 vectors of 1536 numbers: 61440 MB as 32-bit floats, 15360 MB as 8-bit codes