FAQ¶
General¶
Does ruxla replace JAX?¶
No. ruxla is designed to be used with JAX. Training ecosystems (Flax, Optax, distributed training) stay in JAX; ruxla lets you write environments (and small models) in Rust, test and run them natively, and embed them in JAX programs, or import JAX environments into Rust.
Is ruxla released?¶
Yes, as 0.x versions: pip install ruxla for Python and cargo add ruxla for Rust (see
Installation). The API can still change between them.
Which JAX version is supported?¶
JAX 0.12.x (jax>=0.12,<0.13). The parts that use JAX's extension APIs are gathered in
python/ruxla/_primitive.py; the jaxpr rewriting in python/ruxla/_jaxpr.py also follows JAX 0.12's
primitives and parameters.
Are dynamic shapes supported?¶
No. As in JAX, shapes are fixed when a program is traced (and within one native program). Shape-changing configuration (grid sizes, numbers of agents) belongs in the environment struct, not in the parameters.
Which environments are included?¶
Five reference ports (CartPole, Acrobot, Pendulum, MinAtar Breakout, Pgx's tic-tac-toe), which serve as examples and agreement tests. Other gymnax, Pgx, and Jumanji environments are imported from JAX instead of ported. See Environments.
Results and accuracy¶
Are results bit-identical to JAX?¶
Integers, booleans, and random bits agree exactly: the threefry implementation and the semantics of integer operations follow JAX and XLA. Floats agree within a tolerance, not always bit for bit. XLA fuses multiplications and additions into FMAs, replaces divisions by constants with multiplications by reciprocals, and has its own transcendental functions; ruxla rounds each operation separately and uses libm-compatible kernels natively. For example, CartPole agrees with gymnax up to the last bits of floats natively, while inside JAX (where XLA compiles both) the trajectories match bit for bit. Integer-only environments (MinAtar, board games, mazes) agree bit for bit.
Do native and compiled environments agree?¶
Yes: the environments compiled ahead of time give bit-identical trajectories to the Native backend
(tested in crates/ruxla-env-aot/tests/agree.rs), and the interpreter shares the native kernels.
Why do physics environments only agree step by step?¶
In rigid-body physics, rounding differences grow along the trajectory, and contact solvers amplify them
in ill-conditioned contacts (JAX itself differs between jit and op-by-op execution there). Such
environments are best compared step by step from recorded states, with looser tolerances.
Exporting to JAX¶
My exported environment hangs or misbehaves under jax.vmap. Why?¶
Under jax.vmap, a lax::cond whose predicate differs between environments computes both branches
for every environment and selects, and a lax::while_loop keeps iterating until every environment's
loop has finished (the finished lanes keep their values). Natively and in the compiled code, only the
taken branch runs, so a loop that terminates for every state that can reach it may never run on other
states. Under vmap, the not-taken branch runs on states it was never meant to see (for example a broken
linked list after an illegal move), and its loop may never end.
Make such loops safe for every lane: bound their number of iterations (for example by the size of the
board) and make them not iterate when their branch is not selected. Test the exported environment under
jax.vmap against its native version, step by step.
Can I call native-only code from an exported environment?¶
No. pure_callback works natively, on the interpreter, and with ruxla's own XLA path, but programs that
use it cannot be saved to .ruxlapkg, embedded in JAX, or compiled ahead of time.
Can JAX differentiate ruxla functions?¶
Yes: jax.grad, jax.jvp, jax.vjp, jax.linearize, jax.linear_transpose, and jax.hessian work
through ruxla_call, also under jit, vmap, scan, pmap, and shard_map. A ruxla while_loop is
differentiable in forward mode only (as in JAX).
Performance¶
When should I use the CPU and when the GPU?¶
It depends mostly on the batch size. With one or a few environments, ruxla's compiled code on one CPU core is faster than JAX on an RTX 5090 for 69 of the 71 benchmark environments; the GPU needs thousands of environments to catch up with 16 CPU cores, and exceeds them on 58 of the 71 from a median batch of 16,384. Rigid-body physics in large batches belongs on the GPU. See Choosing CPU or GPU and Benchmarks.
Why is the Native backend slower than the compiled code?¶
The eager Native backend checks shapes and allocates for each array operation, which costs tens of
nanoseconds per operation; for environments made of many small operations this dominates. The
ahead-of-time compiled code works on fixed-size arrays with fused loops and real branches. Use Native
for tests, debugging, and code that should not need a build step; use AOT compilation
for speed.
How long does ahead-of-time compilation take?¶
Small environments take about a second to generate and compile; large imported environments take tens
of seconds (Craftax: about 20 s). The generated code is compiled once and cached by Cargo, whereas XLA's
jit compiles in every process (unless a persistent cache is used). Compiling generated code can take
several GB of memory per rustc process; limit the parallelism on small machines.
Why does a compiled environment overflow the stack?¶
Compiled environments keep their arrays on the stack (arrays above 256 KB go to the heap), and large
ones can exceed the default 2 MiB stack of a new thread. VecEnv and CompiledVecEnv workers use
32 MiB stacks; give your own threads a larger stack too
(std::thread::Builder::new().stack_size(..)).
Why is ruxla.VecEnv slow at small batch sizes from Python?¶
Each call allocates new NumPy arrays and releases the GIL (about 1.5 µs of fixed cost), which
dominates for a few environments. NumPy's OpenBLAS threads can also spin and take CPU time from ruxla's
workers: set OPENBLAS_NUM_THREADS=1 when measuring.
Contributing¶
See CONTRIBUTING.md for the build, test, and style rules, and the developer guide for the architecture.
License¶
ruxla is licensed under the Apache License 2.0. Parts
are derived from JAX and XLA, both Apache-2.0: the PJRT C API bindings (crates/ruxla-pjrt/src/sys.rs)
are generated from XLA's pjrt_c_api.h, and the semantics and names of the primitives follow jax.lax.
See NOTICE for details.