-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Pull out common subclasses for our custom flat & hnsw vector formats #132663
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
129bc37
Pull out common subclasses for our custom flat & hnsw vector formats
thecoop 95924de
[CI] Auto commit changes from spotless
1d8d757
Fix compile
thecoop 98a38ab
Change to FlatVectorsFormat
thecoop 8cf7302
Merge branch 'main' into abstract-knn-format-classes
thecoop 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
44 changes: 44 additions & 0 deletions
44
server/src/main/java/org/elasticsearch/index/codec/vectors/AbstractFlatVectorsFormat.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,44 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package org.elasticsearch.index.codec.vectors; | ||
|
|
||
| import org.apache.lucene.codecs.hnsw.FlatVectorsFormat; | ||
| import org.apache.lucene.codecs.hnsw.FlatVectorsScorer; | ||
| import org.elasticsearch.core.SuppressForbidden; | ||
|
|
||
| import static org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper.MAX_DIMS_COUNT; | ||
|
|
||
| public abstract class AbstractFlatVectorsFormat extends FlatVectorsFormat { | ||
|
|
||
| public static final boolean USE_DIRECT_IO = getUseDirectIO(); | ||
|
|
||
| @SuppressForbidden( | ||
| reason = "TODO Deprecate any lenient usage of Boolean#parseBoolean https://github.com/elastic/elasticsearch/issues/128993" | ||
| ) | ||
| private static boolean getUseDirectIO() { | ||
| return Boolean.parseBoolean(System.getProperty("vector.rescoring.directio", "false")); | ||
| } | ||
|
|
||
| protected AbstractFlatVectorsFormat(String name) { | ||
| super(name); | ||
| } | ||
|
|
||
| protected abstract FlatVectorsScorer flatVectorsScorer(); | ||
|
|
||
| @Override | ||
| public int getMaxDimensions(String fieldName) { | ||
| return MAX_DIMS_COUNT; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return getName() + "(name=" + getName() + ", flatVectorScorer=" + flatVectorsScorer() + ")"; | ||
| } | ||
| } | ||
115 changes: 115 additions & 0 deletions
115
server/src/main/java/org/elasticsearch/index/codec/vectors/AbstractHnswVectorsFormat.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,115 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package org.elasticsearch.index.codec.vectors; | ||
|
|
||
| import org.apache.lucene.codecs.KnnVectorsFormat; | ||
| import org.apache.lucene.codecs.hnsw.FlatVectorsFormat; | ||
| import org.apache.lucene.codecs.lucene99.Lucene99HnswVectorsFormat; | ||
| import org.apache.lucene.search.TaskExecutor; | ||
| import org.apache.lucene.util.hnsw.HnswGraph; | ||
|
|
||
| import java.util.concurrent.ExecutorService; | ||
|
|
||
| import static org.apache.lucene.codecs.lucene99.Lucene99HnswVectorsFormat.DEFAULT_BEAM_WIDTH; | ||
| import static org.apache.lucene.codecs.lucene99.Lucene99HnswVectorsFormat.DEFAULT_MAX_CONN; | ||
| import static org.apache.lucene.codecs.lucene99.Lucene99HnswVectorsFormat.DEFAULT_NUM_MERGE_WORKER; | ||
| import static org.apache.lucene.codecs.lucene99.Lucene99HnswVectorsFormat.MAXIMUM_BEAM_WIDTH; | ||
| import static org.apache.lucene.codecs.lucene99.Lucene99HnswVectorsFormat.MAXIMUM_MAX_CONN; | ||
| import static org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper.MAX_DIMS_COUNT; | ||
|
|
||
| public abstract class AbstractHnswVectorsFormat extends KnnVectorsFormat { | ||
|
|
||
| /** | ||
| * Controls how many of the nearest neighbor candidates are connected to the new node. Defaults to | ||
| * {@link Lucene99HnswVectorsFormat#DEFAULT_MAX_CONN}. See {@link HnswGraph} for more details. | ||
| */ | ||
| protected final int maxConn; | ||
|
|
||
| /** | ||
| * The number of candidate neighbors to track while searching the graph for each newly inserted | ||
| * node. Defaults to {@link Lucene99HnswVectorsFormat#DEFAULT_BEAM_WIDTH}. See {@link HnswGraph} | ||
| * for details. | ||
| */ | ||
| protected final int beamWidth; | ||
|
|
||
| protected final int numMergeWorkers; | ||
| protected final TaskExecutor mergeExec; | ||
|
|
||
| /** Constructs a format using default graph construction parameters */ | ||
| protected AbstractHnswVectorsFormat(String name) { | ||
| this(name, DEFAULT_MAX_CONN, DEFAULT_BEAM_WIDTH, DEFAULT_NUM_MERGE_WORKER, null); | ||
| } | ||
|
|
||
| /** | ||
| * Constructs a format using the given graph construction parameters. | ||
| * | ||
| * @param maxConn the maximum number of connections to a node in the HNSW graph | ||
| * @param beamWidth the size of the queue maintained during graph construction. | ||
| */ | ||
| protected AbstractHnswVectorsFormat(String name, int maxConn, int beamWidth) { | ||
| this(name, maxConn, beamWidth, DEFAULT_NUM_MERGE_WORKER, null); | ||
| } | ||
|
|
||
| /** | ||
| * Constructs a format using the given graph construction parameters and scalar quantization. | ||
| * | ||
| * @param maxConn the maximum number of connections to a node in the HNSW graph | ||
| * @param beamWidth the size of the queue maintained during graph construction. | ||
| * @param numMergeWorkers number of workers (threads) that will be used when doing merge. If | ||
| * larger than 1, a non-null {@link ExecutorService} must be passed as mergeExec | ||
| * @param mergeExec the {@link ExecutorService} that will be used by ALL vector writers that are | ||
| * generated by this format to do the merge | ||
| */ | ||
| protected AbstractHnswVectorsFormat(String name, int maxConn, int beamWidth, int numMergeWorkers, ExecutorService mergeExec) { | ||
| super(name); | ||
| if (maxConn <= 0 || maxConn > MAXIMUM_MAX_CONN) { | ||
| throw new IllegalArgumentException( | ||
| "maxConn must be positive and less than or equal to " + MAXIMUM_MAX_CONN + "; maxConn=" + maxConn | ||
| ); | ||
| } | ||
| if (beamWidth <= 0 || beamWidth > MAXIMUM_BEAM_WIDTH) { | ||
| throw new IllegalArgumentException( | ||
| "beamWidth must be positive and less than or equal to " + MAXIMUM_BEAM_WIDTH + "; beamWidth=" + beamWidth | ||
| ); | ||
| } | ||
| this.maxConn = maxConn; | ||
| this.beamWidth = beamWidth; | ||
| if (numMergeWorkers == 1 && mergeExec != null) { | ||
| throw new IllegalArgumentException("No executor service is needed as we'll use single thread to merge"); | ||
| } | ||
| this.numMergeWorkers = numMergeWorkers; | ||
| if (mergeExec != null) { | ||
| this.mergeExec = new TaskExecutor(mergeExec); | ||
| } else { | ||
| this.mergeExec = null; | ||
| } | ||
| } | ||
|
|
||
| protected abstract FlatVectorsFormat flatVectorsFormat(); | ||
|
|
||
| @Override | ||
| public int getMaxDimensions(String fieldName) { | ||
| return MAX_DIMS_COUNT; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return getName() | ||
| + "(name=" | ||
| + getName() | ||
| + ", maxConn=" | ||
| + maxConn | ||
| + ", beamWidth=" | ||
| + beamWidth | ||
| + ", flatVectorFormat=" | ||
| + flatVectorsFormat() | ||
| + ")"; | ||
| } | ||
| } |
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
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
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.
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.
seems like since we are moving to a mapper setting, direct IO can be a parameter or an abstract method (like you did with flat in HNSW) that is supplied in the ctor and sub-classes of the appropriate type will always provide their subsequent "true/false"?
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.
These are kinda in the same code area - I would prefer to merge this in first (which doesn't change behaviour), then update the direct IO PR to use the abstract classes introduced here in its formats, removing the direct IO JVM option at the same time