Experimental file-backed analysis
Stefano Cacciatore
Source:vignettes/massive-data.Rmd
massive-data.RmdOne analysis interface
Use pca(), tsne(), umap(), and
graph_cluster() for both resident and file-backed analysis.
massive_matrix() describes a row-major float32 file without
loading it. A raw .f32 file needs its row and column
counts; an .fbin file contains them in its header. Keep all
output paths on storage with sufficient space. These routes are
experimental and their supported backends differ.
Base R’s options() can set session defaults for the
backend, CPU workers, storage preference, and file-backed working-memory
budget. Explicit backend and memory_limit
arguments take precedence over their session defaults. The public
analysis functions also accept an explicit storage
argument. A storage choice that conflicts with the input is rejected.
storage = "file_backed" requires a disk-backed descriptor;
it never writes a resident matrix to disk implicitly. The memory budget
limits algorithm working buffers, not all process RSS or GPU allocator
overhead. These ordinary R options can be shared by packages that use
the same values; restore the previous values when the analysis is
finished.
old_options <- options(
backend = "cpu", n.cores = 4L,
storage = "file_backed",
memory_limit = "8GB"
)
x <- massive_matrix("features.f32", nrow = 1000000L, ncol = 128L)
knn <- precompute_knn(x, k = 30L, output = "features-knn")
fuzzy <- massive_umap_fuzzy_graph(knn, output = "features-fuzzy",
sort_method = "bucketed")
options(old_options)For file-backed KNN, precompute_knn() returns a
file-backed graph descriptor rather than a resident
fastEmbedR_knn object. It selects bounded exact or sharded
approximate search according to the backend and row count. Only
Euclidean distance is currently supported for this route. The output
prefix must be new and point to storage with enough free space. Convert
the KNN descriptor to a fuzzy graph or affinity graph before a full UMAP
or t-SNE fit. The fuzzy-graph conversion is currently a CPU step and
also reads the session memory budget when memory_limit is
omitted. For t-SNE, use
massive_tsne_affinities(knn, perplexity = 30, output = "features-affinity").
library(fastEmbedR)
x <- massive_matrix("features.f32", nrow = 1000000, ncol = 128)
fit_pca <- pca(x, ncomp = 30L, backend = "cpu",
massive = "out_of_core",
output = "scores.f32", memory_limit = "8GB")
first_scores <- massive_read_rows(fit_pca$scores, first = 1L, n = 6L)pca() selects a memory-aware route for a file-backed
descriptor when massive is omitted. To require bounded
block reads even when the source is small, set
massive = "out_of_core". A large resident R matrix is not
silently converted to a file-backed source.
File-backed PCA also accepts a landmark count or fraction through the
same pca() entry point. It fits the selected rows, projects
every source row into a file-backed score matrix, and reports the
landmark scope and stages. massive_read_rows() reads only
the requested scores. TRUE chooses an automatic count.
Explicit row indices and checkpoint/resume for this PCA landmark route
are not yet supported; such requests fail before fitting.
fit_pca_landmark <- pca(x, ncomp = 30L, landmarks = 10000L,
backend = "cpu", output = "scores-landmark.f32",
memory_limit = "8GB")
summary(fit_pca_landmark)
first_scores <- massive_read_rows(fit_pca_landmark, first = 1L, n = 20L)To load a file-backed matrix into RAM intentionally, set
storage = "resident" and provide a memory limit that admits
the float32 input. The same choice works with tsne(),
umap(), and graph_cluster(). The file is
loaded once into a float::float32 matrix before fitting.
The limit checks input admission and does not bound the fit’s later RAM
or GPU allocations. Resident loading does not accept file-backed output,
chunk, or resume controls. Leave storage unset to keep the
file-backed route.
x <- massive_matrix("features.f32", nrow = 1000000L, ncol = 128L)
fit <- pca(x, ncomp = 30L, backend = "cpu", n.cores = 16L,
storage = "resident", memory_limit = "4GB")Landmark embedding and clustering
For a file-backed matrix, set landmarks to a positive
integer count to request an approximate reference fit and
fixed-reference projection. Resident matrices also accept fractions and
explicit row indices. This changes the algorithm and should be reported
with the result. A persistent output path is required for file-backed
inputs. A CPU, Metal, or CUDA request either runs on that backend or
fails explicitly. Metal uses one Apple GPU; CUDA device support varies
by route.
layout_u <- umap(x, landmarks = 10000L, backend = "cpu",
output = "umap.f32", memory_limit = "8GB")
layout_t <- tsne(x, landmarks = 10000L, backend = "cpu",
output = "tsne.f32", memory_limit = "8GB")
groups <- graph_cluster(x, method = "leiden", landmarks = 10000L,
backend = "cpu", output = "clusters",
memory_limit = "8GB")
summary(layout_u)
massive_read_rows(layout_u, first = 1L, n = 20L)
massive_read_rows(groups, first = 1L, n = 20L)For a file-backed landmark fit, summary() reports the
landmark scope, actual backend, stage times, and bounded output access.
massive_read_rows() reads only the requested rows of an
embedding or clustering result. For a resident matrix, the same calls
use the ordinary resident route when landmarks is absent.
graph_cluster() accepts Leiden, Louvain, and Leiden. A
landmark result assigns the remaining observations by weighted voting;
it is not full-data graph clustering.
Full-graph analysis
A full file-backed fit can prepare its nearest-neighbor graph and
initialization in one call. UMAP uses a streamed spectral start from its
fuzzy graph; t-SNE uses PCA. It records the KNN, graph, initialization,
and optimization times separately. The KNN policy may choose approximate
search for larger inputs; all rows are still optimized. Set
checkpoint = TRUE to record completed stages and native
progress. For a Metal one-call full fit, faissR’s exact Metal batches
prepare KNN before the Metal optimizer.
fit$pipeline$stage_backend, knn_method, and
stage_resources record the work and memory estimates for
each stage. Exact file-backed KNN can be much slower than approximate
resident KNN; compare measured quality and timing on each dataset. Metal
t-SNE bounds graph uploads when graph streaming is selected, but keeps
coordinates and optimizer state in memory and reserves FFT space. The
device working-set admission rejects oversized fits before graph
mapping; runtime checks stop a fit that exceeds the budget. Its existing
atomic checkpoints preserve the completed iteration and optimizer state.
This route streams the graph only, not the entire fit. The resident 2D
Metal UMAP quality repair does not qualify file-backed UMAP. Resident
UMAP fits, including wide CSR and prepared resident graphs, do not
checkpoint optimizer state. Interruption requires a new fit from the
beginning; reusing a prepared graph only avoids rebuilding that graph.
checkpoint = TRUE and resume = TRUE apply to
the separate file-backed route. That route uses a restartable global
graph sort and needs more SSD space. After an interruption, repeat the
same call with checkpoint = TRUE, resume = TRUE; changed
inputs or controls fail. An incomplete spectral output is rebuilt from
the verified graph on resume. Other work completed before its first
native checkpoint may need a new output.
small <- massive_matrix("small-features.f32", nrow = 2000L,
ncol = 128L)
layout <- umap(small, n_neighbors = 30L, n_components = 2L,
backend = "cpu", storage = "file_backed",
output = "full-umap.f32", memory_limit = "8GB")
layout_t <- tsne(small, perplexity = 30, n_components = 3L,
backend = "cpu", storage = "file_backed",
output = "full-tsne.f32", memory_limit = "8GB")
summary(layout)
massive_read_rows(layout, first = 1L, n = 20L)Prepare and reuse the graph explicitly when multiple fits share KNN or when stage-level recovery is required. The example below uses a bounded exact graph on a size where that search is practical.
small <- massive_matrix("small-features.f32", nrow = 2000L,
ncol = 128L)
knn <- precompute_knn(small, k = 30L, output = "full_knn",
backend = "cpu", memory_limit = "8GB")
fuzzy <- massive_umap_fuzzy_graph(knn, output = "fuzzy",
sort_method = "bucketed")
initial <- massive_matrix("initial_2d.f32", nrow = small$nrow,
ncol = 2L)
layout <- umap(fuzzy, init = initial, output = "full_umap.f32",
backend = "cpu", memory_limit = "8GB")
communities <- graph_cluster(fuzzy, method = "leiden",
output = "full_leiden",
backend = "cpu", memory_limit = "8GB")The initialization must have one row per observation and two or three columns. A full t-SNE fit uses an affinity graph from the same KNN result, with a matching file-backed initialization:
affinity <- massive_tsne_affinities(
knn, perplexity = 30, output = "full_affinity")
layout_t <- tsne(affinity, perplexity = 30, Y_init = initial,
output = "full_tsne.f32", backend = "cpu",
memory_limit = "8GB")For file-backed full-graph fits, umap(),
tsne(), and graph_cluster() infer the route
from the prepared graph. Specify
massive = "out_of_core_graph" when an explicit route is
preferable. Choose a file-backed graph and initialization appropriate to
available memory and storage. Neither memory_limit nor CUDA
buffer estimates are guarantees about total process RSS or GPU allocator
overhead.
Checkpoint and resume controls are available for supported stages, but recovery requires the same input files, backend, graph, and controls. File-backed work should be run on a machine where SSD streaming is permitted; the UCT HPC benchmark uses resident data only.