docbert-plaid 0.6.1

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
//! Save and load a built [`Index`] to/from disk.
//!
//! The on-disk format is a single little-endian binary file with a small
//! header followed by a sequence of plain f32/u32/u64 blobs. The layout
//! is deliberately boring — there's no compression, no extra framing,
//! and no schema evolution today; we can layer any of that on top later
//! without changing the semantic API.
//!
//! Layout, every field little-endian:
//!
//! ```text
//! magic           : 8 bytes, b"PLAIDIDX"
//! version         : u32     (currently 1)
//! dim             : u32
//! nbits           : u32
//! k_centroids    : u32
//! max_kmeans_iters: u32
//! n_documents     : u64
//! centroids       : dim * k_centroids     f32
//! bucket_cutoffs  : 2^nbits - 1            f32
//! bucket_weights  : 2^nbits                f32
//! doc_ids         : n_documents            u64
//! token_counts    : n_documents            u32  (tokens per document)
//! encoded_tokens  : for each token, u32 centroid_id then `dim` u8 codes
//! ```
//!
//! The inverted file is *not* persisted — it's derivable from the
//! encoded tokens in O(n_tokens) time on load and storing it would just
//! duplicate state that's already on disk elsewhere.

use std::{
    fs::File,
    io::{BufReader, BufWriter, Read, Write},
    path::Path,
};

use crate::{
    PlaidError,
    Result,
    codec::{EncodedVector, ResidualCodec, packed_bytes_per_vector},
    index::{Index, IndexParams, build_inverted_file},
};

const MAGIC: &[u8; 8] = b"PLAIDIDX";
/// Format versions in the wild:
///
/// - `1`: unpacked residual codes, one byte per residual dimension.
/// - `2`: LSB-first bit-packed codes at `nbits ∈ {1, 2, 4, 8}`;
///   `(dim * nbits) / 8` bytes per token.
const FORMAT_VERSION: u32 = 2;

/// Write `index` to `path`, creating or truncating the file as needed.
///
/// # Errors
///
/// Returns [`PlaidError::Io`] for any underlying I/O failure, or
/// [`PlaidError::InvalidIndex`] if an encoded token's `codes` buffer
/// doesn't match the codec's advertised packed length (a caller bug
/// worth flagging loudly on write rather than producing a file that
/// would fail to load back).
///
/// [`PlaidError::Io`]: crate::PlaidError::Io
/// [`PlaidError::InvalidIndex`]: crate::PlaidError::InvalidIndex
pub fn save(index: &Index, path: &Path) -> Result<()> {
    let file = File::create(path)?;
    let mut writer = BufWriter::new(file);
    write_index(index, &mut writer)?;
    writer.flush()?;
    Ok(())
}

/// Read an [`Index`] back from `path`.
///
/// # Errors
///
/// Returns [`PlaidError::Io`] for any read failure, or
/// [`PlaidError::InvalidIndex`] if the file's magic, version, or
/// header fields don't match this crate's format.
///
/// [`PlaidError::Io`]: crate::PlaidError::Io
/// [`PlaidError::InvalidIndex`]: crate::PlaidError::InvalidIndex
pub fn load(path: &Path) -> Result<Index> {
    let file = File::open(path)?;
    let mut reader = BufReader::new(file);
    read_index(&mut reader)
}

