ruvector-core 2.2.0

High-performance Rust vector database core with HNSW indexing
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
417
418
419
420
421
422
423
424
425
426
427
//! # Neural Hash Functions
//!
//! Learn similarity-preserving binary projections for extreme compression.
//! Achieves 32-128x compression with 90-95% recall preservation.

use crate::types::VectorId;
use ndarray::{Array1, Array2};
use rand::Rng;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Neural hash function for similarity-preserving binary codes
pub trait NeuralHash {
    /// Encode a vector to binary code
    fn encode(&self, vector: &[f32]) -> Vec<u8>;

    /// Compute Hamming distance between two codes
    fn hamming_distance(&self, code_a: &[u8], code_b: &[u8]) -> u32;

    /// Estimate similarity from Hamming distance
    fn estimate_similarity(&self, hamming_dist: u32, code_bits: usize) -> f32;
}

/// Deep hash embedding with learned projections
#[derive(Clone, Serialize, Deserialize)]
pub struct DeepHashEmbedding {
    /// Projection matrices for each layer
    projections: Vec<Array2<f32>>,
    /// Biases for each layer
    biases: Vec<Array1<f32>>,
    /// Number of output bits
    output_bits: usize,
    /// Input dimensions
    input_dims: usize,
}

impl DeepHashEmbedding {
    /// Create a new deep hash embedding
    pub fn new(input_dims: usize, hidden_dims: Vec<usize>, output_bits: usize) -> Self {
        let mut rng = rand::thread_rng();
        let mut projections = Vec::new();
        let mut biases = Vec::new();

        let mut layer_dims = vec![input_dims];
        layer_dims.extend(&hidden_dims);
        layer_dims.push(output_bits);

        // Initialize random projections (Xavier initialization)
        for i in 0..layer_dims.len() - 1 {
            let in_dim = layer_dims[i];
            let out_dim = layer_dims[i + 1];

            let scale = (2.0 / (in_dim + out_dim) as f32).sqrt();
            let proj = Array2::from_shape_fn((out_dim, in_dim), |_| {
                rng.gen::<f32>() * 2.0 * scale - scale
            });

            let bias = Array1::zeros(out_dim);

            projections.push(proj);
            biases.push(bias);
        }

        Self {
            projections,
            biases,
            output_bits,
            input_dims,
        }
    }

    /// Forward pass through the network
    fn forward(&self, input: &[f32]) -> Vec<f32> {
        let mut activations = Array1::from_vec(input.to_vec());

        for (proj, bias) in self.projections.iter().zip(self.biases.iter()) {
            // Linear layer: y = Wx + b
            activations = proj.dot(&activations) + bias;

            // ReLU activation (except last layer)
            if proj.nrows() != self.output_bits {
                activations.mapv_inplace(|x| x.max(0.0));
            }
        }

        activations.to_vec()
    }

    /// Train on pairs of similar/dissimilar examples
    pub fn train(
        &mut self,
        positive_pairs: &[(Vec<f32>, Vec<f32>)],
        negative_pairs: &[(Vec<f32>, Vec<f32>)],
        learning_rate: f32,
        epochs: usize,
    ) {
        // Simplified training with contrastive loss
        // Production would use proper backpropagation
        for _ in 0..epochs {
            // Positive pairs should have small Hamming distance
            for (a, b) in positive_pairs {
                let code_a = self.encode(a);
                let code_b = self.encode(b);
                let dist = self.hamming_distance(&code_a, &code_b);

                // If distance is too large, update towards similarity
                if dist as f32 > self.output_bits as f32 * 0.3 {
                    self.update_weights(a, b, learning_rate, true);
                }
            }

            // Negative pairs should have large Hamming distance
            for (a, b) in negative_pairs {
                let code_a = self.encode(a);
                let code_b = self.encode(b);
                let dist = self.hamming_distance(&code_a, &code_b);

                // If distance is too small, update towards dissimilarity
                if (dist as f32) < self.output_bits as f32 * 0.6 {
                    self.update_weights(a, b, learning_rate, false);
                }
            }
        }
    }

