shyjsarah commented on code in PR #76: URL: https://github.com/apache/paimon-vector-index/pull/76#discussion_r3772200784
########## core/benches/ivfpq_filter_scan_bench.rs: ########## @@ -0,0 +1,152 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use paimon_vindex_core::distance::MetricType; +use paimon_vindex_core::io::{write_index, IVFPQIndexReader, PosWriter}; +use paimon_vindex_core::ivfpq::{search_batch_reader_filter, IVFPQIndex, RowIdFilter}; +use rand::rngs::StdRng; +use rand::seq::SliceRandom; +use rand::{Rng, SeedableRng}; +use std::hint::black_box; +use std::io::Cursor; +use std::time::{Duration, Instant}; + +const D: usize = 768; +const M: usize = 96; +const NLIST: usize = 128; +const NPROBE: usize = 64; +const NQ: usize = 64; +const K: usize = 3; +const ROWS_PER_LIST: usize = 6_800; +const WARMUPS: usize = 1; +const ROUNDS: usize = 3; + +struct DensityFilter<'a> { + row_ranks: &'a [usize], + max_rank: usize, +} + +impl RowIdFilter for DensityFilter<'_> { + fn contains(&self, id: i64) -> bool { + self.row_ranks + .get(id as usize) + .is_some_and(|&rank| rank < self.max_rank) + } +} + +fn randomized_row_ranks() -> Vec<usize> { + let mut rng = StdRng::seed_from_u64(43); + let mut row_ranks = vec![0; NLIST * ROWS_PER_LIST]; + let mut row_order = (0..ROWS_PER_LIST).collect::<Vec<_>>(); + for list_id in 0..NLIST { + row_order.shuffle(&mut rng); + let base = list_id * ROWS_PER_LIST; + for (rank, &row) in row_order.iter().enumerate() { + row_ranks[base + row] = rank; + } + } + row_ranks +} + +fn search( + reader: &mut IVFPQIndexReader<Cursor<Vec<u8>>>, + queries: &[f32], + filter: &DensityFilter, +) -> Duration { + let started = Instant::now(); + let result = search_batch_reader_filter(reader, queries, NQ, K, NPROBE, Some(filter)).unwrap(); + let elapsed = started.elapsed(); + black_box(result); + elapsed +} + +fn median(samples: &mut [Duration]) -> Duration { + samples.sort_unstable(); + samples[samples.len() / 2] +} + +fn main() { + assert_eq!((NQ, NPROBE), (64, 64), "benchmark production query shape"); + assert!( + std::env::var_os("PAIMON_VINDEX_LOG_IVFPQ_TIMING").is_none(), Review Comment: This guard checks a stale environment variable name. The exercised search path currently reads `PAIMON_VINDEX_LOG_IVFPQ_BATCH_TIMING` and `PAIMON_VINDEX_LOG_IVFPQ_BATCH_REUSE`, so enabling either diagnostic still passes this assertion and can add instrumentation/logging overhead to the measured samples. Could we assert that both current variables are unset before running the benchmark? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