fn write_index<W: Write>(index: &Index, w: &mut W) -> Result<()> {
    w.write_all(MAGIC)?;
    write_u32(w, FORMAT_VERSION)?;

    let params = &index.params;
    write_u32(w, params.dim as u32)?;
    write_u32(w, params.nbits)?;
    write_u32(w, params.k_centroids as u32)?;
    write_u32(w, params.max_kmeans_iters as u32)?;
    write_u64(w, index.doc_ids.len() as u64)?;

    write_f32_slice(w, &index.codec.centroids)?;
    write_f32_slice(w, &index.codec.bucket_cutoffs)?;
    write_f32_slice(w, &index.codec.bucket_weights)?;

    write_u64_slice(w, &index.doc_ids)?;

    let token_counts: Vec<u32> = index
        .doc_tokens
        .iter()
        .map(|tokens| tokens.len() as u32)
        .collect();
    write_u32_slice(w, &token_counts)?;

    let packed_bytes = packed_bytes_per_vector(params.dim, params.nbits);
    for encoded_doc in &index.doc_tokens {
        for ev in encoded_doc {
            write_u32(w, ev.centroid_id)?;
            if ev.codes.len() != packed_bytes {
                return Err(PlaidError::InvalidIndex(format!(
                    "encoded token has {} packed bytes but codec expects {packed_bytes}",
                    ev.codes.len(),
                )));
            }
            w.write_all(&ev.codes)?;
        }
    }

    Ok(())
}

fn read_index<R: Read>(r: &mut R) -> Result<Index> {
    let mut magic = [0u8; 8];
    r.read_exact(&mut magic)?;
    if &magic != MAGIC {
        return Err(PlaidError::InvalidIndex(
            "not a docbert-plaid index (magic bytes mismatch)".into(),
        ));
    }

    let version = read_u32(r)?;
    if version != FORMAT_VERSION {
        return Err(PlaidError::InvalidIndex(format!(
            "unsupported plaid index version {version}, expected {FORMAT_VERSION}",
        )));
    }

    let dim = read_u32(r)? as usize;
    let nbits = read_u32(r)?;
    let k_centroids = read_u32(r)? as usize;
    let max_kmeans_iters = read_u32(r)? as usize;
    let n_documents = read_u64(r)? as usize;

    if dim == 0 || k_centroids == 0 || !matches!(nbits, 1 | 2 | 4 | 8) {
        return Err(PlaidError::InvalidIndex(
            "plaid index header has invalid dim/k_centroids/nbits".into(),
        ));
    }

    let params = IndexParams {
        dim,
        nbits,
        k_centroids,
        max_kmeans_iters,
    };

    let centroids = read_f32_vec(r, k_centroids * dim)?;
    let num_buckets = 1usize << nbits;
    let bucket_cutoffs = read_f32_vec(r, num_buckets - 1)?;
    let bucket_weights = read_f32_vec(r, num_buckets)?;

    let doc_ids = read_u64_vec(r, n_documents)?;
    let token_counts = read_u32_vec(r, n_documents)?;
    let packed_bytes = packed_bytes_per_vector(dim, nbits);

    let mut doc_tokens: Vec<Vec<EncodedVector>> =
        Vec::with_capacity(n_documents);
    for count in token_counts.iter() {
        let mut encoded_doc = Vec::with_capacity(*count as usize);
        for _ in 0..*count {
            let centroid_id = read_u32(r)?;
            if (centroid_id as usize) >= k_centroids {
                return Err(PlaidError::InvalidIndex(format!(
                    "centroid_id {centroid_id} out of range 0..{k_centroids}",
                )));
            }
            let mut codes = vec![0u8; packed_bytes];
            r.read_exact(&mut codes)?;
            encoded_doc.push(EncodedVector { centroid_id, codes });
        }
        doc_tokens.push(encoded_doc);
    }
    let ivf = build_inverted_file(&doc_tokens, k_centroids);

    let codec = ResidualCodec {
        nbits,
        dim,
        centroids,
        bucket_cutoffs,
        bucket_weights,
    };
    codec.validate()?;

    Ok(Index {
        params,
        codec,
        doc_ids,
        doc_tokens,
        ivf,
    })
}

fn write_u32<W: Write>(w: &mut W, v: u32) -> Result<()> {
    w.write_all(&v.to_le_bytes())?;
    Ok(())
}

fn write_u64<W: Write>(w: &mut W, v: u64) -> Result<()> {
    w.write_all(&v.to_le_bytes())?;
    Ok(())
}

