Skip to contents

[Experimental] Builds a kNN graph from an embedding matrix on the GPU. Four searches are available: an exact brute-force scan, an IVF index that probes a subset of Voronoi cells, the NN-descent kNN graph itself (no index, no search), and a CAGRA graph pruned from the NN-descent build and beam searched. Rows the descent left short are topped up with an exact search of only those rows.

Euclidean distances come back as true L2, not squared.

Usage

rs_gpu_knn(embd, k, knn_method, nn_params, seed, backend = "wgpu", verbose)

Arguments

embd

Numeric matrix of embeddings, cells x features.

k

Integer. Number of neighbours to return, self excluded.

knn_method

String. One of c("nndescent", "cagra", "exhaustive", "ivf").

nn_params

A named list with the parameters, see params_nn_gpu()

seed

Integer. Random seed for reproducibility.

backend

String. "wgpu" or "mlx" (Apple Silicon, needs a build with the MLX backend, see mlx_available()).

verbose

Integer. 0L - quiet; 1L - normal verbosity; 2L - detailed verbosity.

Value

A named list with:

  • indices - Integer matrix of shape cells x k with 0-based neighbour indices.

  • dist - Numeric matrix of shape cells x k with distances to the neighbours.

  • dist_metric - Character. The distance metric used.