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

Fix kmeans init endless loop when dist is inf #557

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
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
19 changes: 16 additions & 3 deletions src/math_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,10 +397,7 @@ void kmeanspp_selecting_pivots(float *data, size_t num_points, size_t dim, float
std::uniform_real_distribution<> distribution(0, 1);
std::uniform_int_distribution<size_t> int_dist(0, num_points - 1);
size_t init_id = int_dist(generator);
size_t num_picked = 1;

picked.push_back(init_id);
std::memcpy(pivot_data, data + init_id * dim, dim * sizeof(float));

float *dist = new float[num_points];

Expand All @@ -410,6 +407,22 @@ void kmeanspp_selecting_pivots(float *data, size_t num_points, size_t dim, float
dist[i] = math_utils::calc_distance(data + i * dim, data + init_id * dim, dim);
}

for (int64_t i = 0; i < (int64_t)num_points; i++)
{
if (std::isif(dist[i])) {
diskann::cout << "dist is inf, falling back to random pivot";
<< std::endl;
delete[] dist;
selecting_pivots(data, num_points, dim, pivot_data, num_centers);
return;
}
}

size_t num_picked = 1;

picked.push_back(init_id);
std::memcpy(pivot_data, data + init_id * dim, dim * sizeof(float));

double dart_val;
size_t tmp_pivot;
bool sum_flag = false;
Expand Down