diskann-providers 0.51.0

DiskANN is a fast approximate nearest neighbor search library for high dimensional data
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
/*
 * Copyright (c) Microsoft Corporation.
 * Licensed under the MIT license.
 */

use std::sync::{Arc, Mutex};

use arc_swap::{ArcSwap, Guard};
use diskann::{ANNError, ANNResult};
use diskann_vector::{DistanceFunction, PreprocessedDistanceFunction, distance::Metric};
use rand::{Rng, SeedableRng, rngs::StdRng};

use crate::model::{
    FixedChunkPQTable,
    pq::{distance::multi, generate_pq_data_from_pivots_from_membuf},
};

/// The discriminant type for PQ vector versions.
type VersionId = u8;
type VersionedPQVector = multi::VersionedPQVector<VersionId>;
type TableType = Arc<FixedChunkPQTable>;
type MultiTable = multi::MultiTable<TableType, VersionId>;
type QueryComputer = multi::MultiQueryComputer<TableType, VersionId>;
type DistanceComputer = multi::MultiDistanceComputer<TableType, VersionId>;

/// A provider that has two PQ schemas.
pub struct TestMultiPQProviderAsync {
    max_vectors: usize,
    num_start_points: usize,
    quant_vectors: Vec<ArcSwap<VersionedPQVector>>,
    table_new: Arc<FixedChunkPQTable>,
    /// The secondary schema to use. If `None`, then only one schema is used and all
    /// vectors added to/provided by this class will be assigned to table1.
    table_old: Option<Arc<FixedChunkPQTable>>,
    metric: Metric,
    /// A ratio between 0.0 and 1.0 deciding which schema vectors are assigned to.
    split: f64,
    rng: Mutex<StdRng>,
}

impl TestMultiPQProviderAsync {
    pub fn new(
        metric: Metric,
        max_vectors: usize,
        num_start_points: usize,
        table_new: FixedChunkPQTable,
        table_old: Option<FixedChunkPQTable>,
        split: f64,
        seed: u64,
    ) -> Self {
        let quant_vectors = (0..max_vectors + num_start_points)
            .map(|_| ArcSwap::new(Arc::new(VersionedPQVector::new(Vec::new(), 0))))
            .collect();

        Self {
            max_vectors,
            num_start_points,
            quant_vectors,
            table_new: Arc::new(table_new),
            table_old: table_old.map(Arc::new),
            metric,
            split,
            rng: Mutex::new(StdRng::seed_from_u64(seed)),
        }
    }

    pub fn num_pq_chunks(&self) -> usize {
        self.table_new.get_num_chunks()
    }

    pub fn multi_table(&self) -> Result<MultiTable, multi::EqualVersionsError> {
        match &self.table_old {
            None => Ok(MultiTable::one(self.table_new.clone(), 1)),
            Some(table_old) => MultiTable::two(self.table_new.clone(), table_old.clone(), 2, 1),
        }
    }

    pub fn get_query_computer<T>(&self, query: &[T]) -> ANNResult<NoneToInfinity<QueryComputer>>
    where
        T: Copy + Into<f32>,
    {
        let table = self.multi_table().map_err(|err| {
            ANNError::log_index_error(format_args!("Table construction failed with: {}", err))
        })?;
        Ok(NoneToInfinity(QueryComputer::new(
            table,
            self.metric,
            query,
        )?))
    }

    pub fn get_distance_computer(&self) -> ANNResult<NoneToInfinity<DistanceComputer>> {
        let table = self.multi_table().map_err(|err| {
            ANNError::log_index_error(format_args!("Table construction failed with: {}", err))
        })?;
        Ok(NoneToInfinity(DistanceComputer::new(table, self.metric)))
    }

    pub fn get_vector(&self, id: usize) -> ANNResult<Guard<Arc<VersionedPQVector>>> {
        match self.quant_vectors.get(id) {
            Some(vector) => Ok(vector.load()),
            None => Err(ANNError::log_index_error(
                "Vector id is out of boundary in the dataset.",
            )),
        }
    }

