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

[REVIEW] Porting over recent updates to distance prim from cuml #172

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion cpp/include/raft/sparse/distance/bin_distance.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,13 @@ class jaccard_expanded_distances_t : public distances_t<value_t> {
config_->handle, config_->allocator, config_->stream,
[] __device__ __host__(value_t dot, value_t q_norm, value_t r_norm) {
value_t q_r_union = q_norm + r_norm;
return 1 - (dot / (q_r_union - dot));
value_t denom = q_r_union - dot;

value_t jacc = ((denom != 0) * dot) / ((denom == 0) + denom);

// flip the similarity when both rows are 0
bool both_empty = q_r_union == 0;
return 1 - ((!both_empty * jacc) + both_empty);
});
}

Expand Down
5 changes: 4 additions & 1 deletion cpp/include/raft/sparse/distance/l2_distance.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,10 @@ class cosine_expanded_distances_t : public distances_t<value_t> {
value_t norms = sqrt(q_norm) * sqrt(r_norm);
// deal with potential for 0 in denominator by forcing 0/1 instead
value_t cos = ((norms != 0) * dot) / ((norms == 0) + norms);
return 1 - cos;

// flip the similarity when both rows are 0
bool both_empty = (q_norm == 0) && (r_norm == 0);
return 1 - ((!both_empty * cos) + both_empty);
});
}

Expand Down
16 changes: 16 additions & 0 deletions cpp/test/sparse/distance.cu
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,22 @@ class SparseDistanceTest
};

const std::vector<SparseDistanceInputs<int, float>> inputs_i32_f = {
{5,
{0, 0, 1, 2},

{1, 2},
{0.5, 0.5},
{0, 1, 1, 1, 0, 1, 1, 1, 0},
raft::distance::DistanceType::CosineExpanded,
0.0},
{5,
{0, 0, 1, 2},

{1, 2},
{1.0, 1.0},
{0, 1, 1, 1, 0, 1, 1, 1, 0},
raft::distance::DistanceType::JaccardExpanded,
0.0},
{2,
{0, 2, 4, 6, 8},
{0, 1, 0, 1, 0, 1, 0, 1}, // indices
Expand Down