Expand description
Reinforcement learning environments with gymnax’s API, which run as native code and export to JAX.
An environment implements Environment with code generic over the
backend: Environment::reset and Environment::step_env are pure
functions of a key, a state, an action, and parameters, and
Environment::step adds gymnax’s automatic reset. The same code runs
natively (B = Native, also in parallel with VecEnv), with XLA
through jit, compiled ahead of time to Rust
(compiled), and inside JAX programs after exporting it for the
Python package ruxla. Environments written in JAX can be imported
(jax_env) and compiled ahead of time too.
The crate contains a few reference environments, which match the
originals bit for bit (or to rounding, for the floating-point physics)
and serve as examples: CartPole, Acrobot, and Pendulum
(gymnax’s CartPole-v1, Acrobot-v1, and Pendulum-v1), Breakout
(gymnax’s Breakout-MinAtar), and TicTacToe (Pgx’s tic_tac_toe).
§Examples
use ruxla_core::{I32, Native, random};
use ruxla_env::{CartPole, Environment};
let env = CartPole;
let params = env.default_params::<Native>();
let (obs, state) = env.reset(random::key(0), ¶ms);
let step = env.step(random::key(1), &state, I32::new(1), ¶ms);
assert_eq!(obs.shape().to_vec(), [4]);
assert_eq!(step.reward.value(), 1.0);Modules§
- compiled
- Environments compiled ahead of time to Rust:
generatetraces an environment’sresetandstep(with the automatic reset), optimizes the IR, and writes a Rust module whose functions work on fixed-size arrays; a build script includes the module, andrustccompiles it like handwritten code. The module implementsCompiledEnv. - dylib
- Compiled vector environments in shared libraries, behind a C ABI.
- jax_env
- Environments written in JAX, imported from the directories that
python -m ruxla.exportwrites. - testing
- Tools for testing environments: agreement of the native backend, the
interpreter, and XLA, and of
vmapwith native loops.
Macros§
- export_
vec_ envs - Exports the environments of the code of
include_envs!(MODULESandvisitin scope) from acdylibthrough the C ABI of this module. - flat_
obs - Implements
FlatObsfor an observation struct of several fields (forNative): the fields, in the order given, flattened one after another, with the shape[total size]. - include_
envs - Includes the environments that
ruxla-buildcompiled in the build script ($OUT_DIR/ruxla_envs.rs): a module per environment,MODULES,DATA_DIR, andvisit.
Structs§
- Acrobot
- gymnax’s Acrobot (
Acrobot-v1): swing up a two-link pendulum by applying a torque of -1 (action 0), 0 (1), or +1 (2) at the joint between the links. - Acrobot
Params - The parameters of Acrobot, in the order of gymnax’s
EnvParamsleaves. Defaults followAcrobot-v1. - Acrobot
State - The state of an Acrobot episode. The fields are in the order of gymnax’s
EnvStateleaves. - Breakout
- gymnax’s Breakout (
Breakout-MinAtar): move a paddle at the bottom of a 10x10 grid to bounce a ball into three rows of bricks. Actions: no-op (0), left (1), right (2). The observation isf32[10, 10, 4]with the channels paddle, ball, trail, and bricks (gymnax converts its boolean grid tof32). - Breakout
Params - The parameters of Breakout.
- Breakout
State - The state of a Breakout episode. The fields are in the order of gymnax’s
EnvStateleaves. - Cart
Pole - gymnax’s CartPole (
CartPole-v1): balance a pole on a cart by pushing it left (action 0) or right (action 1). - Cart
Pole Params - The parameters of CartPole. Defaults follow
CartPole-v1. - Cart
Pole State - The state of a CartPole episode. The fields are in the order of gymnax’s
EnvStateleaves. - Compiled
VecEnv - A batch of environments compiled ahead of time (
CompiledEnv), stepped in parallel and reset automatically, likeVecEnv(the same keys, so the same trajectories as aVecEnvof the environment). - Package
Writer - Writes a
.ruxlapkgpackage of any functions: each is traced and written as IR, andfinishwrites the manifest. The Python packageruxlaloads it withruxla.loadand calls the functions inside JAX programs through the primitiveruxla_call.exportuses it for environments. - Pendulum
- gymnax’s Pendulum (
Pendulum-v1): swing up a pendulum and keep it upright by applying a torque, an action of shape[1]in[-max_torque, max_torque](clipped as gymnax does). - Pendulum
Params - The parameters of Pendulum, in the order of gymnax’s
EnvParamsleaves. Defaults followPendulum-v1. - Pendulum
State - The state of a Pendulum episode. The fields are in the order of gymnax’s
EnvStateleaves. - Step
- The result of
Environment::step, with automatic reset (gymnax’s(obs, state, reward, terminated, truncated, info)). - Step
Buffers - Buffers that
VecEnv::step_intowrites the results of a step to, one entry per environment;obsandfinal_obshold the flat observations (FlatObs) environment after environment. - TicTac
Toe - Pgx’s tic-tac-toe (
tic_tac_toe): two players place marks on a 3x3 board; three in a row wins (+1, and −1 for the other player). Actioniplaces a mark on celli(row-major). The observation isbool[3, 3, 2]: the current player’s marks and the opponent’s. The first player is random. - TicTac
ToeState - The state of a game of tic-tac-toe: Pgx’s
Statewithout the observation and the rewards (whichstepreturns), in the order of its leaves. - Transition
- The result of
Environment::step_env. - VecEnv
- A batch of native environments, stepped in parallel and reset
automatically (
Environment::step). - VecStep
- The result of
VecEnv::step: one entry per environment.
Enums§
- Action
Space - The actions of an environment (gymnax’s
action_space). - Actions
- The actions of a batch of environments in a flat buffer: one integer
per environment, or
ActionSpace::flat_lenfloats per environment. - Export
Error - Errors from
export.
Constants§
- MAX_
MASK_ ACTIONS - The most joint actions that a legal action mask covers (see
ActionSpace::mask_len). - SCHEMA_
VERSION - The version of the package layout written by
export.
Traits§
- Action
Value - A type of actions (
crate::Environment::Action). - Environment
- A reinforcement learning environment with gymnax’s API.
- FlatObs
- A native observation that can be copied into a flat
f32buffer, forVecEnv::step_into(which the NumPy arrays of the Python package use).
Functions§
- choose_
legal - The joint action that tests and benchmarks take where the environment
gives a legal action mask (row-major over the joint actions): the
pattern_discrete(i, count)-th legal one (0 if none is legal), the same in every implementation. - export
- Writes an environment as a
.ruxlapkgpackage (a directory) for the Python packageruxla, which loads it withruxla.load_env. - pattern_
continuous - Element
kof theith of the pseudo-random sequence of continuous actions betweenlowandhigh. - pattern_
discrete - The
ith of the pseudo-random sequence of discrete actions in0..n. - unravel_
joint - The components of joint action
indexovernvec(row-major).
Type Aliases§
- Step
Input - The arguments of
Environment::stepas one pytree:(key, state, action, params). - StepOf
- The
Stepof an environment.