-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmod.rs
More file actions
570 lines (499 loc) · 19.6 KB
/
Copy pathmod.rs
File metadata and controls
570 lines (499 loc) · 19.6 KB
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
558
559
560
561
562
563
564
565
566
567
568
569
570
//! Benchmark Suite for Ladybug-RS
//!
//! Proves the "holy shit" claims:
//! - 50-100x less RAM than float32 vector DBs
//! - >95% recall with bitpacked Hamming
//! - Sub-millisecond search on millions of vectors
//!
//! Run: cargo bench --features bench
//! Or: cargo run --release --features bench --bin ladybug-bench
pub mod memory;
pub mod recall;
pub mod throughput;
pub mod comparison;
use std::time::{Duration, Instant};
use crate::core::Fingerprint;
use crate::storage::{BindSpace, Substrate, SubstrateConfig, FINGERPRINT_WORDS};
// =============================================================================
// BENCHMARK CONFIGURATION
// =============================================================================
/// Benchmark configuration
#[derive(Clone, Debug)]
pub struct BenchConfig {
/// Number of vectors to index
pub num_vectors: usize,
/// Number of queries to run
pub num_queries: usize,
/// K for recall@K measurement
pub k: usize,
/// Number of warmup iterations
pub warmup_iters: usize,
/// Random seed for reproducibility
pub seed: u64,
}
impl Default for BenchConfig {
fn default() -> Self {
Self {
num_vectors: 100_000,
num_queries: 1000,
k: 10,
warmup_iters: 100,
seed: 42,
}
}
}
impl BenchConfig {
pub fn small() -> Self {
Self {
num_vectors: 10_000,
num_queries: 100,
..Default::default()
}
}
pub fn medium() -> Self {
Self {
num_vectors: 100_000,
num_queries: 1000,
..Default::default()
}
}
pub fn large() -> Self {
Self {
num_vectors: 1_000_000,
num_queries: 1000,
..Default::default()
}
}
pub fn xlarge() -> Self {
Self {
num_vectors: 10_000_000,
num_queries: 100,
..Default::default()
}
}
}
// =============================================================================
// BENCHMARK RESULTS
// =============================================================================
/// Results from a benchmark run
#[derive(Clone, Debug)]
pub struct BenchResult {
/// Configuration used
pub config: BenchConfig,
/// Memory usage in bytes
pub memory_bytes: usize,
/// Memory per vector in bytes
pub bytes_per_vector: f64,
/// Index build time
pub build_time: Duration,
/// Average query latency
pub avg_latency: Duration,
/// P50 query latency
pub p50_latency: Duration,
/// P95 query latency
pub p95_latency: Duration,
/// P99 query latency
pub p99_latency: Duration,
/// Queries per second
pub qps: f64,
/// Recall@K
pub recall_at_k: f64,
/// Comparison metrics
pub comparison: Option<ComparisonMetrics>,
}
/// Comparison against baseline
#[derive(Clone, Debug)]
pub struct ComparisonMetrics {
/// RAM savings vs float32 (e.g., 50.0 = 50x less RAM)
pub ram_savings_factor: f64,
/// Speed improvement vs float32 cosine
pub speed_factor: f64,
/// float32 baseline memory per vector
pub baseline_bytes_per_vector: f64,
/// float32 baseline latency
pub baseline_latency: Duration,
}
impl BenchResult {
/// Print formatted results
pub fn print(&self) {
println!("\n╔════════════════════════════════════════════════════════════════╗");
println!("║ LADYBUG-RS BENCHMARK ║");
println!("╠════════════════════════════════════════════════════════════════╣");
println!("║ Vectors: {:>12} | Queries: {:>8} | K: {:>4} ║",
format_num(self.config.num_vectors),
format_num(self.config.num_queries),
self.config.k
);
println!("╠════════════════════════════════════════════════════════════════╣");
println!("║ MEMORY ║");
println!("║ Total: {:>12} ║", format_bytes(self.memory_bytes));
println!("║ Per vector: {:>12} ║", format_bytes(self.bytes_per_vector as usize));
println!("╠════════════════════════════════════════════════════════════════╣");
println!("║ LATENCY ║");
println!("║ Average: {:>10} ║", format_duration(self.avg_latency));
println!("║ P50: {:>10} ║", format_duration(self.p50_latency));
println!("║ P95: {:>10} ║", format_duration(self.p95_latency));
println!("║ P99: {:>10} ║", format_duration(self.p99_latency));
println!("╠════════════════════════════════════════════════════════════════╣");
println!("║ THROUGHPUT ║");
println!("║ QPS: {:>12.0} ║", self.qps);
println!("║ Build: {:>10} ║", format_duration(self.build_time));
println!("╠════════════════════════════════════════════════════════════════╣");
println!("║ RECALL ║");
println!("║ Recall@{}: {:>6.2}% ║", self.config.k, self.recall_at_k * 100.0);
if let Some(cmp) = self.comparison.as_ref() {
println!("╠════════════════════════════════════════════════════════════════╣");
println!("║ VS FLOAT32 COSINE BASELINE ║");
println!("║ RAM savings: {:>6.1}x less memory ║", cmp.ram_savings_factor);
println!("║ Speed factor: {:>6.1}x faster ║", cmp.speed_factor);
println!("║ Baseline RAM: {:>12} per vector ║", format_bytes(cmp.baseline_bytes_per_vector as usize));
println!("║ Baseline lat: {:>10} ║", format_duration(cmp.baseline_latency));
}
println!("╚════════════════════════════════════════════════════════════════╝");
}
/// Export as JSON
pub fn to_json(&self) -> String {
serde_json::json!({
"config": {
"num_vectors": self.config.num_vectors,
"num_queries": self.config.num_queries,
"k": self.config.k
},
"memory": {
"total_bytes": self.memory_bytes,
"bytes_per_vector": self.bytes_per_vector
},
"latency": {
"avg_ns": self.avg_latency.as_nanos(),
"p50_ns": self.p50_latency.as_nanos(),
"p95_ns": self.p95_latency.as_nanos(),
"p99_ns": self.p99_latency.as_nanos()
},
"throughput": {
"qps": self.qps,
"build_time_ms": self.build_time.as_millis()
},
"recall": {
"recall_at_k": self.recall_at_k
},
"comparison": self.comparison.as_ref().map(|c| {
serde_json::json!({
"ram_savings_factor": c.ram_savings_factor,
"speed_factor": c.speed_factor
})
})
}).to_string()
}
}
// =============================================================================
// VECTOR GENERATION
// =============================================================================
/// Generate random fingerprints for benchmarking
pub fn generate_random_fingerprints(count: usize, seed: u64) -> Vec<[u64; FINGERPRINT_WORDS]> {
use std::hash::{Hash, Hasher};
use std::collections::hash_map::DefaultHasher;
let mut fingerprints = Vec::with_capacity(count);
for i in 0..count {
let mut fp = [0u64; FINGERPRINT_WORDS];
for j in 0..FINGERPRINT_WORDS {
let mut hasher = DefaultHasher::new();
seed.hash(&mut hasher);
i.hash(&mut hasher);
j.hash(&mut hasher);
fp[j] = hasher.finish();
}
fingerprints.push(fp);
}
fingerprints
}
/// Generate clustered fingerprints (more realistic for recall testing)
pub fn generate_clustered_fingerprints(
count: usize,
num_clusters: usize,
seed: u64
) -> Vec<[u64; FINGERPRINT_WORDS]> {
use std::hash::{Hash, Hasher};
use std::collections::hash_map::DefaultHasher;
// Generate cluster centers
let centers: Vec<[u64; FINGERPRINT_WORDS]> = (0..num_clusters)
.map(|c| {
let mut fp = [0u64; FINGERPRINT_WORDS];
for j in 0..FINGERPRINT_WORDS {
let mut hasher = DefaultHasher::new();
seed.hash(&mut hasher);
c.hash(&mut hasher);
j.hash(&mut hasher);
fp[j] = hasher.finish();
}
fp
})
.collect();
// Generate points around cluster centers
let mut fingerprints = Vec::with_capacity(count);
for i in 0..count {
let cluster_idx = i % num_clusters;
let center = ¢ers[cluster_idx];
// Flip some bits from center (noise)
let mut fp = *center;
let noise_bits = (i / num_clusters) % 100; // 0-99 bits of noise
for b in 0..noise_bits {
let mut hasher = DefaultHasher::new();
seed.hash(&mut hasher);
i.hash(&mut hasher);
b.hash(&mut hasher);
let bit_idx = (hasher.finish() % (FINGERPRINT_WORDS as u64 * 64)) as usize;
let word_idx = bit_idx / 64;
let bit_pos = bit_idx % 64;
fp[word_idx] ^= 1 << bit_pos;
}
fingerprints.push(fp);
}
fingerprints
}
// =============================================================================
// GROUND TRUTH COMPUTATION
// =============================================================================
/// Compute ground truth nearest neighbors using brute force
pub fn compute_ground_truth(
queries: &[[u64; FINGERPRINT_WORDS]],
database: &[[u64; FINGERPRINT_WORDS]],
k: usize,
) -> Vec<Vec<usize>> {
queries.iter().map(|query| {
let mut distances: Vec<(usize, u32)> = database.iter()
.enumerate()
.map(|(idx, vec)| {
let dist: u32 = query.iter()
.zip(vec.iter())
.map(|(a, b)| (a ^ b).count_ones())
.sum();
(idx, dist)
})
.collect();
distances.sort_by_key(|(_, d)| *d);
distances.into_iter().take(k).map(|(idx, _)| idx).collect()
}).collect()
}
/// Compute recall given predictions and ground truth
pub fn compute_recall(
predictions: &[Vec<usize>],
ground_truth: &[Vec<usize>],
) -> f64 {
let mut total_correct = 0;
let mut total_expected = 0;
for (pred, truth) in predictions.iter().zip(ground_truth.iter()) {
for p in pred {
if truth.contains(p) {
total_correct += 1;
}
}
total_expected += truth.len();
}
total_correct as f64 / total_expected as f64
}
// =============================================================================
// MAIN BENCHMARK RUNNER
// =============================================================================
/// Run full benchmark suite
pub fn run_benchmark(config: BenchConfig) -> BenchResult {
println!("Generating {} random fingerprints...", config.num_vectors);
let start = Instant::now();
let database = generate_clustered_fingerprints(
config.num_vectors,
100, // 100 clusters
config.seed
);
let gen_time = start.elapsed();
println!(" Generated in {:?}", gen_time);
// Measure memory before indexing
let mem_before = get_memory_usage();
println!("Building Substrate index...");
let build_start = Instant::now();
let substrate = Substrate::new(SubstrateConfig::default());
for fp in &database {
substrate.write(*fp);
}
let build_time = build_start.elapsed();
println!(" Built in {:?}", build_time);
// Measure memory after indexing
let mem_after = get_memory_usage();
let memory_bytes = mem_after.saturating_sub(mem_before);
let bytes_per_vector = memory_bytes as f64 / config.num_vectors as f64;
// Generate queries
println!("Running {} queries...", config.num_queries);
let queries = generate_random_fingerprints(config.num_queries, config.seed + 1);
// Warmup
for query in queries.iter().take(config.warmup_iters) {
let _ = substrate.resonate(query, config.k);
}
// Timed queries
let mut latencies = Vec::with_capacity(config.num_queries);
let query_start = Instant::now();
for query in &queries {
let iter_start = Instant::now();
let _ = substrate.resonate(query, config.k);
latencies.push(iter_start.elapsed());
}
let total_query_time = query_start.elapsed();
// Compute latency percentiles
latencies.sort();
let avg_latency = total_query_time / config.num_queries as u32;
let p50_latency = latencies[latencies.len() / 2];
let p95_latency = latencies[latencies.len() * 95 / 100];
let p99_latency = latencies[latencies.len() * 99 / 100];
let qps = config.num_queries as f64 / total_query_time.as_secs_f64();
// Compute recall (sample for large datasets)
println!("Computing recall@{}...", config.k);
let recall_sample_size = config.num_queries.min(100);
let query_sample: Vec<_> = queries.iter().take(recall_sample_size).cloned().collect();
// Get predictions from substrate
let predictions: Vec<Vec<usize>> = query_sample.iter()
.map(|q| {
substrate.resonate(q, config.k)
.into_iter()
.map(|(addr, _)| addr.0 as usize)
.collect()
})
.collect();
// Compute ground truth
let ground_truth = compute_ground_truth(&query_sample, &database, config.k);
let recall_at_k = compute_recall(&predictions, &ground_truth);
// Compute comparison metrics
let comparison = compute_comparison_metrics(
bytes_per_vector,
avg_latency,
);
BenchResult {
config,
memory_bytes,
bytes_per_vector,
build_time,
avg_latency,
p50_latency,
p95_latency,
p99_latency,
qps,
recall_at_k,
comparison: Some(comparison),
}
}
/// Compute comparison against float32 baseline
fn compute_comparison_metrics(
bytes_per_vector: f64,
avg_latency: Duration,
) -> ComparisonMetrics {
// Float32 baseline assumptions:
// - 768-dim embedding (typical for sentence transformers)
// - 4 bytes per float32
// - Plus overhead for HNSW graph (~100 bytes per vector typical)
let baseline_embedding_size = 768 * 4; // 3072 bytes
let baseline_hnsw_overhead = 100; // conservative
let baseline_bytes_per_vector = (baseline_embedding_size + baseline_hnsw_overhead) as f64;
// Typical HNSW latency on 1M vectors: ~1-5ms
let baseline_latency = Duration::from_micros(2000);
let ram_savings_factor = baseline_bytes_per_vector / bytes_per_vector;
let speed_factor = baseline_latency.as_nanos() as f64 / avg_latency.as_nanos() as f64;
ComparisonMetrics {
ram_savings_factor,
speed_factor,
baseline_bytes_per_vector,
baseline_latency,
}
}
// =============================================================================
// UTILITIES
// =============================================================================
fn get_memory_usage() -> usize {
// Simple approximation - in production would use jemalloc stats
#[cfg(target_os = "linux")]
{
if let Ok(status) = std::fs::read_to_string("/proc/self/status") {
for line in status.lines() {
if line.starts_with("VmRSS:") {
if let Some(kb) = line.split_whitespace().nth(1) {
if let Ok(kb_val) = kb.parse::<usize>() {
return kb_val * 1024;
}
}
}
}
}
}
0
}
fn format_num(n: usize) -> String {
if n >= 1_000_000_000 {
format!("{:.1}B", n as f64 / 1_000_000_000.0)
} else if n >= 1_000_000 {
format!("{:.1}M", n as f64 / 1_000_000.0)
} else if n >= 1_000 {
format!("{:.1}K", n as f64 / 1_000.0)
} else {
format!("{}", n)
}
}
fn format_bytes(b: usize) -> String {
if b >= 1024 * 1024 * 1024 {
format!("{:.2} GB", b as f64 / (1024.0 * 1024.0 * 1024.0))
} else if b >= 1024 * 1024 {
format!("{:.2} MB", b as f64 / (1024.0 * 1024.0))
} else if b >= 1024 {
format!("{:.2} KB", b as f64 / 1024.0)
} else {
format!("{} B", b)
}
}
fn format_duration(d: Duration) -> String {
let nanos = d.as_nanos();
if nanos >= 1_000_000_000 {
format!("{:.2}s", d.as_secs_f64())
} else if nanos >= 1_000_000 {
format!("{:.2}ms", nanos as f64 / 1_000_000.0)
} else if nanos >= 1_000 {
format!("{:.2}µs", nanos as f64 / 1_000.0)
} else {
format!("{}ns", nanos)
}
}
// =============================================================================
// TESTS
// =============================================================================
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_small_benchmark() {
let config = BenchConfig {
num_vectors: 1000,
num_queries: 10,
k: 5,
warmup_iters: 5,
seed: 42,
};
let result = run_benchmark(config);
result.print();
assert!(result.recall_at_k > 0.0);
assert!(result.qps > 0.0);
}
#[test]
fn test_generate_fingerprints() {
let fps = generate_random_fingerprints(100, 42);
assert_eq!(fps.len(), 100);
// Check they're not all zeros
let total_bits: u32 = fps.iter()
.flat_map(|fp| fp.iter())
.map(|w| w.count_ones())
.sum();
assert!(total_bits > 0);
}
#[test]
fn test_ground_truth() {
let database = generate_random_fingerprints(100, 42);
let queries = generate_random_fingerprints(10, 43);
let gt = compute_ground_truth(&queries, &database, 5);
assert_eq!(gt.len(), 10);
for neighbors in > {
assert_eq!(neighbors.len(), 5);
}
}
}