From 63961547b13a6c3e3842ac54da6b517033016234 Mon Sep 17 00:00:00 2001 From: JingsongLi Date: Thu, 19 Nov 2026 16:03:40 +0800 Subject: [PATCH] Remove automatic probe sizing from the centroid router --- core/src/autotune.rs | 52 ++++------------ core/src/index.rs | 21 +------ core/src/index/router.rs | 59 ------------------- docs/api.html | 2 +- .../paimon/index/vector/CentroidRouter.java | 13 ---- .../index/vector/VectorIndexNative.java | 3 - .../VectorIndexCentroidShardingTest.java | 8 +-- jni/src/router.rs | 35 ----------- 8 files changed, 18 insertions(+), 175 deletions(-) diff --git a/core/src/autotune.rs b/core/src/autotune.rs index c42c960..fe534ec 100644 --- a/core/src/autotune.rs +++ b/core/src/autotune.rs @@ -148,22 +148,14 @@ pub fn infer_ivf_nprobe( top_k: usize, matching_count: Option, ) -> io::Result { - infer_ivf_nprobe_with_limits( - nlist, - vector_count, - top_k, - matching_count, - DEFAULT_IVF_MIN_NPROBE, - None, - ) + infer_ivf_nprobe_with_filter_expansion_cap(nlist, vector_count, top_k, matching_count, None) } -pub(crate) fn infer_ivf_nprobe_with_limits( +pub(crate) fn infer_ivf_nprobe_with_filter_expansion_cap( nlist: usize, vector_count: usize, top_k: usize, matching_count: Option, - min_nprobe: usize, max_initial_filter_expansion_factor: Option, ) -> io::Result { if nlist == 0 { @@ -181,7 +173,7 @@ pub(crate) fn infer_ivf_nprobe_with_limits( .checked_mul(DEFAULT_IVF_CANDIDATES_PER_RESULT) .ok_or_else(|| invalid_input("automatic nprobe candidate count overflows usize"))?; let candidate_lists = candidate_rows.div_ceil(average_list_rows); - let mut nprobe = min_nprobe + let mut nprobe = DEFAULT_IVF_MIN_NPROBE .max(nlist.div_ceil(DEFAULT_IVF_LIST_FRACTION)) .max(candidate_lists) .min(nlist); @@ -429,6 +421,8 @@ mod tests { #[test] fn automatic_nprobe_scales_for_lists_topk_and_filters() { + assert_eq!(infer_ivf_nprobe(4, 25_000, 10, None).unwrap(), 4); + assert_eq!(infer_ivf_nprobe(8, 25_000, 10, None).unwrap(), 8); assert_eq!(infer_ivf_nprobe(64, 1_000_000, 10, None).unwrap(), 8); assert_eq!(infer_ivf_nprobe(1024, 1_000_000, 10, None).unwrap(), 64); assert_eq!( @@ -441,15 +435,8 @@ mod tests { #[test] fn automatic_nprobe_caps_initial_filter_expansion() { assert_eq!( - infer_ivf_nprobe_with_limits( - 256, - 2_560_000, - 3, - Some(256_000), - DEFAULT_IVF_MIN_NPROBE, - Some(4) - ) - .unwrap(), + infer_ivf_nprobe_with_filter_expansion_cap(256, 2_560_000, 3, Some(256_000), Some(4)) + .unwrap(), 64 ); } @@ -457,15 +444,8 @@ mod tests { #[test] fn automatic_nprobe_filter_expansion_cap_preserves_bounds() { assert_eq!( - infer_ivf_nprobe_with_limits( - 256, - 2_560_000, - 3, - Some(256_000), - DEFAULT_IVF_MIN_NPROBE, - Some(1) - ) - .unwrap(), + infer_ivf_nprobe_with_filter_expansion_cap(256, 2_560_000, 3, Some(256_000), Some(1)) + .unwrap(), 16 ); assert_eq!( @@ -473,23 +453,15 @@ mod tests { 160 ); assert_eq!( - infer_ivf_nprobe_with_limits( - 256, - 2_560_000, - 3, - Some(5_120_000), - DEFAULT_IVF_MIN_NPROBE, - Some(4) - ) - .unwrap(), + infer_ivf_nprobe_with_filter_expansion_cap(256, 2_560_000, 3, Some(5_120_000), Some(4)) + .unwrap(), 16 ); - assert!(infer_ivf_nprobe_with_limits( + assert!(infer_ivf_nprobe_with_filter_expansion_cap( 256, 2_560_000, 3, Some(256_000), - DEFAULT_IVF_MIN_NPROBE, Some(0) ) .is_err()); diff --git a/core/src/index.rs b/core/src/index.rs index 0845933..715b050 100644 --- a/core/src/index.rs +++ b/core/src/index.rs @@ -17,8 +17,7 @@ use crate::autotune::{ default_training_vector_count, diskann_build_preset, infer_diskann_l_search, infer_ivf_nlist, - infer_ivf_nprobe_with_limits, infer_rq_bits, DiskAnnBuildPreset, TuningObjective, - DEFAULT_IVF_MIN_NPROBE, + infer_ivf_nprobe_with_filter_expansion_cap, infer_rq_bits, DiskAnnBuildPreset, TuningObjective, }; use crate::diskann::{ diskann_training_sample_limit, validate_diskann_format_configuration, @@ -1244,29 +1243,13 @@ impl VectorSearchParams { nlist: usize, vector_count: usize, matching_count: Option, - ) -> io::Result { - self.resolve_ivf_nprobe_with_min( - nlist, - vector_count, - matching_count, - DEFAULT_IVF_MIN_NPROBE, - ) - } - - fn resolve_ivf_nprobe_with_min( - self, - nlist: usize, - vector_count: usize, - matching_count: Option, - min_nprobe: usize, ) -> io::Result { match self.search_width { - SearchWidth::Auto => infer_ivf_nprobe_with_limits( + SearchWidth::Auto => infer_ivf_nprobe_with_filter_expansion_cap( nlist, vector_count, self.top_k, matching_count, - min_nprobe, self.max_initial_filter_expansion_factor, ), SearchWidth::IvfNProbe if self.width > 0 => Ok(self.width.min(nlist)), diff --git a/core/src/index/router.rs b/core/src/index/router.rs index 31421de..cc59e71 100644 --- a/core/src/index/router.rs +++ b/core/src/index/router.rs @@ -106,23 +106,6 @@ impl CentroidRouter { }) } - /// Resolve the outer shard count, allowing small routers to prune whole files. - pub fn resolve_nprobe( - &self, - params: VectorSearchParams, - total_vectors: usize, - matching_vectors: Option, - ) -> io::Result { - params.validate()?; - let nlist = self.nlist(); - let min_nprobe = match nlist { - 4 => 3, - 8 => 4, - _ => DEFAULT_IVF_MIN_NPROBE, - }; - params.resolve_ivf_nprobe_with_min(nlist, total_vectors, matching_vectors, min_nprobe) - } - fn routing_vector<'a>(&self, query: &'a [f32]) -> io::Result> { let d = self.dimension(); validate_query(query, d)?; @@ -189,48 +172,6 @@ impl CentroidRouter { mod tests { use super::*; - #[test] - fn outer_routing_uses_small_floors_before_candidate_and_filter_expansion() { - let auto = VectorSearchParams::automatic(10); - for (nlist, expected) in [ - (3, 3), - (4, 3), - (5, 5), - (7, 7), - (8, 4), - (9, 8), - (16, 8), - (256, 16), - ] { - let router = CentroidRouter::from_centers(1, MetricType::L2, vec![0.; nlist]).unwrap(); - assert_eq!(router.resolve_nprobe(auto, 25_000, None).unwrap(), expected); - } - for (nlist, filtered, large_k) in [(4, 4, 4), (8, 5, 7)] { - let router = CentroidRouter::from_centers(1, MetricType::L2, vec![0.; nlist]).unwrap(); - assert_eq!( - router.resolve_nprobe(auto, 25_000, Some(20_000)).unwrap(), - filtered - ); - assert_eq!( - router - .resolve_nprobe(VectorSearchParams::automatic(5_000), 25_000, None) - .unwrap(), - large_k - ); - assert_eq!( - router.resolve_nprobe(auto, 25_000, Some(500)).unwrap(), - nlist - ); - assert_eq!( - router - .resolve_nprobe(VectorSearchParams::new(10, 1), 25_000, Some(500)) - .unwrap(), - 1 - ); - assert_eq!(auto.resolve_ivf_nprobe(nlist, 25_000, None).unwrap(), nlist); - } - } - #[test] fn router_payload_is_independent_of_shard_type_and_rejects_corruption() { // Frozen routing format: two 2D cosine centers, including a tie on [1, 1]. diff --git a/docs/api.html b/docs/api.html index 845d333..a06b634 100644 --- a/docs/api.html +++ b/docs/api.html @@ -325,7 +325,7 @@

Centroid routing for local indexes

int[] filesToProbe = router.findNearestCentroids(query, 8); }

Call planTrainingSample(vectorCount) on a fresh IVF-FLAT trainer with the outer routing options for every shard type. It returns zero-based input ordinals in reservoir slot order. A distributed job can read those rows from its splits and feed them back in that order to reproduce streaming IVF-FLAT training. DiskANN’s seed and memory budget apply when training PQ and building the graph inside each shard. The serialized router contains the metric, dimension and coarse centers.

-

In each shard, use VectorIndexTrainer.create, VectorIndexWriter.addVectors and writeIndex. Use the standard VectorIndexReader on the selected files and merge results by score. The caller chooses the outer probe count explicitly or with router.resolveNprobe. Local IVF probes and DiskANN graph search width are independent. Keep the router and its center-to-file mapping in the same index generation.

+

In each shard, use VectorIndexTrainer.create, VectorIndexWriter.addVectors and writeIndex. Use the standard VectorIndexReader on the selected files and merge results by score. Pass the number of shards to search to findNearestCentroids. The Paimon routed index reader chooses this width for each row-ID range. Local IVF probes and DiskANN graph search width are independent. Keep the router and its center-to-file mapping in the same index generation.

The corresponding Rust entry points are VectorIndexTrainer::new, CentroidRouter::from_training, and CentroidRouter::{serialize, deserialize, assign_centroid, find_nearest_centroids}. The wire format is documented in STORAGE_FORMAT.md.

diff --git a/java/src/main/java/org/apache/paimon/index/vector/CentroidRouter.java b/java/src/main/java/org/apache/paimon/index/vector/CentroidRouter.java index 657e5a3..26de983 100644 --- a/java/src/main/java/org/apache/paimon/index/vector/CentroidRouter.java +++ b/java/src/main/java/org/apache/paimon/index/vector/CentroidRouter.java @@ -67,19 +67,6 @@ public synchronized int[] findNearestCentroids(float[] query, int nprobe) { return VectorIndexNative.findNearestCentroids(requireOpen(), query, nprobe); } - /** - * Resolves the initial number of shards to probe. Automatic routing starts at three shards for - * four centers and four shards for eight centers. Top-K and filter selectivity can increase - * the width. Pass -1 for matchingVectors when there is no row-ID filter. - */ - public synchronized int resolveNprobe( - VectorSearchParams params, long totalVectors, long matchingVectors) { - if (params == null) { - throw new NullPointerException("params"); - } - return VectorIndexNative.routerNprobe(requireOpen(), params, totalVectors, matchingVectors); - } - @Override public synchronized void close() { if (nativePtr != 0L) { diff --git a/java/src/main/java/org/apache/paimon/index/vector/VectorIndexNative.java b/java/src/main/java/org/apache/paimon/index/vector/VectorIndexNative.java index 38dcd25..9f05c38 100644 --- a/java/src/main/java/org/apache/paimon/index/vector/VectorIndexNative.java +++ b/java/src/main/java/org/apache/paimon/index/vector/VectorIndexNative.java @@ -69,9 +69,6 @@ static void requireRangeResultApi() { static native void freeRouter(long ptr); - static native int routerNprobe( - long ptr, VectorSearchParams params, long totalVectors, long matchingVectors); - static native int assignCentroid(long ptr, float[] vector); static native int[] findNearestCentroids(long ptr, float[] query, int nprobe); diff --git a/java/src/test/java/org/apache/paimon/index/vector/VectorIndexCentroidShardingTest.java b/java/src/test/java/org/apache/paimon/index/vector/VectorIndexCentroidShardingTest.java index b8d1a49..ba5c7e5 100644 --- a/java/src/test/java/org/apache/paimon/index/vector/VectorIndexCentroidShardingTest.java +++ b/java/src/test/java/org/apache/paimon/index/vector/VectorIndexCentroidShardingTest.java @@ -61,11 +61,9 @@ static void run() { check(payload.length < CentroidRouter.maxSerializedBytes(), "router size bound"); float[] query = Arrays.copyOf(rows, 4); int selected = router.assignCentroid(query); - check(router.findNearestCentroids(query, 1)[0] == selected, "routing consistency"); - VectorSearchParams automatic = VectorSearchParams.automatic(10); - check(router.resolveNprobe(automatic, 256, -1) == 3, "automatic outer width"); - check(router.resolveNprobe(automatic, 256, 128) == 4, "filtered outer width"); - check(router.resolveNprobe(VectorSearchParams.ivf(1, 2), 256, -1) == 2, "outer width"); + int[] nearest = router.findNearestCentroids(query, 2); + check(nearest.length == 2, "caller-selected routing width"); + check(nearest[0] == selected, "routing consistency"); long[] ids = new long[256]; float[] vectors = new float[rows.length]; int count = 0; diff --git a/jni/src/router.rs b/jni/src/router.rs index 9ab4be4..caea1eb 100644 --- a/jni/src/router.rs +++ b/jni/src/router.rs @@ -18,41 +18,6 @@ use super::*; use paimon_vindex_core::index::CentroidRouter; -#[no_mangle] -pub extern "system" fn Java_org_apache_paimon_index_vector_VectorIndexNative_routerNprobe( - env: JNIEnv, - _class: JClass, - ptr: jlong, - params: JObject, - total_vectors: jlong, - matching_vectors: jlong, -) -> jint { - jni_call(env, |env| { - if total_vectors < 0 || matching_vectors < -1 { - return throw_and_return(env, "invalid vector count"); - } - let params = match search_params(env, params) { - Ok(p) => p, - Err(e) => return throw_and_return(env, &e), - }; - match router(ptr).and_then(|t| { - t.resolve_nprobe( - params, - total_vectors as usize, - if matching_vectors < 0 { - None - } else { - Some(matching_vectors as usize) - }, - ) - .map_err(|e| e.to_string()) - }) { - Ok(nprobe) => nprobe as jint, - Err(e) => throw_and_return(env, &e), - } - }) -} - fn router(ptr: jlong) -> Result<&'static CentroidRouter, String> { if ptr == 0 { return Err("null router pointer".into());