    pub fn set_vector<T>(&self, id: usize, v: &[T]) -> ANNResult<()>
    where
        T: Copy + Into<f32>,
    {
        if id >= self.max_vectors + self.num_start_points {
            return Err(ANNError::log_index_error(
                "Vector id is out of boundary in the dataset.",
            ));
        }
        if v.len() != self.table_new.get_dim() {
            return Err(ANNError::log_index_error(
                "Vector dimension is not equal to the expected dimension.",
            ));
        }

        let vector_f32: Vec<f32> = v.iter().map(|&x| x.into()).collect::<Vec<f32>>();

        // Determine the table and version to use for this vector.
        //
        // If only one table is used, than use that table.
        //
        // Otherwise, generate a random number between 0 and 1 to determine which table
        // this vector will get assigned to.
        let (table, version): (_, u8) = match &self.table_old {
            None => (&self.table_new, 1),
            Some(table_old) => {
                let v: f64 = {
                    let mut guard = self.rng.lock().map_err(|_| {
                        ANNError::log_lock_poison_error("in multi provider".to_string())
                    })?;
                    guard.random()
                };
                if v <= self.split {
                    (table_old, 1)
                } else {
                    (&self.table_new, 2)
                }
            }
        };

        let mut quant_vector: Vec<u8> = vec![0; table.get_num_chunks()];
        if generate_pq_data_from_pivots_from_membuf(
            &vector_f32,
            table.get_pq_table(),
            table.get_num_centers(),
            Some(table.get_centroids()),
            table.get_chunk_offsets(),
            &mut quant_vector,
        )
        .is_err()
        {
            return Err(ANNError::log_index_error("Error in generating PQ data."));
        }

        let new = Arc::new(VersionedPQVector::new(quant_vector, version));
        self.quant_vectors[id].swap(new);
        Ok(())
    }
}

/// A distance adaptor that converts the `None`s returned by the multi distance
/// providers into infinities.
///
/// This effectively ignores vectors for which the ID's are not recognized.
#[repr(transparent)]
pub struct NoneToInfinity<T>(T);

impl<B, T> DistanceFunction<&[f32], &Guard<Arc<B>>, f32> for NoneToInfinity<T>
where
    T: for<'a, 'b> DistanceFunction<&'a [f32], &'a B, Option<f32>>,
{
    #[inline(always)]
    fn evaluate_similarity(&self, x: &[f32], y: &Guard<Arc<B>>) -> f32 {
        self.0.evaluate_similarity(x, &**y).unwrap_or(f32::INFINITY)
    }
}

impl<A, B, T> DistanceFunction<&Guard<Arc<A>>, &Guard<Arc<B>>, f32> for NoneToInfinity<T>
where
    T: for<'a, 'b> DistanceFunction<&'a A, &'b B, Option<f32>>,
{
    #[inline(always)]
    fn evaluate_similarity(&self, x: &Guard<Arc<A>>, y: &Guard<Arc<B>>) -> f32 {
        self.0
            .evaluate_similarity(&**x, &**y)
            .unwrap_or(f32::INFINITY)
    }
}

impl<A, T> PreprocessedDistanceFunction<&Guard<Arc<A>>, f32> for NoneToInfinity<T>
where
    T: for<'a> PreprocessedDistanceFunction<&'a A, Option<f32>>,
{
    #[inline(always)]
    fn evaluate_similarity(&self, x: &Guard<Arc<A>>) -> f32 {
        self.0.evaluate_similarity(&**x).unwrap_or(f32::INFINITY)
    }
}

/// Testing Stratetgy:
///
/// We use the test utils in `pq` to generate pivot tables with known quantities.
///
/// We can then feed in vectors specifically constructed to match known schemas, so we can
/// determine what distances should be.
#[cfg(test)]
mod tests {
    use diskann_vector::{PureDistanceFunction, distance::SquaredL2};

    use super::*;
    use crate::model::pq::distance::test_utils;

    ////////////////
    // One Schema //
    ////////////////

