Skip to content

Commit 7fa26e8

Browse files
committed
Fix adjustment on non-aligned vectors
1 parent 47f4875 commit 7fa26e8

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

libs/simdvec/src/main21/java/org/elasticsearch/simdvec/internal/Int7SQVectorScorerSupplier.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,15 @@ protected void bulkScoreFromSegment(
128128
) {
129129
long firstByteOffset = (long) firstOrd * vectorPitch;
130130
var a = vectors.asSlice(firstByteOffset, vectorLength);
131-
var aOffset = Float.intBitsToFloat(vectors.asSlice(firstByteOffset + vectorLength, Float.BYTES).get(ValueLayout.JAVA_INT, 0));
131+
var aOffset = Float.intBitsToFloat(
132+
vectors.asSlice(firstByteOffset + vectorLength, Float.BYTES).getAtIndex(ValueLayout.JAVA_INT_UNALIGNED, 0)
133+
);
132134
for (int i = 0; i < numNodes; ++i) {
133135
var secondOrd = ordinals.getAtIndex(ValueLayout.JAVA_INT, i);
134136
long secondByteOffset = (long) secondOrd * vectorPitch;
135137
var b = vectors.asSlice(secondByteOffset, vectorLength);
136138
var bOffset = Float.intBitsToFloat(
137-
vectors.asSlice(secondByteOffset + vectorLength, Float.BYTES).getAtIndex(ValueLayout.JAVA_INT, 0)
139+
vectors.asSlice(secondByteOffset + vectorLength, Float.BYTES).getAtIndex(ValueLayout.JAVA_INT_UNALIGNED, 0)
138140
);
139141
var score = scoreFromSegments(a, aOffset, b, bOffset);
140142
scores.setAtIndex(ValueLayout.JAVA_FLOAT, i, score);
@@ -230,13 +232,15 @@ protected void bulkScoreFromSegment(
230232
Similarities.dotProduct7uBulkWithOffsets(vectors, firstVector, dims, vectorPitch, ordinals, numNodes, scores);
231233

232234
// Java-side adjustment
233-
var aOffset = Float.intBitsToFloat(vectors.asSlice(firstByteOffset + vectorLength, Float.BYTES).get(ValueLayout.JAVA_INT, 0));
235+
var aOffset = Float.intBitsToFloat(
236+
vectors.asSlice(firstByteOffset + vectorLength, Float.BYTES).getAtIndex(ValueLayout.JAVA_INT_UNALIGNED, 0)
237+
);
234238
for (int i = 0; i < numNodes; ++i) {
235239
var dotProduct = scores.getAtIndex(ValueLayout.JAVA_FLOAT, i);
236240
var secondOrd = ordinals.getAtIndex(ValueLayout.JAVA_INT, i);
237241
long secondByteOffset = (long) secondOrd * vectorPitch;
238242
var bOffset = Float.intBitsToFloat(
239-
vectors.asSlice(secondByteOffset + vectorLength, Float.BYTES).getAtIndex(ValueLayout.JAVA_INT, 0)
243+
vectors.asSlice(secondByteOffset + vectorLength, Float.BYTES).getAtIndex(ValueLayout.JAVA_INT_UNALIGNED, 0)
240244
);
241245
float adjustedDistance = dotProduct * scoreCorrectionConstant + aOffset + bOffset;
242246
scores.setAtIndex(ValueLayout.JAVA_FLOAT, i, Math.max((1 + adjustedDistance) / 2, 0f));

0 commit comments

Comments
 (0)