-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfinetune_gym.py
More file actions
435 lines (391 loc) · 15 KB
/
Copy pathfinetune_gym.py
File metadata and controls
435 lines (391 loc) · 15 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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
import os
from functools import partial
import gym
import jax
import numpy as np
import tqdm
from absl import app, flags, logging
from flax.training import checkpoints
from ml_collections import config_flags
from copy import deepcopy
from experiments.configs.ensemble_config import add_redq_config
from backend.agents import agents
from backend.common.evaluation import evaluate_and_record_videos_baseline
from backend.common.wandb import WandBLogger
from backend.data.replay_buffer import ReplayBuffer, ReplayBufferMC
from backend.envs.adroit_binary_dataset import get_hand_dataset_with_mc_calculation
from backend.envs.d4rl_dataset import (
get_d4rl_dataset,
get_d4rl_dataset_with_mc_calculation,
)
from backend.envs.env_common import get_env_type, make_gym_env
from backend.utils.timer_utils import Timer
from backend.utils.train_utils import concatenate_batches, subsample_batch
FLAGS = flags.FLAGS
# env
flags.DEFINE_string("env", "antmaze-large-diverse-v2", "Environemnt to use")
flags.DEFINE_float("reward_scale", 1.0, "Reward scale.")
flags.DEFINE_float("reward_bias", -1.0, "Reward bias.")
flags.DEFINE_float(
"clip_action",
0.99999,
"Clip actions to be between [-n, n]. This is needed for tanh policies.",
)
# training
flags.DEFINE_integer("num_offline_steps", 1_000_000, "Number of offline epochs.")
flags.DEFINE_integer("num_online_steps", 500_000, "Number of online epochs.")
flags.DEFINE_float(
"offline_data_ratio",
0.0,
"How much offline data to retain in each online batch update",
)
flags.DEFINE_string(
"online_sampling_method",
"mixed",
"""Method of sampling data during online update: mixed or append.
`mixed` samples from a mix of offline and online data according to offline_data_ratio.
`append` adds offline data to replay buffer and samples from it.""",
)
flags.DEFINE_bool(
"online_use_cql_loss",
True,
"""When agent is CQL/CalQL, whether to use CQL loss for the online phase (use SAC loss if False)""",
)
flags.DEFINE_integer(
"warmup_steps", 0, "number of warmup steps (WSRL) before performing online updates"
)
# agent
flags.DEFINE_string("agent", "calql", "what RL agent to use")
flags.DEFINE_integer("utd", 1, "update-to-data ratio of the critic")
flags.DEFINE_integer("batch_size", 256, "batch size for training")
flags.DEFINE_integer("replay_buffer_capacity", int(2e6), "Replay buffer capacity")
flags.DEFINE_bool("use_redq", False, "Use an ensemble of Q-functions for the agent")
# experiment house keeping
flags.DEFINE_integer("seed", 20, "Random seefd.")
flags.DEFINE_string(
"save_dir",
os.path.expanduser("~/q2rl_log"),
"Directory to save the logs and checkpoints",
)
flags.DEFINE_string("resume_path", "", "Path to resume from")
flags.DEFINE_integer("log_interval", 5_000, "Log every n steps")
flags.DEFINE_integer("eval_interval", 20_000, "Evaluate every n steps")
flags.DEFINE_integer("video_interval", 20_000, "Evaluate every n steps")
flags.DEFINE_integer("save_interval", 20_000, "Save every n steps.")
flags.DEFINE_integer(
"n_eval_trajs", 20, "Number of trajectories to use for each evaluation."
)
flags.DEFINE_bool("deterministic_eval", True, "Whether to use deterministic evaluation")
# wandb
flags.DEFINE_string("exp_name", "", "Experiment name for wandb logging")
flags.DEFINE_string("project", "q2rl", "Wandb project folder")
flags.DEFINE_string("group", None, "Wandb group of the experiment")
flags.DEFINE_bool("debug", False, "If true, no logging to wandb")
config_flags.DEFINE_config_file(
"config",
None,
"File path to the training hyperparameter configuration.",
lock_config=False,
)
"""
Directly taken from WSRL - https://github.com/zhouzypaul/wsrl
"""
def main(_):
"""
house keeping
"""
assert FLAGS.online_sampling_method in [
"mixed",
"append",
], "incorrect online sampling method"
if FLAGS.use_redq:
FLAGS.config.agent_kwargs = add_redq_config(FLAGS.config.agent_kwargs)
min_steps_to_update = FLAGS.batch_size * (1 - FLAGS.offline_data_ratio)
if FLAGS.agent == "calql":
min_steps_to_update = max(
min_steps_to_update, gym.make(FLAGS.env)._max_episode_steps
)
"""
wandb and logging
"""
wandb_config = WandBLogger.get_default_config()
wandb_config.update(
{
"project": FLAGS.project,
"group": FLAGS.group,
"exp_descriptor": f"{FLAGS.exp_name}_{FLAGS.env}_{FLAGS.agent}_seed{FLAGS.seed}",
}
)
wandb_logger = WandBLogger(
wandb_config=wandb_config,
variant=FLAGS.config.to_dict(),
random_str_in_identifier=True,
disable_online_logging=FLAGS.debug,
)
save_dir = os.path.join(
FLAGS.save_dir,
wandb_logger.config.project,
f"{wandb_logger.config.exp_descriptor}_{wandb_logger.config.unique_identifier}",
)
"""
env
"""
# do not clip adroit actions online following CalQL repo
# https://github.com/nakamotoo/Cal-QL
env_type = get_env_type(FLAGS.env)
finetune_env = make_gym_env(
env_name=FLAGS.env,
reward_scale=FLAGS.reward_scale,
reward_bias=FLAGS.reward_bias,
scale_and_clip_action=env_type in ("antmaze", "kitchen", "locomotion"),
action_clip_lim=FLAGS.clip_action,
seed=FLAGS.seed,
)
eval_env = make_gym_env(
env_name=FLAGS.env,
scale_and_clip_action=env_type in ("antmaze", "kitchen", "locomotion"),
action_clip_lim=FLAGS.clip_action,
seed=FLAGS.seed + 1000,
)
"""
load dataset
"""
if env_type == "adroit-binary":
dataset = get_hand_dataset_with_mc_calculation(
FLAGS.env,
gamma=FLAGS.config.agent_kwargs.discount,
reward_scale=FLAGS.reward_scale,
reward_bias=FLAGS.reward_bias,
clip_action=FLAGS.clip_action,
)
else:
if FLAGS.agent == "calql":
# need dataset with mc return
dataset = get_d4rl_dataset_with_mc_calculation(
FLAGS.env,
reward_scale=FLAGS.reward_scale,
reward_bias=FLAGS.reward_bias,
clip_action=FLAGS.clip_action,
gamma=FLAGS.config.agent_kwargs.discount,
)
else:
dataset = get_d4rl_dataset(
FLAGS.env,
reward_scale=FLAGS.reward_scale,
reward_bias=FLAGS.reward_bias,
clip_action=FLAGS.clip_action,
)
"""
replay buffer
"""
replay_buffer_type = ReplayBufferMC if FLAGS.agent == "calql" else ReplayBuffer
replay_buffer = replay_buffer_type(
finetune_env.observation_space,
finetune_env.action_space,
capacity=FLAGS.replay_buffer_capacity,
seed=FLAGS.seed,
discount=FLAGS.config.agent_kwargs.discount if FLAGS.agent == "calql" else None,
)
"""
Initialize agent
"""
rng = jax.random.PRNGKey(FLAGS.seed)
rng, construct_rng = jax.random.split(rng)
example_batch = subsample_batch(dataset, FLAGS.batch_size)
agent = agents[FLAGS.agent].create(
rng=construct_rng,
observations=example_batch["observations"],
actions=example_batch["actions"],
encoder_def=None,
**FLAGS.config.agent_kwargs,
)
if FLAGS.resume_path != "":
assert os.path.exists(FLAGS.resume_path), "resume path does not exist"
agent = checkpoints.restore_checkpoint(FLAGS.resume_path, target=agent)
"""
eval function
"""
def evaluate_and_log_results(
eval_env,
policy_fn,
eval_func,
step_number,
wandb_logger,
n_eval_trajs=FLAGS.n_eval_trajs,
):
stats, trajs = eval_func(
policy_fn,
eval_env,
n_eval_trajs,
)
eval_info = {
"average_return": np.mean([np.sum(t["rewards"]) for t in trajs]),
"average_traj_length": np.mean([len(t["rewards"]) for t in trajs]),
}
if env_type == "adroit-binary":
# adroit
eval_info["success_rate"] = np.mean(
[any(d["goal_achieved"] for d in t["infos"]) for t in trajs]
)
elif env_type == "kitchen":
# kitchen
eval_info["num_stages_solved"] = np.mean([t["rewards"][-1] for t in trajs])
eval_info["success_rate"] = np.mean([t["rewards"][-1] for t in trajs]) / 4
else:
# d4rl antmaze, locomotion
eval_info["success_rate"] = eval_info[
"average_normalized_return"
] = np.mean(
[eval_env.get_normalized_score(np.sum(t["rewards"])) for t in trajs]
)
wandb_logger.log({"evaluation": eval_info}, step=step_number)
"""
training loop
"""
timer = Timer()
step = int(agent.state.step) # 0 for new agents, or load from pre-trained
is_online_stage = False
observation, info = finetune_env.reset()
done = False # env done signal
for _ in tqdm.tqdm(range(step, FLAGS.num_offline_steps + FLAGS.num_online_steps)):
"""
Switch from offline to online
"""
if not is_online_stage and step >= FLAGS.num_offline_steps:
logging.info("Switching to online training")
is_online_stage = True
# upload offline data to online buffer
if FLAGS.online_sampling_method == "append":
offline_dataset_size = dataset["actions"].shape[0]
dataset_items = dataset.items()
for j in range(offline_dataset_size):
transition = {k: v[j] for k, v in dataset_items}
replay_buffer.insert(transition)
# option for CQL and CalQL to change the online alpha, and whether to use CQL regularizer
if FLAGS.agent in ("cql", "calql"):
online_agent_configs = {
"cql_alpha": FLAGS.config.agent_kwargs.get(
"online_cql_alpha", None
),
"use_cql_loss": FLAGS.online_use_cql_loss,
}
agent.update_config(online_agent_configs)
timer.tick("total")
"""
Env Step
"""
with timer.context("env step"):
if is_online_stage:
rng, action_rng = jax.random.split(rng)
action = agent.sample_actions(observation, seed=action_rng)
next_observation, reward, done, truncated, info = finetune_env.step(
action
)
transition = dict(
observations=observation,
next_observations=next_observation,
actions=action,
rewards=reward,
masks=1.0 - done,
dones=1.0 if (done or truncated) else 0,
)
replay_buffer.insert(transition)
observation = next_observation
if done or truncated:
observation, info = finetune_env.reset()
done = False
"""
Updates
"""
with timer.context("update"):
# offline updates
if not is_online_stage:
batch = subsample_batch(dataset, FLAGS.batch_size)
agent, update_info = agent.update(
batch,
)
# online updates
else:
if step - FLAGS.num_offline_steps <= max(
FLAGS.warmup_steps, min_steps_to_update
):
# no updates during warmup
pass
else:
# do online updates, gather batch
if FLAGS.online_sampling_method == "mixed":
# batch from a mixing ratio of offline and online data
batch_size_offline = int(
FLAGS.batch_size * FLAGS.offline_data_ratio
)
batch_size_online = FLAGS.batch_size - batch_size_offline
online_batch = replay_buffer.sample(batch_size_online)
offline_batch = subsample_batch(dataset, batch_size_offline)
# update with the combined batch
batch = concatenate_batches([online_batch, offline_batch])
elif FLAGS.online_sampling_method == "append":
# batch from online replay buffer, with is initialized with offline data
batch = replay_buffer.sample(FLAGS.batch_size)
else:
raise RuntimeError("Incorrect online sampling method")
# update
if FLAGS.utd > 1:
agent, update_info = agent.update_high_utd(
batch,
utd_ratio=FLAGS.utd,
)
else:
agent, update_info = agent.update(
batch,
)
"""
Advance Step
"""
step += 1
"""
Evals
"""
eval_steps = (
FLAGS.num_offline_steps, # finish offline training
FLAGS.num_offline_steps + 1, # start of online training
FLAGS.num_offline_steps + FLAGS.num_online_steps, # end of online training
)
if step % FLAGS.eval_interval == 0 or step in eval_steps:
logging.info("Evaluating...")
with timer.context("evaluation"):
policy_fn = partial(
agent.sample_actions, argmax=FLAGS.deterministic_eval
)
eval_func = partial(
evaluate_and_record_videos_baseline, clip_action=FLAGS.clip_action,
record_video=True if step%FLAGS.video_interval == 0 else False,
wandb_logger=wandb_logger, step_number=step
)
evaluate_and_log_results(
eval_env=eval_env,
policy_fn=policy_fn,
eval_func=eval_func,
step_number=step,
wandb_logger=wandb_logger,
)
"""
Save Checkpoint
"""
if step % FLAGS.save_interval == 0 or step == FLAGS.num_offline_steps:
logging.info("Saving checkpoint...")
checkpoint_path = checkpoints.save_checkpoint(
save_dir, agent, step=step, keep=30
)
logging.info("Saved checkpoint to %s", checkpoint_path)
timer.tock("total")
"""
Logging
"""
if step % FLAGS.log_interval == 0:
# check if update_info is available (False during warmup)
if "update_info" in locals():
update_info = jax.device_get(update_info)
wandb_logger.log({"training": update_info}, step=step)
wandb_logger.log({"timer": timer.get_average_times()}, step=step)
if __name__ == "__main__":
app.run(main)