-
Notifications
You must be signed in to change notification settings - Fork 25.5k
[GPU] Optimize merge memory usage #136411
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
Open
ldematte
wants to merge
10
commits into
elastic:main
Choose a base branch
from
ldematte:gpu/optimize-merge-space
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+296
−124
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c56c707
Use the internal raw vector data during merge, avoid additional tmp file
ldematte 0819dbd
Fix access
ldematte 48a3f7c
Expose vector values from Int7SQVectorScorerSupplier
ldematte 7cede4c
Merge branch 'main' into gpu/optimize-merge-space
ldematte 1fc9ff1
PR feedback
ldematte b4bad58
Merge remote-tracking branch 'upstream/main' into gpu/optimize-merge-…
ldematte a904598
[CI] Auto commit changes from spotless
3622f30
Merge remote-tracking branch 'upstream/main' into gpu/optimize-merge-…
ldematte de1f42a
Fix
ldematte 9da8566
Merge branch 'main' into gpu/optimize-merge-space
ldematte 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
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
97 changes: 97 additions & 0 deletions
97
...main/java/org/elasticsearch/index/codec/vectors/reflect/VectorsFormatReflectionUtils.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,97 @@ | ||
/* | ||
* 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.reflect; | ||
|
||
import org.apache.lucene.codecs.lucene95.HasIndexSlice; | ||
import org.apache.lucene.codecs.lucene99.Lucene99ScalarQuantizedVectorsWriter; | ||
import org.apache.lucene.index.FloatVectorValues; | ||
import org.apache.lucene.util.hnsw.CloseableRandomVectorScorerSupplier; | ||
import org.apache.lucene.util.hnsw.RandomVectorScorerSupplier; | ||
import org.elasticsearch.simdvec.QuantizedByteVectorValuesAccess; | ||
|
||
import java.lang.invoke.MethodHandles; | ||
import java.lang.invoke.VarHandle; | ||
|
||
public class VectorsFormatReflectionUtils { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wow, very nice organization! +1 for using VarHandle for reflection |
||
private static final VarHandle FLOAT_SUPPLIER_HANDLE; | ||
private static final VarHandle BYTE_SUPPLIER_HANDLE; | ||
private static final VarHandle FLOAT_VECTORS_HANDLE; | ||
|
||
private static final Class<?> FLAT_CLOSEABLE_RANDOM_VECTOR_SCORER_SUPPLIER_CLASS; | ||
private static final Class<?> SCALAR_QUANTIZED_CLOSEABLE_RANDOM_VECTOR_SCORER_SUPPLIER_CLASS; | ||
private static final Class<?> FLOAT_SCORING_SUPPLIER_CLASS; | ||
static { | ||
try { | ||
FLAT_CLOSEABLE_RANDOM_VECTOR_SCORER_SUPPLIER_CLASS = Class.forName( | ||
"org.apache.lucene.codecs.lucene99.Lucene99FlatVectorsWriter$FlatCloseableRandomVectorScorerSupplier" | ||
); | ||
var lookup = MethodHandles.privateLookupIn(FLAT_CLOSEABLE_RANDOM_VECTOR_SCORER_SUPPLIER_CLASS, MethodHandles.lookup()); | ||
FLOAT_SUPPLIER_HANDLE = lookup.findVarHandle( | ||
FLAT_CLOSEABLE_RANDOM_VECTOR_SCORER_SUPPLIER_CLASS, | ||
"supplier", | ||
RandomVectorScorerSupplier.class | ||
); | ||
|
||
FLOAT_SCORING_SUPPLIER_CLASS = Class.forName( | ||
"org.apache.lucene.internal.vectorization.Lucene99MemorySegmentFloatVectorScorerSupplier" | ||
); | ||
lookup = MethodHandles.privateLookupIn(FLOAT_SCORING_SUPPLIER_CLASS, MethodHandles.lookup()); | ||
FLOAT_VECTORS_HANDLE = lookup.findVarHandle(FLOAT_SCORING_SUPPLIER_CLASS, "values", FloatVectorValues.class); | ||
|
||
SCALAR_QUANTIZED_CLOSEABLE_RANDOM_VECTOR_SCORER_SUPPLIER_CLASS = Class.forName( | ||
Lucene99ScalarQuantizedVectorsWriter.class.getCanonicalName() + "$ScalarQuantizedCloseableRandomVectorScorerSupplier" | ||
); | ||
lookup = MethodHandles.privateLookupIn(SCALAR_QUANTIZED_CLOSEABLE_RANDOM_VECTOR_SCORER_SUPPLIER_CLASS, MethodHandles.lookup()); | ||
BYTE_SUPPLIER_HANDLE = lookup.findVarHandle( | ||
SCALAR_QUANTIZED_CLOSEABLE_RANDOM_VECTOR_SCORER_SUPPLIER_CLASS, | ||
"supplier", | ||
RandomVectorScorerSupplier.class | ||
); | ||
|
||
} catch (IllegalAccessException e) { | ||
throw new AssertionError("should not happen, check opens", e); | ||
} catch (ReflectiveOperationException e) { | ||
throw new AssertionError(e); | ||
} | ||
} | ||
|
||
public static RandomVectorScorerSupplier getFlatRandomVectorScorerInnerSupplier(CloseableRandomVectorScorerSupplier scorerSupplier) { | ||
if (scorerSupplier.getClass().equals(FLAT_CLOSEABLE_RANDOM_VECTOR_SCORER_SUPPLIER_CLASS)) { | ||
return (RandomVectorScorerSupplier) FLOAT_SUPPLIER_HANDLE.get(scorerSupplier); | ||
} | ||
return null; | ||
} | ||
|
||
public static RandomVectorScorerSupplier getScalarQuantizedRandomVectorScorerInnerSupplier( | ||
CloseableRandomVectorScorerSupplier scorerSupplier | ||
) { | ||
if (scorerSupplier.getClass().equals(SCALAR_QUANTIZED_CLOSEABLE_RANDOM_VECTOR_SCORER_SUPPLIER_CLASS)) { | ||
return (RandomVectorScorerSupplier) BYTE_SUPPLIER_HANDLE.get(scorerSupplier); | ||
} | ||
return null; | ||
} | ||
|
||
public static HasIndexSlice getFloatScoringSupplierVectorOrNull(RandomVectorScorerSupplier scorerSupplier) { | ||
if (FLOAT_SCORING_SUPPLIER_CLASS.isAssignableFrom(scorerSupplier.getClass())) { | ||
var vectorValues = FLOAT_VECTORS_HANDLE.get(scorerSupplier); | ||
if (vectorValues instanceof HasIndexSlice indexSlice) { | ||
return indexSlice; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public static HasIndexSlice getByteScoringSupplierVectorOrNull(RandomVectorScorerSupplier scorerSupplier) { | ||
if (scorerSupplier instanceof QuantizedByteVectorValuesAccess quantizedByteVectorValuesAccess) { | ||
ldematte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return quantizedByteVectorValuesAccess.get(); | ||
} | ||
return null; | ||
} | ||
} |
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.
Is there an open Lucene issue or PR for that?
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.
Not yet; depending on how #136416 goes, and the opinion of people more expert in Lucene (you, Chris, Ben), I'd like to generalize what we did there and raise a Lucene issue to have it.
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.
FYI, I honestly don't see why Lucene would ever expose this information. It expands an API for no good purpose within Lucene.
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.
Maybe not this API as-is, but I'd think there is value in having the ability to access back in a convenient and efficient way what has been written so far; it avoids having to write the same data more than once, or keep copies on memory, when we need the original data (e.g. raw vectors) to add "something" on top of it (e.g. quantized vectors, graph, etc.).
(But maybe I'm naive)