pca() computes principal component scores with a backend-native truncated
decomposition. CPU uses float32 blocked randomized SVD (rSVD) or bounded
covariance for suitable narrow inputs. Metal uses a package-native float32
block-subspace rSVD whose large
matrix products are executed with Metal Performance Shaders. CUDA selects
between package-native float32 rSVD and, when enabled, RAPIDS RAFT TSVD
from the matrix shape, requested rank, and estimated arithmetic cost.
Wide, low-rank problems favor rSVD; smaller, narrower, or less-truncated
problems favor TSVD. Without RAFT, CUDA uses native rSVD. CUDA requests
fail explicitly when CUDA is unavailable; they never fall back to CPU PCA.
For resident float32 CPU and CUDA matrices with at most 64 columns and
at least five million elements, PCA uses bounded covariance and projection.
The CPU route also handles matrices beyond the native rSVD size limit.
Metal uses bounded blocks beyond its safe matrix-size limit. CPU
covariance and projection use native parallel workers. CUDA uses cuBLAS
for covariance and projection. Metal covariance
and projection use native Metal blocks. The small covariance eigensystem is
solved on CPU. Scores and loadings remain float32 without writing to disk.
Usage
pca(
x,
ncomp = 2L,
xtest = NULL,
center = TRUE,
scale = FALSE,
backend = NULL,
n.cores = NULL,
seed = 4L,
tsne_init = FALSE,
massive = "off",
output = NULL,
chunk_rows = NULL,
memory_limit = "available",
checkpoint = FALSE,
resume = FALSE,
devices = NULL,
landmarks = FALSE,
storage = NULL,
power_iterations = NULL
)Arguments
- x
Numeric matrix/data frame,
float::float32matrix, ormassive_matrix()file descriptor with observations in rows.- ncomp
Number of principal components.
- xtest
Optional test/query matrix with the same columns as
x. In out-of-core mode, supply a matchingmassive_matrix()descriptor; projected scores are written besideoutputas.test.f32. Projected coordinates are returned inscores_test.- center
If
TRUE, mean-center columns before decomposition.- scale
If
TRUE, scale centered columns to unit sample standard deviation before decomposition.- backend
PCA backend:
"cpu","cuda", or"metal".- n.cores
Positive integer CPU worker limit. Small stages use fewer workers automatically. BLAS and native preprocessing use this limit in sequential stages to avoid nested parallelism. In experimental out-of-core CUDA PCA it controls the streamed CPU mean pass. It is otherwise ignored by Metal and CUDA.
- seed
Random seed for backends that use a Gaussian subspace sketch, including experimental wide streamed CPU or CUDA PCA. The RAFT covariance-eigensolver route records but does not consume this value.
- tsne_init
If
TRUE, addtsne_initto the returned PCA object. This matrix is centered and rescaled so its largest component standard deviation is1e-4, ready to pass asY_inittotsne()ortsne_knn(). In out-of-core mode, this is a file-backed 2D/3D.init.f32matrix formassive_tsne_optimize(); its normalization runs on CPU in bounded passes.- massive
"off"keeps the in-memory route. When omitted for a file-backed input descriptor,"auto"is selected."auto"estimates memory and selects the in-memory route when safe, otherwise streamed PCA."out_of_core"explicitly activates covariance PCA for amassive_matrix()source with at most 1024 columns on CPU or 2048 on CUDA. Wider matrices use randomized streamed PCA. Wide CUDA streams the mean and scale passes on CPU, runs its sketch and projection on CUDA, and reports stage backends. Metal streams covariance and score projection for at most 1024 columns using one CPU worker; the small eigendecomposition runs on CPU.- output
Required
.f32score-file path in out-of-core mode, including when auto mode selects that route.- chunk_rows
Maximum rows read per block in out-of-core mode.
- memory_limit
Conservative RAM budget for out-of-core buffers and any resident R-matrix input, for example
"512MB"or"16GB". When omitted for file-backed input, usesoptions(memory_limit = ...), or detected available RAM. This does not limit R itself or linked-library memory.- checkpoint
Save the experimental out-of-core PCA model and completed score batches beside
outputfor recovery.- resume
Continue from a matching PCA checkpoint and partial score file. Available when out-of-core processing is selected.
- devices
Optional distinct CUDA device IDs. With resident matrix input, one or more devices use the same covariance and score-projection algorithm; the small eigendecomposition runs on CPU. Multiple devices split the rows. Out-of-core CUDA PCA shards file-backed fitting and score projection across devices.
- landmarks
FALSEfits all rows. For resident PCA, a count, fraction, explicit row indices, orTRUEselects fitting rows and projects every input row onto their principal components. File-backed PCA acceptsTRUE, a fraction, or a count and writes all-row scores tooutput; explicit row indices and landmark checkpoint/resume are not yet supported. Resident Metal projects with its native matrix multiply; CPU and single-device CUDA use CPU projection. Multi-GPU CUDA projects on the requested devices. The route is recorded inprojection_backend.- storage
"auto","resident", or"file_backed". When omitted, usesoptions(storage = ...). Explicit"resident"on amassive_matrix()file descriptor loads float32 input after a RAM andmemory_limitcheck; the limit does not bound the subsequent fit."file_backed"forces out-of-core PCA, while conflictingmassivevalues fail before fitting.- power_iterations
Nonnegative integer number of power iterations for full resident CPU rSVD.
NULLuses the default for the fitted rank. Covariance, file-backed, landmark, and GPU fits requireNULL. The returnedpowergives the effective count, which is zero when the sketch already spans the full dimension.
Value
A fastEmbedR_pca list with scores, loadings,
singular_values, centering/scaling vectors, backend metadata, and
decomposition metadata. When tsne_init = TRUE, the list also
contains tsne_init; when xtest is supplied, it also contains
scores_test. Out-of-core scores_test is file-backed. Metal and CUDA
preserve float::float32 scores, loadings,
initialization, and compatible test projections when the input is
float32. CPU results also record the worker and BLAS limits.
Details
CPU matrix products use single-precision OpenBLAS when linked to R;
otherwise the portable native kernels are used. QR and SVD
use the BLAS/LAPACK linked to R. The returned gemm_backend reports the
matrix-product implementation on the rSVD route.
Set n.cores to control their CPU core limit. The requested value is
applied temporarily through standard BLAS/OpenMP environment variables and,
when installed, RhpcBLASctl; the prior process settings are restored after
the call. A single-threaded BLAS can still report one effective thread. With
float::float32 input, native Metal and CUDA preprocessing and the returned
scores/loadings also remain float32. The API intentionally has no
decomposition method menu and does not call Python. Explicit unavailable GPU
requests fail instead of being reported as GPU work.
For difficult low-rank CPU rSVD fits, compare loading subspaces across
seeds or with a centered reference on the same input. The smallest
singular value of the cross-product of two orthonormal loading bases
measures their least-aligned direction. If agreement is poor, refit with
power_iterations = 3L and reassess. Extra iterations add matrix passes.

