fix(search): keep zero float16 cosine scores finite

Updated the handling of zero-norm rows to use a representable denominator of 1 instead of 1e-10.
This commit is contained in:
yunyv 2026-10-24 10:52:20 -06:00 • committed by GitHub
parent c773fa7b77
commit 3e6a37318a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -33,7 +33,7 @@ def batch_cosine_similarity(nd_array1: np.ndarray, nd_array2: np.ndarray) -> np.
norms1 = np.linalg.norm(nd_array1, axis=1)
norms2 = np.linalg.norm(nd_array2, axis=1)
norm_products = np.outer(norms1, norms2)
# Guard against zero-norm rows to keep division finite.
norm_products = np.where(norm_products == 0, 1e-10, norm_products)
# Use a representable denominator even in float16; zero rows have zero dot products.
norm_products = np.where(norm_products == 0, 1, norm_products)
return dot_products / norm_products