embedvec 0.7.0

Fast, lightweight, in-process vector database with HNSW indexing, E8/H4 lattice quantization (up to 24.8x compression), metadata filtering, and PyO3 bindings
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//! Vector Storage Module
//!
//! ## Table of Contents
//! - **VectorStorage**: Main storage for vectors (raw f32, E8-quantized, or H4-quantized)
//! - **StoredVector**: Enum representing stored vector format
//! - **Storage operations**: add, get, clear, re-quantization

use crate::e8::{E8Codec, E8EncodedVector};
use crate::h4::{H4Codec, H4EncodedVector};
use crate::error::{EmbedVecError, Result};
use crate::quantization::Quantization;
use serde::{Deserialize, Serialize};

/// Stored vector representation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum StoredVector {
    /// Raw f32 vector (no quantization)
    Raw(Vec<f32>),
    /// E8-quantized vector (8D blocks, D8 ∪ D8+½ decomposition)
    E8(E8EncodedVector),
    /// H4-quantized vector (4D blocks, 600-cell vertex indices)
    H4(H4EncodedVector),
}

impl StoredVector {
    /// Get the raw f32 vector (decoding if necessary)
    pub fn to_f32(&self, e8_codec: Option<&E8Codec>, h4_codec: Option<&H4Codec>) -> Vec<f32> {
        match self {
            StoredVector::Raw(v) => v.clone(),
            StoredVector::E8(encoded) => {
                if let Some(c) = e8_codec {
                    c.decode(encoded)
                } else {
                    // Fallback: return zeros if no codec (shouldn't happen)
                    vec![0.0; encoded.points.len() * 8]
                }
            }
            StoredVector::H4(encoded) => {
                if let Some(c) = h4_codec {
                    c.decode(encoded)
                } else {
                    // Fallback: return zeros if no codec (shouldn't happen)
                    vec![0.0; encoded.indices.len() * 4]
                }
            }
        }
    }

    /// Get size in bytes
    pub fn size_bytes(&self) -> usize {
        match self {
            StoredVector::Raw(v) => v.len() * 4,
            StoredVector::E8(encoded) => encoded.size_bytes(),
            StoredVector::H4(encoded) => encoded.size_bytes(),
        }
    }
}

/// Vector storage with optional quantization
///
/// Manages storage of vectors in either raw f32 format or E8-quantized format.
/// Supports dynamic re-quantization when changing modes.
#[derive(Debug)]
pub struct VectorStorage {
    /// Vector dimension
    dimension: usize,
    /// Stored vectors
    vectors: Vec<StoredVector>,
    /// Current quantization mode
    quantization: Quantization,
    /// Total memory usage in bytes
    memory_bytes: usize,
}

impl VectorStorage {
    /// Create new vector storage
    ///
    /// # Arguments
    /// * `dimension` - Vector dimension
    /// * `quantization` - Quantization mode
    pub fn new(dimension: usize, quantization: Quantization) -> Self {
        Self {
            dimension,
            vectors: Vec::new(),
            quantization,
            memory_bytes: 0,
        }
    }

    /// Add a vector to storage
    ///
    /// # Arguments
    /// * `vector` - Raw f32 vector to store
    /// * `e8_codec` - Optional E8 codec (required when quantization is E8)
    /// * `h4_codec` - Optional H4 codec (required when quantization is H4)
    ///
    /// # Returns
    /// Assigned vector ID
    pub fn add(
        &mut self,
        vector: &[f32],
        e8_codec: Option<&E8Codec>,
        h4_codec: Option<&H4Codec>,
    ) -> Result<usize> {
        let stored = match &self.quantization {
            Quantization::None => StoredVector::Raw(vector.to_vec()),
            Quantization::E8 { .. } => {
                if let Some(c) = e8_codec {
                    let encoded = c.encode(vector)?;
                    StoredVector::E8(encoded)
                } else {
                    return Err(EmbedVecError::QuantizationError(
                        "E8 codec required for E8 quantization".to_string(),
                    ));
                }
            }
            Quantization::H4 { .. } => {
                if let Some(c) = h4_codec {
                    let encoded = c.encode(vector)?;
                    StoredVector::H4(encoded)
                } else {
                    return Err(EmbedVecError::QuantizationError(
                        "H4 codec required for H4 quantization".to_string(),
                    ));
                }
            }
        };

        self.memory_bytes += stored.size_bytes();
        let id = self.vectors.len();
        self.vectors.push(stored);
        Ok(id)
    }

