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

Add updateable random scorer interface for vector index building #14181

Merged

Conversation

benwtrent
Copy link
Member

As stated by @ChrisHegarty and @msokolov the amount of garbage we create during vector index creation is pretty astounding.

This adjusts the interface to allow an "Updateable" random vector interface (@msokolov 's idea if I remember correctly) and refactors the usage to keep it threadsafe.

I still need to do some larger scale tests, I imagine the actual indexing times are not effected much.

I think I caught all the weird edge cases.

Copy link
Contributor

@msokolov msokolov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for tackling this! I have a few questions, possible suggestions for further improvement, but they shouldn't block anything.

@@ -90,23 +91,29 @@ public String toString() {
private static final class ByteScoringSupplier implements RandomVectorScorerSupplier {
private final ByteVectorValues vectors;
private final ByteVectorValues vectors1;
private final ByteVectorValues vectors2;
private final VectorSimilarityFunction similarityFunction;

private ByteScoringSupplier(
ByteVectorValues vectors, VectorSimilarityFunction similarityFunction) throws IOException {
this.vectors = vectors;
vectors1 = vectors.copy();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since it is asymmetric now, instead of 1 vs 2, maybe rename to targetVectors or so?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can happily rename everywhere. We use the same pattern many times.

}
super.addGraphNode(node, scorer);
}

@Override
public void addGraphNode(int node) throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curious if we still need this, and generally, if we are still using non-updateable RandomVectorScorer?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was unsure about adjusting the full interface :/ There are things that inherit from HNSW graph, and I wanted it to "just work" without any fuss.

@Override
public void addGraphNode(int node) throws IOException {
if (initializedNodes != null && initializedNodes.get(node)) {
return;
}
super.addGraphNode(node);
if (scorer == null) {
scorer = scorerSupplier.scorer(node);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it make sense to add this (maybe with node==0?) when creating the builder so it's never null

Copy link
Member Author

@benwtrent benwtrent Jan 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me see if I can adjust the interface a bit to ensure that we allow a "null" ordinal that simply initializes the buffer.

for (int node = minOrd; node < maxOrd; node++) {
addGraphNode(node);
if (scorer == null) {
scorer = scorerSupplier.scorer(node);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto here - these lazy inits are okay, but proactively initializing is better

to the newly introduced levels (repeating step 2,3 for new levels) and again try to
promote the node to entry node.
*/
UpdateableRandomVectorScorer scorer = scorerSupplier.scorer(node);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how did we get away with not doing this before? Is it that we pushed the supplier.scorer() call deeper in the call graph?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, supplier got pushed all the way down everywhere. Creating new scorers during diversity checking, etc. So, a single node add would likely create many many short lived objects for no useful purpose.

@@ -463,7 +478,11 @@ private boolean connectComponents(int level) throws IOException {

beam.clear();
eps[0] = c0.start();
RandomVectorScorer scorer = scorerSupplier.scorer(c.start());
if (scorer == null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yes, here it is -- again could we initialize outside the loop, maybe even initialize to all zeros? Can ScorerSupplier accept null??

@benwtrent
Copy link
Member Author

Ugh, I just tested recall over a larger data set with multiple indexing and merging threads. Recall is negatively effected by this change, indicating I have a silly bug somewhere.

@msokolov
Copy link
Contributor

msokolov commented Jan 31, 2025

sorry, one more idea; please feel to go ahead with what you have, but I wonder, now that the RandomVectorScorer is updateable, do we even need to initialize it with an ord at all? Won't we generally speaking be updating it? If that were the case then we could change to scorer() and wouldn't need the null checks (it would always be the null case). Hmm but I guess this only applies to UpdateableRandomVectorScorer? But ... again -- do we have non-updateable instances? We shouldn't be afraid of making API changes on main at least

@benwtrent benwtrent added this to the 10.2.0 milestone Feb 5, 2025
@benwtrent
Copy link
Member Author

OK, benchmarks show pretty much no difference when I was testing on my machine. But, recall numbers all checkout and the API is nicer and makes more sense for vector merging.

So, I am gonna merge.

@benwtrent benwtrent merged commit 48884e7 into apache:main Feb 6, 2025
6 checks passed
@benwtrent benwtrent deleted the refactor/adj-scorer-reuse-hnsw-building branch February 6, 2025 16:12
@ChrisHegarty
Copy link
Contributor

Thank you @benwtrent - let's wait for the next lucene nightly. If no perf improvement, that's ok. There should be a lot less garbage created, and CPU devoted to cleaning young heap regions.

benwtrent added a commit that referenced this pull request Feb 6, 2025
)

As stated by @ChrisHegarty and @msokolov the amount of garbage we create during vector index creation is pretty astounding.

This adjusts the interface to allow an "Updateable" random vector interface (@msokolov 's idea if I remember correctly) and refactors the usage to keep it threadsafe.
benwtrent added a commit to elastic/elasticsearch that referenced this pull request Feb 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants