rag-plusplus-core 0.1.0

High-performance retrieval engine with SIMD-accelerated vector search and trajectory memory
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
//! End-to-End Benchmarks for RAG++ SOTA Improvements
//!
//! Compares:
//! - Dense-only vs Hybrid (Dense + Sparse BM25) retrieval
//! - Memory usage: Full f32 vs SQ8 (4x) vs PQ (32-128x)
//! - Index construction: Sequential vs Parallel HNSW
//!
//! Run with: cargo bench --bench e2e_benchmark

use std::collections::HashSet;
use std::time::{Duration, Instant};

use criterion::{
    black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput,
};
use rand::prelude::*;

use rag_plusplus_core::{
    // Indexes
    FlatIndex, HNSWConfig, HNSWIndex, IndexConfig, VectorIndex,
    ParallelHNSWBuilder, ParallelHNSWIndex,
    // Sparse / Hybrid
    BM25Index, HybridSearcher, HybridFusionStrategy, HybridSearchConfig,
    // Quantization
    SQ8Quantizer, PQQuantizer, PQConfig, Quantizer, AsymmetricDistance,
    // Evaluation
    Evaluator, EvaluationSummary,
};

// =============================================================================
// DATASET GENERATORS
// =============================================================================

/// Generate random normalized vectors.
fn generate_vectors(n: usize, dim: usize, seed: u64) -> Vec<Vec<f32>> {
    let mut rng = rand::rngs::StdRng::seed_from_u64(seed);
    (0..n)
        .map(|_| {
            let v: Vec<f32> = (0..dim).map(|_| rng.gen::<f32>() - 0.5).collect();
            let norm: f32 = v.iter().map(|x| x * x).sum::<f32>().sqrt();
            if norm > 0.0 {
                v.into_iter().map(|x| x / norm).collect()
            } else {
                v
            }
        })
        .collect()
}

/// Generate text documents for BM25.
fn generate_documents(n: usize, seed: u64) -> Vec<String> {
    let vocabulary = [
        "machine", "learning", "deep", "neural", "network", "algorithm",
        "training", "model", "data", "feature", "optimization", "gradient",
        "loss", "accuracy", "precision", "recall", "transformer", "attention",
        "embedding", "vector", "retrieval", "search", "index", "query",
        "reinforcement", "policy", "agent", "environment", "reward", "state",
        "action", "value", "function", "approximation", "convergence", "batch",
        "epoch", "layer", "activation", "dropout", "regularization", "inference",
    ];

    let mut rng = rand::rngs::StdRng::seed_from_u64(seed);
    (0..n)
        .map(|_| {
            let num_words = rng.gen_range(5..20);
            (0..num_words)
                .map(|_| vocabulary[rng.gen_range(0..vocabulary.len())])
                .collect::<Vec<_>>()
                .join(" ")
        })
        .collect()
}

/// Generate ground truth: for each query, the k-nearest neighbors.
fn compute_ground_truth(
    corpus: &[Vec<f32>],
    queries: &[Vec<f32>],
    k: usize,
) -> Vec<HashSet<String>> {
    queries
        .iter()
        .map(|query| {
            let mut distances: Vec<(usize, f32)> = corpus
                .iter()
                .enumerate()
                .map(|(i, doc)| {
                    let dist: f32 = query
                        .iter()
                        .zip(doc.iter())
                        .map(|(a, b)| (a - b).powi(2))
                        .sum();
                    (i, dist)
                })
                .collect();
            distances.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap());
            distances
                .into_iter()
                .take(k)
                .map(|(i, _)| format!("doc-{i}"))
                .collect()
        })
        .collect()
}

// =============================================================================
// BENCHMARK: DENSE VS HYBRID RETRIEVAL
// =============================================================================

