diskann-benchmark 0.50.1

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

use diskann_benchmark_runner::registry::Benchmarks;

// Create a stub-module if the "spherical-quantization" feature is disabled.
crate::utils::stub_impl!(
    "spherical-quantization",
    inputs::async_::SphericalQuantBuild
);

pub(super) fn register_benchmarks(benchmarks: &mut Benchmarks) {
    const NAME: &str = "async-spherical-quantization";

    #[cfg(feature = "spherical-quantization")]
    {
        benchmarks.register::<imp::SphericalQ<'static, 1>>(NAME);
        benchmarks.register::<imp::SphericalQ<'static, 2>>(NAME);
        benchmarks.register::<imp::SphericalQ<'static, 4>>(NAME);
    }

    // Stub implementation
    #[cfg(not(feature = "spherical-quantization"))]
    imp::register(NAME, benchmarks)
}

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

#[cfg(feature = "spherical-quantization")]
mod imp {
    use diskann::graph::StartPointStrategy;
    use diskann_benchmark_core as benchmark_core;
    use diskann_benchmark_runner::{
        describeln,
        dispatcher::{DispatchRule, FailureScore, MatchScore},
        utils::{datatype, MicroSeconds},
        Benchmark, Checkpoint, Output,
    };
    use diskann_providers::{
        index::diskann_async::{self},
        model::graph::provider::async_::{common::NoDeletes, inmem},
    };
    use diskann_quantization::alloc::GlobalAllocator;
    use diskann_utils::views::Matrix;
    use rand::SeedableRng;
    use serde::Serialize;
    use std::{io::Write, sync::Arc};

    use crate::{
        backend::index::{
            benchmarks::BuildAndSearch,
            build::{self, only_single_insert, BuildStats},
            result::AggregatedSearchResults,
            search,
        },
        inputs::{
            async_::{SearchPhase, SphericalQuantBuild},
            exhaustive,
        },
        utils::{
            self, datafiles,
            filters::{generate_bitmaps, setup_filter_strategies},
        },
    };