    /// An auxiliary helper function is used to avoid writing
    /// `<Provider as QuantVecProviderAsync<DefaultContext, Data>` for every single method.
    ///
    /// This function is closely coupled with `test_single_schema`.
    fn test_single_schema_as_qvpa(
        provider: &TestMultiPQProviderAsync,
        config: &test_utils::TableConfig,
    ) {
        let table = test_utils::seed_pivot_table(*config);
        let generate_expected_vector = |v: &[u8]| {
            test_utils::generate_expected_vector(v, table.get_chunk_offsets(), config.start_value)
        };

        assert_eq!(provider.num_pq_chunks(), config.pq_chunks);

        // Test vector assignment.
        let v0: Vec<u8> = vec![0, 1, 2, 3, 4, 5];
        let v1: Vec<u8> = vec![5, 4, 3, 2, 1, 0];
        let v2: Vec<u8> = vec![0, 1, 0, 1, 0, 1];
        let v3: Vec<u8> = vec![3, 4, 0, 3, 2, 5];
        let v4: Vec<u8> = vec![4, 4, 4, 4, 4, 4];

        let test_vec: Vec<f32> = vec![1.5; config.dim];
        let distance_computer = provider.get_distance_computer().unwrap();
        let query_computer = provider.get_query_computer(&test_vec).unwrap();

        let vecs = [v0.clone(), v1, v2, v3, v4];
        for (i, v) in vecs.iter().enumerate() {
            let expected = generate_expected_vector(v);
            provider.set_vector(i, &expected).unwrap();

            let output = provider.get_vector(i).unwrap();
            assert_eq!(output.version(), &1);
            assert_eq!(output.data(), v);

            let expected_distance: f32 = SquaredL2::evaluate(&*test_vec, &*expected);
            assert_eq!(
                distance_computer.evaluate_similarity(&*test_vec, &output),
                expected_distance
            );
            assert_eq!(
                query_computer.evaluate_similarity(&output),
                expected_distance
            );
        }

        // Test that providing PQ vectors with an invalid version returns infinity
        let invalid_vector = ArcSwap::new(Arc::new(VersionedPQVector::new(v0.clone(), 100)));
        assert_eq!(
            distance_computer.evaluate_similarity(&*test_vec, &invalid_vector.load()),
            f32::INFINITY
        );
        assert_eq!(
            query_computer.evaluate_similarity(&invalid_vector.load()),
            f32::INFINITY
        );
        assert_eq!(
            distance_computer.evaluate_similarity(&invalid_vector.load(), &invalid_vector.load()),
            f32::INFINITY
        );

        // Errors
        assert!(provider.set_vector(10, &test_vec).is_err());
        assert!(provider.get_vector(10).is_err());

        // Make sure that we can detect mis-sized vectors and if we do, the underlying
        // data is not changed.
        let mut too_long = test_vec.clone();
        too_long.push(1.0);
        assert!(provider.set_vector(0, &too_long).is_err());
        assert_eq!(provider.get_vector(0).unwrap().data(), v0);
    }

    // One Schema.
    #[test]
    fn test_single_schema() {
        let config = test_utils::TableConfig {
            dim: 20,
            pq_chunks: 6,
            num_pivots: 16,
            start_value: 1.0,
        };

        let provider = TestMultiPQProviderAsync::new(
            Metric::L2,
            4,
            1,
            test_utils::seed_pivot_table(config),
            None,
            0.0,
            0,
        );

        test_single_schema_as_qvpa(&provider, &config);
    }

    /////////////////
    // Two Schemas //
    /////////////////

