stochastic-rs
Concepts

GPU support

What runs on a GPU, on which backend, at what precision — the fGN and sheet pipelines, the Euler engine, and the configurations that stay on the host.

Every process carries its sampling backend as a type parameter and is re-typed with .on::<B>() (see backends). This page is the inventory: which processes run on which device, at what precision, and which configurations stay on the host.

The support matrix

FamilyProcessesCapability boundCpuAccelerateMetalCuda
fGN noiseFgn (1)FgnBackend<T>SIMD + ndrustfftvDSP FFTmetal-oxide FFT pipeline, f32 onlycuFFT + Philox, f32 / f64
SheetFbs (1)SheetBackend<T>SIMD + ndrustfftsame as Cpumetal-oxide sheet pipeline, f32 onlycuFFT + Philox, f32 / f64
Euler engine, one component89 processesEulerBackend<T>the process's own SIMD samplersame as CpuEuler–Maruyama kernel compiled by metal-oxide, f32 onlyNVRTC kernel, f32 / f64
Euler engine, systems39 processesEulerBackend<T>the process's own SIMD samplersame as Cputhe same kernel, up to four componentsthe same kernel
Euler engine, fractionalFbm, Fou, Fcir, Fgbm, Fjacobi, Fvasicek, Cfgns, Cfbms, Cfou, JumpFou, JumpFouCustom (11, counted above)EulerBackend<T>the process's own SIMD samplersame as CpufGN pipeline feeds the same Metal kernelfGN pipeline feeds the same NVRTC kernel

Metal needs Apple Silicon with macOS 15 or newer, since its kernels ship as a metallib built for macOS 15; on an earlier macOS the device answers DeviceError::Unavailable, and Cpu runs every process there.

Every process reaches a device: Fgn and Fbs through pipelines of their own, the other 128 through the Euler engine. The engine covers the diffusions; the stochastic-volatility models under both their Euler and quadratic-exponential schemes; the short-rate models, including the curve-driven ones; the fractional processes; the jump models (a process generic over its jump-size law when that law is SimdNormal or SimdExp, and Bates under its LogNormalJump too); the Lévy processes and subordinators; the order-(1,1) conditional-variance and first-order lag models; the noise generators; the Riemann–Liouville and Volterra models under the Markov lift of their kernel; the rough-volatility models with a memory integral, Lfsm, Arima and Sarima on the host's exact scheme for grids up to 512 points; the point processes in count mode; and the matrix and basket models within the limits below.

What stays on the host

