diskann-benchmark 0.51.0

DiskANN is a fast approximate nearest neighbor search library for high dimensional data
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/*
 * Copyright (c) Microsoft Corporation.
 * Licensed under the MIT license.
 */

use diskann_benchmark_runner::registry::Benchmarks;

const NAME: &str = "spherical-exhaustive-search";

crate::utils::stub_impl!("spherical-quantization", inputs::exhaustive::Spherical);

// Spherical - requires feature "spherical-quantization"
#[cfg(feature = "spherical-quantization")]
pub(super) fn register_benchmarks(benchmarks: &mut Benchmarks) {
    benchmarks.register(NAME, imp::SphericalQ::<1>);
    benchmarks.register(NAME, imp::SphericalQ::<2>);
    benchmarks.register(NAME, imp::SphericalQ::<4>);
    benchmarks.register(NAME, imp::SphericalQ::<8>);
}

// Stub implementation
#[cfg(not(feature = "spherical-quantization"))]
pub(super) fn register_benchmarks(benchmarks: &mut Benchmarks) {
    imp::register(NAME, benchmarks)
}

////////////////
// SphericalQ //
////////////////

#[cfg(feature = "spherical-quantization")]
mod imp {
    use std::io::Write;

    use diskann_benchmark_runner::{
        dispatcher::{FailureScore, MatchScore},
        utils::{percentiles, MicroSeconds},
        Benchmark, Output,
    };
    use diskann_providers::model::graph::provider::async_::distances::UnwrapErr;
    use diskann_quantization::{
        alloc::{GlobalAllocator, ScopedAllocator},
        bits::{Representation, Unsigned},
        spherical::{DataMut, SphericalQuantizer},
        CompressIntoWith,
    };
    use indicatif::{ProgressBar, ProgressStyle};
    use rand::SeedableRng;
    use rayon::iter::{IndexedParallelIterator, ParallelIterator};
    use serde::Serialize;

    use crate::{
        backend::exhaustive::algos::{self, LinearSearch},
        inputs,
        utils::{
            datafiles::{self, ConvertingLoad},
            recall, SimilarityMeasure,
        },
    };

    macro_rules! write_field {
        ($f:ident, $field:tt, $fmt:literal, $($expr:tt)*) => {
            writeln!($f, concat!("{:>19}: ", $fmt), $field, $($expr)*)
        }
    }

    fn make_progress_bar(
        message: &str,
        count: usize,
        draw_target: indicatif::ProgressDrawTarget,
    ) -> anyhow::Result<ProgressBar> {
        let progress = ProgressBar::with_draw_target(Some(count as u64), draw_target);
        progress.set_style(ProgressStyle::with_template(&format!(
            "{} [{{elapsed_precise}}] {{wide_bar}} {{percent}}",
            message
        ))?);
        Ok(progress)
    }

    /// The dispatcher target for `spherical-quantization` operations.
    pub(super) struct SphericalQ<const NBITS: usize>;