    /// An auxiliary helper function is used to avoid writing
    /// `<Provider as QuantVecProviderAsync<DefaultContext, Data>` for every single method.
    ///
    /// This function is closely coupled with `test_double_schema`.
    fn test_double_schema_as_qvpa(
        provider: &TestMultiPQProviderAsync,
        config_new: &test_utils::TableConfig,
        config_old: &test_utils::TableConfig,
    ) {
        let table_new = test_utils::seed_pivot_table(*config_new);
        let table_old = test_utils::seed_pivot_table(*config_old);
        let generate_expected_vector = |v: &[u8], new: bool| {
            if new {
                test_utils::generate_expected_vector(
                    v,
                    table_new.get_chunk_offsets(),
                    config_new.start_value,
                )
            } else {
                test_utils::generate_expected_vector(
                    v,
                    table_old.get_chunk_offsets(),
                    config_old.start_value,
                )
            }
        };

        assert_eq!(provider.num_pq_chunks(), config_new.pq_chunks);

        // Test vector assignment.
        // NOTE: As long as the encodings are in `[1, num_pq_chunks - 1)` - reconstruction
        // should be exact for both schemas.
        assert_eq!(config_new.pq_chunks, 6);
        assert_eq!(config_old.pq_chunks, 6);
        assert_eq!(config_new.start_value, 2.0);
        assert_eq!(config_old.start_value, 1.0);

        // These vectors here all use encodings for the old PQ schema.
        // To get the encoding for the new PQ schema - subtract 1 from each component.
        let v0: Vec<u8> = vec![1, 1, 2, 3, 4, 4];
        let v1: Vec<u8> = vec![5, 4, 3, 2, 1, 1];
        let v2: Vec<u8> = vec![1, 1, 1, 1, 1, 1];
        let v3: Vec<u8> = vec![3, 4, 1, 3, 2, 4];
        let v4: Vec<u8> = vec![4, 4, 4, 4, 4, 4];

        assert_eq!(config_old.dim, config_new.dim);
        let test_vec: Vec<f32> = vec![1.5; config_new.dim];
        let distance_computer = provider.get_distance_computer().unwrap();
        let query_computer = provider.get_query_computer(&test_vec).unwrap();

        // Track whether or not we've seen the old and new versions.
        let mut old_seen = false;
        let mut new_seen = false;
        let vecs = [v0.clone(), v1, v2, v3, v4];
        for (i, v) in vecs.iter().enumerate() {
            provider
                .set_vector(i, &generate_expected_vector(v, false))
                .unwrap();

            let output = provider.get_vector(i).unwrap();

            let version = *output.version();
            let encoding = if version == 1 {
                old_seen = true;
                v.clone()
            } else if version == 2 {
                new_seen = true;
                v.iter().map(|i| i - 1).collect::<Vec<u8>>()
            } else {
                panic!("Unexpected version: {version}");
            };
            assert_eq!(output.data(), encoding);

            let expected = if version == 1 {
                generate_expected_vector(&encoding, false)
            } else {
                generate_expected_vector(&encoding, true)
            };

            let expected_distance: f32 = SquaredL2::evaluate(&*test_vec, &*expected);
            assert_eq!(
                distance_computer.evaluate_similarity(&*test_vec, &output),
                expected_distance
            );
            assert_eq!(
                query_computer.evaluate_similarity(&output),
                expected_distance
            );
        }

        assert!(old_seen);
        assert!(new_seen);

        // Test that providing PQ vectors with an invalid version returns infinity
        let invalid_vector = ArcSwap::new(Arc::new(VersionedPQVector::new(v0.clone(), 100)));
        assert_eq!(
            distance_computer.evaluate_similarity(&*test_vec, &invalid_vector.load()),
            f32::INFINITY
        );
        assert_eq!(
            query_computer.evaluate_similarity(&invalid_vector.load()),
            f32::INFINITY
        );
        assert_eq!(
            distance_computer.evaluate_similarity(&invalid_vector.load(), &invalid_vector.load()),
            f32::INFINITY
        );
    }

    #[test]
    fn test_double_schema() {
        let config_old = test_utils::TableConfig {
            dim: 20,
            pq_chunks: 6,
            num_pivots: 16,
            start_value: 1.0,
        };

        let config_new = test_utils::TableConfig {
            dim: 20,
            pq_chunks: 6,
            num_pivots: 16,
            start_value: 2.0,
        };

        let provider = TestMultiPQProviderAsync::new(
            Metric::L2,
            4,
            1,
            test_utils::seed_pivot_table(config_new),
            Some(test_utils::seed_pivot_table(config_old)),
            0.5,
            0x4644c5bcfe4f985f,
        );

        test_double_schema_as_qvpa(&provider, &config_new, &config_old);
    }
}