oxirs-vec 0.2.4

Vector index abstractions for semantic similarity and AI-augmented querying
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
//! GPU-accelerated vector index implementations

use super::{GpuAccelerator, GpuConfig, GpuMemoryPool, GpuPerformanceStats};
use crate::{similarity::SimilarityMetric, Vector, VectorData};
use anyhow::{anyhow, Result};
use parking_lot::RwLock;
use std::collections::HashMap;
use std::sync::Arc;

/// GPU-accelerated vector index
#[derive(Debug)]
pub struct GpuVectorIndex {
    accelerator: Arc<GpuAccelerator>,
    vectors: Vec<Vector>,
    vector_data: Vec<f32>,
    dimension: usize,
    memory_pool: Arc<RwLock<GpuMemoryPool>>,
    uri_map: HashMap<String, usize>,
}

impl GpuVectorIndex {
    /// Create a new GPU vector index
    pub fn new(config: GpuConfig) -> Result<Self> {
        let accelerator = Arc::new(GpuAccelerator::new(config.clone())?);
        let memory_pool = Arc::new(RwLock::new(GpuMemoryPool::new(&config, 1024)?));

        Ok(Self {
            accelerator,
            vectors: Vec::new(),
            vector_data: Vec::new(),
            dimension: 0,
            memory_pool,
            uri_map: HashMap::new(),
        })
    }

    /// Add vectors to the index
    pub fn add_vectors(&mut self, vectors: Vec<Vector>) -> Result<()> {
        if vectors.is_empty() {
            return Ok(());
        }

        // Set dimension from first vector if not set
        if self.dimension == 0 {
            self.dimension = vectors[0].dimensions;
        }

        // Validate all vectors have the same dimension
        for vector in &vectors {
            if vector.dimensions != self.dimension {
                return Err(anyhow!(
                    "Vector dimension mismatch: expected {}, got {}",
                    self.dimension,
                    vector.dimensions
                ));
            }
        }

        // Flatten vector data for GPU processing
        for vector in &vectors {
            match &vector.values {
                VectorData::F32(data) => self.vector_data.extend(data),
                VectorData::F64(data) => {
                    // Convert f64 to f32 for GPU processing
                    self.vector_data.extend(data.iter().map(|&x| x as f32));
                }
                _ => return Err(anyhow!("Unsupported vector precision for GPU processing")),
            }
        }

        self.vectors.extend(vectors);
        Ok(())
    }

    /// Search for similar vectors
    pub fn search(
        &self,
        query: &Vector,
        k: usize,
        metric: SimilarityMetric,
    ) -> Result<Vec<(usize, f32)>> {
        if self.vectors.is_empty() {
            return Ok(Vec::new());
        }

        let query_data = match &query.values {
            VectorData::F32(data) => data.clone(),
            VectorData::F64(data) => data.iter().map(|&x| x as f32).collect(),
            _ => {
                return Err(anyhow!(
                    "Unsupported query vector precision for GPU processing"
                ))
            }
        };

        if query.dimensions != self.dimension {
            return Err(anyhow!(
                "Query dimension mismatch: expected {}, got {}",
                self.dimension,
                query.dimensions
            ));
        }

        // Compute similarities using GPU
        let similarities = self.accelerator.compute_similarity(
            &query_data,
            &self.vector_data,
            1,
            self.vectors.len(),
            self.dimension,
            metric,
        )?;

        // Sort and return top-k results
        let mut results: Vec<(usize, f32)> = similarities.into_iter().enumerate().collect();

        // Sort by similarity (descending for similarity, ascending for distance)
        match metric {
            SimilarityMetric::Cosine | SimilarityMetric::Pearson | SimilarityMetric::Jaccard => {
                results.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
            }
            _ => {
                results.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));
            }
        }

        results.truncate(k);
        Ok(results)
    }

    /// Batch search for multiple queries
    pub fn batch_search(
        &self,
        queries: &[Vector],
        k: usize,
        metric: SimilarityMetric,
    ) -> Result<Vec<Vec<(usize, f32)>>> {
        let mut results = Vec::new();

        for query in queries {
            let query_results = self.search(query, k, metric)?;
            results.push(query_results);
        }

        Ok(results)
    }

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

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

    /// Get the dimension of vectors in this index
    pub fn dimension(&self) -> usize {
        self.dimension
    }

    /// Get performance statistics
    pub fn performance_stats(&self) -> Arc<parking_lot::RwLock<GpuPerformanceStats>> {
        self.accelerator.performance_stats()
    }

    /// Clear the index
    pub fn clear(&mut self) {
        self.vectors.clear();
        self.vector_data.clear();
        self.dimension = 0;
        self.accelerator.reset_stats();
    }
}

