-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse_rejax.py
More file actions
54 lines (41 loc) · 1.42 KB
/
use_rejax.py
File metadata and controls
54 lines (41 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'''Use HighJax with Rejax for JIT-compiled training.
Rejax accepts gymnax environment objects directly. The entire training loop
is JIT-compiled and can be vmapped across seeds for parallel runs.
Requires: pip install rejax
Note: Rejax checks for gymnax.environments.spaces.Discrete internally. If you
hit a space type error, wrap the env to return gymnax spaces instead of
gymnasium spaces from action_space() / observation_space().
'''
from __future__ import annotations
import jax
# Requires: pip install rejax
from rejax import PPO
import highjax
def main():
env, env_params = highjax.make('highjax-v0')
algo = PPO.create(
env=env,
env_params=env_params,
total_timesteps=131_072,
num_envs=32,
num_steps=64,
num_epochs=8,
learning_rate=3e-4,
gamma=0.99,
gae_lambda=0.95,
clip_eps=0.2,
ent_coef=0.01,
vf_coef=0.5,
)
# JIT-compile the full training loop
train_fn = jax.jit(algo.train)
key = jax.random.PRNGKey(0)
train_state, evaluation = train_fn(key)
print(f'Training complete. Evaluation returns: {evaluation}')
# Vmap across seeds for parallel independent runs
keys = jax.random.split(jax.random.PRNGKey(0), 5)
vmapped_train = jax.vmap(train_fn)
train_states, evaluations = vmapped_train(keys)
print(f'Parallel runs complete. Shape: {evaluations.shape}')
if __name__ == '__main__':
main()