fix(accel): restore f32::EPSILON near-zero-denom guard in cosine_similarity
CI / test (pull_request) Canceled after 0s
CI / test (pull_request) Canceled after 0s
The SIMD migration weakened the near-zero-norm guard in all four clawhdf5-accel cosine_similarity backends (scalar/avx2/avx512/neon) from `denom < f32::EPSILON` to `denom == 0.0`. Vectors with a tiny but nonzero norm (denom in (0, 1.19e-7)) fell through to dot/denom and scored as identical instead of maximally dissimilar, diverging from the pre-SIMD scalar loop's documented fallback behavior. Restores the epsilon threshold in all four backends so `1.0 - cosine_similarity(...)` in hnsw.rs::compute_distance reproduces the old fallback exactly. Adds regression tests in clawhdf5-accel and clawhdf5-ann locking in the near-zero-norm case. Co-Authored-By: Claude Sonnet 5 <[email protected]>
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
07b7301ded
commit
377c8b6f17
@@ -1300,6 +1300,18 @@ mod tests {
|
||||
assert!((d - 1.0).abs() < 1e-6); // zero vector -> distance 1
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cosine_near_zero_vector() {
|
||||
// Tiny-but-nonzero, identical-direction vectors: denom is well
|
||||
// below f32::EPSILON but not exactly 0.0. Must still be treated
|
||||
// as a degenerate/unreliable direction (distance 1, "maximally
|
||||
// dissimilar"), not as an exact match (distance 0).
|
||||
let a = vec![1e-4, 1e-4];
|
||||
let b = vec![1e-4, 1e-4];
|
||||
let d = compute_distance(&a, &b, DistanceMetric::Cosine);
|
||||
assert!((d - 1.0).abs() < 1e-6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn insert_into_empty_index() {
|
||||
let mut index = HnswIndex::new(4, 16, DistanceMetric::L2);
|
||||
|
||||
Reference in New Issue
Block a user