Skip to content

Commit 9ae3844

Browse files
authored
Don't normalize centroids with zero samples. Fixes #15209 (#15213)
1 parent 15ed5d7 commit 9ae3844

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

lucene/CHANGES.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ Bug Fixes
8989
* GITHUB#14847: Allow Faiss vector format to index >2GB of vectors per-field per-segment by using MemorySegment APIs
9090
(instead of ByteBuffer) to copy bytes to native memory. (Kaival Parikh)
9191

92+
* GITHUB#15213: Don't normalize centroids with zero samples, which caused divide-by-zero. (Mike Sokolov)
93+
9294
Changes in Runtime Behavior
9395
---------------------
9496
* GITHUB#14187: The query cache is now disabled by default. (Adrien Grand)

lucene/core/src/java/org/apache/lucene/codecs/lucene104/Lucene104ScalarQuantizedVectorsWriter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,9 @@ static int mergeAndRecalculateCentroids(
529529
mergedCentroid[j] += centroid[j] * vectorCount;
530530
}
531531
}
532-
if (recalculate) {
532+
if (totalVectorCount == 0) {
533+
return 0;
534+
} else if (recalculate) {
533535
return calculateCentroid(mergeState, fieldInfo, mergedCentroid);
534536
} else {
535537
for (int j = 0; j < mergedCentroid.length; j++) {

lucene/core/src/java/org/apache/lucene/util/VectorUtil.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,14 @@ public static float dotProduct(float[] a, float[] b) {
6565
throw new IllegalArgumentException("vector dimensions differ: " + a.length + "!=" + b.length);
6666
}
6767
float r = IMPL.dotProduct(a, b);
68-
assert Float.isFinite(r);
68+
assert Float.isFinite(r)
69+
: "not finite: "
70+
+ r
71+
+ " from <"
72+
+ java.util.Arrays.toString(a)
73+
+ ","
74+
+ java.util.Arrays.toString(b)
75+
+ ">";
6976
return r;
7077
}
7178

0 commit comments

Comments
 (0)