-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Feature/collaborative hnsw search #15676
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
krickert
wants to merge
34
commits into
apache:main
Choose a base branch
from
ai-pipestream:feature/collaborative-hnsw-search
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+618
−2
Draft
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
33039a3
Allow dynamic HNSW search threshold updates for collaborative search
krickert 63eb03f
Introduce CollaborativeKnnCollector and Manager to core
krickert 323c395
Clarify visibility semantics and apply formatting
krickert 13a5960
Add CHANGES.txt entry for collaborative search
krickert 50c6255
Add High-K and High-Dimension test scenarios for collaborative search
krickert 2f36edd
Commit missing CollaborativeKnnCollector and Manager
krickert 564f878
Cleanup extraneous newlines in TestCollaborativeHnswSearch
krickert 3eb6a8e
Remove extraneous newlines and fix indentation in TestCollaborativeHn…
krickert 5d1019d
Replace AtomicLong with AtomicInteger for global similarity threshold
krickert d90da2a
Add multi-segment collaborative pruning test for HNSW search
krickert 88a5188
Add multi-index performance tests for collaborative HNSW pruning
krickert f7c8b63
Comprehensive multi-segment and multi-index collaborative tests
krickert 66c7ad3
Fix forbiddenApis by adding Locale.ROOT to String.format
krickert f0dc67c
Mark multi-index collaborative tests as @Nightly
krickert c452d14
Move testHighKPruning and testHighDimensionPruning to @Nightly
krickert bbc0875
Idiomatic Collaborative HNSW search with LongAccumulator and DocScore…
krickert 2e3c64c
Fix multi-index pruning bug and add recall measurement to tests
krickert 17fba5c
Add definitive scaling and stress tests for collaborative search
krickert 3476365
Cleanup and concurrent simulation for TestCollaborativeHnswSearch
krickert 3c491fe
Accumulate floor score for high-recall pruning and add real-world seg…
krickert 1f8cee1
Merge branch 'apache:main' into feature/collaborative-hnsw-search
krickert 713325a
Feature/HNSW: Refine Collaborative Search for robust recall and distr…
krickert 6d135a9
Feature/HNSW: Refine Collaborative Search for robust recall and distr…
krickert e5c894e
Lucene: implement Recall-Safe pruning using Global Floor vs Local Max…
krickert 65b27bb
Lucene: implement topology-aware coordination with Hamming Affinity a…
krickert 7677ac2
Restore CollaborativeKnnCollector Golden Logic (earlyTerminated, loca…
krickert 9cc3d4c
Add 4-arg constructor for TestCollaborativeHnswSearch compatibility
krickert b330268
Fix trailing whitespace in CHANGES.txt
krickert d4adb69
Apply google-java-format via gradlew tidy
krickert 926e5f4
Remove unused fields and imports to fix ecjLint failures
krickert 7b77d6a
Apply google-java-format to CollaborativeKnnCollector
krickert 56018b4
Fix forbiddenApis by using NamedThreadFactory in TestCollaborativeHns…
krickert 2691faa
Apply google-java-format to TestCollaborativeHnswSearch
krickert f618bdd
Merge branch 'apache:main' into feature/collaborative-hnsw-search
krickert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
127 changes: 127 additions & 0 deletions
127
lucene/core/src/java/org/apache/lucene/search/CollaborativeKnnCollector.java
This file contains hidden or 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,127 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| package org.apache.lucene.search; | ||
|
|
||
| import java.util.concurrent.atomic.LongAccumulator; | ||
| import java.util.function.IntUnaryOperator; | ||
| import org.apache.lucene.search.knn.KnnSearchStrategy; | ||
|
|
||
| /** | ||
| * A {@link KnnCollector} that allows for collaborative search. PRUNING BASED ON GLOBAL FLOOR vs | ||
| * LOCAL MAX. | ||
| */ | ||
| public class CollaborativeKnnCollector extends KnnCollector.Decorator { | ||
|
|
||
| private static final IntUnaryOperator IDENTITY_MAPPER = docId -> docId; | ||
| private static final int GLOBAL_BAR_MIN_VISITS = 100; | ||
| private static final float GLOBAL_BAR_TERMINATION_SLACK = 0.0001f; | ||
|
|
||
| private final LongAccumulator minScoreAcc; | ||
| private final int docBase; | ||
| private final IntUnaryOperator docIdMapper; | ||
|
|
||
| private float localMaxScore = Float.NEGATIVE_INFINITY; | ||
| private float lastSharedScore = Float.NEGATIVE_INFINITY; | ||
|
|
||
| /** Convenience constructor for tests. */ | ||
| public CollaborativeKnnCollector( | ||
| int k, int visitLimit, LongAccumulator minScoreAcc, int docBase) { | ||
| this(new TopKnnCollector(k, visitLimit), minScoreAcc, docBase, IDENTITY_MAPPER); | ||
| } | ||
|
|
||
| public CollaborativeKnnCollector( | ||
| int k, | ||
| int visitLimit, | ||
| LongAccumulator minScoreAcc, | ||
| int docBase, | ||
| IntUnaryOperator docIdMapper) { | ||
| this(new TopKnnCollector(k, visitLimit), minScoreAcc, docBase, docIdMapper); | ||
| } | ||
|
|
||
| public CollaborativeKnnCollector( | ||
| int k, | ||
| int visitLimit, | ||
| KnnSearchStrategy searchStrategy, | ||
| LongAccumulator minScoreAcc, | ||
| int docBase, | ||
| IntUnaryOperator docIdMapper) { | ||
| this(new TopKnnCollector(k, visitLimit, searchStrategy), minScoreAcc, docBase, docIdMapper); | ||
| } | ||
|
|
||
| private CollaborativeKnnCollector( | ||
| KnnCollector delegate, | ||
| LongAccumulator minScoreAcc, | ||
| int docBase, | ||
| IntUnaryOperator docIdMapper) { | ||
| super(delegate); | ||
| this.minScoreAcc = minScoreAcc; | ||
| this.docBase = docBase; | ||
| this.docIdMapper = docIdMapper; | ||
| } | ||
|
|
||
| @Override | ||
| public float minCompetitiveSimilarity() { | ||
| // Pathfinding always uses local bar | ||
| return super.minCompetitiveSimilarity(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean earlyTerminated() { | ||
| if (super.earlyTerminated()) return true; | ||
| if (visitedCount() < GLOBAL_BAR_MIN_VISITS) return false; | ||
|
|
||
| long globalFloorCode = minScoreAcc.get(); | ||
| if (globalFloorCode == Long.MIN_VALUE) return false; | ||
|
|
||
| float globalFloorScore = DocScoreEncoder.toScore(globalFloorCode); | ||
|
|
||
| // CRITICAL: Only stop if our BEST hit is worse than the global floor. | ||
| // If localMax < globalFloor, it's impossible for this shard to make the Top K. | ||
| return localMaxScore > Float.NEGATIVE_INFINITY | ||
| && localMaxScore < (globalFloorScore - GLOBAL_BAR_TERMINATION_SLACK); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean collect(int docId, float similarity) { | ||
| boolean collected = super.collect(docId, similarity); | ||
|
|
||
| if (similarity > localMaxScore) { | ||
| localMaxScore = similarity; | ||
| } | ||
|
|
||
| if (collected) { | ||
| float floorScore = super.minCompetitiveSimilarity(); | ||
| if (floorScore > Float.NEGATIVE_INFINITY && floorScore > lastSharedScore + 0.0001f) { | ||
|
|
||
| int absoluteDocId = docId + docBase; | ||
| minScoreAcc.accumulate( | ||
| DocScoreEncoder.encode(docIdMapper.applyAsInt(absoluteDocId), floorScore)); | ||
| lastSharedScore = floorScore; | ||
| } | ||
| } | ||
| return collected; | ||
| } | ||
|
|
||
| public static float toScore(long value) { | ||
| return DocScoreEncoder.toScore(value); | ||
| } | ||
|
|
||
| public static long encode(int docId, float score) { | ||
| return DocScoreEncoder.encode(docId, score); | ||
| } | ||
| } |
64 changes: 64 additions & 0 deletions
64
lucene/core/src/java/org/apache/lucene/search/knn/CollaborativeKnnCollectorManager.java
This file contains hidden or 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 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| package org.apache.lucene.search.knn; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.concurrent.atomic.LongAccumulator; | ||
| import java.util.function.IntUnaryOperator; | ||
| import org.apache.lucene.index.LeafReaderContext; | ||
| import org.apache.lucene.search.CollaborativeKnnCollector; | ||
| import org.apache.lucene.search.KnnCollector; | ||
|
|
||
| /** | ||
| * A {@link KnnCollectorManager} that creates {@link CollaborativeKnnCollector} instances sharing a | ||
| * single {@link LongAccumulator} for global pruning across segments, gated by topological hints. | ||
| * | ||
| * @lucene.experimental | ||
| */ | ||
| public class CollaborativeKnnCollectorManager implements KnnCollectorManager { | ||
|
|
||
| private final int k; | ||
| private final LongAccumulator minScoreAcc; | ||
| private final IntUnaryOperator docIdMapper; | ||
|
|
||
| /** | ||
| * Create a new CollaborativeKnnCollectorManager | ||
| * | ||
| * @param k number of neighbors to collect | ||
| * @param minScoreAcc shared accumulator for global pruning | ||
| */ | ||
| public CollaborativeKnnCollectorManager(int k, LongAccumulator minScoreAcc) { | ||
| this(k, minScoreAcc, docId -> docId); | ||
| } | ||
|
|
||
| /** Create a new CollaborativeKnnCollectorManager with a docId mapper */ | ||
| public CollaborativeKnnCollectorManager( | ||
| int k, LongAccumulator minScoreAcc, IntUnaryOperator docIdMapper) { | ||
| this.k = k; | ||
| this.minScoreAcc = minScoreAcc; | ||
| this.docIdMapper = docIdMapper; | ||
| } | ||
|
|
||
| @Override | ||
| public KnnCollector newCollector( | ||
| int visitedLimit, KnnSearchStrategy searchStrategy, LeafReaderContext context) | ||
| throws IOException { | ||
| return new CollaborativeKnnCollector( | ||
| k, visitedLimit, searchStrategy, minScoreAcc, context.docBase, docIdMapper); | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what does this do when we are not using external pruning?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When we're not using external pruning,
results.minCompetitiveSimilarity()still returns the minimum competitive similarity of the current top‑K results, but only from this shard’s collector.So it's the same kind of threshold (used to prune the graph: skip nodes that can’t beat the worst of the current top‑K), but it's not updated from other shards.
The loop and the pruning logic are unchanged; only the source of the threshold is internal (this shard) instead of external (collaborative). In other words: same API, same pruning, no cross-shard updates when external pruning is off.