Skip to content
Merged
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
Prev Previous commit
Clamp IVF-RaBitQ probe distances to non-negative values
  • Loading branch information
bdice committed Oct 10, 2026
commit c799bd980b9fc3b69240552bcc5ffaecc9c9afb4
10 changes: 10 additions & 0 deletions cpp/src/neighbors/ivf_rabitq/gpu_index/ivf_gpu.cu
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
#include <cuvs/selection/select_k.hpp>
#include <raft/core/cublas_macros.hpp>
#include <raft/core/device_mdspan.hpp>
#include <raft/core/operators.hpp>
#include <raft/core/resources.hpp>
#include <raft/linalg/detail/cublaslt_wrappers.hpp>
#include <raft/linalg/norm.cuh>
#include <raft/linalg/unary_op.cuh>

#include <rmm/device_buffer.hpp>
#include <rmm/device_uvector.hpp>
Expand Down Expand Up @@ -1016,6 +1018,14 @@ void IVFGPU::PrepareClusterSearchInputs(
&beta,
searcher.get_centroid_distances(),
num_centroids);
// Rounding in the expanded form can make ||q - c||^2 slightly negative when the query is on (or
// very near) a centroid. The search kernels take sqrt(||q - c||^2) for the error bound, which is
// NaN for a negative value, and a NaN lower bound drops every vector of that list, so clamp to 0.
raft::linalg::unaryOp(searcher.get_centroid_distances(),
searcher.get_centroid_distances(),
batch_size * num_centroids,
raft::plug_const_op<float, raft::max_op>{0.f},
searcher_stream.get());
Comment on lines +1021 to +1028

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The GEMM above accumulates -2 q.c onto ||q||^2 + ||c||^2 (L990-L1020). When q == c, cancellation can leave a tiny negative value. The search kernels read that value as q_g_add and take sqrtf(q_g_add) for the error bound: bitwise block sort L56-L58, LUT16 L52-L57, LUT emit-top-k L55. The NaN then propagates into low_dist, and low_dist < threshold is false for every vector in the list (bitwise L92-L96).

I put the clamp here because every search mode goes through this function: BatchClusterSearch, BatchClusterSearchLUT16 and BatchClusterSearchQuantizeQuery all call it (L1072, L1099, L1127). The same clamped values feed select_k for probe selection and est_dist in the kernels, so probe selection and the distance estimate use the same, valid distance.

Question for reviewers: this adds one elementwise pass over batch_size * n_lists floats per search batch. The alternative is sqrtf(fmaxf(q_g_add, 0.f)) in each kernel, as searcher_gpu_quantize_query.cu L117 already does for norm_quan. That has no extra pass, but every new kernel would have to remember the clamp, and the negative value would still reach select_k and est_dist. Which do you prefer?


// Step 4: select top-nprobe clusters per query
auto d_raft_vals = raft::make_device_matrix<float, int64_t>(searcher_handle, batch_size, nprobe);
Expand Down
Loading