forked from RobertTLange/gymnax-blines
-
Notifications
You must be signed in to change notification settings - Fork 0
/
speed.py
171 lines (152 loc) · 4.38 KB
/
speed.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
def speed(
env_name: str,
num_env_steps: int,
num_envs: int,
num_runs: int,
use_network: bool,
use_np_env: bool,
):
import jax
import numpy as np
from utils.models import get_model_ready
from utils.helpers import load_config
from utils.benchmark import (
speed_numpy_random,
speed_numpy_network,
speed_gymnax_random,
speed_gymnax_network,
)
# Get the policy and parameters (if not random)
configs = load_config(f"agents/{env_name}/es.yaml")
rng = jax.random.PRNGKey(0)
if use_network:
model, params = get_model_ready(rng, configs.train_config, speed=True)
run_times = []
for run_id in range(num_runs):
rng, rng_run = jax.random.split(rng)
np.random.seed(run_id)
# Use numpy env + random policy
if not use_network and use_np_env:
r_time = speed_numpy_random(env_name, num_env_steps, num_envs)
# Use numpy env + MLP policy
elif use_network and use_np_env:
r_time = speed_numpy_network(
env_name, num_env_steps, num_envs, rng_run, model, params
)
# Use gymnax env + random policy
elif not use_network and not use_np_env:
r_time = speed_gymnax_random(
env_name,
num_env_steps,
num_envs,
rng_run,
configs.train_config.env_kwargs,
)
# Use gymnax env + MLP policy
elif use_network and not use_np_env:
r_time = speed_gymnax_network(
env_name,
num_env_steps,
num_envs,
rng_run,
model,
params,
configs.train_config.env_kwargs,
)
# Store the computed run time
print(f"Run {run_id + 1} - Env: {env_name} - Done after {r_time}")
run_times.append(r_time)
print(run_times)
return np.mean(run_times), np.std(run_times)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
"-env",
"--env_name",
type=str,
default="CartPole-v1",
help="Environment to estimate step speed for.",
)
parser.add_argument(
"-steps",
"--num_env_steps",
type=int,
default=1_000_000,
help="Environment steps.",
)
parser.add_argument(
"-runs",
"--num_runs",
type=int,
default=10,
help="Number of random seeds for estimation.",
)
parser.add_argument(
"-n_envs",
"--num_envs",
type=int,
default=1,
help="Number of parallel workers/envs.",
)
parser.add_argument(
"-network",
"--use_network",
action="store_true",
default=False,
help="MLP/Random rollout.",
)
parser.add_argument(
"-np",
"--use_np_env",
action="store_true",
default=False,
help="Numpy rollout.",
)
parser.add_argument(
"-gpu",
"--use_gpu",
action="store_true",
default=False,
help="Use gpu if available.",
)
parser.add_argument(
"-a100",
"--a100",
action="store_true",
default=False,
help="Use A100 gpu - store extension.",
)
args, _ = parser.parse_known_args()
import os
if args.use_gpu:
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
else:
os.environ["CUDA_VISIBLE_DEVICES"] = ""
print(30 * "=", "START SPEED BENCH", 30 * "=")
print(
f"Env: {args.env_name}, #Envs: {args.num_envs}, Network:"
f" {args.use_network}, np env: {args.use_np_env}, GPU: {args.use_gpu}"
)
print(79 * "=")
mean_rt, std_rt = speed(
args.env_name,
args.num_env_steps,
args.num_envs,
args.num_runs,
args.use_network,
args.use_np_env,
)
conf_name = f"net-{args.use_network}-envs-{args.num_envs}-np-{args.use_np_env}-gpu-{args.use_gpu}"
if args.a100:
conf_name += "-a100"
print(conf_name)
# Write data to pkl file
fname = f"agents/{args.env_name}/speed.pkl"
from utils.helpers import load_pkl_object, save_pkl_object
if os.path.exists(fname):
log = load_pkl_object(fname)
else:
log = {}
log[conf_name] = [mean_rt, std_rt]
save_pkl_object(log, fname)