precompute_knn() exposes the same nearest-neighbor search
used internally by umap() and tsne(). The native faissR selector
chooses the search method for the requested backend.
Usage
precompute_knn(
data,
k = 30L,
metric = c("euclidean", "cosine", "correlation"),
backend = NULL,
n.cores = NULL,
storage = NULL,
output = NULL,
memory_limit = NULL,
devices = NULL,
checkpoint = FALSE,
resume = FALSE,
checkpoint_every = 100L,
target_recall = NULL,
audit_rows = 0L
)Arguments
- data
Numeric matrix, numeric data frame,
float::float32matrix, ormassive_matrix()file descriptor 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.
- storage
"auto","resident", or"file_backed". When omitted,options(storage = ...)is used. A file-backed input must be a disk-backedmassive_matrix()descriptor; a resident matrix is never silently copied to disk.- output
New file prefix required for a file-backed KNN result.
- memory_limit
Working-memory budget for file-backed KNN. CUDA also uses it to cap estimated VRAM. When omitted,
options(memory_limit = ...)is used, or detected available RAM.- devices
Optional distinct CUDA device indices. Device work division depends on the selected KNN method. See Details.
- checkpoint
Save completed file-backed KNN query blocks.
- resume
Continue a matching file-backed KNN checkpoint.
- checkpoint_every
Save progress after this many query blocks.
- target_recall
Requested approximate-search recall tier:
0.9,0.95, or0.99(default). When omitted, usesoptions(fastEmbedR.target_recall = ...). This is a tuning target, not a guarantee of measured recall. Exact search remains exact.- audit_rows
Number of fixed file-backed query rows to compare with an exact full-reference search (default zero). At most 10,000 rows. The returned graph reports measured sampled recall separately from
target_recall. This control is unavailable for resident input.
Value
A resident fastEmbedR_knn object, or a file-backed
fastEmbedR_massive_graph descriptor. Resident host results contain
indices and distances. CUDA results with device-resident buffers
also inherit from fastEmbedR_gpu_knn. Resident
results record knn_provider = "faissR" and the selected engine.
Both storage modes return knn_selection with the method, selection
reason, shape group, and requested recall tier reported by faissR.
Details
For resident inputs, CPU uses Flat below 5,000 observations and
HNSW for larger inputs. Euclidean HNSW checks a full-reference pilot.
Cosine and correlation use shape-based HNSW tuning and report recall as
unaudited. CUDA and Metal choose between Flat and IVF-Flat according to
the input shape, search effort, and backend limits. The policy uses k
and target_recall where they affect the available search. For GPU
inputs with at least 500,000 rows and 256 columns, a target of 0.99
selects Flat. Inputs with at least 500,000 rows and 1,024 columns select
Flat at all three tiers.
Approximate search uses the requested tier to tune search effort. Pilot and sampled recall refer to the rows checked. Exact routes report exact search by construction.
A file-backed descriptor uses bounded KNN graph construction and writes
indices and distances under output. CPU uses Flat up to 5,000 rows if
estimated pair-feature work is at most 125 billion, and sharded HNSW
otherwise. CUDA and Metal use the native selector. On one GPU, file-backed
Flat holds one reference shard at a time and reuses it across query
batches. Metal IVF requires a passing capacity check and checkpointing to
be off. A failed IVF pilot raises an error. File-backed KNN supports
Euclidean distance. The
result is a fastEmbedR_massive_graph descriptor; construct UMAP fuzzy
weights or t-SNE affinities from it before full-graph fitting.
Resident CUDA Flat and IVF results stay on the device when devices is
omitted and can be passed directly to umap_knn() or tsne_knn() with
backend = "cuda". An explicit device selection returns host matrices.
For large Euclidean CUDA self searches with cuVS, faissR can select CAGRA
with NN-descent and shard a file-backed index to fit the memory budget.
Set options(fastEmbedR.cuda_knn_method = "cagra") to use faissR's
cuVS CAGRA settings for the requested recall tier, or use
"cagra_width4" to request a width-four graph search. These options
apply to CUDA Euclidean self KNN with at least 500,000 rows and require
a cuVS-enabled faissR build with CAGRA support.
Resident CAGRA returns host neighbor matrices. File-backed CAGRA stages
the source in batches and writes neighbors to disk. With
"cagra_width4" and two or more explicit CUDA devices, each GPU
builds a disjoint reference shard and searches all query batches.
faissR merges candidate neighbors before writing each batch.
File-backed CAGRA checks capacity and requires checkpointing off.
Eligible CUDA calls with multiple explicit devices use disjoint CAGRA
reference shards. faissR merges per-shard candidates on the host.
The target tier describes the settings used. Supply audit rows to
measure recall for a file-backed fit.
Set options(fastEmbedR.metal_knn_method = "cagra") for native Metal
CAGRA on resident or file-backed Euclidean input. This route requires
at least 129 rows, up to 1,024 features, and up to 128 neighbors.
A file-backed Metal CAGRA index uses shared memory on one device.
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.