You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by so...@apache.org on 2025/09/23 17:45:29 UTC

(lucene) branch branch_10x updated: Don't normalize centroids with zero samples. Fixes #15209 (#15213)

This is an automated email from the ASF dual-hosted git repository.

sokolov pushed a commit to branch branch_10x
in repository https://gitbox.apache.org/repos/asf/lucene.git


The following commit(s) were added to refs/heads/branch_10x by this push:
     new e5f58adcf0f Don't normalize centroids with zero samples. Fixes #15209 (#15213)
e5f58adcf0f is described below

commit e5f58adcf0f51b1e89c6afd6df63d8756db2bf51
Author: Michael Sokolov <so...@falutin.net>
AuthorDate: Mon Sep 22 11:10:20 2025 -0400

    Don't normalize centroids with zero samples. Fixes #15209 (#15213)
---
 lucene/CHANGES.txt                                               | 2 ++
 .../codecs/lucene104/Lucene104ScalarQuantizedVectorsWriter.java  | 4 +++-
 lucene/core/src/java/org/apache/lucene/util/VectorUtil.java      | 9 ++++++++-
 3 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 87dccb6055c..8625c382d47 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -61,6 +61,8 @@ Bug Fixes
   removing co-planar points to do it once after all holes are removed instead of doing it for each
   removed hole.
 
+* GITHUB#15213: Don't normalize centroids with zero samples, which caused divide-by-zero. (Mike Sokolov)
+
 Other
 ---------------------
 (No changes)
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene104/Lucene104ScalarQuantizedVectorsWriter.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene104/Lucene104ScalarQuantizedVectorsWriter.java
index a63c3730afa..5455568fdb6 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene104/Lucene104ScalarQuantizedVectorsWriter.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene104/Lucene104ScalarQuantizedVectorsWriter.java
@@ -530,7 +530,9 @@ public class Lucene104ScalarQuantizedVectorsWriter extends FlatVectorsWriter {
         mergedCentroid[j] += centroid[j] * vectorCount;
       }
     }
-    if (recalculate) {
+    if (totalVectorCount == 0) {
+      return 0;
+    } else if (recalculate) {
       return calculateCentroid(mergeState, fieldInfo, mergedCentroid);
     } else {
       for (int j = 0; j < mergedCentroid.length; j++) {
diff --git a/lucene/core/src/java/org/apache/lucene/util/VectorUtil.java b/lucene/core/src/java/org/apache/lucene/util/VectorUtil.java
index 133000dc016..9ad396a1f0e 100644
--- a/lucene/core/src/java/org/apache/lucene/util/VectorUtil.java
+++ b/lucene/core/src/java/org/apache/lucene/util/VectorUtil.java
@@ -65,7 +65,14 @@ public final class VectorUtil {
       throw new IllegalArgumentException("vector dimensions differ: " + a.length + "!=" + b.length);
     }
     float r = IMPL.dotProduct(a, b);
-    assert Float.isFinite(r);
+    assert Float.isFinite(r)
+        : "not finite: "
+            + r
+            + " from <"
+            + java.util.Arrays.toString(a)
+            + ","
+            + java.util.Arrays.toString(b)
+            + ">";
     return r;
   }