    /// Get a vector by ID
    ///
    /// # Arguments
    /// * `id` - Vector ID
    /// * `e8_codec` - Optional E8 codec for decoding
    /// * `h4_codec` - Optional H4 codec for decoding
    ///
    /// # Returns
    /// Raw f32 vector (decoded if quantized)
    #[inline]
    pub fn get(
        &self,
        id: usize,
        e8_codec: Option<&E8Codec>,
        h4_codec: Option<&H4Codec>,
    ) -> Result<Vec<f32>> {
        self.vectors
            .get(id)
            .map(|v| v.to_f32(e8_codec, h4_codec))
            .ok_or(EmbedVecError::VectorNotFound(id))
    }

    /// Get raw vector slice for unquantized storage (zero-copy)
    /// Returns None if vector is quantized or ID is invalid
    #[inline]
    pub fn get_raw_slice(&self, id: usize) -> Option<&[f32]> {
        match self.vectors.get(id) {
            Some(StoredVector::Raw(v)) => Some(v.as_slice()),
            _ => None,
        }
    }

    /// Get stored vector reference by ID
    #[inline]
    pub fn get_stored(&self, id: usize) -> Option<&StoredVector> {
        self.vectors.get(id)
    }
    
    /// Batch get multiple vectors by IDs (more efficient than individual gets)
    pub fn get_batch(
        &self,
        ids: &[usize],
        e8_codec: Option<&E8Codec>,
        h4_codec: Option<&H4Codec>,
    ) -> Vec<Option<Vec<f32>>> {
        ids.iter()
            .map(|&id| self.vectors.get(id).map(|v| v.to_f32(e8_codec, h4_codec)))
            .collect()
    }

    /// Get number of stored vectors
    pub fn len(&self) -> usize {
        self.vectors.len()
    }

    /// Check if storage is empty
    pub fn is_empty(&self) -> bool {
        self.vectors.is_empty()
    }

    /// Clear all vectors
    pub fn clear(&mut self) {
        self.vectors.clear();
        self.memory_bytes = 0;
    }

    /// Get total memory usage in bytes
    pub fn memory_bytes(&self) -> usize {
        self.memory_bytes
    }

    /// Get memory usage per vector (average)
    pub fn bytes_per_vector(&self) -> f32 {
        if self.vectors.is_empty() {
            0.0
        } else {
            self.memory_bytes as f32 / self.vectors.len() as f32
        }
    }

    /// Change quantization mode (re-quantizes all vectors)
    ///
    /// # Arguments
    /// * `new_quantization` - New quantization mode
    /// * `e8_codec` - E8 codec (required if switching to or from E8)
    /// * `h4_codec` - H4 codec (required if switching to or from H4)
    pub fn set_quantization(
        &mut self,
        new_quantization: Quantization,
        e8_codec: Option<&E8Codec>,
        h4_codec: Option<&H4Codec>,
    ) -> Result<()> {
        if self.quantization == new_quantization {
            return Ok(());
        }

        // Re-quantize all vectors
        let mut new_vectors = Vec::with_capacity(self.vectors.len());
        let mut new_memory = 0usize;

        for stored in &self.vectors {
            // First decode to f32 using whichever codec applies to current format
            let raw = stored.to_f32(e8_codec, h4_codec);

            // Then encode with new quantization
            let new_stored = match &new_quantization {
                Quantization::None => StoredVector::Raw(raw),
                Quantization::E8 { .. } => {
                    if let Some(c) = e8_codec {
                        let encoded = c.encode(&raw)?;
                        StoredVector::E8(encoded)
                    } else {
                        return Err(EmbedVecError::QuantizationError(
                            "E8 codec required for E8 quantization".to_string(),
                        ));
                    }
                }
                Quantization::H4 { .. } => {
                    if let Some(c) = h4_codec {
                        let encoded = c.encode(&raw)?;
                        StoredVector::H4(encoded)
                    } else {
                        return Err(EmbedVecError::QuantizationError(
                            "H4 codec required for H4 quantization".to_string(),
                        ));
                    }
                }
            };

            new_memory += new_stored.size_bytes();
            new_vectors.push(new_stored);
        }

        self.vectors = new_vectors;
        self.memory_bytes = new_memory;
        self.quantization = new_quantization;

        Ok(())
    }

    /// Get current quantization mode
    pub fn quantization(&self) -> &Quantization {
        &self.quantization
    }

