Metadata-Version: 2.4
Name: ruxla
Version: 0.1.0
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: MacOS
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Rust
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Typing :: Typed
Requires-Dist: numpy>=1.24
Requires-Dist: jax>=0.12,<0.13 ; extra == 'export'
Requires-Dist: safetensors ; extra == 'export'
Requires-Dist: flatbuffers ; extra == 'export'
Requires-Dist: gymnasium>=1.0 ; extra == 'gymnasium'
Requires-Dist: jax>=0.12,<0.13 ; extra == 'jax'
Provides-Extra: export
Provides-Extra: gymnasium
Provides-Extra: jax
License-File: LICENSE
License-File: NOTICE
Summary: Native-speed reinforcement learning environments between Rust and JAX
Keywords: reinforcement-learning,jax,rust,environments,xla,pytorch
License-Expression: Apache-2.0
Requires-Python: >=3.10
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Documentation, https://ymgaq.github.io/ruxla/docs/
Project-URL: Homepage, https://ymgaq.github.io/ruxla/
Project-URL: Issues, https://github.com/ymgaq/ruxla/issues
Project-URL: Repository, https://github.com/ymgaq/ruxla

# ruxla

ruxla runs reinforcement learning environments at native speed and keeps them compatible with
[JAX](https://github.com/jax-ml/jax). Environments written in Rust, or imported from their JAX
implementations (gymnax, Pgx, Jumanji, Craftax, ...) and compiled ahead of time, run as native code on
CPU threads, or inside JAX under `jit`, `vmap`, and `grad`.

- [Documentation](https://ymgaq.github.io/ruxla/docs/)
- [Project page](https://ymgaq.github.io/ruxla/)
- [Paper](https://ymgaq.github.io/ruxla/paper/ruxla.pdf)
- [Repository](https://github.com/ymgaq/ruxla) (the Rust crates, the examples, and the paper)
- [Rust crates (crates.io)](https://crates.io/crates/ruxla)

## Installation

```bash
pip install ruxla            # ruxla.VecEnv: native environments with NumPy, no JAX
pip install "ruxla[jax]"     # also ruxla functions and environments inside JAX, and the exporters
```

`ruxla.compile_env`, which compiles environments exported from JAX on the spot, also needs a Rust
toolchain (`cargo`).

Wheels are built for Linux (x86-64 and AArch64) and macOS on Apple silicon (11 or later); on other
platforms pip builds the sdist, which needs a Rust toolchain (Windows is untested).

## Native environments with NumPy

```python
import numpy as np
import ruxla

envs = ruxla.VecEnv("CartPole-v1", num_envs=256, seed=0)   # stepped on CPU threads
obs = envs.reset()
actions = np.random.randint(0, envs.num_actions, envs.num_envs)
obs, reward, terminated, truncated, info = envs.step(actions)
```

`VecEnv.step_into` writes into arrays you allocate once, such as pinned PyTorch tensors; see
[Using ruxla from PyTorch](https://ymgaq.github.io/ruxla/docs/guide/pytorch/).

## Environments imported from JAX

Exporting needs `ruxla[jax]` and the environment's library (here gymnax); compiling needs `cargo`.

```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)
obs = envs.reset()
obs, reward, terminated, truncated, info = envs.step(np.zeros(4096, np.int32))
```

## License

[Apache License 2.0](https://github.com/ymgaq/ruxla/blob/main/LICENSE); see
[NOTICE](https://github.com/ymgaq/ruxla/blob/main/NOTICE) for the parts derived from JAX and XLA.

