Skip to content

[BUG] Sparse brute-force kNN / sparse L2 distances are not deterministic across runs #3094

Open

Description

@betatim

Describe the bug

cuvs::neighbors::brute_force::search on a sparse (CSR) index returns different distances, and different neighbors, for identical inputs from one call to the next.

Downstream impact: cuML's t-SNE (and other kNN-graph consumers such as UMAP and NearestNeighbors) document that a fixed random_state gives reproducible results. On sparse input that isn't true, because the kNN graph changes between runs.

I found this while investigating an intermittent cuML CI failure in test_tsne_distance_metrics_on_sparse_input[euclidean-fft] (NVIDIA/cuml#8939).

Steps/Code to reproduce bug

cuVS's Python API doesn't expose sparse brute-force search, so this goes through cuML's NearestNeighbors. With sparse input it calls cuvs::neighbors::brute_force::build/search with sparse_search_params.

import numpy as np
import scipy.sparse
from sklearn.datasets import make_blobs

from cuml.neighbors import NearestNeighbors

X, _ = make_blobs(n_samples=1000, n_features=64, centers=5, random_state=42)
X = scipy.sparse.csr_matrix(X.astype(np.float32))

def knn():
    nn = NearestNeighbors(n_neighbors=90, metric="euclidean").fit(X)
    d, i = nn.kneighbors(X)
    return np.asarray(d), np.asarray(i)

d0, i0 = knn()
for run in range(1, 6):
    d, i = knn()
    print(
        f"run {run}: dists identical={np.array_equal(d0, d)}, "
        f"max |diff|={np.abs(d0 - d).max():.6f}, "
        f"rows with different neighbors={(i0 != i).any(axis=1).sum()}"
    )

Output (RTX A6000):

run 1: dists identical=False, max |diff|=0.038273, rows with different neighbors=33
run 2: dists identical=False, max |diff|=0.038273, rows with different neighbors=38
run 3: dists identical=False, max |diff|=0.038273, rows with different neighbors=32
run 4: dists identical=False, max |diff|=0.038273, rows with different neighbors=24
run 5: dists identical=False, max |diff|=0.038273, rows with different neighbors=27

The same data as a dense array, which goes through the dense brute-force path, returns identical results on every run.

Expected behavior

Repeated calls with the same index, queries and parameters on the same GPU return bit-identical distances and indices, as the dense brute-force path does. If deterministic accumulation costs too much to be the default, an opt-in (e.g. a flag on sparse_search_params) would let callers that promise reproducibility use it.

Environment details (please complete the following information):

  • Environment location: Bare-metal, NVIDIA RTX A6000, driver 580.178.04
  • Method of cuVS install: conda (rapidsai-nightly), libcuvs 27.02.00a9 cuda13_261115054142_aac89669, with cuML 27.02 nightly, Python 3.14

The same code is on cuVS release/26.12 (668006e), and the original CI failure was on cuML 26.12 (wheel, CUDA 12.9, H100).

Additional context

The same atomic_add_op / compute_row_*_kernel pattern is shared by other sparse metrics (cosine, correlation, Hellinger, …), so they likely have the same issue.

Claude helped track this down and gave some additional details.

Activity

  1. arcusbuilds commented on Nov 21, 2026

    @arcusbuilds

    I'd like to take this. The drift comes from two places:

    1. L2 row norms: compute_row_norm_kernel / compute_row_sum_kernel in l2_distance.cuh (and bin_distance.cuh) atomicAdd every non-zero into out[row].
    2. coo_spmv_kernel.cuh: a B row can span several warps/blocks, and each partial sum is atomic-added into the same output cell.

    Plan: PR 1 sums the row norms in a fixed order over indptr (cheap, so no flag) and adds a test that runs the search twice and checks the output is bit-identical. PR 2 adds an opt-in deterministic option where one warp owns each output cell, default off. Does that split work for you, and is there a preferred place for the flag?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions