forked from lancedb/lance
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: improve PQ computing distances (lancedb#3150)
this is done by make the compiler know the size of distance table slice ``` 5242880,L2,PQ=96,DIM=1536 time: [148.44 ms 149.47 ms 150.50 ms] change: [-53.716% -53.486% -53.252%] (p = 0.00 < 0.10) Performance has improved. 5242880,Cosine,PQ=96,DIM=1536 time: [191.84 ms 192.21 ms 192.75 ms] change: [-46.738% -46.621% -46.461%] (p = 0.00 < 0.10) Performance has improved. ``` --------- Signed-off-by: BubbleCal <[email protected]>
- Loading branch information
Showing
4 changed files
with
100 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-FileCopyrightText: Copyright The Lance Authors | ||
|
||
//! Benchmark of building PQ distance table. | ||
use std::iter::repeat; | ||
|
||
use arrow_array::types::Float32Type; | ||
use arrow_array::{FixedSizeListArray, UInt8Array}; | ||
use criterion::{black_box, criterion_group, criterion_main, Criterion}; | ||
use lance_arrow::FixedSizeListArrayExt; | ||
use lance_index::vector::pq::ProductQuantizer; | ||
use lance_linalg::distance::DistanceType; | ||
use lance_testing::datagen::generate_random_array_with_seed; | ||
use rand::{prelude::StdRng, Rng, SeedableRng}; | ||
|
||
#[cfg(target_os = "linux")] | ||
use pprof::criterion::{Output, PProfProfiler}; | ||
|
||
const PQ: usize = 96; | ||
const DIM: usize = 1536; | ||
const TOTAL: usize = 16 * 1000; | ||
|
||
fn dist_table(c: &mut Criterion) { | ||
let codebook = generate_random_array_with_seed::<Float32Type>(256 * DIM, [88; 32]); | ||
let query = generate_random_array_with_seed::<Float32Type>(DIM, [32; 32]); | ||
|
||
let mut rnd = StdRng::from_seed([32; 32]); | ||
let code = UInt8Array::from_iter_values(repeat(rnd.gen::<u8>()).take(TOTAL * PQ)); | ||
|
||
for dt in [DistanceType::L2, DistanceType::Cosine, DistanceType::Dot].iter() { | ||
let pq = ProductQuantizer::new( | ||
PQ, | ||
4, | ||
DIM, | ||
FixedSizeListArray::try_new_from_values(codebook.clone(), DIM as i32).unwrap(), | ||
*dt, | ||
); | ||
|
||
c.bench_function( | ||
format!("{},{},PQ={},DIM={}", TOTAL, dt, PQ, DIM).as_str(), | ||
|b| { | ||
b.iter(|| { | ||
black_box(pq.compute_distances(&query, &code).unwrap()); | ||
}) | ||
}, | ||
); | ||
} | ||
} | ||
|
||
#[cfg(target_os = "linux")] | ||
criterion_group!( | ||
name=benches; | ||
config = Criterion::default().significance_level(0.1).sample_size(10) | ||
.with_profiler(PProfProfiler::new(100, Output::Flamegraph(None))); | ||
targets = dist_table); | ||
|
||
#[cfg(not(target_os = "linux"))] | ||
criterion_group!( | ||
name=benches; | ||
config = Criterion::default().significance_level(0.1).sample_size(10); | ||
targets = dist_table); | ||
|
||
criterion_main!(benches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters