docbert-plaid 1.0.0

PLAID-style multi-vector index for docbert (ColBERT late-interaction)
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
//! Gold-standard recall checks for the PLAID index.
//!
//! These tests build a real PLAID index over a synthetic corpus, then
//! compare its top-K results against a brute-force exact MaxSim
//! computation that doesn't go through any of our index data
//! structures. The closer PLAID's top-K is to the exhaustive top-K,
//! the better the index quality.
//!
//! Two flavours of "exact":
//!
//! 1. **Exact-on-decoded**: brute-force MaxSim against the *decoded*
//!    tokens stored in the index. This isolates the IVF prune step:
//!    if PLAID returns the same top-K as a decoded-but-unprobed scan,
//!    the prune isn't dropping good candidates.
//!
//! 2. **Exact-on-original**: brute-force MaxSim against the *original*
//!    (pre-quantization) tokens. This bounds end-to-end recall after
//!    accounting for both IVF pruning and codec quantization error.
//!
//! Both tests assert recall@K ≥ a threshold. Thresholds are
//! intentionally loose for this synthetic corpus; the point is to
//! catch large regressions, not pin specific values that drift with
//! every algorithm tweak.

use docbert_plaid::{
    distance::dot,
    index::{DocumentTokens, IndexParams, build_index},
    search::{SearchParams, search},
};
use rand::{RngExt, SeedableRng, rngs::StdRng};

const DIM: usize = 32;

fn normalize(row: &mut [f32]) {
    let n = row.iter().map(|x| x * x).sum::<f32>().sqrt().max(1e-9);
    for v in row {
        *v /= n;
    }
}

fn random_unit_vectors(seed: u64, n: usize, dim: usize) -> Vec<f32> {
    let mut rng = StdRng::seed_from_u64(seed);
    let mut data: Vec<f32> = (0..n * dim)
        .map(|_| rng.random::<f32>() * 2.0 - 1.0)
        .collect();
    for row in data.chunks_exact_mut(dim) {
        normalize(row);
    }
    data
}

fn build_corpus(
    seed: u64,
    n_docs: usize,
    tokens_per_doc: usize,
) -> Vec<DocumentTokens> {
    let mut rng = StdRng::seed_from_u64(seed);
    (0..n_docs)
        .map(|i| {
            let inner = rng.random::<u64>();
            DocumentTokens {
                doc_id: i as u64,
                tokens: random_unit_vectors(inner, tokens_per_doc, DIM),
                n_tokens: tokens_per_doc,
            }
        })
        .collect()
}

/// Exhaustive MaxSim against a slice of token tensors (each
/// `n_tokens × dim`, flat). Returns ranked `(doc_id, score)` pairs,
/// highest score first.
fn exhaustive_maxsim(
    query: &[f32],
    docs: &[(u64, Vec<f32>)],
    dim: usize,
) -> Vec<(u64, f32)> {
    let mut results: Vec<(u64, f32)> = docs
        .iter()
        .map(|(doc_id, tokens)| {
            let mut score = 0.0f32;
            for q in query.chunks_exact(dim) {
                let best = tokens
                    .chunks_exact(dim)
                    .map(|d| dot(q, d))
                    .fold(f32::NEG_INFINITY, f32::max);
                if best.is_finite() {
                    score += best;
                }
            }
            (*doc_id, score)
        })
        .collect();
    results.sort_by(|a, b| {
        b.1.partial_cmp(&a.1)
            .unwrap_or(std::cmp::Ordering::Equal)
            .then_with(|| a.0.cmp(&b.0))
    });
    results
}

fn recall_at_k(plaid: &[u64], exact: &[u64]) -> f32 {
    if exact.is_empty() {
        return 1.0;
    }
    let plaid_set: std::collections::HashSet<u64> =
        plaid.iter().copied().collect();
    let hits = exact.iter().filter(|id| plaid_set.contains(id)).count();
    hits as f32 / exact.len() as f32
}

