Skip to main content

xz_embed/quantize/
mod.rs

1pub mod product;
2pub mod scalar;
3
4pub use product::ProductQuantizer;
5pub use scalar::ScalarQuantizer;
6
7use std::fmt::Debug;
8
9/// 向量量化 trait
10pub trait VectorQuantizer: Send + Sync + Debug {
11    /// 压缩向量(float → quantized)
12    fn compress(&self, vectors: &[Vec<f32>]) -> Vec<Vec<u8>>;
13
14    /// 解压向量(quantized → float)
15    fn decompress(&self, quantized: &[Vec<u8>]) -> Vec<Vec<f32>>;
16}
17
18/// 空量化器(不做量化)
19#[derive(Debug)]
20pub struct NoopQuantizer;
21
22impl VectorQuantizer for NoopQuantizer {
23    fn compress(&self, vectors: &[Vec<f32>]) -> Vec<Vec<u8>> {
24        vectors.iter().map(|v| v.iter().flat_map(|f| f.to_le_bytes()).collect()).collect()
25    }
26
27    fn decompress(&self, quantized: &[Vec<u8>]) -> Vec<Vec<f32>> {
28        quantized
29            .iter()
30            .map(|bytes| {
31                bytes
32                    .chunks(4)
33                    .map(|c| {
34                        let arr: [u8; 4] = c.try_into().unwrap_or([0; 4]);
35                        f32::from_le_bytes(arr)
36                    })
37                    .collect()
38            })
39            .collect()
40    }
41}