Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adjust Hellinger pairwise distance to vaoid NaNs (rapidsai#189)
Browse files Browse the repository at this point in the history
This change will fix NaNs that arise in Hellinger pairwise distance when the input to sqrt is negative.
These kind of inputs can happen even when the inputs are normalized row-wise to 1 due to accumulation of rounding approximation.

Authors:
  - Micka (https://github.com/lowener)

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

URL: rapidsai/raft#189
lowener authored Apr 1, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent a57cf7d commit d1fd927
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion cpp/include/raft/sparse/distance/l2_distance.cuh
Original file line number Diff line number Diff line change
@@ -270,7 +270,11 @@ class hellinger_expanded_distances_t : public distances_t<value_t> {

raft::linalg::unaryOp<value_t>(
out_dists, out_dists, config_->a_nrows * config_->b_nrows,
[=] __device__(value_t input) { return sqrt(1 - input); },
[=] __device__(value_t input) {
// Adjust to replace NaN in sqrt with 0 if input to sqrt is negative
bool rectifier = abs(1 - input) > 1e-8;
return rectifier * sqrt(1 - input);
},
config_->stream);
}

0 comments on commit d1fd927

Please sign in to comment.