fn write_f32_slice<W: Write>(w: &mut W, slice: &[f32]) -> Result<()> {
    w.write_all(bytemuck::cast_slice(slice))?;
    Ok(())
}

fn write_u32_slice<W: Write>(w: &mut W, slice: &[u32]) -> Result<()> {
    w.write_all(bytemuck::cast_slice(slice))?;
    Ok(())
}

fn write_u64_slice<W: Write>(w: &mut W, slice: &[u64]) -> Result<()> {
    w.write_all(bytemuck::cast_slice(slice))?;
    Ok(())
}

fn read_u32<R: Read>(r: &mut R) -> Result<u32> {
    let mut buf = [0u8; 4];
    r.read_exact(&mut buf)?;
    Ok(u32::from_le_bytes(buf))
}

fn read_u64<R: Read>(r: &mut R) -> Result<u64> {
    let mut buf = [0u8; 8];
    r.read_exact(&mut buf)?;
    Ok(u64::from_le_bytes(buf))
}

fn read_f32_vec<R: Read>(r: &mut R, n: usize) -> Result<Vec<f32>> {
    let mut out = vec![0.0f32; n];
    r.read_exact(bytemuck::cast_slice_mut(&mut out))?;
    Ok(out)
}

fn read_u32_vec<R: Read>(r: &mut R, n: usize) -> Result<Vec<u32>> {
    let mut out = vec![0u32; n];
    r.read_exact(bytemuck::cast_slice_mut(&mut out))?;
    Ok(out)
}

