-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Speedup concurrent multi-segment HNWS graph search (#12962)
Speedup concurrent multi-segment HNWS graph search by exchanging the global top candidated collected so far across segments. These global top candidates set the minimum threshold that new candidates need to pass to be considered. This allows earlier stopping for segments that don't have good candidates.
- Loading branch information
1 parent
635d090
commit d095ed0
Showing
19 changed files
with
901 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
38 changes: 38 additions & 0 deletions
38
lucene/core/src/java/org/apache/lucene/search/knn/KnnCollectorManager.java
This file contains 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,38 @@ | ||
/* | ||
* 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 org.apache.lucene.index.LeafReaderContext; | ||
import org.apache.lucene.search.KnnCollector; | ||
|
||
/** | ||
* KnnCollectorManager responsible for creating {@link KnnCollector} instances. Useful to create | ||
* {@link KnnCollector} instances that share global state across leaves, such a global queue of | ||
* results collected so far. | ||
*/ | ||
public interface KnnCollectorManager { | ||
|
||
/** | ||
* Return a new {@link KnnCollector} instance. | ||
* | ||
* @param visitedLimit the maximum number of nodes that the search is allowed to visit | ||
* @param context the leaf reader context | ||
*/ | ||
KnnCollector newCollector(int visitedLimit, LeafReaderContext context) throws IOException; | ||
} |
110 changes: 110 additions & 0 deletions
110
lucene/core/src/java/org/apache/lucene/search/knn/MultiLeafTopKnnCollector.java
This file contains 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,110 @@ | ||
/* | ||
* 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 org.apache.lucene.search.ScoreDoc; | ||
import org.apache.lucene.search.TopDocs; | ||
import org.apache.lucene.search.TopKnnCollector; | ||
import org.apache.lucene.search.TotalHits; | ||
import org.apache.lucene.util.hnsw.BlockingFloatHeap; | ||
import org.apache.lucene.util.hnsw.FloatHeap; | ||
|
||
/** | ||
* MultiLeafTopKnnCollector is a specific KnnCollector that can exchange the top collected results | ||
* across segments through a shared global queue. | ||
* | ||
* @lucene.experimental | ||
*/ | ||
public final class MultiLeafTopKnnCollector extends TopKnnCollector { | ||
|
||
// greediness of globally non-competitive search: (0,1] | ||
private static final float DEFAULT_GREEDINESS = 0.9f; | ||
// the global queue of the highest similarities collected so far across all segments | ||
private final BlockingFloatHeap globalSimilarityQueue; | ||
// the local queue of the highest similarities if we are not competitive globally | ||
// the size of this queue is defined by greediness | ||
private final FloatHeap nonCompetitiveQueue; | ||
private final float greediness; | ||
// the queue of the local similarities to periodically update with the global queue | ||
private final FloatHeap updatesQueue; | ||
// interval to synchronize the local and global queues, as a number of visited vectors | ||
private final int interval = 0xff; // 255 | ||
private boolean kResultsCollected = false; | ||
private float cachedGlobalMinSim = Float.NEGATIVE_INFINITY; | ||
|
||
/** | ||
* @param k the number of neighbors to collect | ||
* @param visitLimit how many vector nodes the results are allowed to visit | ||
*/ | ||
public MultiLeafTopKnnCollector(int k, int visitLimit, BlockingFloatHeap globalSimilarityQueue) { | ||
super(k, visitLimit); | ||
this.greediness = DEFAULT_GREEDINESS; | ||
this.globalSimilarityQueue = globalSimilarityQueue; | ||
this.nonCompetitiveQueue = new FloatHeap(Math.max(1, Math.round((1 - greediness) * k))); | ||
this.updatesQueue = new FloatHeap(k); | ||
} | ||
|
||
@Override | ||
public boolean collect(int docId, float similarity) { | ||
boolean localSimUpdated = queue.insertWithOverflow(docId, similarity); | ||
boolean firstKResultsCollected = (kResultsCollected == false && queue.size() == k()); | ||
if (firstKResultsCollected) { | ||
kResultsCollected = true; | ||
} | ||
updatesQueue.offer(similarity); | ||
boolean globalSimUpdated = nonCompetitiveQueue.offer(similarity); | ||
|
||
if (kResultsCollected) { | ||
// as we've collected k results, we can start do periodic updates with the global queue | ||
if (firstKResultsCollected || (visitedCount & interval) == 0) { | ||
cachedGlobalMinSim = globalSimilarityQueue.offer(updatesQueue.getHeap()); | ||
updatesQueue.clear(); | ||
globalSimUpdated = true; | ||
} | ||
} | ||
return localSimUpdated || globalSimUpdated; | ||
} | ||
|
||
@Override | ||
public float minCompetitiveSimilarity() { | ||
if (kResultsCollected == false) { | ||
return Float.NEGATIVE_INFINITY; | ||
} | ||
return Math.max(queue.topScore(), Math.min(nonCompetitiveQueue.peek(), cachedGlobalMinSim)); | ||
} | ||
|
||
@Override | ||
public TopDocs topDocs() { | ||
assert queue.size() <= k() : "Tried to collect more results than the maximum number allowed"; | ||
ScoreDoc[] scoreDocs = new ScoreDoc[queue.size()]; | ||
for (int i = 1; i <= scoreDocs.length; i++) { | ||
scoreDocs[scoreDocs.length - i] = new ScoreDoc(queue.topNode(), queue.topScore()); | ||
queue.pop(); | ||
} | ||
TotalHits.Relation relation = | ||
earlyTerminated() | ||
? TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO | ||
: TotalHits.Relation.EQUAL_TO; | ||
return new TopDocs(new TotalHits(visitedCount(), relation), scoreDocs); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "MultiLeafTopKnnCollector[k=" + k() + ", size=" + queue.size() + "]"; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
lucene/core/src/java/org/apache/lucene/search/knn/TopKnnCollectorManager.java
This file contains 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,59 @@ | ||
/* | ||
* 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 org.apache.lucene.index.LeafReaderContext; | ||
import org.apache.lucene.search.IndexSearcher; | ||
import org.apache.lucene.search.TopKnnCollector; | ||
import org.apache.lucene.util.hnsw.BlockingFloatHeap; | ||
|
||
/** | ||
* TopKnnCollectorManager responsible for creating {@link TopKnnCollector} instances. When | ||
* concurrency is supported, the {@link BlockingFloatHeap} is used to track the global top scores | ||
* collected across all leaves. | ||
*/ | ||
public class TopKnnCollectorManager implements KnnCollectorManager { | ||
|
||
// the number of docs to collect | ||
private final int k; | ||
// the global score queue used to track the top scores collected across all leaves | ||
private final BlockingFloatHeap globalScoreQueue; | ||
|
||
public TopKnnCollectorManager(int k, IndexSearcher indexSearcher) { | ||
boolean isMultiSegments = indexSearcher.getIndexReader().leaves().size() > 1; | ||
this.k = k; | ||
this.globalScoreQueue = isMultiSegments ? new BlockingFloatHeap(k) : null; | ||
} | ||
|
||
/** | ||
* Return a new {@link TopKnnCollector} instance. | ||
* | ||
* @param visitedLimit the maximum number of nodes that the search is allowed to visit | ||
* @param context the leaf reader context | ||
*/ | ||
@Override | ||
public TopKnnCollector newCollector(int visitedLimit, LeafReaderContext context) | ||
throws IOException { | ||
if (globalScoreQueue == null) { | ||
return new TopKnnCollector(k, visitedLimit); | ||
} else { | ||
return new MultiLeafTopKnnCollector(k, visitedLimit, globalScoreQueue); | ||
} | ||
} | ||
} |
Oops, something went wrong.