<p align="center">
<img src="https://raw.githubusercontent.com/ymgaq/ruxla/main/docs/assets/ruxla-banner.png" alt="ruxla" width="640">
</p>
<p align="center">
<a href="https://ymgaq.github.io/ruxla/"><img src="https://raw.githubusercontent.com/ymgaq/ruxla/main/docs/assets/button-project.png" alt="Project page" height="30"></a>
<a href="https://ymgaq.github.io/ruxla/paper/ruxla.pdf"><img src="https://raw.githubusercontent.com/ymgaq/ruxla/main/docs/assets/button-paper.png" alt="Paper" height="30"></a>
<a href="https://ymgaq.github.io/ruxla/docs/"><img src="https://raw.githubusercontent.com/ymgaq/ruxla/main/docs/assets/button-docs.png" alt="Docs" height="30"></a>
</p>
<p align="center">
<a href="https://pypi.org/project/ruxla/"><img src="https://img.shields.io/badge/python-3.10%2B-3776AB?logo=python&logoColor=white" alt="Python 3.10+"></a>
<a href="https://pypi.org/project/ruxla/"><img src="https://img.shields.io/pypi/v/ruxla?logo=pypi&logoColor=white&label=PyPI" alt="PyPI"></a>
<a href="https://crates.io/crates/ruxla"><img src="https://img.shields.io/crates/v/ruxla?logo=rust&label=crates.io" alt="crates.io"></a>
<a href="https://docs.rs/ruxla"><img src="https://img.shields.io/docsrs/ruxla?logo=docsdotrs&label=docs.rs" alt="docs.rs"></a>
<a href="https://github.com/ymgaq/ruxla/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-Apache%202.0-blue" alt="License: Apache 2.0"></a>
<a href="https://github.com/ymgaq/ruxla/actions/workflows/ci.yml"><img src="https://github.com/ymgaq/ruxla/actions/workflows/ci.yml/badge.svg?branch=main" alt="CI"></a>
</p>
ruxla runs reinforcement learning environments at native speed and keeps them compatible with [JAX](https://github.com/jax-ml/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.
<p align="center">
<img src="https://raw.githubusercontent.com/ymgaq/ruxla/main/docs/assets/ruxla-overview.png" alt="Two workflows through one IR: JAX environments compiled to native code, and Rust environments exported into JAX" width="800">
</p>
## Performance
On 71 [gymnax](https://github.com/RobertTLange/gymnax), [Pgx](https://github.com/sotetsuk/pgx), and [Jumanji](https://github.com/instadeepai/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](site/docs/benchmarks.md) and the [paper](https://ymgaq.github.io/ruxla/paper/ruxla.pdf).
<p align="center">
<img src="https://raw.githubusercontent.com/ymgaq/ruxla/main/docs/assets/ruxla-speed.png" alt="Environment steps per second" width="800"><br>
<em>Environment steps per second (geometric mean over 71 gymnax, Pgx, and Jumanji environments): ruxla on the CPU, JAX on the CPU, and JAX on an RTX 5090.</em>
</p>
<table align="center">
<tr>
<td align="center"><img src="https://raw.githubusercontent.com/ymgaq/ruxla/main/docs/assets/ruxla-go2-backflip.gif" alt="Go2 backflip (MJX)" width="230"><br><sub>Go2 backflip (MJX)</sub></td>
<td align="center"><img src="https://raw.githubusercontent.com/ymgaq/ruxla/main/docs/assets/ruxla-waymax.gif" alt="Waymax" width="230"><br><sub>Waymax</sub></td>
<td align="center"><img src="https://raw.githubusercontent.com/ymgaq/ruxla/main/docs/assets/ruxla-craftax.gif" alt="Craftax" width="230"><br><sub>Craftax</sub></td>
</tr>
</table>
<p align="center"><em>Environments imported from their JAX implementations and run as native code: a Unitree Go2 backflip with a trained policy (MJX), Waymax, and Craftax.</em></p>
## 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`, and `grad`.
- **Works with PyTorch.** `ruxla.VecEnv` steps 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](https://github.com/MichaelTMatthews/Craftax), [Brax](https://github.com/google/brax), [MuJoCo
Playground](https://github.com/google-deepmind/mujoco_playground), [NAVIX](https://github.com/epignatelli/navix), [XLand-MiniGrid](https://github.com/dunnolab/xland-minigrid), [JaxMARL](https://github.com/FLAIROx/JaxMARL), and [Waymax](https://github.com/waymo-research/waymax) compile to Rust and match JAX's results.
## Installation
```bash
pip install ruxla # Python: ruxla.VecEnv with NumPy; "ruxla[jax]" adds the JAX parts (JAX 0.12)
cargo add ruxla # Rust
cargo add --build ruxla-build # Rust: compile environments exported from JAX in build.rs
```
Packages: [PyPI](https://pypi.org/project/ruxla/) · [crates.io](https://crates.io/crates/ruxla) ·
[API reference (docs.rs)](https://docs.rs/ruxla). `ruxla.compile_env` and `ruxla-build` also need a
Rust toolchain (`cargo`).
Or build from a clone, the Python package with [maturin](https://www.maturin.rs/):
```bash
git clone https://github.com/ymgaq/ruxla && cd ruxla
pip install "jax==0.12.*" maturin
cd python && maturin develop --release
```
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](site/docs/getting-started/installation.md#platforms).
## Quick examples
### 1. Write an environment in Rust and step it natively
```rust
use ruxla::env::{ActionSpace, Environment, Transition, VecEnv};
use ruxla::prelude::*;
/// A walk on a line: each step moves left or right; the episode ends at either end.
#[derive(Clone, Debug, Tree)]
pub struct WalkState<B: Backend> {
pub x: F32<B>,
pub time: I32<B>,
}
#[derive(Clone, Copy, Debug, Default)]
pub struct Walk;
impl Environment for Walk {
type State<B: Backend> = WalkState<B>;
type Params<B: Backend> = ();
type Obs<B: Backend> = F32<B>;
type Action<B: Backend> = I32<B>;
fn name(&self) -> &str {
"Walk"
}
fn action_space(&self) -> ActionSpace {
ActionSpace::Discrete(2)
}
fn default_params<B: Backend>(&self) {}
fn reset<B: Backend>(&self, key: Key<B>, _: &()) -> (F32<B>, WalkState<B>) {
let x: F32<B> = random::uniform(&key, [], -1.0f32, 1.0f32).to_scalar();
(x, WalkState { x, time: I32::new(0) })
}
fn step_env<B: Backend>(
&self,
key: Key<B>,
s: &WalkState<B>,
action: I32<B>,
_: &(),
) -> Transition<B, WalkState<B>, F32<B>> {
let noise: F32<B> = random::normal(&key, []).to_scalar();
let x = s.x + action.cast::<f32>() * 2.0 - 1.0 + noise * 0.1;
let terminated = x.gt(5.0) | x.lt(-5.0);
let reward = lax::select(terminated, F32::new(1.0), F32::new(0.0));
let state = WalkState { x, time: s.time + 1 };
Transition { obs: x, state, reward, terminated }
}
fn is_truncated<B: Backend>(&self, s: &WalkState<B>, _: &()) -> Bool<B> {
s.time.ge(100)
}
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Natively: 1,024 walks stepped in parallel on all cores, with automatic resets.
let mut envs = VecEnv::new(Walk, 1024, 0);
let step = envs.step(&vec![1; 1024]);
println!("reward of the first walk: {}", step.reward[0]);
// As IR: a package for JAX.
ruxla::env::export(&Walk, "walk.ruxlapkg")?;
Ok(())
}
```
`ruxla_env::compiled::generate` compiles the same environment ahead of time to Rust for the fastest
native code ([guide](site/docs/guide/aot.md)); `crates/ruxla-env-aot` is a template.
### 2. Use it in JAX with `jit`, `vmap`, and `grad`
```python
import jax
import jax.numpy as jnp
import ruxla
env = ruxla.load_env("walk.ruxlapkg") # gymnax's API
keys = jax.random.split(jax.random.key(0), 4096)
obs, states = jax.vmap(env.reset)(keys)
step = jax.jit(jax.vmap(env.step))
obs, states, reward, terminated, truncated, info = step(keys, states, jnp.ones(4096, jnp.int32))
# Derivatives flow through the environment (here Pendulum, a reference environment
# exported with `cargo run -p ruxla-env --example export_env -- pendulum pendulum.ruxlapkg`).
pendulum = ruxla.load_env("pendulum.ruxlapkg")
key = jax.random.key(1)
_, state = pendulum.reset(key)
d_reward = jax.grad(lambda a: pendulum.step(key, state, a)[2])(jnp.array([0.5]))
```
Inside JAX the environment is the primitive `ruxla_call`, which XLA compiles together with the rest of
the program ([guide](site/docs/guide/jax-interop.md)).
### 3. Import an existing JAX environment and run it natively
```python
import numpy as np
import ruxla
# Export gymnax's CartPole (its jaxprs), compile it to native code with cargo, and load it.
compiled = ruxla.compile_env("gymnax:CartPole-v1")
envs = compiled.vec_env("gymnax_cartpole_v1", num_envs=4096, seed=0) # NumPy, CPU threads
obs = envs.reset()
obs, reward, terminated, truncated, info = envs.step(np.zeros(4096, np.int32))
```
`compile_env` needs a Rust toolchain (`cargo`). In a Rust program, `ruxla-build` compiles the exported
environments from the build script:
```bash
python -m ruxla.export gymnax:CartPole-v1 --out envs # exported jaxprs in envs/gymnax_cartpole_v1
```
```rust
// build.rs (with ruxla-build as a build dependency)
fn main() { ruxla_build::compile_envs("envs"); }
// src/main.rs
ruxla_env::include_envs!();
use ruxla_env::compiled::{CompiledEnv, key, split_key};
fn main() {
let env = gymnax_cartpole_v1::Env;
let (_obs, mut state) = env.reset(key(0));
let mut k = key(1);
for t in 0..1000 {
let (next, sub) = split_key(k);
k = next;
state = env.step(sub, &state, t % 2).state;
}
}
```
See the [guide](site/docs/guide/import-jax-envs.md) 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:
```python
import torch, ruxla
envs = ruxla.VecEnv("Breakout-MinAtar", num_envs=64, seed=0) # CPU threads, NumPy arrays
obs = torch.from_numpy(envs.reset()).cuda()
for t in range(1000):
action = policy(obs) # your PyTorch agent on the GPU
obs, reward, terminated, truncated, info = envs.step(action.int().cpu().numpy())
obs = torch.from_numpy(obs).cuda()
```
`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`](python/examples/ppo_torch.py) is a complete CleanRL-style
PPO, with an optional `torch.compile` agent; see [Using ruxla from PyTorch](site/docs/guide/pytorch.md).
## Documentation
- [Getting started](site/docs/getting-started/quickstart.md) and the [guides](site/docs/index.md)
(source of the documentation site, built with MkDocs from `site/`)
- [Reference environments and importable libraries](site/docs/environments.md)
- [Developer guide](site/docs/development.md) and [contributing](CONTRIBUTING.md)
## Citation
```bibtex
@misc{yamaguchi2026ruxla,
title = {ruxla: Native-Speed {JAX} Environments for Reinforcement Learning at Small Batch Sizes},
author = {Yamaguchi, Yu},
year = {2026},
url = {https://ymgaq.github.io/ruxla/paper/ruxla.pdf},
note = {Preprint}
}
```
## License
[Apache License 2.0](LICENSE). See [NOTICE](NOTICE) for the parts derived from JAX and XLA.