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

LUCENE-10391: Reuse data structures across HnswGraph#searchLevel calls #641

Merged
merged 4 commits into from Feb 4, 2022

Conversation

jtibshirani
Copy link
Member

A couple of the data structures used in HNSW search are pretty large and
expensive to allocate. This commit creates a shared candidates queue and visited
set that are reused across calls to HnswGraph#searchLevel. Now the same data
structures are used for building the entire graph, which can really cut down on
allocations during indexing.

A couple of the data structures used in HNSW search are pretty large and
expensive to allocate. This commit creates a shared candidates queue and visited
set that are reused across calls to HnswGraph#searchLevel. Now the same data
structures are used for building the entire graph, which can cut down on
allocations during indexing.
@jtibshirani
Copy link
Member Author

This PR contains two changes:

  • Reuse visited bit set across calls. For graph construction, switch to FixedBitSet instead of SparseBitSet.
  • Reuse candidates queue across calls.

They both seem to have a small positive impact on indexing. Here's an example using KnnGraphTester with glove-100-angular:

Baseline: 788966 msec to write vectors
Only reuse visited set: 768152 msec to write vectors
PR (reuse both visited and candidates): 760896 msec to write vectors

The PR also shows a small search improvement:

Baseline

Recall    QPS
0.741     2515.710
0.814     1589.089
0.924      459.971

PR (reuse both visited and candidates)

Recall    QPS
0.740     2569.156
0.813     1626.779
0.925      468.207

* to allocate, so whenever possible they're cleared and reused across calls.
*/
static class HnswSearchState {
final NeighborQueue candidates;
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 didn't include the results queue because it's typically a lot smaller and I thought it made the logic less clear (results are returned but also passed in as scratch state?)

Copy link
Contributor

@jpountz jpountz left a comment

Choose a reason for hiding this comment

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

This looks good to me as-is, but I wonder if this could look better by extracting an HnswGraphSearcher that would be responsible for keeping the state it needs, as opposed to creating this state object that needs to be passed to a static method.

I'm expecting FixedBitSet.clear to be quite expensive, maybe we'll need to reconsider whether we should use a bitset or a hash set to keep track of the visited nodes.

@msokolov
Copy link
Contributor

msokolov commented Feb 3, 2022

Thanks for working on this, and it shows a nice result. I had been discussing with some folks at work and we were considering whether it would be possible to maintain the state even longer on the search side by pooling such states in the codec. Maybe we can try in a follow-up issue.

@jpountz
Copy link
Contributor

jpountz commented Feb 3, 2022

I've become a bit cautious with codec-level caching. E.g. we have threadlocals for stored fields which end up storing num_search_threads * num_indices * num_segments_per_index states overall, which is not always negligible depending on the size of the state. It's possible to try to share across fields or segments, but you need to pay attention to the case when they might use different file formats, which introduces complexity.

@jtibshirani
Copy link
Member Author

jtibshirani commented Feb 3, 2022

Extracting HnswGraphSearcher is a lot nicer, I will push a refactor.

On the topic of hash sets, I tried switching to IntIntHashMap on top of this PR and it gives a nice indexing speed-up. I can open a follow-up PR.

About reusing data structures across searches -- I've heard that some other HNSW implementations found this to be beneficial, specifically maintaining a shared pool of "visited" sets for searches to use. It could indeed be complex though, I'd be curious to understand the the magnitude of the improvement.

Copy link
Contributor

@mayya-sharipova mayya-sharipova left a comment

Choose a reason for hiding this comment

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

@jtibshirani Thanks Julie, very nice enhancement

new HnswGraphSearcher(
similarityFunction,
new NeighborQueue(beamWidth, similarityFunction.reversed == false),
new FixedBitSet(vectorValues.size()));
Copy link
Contributor

Choose a reason for hiding this comment

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

@jtibshirani I am wondering what is the reasoning of using FixedBitSet during graph construction? Are we expecting to visit most of the graph nodes ( I guess that's true on a smaller graphs)?

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 used a FixedBitSet here because the insertion and access operations are faster. Also, using SparseFixedBitSet here doesn't actually really save memory allocations, since during the graph construction we're expected to visit each node at least once.

I tried an experiment where we shared the visited set but kept SparseFixedBitSet and it didn't help performance. I also tried switching to FixedBitSet during search and it hurt performance, probably because we are allocating a very large array once per search.

@jtibshirani jtibshirani merged commit 57d9515 into apache:main Feb 4, 2022
@jtibshirani jtibshirani deleted the hnsw-search-state branch February 4, 2022 00:00
jtibshirani added a commit that referenced this pull request Feb 4, 2022
#641)

A couple of the data structures used in HNSW search are pretty large and
expensive to allocate. This commit creates a shared candidates queue and
visited set that are reused across calls to HnswGraph#searchLevel. Now the same
data structures are used for building the entire graph, which can cut down on
allocations during indexing. For graph building it also switches the visited
set to FixedBitSet for better performance.
@jtibshirani
Copy link
Member Author

I filed https://issues.apache.org/jira/browse/LUCENE-10404 so we don't forget about the hash set idea.

@msokolov
Copy link
Contributor

msokolov commented Feb 4, 2022 via email

@msokolov
Copy link
Contributor

msokolov commented Feb 4, 2022

re: sharing across searches I agree it gets tricky, but can be bounded RAM if we use some kind of LRU cache as a pool, and in this case the only "compatibility" check is the size of the bitset. But yeah, definitely more complex, we should only do it if there is a good speedup

@jtibshirani
Copy link
Member Author

I think we cannot use intinthashset in core

I used IntIntHashMap as a quick test, which was recently copied in from hppc to org.apache.lucene.util.hppc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants