Quantize / dequantize
# grade this stage (from your course directory)
npx theinference test --stage quantize --watchBuild 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
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.
export function runQuantize(vectors: number[][], quantile: number): number[] {
// bounds = computeBounds(vectors, quantile)
// return [bounds.offset, bounds.alpha, ...flatten(quantizeVec(v, bounds))]
}
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.
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.