Quantize a single FP32 vector to INT8.
(&self, vector: &[f32])
| 87 | |
| 88 | /// Quantize a single FP32 vector to INT8. |
| 89 | pub fn quantize(&self, vector: &[f32]) -> Vec<u8> { |
| 90 | debug_assert_eq!(vector.len(), self.dim); |
| 91 | let mut out = Vec::with_capacity(self.dim); |
| 92 | for ((&v, &min), (&max, &inv_scale)) in vector |
| 93 | .iter() |
| 94 | .zip(self.mins.iter()) |
| 95 | .zip(self.maxs.iter().zip(self.inv_scales.iter())) |
| 96 | { |
| 97 | let clamped = v.clamp(min, max); |
| 98 | let q = ((clamped - min) * inv_scale).round() as u8; |
| 99 | out.push(q); |
| 100 | } |
| 101 | out |
| 102 | } |
| 103 | |
| 104 | /// Batch quantize: quantize all vectors into a contiguous byte array. |
| 105 | /// |