Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Numerical stability fixes for l2 pairwise distance #1319

Merged
merged 4 commits into from
Mar 9, 2023

Conversation

benfred
Copy link
Member

@benfred benfred commented Mar 8, 2023

When computing L2 distance, we could sometimes see the distances go slightly negative. This is because of numerical instability, especially when computing self distance. For L2SqrtExpanded, this would end up taking the sqrt of this negative value - which introduced NaN values.

Fix by clamping distances to 0.

This matches the behaviour found in the fused_l2 distance calculation:

acc[i][j] = acc_ij > DataT{0} ? raft::sqrt(acc_ij) : DataT{0};

And is similar to that found in the sparse l2 distance calculation:

// correct for small instabilities
C[(size_t)i * n_cols + j] = val * (fabs(val) >= 0.0001);

When computing L2 distance, we could sometimes see the distances go slightly negative.
This is because of numerical instability, especially when computing self distance.
For L2SqrtExpanded, this would end up taking the sqrt of this negative value -
which introduced NaN values.

Fix by clamping distances to 0.

This matches the behaviour found in the fused_l2 distance calculation:
https://github.com/rapidsai/raft/blob/3ca7eacc5cb411facdfb08ff27663a3402486bc4/cpp/include/raft/distance/detail/fused_l2_nn.cuh#L179

And is similar to that found in the sparse l2 distance calculation:
https://github.com/rapidsai/raft/blob/3ca7eacc5cb411facdfb08ff27663a3402486bc4/cpp/include/raft/sparse/distance/detail/l2_distance.cuh#L84-L85
@benfred benfred requested a review from a team as a code owner March 8, 2023 01:21
@github-actions github-actions bot added the cpp label Mar 8, 2023
@benfred benfred added improvement Improvement / enhancement to an existing function non-breaking Non-breaking change and removed cpp labels Mar 8, 2023
@cjnolet
Copy link
Member

cjnolet commented Mar 8, 2023

/merge

@github-actions github-actions bot added the cpp label Mar 8, 2023
@rapids-bot rapids-bot bot merged commit 0f774b9 into rapidsai:branch-23.04 Mar 9, 2023
@benfred benfred deleted the l2_stability branch March 10, 2023 17:15
lowener pushed a commit to lowener/raft that referenced this pull request Mar 15, 2023
When computing L2 distance, we could sometimes see the distances go slightly negative. This is because of numerical instability, especially when computing self distance. For L2SqrtExpanded, this would end up taking the sqrt of this negative value - which introduced NaN values.

Fix by clamping distances to 0.

This matches the behaviour found in the fused_l2 distance calculation: https://github.com/rapidsai/raft/blob/3ca7eacc5cb411facdfb08ff27663a3402486bc4/cpp/include/raft/distance/detail/fused_l2_nn.cuh#L179

And is similar to that found in the sparse l2 distance calculation: https://github.com/rapidsai/raft/blob/3ca7eacc5cb411facdfb08ff27663a3402486bc4/cpp/include/raft/sparse/distance/detail/l2_distance.cuh#L84-L85

Authors:
  - Ben Frederickson (https://github.com/benfred)
  - Corey J. Nolet (https://github.com/cjnolet)

Approvers:
  - Corey J. Nolet (https://github.com/cjnolet)

URL: rapidsai#1319
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cpp improvement Improvement / enhancement to an existing function non-breaking Non-breaking change
Projects
Development

Successfully merging this pull request may close these issues.

2 participants