fn bench_dense_vs_hybrid(c: &mut Criterion) {
    let mut group = c.benchmark_group("retrieval/dense_vs_hybrid");
    group.sample_size(30);

    let n_docs = 10000;
    let n_queries = 100;
    let dim = 128;
    let k = 10;

    // Generate data
    let corpus = generate_vectors(n_docs, dim, 42);
    let documents = generate_documents(n_docs, 42);
    let queries = generate_vectors(n_queries, dim, 123);
    let query_texts: Vec<String> = (0..n_queries)
        .map(|i| {
            // Create query text that matches some documents
            let words = ["machine learning", "neural network", "deep learning", "retrieval search"];
            words[i % words.len()].to_string()
        })
        .collect();

    // Build dense index
    let mut dense_index = FlatIndex::new(IndexConfig::new(dim));
    for (i, vec) in corpus.iter().enumerate() {
        dense_index.add(format!("doc-{i}"), vec).unwrap();
    }

    // Build sparse index
    let mut sparse_index = BM25Index::new();
    for (i, doc) in documents.iter().enumerate() {
        sparse_index.add(format!("doc-{i}"), doc);
    }

    // Compute ground truth for recall evaluation
    let ground_truth = compute_ground_truth(&corpus, &queries, k);

    // === Dense-only benchmark ===
    group.throughput(Throughput::Elements(n_queries as u64));
    group.bench_function("dense_only", |bench| {
        bench.iter(|| {
            for query in &queries {
                let _ = dense_index.search(black_box(query), k);
            }
        })
    });

    // === Hybrid (RRF) benchmark ===
    group.bench_function("hybrid_rrf", |bench| {
        let config = HybridSearchConfig {
            strategy: HybridFusionStrategy::RRF { k: 60.0 },
            candidates_per_index: 50,
            min_sparse_score: 0.0,
        };
        let searcher = HybridSearcher::new(&dense_index, &sparse_index).with_config(config);

        bench.iter(|| {
            for (query, text) in queries.iter().zip(query_texts.iter()) {
                let _ = searcher.search(black_box(query), black_box(text), k);
            }
        })
    });

    // === Hybrid (Linear) benchmark ===
    group.bench_function("hybrid_linear", |bench| {
        let config = HybridSearchConfig {
            strategy: HybridFusionStrategy::Linear { alpha: 0.7 },
            candidates_per_index: 50,
            min_sparse_score: 0.0,
        };
        let searcher = HybridSearcher::new(&dense_index, &sparse_index).with_config(config);

        bench.iter(|| {
            for (query, text) in queries.iter().zip(query_texts.iter()) {
                let _ = searcher.search(black_box(query), black_box(text), k);
            }
        })
    });

    // Print recall comparison
    println!("\n=== Recall@{k} Comparison ===");

    // Dense recall
    let evaluator = Evaluator::new(k);
    let mut dense_evals = Vec::new();
    for (i, query) in queries.iter().enumerate() {
        let start = Instant::now();
        let results = dense_index.search(query, k).unwrap();
        let retrieved: Vec<String> = results.into_iter().map(|r| r.id).collect();
        let eval = evaluator.evaluate_query(
            format!("q-{i}"),
            &retrieved,
            &ground_truth[i],
            start.elapsed(),
        );
        dense_evals.push(eval);
    }
    let dense_summary = EvaluationSummary::from_evaluations(&dense_evals);
    println!("Dense-only:   Recall@{k} = {:.4}, MRR = {:.4}", dense_summary.mean_recall, dense_summary.mrr);

    // Hybrid recall (using dense ground truth for fair comparison)
    let config = HybridSearchConfig {
        strategy: HybridFusionStrategy::RRF { k: 60.0 },
        candidates_per_index: 50,
        min_sparse_score: 0.0,
    };
    let searcher = HybridSearcher::new(&dense_index, &sparse_index).with_config(config);
    let mut hybrid_evals = Vec::new();
    for (i, (query, text)) in queries.iter().zip(query_texts.iter()).enumerate() {
        let start = Instant::now();
        let results = searcher.search(query, text, k).unwrap();
        let retrieved: Vec<String> = results.into_iter().map(|r| r.id).collect();
        let eval = evaluator.evaluate_query(
            format!("q-{i}"),
            &retrieved,
            &ground_truth[i],
            start.elapsed(),
        );
        hybrid_evals.push(eval);
    }
    let hybrid_summary = EvaluationSummary::from_evaluations(&hybrid_evals);
    println!("Hybrid (RRF): Recall@{k} = {:.4}, MRR = {:.4}", hybrid_summary.mean_recall, hybrid_summary.mrr);

    group.finish();
}