    fn update_weights(&mut self, a: &[f32], b: &[f32], lr: f32, attract: bool) {
        // Simplified gradient update (production would use proper autodiff)
        let direction = if attract { 1.0 } else { -1.0 };

        // Update only the last layer for simplicity
        if let Some(last_proj) = self.projections.last_mut() {
            let a_arr = Array1::from_vec(a.to_vec());
            let b_arr = Array1::from_vec(b.to_vec());

            for i in 0..last_proj.nrows() {
                for j in 0..last_proj.ncols() {
                    let grad = direction * lr * (a_arr[j] - b_arr[j]);
                    last_proj[[i, j]] += grad * 0.001; // Small update
                }
            }
        }
    }

    /// Get dimensions
    pub fn dimensions(&self) -> (usize, usize) {
        (self.input_dims, self.output_bits)
    }
}

impl NeuralHash for DeepHashEmbedding {
    fn encode(&self, vector: &[f32]) -> Vec<u8> {
        if vector.len() != self.input_dims {
            return vec![0; self.output_bits.div_ceil(8)];
        }

        let logits = self.forward(vector);

        // Threshold at 0 to get binary codes
        let mut bits = vec![0u8; self.output_bits.div_ceil(8)];

        for (i, &logit) in logits.iter().enumerate() {
            if logit > 0.0 {
                let byte_idx = i / 8;
                let bit_idx = i % 8;
                bits[byte_idx] |= 1 << bit_idx;
            }
        }

        bits
    }

    fn hamming_distance(&self, code_a: &[u8], code_b: &[u8]) -> u32 {
        code_a
            .iter()
            .zip(code_b.iter())
            .map(|(a, b)| (a ^ b).count_ones())
            .sum()
    }

    fn estimate_similarity(&self, hamming_dist: u32, code_bits: usize) -> f32 {
        // Convert Hamming distance to approximate cosine similarity
        let normalized_dist = hamming_dist as f32 / code_bits as f32;
        1.0 - 2.0 * normalized_dist
    }
}

/// Simple LSH (Locality Sensitive Hashing) baseline
#[derive(Clone, Serialize, Deserialize)]
pub struct SimpleLSH {
    /// Random projection vectors
    projections: Array2<f32>,
    /// Number of hash bits
    num_bits: usize,
}

impl SimpleLSH {
    /// Create a new LSH with random projections
    pub fn new(input_dims: usize, num_bits: usize) -> Self {
        let mut rng = rand::thread_rng();

        // Random Gaussian projections
        let projections =
            Array2::from_shape_fn((num_bits, input_dims), |_| rng.gen::<f32>() * 2.0 - 1.0);

        Self {
            projections,
            num_bits,
        }
    }
}

impl NeuralHash for SimpleLSH {
    fn encode(&self, vector: &[f32]) -> Vec<u8> {
        let input = Array1::from_vec(vector.to_vec());
        let projections = self.projections.dot(&input);

        let mut bits = vec![0u8; self.num_bits.div_ceil(8)];

        for (i, &val) in projections.iter().enumerate() {
            if val > 0.0 {
                let byte_idx = i / 8;
                let bit_idx = i % 8;
                bits[byte_idx] |= 1 << bit_idx;
            }
        }

        bits
    }

    fn hamming_distance(&self, code_a: &[u8], code_b: &[u8]) -> u32 {
        code_a
            .iter()
            .zip(code_b.iter())
            .map(|(a, b)| (a ^ b).count_ones())
            .sum()
    }

    fn estimate_similarity(&self, hamming_dist: u32, code_bits: usize) -> f32 {
        let normalized_dist = hamming_dist as f32 / code_bits as f32;
        1.0 - 2.0 * normalized_dist
    }
}

/// Hash index for fast approximate nearest neighbor search
pub struct HashIndex<H: NeuralHash + Clone> {
    /// Hash function
    hasher: H,
    /// Hash tables: binary code -> list of vector IDs
    tables: HashMap<Vec<u8>, Vec<VectorId>>,
    /// Original vectors for verification
    vectors: HashMap<VectorId, Vec<f32>>,
    /// Code bits
    code_bits: usize,
}

impl<H: NeuralHash + Clone> HashIndex<H> {
    /// Create a new hash index
    pub fn new(hasher: H, code_bits: usize) -> Self {
        Self {
            hasher,
            tables: HashMap::new(),
            vectors: HashMap::new(),
            code_bits,
        }
    }

    /// Insert a vector
    pub fn insert(&mut self, id: VectorId, vector: Vec<f32>) {
        let code = self.hasher.encode(&vector);

        self.tables.entry(code).or_default().push(id.clone());

        self.vectors.insert(id, vector);
    }