#[test]
fn plaid_search_recall_at_10_against_decoded_brute_force_is_high() {
    // Configuration: small enough to exhaustively brute-force, large
    // enough that the IVF actually prunes (k_centroids ≪ n_docs).
    let n_docs = 200;
    let tokens_per_doc = 40;
    let query_tokens = 16;
    let top_k = 10;
    let n_probe = 8;

    let corpus = build_corpus(0xCAFE, n_docs, tokens_per_doc);
    let index = build_index(
        &corpus,
        IndexParams {
            dim: DIM,
            nbits: 4,
            k_centroids: 32,
            max_kmeans_iters: 20,
        },
    )
    .unwrap();

    // Decoded-tokens snapshot: what the index "thinks" each doc looks
    // like after residual quantization.
    let decoded_docs: Vec<(u64, Vec<f32>)> = index
        .doc_ids
        .iter()
        .enumerate()
        .map(|(i, doc_id)| {
            let encoded = index.doc_tokens_vec(i);
            let mut tokens = Vec::with_capacity(encoded.len() * DIM);
            for ev in &encoded {
                tokens.extend(index.codec.decode_vector(ev).unwrap());
            }
            (*doc_id, tokens)
        })
        .collect();

    let mut total_recall = 0.0f32;
    let n_queries = 5;
    for q_seed in 0..n_queries {
        let query =
            random_unit_vectors(0xBEEF + q_seed as u64, query_tokens, DIM);
        let plaid_top: Vec<u64> = search(
            &index,
            &query,
            SearchParams {
                top_k,
                n_probe,
                n_candidate_docs: None,
                centroid_score_threshold: None,
            },
        )
        .unwrap()
        .into_iter()
        .map(|r| r.doc_id)
        .collect();

        let exact_top: Vec<u64> = exhaustive_maxsim(&query, &decoded_docs, DIM)
            .into_iter()
            .take(top_k)
            .map(|(doc_id, _)| doc_id)
            .collect();

        total_recall += recall_at_k(&plaid_top, &exact_top);
    }
    let avg = total_recall / n_queries as f32;
    assert!(
        avg >= 0.85,
        "recall@10 vs decoded brute-force should be ≥ 0.85; got {avg:.3}",
    );
}

#[test]
fn plaid_search_recall_at_10_against_original_brute_force_is_meaningful() {
    // End-to-end recall: PLAID's top-K vs a brute-force on the
    // *original* (pre-quantization) tokens. Quantization error eats a
    // little recall here so the bar is lower than the decoded test
    // above. This still catches "the index returned wildly wrong docs".
    let n_docs = 200;
    let tokens_per_doc = 40;
    let query_tokens = 16;
    let top_k = 10;
    let n_probe = 8;

    let corpus = build_corpus(0xF00D, n_docs, tokens_per_doc);
    let index = build_index(
        &corpus,
        IndexParams {
            dim: DIM,
            nbits: 4,
            k_centroids: 32,
            max_kmeans_iters: 20,
        },
    )
    .unwrap();

    // Snapshot of original tokens, indexed by doc_id.
    let original_docs: Vec<(u64, Vec<f32>)> = corpus
        .iter()
        .map(|d| (d.doc_id, d.tokens.clone()))
        .collect();

    let mut total_recall = 0.0f32;
    let n_queries = 5;
    for q_seed in 0..n_queries {
        let query =
            random_unit_vectors(0xDEADBEEF + q_seed as u64, query_tokens, DIM);
        let plaid_top: Vec<u64> = search(
            &index,
            &query,
            SearchParams {
                top_k,
                n_probe,
                n_candidate_docs: None,
                centroid_score_threshold: None,
            },
        )
        .unwrap()
        .into_iter()
        .map(|r| r.doc_id)
        .collect();

        let exact_top: Vec<u64> =
            exhaustive_maxsim(&query, &original_docs, DIM)
                .into_iter()
                .take(top_k)
                .map(|(doc_id, _)| doc_id)
                .collect();

        total_recall += recall_at_k(&plaid_top, &exact_top);
    }
    let avg = total_recall / n_queries as f32;
    assert!(
        avg >= 0.70,
        "end-to-end recall@10 should be ≥ 0.70; got {avg:.3}",
    );
}

#[test]
fn plaid_search_score_matches_recomputed_maxsim_on_decoded_tokens() {
    // For every doc PLAID returns, the score it reports must equal
    // the MaxSim we'd compute by hand against the index's own decoded
    // tokens. This pins the scorer to its specification independently
    // of the IVF-prune behaviour above.
    let corpus = build_corpus(0x1234, 50, 30);
    let index = build_index(
        &corpus,
        IndexParams {
            dim: DIM,
            nbits: 4,
            k_centroids: 16,
            max_kmeans_iters: 20,
        },
    )
    .unwrap();

    let query = random_unit_vectors(0x9999, 12, DIM);
    let results = search(
        &index,
        &query,
        SearchParams {
            top_k: 5,
            n_probe: 8,
            n_candidate_docs: None,
            centroid_score_threshold: None,
        },
    )
    .unwrap();

    for r in &results {
        let doc_idx = index.position_of(r.doc_id).expect("doc present");
        let mut decoded_tokens = Vec::new();
        // search() L2-normalises each decoded row before MaxSim;
        // the reference here must do the same or the scores won't
        // agree (what this assertion is checking).
        for ev in &index.doc_tokens_vec(doc_idx) {
            let raw = index.codec.decode_vector(ev).unwrap();
            let norm = raw.iter().map(|v| v * v).sum::<f32>().sqrt().max(1e-12);
            decoded_tokens.extend(raw.iter().map(|v| v / norm));
        }
        let recomputed = {
            let mut score = 0.0f32;
            for q in query.as_chunks::<DIM>().0 {
                let best = decoded_tokens
                    .as_chunks::<DIM>()
                    .0
                    .iter()
                    .map(|d| dot(q, d))
                    .fold(f32::NEG_INFINITY, f32::max);
                if best.is_finite() {
                    score += best;
                }
            }
            score
        };
        assert!(
            (recomputed - r.score).abs() < 1e-4,
            "doc {} score {} differs from manual MaxSim {}",
            r.doc_id,
            r.score,
            recomputed,
        );
    }
}