// =============================================================================
// BENCHMARK: MEMORY COMPRESSION (FULL vs SQ8 vs PQ)
// =============================================================================

fn bench_memory_compression(c: &mut Criterion) {
    let mut group = c.benchmark_group("memory/compression");
    group.sample_size(20);

    let n_docs = 50000;
    let dim = 512;
    let n_queries = 100;
    let k = 10;

    println!("\n=== Memory Compression Benchmark ===");
    println!("Corpus: {} vectors x {} dimensions", n_docs, dim);

    // Generate data
    let corpus = generate_vectors(n_docs, dim, 42);
    let queries = generate_vectors(n_queries, dim, 123);

    // Memory calculation
    let full_memory = n_docs * dim * 4; // f32 = 4 bytes
    println!("\nMemory Usage:");
    println!("  Full f32:  {:>8.2} MB", full_memory as f64 / 1_000_000.0);
    println!("  SQ8 (4x):  {:>8.2} MB", (full_memory / 4) as f64 / 1_000_000.0);
    println!("  PQ-16 (32x): {:>6.2} MB", (n_docs * 16) as f64 / 1_000_000.0);
    println!("  PQ-8 (64x):  {:>6.2} MB", (n_docs * 8) as f64 / 1_000_000.0);

    // Train quantizers
    let training_sample: Vec<Vec<f32>> = corpus.iter().take(5000).cloned().collect();

    println!("\nTraining quantizers...");
    let sq8 = SQ8Quantizer::train(&training_sample);

    let pq16_config = PQConfig {
        m: 16,
        k: 256,
        kmeans_iters: 10,
        seed: Some(42),
    };
    let pq16 = PQQuantizer::train_with_config(&training_sample, &pq16_config);

    let pq8_config = PQConfig {
        m: 8,
        k: 256,
        kmeans_iters: 10,
        seed: Some(42),
    };
    let pq8 = PQQuantizer::train_with_config(&training_sample, &pq8_config);

    // Encode corpus
    println!("Encoding corpus...");
    let sq8_corpus: Vec<_> = corpus.iter().map(|v| sq8.encode(v)).collect();
    let pq16_corpus: Vec<_> = corpus.iter().map(|v| pq16.encode(v)).collect();
    let pq8_corpus: Vec<_> = corpus.iter().map(|v| pq8.encode(v)).collect();

    // === Full precision search ===
    group.throughput(Throughput::Elements(n_queries as u64));
    group.bench_function("full_f32_search", |bench| {
        bench.iter(|| {
            for query in &queries {
                // Brute force search over full corpus
                let mut scores: Vec<(usize, f32)> = corpus
                    .iter()
                    .enumerate()
                    .map(|(i, doc)| {
                        let ip: f32 = query.iter().zip(doc.iter()).map(|(a, b)| a * b).sum();
                        (i, ip)
                    })
                    .collect();
                scores.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap());
                let _top_k: Vec<_> = scores.into_iter().take(k).collect();
            }
        })
    });

    // === SQ8 search ===
    group.bench_function("sq8_search", |bench| {
        bench.iter(|| {
            for query in &queries {
                let mut scores: Vec<(usize, f32)> = sq8_corpus
                    .iter()
                    .enumerate()
                    .map(|(i, enc)| {
                        let ip = sq8.asymmetric_inner_product(query, enc);
                        (i, ip)
                    })
                    .collect();
                scores.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap());
                let _top_k: Vec<_> = scores.into_iter().take(k).collect();
            }
        })
    });

    // === PQ-16 search with precomputed tables ===
    group.bench_function("pq16_table_search", |bench| {
        bench.iter(|| {
            for query in &queries {
                let table = pq16.compute_distance_table(query);
                let mut scores: Vec<(usize, f32)> = pq16_corpus
                    .iter()
                    .enumerate()
                    .map(|(i, enc)| {
                        let dist = pq16.asymmetric_l2_squared_with_table(&table, enc);
                        (i, -dist) // Negative for sorting (lower dist = better)
                    })
                    .collect();
                scores.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap());
                let _top_k: Vec<_> = scores.into_iter().take(k).collect();
            }
        })
    });

    // === PQ-8 search with precomputed tables ===
    group.bench_function("pq8_table_search", |bench| {
        bench.iter(|| {
            for query in &queries {
                let table = pq8.compute_distance_table(query);
                let mut scores: Vec<(usize, f32)> = pq8_corpus
                    .iter()
                    .enumerate()
                    .map(|(i, enc)| {
                        let dist = pq8.asymmetric_l2_squared_with_table(&table, enc);
                        (i, -dist)
                    })
                    .collect();
                scores.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap());
                let _top_k: Vec<_> = scores.into_iter().take(k).collect();
            }
        })
    });

    // Print recall comparison (sample)
    println!("\n=== Recall@{k} vs Full Precision ===");

    let ground_truth = compute_ground_truth(&corpus, &queries, k);
    let evaluator = Evaluator::new(k);

    // SQ8 recall
    let mut sq8_evals = Vec::new();
    for (i, query) in queries.iter().enumerate() {
        let start = Instant::now();
        let mut scores: Vec<(usize, f32)> = sq8_corpus
            .iter()
            .enumerate()
            .map(|(j, enc)| (j, sq8.asymmetric_inner_product(query, enc)))
            .collect();
        scores.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap());
        let retrieved: Vec<String> = scores.into_iter().take(k).map(|(j, _)| format!("doc-{j}")).collect();
        let eval = evaluator.evaluate_query(format!("q-{i}"), &retrieved, &ground_truth[i], start.elapsed());
        sq8_evals.push(eval);
    }
    let sq8_summary = EvaluationSummary::from_evaluations(&sq8_evals);
    println!("SQ8 (4x):    Recall@{k} = {:.4}", sq8_summary.mean_recall);

    // PQ-16 recall
    let mut pq16_evals = Vec::new();
    for (i, query) in queries.iter().enumerate() {
        let start = Instant::now();
        let table = pq16.compute_distance_table(query);
        let mut scores: Vec<(usize, f32)> = pq16_corpus
            .iter()
            .enumerate()
            .map(|(j, enc)| (j, -pq16.asymmetric_l2_squared_with_table(&table, enc)))
            .collect();
        scores.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap());
        let retrieved: Vec<String> = scores.into_iter().take(k).map(|(j, _)| format!("doc-{j}")).collect();
        let eval = evaluator.evaluate_query(format!("q-{i}"), &retrieved, &ground_truth[i], start.elapsed());
        pq16_evals.push(eval);
    }
    let pq16_summary = EvaluationSummary::from_evaluations(&pq16_evals);
    println!("PQ-16 (32x): Recall@{k} = {:.4}", pq16_summary.mean_recall);

    group.finish();
}

