laurus 0.11.0

Unified search library for lexical, vector, and semantic retrieval
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
//! Issue #498: reproducible SIFT (TEXMEX) sweep for Stage 2 rerank.
//!
//! Loads a SIFT base / query pair, computes brute-force Cosine
//! ground-truth top-10, and runs a `(ef_search × rerank_factor × HNSW
//! config)` sweep over the int8 + rerank Stage 2 path. Each cell
//! reports Recall@10 and average per-query latency.
//!
//! This example is committed so users (and CI machines) can reproduce
//! the Issue #498 Phase 0 evidence on their own hardware without
//! standing up an external ANN benchmarking framework.
//!
//! # Usage
//!
//! ```sh
//! ./scripts/fetch-sift.sh --small               # fetch siftsmall (~5MB)
//! ./scripts/fetch-sift.sh --large               # fetch SIFT1M  (~478MB)
//! cargo run --release --example sift_rerank_probe -- --dataset siftsmall
//! cargo run --release --example sift_rerank_probe -- --dataset sift --subsample 50000
//! ```
//!
//! Output cells are tagged `*PASS*` when **both** Recall@10 ≥ 0.99 and
//! the in-process brute-force-f32 speedup ≥ 3× hold. The brute-force-
//! f32 number is an absolute baseline (recall = 1.0); the Issue #498
//! acceptance gate (≥ 1.5× vs the pre-Stage-1 main commit's HNSW f32
//! path) is measured separately via `cargo bench` in the
//! `vector_search_bench` suite — see the docs for the cross-branch
//! protocol.

use std::env;
use std::fs::File;
use std::io::{BufReader, Read};
use std::path::{Path, PathBuf};
use std::time::Instant;

use laurus::storage::StorageConfig;
use laurus::storage::StorageFactory;
use laurus::storage::memory::MemoryStorageConfig;
use laurus::vector::core::distance::DistanceMetric;
use laurus::vector::core::rerank::RerankStorageKind;
use laurus::vector::core::vector::Vector;
use laurus::vector::index::VectorIndex;
use laurus::vector::index::config::HnswIndexConfig;
use laurus::vector::index::hnsw::HnswIndex;
use laurus::vector::index::hnsw::searcher::HnswSearcher;
use laurus::vector::search::searcher::{VectorIndexQuery, VectorIndexSearcher};

const DIM: usize = 128;
const TOP_K: usize = 10;
/// Subsample the queries for the sweep so iteration finishes fast.
/// Full siftsmall query set is 100; SIFT1M is 10 000. 50 queries are
/// enough to detect a recall surface that meets the 0.99 gate.
const N_QUERIES_DEFAULT: usize = 50;

/// `.fvecs` format: per vector, [dim: u32 LE][values: f32 LE × dim].
fn read_fvecs(path: &Path, expect_dim: usize, max: Option<usize>) -> Vec<Vec<f32>> {
    let file = File::open(path).unwrap_or_else(|e| panic!("open {}: {e}", path.display()));
    let mut reader = BufReader::new(file);
    let mut out = Vec::new();
    let mut hdr = [0u8; 4];
    let mut vec_buf = vec![0u8; expect_dim * 4];
    loop {
        if reader.read_exact(&mut hdr).is_err() {
            break;
        }
        let dim = i32::from_le_bytes(hdr) as usize;
        assert_eq!(dim, expect_dim, "dim mismatch in {}", path.display());
        reader.read_exact(&mut vec_buf).expect("vec body");
        let mut v = Vec::with_capacity(dim);
        for chunk in vec_buf.chunks_exact(4) {
            v.push(f32::from_le_bytes(chunk.try_into().unwrap()));
        }
        out.push(v);
        if let Some(cap) = max
            && out.len() >= cap
        {
            break;
        }
    }
    out
}