    impl<const NBITS: usize> SphericalQ<NBITS> {
        pub(super) fn run(
            &self,
            input: &inputs::exhaustive::Spherical,
            mut output: &mut dyn Output,
        ) -> anyhow::Result<Results>
        where
            Unsigned: Representation<NBITS>,
            Plan: algos::CreateQuantComputer<Store<NBITS>>,
            diskann_quantization::spherical::iface::Impl<NBITS>:
                diskann_quantization::spherical::iface::Constructible,
            SphericalQuantizer:
                for<'x> CompressIntoWith<&'x [f32], DataMut<'x, NBITS>, ScopedAllocator<'x>>,
        {
            writeln!(output, "{}", input)?;

            // Training
            let data = f32::converting_load(datafiles::BinFile(&input.data), input.data_type)?;
            let start = std::time::Instant::now();
            let metric: diskann_vector::distance::Metric = input.distance.into();
            let quantizer = diskann_quantization::spherical::SphericalQuantizer::train(
                data.as_view(),
                (&input.transform_kind).into(),
                metric.try_into()?,
                input.pre_scale.try_into()?,
                &mut rand::rngs::StdRng::seed_from_u64(input.seed),
                GlobalAllocator,
            )?;

            let training_time: MicroSeconds = start.elapsed().into();

            // Compressing
            let start = std::time::Instant::now();
            let store = {
                let compression_progress =
                    make_progress_bar("compressing", data.nrows(), output.draw_target())?;
                let store = Store::new(
                    data.as_view(),
                    diskann_quantization::spherical::iface::Impl::<NBITS>::new(quantizer)?,
                    &compression_progress,
                )?;
                compression_progress.finish();
                store
            };
            let compression_time: MicroSeconds = start.elapsed().into();

            // Search
            let queries =
                f32::converting_load(datafiles::BinFile(&input.search.queries), input.data_type)?;

            let groundtruth =
                datafiles::load_groundtruth(datafiles::BinFile(&input.search.groundtruth))?;

            let search_progress = make_progress_bar(
                "running search",
                input.query_layouts.len() * queries.nrows(),
                output.draw_target(),
            )?;

            let mut search_results = Vec::<SearchResults>::new();
            let threadpool = rayon::ThreadPoolBuilder::new()
                .num_threads(input.search.num_threads.get())
                .build()?;

            let recall_n = input
                .search
                .recalls
                .recall_n
                .last()
                .ok_or_else(|| anyhow::anyhow!("expected at least one value for `recall_n`"))?;

            for layout in input.query_layouts.iter() {
                let plan = Plan {
                    measure: input.distance,
                    layout: *layout,
                };

                let r = threadpool.install(|| {
                    algos::linear_search(
                        &store,
                        queries.as_view(),
                        &plan,
                        *recall_n,
                        &search_progress,
                    )
                })?;

                let recalls = recall::compute_multiple_recalls(
                    &r.ids,
                    &groundtruth,
                    &input.search.recalls.recall_k,
                    &input.search.recalls.recall_n,
                )?;

                search_results.push(SearchResults::new(
                    r,
                    *layout,
                    input.search.num_threads.get(),
                    recalls,
                )?);
            }
            search_progress.finish();

            // Aggregate and print results.
            let result = Results {
                training_time,
                compression_time,
                search_results,
                quantized_dim: store.quantized_dim(),
                quantized_bytes: store.quantized_bytes(),
                original_dim: data.ncols(),
            };

            writeln!(output, "\n\n{}", result)?;
            Ok(result)
        }
    }

    impl<const NBITS: usize> Benchmark for SphericalQ<NBITS>
    where
        Unsigned: Representation<NBITS>,
        Plan: algos::CreateQuantComputer<Store<NBITS>>,
        diskann_quantization::spherical::iface::Impl<NBITS>:
            diskann_quantization::spherical::iface::Constructible,
        SphericalQuantizer:
            for<'x> CompressIntoWith<&'x [f32], DataMut<'x, NBITS>, ScopedAllocator<'x>>,
    {
        type Input = inputs::exhaustive::Spherical;
        type Output = Results;

        fn try_match(
            &self,
            input: &inputs::exhaustive::Spherical,
        ) -> Result<MatchScore, FailureScore> {
            let num_bits = input.num_bits.get();
            if num_bits == NBITS {
                Ok(MatchScore(0))
            } else {
                Err(FailureScore(
                    NBITS.abs_diff(num_bits).try_into().unwrap_or(u32::MAX),
                ))
            }
        }

        fn description(
            &self,
            f: &mut std::fmt::Formatter<'_>,
            input: Option<&inputs::exhaustive::Spherical>,
        ) -> std::fmt::Result {
            match input {
                None => {
                    writeln!(
                        f,
                        "- Exhaustive search for {}-bit spherical quantization",
                        NBITS
                    )?;
                    writeln!(f, "- Requires `float32` data")?;
                    writeln!(f, "- Implements `squared_l2` or `inner_product` distance")?;
                }
                Some(from) => {
                    if from.num_bits.get() != NBITS {
                        writeln!(
                            f,
                            "- Expected \"num_bits = {}\", instead got {}",
                            NBITS,
                            from.num_bits.get(),
                        )?;
                    }
                }
            }
            Ok(())
        }

        fn run(
            &self,
            input: &inputs::exhaustive::Spherical,
            _checkpoint: diskann_benchmark_runner::Checkpoint<'_>,
            output: &mut dyn Output,
        ) -> anyhow::Result<Results> {
            self.run(input, output)
        }
    }

