Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Updates to functional jax vector envs #622

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 15 additions & 31 deletions gymnasium/experimental/functional_jax_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ def __init__(
metadata = {}
self.func_env = func_env
self.num_envs = num_envs

self.single_observation_space = func_env.observation_space
self.single_action_space = func_env.action_space
self.observation_space = batch_space(
Expand Down Expand Up @@ -164,6 +163,7 @@ def __init__(
self.rng = jrng.PRNGKey(seed)

self.func_env.transform(jax.vmap)
self.func_env.transform(jax.jit)

def reset(self, *, seed: int | None = None, options: dict | None = None):
"""Resets the environment."""
Expand Down Expand Up @@ -212,46 +212,30 @@ def step(self, action: ActType):
)

info = self.func_env.step_info(self.state, action, next_state)

done = jnp.logical_or(terminated, truncated)
if jnp.any(done):
final_obs = self.func_env.observation(next_state)

to_reset = jnp.where(done)[0]
reset_count = to_reset.shape[0]

rng, self.rng = jrng.split(self.rng)
rng = jrng.split(rng, reset_count)
rng = jrng.split(rng, self.num_envs)

new_initials = self.func_env.initial(rng)

next_state = self.state.at[to_reset].set(new_initials)
self.steps = self.steps.at[to_reset].set(0)
next_state = jnp.where(done[:, None], new_initials, next_state)
self.steps = jnp.where(done, jnp.zeros_like(self.steps), self.steps)

# Get the final observations and infos
info["final_observation"] = np.array([None for _ in range(self.num_envs)])
info["final_info"] = np.array([None for _ in range(self.num_envs)])

info["_final_observation"] = np.array([False for _ in range(self.num_envs)])
info["_final_info"] = np.array([False for _ in range(self.num_envs)])

# TODO: this can maybe be optimized, but right now I don't know how
for i in to_reset:
info["final_observation"][i] = final_obs[i]
info["final_info"][i] = {
k: v[i]
for k, v in info.items()
if k
not in {
"final_observation",
"final_info",
"_final_observation",
"_final_info",
}
}

info["_final_observation"][i] = True
info["_final_info"][i] = True
# info["final_observation"] = np.array([None for _ in range(self.num_envs)])

info["final_observation"] = final_obs
info["final_info"] = info

info["_final_observation"] = done
info["_final_info"] = done

start_info = self.func_env.state_info(next_state)

info = jax.tree_map(lambda a, b: jnp.where(done, b, a), info, start_info)

observation = self.func_env.observation(next_state)
observation = jax_to_numpy(observation)
Expand Down