Experimental file-backed query-to-landmark nearest neighbors
Source:R/massive_knn.R
massive_landmark_knn.RdSearches a disk-backed matrix in bounded query batches. The default
search keeps the landmark reference in RAM or VRAM. Explicit CPU
stream_exact scans bounded reference blocks without materializing the
full landmark matrix; this exact route is quadratic in query and
landmark counts. CPU exact and persistent faissR HNSW, and CUDA exact
or IVF-Flat search retain references across query batches. Metal exact
search retains a host float32 reference but reuploads it each batch.
CUDA needs faissR FAISS GPU support for the single-device landmark route.
Sharded landmark and full-graph CUDA search also use faissR.
Landmark faissR IVF uses a heuristic until a separate exact-reference
audit is run. Sharded IVF-Flat calibrates on the first query batch;
pilot recall is not guaranteed for later rows. For CUDA IVF,
pilot_recall counts distance-equivalent ties at the exact kth distance;
pilot_id_recall reports mean neighbor-ID overlap. Persistent sharded
IVF also requires tie-aware pilot_min_row_recall and mean strict
pilot_id_recall to meet the target. pilot_min_strict_row_recall
and pilot_tied_rows expose boundary-tie effects. Stricter probing
can increase query time substantially.
With method = "auto", faissR selects an available search method
using the input shape, query count, k, and recall target.
Output files contain row-major, one-based uint32 landmark indices and
float32 distances. File-backed CPU HNSW streams reference chunks through
faissR into a resident index; the index itself is not disk resident.
HNSW's 0.99 recall is a target, not a measured result.
Checkpointed HNSW uses one index-build thread for reproducible resume;
n.cores still controls query search. The effective build thread count
is recorded in hnsw_settings.
Explicit CUDA sharded_exact builds one bounded reference shard at a
time and merges exact candidates on disk. It rereads every query for
each shard and can be much slower than an in-VRAM index.
Usage
massive_landmark_knn(
x,
reference,
k,
output,
backend = "cpu",
n.cores = 1L,
method = c("auto", "exact", "stream_exact", "hnsw", "ivf", "sharded_exact",
"sharded_ivf"),
chunk_rows = NULL,
reference_chunk_rows = NULL,
memory_limit = fastembedr_memory_option(),
checkpoint = FALSE,
resume = FALSE,
devices = NULL,
checkpoint_every = 100L,
ef_search = 0L,
target_recall = NULL
)Arguments
- x
Query
massive_matrix()descriptor.- reference
Landmark
massive_matrix()descriptor or the result ofmassive_select_landmarks().- k
Number of nearest landmarks.
- output
New file prefix for
.indices.u32and.distances.f32.- backend
"cpu","cuda", or"metal". Metal uses exact search with bounded query batches and a resident reference.- n.cores
CPU worker count.
- method
"auto","exact"(also Metal),"stream_exact"(CPU with a file-backed reference),"hnsw"(CPU faissR),"ivf"(CUDA), or"sharded_exact"or"sharded_ivf"(CUDA with a file-backed reference). Sharded IVF is approximate and needs a recall audit.- chunk_rows
Maximum query rows per batch.
- reference_chunk_rows
Maximum reference rows per batch for
"stream_exact","sharded_exact", or"sharded_ivf"; otherwise must beNULL.- memory_limit
Conservative RAM budget. For CUDA, the planner also caps estimated VRAM use at 70% of this limit and 65% of free VRAM.
- checkpoint
Save completed KNN batches to a sidecar file. Multi-GPU
"sharded_ivf"checkpoints only completed GPU query shards. Single-GPU"sharded_ivf"does not support checkpointing.- resume
Continue from matching partial KNN files and checkpoint. Sharded exact CUDA search commits every 100 query blocks and at each reference-shard boundary. Multi-GPU sharded IVF reuses completed GPU shards but restarts an interrupted shard from its first query row.
- devices
Optional CUDA device indices. Multiple devices search disjoint query ranges, including with
sharded_exact. Checkpointing requires file-backed query and reference sources.- checkpoint_every
Commit every this many query blocks for sharded CUDA search. Other search methods use their existing checkpoint policy.
- ef_search
CPU HNSW landmark search effort. Zero uses the faissR preset. A positive value must be at least
k + 1; it changes search effort but does not guarantee recall, which needs a separate audit.- target_recall
Requested tuning tier:
0.9,0.95, or0.99.NULLusesoptions(fastEmbedR.target_recall = ...), then0.99.