// =============================================================================
// BENCHMARK: SEQUENTIAL VS PARALLEL HNSW CONSTRUCTION
// =============================================================================

fn bench_hnsw_construction(c: &mut Criterion) {
    let mut group = c.benchmark_group("index/hnsw_construction");
    group.sample_size(10);

    println!("\n=== HNSW Construction Benchmark ===");

    for (n_docs, dim) in [(5000, 128), (10000, 128), (20000, 64)] {
        let vectors: Vec<(String, Vec<f32>)> = generate_vectors(n_docs, dim, 42)
            .into_iter()
            .enumerate()
            .map(|(i, v)| (format!("doc-{i}"), v))
            .collect();

        let config = HNSWConfig::new(dim)
            .with_m(16)
            .with_ef_construction(100);

        println!("\nDataset: {} vectors x {} dim", n_docs, dim);

        // === Sequential HNSW ===
        group.throughput(Throughput::Elements(n_docs as u64));
        group.bench_with_input(
            BenchmarkId::new("sequential", format!("{}x{}", n_docs, dim)),
            &vectors,
            |bench, vectors| {
                bench.iter(|| {
                    let mut index = HNSWIndex::new(config.clone());
                    for (id, vec) in vectors {
                        index.add(id.clone(), vec).unwrap();
                    }
                    black_box(index)
                })
            },
        );

        // === Parallel HNSW (default threads) ===
        group.bench_with_input(
            BenchmarkId::new("parallel_auto", format!("{}x{}", n_docs, dim)),
            &vectors,
            |bench, vectors| {
                bench.iter(|| {
                    let index = ParallelHNSWBuilder::new()
                        .with_config(config.clone())
                        .with_seed(42)
                        .build(vectors.clone())
                        .unwrap();
                    black_box(index)
                })
            },
        );

        // === Parallel HNSW (4 threads) ===
        group.bench_with_input(
            BenchmarkId::new("parallel_4t", format!("{}x{}", n_docs, dim)),
            &vectors,
            |bench, vectors| {
                bench.iter(|| {
                    let index = ParallelHNSWBuilder::new()
                        .with_config(config.clone())
                        .with_threads(4)
                        .with_seed(42)
                        .build(vectors.clone())
                        .unwrap();
                    black_box(index)
                })
            },
        );
    }

    group.finish();
}

