Skip to main content

Module quant

Module quant 

Source
Expand description

How a vector set squeezes a vector before it stores it.

Three ways, and they are Redis’s three rather than ours, because a client can see all of them. VEMB hands back what was stored and not what was sent, VEMB RAW hands back the bytes, and VSIM scores whatever is in there, so a set written with Q8 and a set written with NOQUANT answer differently and a client is entitled to both answers.

§What is stored is a direction and a length

Every one of the three splits a vector into the length it had and the direction it pointed, keeps the length as one float beside the element, and squeezes only the direction. That is what makes the three comparable: they differ in how much of the direction survives and in nothing else.

Quant::None keeps the direction as it was, four bytes a coordinate. Quant::Int8 keeps it as a signed byte a coordinate against a scale that is the largest coordinate there is. Quant::Bin keeps one bit a coordinate, which is the sign, and throws the rest away.

§The arithmetic is the arithmetic a real server does

Down to which multiplication happens first, because the answers are visible. A code is round(unit * (127 / range)) with the reciprocal formed once, and not round(unit / range * 127), which disagrees about one coordinate in ten. What comes back out is (code * range) / 127, and not (code / 127) * range, which disagrees about half the time. Both were read off a real server over several hundred vectors rather than guessed.

The length is norm, which is the strangest of the three and the one that took the longest to pin down, because it is neither an accurate sum of squares nor a naive one. It is Redis’s loop from hnsw.c including its unroll by four and which of its multiplies the compiler fused into the adds beside them, and it matched a real server on 800 vectors out of 800 where every simpler shape matched about seven in ten.

Structs§

Squeezed
A vector split into the direction that gets stored and the two numbers that turn it back into what the client sent.

Enums§

Quant
How a vector set stores the direction of its vectors.

Functions§

norm
The euclidean length of a vector, to the bit.
raw
The bytes VEMB RAW writes for a stored direction.
restore
What VEMB says about a stored direction, which is the client’s vector back again as near as the quantisation kept it.
squeeze
Split v into what gets stored and what gets kept beside it.