-
Notifications
You must be signed in to change notification settings - Fork 4
/
train_dynamics.py
191 lines (176 loc) · 6.16 KB
/
train_dynamics.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
from dataclasses import dataclass
import os
import time
import einops
from flax.training import orbax_utils
from flax.training.train_state import TrainState
import optax
import orbax
import numpy as np
import jax
import jax.numpy as jnp
import wandb
import tyro
from genie import Genie, restore_genie_checkpoint
from data.dataloader import get_dataloader
ts = int(time.time())
@dataclass
class Args:
# Experiment
num_steps: int = 200_000
seed: int = 0
seq_len: int = 16
image_channels: int = 3
image_resolution: int = 64
file_path: str = "data/coinrun.npy"
# Optimization
batch_size: int = 36
min_lr: float = 3e-6
max_lr: float = 3e-5
warmup_steps: int = 5000
# Tokenizer
tokenizer_dim: int = 512
latent_patch_dim: int = 32
num_patch_latents: int = 1024
patch_size: int = 4
tokenizer_num_blocks: int = 8
tokenizer_num_heads: int = 8
tokenizer_checkpoint: str = ""
# LAM
lam_dim: int = 512
latent_action_dim: int = 32
num_latent_actions: int = 6
lam_patch_size: int = 16
lam_num_blocks: int = 8
lam_num_heads: int = 8
lam_checkpoint: str = ""
# Dynamics
dyna_dim: int = 512
dyna_num_blocks: int = 12
dyna_num_heads: int = 8
dropout: float = 0.0
mask_limit: float = 0.5
# Logging
log: bool = False
entity: str = ""
project: str = ""
log_interval: int = 5
log_image_interval: int = 250
ckpt_dir: str = ""
log_checkpoint_interval: int = 25000
log_gradients: bool = False
args = tyro.cli(Args)
rng = jax.random.PRNGKey(args.seed)
if args.log:
wandb.init(entity=args.entity, project=args.project, group="debug", config=args)
# --- Construct train state ---
genie = Genie(
# Tokenizer
in_dim=args.image_channels,
tokenizer_dim=args.tokenizer_dim,
latent_patch_dim=args.latent_patch_dim,
num_patch_latents=args.num_patch_latents,
patch_size=args.patch_size,
tokenizer_num_blocks=args.tokenizer_num_blocks,
tokenizer_num_heads=args.tokenizer_num_heads,
# LAM
lam_dim=args.lam_dim,
latent_action_dim=args.latent_action_dim,
num_latent_actions=args.num_latent_actions,
lam_patch_size=args.lam_patch_size,
lam_num_blocks=args.lam_num_blocks,
lam_num_heads=args.lam_num_heads,
# Dynamics
dyna_dim=args.dyna_dim,
dyna_num_blocks=args.dyna_num_blocks,
dyna_num_heads=args.dyna_num_heads,
dropout=args.dropout,
mask_limit=args.mask_limit,
)
rng, _rng = jax.random.split(rng)
image_shape = (args.image_resolution, args.image_resolution, args.image_channels)
dummy_inputs = dict(
videos=jnp.zeros((args.batch_size, args.seq_len, *image_shape), dtype=jnp.float32),
mask_rng=_rng,
)
rng, _rng = jax.random.split(rng)
init_params = genie.init(_rng, dummy_inputs)
init_params = restore_genie_checkpoint(
init_params, args.tokenizer_checkpoint, args.lam_checkpoint
)
lr_schedule = optax.warmup_cosine_decay_schedule(
args.min_lr, args.max_lr, args.warmup_steps, args.num_steps
)
tx = optax.adamw(learning_rate=lr_schedule, b1=0.9, b2=0.9, weight_decay=1e-4)
train_state = TrainState.create(apply_fn=genie.apply, params=init_params, tx=tx)
def dynamics_loss_fn(params, state, inputs):
# --- Compute masked loss ---
outputs = state.apply_fn(
params, inputs, training=True, rngs={"dropout": inputs["dropout_rng"]}
)
mask = outputs["mask"]
ce_loss = optax.softmax_cross_entropy_with_integer_labels(
outputs["token_logits"], outputs["video_tokens"]
)
ce_loss = (mask * ce_loss).sum() / mask.sum()
acc = outputs["token_logits"].argmax(-1) == outputs["video_tokens"]
acc = (mask * acc).sum() / mask.sum()
metrics = dict(cross_entropy_loss=ce_loss, masked_token_accuracy=acc)
return ce_loss, (outputs["recon"], metrics)
# --- Define train step ---
@jax.jit
def train_step(state, inputs):
grad_fn = jax.value_and_grad(dynamics_loss_fn, has_aux=True, allow_int=True)
(loss, (recon, metrics)), grads = grad_fn(state.params, state, inputs)
state = state.apply_gradients(grads=grads)
if args.log_gradients:
metrics["gradients_std/"] = jax.tree.map(
lambda x: x.std(), grads["params"]["dynamics"]
)
return state, loss, recon, metrics
# --- TRAIN LOOP ---
dataloader = get_dataloader(args.file_path, args.seq_len, args.batch_size)
step = 0
while step < args.num_steps:
for videos in dataloader:
# --- Train step ---
rng, _rng, _mask_rng = jax.random.split(rng, 3)
inputs = dict(
videos=jnp.array(videos, dtype=jnp.float32) / 255.0,
action=jnp.zeros((args.batch_size, args.seq_len), dtype=jnp.float32),
dropout_rng=_rng,
mask_rng=_mask_rng,
)
train_state, loss, recon, metrics = train_step(train_state, inputs)
print(f"Step {step}, loss: {loss}")
step += 1
# --- Logging ---
if args.log:
if step % args.log_interval == 0:
wandb.log({"loss": loss, "step": step, **metrics})
if step % args.log_image_interval == 0:
gt_seq = inputs["videos"][0]
recon_seq = recon[0].clip(0, 1)
comparison_seq = jnp.concatenate((gt_seq, recon_seq), axis=1)
comparison_seq = einops.rearrange(
comparison_seq * 255, "t h w c -> h (t w) c"
)
log_images = dict(
image=wandb.Image(np.asarray(gt_seq[15])),
recon=wandb.Image(np.asarray(recon_seq[15])),
true_vs_recon=wandb.Image(
np.asarray(comparison_seq.astype(np.uint8))
),
)
wandb.log(log_images)
if step % args.log_checkpoint_interval == 0:
ckpt = {"model": train_state}
orbax_checkpointer = orbax.checkpoint.PyTreeCheckpointer()
save_args = orbax_utils.save_args_from_target(ckpt)
orbax_checkpointer.save(
os.path.join(args.ckpt_dir, f"checkpoints/genie_{ts}_{step}"),
ckpt,
save_args=save_args,
)
if step >= args.num_steps:
break