Skip to main content

Crate ruxla_env

Crate ruxla_env 

Source
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), &params);
let step = env.step(random::key(1), &state, I32::new(1), &params);
assert_eq!(obs.shape().to_vec(), [4]);
assert_eq!(step.reward.value(), 1.0);

Modules§

compiled
Environments compiled ahead of time to Rust: generate traces an environment’s reset and step (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, and rustc compiles it like handwritten code. The module implements CompiledEnv.
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.export writes.
testing
Tools for testing environments: agreement of the native backend, the interpreter, and XLA, and of vmap with native loops.

Macros§

export_vec_envs
Exports the environments of the code of include_envs! (MODULES and visit in scope) from a cdylib through the C ABI of this module.
flat_obs
Implements FlatObs for an observation struct of several fields (for Native): the fields, in the order given, flattened one after another, with the shape [total size].
include_envs
Includes the environments that ruxla-build compiled in the build script ($OUT_DIR/ruxla_envs.rs): a module per environment, MODULES, DATA_DIR, and visit.

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.
AcrobotParams
The parameters of Acrobot, in the order of gymnax’s EnvParams leaves. Defaults follow Acrobot-v1.
AcrobotState
The state of an Acrobot episode. The fields are in the order of gymnax’s EnvState leaves.
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 is f32[10, 10, 4] with the channels paddle, ball, trail, and bricks (gymnax converts its boolean grid to f32).
BreakoutParams
The parameters of Breakout.
BreakoutState
The state of a Breakout episode. The fields are in the order of gymnax’s EnvState leaves.
CartPole
gymnax’s CartPole (CartPole-v1): balance a pole on a cart by pushing it left (action 0) or right (action 1).
CartPoleParams
The parameters of CartPole. Defaults follow CartPole-v1.
CartPoleState
The state of a CartPole episode. The fields are in the order of gymnax’s EnvState leaves.
CompiledVecEnv
A batch of environments compiled ahead of time (CompiledEnv), stepped in parallel and reset automatically, like VecEnv (the same keys, so the same trajectories as a VecEnv of the environment).
PackageWriter
Writes a .ruxlapkg package of any functions: each is traced and written as IR, and finish writes the manifest. The Python package ruxla loads it with ruxla.load and calls the functions inside JAX programs through the primitive ruxla_call. export uses 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).
PendulumParams
The parameters of Pendulum, in the order of gymnax’s EnvParams leaves. Defaults follow Pendulum-v1.
PendulumState
The state of a Pendulum episode. The fields are in the order of gymnax’s EnvState leaves.
Step
The result of Environment::step, with automatic reset (gymnax’s (obs, state, reward, terminated, truncated, info)).
StepBuffers
Buffers that VecEnv::step_into writes the results of a step to, one entry per environment; obs and final_obs hold the flat observations (FlatObs) environment after environment.
TicTacToe
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). Action i places a mark on cell i (row-major). The observation is bool[3, 3, 2]: the current player’s marks and the opponent’s. The first player is random.
TicTacToeState
The state of a game of tic-tac-toe: Pgx’s State without the observation and the rewards (which step returns), 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§

ActionSpace
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_len floats per environment.
ExportError
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§

ActionValue
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 f32 buffer, for VecEnv::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 .ruxlapkg package (a directory) for the Python package ruxla, which loads it with ruxla.load_env.
pattern_continuous
Element k of the ith of the pseudo-random sequence of continuous actions between low and high.
pattern_discrete
The ith of the pseudo-random sequence of discrete actions in 0..n.
unravel_joint
The components of joint action index over nvec (row-major).

Type Aliases§

StepInput
The arguments of Environment::step as one pytree: (key, state, action, params).
StepOf
The Step of an environment.