Skip to content
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

Implement ACORN-1 search for HNSW #14085

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -222,27 +222,69 @@ void searchLevel(
break;
}

int maxConn = 32; // Hardcode for testing TODO: fetch this value properly

int topCandidateNode = candidates.pop();
// Pre-fetch neighbors into an array
// This is necessary because we need to call `seek` on each neighbor to consider 2-hop neighbors
int[] neighbors = new int[maxConn];
graphSeek(graph, level, topCandidateNode);
int friendOrd;
while ((friendOrd = graphNextNeighbor(graph)) != NO_MORE_DOCS) {
int neighborCount = 0;
int neighborOrd;
while ((neighborOrd = graphNextNeighbor(graph)) != NO_MORE_DOCS && neighborCount < maxConn) {
neighbors[neighborCount++] = neighborOrd;
}

// We only consider maxConn 1/2-hop neighbors
int neighborsProcessed = 0;
// Walk 1-hop neighbors
for (int i = 0; i < neighborCount; i++) {
if (neighborsProcessed > maxConn) break;
int friendOrd = neighbors[i];
assert friendOrd < size : "friendOrd=" + friendOrd + "; size=" + size;
if (visited.getAndSet(friendOrd)) {
continue;
}

if (results.earlyTerminated()) {
break;
}
float friendSimilarity = scorer.score(friendOrd);
results.incVisitedCount(1);
if (friendSimilarity > minAcceptedSimilarity) {
candidates.add(friendOrd, friendSimilarity);
if (acceptOrds == null || acceptOrds.get(friendOrd)) {

// Only calculate score and consider candidate if filter matches
if (acceptOrds == null || acceptOrds.get(friendOrd)) {
neighborsProcessed++;
float friendSimilarity = scorer.score(friendOrd);
results.incVisitedCount(1);
if (friendSimilarity > minAcceptedSimilarity) {
candidates.add(friendOrd, friendSimilarity);
if (results.collect(friendOrd, friendSimilarity)) {
minAcceptedSimilarity = results.minCompetitiveSimilarity();
}
}
} else { // Only walk 2-hop neighbors if filter doesn't match
graphSeek(graph, level, friendOrd);
int twoHopFriendOrd;
while ((twoHopFriendOrd = graphNextNeighbor(graph)) != NO_MORE_DOCS && neighborsProcessed <= maxConn) {
assert twoHopFriendOrd < size : "twoHopFriendOrd=" + twoHopFriendOrd + "; size=" + size;
if (visited.getAndSet(twoHopFriendOrd)) {
continue;
}
if (results.earlyTerminated()) {
break;
}

// Only calculate score and consider candidate if filter matches
if (acceptOrds.get(twoHopFriendOrd)) {
neighborsProcessed++;
float twoHopSimilarity = scorer.score(twoHopFriendOrd);
results.incVisitedCount(1);
if (twoHopSimilarity > minAcceptedSimilarity) {
candidates.add(twoHopFriendOrd, twoHopSimilarity);
if (results.collect(twoHopFriendOrd, twoHopSimilarity)) {
minAcceptedSimilarity = results.minCompetitiveSimilarity();
}
}
}
}
}
}
}
Expand Down
Loading