ruxla¶
Write RL environments and models once in Rust: run them as native code, or export them to JAX and XLA.
ruxla is a Rust library for writing differentiable array programs — reinforcement learning environments in particular — in the style of JAX. You write the program once, as Rust code that is generic over a backend:
- with
B = Native,rustccompiles it to ordinary native code that runs without XLA or Python, with real branches and loops, and in parallel on CPU threads; - with
B = Trace, running the same code records an IR (the equivalent of a jaxpr) that ruxla can transform (vmap,grad), run with XLA through PJRT, or hand to JAX, where the program works insidejax.jit,jax.vmap,jax.lax.scan,pmap, andshard_map; - the IR can also be compiled ahead of time back to Rust (fixed-size arrays, fused loops), which makes an environment step about as fast as handwritten Rust.
ruxla is meant to be used alongside JAX, not instead of it: the learning ecosystem (Flax, Optax, distributed training) stays in JAX.
From Python, the same environments also feed agents written in PyTorch or any other framework:
ruxla.VecEnv steps them on CPU threads and returns NumPy arrays, which an agent on the GPU reads
without JAX (see Using ruxla from PyTorch).
It also goes the other way: environments written in JAX (gymnax, Pgx, Jumanji, and others through a small wrapper) can be imported from their jaxpr into ruxla's IR and compiled ahead of time to native code, without rewriting them, and checked against JAX's own trajectories.
Status
ruxla is under active development (0.x): the API can still change. Install it with
pip install ruxla or cargo add ruxla (see Installation).
Key ideas¶
- One source, several backends
- A program is generic Rust code over
B: Backend.Nativeruns it eagerly as compiled Rust;Traceturns it into IR. Values are typed (F32<B>,I32<B>,Array<B, f32>), so code that would work natively but cannot become IR (anifon a traced value) does not compile. - JAX's names and semantics
jnp,lax, andrandomfollow JAX: the same function names, the same primitives, the same threefry random numbers. Traced programs have the shape of JAX's jaxprs, so environments exported to JAX compile to the same kind of HLO as environments written in JAX.- The IR as the hub
- Transformations (
vmap,jvp,vjp,grad) are IR-to-IR. The IR runs on an interpreter, on XLA (Rust, CPU and CUDA PJRT plugins), inside JAX (Python, through the primitiveruxla_call), or is turned back into Rust by the code generator. - gymnax-compatible environments
- The
Environmenttrait follows gymnax 1.0. ruxla ships five reference environments (CartPole, Acrobot, Pendulum, MinAtar Breakout, and Pgx's tic-tac-toe) that reproduce the originals step for step, a parallelVecEnv, a NumPyVecEnvfor Python, and a Gymnasium wrapper.
A taste¶
use ruxla::prelude::*;
use ruxla::env::{CartPole, Environment, VecEnv};
// Natively: an ordinary Rust call (gymnax's reset and step).
let env = CartPole;
let params = env.default_params::<Native>();
let (obs, state) = env.reset(random::key(0), ¶ms);
let step = env.step(random::key(1), &state, I32::new(1), ¶ms);
// Many environments in parallel on CPU threads.
let mut envs = VecEnv::new(CartPole, 8, 0);
let step = envs.step(&[1; 8]);
// As IR: a package that JAX can load.
ruxla::env::export(&CartPole, "cartpole.ruxlapkg")?;
Where to go next¶
- Installation and the Quickstart
- Concepts: backends, values, control flow, random keys, pytrees
- Writing environments and ahead-of-time compilation
- ruxla environments in JAX and importing JAX environments
- The environments and the benchmarks
- API reference: Python, Rust
- The developer guide, for contributors
ruxla is licensed under the Apache License 2.0; see NOTICE for the parts derived from JAX and XLA.