SteNicholas commented on code in PR #1486:
URL: https://github.com/apache/paimon-rust/pull/1486#discussion_r4653600128


##########
crates/paimon/src/vindex/pkvector/metric.rs:
##########
@@ -139,10 +139,34 @@ fn cosine_similarity(query: &[f32], stored: &[f32]) -> 
f32 {
         stored_norm += stored[i] * stored[i];
     }
     let denominator = ((query_norm as f64).sqrt() * (stored_norm as 
f64).sqrt()) as f32;
+    if query_norm.is_normal()
+        && stored_norm.is_normal()

Review Comment:
   **A normal accumulated norm can still have lost nonzero coordinates.** I 
used a 257-dimensional query with `s = 2^-63` in the last position and zeros 
elsewhere. A second vector has the same last coordinate and `s * 2^-12` in the 
other 256 positions. Each of those squares rounds to zero before the sum. The 
dot product and both norms pass this guard, yet the second vector gets PK 
distance `0.0` and raw cosine `1.0`. Its wide cosine is `0.9999924`, so giving 
it the smaller row ID makes it beat the exact match. Please account for 
underflow during norm accumulation and cover this near-parallel pair in the PK 
and raw scorers.



##########
crates/paimon/src/table/de_vector_read.rs:
##########
@@ -2746,10 +2747,18 @@ fn compute_raw_vector_score_matrix(
                 }
                 RawVectorMetric::Cosine => {
                     let denominator = stored_l2_squared_norm.sqrt() * 
query_l2_squared_norm.sqrt();
-                    if denominator == 0.0 {
-                        0.0
-                    } else {
+                    if stored_l2_squared_norm.is_normal()
+                        && query_l2_squared_norm.is_normal()
+                        && denominator.is_normal()
+                        && score.is_finite()
+                    {
                         *score / denominator
+                    } else {
+                        let stored =
+                            &stored_vectors[row_index * dimension..(row_index 
+ 1) * dimension];
+                        let query = &query_vectors
+                            [matrix_query_index * 
dimension..(matrix_query_index + 1) * dimension];
+                        cosine_similarity_wide(query, stored)

Review Comment:
   **One all-zero query now triggers a full `f64` rescan of every stored 
vector.** Its norm fails the guard for every row, so each call recomputes the 
dot product and both norms after SGEMM. Every finite row scores zero for that 
query. In a local release benchmark of this function with 32 queries, 1,024 
finite rows and 256 dimensions, one zero query made the batch take about 1.7× 
as long as on the base commit. Please distinguish a true zero vector from an 
underflowed norm once per query or stored row so finite pairs containing a zero 
vector can return zero directly.



##########
crates/paimon/src/table/de_vector_read.rs:
##########
@@ -2829,10 +2838,14 @@ fn compute_raw_vector_score_from_values(
                 norm_b += stored * stored;
             }
             let denominator = query_l2_squared_norm.sqrt() * norm_b.sqrt();
-            if denominator == 0.0 {
-                0.0
-            } else {
+            if query_l2_squared_norm.is_normal()
+                && norm_b.is_normal()
+                && denominator.is_normal()
+                && dot.is_finite()

Review Comment:
   **A finite dot product can already have rounded to zero.** With `s = 2^-63`, 
query `[s, 0]`, row 10 `[0, s]` and row 11 `[s * 2^-24, s]`, the exact dot for 
row 11 is `2^-150`. It rounds to zero in `f32` even though its cosine `2^-24` 
is representable. With `s = 1` both raw paths return row 11. At the smaller 
scale both return row 10, and the PK distances tie at `1.0`. Please handle 
dot-product underflow in all three fast paths and add this near-orthogonal 
Top-1 case.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to