-
Environment vectorization & acceleration: Easy composition of JAX primitives (e.g. jit, vmap, pmap):
# Jit-accelerated step transition
jit_step = jax.jit(env.step)
# map (vmap/pmap) across random keys for batch rollouts
reset_key = jax.vmap(env.reset, in_axes=(0, None))
step_key = jax.vmap(env.step, in_axes=(0, 0, 0, None))
# map (vmap/pmap) across env parameters (e.g. for meta-learning)
reset_params = jax.vmap(env.reset, in_axes=(None, 0))
step_params = jax.vmap(env.step, in_axes=(None, 0, 0, 0))
For speed comparisons with standard vectorized NumPy environments check out gymnax-blines.
-
Scan through entire episode rollouts: You can also lax.scan through entire reset, step episode loops for fast compilation:
def rollout(key_input, policy_params, env_params, steps_in_episode):
"""Rollout a jitted gymnax episode with lax.scan."""
# Reset the environment
key_reset, key_episode = jax.random.split(key_input)
obs, state = env.reset(key_reset, env_params)
def policy_step(state_input, tmp):
"""lax.scan compatible step transition in jax env."""
obs, state, policy_params, key = state_input
key, key_step, key_net = jax.random.split(key, 3)
action = model.apply(policy_params, obs)
next_obs, next_state, reward, terminated, truncated, _ = env.step(
key_step, state, action, env_params
)
done = terminated | truncated
carry = [next_obs, next_state, policy_params, key]
return carry, [obs, action, reward, next_obs, done]
# Scan over episode step loop
_, scan_out = jax.lax.scan(
policy_step,
[obs, state, policy_params, key_episode],
(),
steps_in_episode
)
# Return masked sum of rewards accumulated by agent in episode
obs, action, reward, next_obs, done = scan_out
return obs, action, reward, next_obs, done
-
Built-in visualization tools: Generate GIF animations with the Visualizer
tool. Install the optional classic-control renderer first with
pip install "gymnax[visualize]"; GIF export uses Pillow and does not require
ffmpeg. The visualizer supports the Gymnasium classic-control environments and
selected native MinAtar and misc environments:
from gymnax.visualize import Visualizer
state_seq, reward_seq = [], []
key, key_reset = jax.random.split(key)
obs, env_state = env.reset(key_reset, env_params)
while True:
state_seq.append(env_state)
key, key_act, key_step = jax.random.split(key, 3)
action = env.action_space(env_params).sample(key_act)
next_obs, next_env_state, reward, terminated, truncated, info = env.step(
key_step, env_state, action, env_params
)
done = terminated | truncated
reward_seq.append(reward)
if done:
break
else:
obs = next_obs
env_state = next_env_state
cum_rewards = jnp.cumsum(jnp.array(reward_seq))
vis = Visualizer(env, env_params, state_seq, cum_rewards)
vis.animate(f"docs/anim.gif")
Wrap a discrete-action environment with opt-in sticky actions when an
experiment needs action persistence:
from gymnax.wrappers import StickyActionWrapper
env = StickyActionWrapper(env, sticky_action_prob=0.1)
For a transitional five-value API, opt in explicitly:
from gymnax.wrappers import LegacyStepAPIWrapper
legacy_env = LegacyStepAPIWrapper(env)
next_obs, next_state, reward, done, info = legacy_env.step(
key_step, state, action, env_params
)
Native env.render(state, params) methods return Matplotlib figures and axes
for environment-specific debugging. To display one interactively, use an
interactive Matplotlib backend and call plt.show().
-
Training pipelines & pretrained agents: Check out gymnax-blines for trained agents, expert rollout visualizations and PPO/ES pipelines. The agents are minimally tuned, but can help you get up and running.
-
Simple batch agent evaluation: Work-in-progress.
from gymnax.experimental import RolloutWrapper
# Define rollout manager for pendulum env
manager = RolloutWrapper(model.apply, env_name="Pendulum-v1")
# Simple single episode rollout for policy
obs, action, reward, next_obs, done, cum_ret = manager.single_rollout(key, policy_params)
# Multiple rollouts for same network (different key, e.g. eval)
key_batch = jax.random.split(key, 10)
obs, action, reward, next_obs, done, cum_ret = manager.batch_rollout(
key_batch, policy_params
)
# Multiple rollouts for different networks + key (e.g. for ES)
batch_params = jax.tree.map( # Stack parameters or use different
lambda x: jnp.tile(x, (5, 1)).reshape(5, *x.shape), policy_params
)
obs, action, reward, next_obs, done, cum_ret = manager.population_rollout(
key_batch, batch_params
)