CuPy backend

CuPy backend#

This example samples the Himmelblau target described in the array backends overview.

Note

This example requires an NVIDIA GPU with a working CUDA driver and a compatible CuPy installation. It cannot run on CPU; use the NumPy example for CPU sampling.

In run_cupy.py, the prior uses cupy operations and orng draws the initial particles on CUDA:

rng = RandomGenerator(backend="cupy", seed=seed)
particles = rng.uniform(
    lower_bound, upper_bound, size=(n_particles, n_dimensions)
)
initial_state = ParticleState(particles=particles)

The sampler configuration and eager run call follow the NumPy example. Keep model calculations on the CuPy arrays; copying particles to the host inside a callback would introduce device transfers during sampling.

Running the example#

Run from examples/array_backends in a Python environment containing Sequince, its plotting extras, and CuPy compatible with the installed CUDA runtime:

python run_cupy.py

The default output directory is outdir/cupy.

Edit n_particles, seed, and outdir at the top of the script. See the overview for shared settings and guidance on interpreting the plots.