fn read_u64_vec<R: Read>(r: &mut R, n: usize) -> Result<Vec<u64>> {
    let mut out = vec![0u64; n];
    r.read_exact(bytemuck::cast_slice_mut(&mut out))?;
    Ok(out)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::index::{DocumentTokens, build_index};

    fn small_corpus() -> Vec<DocumentTokens> {
        vec![
            DocumentTokens {
                doc_id: 10,
                tokens: vec![0.0, 0.0, 0.1, 0.2, -0.1, 0.1],
                n_tokens: 3,
            },
            DocumentTokens {
                doc_id: 20,
                tokens: vec![10.0, 10.0, 10.2, 9.9, 9.8, 10.1],
                n_tokens: 3,
            },
            DocumentTokens {
                doc_id: 30,
                tokens: vec![0.3, -0.2, 9.7, 10.2],
                n_tokens: 2,
            },
        ]
    }

    fn default_params() -> IndexParams {
        IndexParams {
            dim: 2,
            nbits: 2,
            k_centroids: 2,
            max_kmeans_iters: 50,
        }
    }

    #[test]
    fn round_trip_preserves_codec_parameters_and_doc_ids() {
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("index.plaid");
        let index = build_index(&small_corpus(), default_params()).unwrap();

        save(&index, &path).unwrap();
        let loaded = load(&path).unwrap();

        assert_eq!(loaded.params.dim, index.params.dim);
        assert_eq!(loaded.params.nbits, index.params.nbits);
        assert_eq!(loaded.params.k_centroids, index.params.k_centroids);
        assert_eq!(
            loaded.params.max_kmeans_iters,
            index.params.max_kmeans_iters
        );
        assert_eq!(loaded.doc_ids, index.doc_ids);
    }

    #[test]
    fn round_trip_preserves_codec_tables_byte_for_byte() {
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("index.plaid");
        let index = build_index(&small_corpus(), default_params()).unwrap();

        save(&index, &path).unwrap();
        let loaded = load(&path).unwrap();

        assert_eq!(loaded.codec.centroids, index.codec.centroids);
        assert_eq!(loaded.codec.bucket_cutoffs, index.codec.bucket_cutoffs);
        assert_eq!(loaded.codec.bucket_weights, index.codec.bucket_weights);
    }

    #[test]
    fn round_trip_preserves_encoded_tokens() {
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("index.plaid");
        let index = build_index(&small_corpus(), default_params()).unwrap();

        save(&index, &path).unwrap();
        let loaded = load(&path).unwrap();

        assert_eq!(loaded.doc_tokens.len(), index.doc_tokens.len());
        for (a, b) in loaded.doc_tokens.iter().zip(index.doc_tokens.iter()) {
            assert_eq!(a, b);
        }
    }

    #[test]
    fn round_trip_rebuilds_the_inverted_file() {
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("index.plaid");
        let index = build_index(&small_corpus(), default_params()).unwrap();

        save(&index, &path).unwrap();
        let loaded = load(&path).unwrap();

        assert_eq!(loaded.ivf.num_centroids(), index.ivf.num_centroids());
        assert_eq!(
            loaded.ivf.total_doc_postings(),
            index.ivf.total_doc_postings(),
        );
        for c in 0..index.ivf.num_centroids() {
            let want = index.ivf.docs_for_centroid(c);
            let got = loaded.ivf.docs_for_centroid(c);
            assert_eq!(got, want, "IVF list for centroid {c} differs");
        }
    }

    #[test]
    fn round_trip_preserves_search_results_exactly() {
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("index.plaid");
        let index = build_index(&small_corpus(), default_params()).unwrap();
        save(&index, &path).unwrap();
        let loaded = load(&path).unwrap();

        let query = [0.05f32, 0.1, 9.9, 10.1];
        let params = crate::search::SearchParams {
            top_k: 3,
            n_probe: 2,
            n_candidate_docs: None,
            centroid_score_threshold: None,
        };

        let a = crate::search::search(&index, &query, params).unwrap();
        let b = crate::search::search(&loaded, &query, params).unwrap();
        assert_eq!(a, b);
    }

    #[test]
    fn round_trip_handles_empty_documents() {
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("index.plaid");
        let mut docs = small_corpus();
        docs.push(DocumentTokens {
            doc_id: 999,
            tokens: vec![],
            n_tokens: 0,
        });
        let index = build_index(&docs, default_params()).unwrap();

        save(&index, &path).unwrap();
        let loaded = load(&path).unwrap();

        assert_eq!(loaded.doc_ids, index.doc_ids);
        assert_eq!(loaded.doc_tokens.last().unwrap().len(), 0);
    }

    #[test]
    fn load_rejects_files_with_wrong_magic() {
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("bogus.plaid");
        std::fs::write(&path, b"NOTPLAID").unwrap();

        let err = load(&path).unwrap_err();
        assert!(
            matches!(err, PlaidError::InvalidIndex(ref m) if m.contains("magic")),
            "expected InvalidIndex with magic message, got {err:?}",
        );
    }

    #[test]
    fn load_rejects_unknown_format_version() {
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("future.plaid");
        let mut buf = Vec::new();
        buf.extend_from_slice(MAGIC);
        buf.extend_from_slice(&999u32.to_le_bytes());
        std::fs::write(&path, &buf).unwrap();

        let err = load(&path).unwrap_err();
        assert!(
            matches!(err, PlaidError::InvalidIndex(ref m) if m.contains("version")),
            "expected InvalidIndex with version message, got {err:?}",
        );
    }

    #[test]
    fn load_rejects_legacy_unpacked_format_version_one() {
        // Version 1 (pre-packing) indexes must be rebuilt. The loader
        // should reject them with a clear version mismatch.
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("legacy.plaid");
        let mut buf = Vec::new();
        buf.extend_from_slice(MAGIC);
        buf.extend_from_slice(&1u32.to_le_bytes());
        std::fs::write(&path, &buf).unwrap();

        let err = load(&path).unwrap_err();
        assert!(
            matches!(err, PlaidError::InvalidIndex(ref m) if m.contains("version")),
            "expected InvalidIndex with version message, got {err:?}",
        );
    }
}