The engine steps a bounded forward recursion over four scalar state slots and four shocks. These configurations exceed it and sample on the host, bit-identically to the Cpu build, whatever the backend; device_fallback() names the reason:

  • History models on a long grid. Fheston, FbatesSvj, RoughBergomi, Lfsm, Arima, Sarima and Volterra's reference-convolution branch read their whole past each step, which the kernels carry for grids up to 512 points.
  • Coefficients of the state written as a Rust closure, a Python callable or a Grid2D (Cheyette's sigma(t, x), VolterraSde's drift and diffusion, HestonSlv's leverage). Written as an Expr, the same coefficient runs on the device — see below. A coefficient of time alone, such as Hull–White's theta(t), is tabulated on the grid and runs on the device, whether it is a Rust function or a Python callable.
  • Too many coupled dimensions. Wishart above d = 2 or below degree 3; Lmm, MultiGbm and Mcgns above four components; MultifactorHeston with three or more factors; RegimeSwitchingDiffusion with five or more regimes; MultivariateHawkes above two components or with per-pair decays.
  • Point processes on their own clock. Hawkes, MultivariateHawkes, Poisson, CompoundPoisson, CustomJt and CompoundCustom in horizon mode, where the output length is the number of events; CustomJt, CompoundCustom and JumpFouCustom with an inter-arrival law other than the exponential; Ctrw with gamma, inverse-Gaussian or stable waits.
  • Jump-size laws other than SimdNormal / SimdExp for the processes generic over one (Merton, Kou, LevyDiffusion, Bates, JumpFou, JumpFouCustom); Bates also draws its LogNormalJump on a device.
  • Dense jump counts. A process whose kernel reads the step's Poisson count, past λ·dt = 25 expected events a step, where the kernels' 64-event count would cut the tail.
  • Lag order above one for Ar, Ma and the GARCH family.
  • Svcgmy with 4κη/ζ² < 1, where the exact variance step needs a Poisson mixture.
  • InverseAlphaStableSubordinator with more than 512 u_steps.
  • Fbs when m − 1 or n − 1 is not a power of two.

Coefficients as expressions

Cheyette, VolterraSde and HestonSlv take their coefficients as Fn2D callables of time and state. A Rust closure or a Python callable of the state runs on the host only (a closure of time alone is tabulated on the grid and reaches the device). Written as an Expr, the same coefficient reaches the device:

use stochastic_rs::traits::Expr;

// σ(t, x) = 0.01 + 0.4 |x|, a displaced local volatility
let sigma = (Expr::lit(0.01) + Expr::x().abs() * 0.4).compile()?;
let cheyette = Cheyette::<f32, _>::new(f0, 0.5, sigma, 252, Some(1.0), seed).on::<Cuda>();

Expr::t() and Expr::x() are the time and the state, Expr::lit a constant; the arithmetic operators, sqrt, exp, ln, abs, tanh, powf, max and min build the tree. Expr::compile() (or Fn2D::try_from) turns it into a postfix Program of at most 62 operations on a stack of at most 8, and returns a ProgramError for a tree no kernel could run; HestonSlv::new(.., leverage.compile()?, ..) is the shape. The host evaluates the same program, so host and device compute the same coefficient, not a tabulation of it.

What the device path does

  • fGN noise. The circulant-embedding FFT runs on the device, one launch per sample_par batch.
  • Sheets. Fbs runs its two-dimensional circulant embedding as a pipeline beside the fGN one; the embedding's eigenvalue roots are computed once on the host when the process is built.
  • Fractional diffusions. Fbm, Fou, Fcir, Fgbm, Fjacobi, Fvasicek, Cfgns, Cfbms and Cfou take their increments from the fGN pipeline in device memory and run the whole recursion in the Euler kernel, so a batch crosses the bus once. On an M-series Metal device against the host: fOU with 2000 paths of 512 steps took 5.8 ms against 101 ms, fBM with 4000 paths of 1024 steps 19.2 ms against 252 ms.
  • Euler engine. The whole Euler–Maruyama recursion of a path runs in one kernel thread; sample_par(m) is one launch for all m paths. A system writes one plane per component, so a Heston draw comes back as two arrays. A square-root state steps under the process's VarianceBoundary (Lord, Koekkoek & van Dijk 2010), as on the host. The CUDA and Metal kernels run one generator and counter layout from two sources (C for CUDA, Rust for Metal), so Metal and Cuda agree seed for seed up to f32 libm rounding.
  • Host backends. Cpu is the process's own sampler; Accelerate differs from it only for the fGN family (vDSP FFT).

Large batches

A device handle holds at most batch_budget bytes of path data per launch (1 GiB by default; Cuda::from_env()? reads STOCHASTIC_RS_DEVICE_BATCH_BYTES, or set Cuda::default().with_batch_budget(n)). A larger sample_par(m) runs as several launches whose union is bit-identical to one launch, because every draw's Philox counter carries the global path index; sample_map(m, f) maps each chunk on the host before the next is launched, so the batch never has to fit in memory at once. The fGN and sheet pipelines chunk the same way, each normal's counter carrying its batch-global cell.

A handle keeps what it compiled and allocated between calls — kernels, FFT plans, device buffers, the CUDA pinned staging — and frees nothing on its own. Cuda::default().release() frees what the crate caches for that ordinal, Metal::default().release() what the calling thread caches for it (Metal's caches are per thread), and device::release_host_scratch() the host FFT workspace of the calling thread and of the rayon pool. The next launch rebuilds what it needs and draws the same paths from the same seed.

Precision and reproducibility

BackendPrecisionSame seed, same deviceBit-identical to Cpu
Cpuf32 / f64yes, thread-count independent—
Acceleratef32 / f64reproducible-effort only (vDSP reduction order can differ across core types)no
Metalf32 (an f64 process is a compile error)yesno
Cudaf32 / f64 (a failing f64 launch is reported, never replaced by the f32 kernel)yesno

The device seed is drawn from the process's own seed source, so the device follows the host's rule: two processes built from the same Deterministic seed value give the same paths, consecutive calls on one process advance the stream, and an Unseeded process draws fresh entropy every call. Host and device paths agree in distribution, not bit for bit, because the device draws its own stream.

Probing a device, handling its failures and choosing an ordinal are on Sampling backends.

From Python

89 process classes take device= (PyGbm(..., dtype="f32", device="metal")); the ones without it, listed on Python bindings, sample on the host. The name is one of "cpu", "accelerate", "cuda", "metal", optionally with an ordinal ("cuda:1"); one the build does not carry raises ValueError with a rebuild hint, and the device is probed when the object is built. Arrays come back in the class's dtype, and the f32-only Metal backend needs dtype="f32". A class also answers device_ready() and device_fallback() for the configuration it holds. The published wheels are CPU-only; the device backends need a source build — maturin develop --features metal on a Mac, --features cuda on an NVIDIA machine. See Python bindings.

Trying the CUDA back-end without a CUDA machine

notebooks/colab_cuda_check.ipynb opens in Google Colab: on a T4 runtime it installs Rust, clones the repository, runs the native CUDA tests (the fGN pipeline and the Euler engine), builds the Python wheel with device="cuda" and plots what the GPU produced. The Rust tests build in 10–20 minutes on Colab's two cores, the wheel in another ten.

A run on a T4

notebooks/runs/colab_cuda_check_T4_2026-09-03.ipynb is the executed copy of a run on 2026-09-03 (Tesla T4, driver 580.82 / CUDA 13.0, toolkit 12.8; GitHub's viewer may not render Colab outputs, nbviewer does). The native CUDA tests passed, including a chunked batch equal to one launch path for path in f32 and f64, and so did the wheel's device tests.

fBM paths, the t^(2H) variance law on GPU and CPU, the GBM terminal law against the lognormal density, and wall time per call CPU vs CUDA on a T4

The variance of fractional Brownian motion sampled on the GPU sits on the t^{2H} lines for H = 0.3, 0.5, 0.8 where the CPU samples sit, and the Euler engine's GBM terminal mean over 100k paths was 105.15 against the 105.13 forward. fGN wall time per call, warm, against Colab's two CPU cores:

CaseCPUCuda (T4)
fGN, n = 4096, m = 2000, f64≈ 0.55 s≈ 0.22 s (2.5×)
fGN, n = 16384, m = 1000, f64≈ 0.65 s≈ 0.36 s (1.8×)

The fGN pipeline is FFT-bound and already wins at these sizes; the gap widens with m. For the Euler engine on a T4, dtype="f32" is the setting to use: the card runs double precision at 1/32 of its single-precision rate. Current Euler-engine numbers on the T4 are on Benchmarks.

Stability

What is public and stable is the capability a bound names (EulerBackend<T> and the others), .on::<B>(), with_backend(handle), device_ready(), device_fallback(), probe(), release(), release_host_scratch() and the try_* calls. The engine's launch hooks, family specifications and slot limits are hidden from the rendered documentation and outside the stability promise.

Edit on GitHub

Last updated on

On this page