shyjsarah commented on code in PR #85:
URL: 
https://github.com/apache/paimon-vector-index/pull/85#discussion_r3910957840


##########
core/src/coarse.rs:
##########
@@ -0,0 +1,154 @@
+// 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 crate::diskann::{
+    DiskAnnBuildDistance, DiskAnnBuildParams, DiskAnnRawVectorEncoding, 
DiskAnnStorageLayout,
+};
+use crate::kmeans;
+use crate::logging::{emit_log, LogLevel};
+use crate::vamana::VamanaGraph;
+use rayon::prelude::*;
+
+const APPROX_ASSIGN_SEARCH_LIST: usize = 15;
+const APPROX_ASSIGN_MIN_CENTROID_VALUES: usize = 1_000_000;
+
+fn use_approximate_assignment(d: usize, nlist: usize) -> bool {
+    d.saturating_mul(nlist) >= APPROX_ASSIGN_MIN_CENTROID_VALUES
+}
+
+#[derive(Default)]
+pub(crate) struct CoarseAssignment {
+    graph: Option<VamanaGraph>,
+    build_attempted: bool,
+}
+
+impl CoarseAssignment {
+    pub(crate) fn reset(&mut self) {
+        *self = Self::default();
+    }
+
+    pub(crate) fn prepare(&mut self, centroids: &[f32], nlist: usize, d: 
usize) {
+        if self.build_attempted {
+            return;
+        }
+        self.build_attempted = true;
+        if !use_approximate_assignment(d, nlist) {
+            return;
+        }
+
+        let params = DiskAnnBuildParams {
+            max_degree: 12,
+            build_search_list_size: APPROX_ASSIGN_SEARCH_LIST,
+            alpha: 1.2,
+            seed: 42,
+            memory_budget_bytes: 1024 * 1024 * 1024,
+            storage_layout: DiskAnnStorageLayout::Compact,
+            raw_vector_encoding: DiskAnnRawVectorEncoding::F32,
+            build_distance: DiskAnnBuildDistance::FullPrecision,
+        };
+        match VamanaGraph::build(centroids, nlist, d, params) {
+            Ok(graph) => self.graph = Some(graph),
+            Err(error) => emit_log(
+                LogLevel::Warn,
+                &format!("automatic approximate coarse assignment disabled: 
{error}"),
+            ),
+        }
+    }
+
+    pub(crate) fn assign(
+        &mut self,
+        data: &[f32],
+        n: usize,
+        centroids: &[f32],
+        nlist: usize,
+        d: usize,
+    ) -> Vec<usize> {
+        self.prepare(centroids, nlist, d);
+        let Some(graph) = &self.graph else {
+            return kmeans::find_nearest_batch(data, n, centroids, nlist, d);
+        };
+
+        let mut assignments = vec![0usize; n];
+        let chunk = (n / (rayon::current_num_threads() * 4).max(1)).clamp(16, 
1024);
+        assignments.par_chunks_mut(chunk).enumerate().for_each_init(
+            || graph.search_scratch(APPROX_ASSIGN_SEARCH_LIST),
+            |scratch, (chunk_idx, chunk_assignments)| {
+                let row0 = chunk_idx * chunk;
+                for (i, assignment) in 
chunk_assignments.iter_mut().enumerate() {
+                    let row = row0 + i;
+                    *assignment = graph

Review Comment:
   **[major] The list chosen here is approximate, but all IVF search paths 
still choose probe lists with exact centroid top-k. The two policies can 
disagree, so a vector may be stored in a list that a self-query with `nprobe=1` 
never probes. I reproduced this at `d=256, nlist=4096`: 1,807/4,096 centroid 
vectors moved away from their exact list, and id 2461 was stored in list 3 
while its self-query returned `-1 / f32::MAX`. Please keep one coarse-quantizer 
contract for both insertion and lookup, or make this approximation explicit and 
ensure search covers the graph-selected list. A threshold-enabled self-recall 
regression test would catch this.



-- 
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]

Reply via email to