/// L2-normalise so Cosine distance is well-defined; SIFT vectors are
/// non-negative integer histograms that have non-zero norms.
fn normalise(v: &mut [f32]) {
    let norm: f32 = v.iter().map(|x| x * x).sum::<f32>().sqrt();
    if norm > 0.0 {
        for x in v.iter_mut() {
            *x /= norm;
        }
    }
}

fn exact_cosine_distance(a: &[f32], b: &[f32]) -> f32 {
    let mut dot = 0.0_f32;
    let mut na = 0.0_f32;
    let mut nb = 0.0_f32;
    for (x, y) in a.iter().zip(b.iter()) {
        dot += x * y;
        na += x * x;
        nb += y * y;
    }
    let denom = na.sqrt() * nb.sqrt();
    if denom == 0.0 {
        1.0
    } else {
        let cos = (dot / denom).clamp(-1.0, 1.0);
        1.0 - cos
    }
}

fn exact_top_k(corpus: &[Vec<f32>], q: &[f32], k: usize) -> Vec<u64> {
    let mut scored: Vec<(u64, f32)> = corpus
        .iter()
        .enumerate()
        .map(|(i, v)| (i as u64, exact_cosine_distance(q, v)))
        .collect();
    scored.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));
    scored.into_iter().take(k).map(|(id, _)| id).collect()
}

fn recall_at_k(exact: &[u64], approx: &[u64], k: usize) -> f32 {
    let exact_set: std::collections::HashSet<u64> = exact.iter().copied().collect();
    let approx_set: std::collections::HashSet<u64> = approx.iter().copied().collect();
    exact_set.intersection(&approx_set).count() as f32 / k as f32
}

struct Cell {
    label: &'static str,
    ef_search: usize,
    rerank_factor: Option<usize>,
    m: usize,
    ef_construction: usize,
}

fn build_index(corpus: &[Vec<f32>], rerank: bool, m: usize, ef_construction: usize) -> HnswIndex {
    let storage = StorageFactory::create(StorageConfig::Memory(MemoryStorageConfig::default()))
        .expect("memory storage");
    let config = HnswIndexConfig {
        dimension: DIM,
        m,
        ef_construction,
        distance_metric: DistanceMetric::Cosine,
        rerank_storage: if rerank {
            Some(RerankStorageKind::F32)
        } else {
            None
        },
        ..Default::default()
    };
    let index = HnswIndex::create(storage, "probe_index", config).expect("create index");
    let mut writer = index.writer().expect("writer");
    let docs: Vec<(u64, String, Vector)> = corpus
        .iter()
        .enumerate()
        .map(|(i, v)| (i as u64, "embedding".to_string(), Vector::new(v.clone())))
        .collect();
    writer.build(docs).expect("build");
    writer.finalize().expect("finalize");
    writer.commit().expect("commit");
    index
}

fn measure_brute_force_f32_latency(corpus: &[Vec<f32>], queries: &[Vec<f32>]) -> f64 {
    let start = Instant::now();
    let mut sink: u64 = 0;
    for q in queries {
        let mut scored: Vec<(u64, f32)> = corpus
            .iter()
            .enumerate()
            .map(|(i, v)| (i as u64, exact_cosine_distance(q, v)))
            .collect();
        scored.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));
        // Touch the top result so the optimiser can't elide work.
        sink = sink.wrapping_add(scored[0].0);
    }
    let elapsed = start.elapsed().as_secs_f64() / queries.len() as f64 * 1e6;
    // Use sink so the loop is not optimised out.
    std::hint::black_box(sink);
    elapsed
}

