Skip to content

Choosing CPU or GPU

ruxla gives an environment two homes: native code on CPU threads, and a primitive inside a JAX program on whatever device JAX uses. Which one is faster depends mostly on how many environments you step at a time and on what the environment computes. This page turns the measurements into rules of thumb.

The short version

Situation Use
One or a few environments at a time (tree search, evaluation, debugging, MPC) native compiled code on the CPU
Hundreds to a few thousand environments, rule-based (games, grid worlds, Craftax) native compiled code on 16+ CPU cores
Tens of thousands of environments, the whole loop already on a GPU the environment inside JAX on the GPU
Rigid-body physics (MJX) in large batches JAX on the GPU
No Python allowed at run time (deployment, embedded) native compiled code

Why small batches favor the CPU

An environment step is a small program with many branches. XLA compiles it well for large batches, but per step it still pays for computing both sides of selections, for kernel launches, and, on a GPU, for synchronization. With one environment, JAX on an RTX 5090 runs 69 of the 71 benchmark environments slower than ruxla's native code on a single CPU core, by a median factor of 470.

Where the GPU catches up

Throughput on the GPU grows almost linearly with the batch, so it eventually overtakes a CPU. In our measurements (Ryzen 9 9950X and RTX 5090), the GPU needs thousands of environments to approach 16 CPU cores. Run toward its memory limit (about a million environments), it exceeds the CPU's best configuration on 58 of the 71 environments, from a median batch of 16,384, and on the other 13 the CPU stays ahead at every batch tested. For large batches of physics the GPU wins clearly, by 9 to 15 times over 16 cores.

The environments where the CPU keeps its lead are the ones with rare but expensive branches (a reset that generates a level, a scoring routine that runs at the end of a game) and gather-heavy puzzles, because native code executes the branch only when it is taken.

Combining both

Two arrangements combine the strengths:

  • Native environments, GPU agent. Step imported environments with ruxla's vectorized environment on CPU threads (Native execution and VecEnv, or Python VecEnv from a Python training loop) and run the policy and the update on the GPU. This is the arrangement of actor--learner systems; the cost is copying observations to the GPU each step.
  • Everything inside JAX. Export the environment written with ruxla into JAX (ruxla environments in JAX). It runs at the speed of JAX code on any device, can be batched with vmap and differentiated with grad, and if it uses faster algorithms than the original JAX environment, that advantage carries over.

Measuring your own environment

On the CPU, the example run of ruxla-env-jax reports the steps per second of an imported environment on all cores:

cargo run --release -p ruxla-env-jax --example run -- <module> <num_envs> <steps>

where <module> is the directory name under .cache/jax-envs/ written by python -m ruxla.export. Compare it with the original environment under jax.jit(jax.vmap(...)) on your GPU at the batch sizes you plan to train with.