Skip to contents

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::float32 matrix, or massive_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 matching massive_matrix() descriptor; projected scores are written beside output as .test.f32. Projected coordinates are returned in scores_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, add tsne_init to the returned PCA object. This matrix is centered and rescaled so its largest component standard deviation is 1e-4, ready to pass as Y_init to tsne() or tsne_knn(). In out-of-core mode, this is a file-backed 2D/3D .init.f32 matrix for massive_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 a massive_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 .f32 score-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, uses options(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 output for 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

FALSE fits all rows. For resident PCA, a count, fraction, explicit row indices, or TRUE selects fitting rows and projects every input row onto their principal components. File-backed PCA accepts TRUE, a fraction, or a count and writes all-row scores to output; 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 in projection_backend.

storage

"auto", "resident", or "file_backed". When omitted, uses options(storage = ...). Explicit "resident" on a massive_matrix() file descriptor loads float32 input after a RAM and memory_limit check; the limit does not bound the subsequent fit. "file_backed" forces out-of-core PCA, while conflicting massive values fail before fitting.

power_iterations

Nonnegative integer number of power iterations for full resident CPU rSVD. NULL uses the default for the fitted rank. Covariance, file-backed, landmark, and GPU fits require NULL. The returned power gives 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.

Examples

fit <- pca(
    as.matrix(iris[, 1:4]),
    ncomp = 2,
    n.cores = 2,
    seed = 1,
    tsne_init = TRUE
)
plot(fit$scores, pch = 21, bg = iris$Species)

plot(fit$tsne_init, pch = 21, bg = iris$Species)