#[test]
fn plaid_search_returns_results_sorted_descending_by_score() {
    // Independent assertion: regardless of corpus, results must come
    // back in non-increasing score order. This is the contract the
    // search API documents and downstream rankers (e.g. RRF in
    // docbert-core) rely on.
    let corpus = build_corpus(0x5050, 80, 25);
    let index = build_index(
        &corpus,
        IndexParams {
            dim: DIM,
            nbits: 2,
            k_centroids: 16,
            max_kmeans_iters: 10,
        },
    )
    .unwrap();

    let query = random_unit_vectors(0x7777, 10, DIM);
    let results = search(
        &index,
        &query,
        SearchParams {
            top_k: 20,
            n_probe: 4,
            n_candidate_docs: None,
            centroid_score_threshold: None,
        },
    )
    .unwrap();
    assert!(results.len() >= 2);
    for pair in results.windows(2) {
        assert!(
            pair[0].score >= pair[1].score,
            "scores not monotonic: {pair:?}",
        );
    }
}

#[test]
fn centroid_interaction_shortlist_keeps_recall_high_against_no_shortlist() {
    // Centroid-interaction is an approximate intermediate ranker:
    // each doc's MaxSim is approximated by scoring only against the
    // centroids its tokens landed in, skipping residual decode. On
    // uniform-random synthetic tokens the centroids carry less
    // per-doc signal than on real text (which clusters meaningfully),
    // so recall on this corpus is a floor — real documents should do
    // materially better. The test's job is to catch regressions, not
    // pin a production-grade number.
    let n_docs = 200;
    let tokens_per_doc = 40;
    let query_tokens = 16;
    let top_k = 10;
    let n_probe = 8;
    // The two-stage cascade shortlists to `ndocs` (Stage 2) then
    // `ndocs/4` (Stage 3). Asking for Stage 2 to keep ~200% of the
    // corpus makes Stage 2 a no-op, so Stage 3 is the effective cut
    // at `ndocs / 4`.
    let ndocs = top_k * 40;

    let corpus = build_corpus(0xBEEF, n_docs, tokens_per_doc);
    let index = build_index(
        &corpus,
        IndexParams {
            dim: DIM,
            nbits: 4,
            k_centroids: 64,
            max_kmeans_iters: 20,
        },
    )
    .unwrap();

    let mut total_recall = 0.0f32;
    let n_queries = 5;
    for q_seed in 0..n_queries {
        let query =
            random_unit_vectors(0xA11CE + q_seed as u64, query_tokens, DIM);
        let no_shortlist: Vec<u64> = search(
            &index,
            &query,
            SearchParams {
                top_k,
                n_probe,
                n_candidate_docs: None,
                centroid_score_threshold: None,
            },
        )
        .unwrap()
        .into_iter()
        .map(|r| r.doc_id)
        .collect();
        let shortlisted: Vec<u64> = search(
            &index,
            &query,
            SearchParams {
                top_k,
                n_probe,
                n_candidate_docs: Some(ndocs),
                centroid_score_threshold: None,
            },
        )
        .unwrap()
        .into_iter()
        .map(|r| r.doc_id)
        .collect();
        total_recall += recall_at_k(&shortlisted, &no_shortlist);
    }
    let avg = total_recall / n_queries as f32;
    assert!(
        avg >= 0.40,
        "centroid-interaction shortlist recall should be ≥ 0.40; got {avg:.3}",
    );
}

#[test]
fn maxsim_score_for_unit_norm_query_is_at_most_q_times_one() {
    // Per query token, max dot ≤ 1 when both query and doc tokens are
    // unit-norm. Sum over Q query tokens ≤ Q. PLAID's reported scores
    // must respect that ceiling on unit-norm inputs.
    let corpus = build_corpus(0x2222, 30, 20);
    let index = build_index(
        &corpus,
        IndexParams {
            dim: DIM,
            nbits: 4,
            k_centroids: 8,
            max_kmeans_iters: 20,
        },
    )
    .unwrap();

    let query_len = 8;
    let query = random_unit_vectors(0x3333, query_len, DIM);
    let results = search(
        &index,
        &query,
        SearchParams {
            top_k: 5,
            n_probe: 8,
            n_candidate_docs: None,
            centroid_score_threshold: None,
        },
    )
    .unwrap();

    // Quantization can push decoded tokens slightly off the unit
    // sphere, so we allow a small slack above the strict |Q| ceiling.
    let ceiling = query_len as f32 + 0.5;
    for r in &results {
        assert!(
            r.score <= ceiling,
            "doc {} score {} exceeds Q={query_len} ceiling",
            r.doc_id,
            r.score,
        );
    }
}