Compatibility and limits¶
ruxla's IR follows JAX's semantics and carries JAX's transformation rules, but not every JAX program can be imported, and not every transformation applies to every program. This page lists what is supported and what is not.
Transformations¶
| Program | vmap |
forward (JVP) | reverse (VJP, grad) |
|---|---|---|---|
written with ruxla (including custom_jvp, custom_vjp, checkpoint) |
yes | yes | yes |
while_loop with a data-dependent trip count |
yes | yes | no |
| imported from JAX | yes | not preserved | not preserved |
Imported programs are for primal execution and batching. When a JAX program is exported for ruxla
(ruxla.export_env, ruxla.export_jaxpr), the exporter inlines calls and drops what only matters for
differentiation:
custom_jvp_callandcustom_vjp_callare inlined and their custom rules are dropped;stop_gradientbecomes a plain copy;custom_linear_solveis expanded into its solver, instead of JAX's implicit differentiation.
The primal results are the same as JAX's, and the imported program can be compiled, interpreted,
batched with vmap, or lowered to XLA. Differentiating it would give derivatives that can differ from
JAX's wherever the original relied on these constructs, so differentiate the original JAX program, or an
environment written with ruxla, instead.
Other rules:
while_loopcannot be differentiated in reverse mode, as in JAX. Usescan, whose length is known, when you need gradients.- Exported environments written with ruxla (
ruxla_call) get batching and derivative rules from ruxla's own transformations, sojit,vmap,gradand their combinations apply inside JAX (ruxla environments in JAX).
Importing JAX programs¶
- Shapes are static. Every array has a shape known when the program is exported; dynamic shapes are not supported.
- Primitives. ruxla implements 128 primitives, close to JAX's
laxset: elementwise and special functions, indexing, reductions, sorting, windows and convolutions, linear algebra, FFT, random numbers, and structured control flow. A program that uses a primitive outside this set cannot be imported. - JAX version. Export with JAX 0.12. Environments written for older versions of JAX may need small fixes where JAX 0.12 rejects deprecated calls.
- Random numbers. JAX's default Threefry generator is supported, bit for bit; other implementations
(
rbg) are not. - Data types. f32, f64, the integer types and booleans. Complex numbers are supported by the primitives but not by the code generator.
Numerical agreement¶
Imported environments agree with JAX exactly for integers and booleans. Floats can differ in the last bits, because XLA fuses multiply-adds and replaces divisions by constants with multiplications by reciprocals; the agreement tests use a relative tolerance of 10⁻⁵ for most environments. In contact physics these differences grow along a trajectory, as they do between JAX's own compiled and operation-by-operation execution, so physics environments are best compared step by step.
Writing environments for JAX¶
An environment exported into JAX runs under JAX's rules:
- under
vmap, XLA computes both branches of a conditional and keeps running a loop while any environment in the batch needs it, so loops must terminate for every state, including the states of finished episodes; - very large programs can make XLA's compilation slow or memory-hungry (we have seen a large board game make XLA's compiler use more than 50 GB of memory and fail, while it compiled with rustc in seconds).
Generated code¶
- Arrays up to 256 KB live on the stack; larger ones in per-thread pools on the heap. The vectorized environment's worker threads have 32 MiB stacks; threads of your own that step large environments need comparable stacks (FAQ).
- Ahead-of-time compilation takes about a second for small environments and tens of seconds or more for large ones (Benchmarks).