Lesson 3.2

Encoding to bytes

With one codebook per chunk, encoding a vector is just a row of independent lookups: one byte per chunk.

concepts 8 min read

A row of independent lookups

Encoding is the payoff. For each chunk, find the nearest entry in that chunk's codebook and write down its index (its position in that codebook, a whole number from 0 to 255 that fits in one byte). The chunks do not talk to each other; each is its own little codebook lookup. A 128-number vector split into 8 chunks becomes a row of 8 bytes.

Drag the point in any of the three chunks below. Only that chunk's code changes; the others stay put. The whole vector is now the short code row at the bottom.

Drag any chunk's point; each becomes one code

codes [ 2, 0, 3 ]  ·  3 bytes for the whole vector

Encoding a vector is M independent nearest-entry lookups, one per chunk, producing M bytes. The chunks are quantized completely separately.

The compression, counted

Now the numbers that make this worth doing. Take a real embedding: 128 dimensions of 32-bit floats is 512 bytes. Split into 8 chunks, it encodes to 8 bytes. That is a 64 times reduction, and the codebooks add only a few kilobytes shared across the entire dataset, however many millions of vectors there are.

512 bytes → 8 bytes  =  64× smaller

128-dim float vector to an 8-chunk PQ code

The codes are tiny, but they are lossy. Each chunk now points at its representative, not its real value. The next lesson measures exactly how much was lost.

Key takeaways

1

Encoding is M independent codebook lookups; each chunk becomes one byte, ignoring the others.

2

A 128-dim float vector encodes to 8 bytes, a 64x reduction, with codebooks shared across the dataset.

3

The codes are lossy: each chunk now stands for its representative, not its exact value.