diskann-quantization 0.50.1

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
/*
 * Copyright (c) Microsoft Corporation.
 * Licensed under the MIT license.
 */

// A collection of test helpers to ensure uniformity across tables.
use diskann_utils::views::Matrix;
#[cfg(not(miri))]
use diskann_utils::views::{MatrixView, MutMatrixView};
#[cfg(not(miri))]
use rand::seq::IndexedRandom;
use rand::{
    Rng, SeedableRng,
    distr::{Distribution, Uniform},
};

use crate::traits::CompressInto;
use crate::views::{self, ChunkOffsets, ChunkOffsetsView};

/////////////////////////
// Compression Helpers //
/////////////////////////

// TESTING STRATEGY:
//
// We need the following to test the block compression primitive:
//
// 1. A collection of pivots with known entries.
// 2. A data corpus where we know the mapping of chunks and rows to a center in the
//    previously mentioned known collection of pivots.
//
// This test fulfills these goals in the following way:
//
// ## Pivot Seeding
//
// Use a barrel-shifting approach to seeding the contents of each chunk pivots.
//
// Chunk 0 will contain the following values:
// ```
// 0.25     -0.25      0.25     -0.25     ...  +/- 0.25
// 1.25      0.25      1.25      0.25     ...  1.0 +/- 0.25
// ...
// L + 0.25  L - 0.25  L + 0.25  L - 0.25 ...  L +/- 0.25
// ```
// where Chunk 0 has (L+1) centers (of any dimension).
//
// The integer values are offset by `0.25` to yield a non-zero distance and (in this
// example) are configured so a query with all entries equal to `I <= L` will be mapped to
// row `I`.
//
// Chunk 1 will contain the following:
// ```
// 1.25      0.25      1.25      0.25     ...  1.0 +/- 0.25
// 2.25      1.25      2.25      1.25     ...  2.0 +/- 0.25
// ...
// L + 0.25  L - 0.25  L + 0.25  L - 0.25 ...  L +/- 0.25
// 0.25     -0.25      0.25     -0.25     ...    +/- 0.25
// ```
// That is, Chunk 1 will have the same contents Chunk 0, but the first row of Chunk 0 will
// be moved to the end.
//
// Chunk 2 will continue the pattern by moving the first row of Chunk 1 to the end.
//
// This pattern allows us to compute which center a properly seeded dataset chunk should
// match while providing enough entropy to make sure intermediate values are being
// computed properly.
//
// ## Data Seeding
//
// We will keep data seeding simple, using a seeded random number generate to pick a
// value between O and L and initialize all dimensions with that value.
//
// During seeding, we will record this value so that results yielded by the compression
// algorithm can be checked.
//
// The formula for going from a chunk with index "C" with the assigned value "K" is:
// ```math
// K - C mod (L + 1)
// ```

/// Seed pivot tables for the provided schema using the strategy outlined in the
/// introduction documentation to the test module.
pub(super) fn create_pivot_tables(
    schema: ChunkOffsets,
    num_centers: usize,
) -> (Matrix<f32>, ChunkOffsets) {
    let mut pivots = Matrix::<f32>::new(0.0, num_centers, schema.dim());

    (0..schema.len()).for_each(|chunk| {
        let range = schema.at(chunk);

        (0..num_centers).for_each(|center| {
            let buffer = &mut pivots.row_mut(center)[range.clone()];

            // It's okay if this conversion is lossy (though the magnitude of the
            // numbers involved means that this is almost certainly a lossless
            // conversion).
            //
            // The "remainder" operation is what performs the "barrel shifting"
            // for the centers.
            let base = ((center + chunk) % num_centers) as f32;
            buffer.iter_mut().enumerate().for_each(|(dim, b)| {
                // Flip-flop adding and subtracting 0.25.
                *b = if dim % 2 == 0 {
                    base + 0.25
                } else {
                    base - 0.25
                };
            });
        });
    });

    (pivots, schema)
}

/// Initialize a dataset for the provided schema using the strategy outlined in
/// the test module introduction documentation.
///
/// Returns:
///
/// * The initialized dataset as a Matrix.
/// * The expected center as a Matrix.
pub(super) fn create_dataset<R: Rng>(
    schema: ChunkOffsetsView<'_>,
    num_centers: usize,
    num_data: usize,
    rng: &mut R,
) -> (Matrix<f32>, Matrix<usize>) {
    let mut data = Matrix::<f32>::new(0.0, num_data, schema.dim());
    let mut expected = Matrix::<usize>::new(0, num_data, schema.len());

    let dist = Uniform::new(0, num_centers).unwrap();
    for row_index in 0..data.nrows() {
        let mut row_view = views::MutChunkView::new(data.row_mut(row_index), schema).unwrap();
        for chunk in 0..schema.len() {
            let value = rng.sample(dist);
            row_view[chunk].fill(value as f32);

            // Compute the expected value based on the rotation scheme used in pivot
            // seeding.
            let value: i64 = value.try_into().unwrap();
            let num_centers: i64 = num_centers.try_into().unwrap();
            let chunk_i64: i64 = chunk.try_into().unwrap();

            let expected_index: u64 = (value - chunk_i64)
                .rem_euclid(num_centers)
                .try_into()
                .unwrap();

            expected[(row_index, chunk)] = expected_index as usize;
        }
    }

    (data, expected)
}

