-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Reinstate and test the native int7u bulk dot product #138317
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
ChrisHegarty
merged 11 commits into
elastic:main
from
ChrisHegarty:int7u_native_bulk_test
Nov 20, 2025
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
899ef62
Add a test for the native int7u bulk dot product
ChrisHegarty 1d27a1a
Merge branch 'main' into int7u_native_bulk_test
ChrisHegarty 8719edf
itr
ChrisHegarty 7a469cb
Merge remote-tracking branch 'chegar/int7u_native_bulk_test' into int…
ChrisHegarty c92c7ba
Merge branch 'main' into int7u_native_bulk_test
ChrisHegarty 313a54a
typo
ChrisHegarty d949441
typo
ChrisHegarty 919139a
itr
ChrisHegarty 2f66007
itr
ChrisHegarty 4f1bfe6
reinstate the vector scorer
ChrisHegarty 137a57a
bump version
ChrisHegarty 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
|
|
||
| import java.lang.foreign.MemorySegment; | ||
|
|
||
| import static java.lang.foreign.ValueLayout.JAVA_FLOAT_UNALIGNED; | ||
| import static org.hamcrest.Matchers.containsString; | ||
|
|
||
| public class JDKVectorLibraryInt7uTests extends VectorSimilarityFunctionsTests { | ||
|
|
@@ -71,6 +72,11 @@ public void testInt7BinaryVectors() { | |
| assertEquals(expected, dotProduct7u(heapSeg1, heapSeg2, dims)); | ||
| assertEquals(expected, dotProduct7u(nativeSeg1, heapSeg2, dims)); | ||
| assertEquals(expected, dotProduct7u(heapSeg1, nativeSeg2, dims)); | ||
|
|
||
| // trivial bulk with a single vector | ||
| float[] bulkScore = new float[1]; | ||
| dotProduct7uBulk(nativeSeg1, nativeSeg2, dims, 1, MemorySegment.ofArray(bulkScore)); | ||
| assertEquals(expected, bulkScore[0], 0f); | ||
| } | ||
|
|
||
| // square distance | ||
|
|
@@ -86,6 +92,32 @@ public void testInt7BinaryVectors() { | |
| } | ||
| } | ||
|
|
||
| public void testInt7uBulk() { | ||
|
Contributor
Author
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. This test reliably reproduces the previous segv issue. |
||
| assumeTrue(notSupportedMsg(), supported()); | ||
| final int dims = size; | ||
| final int numVecs = randomIntBetween(2, 101); | ||
| var values = new byte[numVecs][dims]; | ||
| var segment = arena.allocate((long) dims * numVecs); | ||
| for (int i = 0; i < numVecs; i++) { | ||
| randomBytesBetween(values[i], MIN_INT7_VALUE, MAX_INT7_VALUE); | ||
| MemorySegment.copy(MemorySegment.ofArray(values[i]), 0L, segment, (long) i * dims, dims); | ||
| } | ||
| int queryOrd = randomInt(numVecs - 1); | ||
| float[] expectedScores = new float[numVecs]; | ||
| dotProductBulkScalar(values[queryOrd], values, expectedScores); | ||
|
|
||
| var nativeQuerySeg = segment.asSlice((long) queryOrd * dims, dims); | ||
| var bulkScoresSeg = arena.allocate((long) numVecs * Float.BYTES); | ||
| dotProduct7uBulk(segment, nativeQuerySeg, dims, numVecs, bulkScoresSeg); | ||
| assertScoresEquals(expectedScores, bulkScoresSeg); | ||
|
|
||
| if (supportsHeapSegments()) { | ||
| float[] bulkScores = new float[numVecs]; | ||
| dotProduct7uBulk(segment, nativeQuerySeg, dims, numVecs, MemorySegment.ofArray(bulkScores)); | ||
| assertArrayEquals(expectedScores, bulkScores, 0f); | ||
| } | ||
| } | ||
|
|
||
| public void testIllegalDims() { | ||
| assumeTrue(notSupportedMsg(), supported()); | ||
| var segment = arena.allocate((long) size * 3); | ||
|
|
@@ -109,6 +141,26 @@ public void testIllegalDims() { | |
| assertThat(e6.getMessage(), containsString("out of bounds for length")); | ||
| } | ||
|
|
||
| public void testBulkIllegalDims() { | ||
| assumeTrue(notSupportedMsg(), supported()); | ||
| var segA = arena.allocate((long) size * 3); | ||
| var segB = arena.allocate((long) size * 3); | ||
| var segS = arena.allocate((long) size * Float.BYTES); | ||
|
|
||
| var e1 = expectThrows(IOOBE, () -> dotProduct7uBulk(segA, segB, size, 4, segS)); | ||
| assertThat(e1.getMessage(), containsString("out of bounds for length")); | ||
|
|
||
| var e2 = expectThrows(IOOBE, () -> dotProduct7uBulk(segA, segB, size, -1, segS)); | ||
| assertThat(e2.getMessage(), containsString("out of bounds for length")); | ||
|
|
||
| var e3 = expectThrows(IOOBE, () -> dotProduct7uBulk(segA, segB, -1, 3, segS)); | ||
| assertThat(e3.getMessage(), containsString("out of bounds for length")); | ||
|
|
||
| var tooSmall = arena.allocate((long) 3 * Float.BYTES - 1); | ||
| var e4 = expectThrows(IOOBE, () -> dotProduct7uBulk(segA, segB, size, 3, tooSmall)); | ||
| assertThat(e4.getMessage(), containsString("out of bounds for length")); | ||
| } | ||
|
|
||
| int dotProduct7u(MemorySegment a, MemorySegment b, int length) { | ||
| try { | ||
| return (int) getVectorDistance().dotProductHandle7u().invokeExact(a, b, length); | ||
|
|
@@ -137,6 +189,20 @@ int squareDistance7u(MemorySegment a, MemorySegment b, int length) { | |
| } | ||
| } | ||
|
|
||
| void dotProduct7uBulk(MemorySegment a, MemorySegment b, int dims, int count, MemorySegment result) { | ||
| try { | ||
| getVectorDistance().dotProductHandle7uBulk().invokeExact(a, b, dims, count, result); | ||
| } catch (Throwable e) { | ||
| if (e instanceof Error err) { | ||
| throw err; | ||
| } else if (e instanceof RuntimeException re) { | ||
| throw re; | ||
| } else { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** Computes the dot product of the given vectors a and b. */ | ||
| static int dotProductScalar(byte[] a, byte[] b) { | ||
| int res = 0; | ||
|
|
@@ -156,4 +222,18 @@ static int squareDistanceScalar(byte[] a, byte[] b) { | |
| } | ||
| return squareSum; | ||
| } | ||
|
|
||
| static void dotProductBulkScalar(byte[] query, byte[][] data, float[] scores) { | ||
| for (int i = 0; i < data.length; i++) { | ||
| scores[i] = dotProductScalar(query, data[i]); | ||
| } | ||
| } | ||
|
|
||
| static void assertScoresEquals(float[] expectedScores, MemorySegment expectedScoresSeg) { | ||
| assert expectedScores.length == (expectedScoresSeg.byteSize() / Float.BYTES); | ||
| for (int i = 0; i < expectedScores.length; i++) { | ||
| assertEquals(expectedScores[i], expectedScoresSeg.get(JAVA_FLOAT_UNALIGNED, i * Float.BYTES), 0f); | ||
| } | ||
| } | ||
|
|
||
| } | ||
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
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
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
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.
I added these runtime checks here just before going native, similar to other functions, so that we have a handle on the values that we pass through. Passing invalid values could lead to crashes or worse, so we enforce these here.