    /// Get vector dimension
    pub fn dimension(&self) -> usize {
        self.dimension
    }

    /// Compute distance between query and stored vector
    ///
    /// Uses asymmetric distance for quantized vectors (query in f32, db decoded on-the-fly)
    pub fn compute_distance(
        &self,
        query: &[f32],
        id: usize,
        e8_codec: Option<&E8Codec>,
        h4_codec: Option<&H4Codec>,
        distance_fn: impl Fn(&[f32], &[f32]) -> f32,
    ) -> Result<f32> {
        let stored = self
            .vectors
            .get(id)
            .ok_or(EmbedVecError::VectorNotFound(id))?;

        match stored {
            StoredVector::Raw(v) => Ok(distance_fn(query, v)),
            StoredVector::E8(encoded) => {
                if let Some(c) = e8_codec {
                    let decoded = c.decode(encoded);
                    Ok(distance_fn(query, &decoded))
                } else {
                    Err(EmbedVecError::QuantizationError(
                        "E8 codec required for distance computation".to_string(),
                    ))
                }
            }
            StoredVector::H4(encoded) => {
                if let Some(c) = h4_codec {
                    let decoded = c.decode(encoded);
                    Ok(distance_fn(query, &decoded))
                } else {
                    Err(EmbedVecError::QuantizationError(
                        "H4 codec required for distance computation".to_string(),
                    ))
                }
            }
        }
    }

    /// Iterate over all vector IDs
    pub fn iter_ids(&self) -> impl Iterator<Item = usize> {
        0..self.vectors.len()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_raw_storage() {
        let mut storage = VectorStorage::new(4, Quantization::None);

        let v1 = vec![1.0, 2.0, 3.0, 4.0];
        let id = storage.add(&v1, None, None).unwrap();
        assert_eq!(id, 0);

        let retrieved = storage.get(0, None, None).unwrap();
        assert_eq!(retrieved, v1);
    }

    #[test]
    fn test_e8_storage() {
        use crate::e8::E8Codec;
        let codec = E8Codec::new(16, 10, true, 42);
        let mut storage = VectorStorage::new(16, Quantization::e8_default());

        let v1: Vec<f32> = (0..16).map(|i| i as f32 * 0.1).collect();
        let id = storage.add(&v1, Some(&codec), None).unwrap();
        assert_eq!(id, 0);

        let retrieved = storage.get(0, Some(&codec), None).unwrap();
        assert_eq!(retrieved.len(), 16);

        // Check that it's approximately equal (quantization introduces error)
        let mse: f32 = v1
            .iter()
            .zip(retrieved.iter())
            .map(|(a, b)| (a - b).powi(2))
            .sum::<f32>()
            / 16.0;
        assert!(mse < 1.0, "MSE too high: {}", mse);
    }

    #[test]
    fn test_h4_storage() {
        use crate::h4::H4Codec;
        let codec = H4Codec::new(16, true, 42);
        let mut storage = VectorStorage::new(16, Quantization::h4_default());

        let v1: Vec<f32> = (0..16).map(|i| (i as f32 * 0.3).sin()).collect();
        let id = storage.add(&v1, None, Some(&codec)).unwrap();
        assert_eq!(id, 0);

        let retrieved = storage.get(0, None, Some(&codec)).unwrap();
        assert_eq!(retrieved.len(), 16);

        // H4 is a lossy quantizer
        let mse: f32 = v1
            .iter()
            .zip(retrieved.iter())
            .map(|(a, b)| (a - b).powi(2))
            .sum::<f32>()
            / 16.0;
        assert!(mse < 1.0, "H4 MSE too high: {}", mse);
    }

    #[test]
    fn test_memory_tracking() {
        let mut storage = VectorStorage::new(768, Quantization::None);

        for _ in 0..10 {
            let v: Vec<f32> = vec![0.0; 768];
            storage.add(&v, None, None).unwrap();
        }

        assert_eq!(storage.memory_bytes(), 768 * 4 * 10);
    }

    #[test]
    fn test_clear() {
        let mut storage = VectorStorage::new(4, Quantization::None);
        storage.add(&[1.0, 2.0, 3.0, 4.0], None, None).unwrap();
        
        assert_eq!(storage.len(), 1);
        storage.clear();
        assert_eq!(storage.len(), 0);
        assert_eq!(storage.memory_bytes(), 0);
    }
}