Build your own vector database
Scalar (int8) quantization·18 min

Quantize / dequantize

0 / 1
# grade this stage (from your course directory)
npx theinference test --stage quantize --watch

Build the codec. computeBounds finds the quantile-clipped value range; quantizeVec maps a float32 vector to Int8Array; dequantizeVec maps it back. The round-trip is lossy but bounded by alpha.

What to build

Your task

Implement runQuantize

Implement computeBounds(rows, quantile), quantizeVec(vec, bounds), and dequantizeVec(q, bounds) in src/quantize.ts. The adapter flattens the result so it can be graded: it returns [offset, alpha, ...all int8 codes] in one row.

tests/adapters.ts
export function runQuantize(vectors: number[][], quantile: number): number[] {
  // bounds = computeBounds(vectors, quantile)
  // return [bounds.offset, bounds.alpha, ...flatten(quantizeVec(v, bounds))]
}
Quantile bounds clip outliers

computeBounds sorts all entries and takes the value at the quantile (and 1 - quantile) positions as hi/lo, so a few extreme values do not stretch the whole range and waste codes. offset = lo, alpha = (hi - lo) / 255.

How it's graded

Adapter mode: [offset, alpha, ...codes] compared with allclose, atol 1e-4, rtol 1e-3. The int8 codes are integers (exact within tolerance); offset/alpha are floats.