Skip to content

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, rustc compiles 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 inside jax.jit, jax.vmap, jax.lax.scan, pmap, and shard_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. Native runs it eagerly as compiled Rust; Trace turns it into IR. Values are typed (F32<B>, I32<B>, Array<B, f32>), so code that would work natively but cannot become IR (an if on a traced value) does not compile.
JAX's names and semantics
jnp, lax, and random follow 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 primitive ruxla_call), or is turned back into Rust by the code generator.
gymnax-compatible environments
The Environment trait 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 parallel VecEnv, a NumPy VecEnv for 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), &params);
let step = env.step(random::key(1), &state, I32::new(1), &params);

// 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")?;
import jax, ruxla

env = ruxla.load_env("cartpole.ruxlapkg")      # gymnax's interface
keys = jax.random.split(jax.random.key(1), 64)
obs, states = jax.vmap(env.reset)(keys)
obs, states, reward, terminated, truncated, info = jax.vmap(env.step)(
    keys, states, jax.numpy.arange(64) % 2
)
import numpy as np, ruxla

envs = ruxla.VecEnv("CartPole-v1", num_envs=256, seed=0)   # no JAX needed
obs = envs.reset()
actions = np.random.randint(0, envs.num_actions, envs.num_envs)
obs, reward, terminated, truncated, info = envs.step(actions)

Where to go next

ruxla is licensed under the Apache License 2.0; see NOTICE for the parts derived from JAX and XLA.