fn measure_cell(
    corpus: &[Vec<f32>],
    queries: &[Vec<f32>],
    exact_truth: &[Vec<u64>],
    cell: &Cell,
) -> (f32, f64) {
    let rerank = cell.rerank_factor.is_some();
    let index = build_index(corpus, rerank, cell.m, cell.ef_construction);
    let reader = index.reader().expect("reader");
    let mut searcher = HnswSearcher::new(reader).expect("searcher");
    searcher.set_ef_search(cell.ef_search);

    // Warm-up
    for q in queries.iter().take(3) {
        let mut req = VectorIndexQuery::new(Vector::new(q.clone()))
            .top_k(TOP_K)
            .field_name("embedding".to_string());
        if let Some(rf) = cell.rerank_factor {
            req = req.rerank_factor(rf);
        }
        let _ = searcher.search(&req).expect("warmup");
    }

    let mut total_recall = 0.0_f32;
    let start = Instant::now();
    for (qi, q) in queries.iter().enumerate() {
        let mut req = VectorIndexQuery::new(Vector::new(q.clone()))
            .top_k(TOP_K)
            .field_name("embedding".to_string());
        if let Some(rf) = cell.rerank_factor {
            req = req.rerank_factor(rf);
        }
        let res = searcher.search(&req).expect("search");
        let approx: Vec<u64> = res.results.iter().map(|r| r.doc_id).collect();
        total_recall += recall_at_k(&exact_truth[qi], &approx, TOP_K);
    }
    let elapsed_us = start.elapsed().as_secs_f64() / queries.len() as f64 * 1e6;
    let recall = total_recall / queries.len() as f32;
    (recall, elapsed_us)
}

fn parse_args() -> (String, usize, usize) {
    let mut dataset = String::from("siftsmall");
    let mut subsample: usize = 0;
    let mut n_queries = N_QUERIES_DEFAULT;
    let args: Vec<String> = env::args().skip(1).collect();
    let mut i = 0;
    while i < args.len() {
        match args[i].as_str() {
            "--dataset" => {
                dataset = args[i + 1].clone();
                i += 2;
            }
            "--subsample" => {
                subsample = args[i + 1].parse().expect("subsample u64");
                i += 2;
            }
            "--queries" => {
                n_queries = args[i + 1].parse().expect("queries u64");
                i += 2;
            }
            other => panic!("unknown arg {other}"),
        }
    }
    (dataset, subsample, n_queries)
}

fn dataset_paths(name: &str) -> (PathBuf, PathBuf) {
    let cache = Path::new("./.cache/sift").to_path_buf();
    match name {
        "siftsmall" => (
            cache.join("siftsmall/siftsmall_base.fvecs"),
            cache.join("siftsmall/siftsmall_query.fvecs"),
        ),
        "sift" => (
            cache.join("sift/sift_base.fvecs"),
            cache.join("sift/sift_query.fvecs"),
        ),
        other => panic!("unknown dataset {other}"),
    }
}

