-
Notifications
You must be signed in to change notification settings - Fork 16
/
train.py
288 lines (257 loc) · 10.7 KB
/
train.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
279
280
281
282
283
284
285
286
287
288
import logging
from collections import OrderedDict
from pathlib import Path
import hydra
import torch
import torch.nn.functional as F
import tqdm
from torch.utils.data import DataLoader
from models.action_ae.generators.base import GeneratorDataParallel
from models.latent_generators.latent_generator import LatentGeneratorDataParallel
from omegaconf import OmegaConf
import utils
import wandb
class Workspace:
def __init__(self, cfg):
self.work_dir = Path.cwd()
print("Saving to {}".format(self.work_dir))
self.cfg = cfg
self.device = torch.device(cfg.device)
utils.set_seed_everywhere(cfg.seed)
self.dataset = hydra.utils.call(
cfg.env.dataset_fn,
train_fraction=cfg.train_fraction,
random_seed=cfg.seed,
device=self.device,
)
self.train_set, self.test_set = self.dataset
self._setup_loaders()
# Create the model
self.action_ae = None
self.obs_encoding_net = None
self.state_prior = None
if not self.cfg.lazy_init_models:
self._init_action_ae()
self._init_obs_encoding_net()
self._init_state_prior()
self.log_components = OrderedDict()
self.epoch = self.prior_epoch = 0
self.save_training_latents = False
self._training_latents = []
self.wandb_run = wandb.init(
dir=str(self.work_dir),
project=cfg.project,
config=OmegaConf.to_container(cfg, resolve=True),
)
wandb.config.update(
{
"save_path": self.work_dir,
}
)
def _init_action_ae(self):
if self.action_ae is None: # possibly already initialized from snapshot
self.action_ae = hydra.utils.instantiate(
self.cfg.action_ae, _recursive_=False
).to(self.device)
if self.cfg.data_parallel:
self.action_ae = GeneratorDataParallel(self.action_ae)
def _init_obs_encoding_net(self):
if self.obs_encoding_net is None: # possibly already initialized from snapshot
self.obs_encoding_net = hydra.utils.instantiate(self.cfg.encoder)
self.obs_encoding_net = self.obs_encoding_net.to(self.device)
if self.cfg.data_parallel:
self.obs_encoding_net = torch.nn.DataParallel(self.obs_encoding_net)
def _init_state_prior(self):
if self.state_prior is None: # possibly already initialized from snapshot
self.state_prior = hydra.utils.instantiate(
self.cfg.state_prior,
latent_dim=self.action_ae.latent_dim,
vocab_size=self.action_ae.num_latents,
).to(self.device)
if self.cfg.data_parallel:
self.state_prior = LatentGeneratorDataParallel(self.state_prior)
self.state_prior_optimizer = self.state_prior.get_optimizer(
learning_rate=self.cfg.lr,
weight_decay=self.cfg.weight_decay,
betas=tuple(self.cfg.betas),
)
def _setup_loaders(self):
self.train_loader = DataLoader(
self.train_set,
batch_size=self.cfg.batch_size,
shuffle=True,
num_workers=self.cfg.num_workers,
pin_memory=True,
)
self.test_loader = DataLoader(
self.test_set,
batch_size=self.cfg.batch_size,
shuffle=False,
num_workers=self.cfg.num_workers,
pin_memory=True,
)
self.latent_collection_loader = DataLoader(
self.train_set,
batch_size=self.cfg.batch_size,
shuffle=False,
num_workers=self.cfg.num_workers,
pin_memory=True,
)
def train_prior(self):
self.state_prior.train()
with utils.eval_mode(self.obs_encoding_net, self.action_ae):
pbar = tqdm.tqdm(
self.train_loader, desc=f"Training prior epoch {self.prior_epoch}"
)
for data in pbar:
observations, action, mask = data
self.state_prior_optimizer.zero_grad(set_to_none=True)
obs, act = observations.to(self.device), action.to(self.device)
enc_obs = self.obs_encoding_net(obs)
latent = self.action_ae.encode_into_latent(act, enc_obs)
_, loss, loss_components = self.state_prior.get_latent_and_loss(
obs_rep=enc_obs,
target_latents=latent,
return_loss_components=True,
)
loss.backward()
torch.nn.utils.clip_grad_norm_(
self.state_prior.parameters(), self.cfg.grad_norm_clip
)
self.state_prior_optimizer.step()
self.log_append("prior_train", len(observations), loss_components)
def eval_prior(self):
with utils.eval_mode(
self.obs_encoding_net, self.action_ae, self.state_prior, no_grad=True
):
for observations, action, mask in self.test_loader:
obs, act = observations.to(self.device), action.to(self.device)
enc_obs = self.obs_encoding_net(obs)
latent = self.action_ae.encode_into_latent(act, enc_obs)
_, loss, loss_components = self.state_prior.get_latent_and_loss(
obs_rep=enc_obs,
target_latents=latent,
return_loss_components=True,
)
self.log_append("prior_eval", len(observations), loss_components)
def run(self):
snapshot = self.snapshot
if snapshot.exists():
print(f"Resuming: {snapshot}")
self.load_snapshot()
if self.cfg.lazy_init_models:
self._init_obs_encoding_net()
self._init_action_ae()
self.action_ae.fit_model(
self.train_loader,
self.test_loader,
self.obs_encoding_net,
)
if self.cfg.save_latents:
self.save_latents()
# Train the action prior model.
if self.cfg.lazy_init_models:
self._init_state_prior()
self.state_prior_iterator = tqdm.trange(
self.prior_epoch, self.cfg.num_prior_epochs
)
self.state_prior_iterator.set_description("Training prior: ")
# Reset the log.
self.log_components = OrderedDict()
for epoch in self.state_prior_iterator:
self.prior_epoch = epoch
self.train_prior()
if ((self.prior_epoch + 1) % self.cfg.eval_prior_every) == 0:
self.eval_prior()
self.flush_log(epoch=epoch + self.epoch, iterator=self.state_prior_iterator)
self.prior_epoch += 1
if ((self.prior_epoch + 1) % self.cfg.save_prior_every) == 0:
self.save_snapshot()
# expose DataParallel module class name for wandb tags
tag_func = (
lambda m: m.module.__class__.__name__
if self.cfg.data_parallel
else m.__class__.__name__
)
tags = tuple(
map(tag_func, [self.obs_encoding_net, self.action_ae, self.state_prior])
)
self.wandb_run.tags += tags
@property
def snapshot(self):
return self.work_dir / "snapshot.pt"
def save_snapshot(self):
self._keys_to_save = [
"action_ae",
"obs_encoding_net",
"epoch",
"prior_epoch",
"state_prior",
]
payload = {k: self.__dict__[k] for k in self._keys_to_save}
with self.snapshot.open("wb") as f:
torch.save(payload, f)
def save_latents(self):
total_mse_loss = 0
with utils.eval_mode(self.action_ae, self.obs_encoding_net, no_grad=True):
for observations, action, mask in self.latent_collection_loader:
obs, act = observations.to(self.device), action.to(self.device)
enc_obs = self.obs_encoding_net(obs)
latent = self.action_ae.encode_into_latent(act, enc_obs)
reconstructed_action = self.action_ae.decode_actions(
latent,
enc_obs,
)
total_mse_loss += F.mse_loss(act, reconstructed_action, reduction="sum")
if type(latent) == tuple:
# serialize into tensor; assumes last dim is latent dim
detached_latents = tuple(x.detach() for x in latent)
self._training_latents.append(torch.cat(detached_latents, dim=-1))
else:
self._training_latents.append(latent.detach())
self._training_latents_tensor = torch.cat(self._training_latents, dim=0)
logging.info(f"Total MSE reconstruction loss: {total_mse_loss}")
logging.info(
f"Average MSE reconstruction loss: {total_mse_loss / len(self._training_latents_tensor)}"
)
torch.save(self._training_latents_tensor, self.work_dir / "latents.pt")
def load_snapshot(self):
with self.snapshot.open("rb") as f:
payload = torch.load(f)
for k, v in payload.items():
self.__dict__[k] = v
not_in_payload = set(self._keys_to_save) - set(payload.keys())
if len(not_in_payload):
logging.warning("Keys not found in snapshot: %s", not_in_payload)
def log_append(self, log_key, length, loss_components):
for key, value in loss_components.items():
key_name = f"{log_key}/{key}"
count, sum = self.log_components.get(key_name, (0, 0.0))
self.log_components[key_name] = (
count + length,
sum + (length * value.detach().cpu().item()),
)
def flush_log(self, epoch, iterator):
log_components = OrderedDict()
iterator_log_component = OrderedDict()
for key, value in self.log_components.items():
count, sum = value
to_log = sum / count
log_components[key] = to_log
# Set the iterator status
log_key, name_key = key.split("/")
iterator_log_name = f"{log_key[0]}{name_key[0]}".upper()
iterator_log_component[iterator_log_name] = to_log
postfix = ",".join(
"{}:{:.2e}".format(key, iterator_log_component[key])
for key in iterator_log_component.keys()
)
iterator.set_postfix_str(postfix)
wandb.log(log_components, step=epoch)
self.log_components = OrderedDict()
@hydra.main(config_path="configs", config_name="config")
def main(cfg):
workspace = Workspace(cfg)
workspace.run()
if __name__ == "__main__":
main()