    /// Results from an end-to-end run of Spherical Quantization.
    #[derive(Debug, Serialize)]
    pub(super) struct Results {
        /// The time it takes to generate the base quantizer.
        training_time: MicroSeconds,
        /// How long it takes to compress the raw data.
        compression_time: MicroSeconds,
        /// The effective dimensionality of the post-transformed data.
        ///
        /// This dimensionality can be higher or lower than the original dimensionality,
        /// depending on the mechanics of the distance-preserving transform.
        quantized_dim: usize,
        /// The minimum number of bytes required for each compressed vecto4r.
        quantized_bytes: usize,
        /// The dimensionality of the uncompressed data.
        original_dim: usize,
        /// Results for each search kind (varying over query layouts).
        search_results: Vec<SearchResults>,
    }

    impl std::fmt::Display for Results {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            write_field!(f, "Training Time", "{}s", self.training_time.as_seconds())?;
            write_field!(
                f,
                "Compression Time",
                "{}s",
                self.compression_time.as_seconds()
            )?;
            write_field!(f, "Quantized Dim", "{}", self.quantized_dim)?;
            write_field!(f, "Quantized Bytes", "{}", self.quantized_bytes)?;
            write_field!(f, "Original Dim", "{}\n\n", self.original_dim)?;

            for (i, v) in self.search_results.iter().enumerate() {
                write_field!(f, "Run", "{} of {}", i + 1, self.search_results.len())?;
                writeln!(f, "{}", v)?;
            }
            Ok(())
        }
    }

    #[derive(Debug, Serialize)]
    struct SearchResults {
        num_threads: usize,
        time: MicroSeconds,
        qps: f64,
        layout: inputs::exhaustive::SphericalQuery,

        // Latencies
        mean_preprocess: f64,
        p90_preprocess: MicroSeconds,
        p99_preprocess: MicroSeconds,

        mean_search: f64,
        p90_search: MicroSeconds,
        p99_search: MicroSeconds,

        // Values for each combination of recalls.
        recalls: Vec<recall::RecallMetrics>,
    }

    impl SearchResults {
        fn new(
            mut search: LinearSearch,
            layout: inputs::exhaustive::SphericalQuery,
            num_threads: usize,
            recalls: Vec<recall::RecallMetrics>,
        ) -> Result<Self, percentiles::CannotBeEmpty> {
            let preprocess_latency = percentiles::compute_percentiles(&mut search.preprocess)?;
            let search_latency = percentiles::compute_percentiles(&mut search.search)?;

            let time = search.total;
            Ok(Self {
                num_threads,
                time,
                qps: (search.ids.nrows() as f64) / time.as_seconds(),
                layout,
                mean_preprocess: preprocess_latency.mean,
                p90_preprocess: preprocess_latency.p90,
                p99_preprocess: preprocess_latency.p99,
                mean_search: search_latency.mean,
                p90_search: search_latency.p90,
                p99_search: search_latency.p99,
                recalls,
            })
        }
    }

    impl std::fmt::Display for SearchResults {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            write_field!(f, "Query Layout", "{}", self.layout)?;
            write_field!(f, "Total", "{:.2}s", self.time.as_seconds())?;
            write_field!(f, "QPS", "{:.3}", self.qps)?;
            write_field!(f, "Num Threads", "{}", self.num_threads)?;
            write_field!(
                f,
                "Preprocess Latency",
                "{:.1}us ({:.1})",
                self.mean_preprocess,
                self.p99_preprocess.as_f64(),
            )?;
            write_field!(
                f,
                "Search Latency",
                "{:.1}us ({:.1})",
                self.mean_search,
                self.p99_search.as_f64(),
            )?;

            writeln!(f)?;

            let mut table = diskann_benchmark_runner::utils::fmt::Table::new(
                ["K", "N", "Recall (%)"],
                self.recalls.len(),
            );
            self.recalls.iter().enumerate().for_each(|(row, r)| {
                let mut row = table.row(row);
                row.insert(r.recall_k, 0);
                row.insert(r.recall_n, 1);
                row.insert(format!("{:.3}", 100.0 * r.average), 2);
            });

            write!(f, "{}", table)
        }
    }

    /// A store for quantized data.
    pub(super) struct Store<const NBITS: usize> {
        // The number of bytes to take from each row.
        bytes: usize,
        data: diskann_utils::views::Matrix<u8>,
        plan: diskann_quantization::spherical::iface::Impl<NBITS>,
    }

    impl<const NBITS: usize> Store<NBITS>
    where
        Unsigned: Representation<NBITS>,
        SphericalQuantizer:
            for<'a> CompressIntoWith<&'a [f32], DataMut<'a, NBITS>, ScopedAllocator<'a>>,
    {
        fn new(
            input: diskann_utils::views::MatrixView<f32>,
            plan: diskann_quantization::spherical::iface::Impl<NBITS>,
            progress: &ProgressBar,
        ) -> anyhow::Result<Self> {
            // Both the dimensionality of the compressed vector and the exact number of bytes
            // needed to store it can vary.
            //
            // The APIs below should correctly handle these variables.
            let output_dim = plan.quantizer().output_dim();
            let bytes =
                diskann_quantization::spherical::DataRef::<NBITS>::canonical_bytes(output_dim);
            let mut data = diskann_utils::views::Matrix::new(0, input.nrows(), bytes);

            // Compress the data.
            #[allow(clippy::disallowed_methods)]
            data.par_row_iter_mut()
                .zip(input.par_row_iter())
                .try_for_each(|(d, i)| -> anyhow::Result<()> {
                    let c =
                        diskann_quantization::spherical::DataMut::<NBITS>::from_canonical_back_mut(
                            &mut d[..bytes],
                            output_dim,
                        )?;
                    plan.quantizer()
                        .compress_into_with(i, c, ScopedAllocator::global())?;
                    progress.inc(1);
                    Ok(())
                })?;

            Ok(Self { bytes, data, plan })
        }

        /// Return the effective dimensionality of the compressed data.
        fn quantized_dim(&self) -> usize {
            self.plan.quantizer().output_dim()
        }

        /// Return the number of bytes required to store each quantized vector.
        fn quantized_bytes(&self) -> usize {
            self.bytes
        }
    }

    impl<const NBITS: usize> algos::QuantStore for Store<NBITS>
    where
        Unsigned: Representation<NBITS>,
    {
        type Item<'a>
            = diskann_quantization::spherical::iface::Opaque<'a>
        where
            Self: 'a;

        fn iter(&self) -> impl Iterator<Item = Self::Item<'_>> {
            self.data
                .row_iter()
                .map(move |r| diskann_quantization::spherical::iface::Opaque::new(&r[..self.bytes]))
        }
    }

    /// Exhaustive searching depends both on the similarity measure used and the number
    /// of bits use to compress the query.
    ///
    /// This struct uses a factor pattern to realize a distance computer for each valid
    /// combination.
    pub(super) struct Plan {
        measure: SimilarityMeasure,
        layout: inputs::exhaustive::SphericalQuery,
    }

    impl<const NBITS: usize> algos::CreateQuantComputer<Store<NBITS>> for Plan
    where
        Unsigned: Representation<NBITS>,
        diskann_quantization::spherical::iface::Impl<NBITS>:
            diskann_quantization::spherical::iface::Quantizer,
    {
        type Computer<'a> = UnwrapErr<
            diskann_quantization::spherical::iface::QueryComputer,
            diskann_quantization::spherical::iface::QueryDistanceError,
        >;

        fn create_quant_computer<'a>(
            &'a self,
            store: &'a Store<NBITS>,
            query: &[f32],
        ) -> anyhow::Result<Self::Computer<'a>> {
            use diskann_quantization::spherical::iface::Quantizer;

            let allow_rescale = self.measure == SimilarityMeasure::InnerProduct;
            Ok(store
                .plan
                .fused_query_computer(
                    query,
                    self.layout.into(),
                    allow_rescale,
                    GlobalAllocator,
                    ScopedAllocator::global(),
                )
                .map(UnwrapErr::new)?)
        }
    }
}