    /// The dispatcher target for `spherical-quantization` operations.
    pub(super) struct SphericalQ<'a, const NBITS: usize> {
        input: &'a SphericalQuantBuild,
    }

    impl<'a, const NBITS: usize> SphericalQ<'a, NBITS> {
        pub(super) fn new(input: &'a SphericalQuantBuild) -> Self {
            Self { input }
        }
    }

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

    #[derive(Debug, Serialize)]
    struct SearchRun {
        layout: exhaustive::SphericalQuery,
        results: AggregatedSearchResults,
    }

    #[derive(Debug, Serialize)]
    pub struct SphericalBuildResult {
        training_time: MicroSeconds,
        quantized_dim: usize,
        quantized_bytes: usize,
        original_dim: usize,
        build: BuildStats,
        runs: Vec<SearchRun>,
    }

    impl SphericalBuildResult {
        fn append(&mut self, run: SearchRun) {
            self.runs.push(run)
        }
    }

    impl std::fmt::Display for SphericalBuildResult {
        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, "Quantized Dim", "{}", self.quantized_dim)?;
            write_field!(f, "Quantized Bytes", "{}", self.quantized_bytes)?;
            write_field!(f, "Original Dim", "{}\n\n", self.original_dim)?;

            writeln!(f, "{}", self.build)?;

            for (i, v) in self.runs.iter().enumerate() {
                write_field!(f, "Run", "{} of {}", i + 1, self.runs.len())?;
                write_field!(f, "Query Layout", "{}", v.layout)?;
                v.results.fmt(f)?;
            }
            Ok(())
        }
    }

    macro_rules! build_and_search {
        ($N:literal) => {
            impl Benchmark for SphericalQ<'static, $N> {
                type Input = SphericalQuantBuild;
                type Output = SphericalBuildResult;

                fn try_match(input: &SphericalQuantBuild) -> Result<MatchScore, FailureScore> {
                    let mut failure_score: Option<u32> = None;
                    if input.build.multi_insert.is_some() {
                        failure_score = Some(1);
                    }

                    if let Err(FailureScore(_)) =
                        datatype::Type::<f32>::try_match(&input.build.data_type)
                    {
                        *failure_score.get_or_insert(0) += 1;
                    }

                    let num_bits = input.num_bits.get();
                    if num_bits != $N {
                        *failure_score.get_or_insert(0) += ($N as usize)
                            .abs_diff(num_bits)
                            .try_into()
                            .unwrap_or(u32::MAX);
                    }

                    match failure_score {
                        None => Ok(MatchScore(0)),
                        Some(score) => Err(FailureScore(score)),
                    }
                }

                fn description(
                    f: &mut std::fmt::Formatter<'_>,
                    input: Option<&SphericalQuantBuild>,
                ) -> std::fmt::Result {
                    match input {
                        None => {
                            describeln!(
                                f,
                                "- Index Build and Search using {}-bit spherical quantization",
                                $N
                            )?;
                            describeln!(f, "- Requires `float32` data")?;
                            describeln!(
                                f,
                                "- Implements `squared_l2` or `inner_product` distance",
                            )?;
                            describeln!(f, "- Does not support multi-insert")?;
                        }
                        Some(input) => {
                            let num_bits = input.num_bits.get();
                            if num_bits != $N {
                                describeln!(f, "- Expected {} bits, got {}", $N, num_bits)?;
                            }

                            if input.build.multi_insert.is_some() {
                                describeln!(
                                    f,
                                    "- Spherical Quantization does not support multi-insert"
                                )?;
                            }

                            if datatype::Type::<f32>::try_match(&input.build.data_type).is_err() {
                                describeln!(
                                    f,
                                    "- Only `float32` data type is supported. Instead, got {}",
                                    input.build.data_type
                                )?;
                            }
                        }
                    }
                    Ok(())
                }

                fn run(
                    input: &SphericalQuantBuild,
                    checkpoint: Checkpoint<'_>,
                    output: &mut dyn Output,
                ) -> anyhow::Result<SphericalBuildResult> {
                    let sq = SphericalQ::<$N>::new(input);
                    BuildAndSearch::run(sq, checkpoint, output)
                }
            }

            impl<'a> BuildAndSearch<'a> for SphericalQ<'a, $N> {
                type Data = SphericalBuildResult;
                fn run(
                    self,
                    _checkpoint: Checkpoint<'_>,
                    mut output: &mut dyn Output,
                ) -> Result<Self::Data, anyhow::Error> {
                    writeln!(output, "{}", self.input)?;

                    let build = &self.input.build;

                    let data: Arc<Matrix<f32>> =
                        Arc::new(datafiles::load_dataset(datafiles::BinFile(&build.data))?);

                    let start = std::time::Instant::now();
                    let m: diskann_vector::distance::Metric = build.distance.into();
                    let pre_scale = match self.input.pre_scale {
                        Some(v) => v.try_into()?,
                        None => diskann_quantization::spherical::PreScale::None,
                    };

                    let quantizer = diskann_quantization::spherical::SphericalQuantizer::train(
                        data.as_view(),
                        (&self.input.transform_kind).into(),
                        m.try_into()?,
                        pre_scale,
                        &mut rand::rngs::StdRng::seed_from_u64(self.input.seed),
                        GlobalAllocator,
                    )?;

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

                    // We manually inline the build and search loops because we support
                    // multiple different kinds of searches.
                    let index = diskann_async::new_quant_index::<f32, _, _>(
                        self.input.try_as_config()?.build()?,
                        self.input.inmem_parameters(data.nrows(), data.ncols()),
                        diskann_quantization::spherical::iface::Impl::<$N>::new(quantizer)?,
                        NoDeletes,
                    )?;

                    build::set_start_points(
                        index.provider(),
                        data.as_view(),
                        StartPointStrategy::Medoid,
                    )?;

                    let original_dim = data.ncols();
                    let build_stats = only_single_insert(
                        index.clone(),
                        inmem::spherical::Quantized::build(),
                        data.clone(),
                        &build,
                        output,
                    )?;

                    let mut result = SphericalBuildResult {
                        training_time,
                        quantized_dim: index.provider().aux_vectors.output_dim(),
                        quantized_bytes: index.provider().aux_vectors.bytes(),
                        original_dim,
                        build: build_stats,
                        runs: Vec::new(),
                    };

                    match &self.input.search_phase {
                        SearchPhase::Topk(search_phase) => {
                            // Handle Topk search phase

                            // Save construction stats before running queries.
                            _checkpoint.checkpoint(&result)?;

                            let queries: Arc<Matrix<f32>> = Arc::new(datafiles::load_dataset(
                                datafiles::BinFile(&search_phase.queries),
                            )?);

                            let groundtruth = datafiles::load_groundtruth(datafiles::BinFile(
                                &search_phase.groundtruth,
                            ))?;

                            let steps = search::knn::SearchSteps::new(
                                search_phase.reps,
                                &search_phase.num_threads,
                                &search_phase.runs,
                            );

                            for &layout in self.input.query_layouts.iter() {
                                let knn = benchmark_core::search::graph::KNN::new(
                                    index.clone(),
                                    queries.clone(),
                                    benchmark_core::search::graph::Strategy::broadcast(
                                        inmem::spherical::Quantized::search(layout.into()),
                                    ),
                                )?;

                                let search_results = search::knn::run(&knn, &groundtruth, steps)?;
                                result.append(SearchRun {
                                    layout,
                                    results: AggregatedSearchResults::Topk(search_results),
                                });
                            }
                            writeln!(output, "\n\n{}", result)?;
                            Ok(result)
                        }
                        SearchPhase::Range(search_phase) => {
                            // Handle Range search phase

                            // Save construction stats before running queries.
                            _checkpoint.checkpoint(&result)?;

                            let queries: Arc<Matrix<f32>> = Arc::new(datafiles::load_dataset(
                                datafiles::BinFile(&search_phase.queries),
                            )?);

                            let groundtruth = datafiles::load_range_groundtruth(
                                datafiles::BinFile(&search_phase.groundtruth),
                            )?;

                            let steps = search::range::RangeSearchSteps::new(
                                search_phase.reps,
                                &search_phase.num_threads,
                                &search_phase.runs,
                            );

                            for &layout in self.input.query_layouts.iter() {
                                let range = benchmark_core::search::graph::Range::new(
                                    index.clone(),
                                    queries.clone(),
                                    benchmark_core::search::graph::Strategy::broadcast(
                                        inmem::spherical::Quantized::search(layout.into()),
                                    ),
                                )?;

                                let search_results =
                                    search::range::run(&range, &groundtruth, steps)?;

                                result.append(SearchRun {
                                    layout,
                                    results: AggregatedSearchResults::Range(search_results),
                                });
                            }

                            writeln!(output, "\n\n{}", result)?;
                            Ok(result)
                        }
                        SearchPhase::TopkBetaFilter(search_phase) => {
                            // Handle Beta Filtered Topk search phase

                            // Save construction stats before running queries.
                            _checkpoint.checkpoint(&result)?;

                            let queries: Arc<Matrix<f32>> = Arc::new(datafiles::load_dataset(
                                datafiles::BinFile(&search_phase.queries),
                            )?);

                            let groundtruth = datafiles::load_range_groundtruth(
                                datafiles::BinFile(&search_phase.groundtruth),
                            )?;

                            let steps = search::knn::SearchSteps::new(
                                search_phase.reps,
                                &search_phase.num_threads,
                                &search_phase.runs,
                            );

                            let bit_maps = generate_bitmaps(
                                &search_phase.query_predicates,
                                &search_phase.data_labels,
                            )?;

                            let label_providers: Vec<_> = bit_maps
                                .into_iter()
                                .map(utils::filters::as_query_label_provider)
                                .collect();

                            for &layout in self.input.query_layouts.iter() {
                                let strategy = inmem::spherical::Quantized::search(layout.into());
                                let search_strategies = setup_filter_strategies(
                                    search_phase.beta,
                                    label_providers.iter().cloned(),
                                    strategy.clone(),
                                );

                                let knn = benchmark_core::search::graph::KNN::new(
                                    index.clone(),
                                    queries.clone(),
                                    benchmark_core::search::graph::Strategy::Collection(
                                        search_strategies.into(),
                                    ),
                                )?;

                                let search_results = search::knn::run(&knn, &groundtruth, steps)?;

                                result.append(SearchRun {
                                    layout,
                                    results: AggregatedSearchResults::Topk(search_results),
                                });
                            }
                            writeln!(output, "\n\n{}", result)?;
                            Ok(result)
                        }
                        SearchPhase::TopkMultihopFilter(search_phase) => {
                            // Handle Beta Filtered Topk search phase

                            // Save construction stats before running queries.
                            _checkpoint.checkpoint(&result)?;

                            let queries: Arc<Matrix<f32>> = Arc::new(datafiles::load_dataset(
                                datafiles::BinFile(&search_phase.queries),
                            )?);

                            let groundtruth = datafiles::load_groundtruth(datafiles::BinFile(
                                &search_phase.groundtruth,
                            ))?;

                            let steps = search::knn::SearchSteps::new(
                                search_phase.reps,
                                &search_phase.num_threads,
                                &search_phase.runs,
                            );

                            let bit_maps = generate_bitmaps(
                                &search_phase.query_predicates,
                                &search_phase.data_labels,
                            )?;

                            let bit_map_filters: Arc<[_]> = bit_maps
                                .into_iter()
                                .map(utils::filters::as_query_label_provider)
                                .collect();

                            for &layout in self.input.query_layouts.iter() {
                                let multihop = benchmark_core::search::graph::MultiHop::new(
                                    index.clone(),
                                    queries.clone(),
                                    benchmark_core::search::graph::Strategy::broadcast(
                                        inmem::spherical::Quantized::search(layout.into()),
                                    ),
                                    bit_map_filters.clone(),
                                )?;

                                let search_results =
                                    search::knn::run(&multihop, &groundtruth, steps)?;
                                result.append(SearchRun {
                                    layout,
                                    results: AggregatedSearchResults::Topk(search_results),
                                });
                            }
                            writeln!(output, "\n\n{}", result)?;
                            Ok(result)
                        }
                    }
                }
            }
        };
    }

    build_and_search!(1);
    build_and_search!(2);
    build_and_search!(4);
}