fn main() {
    let (dataset, subsample, n_queries) = parse_args();
    let (base_path, query_path) = dataset_paths(&dataset);

    println!(
        "=== Issue #498 Phase 0 probe ===\n\
         dataset = {dataset}\n\
         base    = {}\n\
         query   = {}\n\
         dim     = {DIM} top_k = {TOP_K} queries = {n_queries}",
        base_path.display(),
        query_path.display()
    );

    let cap = if subsample > 0 { Some(subsample) } else { None };
    let mut corpus = read_fvecs(&base_path, DIM, cap);
    let mut queries = read_fvecs(&query_path, DIM, Some(n_queries));
    for v in corpus.iter_mut() {
        normalise(v);
    }
    for v in queries.iter_mut() {
        normalise(v);
    }
    println!(
        "loaded corpus = {}  queries = {}",
        corpus.len(),
        queries.len()
    );

    println!("computing brute-force Cosine ground truth top-{TOP_K}...");
    let start = Instant::now();
    let truth: Vec<Vec<u64>> = queries
        .iter()
        .map(|q| exact_top_k(&corpus, q, TOP_K))
        .collect();
    println!(
        "ground truth built in {:.2}s",
        start.elapsed().as_secs_f64()
    );

    println!("measuring brute-force f32 baseline latency...");
    let bf_us = measure_brute_force_f32_latency(&corpus, &queries);
    println!("  brute-force f32: {bf_us:>9.2} µs/query (absolute baseline, exact recall)");

    println!("\nsweep cells (m, efc, ef_search, rerank_factor):");
    let cells = vec![
        // m=16 ef_construction=200 (default HNSW config), no rerank
        Cell {
            label: "m16efc200 no-rerank        ",
            ef_search: 200,
            rerank_factor: None,
            m: 16,
            ef_construction: 200,
        },
        Cell {
            label: "m16efc200 no-rerank        ",
            ef_search: 400,
            rerank_factor: None,
            m: 16,
            ef_construction: 200,
        },
        // m=16 ef_construction=200, with rerank
        Cell {
            label: "m16efc200 rerank=5         ",
            ef_search: 50,
            rerank_factor: Some(5),
            m: 16,
            ef_construction: 200,
        },
        Cell {
            label: "m16efc200 rerank=5         ",
            ef_search: 100,
            rerank_factor: Some(5),
            m: 16,
            ef_construction: 200,
        },
        Cell {
            label: "m16efc200 rerank=5         ",
            ef_search: 200,
            rerank_factor: Some(5),
            m: 16,
            ef_construction: 200,
        },
        Cell {
            label: "m16efc200 rerank=10        ",
            ef_search: 100,
            rerank_factor: Some(10),
            m: 16,
            ef_construction: 200,
        },
        Cell {
            label: "m16efc200 rerank=10        ",
            ef_search: 200,
            rerank_factor: Some(10),
            m: 16,
            ef_construction: 200,
        },
        // m=32 ef_construction=500 (stronger graph), with rerank
        Cell {
            label: "m32efc500 rerank=5         ",
            ef_search: 50,
            rerank_factor: Some(5),
            m: 32,
            ef_construction: 500,
        },
        Cell {
            label: "m32efc500 rerank=5         ",
            ef_search: 100,
            rerank_factor: Some(5),
            m: 32,
            ef_construction: 500,
        },
        Cell {
            label: "m32efc500 rerank=5         ",
            ef_search: 200,
            rerank_factor: Some(5),
            m: 32,
            ef_construction: 500,
        },
        Cell {
            label: "m32efc500 rerank=10        ",
            ef_search: 50,
            rerank_factor: Some(10),
            m: 32,
            ef_construction: 500,
        },
        Cell {
            label: "m32efc500 rerank=10        ",
            ef_search: 100,
            rerank_factor: Some(10),
            m: 32,
            ef_construction: 500,
        },
        // Strong graph, no rerank (control)
        Cell {
            label: "m32efc500 no-rerank        ",
            ef_search: 200,
            rerank_factor: None,
            m: 32,
            ef_construction: 500,
        },
    ];

    println!(
        "  {:<30}  {:>9}  {:>11}  {:>9}  {:>11}  {:>9}",
        "config", "ef_search", "rerank_fac", "recall@10", "lat µs/qry", "speedup"
    );
    for c in &cells {
        let (recall, us) = measure_cell(&corpus, &queries, &truth, c);
        let rf = c
            .rerank_factor
            .map(|r| r.to_string())
            .unwrap_or_else(|| "".to_string());
        let speedup = bf_us / us;
        let recall_ok = recall >= 0.99;
        let speed_ok = speedup >= 3.0;
        let mark = match (recall_ok, speed_ok) {
            (true, true) => " *PASS*",
            (true, false) => " recall ok",
            (false, true) => " speed ok",
            _ => "",
        };
        println!(
            "  {label:<30}  {ef:>9}  {rf:>11}  {recall:>9.4}  {us:>11.2}  {speedup:>8.2}×{mark}",
            label = c.label,
            ef = c.ef_search,
            rf = rf,
            recall = recall,
            us = us,
            speedup = speedup,
        );
    }
    println!(
        "\n(speedup is vs brute-force f32 above; cross-branch f32 HNSW \
         baseline must be taken separately on pre-Stage-1 main — see \
         `bench_hnsw_graph_search_rerank_real_data` for the Criterion \
         protocol.)"
    );
}