Skip to contents

precompute_knn() exposes the same package-native nearest-neighbor search used internally by umap() and tsne(). The search algorithm is chosen by fastEmbedR for the requested backend and is deliberately not a user parameter.

Usage

precompute_knn(
  data,
  k = 30L,
  metric = c("euclidean", "cosine", "correlation"),
  backend = NULL,
  n.cores = NULL
)

Arguments

data

Numeric matrix, numeric data frame, or a float::float32 matrix with observations in rows.

k

Number of non-self nearest neighbors to return.

metric

Distance metric: "euclidean", "cosine", or "correlation".

backend

Search backend: "cpu", "cuda", or "metal".

n.cores

Number of CPU cores. Native GPU backends ignore this argument.

Value

A fastEmbedR_knn object. Host results contain indices and distances; CUDA results additionally inherit from fastEmbedR_gpu_knn and own device-resident index and distance buffers.

Details

CPU uses native exhaustive float32 search below 5,000 observations and HNSW otherwise. HNSW applies a metric-, shape-, and k-aware policy calibrated for target recall 0.99; its recall is not audited during each call. tuning_reference_target_met describes the faissR calibration cell and is not measured recall for the current call. Metal uses native exact search for small inputs and recall-tuned IVF-Flat for larger inputs. CUDA uses RAPIDS cuVS brute-force exact search below 100,000 observations and cuVS IVF-Flat above that threshold. Approximate Metal and CUDA routes use an internal recall target of 0.99 and report whether their pilot audit met it.

The CUDA result remains on the GPU and can be passed directly to umap_knn() or tsne_knn() with backend = "cuda". CPU and Metal results contain one-based indices and distances matrices. Every result excludes the observation itself. An unavailable requested backend raises an error; no CPU fallback is reported as GPU work.

Examples

x <- scale(as.matrix(iris[, 1:4]))
knn <- precompute_knn(x, k = 15, backend = "cpu", n.cores = 2)
layout <- umap_knn(knn, backend = "cpu", seed = 1)