Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 12 additions & 40 deletions core/src/autotune.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,22 +148,14 @@ pub fn infer_ivf_nprobe(
top_k: usize,
matching_count: Option<usize>,
) -> io::Result<usize> {
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<usize>,
min_nprobe: usize,
max_initial_filter_expansion_factor: Option<usize>,
) -> io::Result<usize> {
if nlist == 0 {
Expand All @@ -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);
Expand Down Expand Up @@ -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!(
Expand All @@ -441,55 +435,33 @@ 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
);
}

#[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!(
infer_ivf_nprobe(256, 2_560_000, 3, Some(256_000)).unwrap(),
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());
Expand Down
21 changes: 2 additions & 19 deletions core/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -1244,29 +1243,13 @@ impl VectorSearchParams {
nlist: usize,
vector_count: usize,
matching_count: Option<usize>,
) -> io::Result<usize> {
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<usize>,
min_nprobe: usize,
) -> io::Result<usize> {
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)),
Expand Down
59 changes: 0 additions & 59 deletions core/src/index/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize>,
) -> io::Result<usize> {
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<Cow<'a, [f32]>> {
let d = self.dimension();
validate_query(query, d)?;
Expand Down Expand Up @@ -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].
Expand Down
2 changes: 1 addition & 1 deletion docs/api.html
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ <h2>Centroid routing for local indexes</h2>
int[] filesToProbe = router.findNearestCentroids(query, 8);
}</code></pre>
<p>Call <code>planTrainingSample(vectorCount)</code> 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.</p>
<p>In each shard, use <code>VectorIndexTrainer.create</code>, <code>VectorIndexWriter.addVectors</code> and <code>writeIndex</code>. Use the standard <code>VectorIndexReader</code> on the selected files and merge results by score. The caller chooses the outer probe count explicitly or with <code>router.resolveNprobe</code>. Local IVF probes and DiskANN graph search width are independent. Keep the router and its center-to-file mapping in the same index generation.</p>
<p>In each shard, use <code>VectorIndexTrainer.create</code>, <code>VectorIndexWriter.addVectors</code> and <code>writeIndex</code>. Use the standard <code>VectorIndexReader</code> on the selected files and merge results by score. Pass the number of shards to search to <code>findNearestCentroids</code>. 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.</p>
<p>The corresponding Rust entry points are <code>VectorIndexTrainer::new</code>, <code>CentroidRouter::from_training</code>, and <code>CentroidRouter::{serialize, deserialize, assign_centroid, find_nearest_centroids}</code>. The wire format is documented in <a href="../core/STORAGE_FORMAT.md">STORAGE_FORMAT.md</a>.</p>
</section>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
35 changes: 0 additions & 35 deletions jni/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Loading