/////////////////////////////////////////
// Testing `CompressInto<[f32], [u8]>` //
/////////////////////////////////////////

// A cantralized test for error handling in `CompressInto<[f32], [u8]>`
pub(super) fn check_pqtable_single_compression_errors<T>(
    build: &dyn Fn(Matrix<f32>, ChunkOffsets) -> T,
    context: &dyn std::fmt::Display,
) where
    T: for<'a, 'b> CompressInto<&'a [f32], &'b mut [u8]>,
{
    let dim = 10;
    let num_chunks = 3;
    let offsets = ChunkOffsets::new(Box::new([0, 4, 9, 10])).unwrap();

    // Set up `ncenters > 256`.
    {
        let pivots = Matrix::new(0.0, 257, dim);
        let table = build(pivots, offsets.clone());

        let input = vec![f32::default(); dim];
        let mut output = vec![u8::MAX; num_chunks];
        let result = table.compress_into(input.as_slice(), output.as_mut_slice());
        assert!(result.is_err());
        assert_eq!(
            result.unwrap_err().to_string(),
            "num centers (257) must be at most 256 to compress into a byte vector",
            "{}",
            context
        );
        assert!(
            output.iter().all(|i| *i == u8::MAX),
            "output vector should be unmodified -- {}",
            context
        );
    }

    // Setup input dim not equal to expected.
    {
        let pivots = Matrix::new(0.0, 10, dim);
        let table = build(pivots, offsets.clone());

        let input = vec![f32::default(); dim - 1];
        let mut output = vec![u8::MAX; num_chunks];
        let result = table.compress_into(input.as_slice(), output.as_mut_slice());
        assert!(result.is_err());
        assert_eq!(
            result.unwrap_err().to_string(),
            format!("invalid input len - expected {}, got {}", dim, dim - 1),
            "{}",
            context,
        );
        assert!(
            output.iter().all(|i| *i == u8::MAX),
            "output vector should be unmodified -- {}",
            context
        );
    }

    // Setup output dim not equal to expected.
    {
        let pivots = Matrix::new(0.0, 10, dim);
        let table = build(pivots, offsets.clone());

        let input = vec![f32::default(); dim];
        let mut output = vec![u8::MAX; num_chunks - 1];
        let result = table.compress_into(input.as_slice(), output.as_mut_slice());
        assert!(result.is_err());
        assert_eq!(
            result.unwrap_err().to_string(),
            format!(
                "invalid PQ buffer len - expected {}, got {}",
                num_chunks,
                num_chunks - 1,
            ),
            "{}",
            context,
        );
        assert!(
            output.iter().all(|i| *i == u8::MAX),
            "output vector should be unmodified -- {}",
            context,
        );
    }

    // Infinity or NaN detection.
    {
        let offsets = ChunkOffsets::new(Box::new([
            0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136,
        ]))
        .unwrap();

        let (pivots, o) = create_pivot_tables(offsets.clone(), 7);
        let table = build(pivots, o);

        let mut buf: Box<[f32]> = (0..offsets.dim()).map(|_| 0.0).collect();
        let mut output: Box<[u8]> = (0..offsets.len()).map(|_| 0).collect();

        fn clear(x: &mut [f32]) {
            x.iter_mut().for_each(|i| *i = 0.0);
        }

        let mut rng = rand::rngs::StdRng::seed_from_u64(0x90a10423fdd8f1cf);
        let values = [f32::NEG_INFINITY, f32::INFINITY, f32::NAN];

        // Feed in positive infinity, negative infinity, and NaN into each chunk.
        for chunk in 0..offsets.len() {
            let range = offsets.at(chunk);
            let distribution = Uniform::new(range.start, range.end).unwrap();
            let expected = format!(
                "a value of infinity or NaN was observed while compressing chunk {}",
                chunk
            );

            for &value in values.iter() {
                clear(&mut buf);
                buf[distribution.sample(&mut rng)] = value;
                let err = table
                    .compress_into(&buf, &mut output)
                    .unwrap_err()
                    .to_string();

                assert!(
                    err.contains(&expected),
                    "wrong error message for {} - expected \"{}\", got \"{}\"",
                    value,
                    expected,
                    err
                );
            }
        }
    }
}

////////////////////////////////////////////////////////////////////
// Testing `CompressInto<MatrixView<'_, f32>, MarixView<'_, u8>>` //
////////////////////////////////////////////////////////////////////