impl crate::VectorIndex for GpuVectorIndex {
    fn insert(&mut self, uri: String, vector: crate::Vector) -> Result<()> {
        // Store the URI mapping
        let index = self.vectors.len();
        self.uri_map.insert(uri, index);
        self.add_vectors(vec![vector])?;
        Ok(())
    }

    fn search_knn(&self, query: &crate::Vector, k: usize) -> Result<Vec<(String, f32)>> {
        let results = self.search(query, k, SimilarityMetric::Cosine)?;
        Ok(results
            .into_iter()
            .filter_map(|(index, score)| {
                // Find URI by index (reverse lookup)
                self.uri_map
                    .iter()
                    .find(|&(_, &idx)| idx == index)
                    .map(|(uri, _)| (uri.clone(), score))
            })
            .collect())
    }

    fn search_threshold(
        &self,
        query: &crate::Vector,
        threshold: f32,
    ) -> Result<Vec<(String, f32)>> {
        // Use large k to get many candidates, then filter by threshold
        let results = self.search(query, 1000, SimilarityMetric::Cosine)?;
        Ok(results
            .into_iter()
            .filter(|(_, score)| *score >= threshold)
            .filter_map(|(index, score)| {
                self.uri_map
                    .iter()
                    .find(|&(_, &idx)| idx == index)
                    .map(|(uri, _)| (uri.clone(), score))
            })
            .collect())
    }

    fn get_vector(&self, uri: &str) -> Option<&crate::Vector> {
        if let Some(&index) = self.uri_map.get(uri) {
            self.vectors.get(index)
        } else {
            None
        }
    }
}

/// Advanced GPU vector index with additional optimizations
#[derive(Debug)]
pub struct AdvancedGpuVectorIndex {
    base_index: GpuVectorIndex,
    enable_quantization: bool,
    quantization_bits: u8,
    use_tensor_cores: bool,
}

impl AdvancedGpuVectorIndex {
    /// Create a new advanced GPU vector index
    pub fn new(mut config: GpuConfig) -> Result<Self> {
        config.enable_tensor_cores = true;
        config.enable_mixed_precision = true;

        let base_index = GpuVectorIndex::new(config)?;

        Ok(Self {
            base_index,
            enable_quantization: false,
            quantization_bits: 8,
            use_tensor_cores: true,
        })
    }

    /// Enable quantization for memory efficiency
    pub fn enable_quantization(&mut self, bits: u8) {
        self.enable_quantization = true;
        self.quantization_bits = bits;
    }

    /// Optimized batch processing for large-scale operations
    pub fn batch_process(
        &self,
        queries: &[Vector],
        batch_size: usize,
        k: usize,
        metric: SimilarityMetric,
    ) -> Result<Vec<Vec<(usize, f32)>>> {
        let mut all_results = Vec::new();

        for batch in queries.chunks(batch_size) {
            let batch_results = self.base_index.batch_search(batch, k, metric)?;
            all_results.extend(batch_results);
        }

        Ok(all_results)
    }

    /// Get memory usage statistics
    pub fn memory_stats(&self) -> Result<MemoryUsageStats> {
        let device = self.base_index.accelerator.device();
        let pool_stats = self.base_index.memory_pool.read().stats();

        Ok(MemoryUsageStats {
            total_gpu_memory: device.total_memory,
            free_gpu_memory: device.free_memory,
            used_by_index: pool_stats.used_memory,
            vector_count: self.base_index.len(),
            dimension: self.base_index.dimension(),
            memory_per_vector: if !self.base_index.is_empty() {
                pool_stats.used_memory / self.base_index.len()
            } else {
                0
            },
        })
    }
}