    /// Search for approximate nearest neighbors
    pub fn search(&self, query: &[f32], k: usize, max_hamming: u32) -> Vec<(VectorId, f32)> {
        let query_code = self.hasher.encode(query);

        let mut candidates = Vec::new();

        // Find all vectors within Hamming distance threshold
        for (code, ids) in &self.tables {
            let hamming = self.hasher.hamming_distance(&query_code, code);

            if hamming <= max_hamming {
                for id in ids {
                    if let Some(vec) = self.vectors.get(id) {
                        let similarity = cosine_similarity(query, vec);
                        candidates.push((id.clone(), similarity));
                    }
                }
            }
        }

        // Sort by similarity and return top-k
        candidates.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap());
        candidates.truncate(k);
        candidates
    }

    /// Get compression ratio
    pub fn compression_ratio(&self) -> f32 {
        if self.vectors.is_empty() {
            return 0.0;
        }

        let original_size: usize = self
            .vectors
            .values()
            .map(|v| v.len() * std::mem::size_of::<f32>())
            .sum();

        let compressed_size = self.tables.len() * self.code_bits.div_ceil(8);

        original_size as f32 / compressed_size as f32
    }

    /// Get statistics
    pub fn stats(&self) -> HashIndexStats {
        let buckets = self.tables.len();
        let total_vectors = self.vectors.len();
        let avg_bucket_size = if buckets > 0 {
            total_vectors as f32 / buckets as f32
        } else {
            0.0
        };

        HashIndexStats {
            total_vectors,
            num_buckets: buckets,
            avg_bucket_size,
            compression_ratio: self.compression_ratio(),
        }
    }
}

/// Hash index statistics
#[derive(Debug, Clone)]
pub struct HashIndexStats {
    pub total_vectors: usize,
    pub num_buckets: usize,
    pub avg_bucket_size: f32,
    pub compression_ratio: f32,
}

fn cosine_similarity(a: &[f32], b: &[f32]) -> f32 {
    let dot: f32 = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum();
    let norm_a: f32 = a.iter().map(|x| x * x).sum::<f32>().sqrt();
    let norm_b: f32 = b.iter().map(|x| x * x).sum::<f32>().sqrt();

    if norm_a > 0.0 && norm_b > 0.0 {
        dot / (norm_a * norm_b)
    } else {
        0.0
    }
}

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

    #[test]
    fn test_deep_hash_encoding() {
        let hash = DeepHashEmbedding::new(4, vec![8], 16);
        let vector = vec![0.1, 0.2, 0.3, 0.4];

        let code = hash.encode(&vector);
        assert_eq!(code.len(), 2); // 16 bits = 2 bytes
    }

    #[test]
    fn test_hamming_distance() {
        let hash = DeepHashEmbedding::new(2, vec![], 8);

        let code_a = vec![0b10101010];
        let code_b = vec![0b11001100];

        let dist = hash.hamming_distance(&code_a, &code_b);
        assert_eq!(dist, 4); // 4 bits differ
    }

    #[test]
    fn test_lsh_encoding() {
        let lsh = SimpleLSH::new(4, 16);
        let vector = vec![1.0, 2.0, 3.0, 4.0];

        let code = lsh.encode(&vector);
        assert_eq!(code.len(), 2);

        // Same vector should produce same code
        let code2 = lsh.encode(&vector);
        assert_eq!(code, code2);
    }

    #[test]
    fn test_hash_index() {
        let lsh = SimpleLSH::new(3, 8);
        let mut index = HashIndex::new(lsh, 8);

        // Insert vectors
        index.insert("0".to_string(), vec![1.0, 0.0, 0.0]);
        index.insert("1".to_string(), vec![0.9, 0.1, 0.0]);
        index.insert("2".to_string(), vec![0.0, 1.0, 0.0]);

        // Search
        let results = index.search(&[1.0, 0.0, 0.0], 2, 4);

        assert!(!results.is_empty());
        let stats = index.stats();
        assert_eq!(stats.total_vectors, 3);
    }

    #[test]
    fn test_compression_ratio() {
        let lsh = SimpleLSH::new(128, 32); // 128D -> 32 bits
        let mut index = HashIndex::new(lsh, 32);

        for i in 0..10 {
            let vec: Vec<f32> = (0..128).map(|j| (i + j) as f32 / 128.0).collect();
            index.insert(i.to_string(), vec);
        }

        let ratio = index.compression_ratio();
        assert!(ratio > 1.0); // Should have compression
    }
}