// A cantralized test for error handling in `CompressInto<[f32], [u8]>`
#[cfg(not(miri))]
pub(super) fn check_pqtable_batch_compression_errors<T>(
    build: &dyn Fn(Matrix<f32>, ChunkOffsets) -> T,
    context: &dyn std::fmt::Display,
) where
    T: for<'a> CompressInto<MatrixView<'a, f32>, MutMatrixView<'a, u8>>,
{
    let dim = 10;
    let num_chunks = 3;
    let offsets = ChunkOffsets::new(Box::new([0, 4, 9, 10])).unwrap();

    let batchsize = 10;

    // Set up `ncenters > 256`.
    {
        let pivots = Matrix::new(0.0, 257, dim);
        let table = build(pivots, offsets.clone());

        let input = Matrix::new(f32::default(), batchsize, dim);
        let mut output = Matrix::new(u8::MAX, batchsize, num_chunks);
        let result = table.compress_into(input.as_view(), output.as_mut_view());
        assert!(result.is_err());
        assert_eq!(
            result.unwrap_err().to_string(),
            "num centers (257) must be at most 256 to compress into a byte vector",
            "{}",
            context
        );
        assert!(
            output.as_slice().iter().all(|i| *i == u8::MAX),
            "output vector should be unmodified -- {}",
            context
        );
    }

    // Setup input dim not equal to expected.
    {
        let pivots = Matrix::new(0.0, 10, dim);
        let table = build(pivots, offsets.clone());

        let input = Matrix::new(f32::default(), batchsize, dim - 1);
        let mut output = Matrix::new(u8::MAX, batchsize, num_chunks);
        let result = table.compress_into(input.as_view(), output.as_mut_view());
        assert!(result.is_err());
        assert_eq!(
            result.unwrap_err().to_string(),
            format!("invalid input len - expected {}, got {}", dim, dim - 1),
            "{}",
            context,
        );
        assert!(
            output.as_slice().iter().all(|i| *i == u8::MAX),
            "output vector should be unmodified -- {}",
            context
        );
    }

    // Setup output dim not equal to expected.
    {
        let pivots = Matrix::new(0.0, 10, dim);
        let table = build(pivots, offsets.clone());

        let input = Matrix::new(f32::default(), batchsize, dim);
        let mut output = Matrix::new(u8::MAX, batchsize, num_chunks - 1);
        let result = table.compress_into(input.as_view(), output.as_mut_view());

        assert!(result.is_err());
        assert_eq!(
            result.unwrap_err().to_string(),
            format!(
                "invalid PQ buffer len - expected {}, got {}",
                num_chunks,
                num_chunks - 1,
            ),
            "{}",
            context,
        );
        assert!(
            output.as_slice().iter().all(|i| *i == u8::MAX),
            "output vector should be unmodified -- {}",
            context,
        );
    }

    // Num rows are different.
    {
        let pivots = Matrix::new(0.0, 10, dim);
        let table = build(pivots, offsets.clone());

        let input = Matrix::new(f32::default(), batchsize, dim);
        let mut output = Matrix::new(u8::MAX, batchsize - 1, num_chunks);
        let result = table.compress_into(input.as_view(), output.as_mut_view());

        assert!(result.is_err());
        assert_eq!(
            result.unwrap_err().to_string(),
            format!(
                "input and output must have the same number of rows - instead, got {0} and {1} \
                 (respectively)",
                batchsize,
                batchsize - 1,
            ),
            "{}",
            context,
        );
        assert!(
            output.as_slice().iter().all(|i| *i == u8::MAX),
            "output vector should be unmodified -- {}",
            context,
        );
    }

    // Infinity and NaN detection.
    {
        let offsets = ChunkOffsets::new(Box::new([
            0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136,
        ]))
        .unwrap();

        let (pivots, o) = create_pivot_tables(offsets.clone(), 7);
        let table = build(pivots, o);

        let num_points = 15;
        let mut buf = Matrix::<f32>::new(0.0, num_points, offsets.dim());
        let mut output = Matrix::<u8>::new(0, num_points, offsets.len());

        fn clear<T: Default>(mut x: MutMatrixView<T>) {
            x.as_mut_slice().iter_mut().for_each(|i| *i = T::default());
        }

        let mut rng = rand::rngs::StdRng::seed_from_u64(0x8aa9f8cc50260d5c);

        let sample = [f32::NEG_INFINITY, f32::INFINITY, f32::NAN];

        // Feed in positive infinity, negative infinity, and NaN into each chunk.
        for chunk in 0..offsets.len() {
            let range = offsets.at(chunk);
            let distribution = Uniform::new(range.start, range.end).unwrap();

            for row in 0..num_points {
                clear(buf.as_mut_view());
                let value = *sample.choose(&mut rng).unwrap();
                buf[(row, distribution.sample(&mut rng))] = value;
                let err = table
                    .compress_into(buf.as_view(), output.as_mut_view())
                    .expect_err(&format!("expected a value of {}", value));

                let message = err.to_string();
                let expected = format!(
                    "a value of infinity or NaN was observed while compressing chunk {} \
                     of batch input {}",
                    chunk, row
                );

                assert!(
                    message.contains(&expected),
                    "wrong error message - expected \"{}\", got \"{}\"",
                    expected,
                    err
                );
            }
        }
    }
}