/// Memory usage statistics for GPU vector index
#[derive(Debug, Clone)]
pub struct MemoryUsageStats {
    pub total_gpu_memory: usize,
    pub free_gpu_memory: usize,
    pub used_by_index: usize,
    pub vector_count: usize,
    pub dimension: usize,
    pub memory_per_vector: usize,
}

impl MemoryUsageStats {
    /// Get memory utilization percentage
    pub fn utilization(&self) -> f64 {
        if self.total_gpu_memory > 0 {
            (self.total_gpu_memory - self.free_gpu_memory) as f64 / self.total_gpu_memory as f64
        } else {
            0.0
        }
    }

    /// Print memory statistics
    pub fn print(&self) {
        println!("GPU Vector Index Memory Usage:");
        println!(
            "  Total GPU Memory: {:.2} GB",
            self.total_gpu_memory as f64 / 1024.0 / 1024.0 / 1024.0
        );
        println!(
            "  Free GPU Memory: {:.2} GB",
            self.free_gpu_memory as f64 / 1024.0 / 1024.0 / 1024.0
        );
        println!(
            "  Used by Index: {:.2} MB",
            self.used_by_index as f64 / 1024.0 / 1024.0
        );
        println!("  Vectors: {} ({}D)", self.vector_count, self.dimension);
        println!(
            "  Memory per Vector: {:.2} KB",
            self.memory_per_vector as f64 / 1024.0
        );
        println!("  GPU Utilization: {:.1}%", self.utilization() * 100.0);
    }
}

/// Batch vector processor for high-throughput operations
#[derive(Debug)]
pub struct BatchVectorProcessor {
    accelerator: Arc<GpuAccelerator>,
    batch_size: usize,
    max_concurrent_batches: usize,
}

impl BatchVectorProcessor {
    /// Create a new batch vector processor
    pub fn new(config: GpuConfig, batch_size: usize) -> Result<Self> {
        let accelerator = Arc::new(GpuAccelerator::new(config)?);
        let max_concurrent_batches = 4; // Adjust based on GPU memory

        Ok(Self {
            accelerator,
            batch_size,
            max_concurrent_batches,
        })
    }

    /// Process vectors in batches with specified operation
    pub fn process_batches<F, R>(&self, vectors: &[Vector], operation: F) -> Result<Vec<R>>
    where
        F: Fn(&[Vector]) -> Result<Vec<R>> + Send + Sync,
        R: Send,
    {
        let mut results = Vec::new();

        for batch in vectors.chunks(self.batch_size) {
            let batch_results = operation(batch)?;
            results.extend(batch_results);
        }

        Ok(results)
    }

    /// Parallel batch processing using multiple streams
    pub fn parallel_process_batches<F, R>(&self, vectors: &[Vector], operation: F) -> Result<Vec<R>>
    where
        F: Fn(&[Vector]) -> Result<Vec<R>> + Send + Sync + Clone + 'static,
        R: Send + 'static,
    {
        use std::thread;

        let chunks: Vec<&[Vector]> = vectors.chunks(self.batch_size).collect();
        let mut handles = Vec::new();
        let mut results = Vec::new();

        for chunk_batch in chunks.chunks(self.max_concurrent_batches) {
            for chunk in chunk_batch {
                let chunk_vec = chunk.to_vec();
                let op = operation.clone();

                let handle = thread::spawn(move || op(&chunk_vec));
                handles.push(handle);
            }

            // Collect results from this batch of threads
            for handle in handles.drain(..) {
                match handle.join() {
                    Ok(Ok(batch_results)) => results.extend(batch_results),
                    Ok(Err(e)) => return Err(e),
                    Err(_) => return Err(anyhow!("Thread panicked during batch processing")),
                }
            }
        }

        Ok(results)
    }

    /// Get processing throughput (vectors per second)
    pub fn throughput(&self, vectors_processed: usize, duration: std::time::Duration) -> f64 {
        if duration.as_secs_f64() > 0.0 {
            vectors_processed as f64 / duration.as_secs_f64()
        } else {
            0.0
        }
    }
}