ggml_matmul: GPU matrix multiply (drop-in for '%*%')

View source: R/gpu_linalg.R

ggml_matmulR Documentation

GPU matrix multiply (drop-in for %*%)

Description

Computes A %*% B on the Vulkan GPU and returns an ordinary R matrix, with a transparent CPU fallback. A drop-in accelerator for a large matrix multiply: no autograd, no wrapper types required — plain matrices in and out.

Usage

ggml_matmul(A, B, device = "auto", prec = "f32")

Arguments

A, B

Numeric matrices with ncol(A) == nrow(B).

device

"auto" (default: GPU when present and the multiply is large enough to amortise the transfer, else CPU), "gpu" (force GPU, still falls back to CPU if none), or "cpu".

prec

"f32" (default; requests f32 accumulation, ~1e-3 relative error or better depending on the driver) or "f16" (faster, never more precise). Only affects the GPU path.

Details

Precision: R multiplies in double precision (f64); a GPU offers f32 at best, so the GPU result never matches R to full double precision. prec = "f32" (the default) requests the f32 accumulation kernel, but how close the result actually lands depends on the Vulkan driver: some accumulate mul_mat in f16 regardless (e.g. RADV / Mesa), giving a relative error around 1e-3 either way. Treat the GPU path as a fast, approximate multiply — typically 1e-3, better on drivers with true f32 accumulation — not a bit-for-bit replacement for %*%. prec = "f16" only ever lowers precision; use it when speed matters more than the last digits.

Value

The product A %*% B as a numeric matrix.

See Also

ggml_crossprod, ggml_tcrossprod, as_gpu_matrix

Examples

A <- matrix(rnorm(4), 2); B <- matrix(rnorm(4), 2)
ggml_matmul(A, B, device = "cpu")

ggmlR documentation built on July 14, 2026, 1:08 a.m.