Build a `CollectionCodec` from a quantization tag and training vectors. Returns `None` for unsupported or unrecognised tags (e.g. "sq8", "pq", "none") — those variants use separate per-segment code paths.
(
quantization: &str,
vectors: &[Vec<f32>],
dim: usize,
m: usize,
ef_construction: usize,
seed: u64,
)
| 67 | /// Returns `None` for unsupported or unrecognised tags (e.g. "sq8", "pq", |
| 68 | /// "none") — those variants use separate per-segment code paths. |
| 69 | pub fn build_collection_codec( |
| 70 | quantization: &str, |
| 71 | vectors: &[Vec<f32>], |
| 72 | dim: usize, |
| 73 | m: usize, |
| 74 | ef_construction: usize, |
| 75 | seed: u64, |
| 76 | ) -> Option<CollectionCodec> { |
| 77 | if vectors.is_empty() { |
| 78 | return None; |
| 79 | } |
| 80 | let refs: Vec<&[f32]> = vectors.iter().map(|v| v.as_slice()).collect(); |
| 81 | match quantization { |
| 82 | "rabitq" => { |
| 83 | let codec = RaBitQCodec::calibrate(&refs, dim, seed); |
| 84 | let mut idx = HnswCodecIndex::new(dim, m, ef_construction, codec, seed); |
| 85 | for (i, v) in vectors.iter().enumerate() { |
| 86 | idx.insert(i as u32, v); |
| 87 | } |
| 88 | Some(CollectionCodec::RaBitQ(idx)) |
| 89 | } |
| 90 | "bbq" => { |
| 91 | let codec = BbqCodec::calibrate(&refs, dim, 3); |
| 92 | let mut idx = HnswCodecIndex::new(dim, m, ef_construction, codec, seed); |
| 93 | for (i, v) in vectors.iter().enumerate() { |
| 94 | idx.insert(i as u32, v); |
| 95 | } |
| 96 | Some(CollectionCodec::Bbq(idx)) |
| 97 | } |
| 98 | _ => None, |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | #[cfg(test)] |
| 103 | mod tests { |