// =============================================================================
// BENCHMARK: HNSW SEARCH PERFORMANCE
// =============================================================================

fn bench_hnsw_search(c: &mut Criterion) {
    let mut group = c.benchmark_group("search/hnsw");
    group.sample_size(30);

    let n_docs = 100000;
    let dim = 128;
    let n_queries = 100;

    println!("\n=== HNSW Search Benchmark ===");
    println!("Corpus: {} vectors x {} dimensions", n_docs, dim);

    let vectors: Vec<(String, Vec<f32>)> = generate_vectors(n_docs, dim, 42)
        .into_iter()
        .enumerate()
        .map(|(i, v)| (format!("doc-{i}"), v))
        .collect();

    let queries = generate_vectors(n_queries, dim, 123);

    // Build HNSW with parallel builder
    let config = HNSWConfig::new(dim)
        .with_m(16)
        .with_ef_construction(100);

    println!("Building HNSW index...");
    let start = Instant::now();
    let index = ParallelHNSWBuilder::new()
        .with_config(config)
        .with_seed(42)
        .build(vectors)
        .unwrap();
    println!("Built in {:?}", start.elapsed());

    // Search benchmarks with different ef values
    for ef in [10, 50, 100, 200] {
        let k = 10;
        group.throughput(Throughput::Elements(n_queries as u64));
        group.bench_with_input(
            BenchmarkId::new("search", format!("ef={}", ef)),
            &ef,
            |bench, &ef| {
                bench.iter(|| {
                    for query in &queries {
                        // Note: ef is used in search, but current API doesn't expose it
                        // This benchmark uses default ef
                        let _ = index.search(black_box(query), k);
                    }
                })
            },
        );
    }

    group.finish();
}

// =============================================================================
// BENCHMARK GROUPS
// =============================================================================

criterion_group!(
    benches,
    bench_dense_vs_hybrid,
    bench_memory_compression,
    bench_hnsw_construction,
    bench_hnsw_search,
);

criterion_main!(benches);