Skip to content

Commit 6206073

Browse files
authored
[BUG] Distance calculations sporadically fail on sse. (#5500)
## Description of changes The distance_sse functions started flaking in CI. It looks like they are correct, just not the same stability as the baseline. Rather than play a game of numerical stability to fix, I'm making the tolerance proportional to vector length and thus the error. Also, up the number of iterations so that failures are >50% likely rather than 1 in 100. ## Test plan CI ## Migration plan N/A ## Observability plan N/A ## Documentation Changes N/A
1 parent adf862d commit 6206073

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

rust/distance/src/distance_sse.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ mod tests {
361361
// Test Euclidean distance
362362
let euclid_simd = unsafe { euclidean_distance(v1, v2) };
363363
let euclid = euclidean_distance_scalar(v1, v2);
364-
let euclid_tolerance = (euclid.abs() * 1e-4).max(1e-5);
364+
let euclid_tolerance = (euclid.abs() * 1e-4).max(1e-5) + (v1.len() as f32 * 1e-5);
365365
assert!(
366366
(euclid_simd - euclid).abs() < euclid_tolerance,
367367
"Euclidean distance mismatch: SIMD={}, Scalar={}, tolerance={}",
@@ -373,7 +373,7 @@ mod tests {
373373
// Test Cosine distance
374374
let cosine_simd = unsafe { cosine_distance(v1, v2) };
375375
let cosine = cosine_distance_scalar(v1, v2);
376-
let cosine_tolerance = (cosine.abs() * 1e-4).max(1e-5);
376+
let cosine_tolerance = (cosine.abs() * 5e-4).max(1e-4) + (v1.len() as f32 * 1e-5);
377377
assert!(
378378
(cosine_simd - cosine).abs() < cosine_tolerance,
379379
"Cosine distance mismatch: SIMD={}, Scalar={}, tolerance={}",
@@ -385,7 +385,7 @@ mod tests {
385385
// Test Inner product
386386
let inner_simd = unsafe { inner_product(v1, v2) };
387387
let inner = inner_product_scalar(v1, v2);
388-
let inner_tolerance = (inner.abs() * 1e-4).max(1e-5);
388+
let inner_tolerance = (inner.abs() * 5e-4).max(1e-4) + (v1.len() as f32 * 1e-5);
389389
assert!(
390390
(inner_simd - inner).abs() < inner_tolerance,
391391
"Inner product mismatch: SIMD={}, Scalar={}, tolerance={}",
@@ -440,11 +440,13 @@ mod tests {
440440
// Test various vector sizes
441441
let sizes = vec![16, 32, 64, 128, 256, 512, 1024, 2048, 4096];
442442

443-
for size in sizes {
444-
println!("Testing size: {}", size);
445-
let v1 = generate_random_vector(size);
446-
let v2 = generate_random_vector(size);
447-
test_distance_functions(&v1, &v2);
443+
for _ in 0..1_000 {
444+
for size in &sizes {
445+
println!("Testing size: {}", size);
446+
let v1 = generate_random_vector(*size);
447+
let v2 = generate_random_vector(*size);
448+
test_distance_functions(&v1, &v2);
449+
}
448450
}
449451
}
450452

0 commit comments

Comments
 (0)