-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheval_offline.py
280 lines (233 loc) · 9.44 KB
/
eval_offline.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
from collections import defaultdict
import gym
import numpy as np
import argparse
import os
import torch
import wandb
from typing import Any, Tuple, Dict, List
from utils.vec_env.subproc_vec_env import VecEnv, SubprocVecEnv
from policies.policy import Policy
from pathlib import Path
from utils.wandb import get_run_from_folder, get_policy_paths_from_folder
from multiprocessing import Process, connection, Queue
from tqdm import tqdm
from algs.core import load_alg_policy, load_trainer
from utils.utils import fix_random_seeds
from datasets.core import load_datasets
from ope.core import OPE_NAME_VAL_S0_FQE, create_eval_trainer, sample_hyperparams
# Done 1. Loss on validation data
# - What Matters in
# Done? 2. TD-Error on validation data
# - ??
# 3. V_s0 using Q from ALG
# - ??
# 4. V_s0 using Q from FQE
# 1. FQE from NeoRL (and Paine et al.)
# 2. FQE from Kostrikov and Nachum
def eval_offline(
wandb_config: Dict[str, Any],
train_config: Dict[str, Any],
device: torch.device,
result_holder: Queue,
) -> float:
# Fix the seeds
fix_random_seeds(train_config["seed"])
# Load the policy
policy = load_alg_policy(
alg_name = wandb_config["alg"],
env_name = wandb_config["env"],
policy_path = train_config["policy_path"]
)
policy.to(device)
# Load the policy's trainer
policy_trainer = load_trainer(
alg_name = wandb_config["alg"],
env_name = wandb_config["env"],
hyperparams = wandb_config,
path = train_config["trainer_path"],
device = device
)
policy_trainer.to(device)
# # Load the datasets
train_dataset, val_dataset = load_datasets(
env_name = wandb_config["env"],
n_trajectories = wandb_config["n_trajectories"],
policy_level = wandb_config["policy_level"],
val_only = True
)
# Create the evaluation trainer
train_config.update(sample_hyperparams(train_config["ope_name"]))
train_config.update({"env": wandb_config["env"]})
eval_trainer = create_eval_trainer(
alg_name = train_config["ope_name"],
policy_trainer = policy_trainer,
policy = policy,
train_config = train_config,
)
eval_trainer.to(device)
# Set the logger if one uses FQE
logger = None
if train_config["ope_name"] == OPE_NAME_VAL_S0_FQE:
logger = wandb.init(project="offline-rl-ope", dir=os.environ["WANDB_PATH"], reinit=True)
logger.config.update(train_config)
logger.config.update({"device": device})
eval_trainer.set_logger(logger)
# Train it (for some offline evals it's noop)
# Offline algos typically use val_dataset only, so train_dataset is None for memory reasons
eval_trainer.train(train_dataset, val_dataset)
# Compute offline metric
offline_metric = eval_trainer.eval(val_dataset)
# Put on a result
result_holder.put(offline_metric)
# Finish the logger
if logger:
logger.finish()
def get_evaluation_process(
job: Dict[str, Any],
device: torch.device
) -> Tuple[Process, Queue]:
result_holder = Queue()
return Process(
target=eval_offline,
kwargs={
"wandb_config" : job["wandb_config"],
"train_config" : job["train_config"],
"device" : device,
"result_holder": result_holder
}
), result_holder
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--envs", type=str, nargs="+", default=["industrial", "finrl", "citylearn"], help="For which environments start offline evaluation.")
parser.add_argument("--algs", type=str, nargs="+", default=["cql", "td3+bc", "bc", "fisherbrc", "crr"])
parser.add_argument("--levels", type=str, nargs="+", default=["low", "medium", "high"])
parser.add_argument("--devices", type=str, nargs="+", default=["cpu"])
parser.add_argument("--strategy", type=str, default="40", choices=["last", "all", "40"])
parser.add_argument("--wandb_entity", type=str, default="vkurenkov")
parser.add_argument("--wandb_project_name", type=str, default="offline-rl-baseline")
parser.add_argument("--ope_name",
type=str,
default="val_vs0_fqe",
choices=["val_loss", "val_tderror", "val_vs0_q", "val_vs0_fqe"]
)
parser.add_argument("--seed", type=int, default=1337)
parser.add_argument("--force", type=bool, default=False)
args = parser.parse_args()
# Some fixes for MacOS and wandb logging
os.environ['KMP_DUPLICATE_LIB_OK'] = "True"
os.environ["WANDB_SILENT"] = "True"
### Parse all the wandb runs to prepare evaluation jobs ###
eval_jobs = []
wandb_path = Path(os.path.join(os.environ["WANDB_PATH"], "wandb"))
subfolders = [f for f in os.scandir(wandb_path) if f.is_dir()]
wandb_api = wandb.Api()
for folder in tqdm(subfolders, desc="Parsing local wandb folders"):
# Skip latest-run folder
if folder.name == "latest-run":
continue
# Parse wandb run from this folder
run = get_run_from_folder(
folder_name=folder.name,
entity=args.wandb_entity,
project_name=args.wandb_project_name,
wandb_api=wandb_api
)
# This run is not on the sever thus skip
if not run:
continue
# If not in the target envs -- skip
if "env" not in run.config or run.config["env"] not in args.envs:
continue
if "policy_level" not in run.config or run.config["policy_level"] not in args.levels:
continue
if "alg" not in run.config or run.config["alg"] not in args.algs:
continue
# If the training is still running or crushed -- skip
if run.state != "finished":
continue
# Extract paths to each policy and add jobs
policies = get_policy_paths_from_folder(folder_path=folder.path)
# Only if the last policy needs to be evaluated
if args.strategy == "last" or args.strategy == "40":
policies = [policies[-1]]
# Add evaluation jobs
for (policy_path, epoch) in policies:
# If the target performance metric is already calculated -- skip
key_name = f"epoch_{epoch}/{args.ope_name}"
if key_name in run.summary and not args.force:
continue
# Only 40th epoch....
if args.strategy == "40" and epoch != 40:
continue
eval_jobs.append({
"run" : run,
"epoch" : epoch,
"wandb_config": run.config,
"train_config": {
"trainer_path": str(policy_path).replace(".policy", ".pt"),
"policy_path": policy_path,
"ope_name": args.ope_name,
"seed": args.seed
}
})
print(f"{len(eval_jobs)} jobs to be run on {len(args.devices)} devices.")
# Debug
# if True:
# job = eval_jobs[0]
# result_holder = Queue()
# eval_offline(
# wandb_config=job["wandb_config"],
# train_config=job["train_config"],
# device="cpu",
# result_holder=result_holder
# )
# print(result_holder.get())
### Run the evaluation jobs ###
n_workers = len(args.devices)
pool = []
for job, device in zip(eval_jobs, args.devices):
eval_prc, eval_res = get_evaluation_process(job=job, device=device)
pool.append((eval_prc, eval_res, device, job))
n_jobs_run = len(args.devices)
# Run the first batch of jobs
for process, _, _, _ in pool:
process.start()
# Progress bar
pbar = tqdm(total=len(eval_jobs), desc="Running evaluation jobs")
# Run all others
while n_jobs_run < len(eval_jobs):
# Wait until one of the processes is over
connection.wait(process.sentinel for process, _, _ , _ in pool)
# Check which of the processes has terminated and released its device
for ind in range(len(pool)):
cur_process = pool[ind][0]
cur_res = pool[ind][1]
cur_device = pool[ind][2]
cur_job = pool[ind][3]
# The job has been completed -- run a new one if needed
if not cur_process.is_alive() and n_jobs_run < len(eval_jobs):
# Retrieve the result
offline_metric = cur_res.get()
# Close this process
cur_process.join()
# Log the result into wandb
cur_job["run"].summary[f"epoch_{cur_job['epoch']}/{args.ope_name}"] = offline_metric
cur_job["run"].summary.update()
# Run a new one
new_job = eval_jobs[n_jobs_run]
eval_prc, eval_res = get_evaluation_process(job=new_job, device=cur_device)
pool[ind] = (eval_prc, eval_res, cur_device, new_job)
pool[ind][0].start()
n_jobs_run += 1
pbar.update(n=1)
# Clean up
for process, cur_res, _, cur_job in pool:
# Retrieve the result
offline_metric = cur_res.get()
# Close this process
process.join()
# Log the result into wandb
cur_job["run"].summary[f"epoch_{cur_job['epoch']}/{args.ope_name}"] = offline_metric
cur_job["run"].summary.update()
pbar.update(1)