ruxla runs reinforcement learning environments at native speed and keeps them compatible with JAX.
Write an environment once in Rust, in the style of JAX, and run it as native code or export it into
JAX, where it works under jit, vmap, and grad. Or take an environment already written in JAX,
import it from its jaxpr, and compile it ahead of time to Rust, without rewriting it. ruxla is meant to
be used together with JAX, not to replace it.
Performance
On 71 gymnax, Pgx, and Jumanji environments (Ryzen 9 9950X), imported environments run 11.2× faster than JAX with one environment and 6.5× faster with 4,096; against a stronger JAX baseline, 3.1× and 2.1×. With a compiled PyTorch PPO agent on the GPU and 8 or 64 environments, they raise training throughput over the best JAX backend by 1.4–5.8× on eight of nine tasks. See the benchmarks and the paper.
Why
- Fast at small batches on CPUs. One to a few thousand environments on CPU cores (search, evaluation, actors) run as plain Rust close to handwritten speed, with no Python or XLA at run time.
- One implementation for both worlds. The same Rust code runs natively and inside JAX, under
jit,vmap, andgrad. - Works with PyTorch.
ruxla.VecEnvsteps the environments on CPU threads and hands NumPy arrays to an agent on the GPU; with a PyTorch PPO agent, Craftax reaches its target 3× sooner than with JAX stepping it on the GPU. - No rewrite of JAX environments. gymnax, Pgx, Jumanji, and on request Craftax, Brax, MuJoCo Playground, NAVIX, XLand-MiniGrid, JaxMARL, and Waymax compile to Rust and match JAX's results.
Installation
Packages: PyPI · crates.io ·
API reference (docs.rs). ruxla.compile_env and ruxla-build also need a
Rust toolchain (cargo).
Or build from a clone, the Python package with maturin:
&&
&&
Wheels are built for Linux (x86-64 and AArch64) and macOS on Apple silicon; Windows builds from source
and is untested. Running XLA from Rust (jit) needs a PJRT plugin: ruxla downloads the CPU plugin on
these three platforms (in a clone, scripts/fetch-pjrt-plugins.sh); everything else works without
one. See the installation guide.
Quick examples
1. Write an environment in Rust and step it natively
use ;
use *;
/// A walk on a line: each step moves left or right; the episode ends at either end.
;
ruxla_env::compiled::generate compiles the same environment ahead of time to Rust for the fastest
native code (guide); crates/ruxla-env-aot is a template.
2. Use it in JAX with jit, vmap, and grad
= # gymnax's API
=
, =
=
, , , , , =
# Derivatives flow through the environment (here Pendulum, a reference environment
# exported with `cargo run -p ruxla-env --example export_env -- pendulum pendulum.ruxlapkg`).
=
=
, =
=
Inside JAX the environment is the primitive ruxla_call, which XLA compiles together with the rest of
the program (guide).
3. Import an existing JAX environment and run it natively
# Export gymnax's CartPole (its jaxprs), compile it to native code with cargo, and load it.
=
= # NumPy, CPU threads
=
, , , , =
compile_env needs a Rust toolchain (cargo). In a Rust program, ruxla-build compiles the exported
environments from the build script:
// build.rs (with ruxla-build as a build dependency)
// src/main.rs
include_envs!;
use ;
See the guide for the other libraries (Craftax, Brax, MuJoCo Playground, NAVIX, XLand-MiniGrid, JaxMARL, Waymax).
4. Train a PyTorch agent
The environments come out as NumPy arrays, so any framework can learn from them. With PyTorch:
= # CPU threads, NumPy arrays
=
= # your PyTorch agent on the GPU
, , , , =
=
step_into writes into arrays you allocate once (pinned tensors give asynchronous copies to the GPU),
and ruxla.compile_env(...).vec_env(...) gives the same VecEnv for an imported JAX environment such
as Craftax. python/examples/ppo_torch.py is a complete CleanRL-style
PPO, with an optional torch.compile agent; see Using ruxla from PyTorch.
Documentation
- Getting started and the guides
(source of the documentation site, built with MkDocs from
site/) - Reference environments and importable libraries
- Developer guide and contributing
Citation
License
Apache License 2.0. See NOTICE for the parts derived from JAX and XLA.