(vec: number[])
| 66 | |
| 67 | /** Symmetric per-vector int8 quantization. */ |
| 68 | export function quantize(vec: number[]): QuantizedVec { |
| 69 | let max = 0; |
| 70 | for (const v of vec) { const a = Math.abs(v); if (a > max) max = a; } |
| 71 | const scale = max === 0 ? 1 : max / 127; |
| 72 | const bytes = new Int8Array(vec.length); |
| 73 | for (let i = 0; i < vec.length; i++) { |
| 74 | const q = Math.round(vec[i]! / scale); |
| 75 | bytes[i] = q > 127 ? 127 : q < -127 ? -127 : q; |
| 76 | } |
| 77 | return { scale, bytes }; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Cosine similarity between a (full-precision) query vector and a quantized |
no test coverage detected