-
Notifications
You must be signed in to change notification settings - Fork 2
/
trainer.py
585 lines (430 loc) · 17.5 KB
/
trainer.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
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
from pathlib import Path
from functools import partial
from packaging import version
from contextlib import nullcontext, contextmanager
import torch
from torch.nn import Module
from torch.utils.data import Dataset, DataLoader
from torch.optim.lr_scheduler import _LRScheduler
from pytorch_custom_utils import (
get_adam_optimizer,
OptimizerWithWarmupSchedule,
add_wandb_tracker_contextmanager
)
from accelerate import Accelerator
from accelerate.utils import DistributedDataParallelKwargs
from beartype import beartype
from beartype.door import is_bearable
from beartype.typing import Optional, Tuple, Type, List
from meshgpt_pytorch.data import custom_collate
from meshgpt_pytorch.version import __version__
# constants
DEFAULT_DDP_KWARGS = DistributedDataParallelKwargs(
find_unused_parameters = True
)
# helper functions
def exists(v):
return v is not None
def default(v, d):
return v if exists(v) else d
def divisible_by(num, den):
return (num % den) == 0
def cycle(dl):
while True:
for data in dl:
yield data
def maybe_del(d: dict, *keys):
for key in keys:
if key not in d:
continue
del d[key]
@contextmanager
def trackers(
trainer,
project_name: str,
run_name = None,
hps = None
):
assert trainer.use_wandb_tracking
trainer.accelerator.init_trackers(project_name, config = hps)
if run_name is not None and len(trainer.accelerator.trackers) > 0:
trainer.accelerator.trackers[0].run.name = run_name
yield
trainer.accelerator.end_training()
# autoencoder trainer
@add_wandb_tracker_contextmanager()
class MeshAutoencoderTrainer(Module):
@beartype
def __init__(
self,
model,
dataset: Dataset,
num_train_steps: int,
batch_size: int,
grad_accum_every: int,
val_dataset: Optional[Dataset] = None,
val_every: int = 100,
val_num_batches: int = 5,
learning_rate: float = 1e-4,
weight_decay: float = 0.,
max_grad_norm: Optional[float] = None,
ema_kwargs: dict = dict(),
scheduler: Optional[Type[_LRScheduler]] = None,
scheduler_kwargs: dict = dict(),
accelerator_kwargs: dict = dict(),
optimizer_kwargs: dict = dict(),
checkpoint_every = 1000,
checkpoint_folder = './checkpoints',
data_kwargs: Tuple[str, ...] = ['vertices', 'faces', 'face_edges'],
warmup_steps = 1000,
use_wandb_tracking = False
):
super().__init__()
# experiment tracker
self.use_wandb_tracking = use_wandb_tracking
if use_wandb_tracking:
accelerator_kwargs['log_with'] = 'wandb'
if 'kwargs_handlers' not in accelerator_kwargs:
accelerator_kwargs['kwargs_handlers'] = [DEFAULT_DDP_KWARGS]
# accelerator
self.accelerator = Accelerator(**accelerator_kwargs)
self.model = model
# if self.is_main:
# self.ema_model = EMA(model, **ema_kwargs)
self.optimizer = OptimizerWithWarmupSchedule(
accelerator = self.accelerator,
optimizer = get_adam_optimizer(model.parameters(), lr = learning_rate * warmup_steps, wd = weight_decay, **optimizer_kwargs),
scheduler = scheduler,
scheduler_kwargs = scheduler_kwargs,
warmup_steps = warmup_steps,
max_grad_norm = max_grad_norm
)
self.dataloader = DataLoader(
dataset,
batch_size = batch_size,
num_workers = 16,
shuffle = True,
drop_last = True,
collate_fn = partial(custom_collate, pad_id = model.pad_id)
)
self.should_validate = exists(val_dataset)
if self.should_validate:
assert len(val_dataset) > 0, 'your validation dataset is empty'
self.val_every = val_every
self.val_num_batches = val_num_batches
self.val_dataloader = DataLoader(
val_dataset,
batch_size = batch_size,
num_workers = 16,
shuffle = True,
drop_last = True,
collate_fn = partial(custom_collate, pad_id = model.pad_id)
)
if hasattr(dataset, 'data_kwargs') and exists(dataset.data_kwargs):
assert is_bearable(dataset.data_kwargs, List[str])
self.data_kwargs = dataset.data_kwargs
else:
self.data_kwargs = data_kwargs
(
self.model,
self.dataloader,
self.val_dataloader,
self.optimizer.optimizer,
self.optimizer.scheduler,
) = self.accelerator.prepare(
self.model,
self.dataloader,
self.val_dataloader,
self.optimizer.optimizer,
self.optimizer.scheduler,
)
self.grad_accum_every = grad_accum_every
self.num_train_steps = num_train_steps
self.register_buffer('step', torch.tensor(0))
self.checkpoint_every = checkpoint_every
self.checkpoint_folder = Path(checkpoint_folder)
self.checkpoint_folder.mkdir(exist_ok = True, parents = True)
def log(self, **data_kwargs):
self.accelerator.log(data_kwargs, step = self.step.item())
@property
def device(self):
return self.unwrapped_model.device
@property
def is_main(self):
return self.accelerator.is_main_process
@property
def unwrapped_model(self):
return self.accelerator.unwrap_model(self.model)
@property
def is_local_main(self):
return self.accelerator.is_local_main_process
def wait(self):
return self.accelerator.wait_for_everyone()
def print(self, msg):
return self.accelerator.print(msg)
def save(self, path, overwrite = True):
path = Path(path)
assert overwrite or not path.exists()
pkg = dict(
model = self.unwrapped_model.state_dict(),
# ema_model = self.ema_model.state_dict(),
optimizer = self.optimizer.state_dict(),
version = __version__,
step = self.step.item(),
config = self.unwrapped_model._config
)
torch.save(pkg, str(path))
def load(self, path):
path = Path(path)
assert path.exists()
pkg = torch.load(str(path))
if version.parse(__version__) != version.parse(pkg['version']):
self.print(f'loading saved mesh autoencoder at version {pkg["version"]}, but current package version is {__version__}')
self.model.load_state_dict(pkg['model'])
# self.ema_model.load_state_dict(pkg['ema_model'])
self.optimizer.load_state_dict(pkg['optimizer'])
self.step.copy_(pkg['step'])
def next_data_to_forward_kwargs(self, dl_iter) -> dict:
data = next(dl_iter)
if isinstance(data, tuple):
forward_kwargs = dict(zip(self.data_kwargs, data))
elif isinstance(data, dict):
forward_kwargs = data
maybe_del(forward_kwargs, 'texts', 'text_embeds')
return forward_kwargs
def forward(self):
step = self.step.item()
dl_iter = cycle(self.dataloader)
if self.is_main and self.should_validate:
val_dl_iter = cycle(self.val_dataloader)
while step < self.num_train_steps:
for i in range(self.grad_accum_every):
is_last = i == (self.grad_accum_every - 1)
maybe_no_sync = partial(self.accelerator.no_sync, self.model) if not is_last else nullcontext
forward_kwargs = self.next_data_to_forward_kwargs(dl_iter)
with self.accelerator.autocast(), maybe_no_sync():
total_loss, (recon_loss, commit_loss) = self.model(
**forward_kwargs,
return_loss_breakdown = True
)
self.accelerator.backward(total_loss / self.grad_accum_every)
self.print(f'step: {step} | recon loss: {recon_loss.item():.3f} | commit loss: {commit_loss.sum().item():.3f} | lr: {self.optimizer.optimizer.param_groups[0]["lr"]}')
self.log(
total_loss = total_loss.item(),
commit_loss = commit_loss.sum().item(),
recon_loss = recon_loss.item(),
lr = self.optimizer.optimizer.param_groups[0]['lr'],
)
self.optimizer.step()
self.optimizer.zero_grad()
step += 1
self.step.add_(1)
self.wait()
# if self.is_main:
# self.ema_model.update()
# self.wait()
if self.is_main and self.should_validate and divisible_by(step, self.val_every):
total_val_recon_loss = 0.
# self.ema_model.eval()
self.unwrapped_model.eval()
num_val_batches = self.val_num_batches * self.grad_accum_every
for _ in range(num_val_batches):
with self.accelerator.autocast(), torch.no_grad():
forward_kwargs = self.next_data_to_forward_kwargs(val_dl_iter)
# val_loss, (val_recon_loss, val_commit_loss) = self.ema_model(
val_loss, (val_recon_loss, val_commit_loss) = self.unwrapped_model(
**forward_kwargs,
return_loss_breakdown = True
)
total_val_recon_loss += (val_recon_loss / num_val_batches)
self.print(f'valid recon loss: {total_val_recon_loss:.3f}')
self.log(val_loss = total_val_recon_loss)
self.unwrapped_model.train()
self.wait()
if self.is_main and divisible_by(step, self.checkpoint_every):
checkpoint_num = step // self.checkpoint_every
self.save(self.checkpoint_folder / f'mesh-autoencoder.ckpt.{checkpoint_num}.pt')
self.wait()
self.print('training complete')
# mesh transformer trainer
@add_wandb_tracker_contextmanager()
class MeshTransformerTrainer(Module):
@beartype
def __init__(
self,
model,
dataset: Dataset,
num_train_steps: int,
batch_size: int,
grad_accum_every: int,
learning_rate: float = 2e-4,
weight_decay: float = 0.,
max_grad_norm: Optional[float] = 0.5,
val_dataset: Optional[Dataset] = None,
val_every = 1,
val_num_batches = 5,
scheduler: Optional[Type[_LRScheduler]] = None,
scheduler_kwargs: dict = dict(),
ema_kwargs: dict = dict(),
accelerator_kwargs: dict = dict(),
optimizer_kwargs: dict = dict(),
checkpoint_every = 1000,
checkpoint_folder = './checkpoints',
data_kwargs: Tuple[str, ...] = ['vertices', 'faces', 'face_edges', 'texts'],
warmup_steps = 1000,
use_wandb_tracking = False
):
super().__init__()
# experiment tracker
self.use_wandb_tracking = use_wandb_tracking
if use_wandb_tracking:
accelerator_kwargs['log_with'] = 'wandb'
if 'kwargs_handlers' not in accelerator_kwargs:
accelerator_kwargs['kwargs_handlers'] = [DEFAULT_DDP_KWARGS]
self.accelerator = Accelerator(**accelerator_kwargs)
self.model = model
optimizer = get_adam_optimizer(
model.parameters(),
lr = learning_rate * warmup_steps,
wd = weight_decay,
filter_by_requires_grad = True,
**optimizer_kwargs
)
self.optimizer = OptimizerWithWarmupSchedule(
accelerator = self.accelerator,
optimizer = optimizer,
scheduler = scheduler,
scheduler_kwargs = scheduler_kwargs,
warmup_steps = warmup_steps,
max_grad_norm = max_grad_norm
)
self.dataloader = DataLoader(
dataset,
batch_size = batch_size,
num_workers = 16,
shuffle = True,
drop_last = True,
collate_fn = partial(custom_collate, pad_id = model.pad_id)
)
self.should_validate = exists(val_dataset)
if self.should_validate:
assert len(val_dataset) > 0, 'your validation dataset is empty'
self.val_every = val_every
self.val_num_batches = val_num_batches
self.val_dataloader = DataLoader(
val_dataset,
batch_size = batch_size,
num_workers = 16,
shuffle = True,
drop_last = True,
collate_fn = partial(custom_collate, pad_id = model.pad_id)
)
if hasattr(dataset, 'data_kwargs') and exists(dataset.data_kwargs):
assert is_bearable(dataset.data_kwargs, List[str])
self.data_kwargs = dataset.data_kwargs
else:
self.data_kwargs = data_kwargs
(
self.model,
self.dataloader,
self.val_dataloader,
self.optimizer.optimizer,
self.optimizer.scheduler,
) = self.accelerator.prepare(
self.model,
self.dataloader,
self.val_dataloader,
self.optimizer.optimizer,
self.optimizer.scheduler,
)
self.grad_accum_every = grad_accum_every
self.num_train_steps = num_train_steps
self.register_buffer('step', torch.tensor(0))
self.checkpoint_every = checkpoint_every
self.checkpoint_folder = Path(checkpoint_folder)
self.checkpoint_folder.mkdir(exist_ok = True, parents = True)
def log(self, **data_kwargs):
self.accelerator.log(data_kwargs, step = self.step.item())
@property
def device(self):
return self.unwrapped_model.device
@property
def is_main(self):
return self.accelerator.is_main_process
@property
def unwrapped_model(self):
return self.accelerator.unwrap_model(self.model)
@property
def is_local_main(self):
return self.accelerator.is_local_main_process
def wait(self):
return self.accelerator.wait_for_everyone()
def print(self, msg):
return self.accelerator.print(msg)
def next_data_to_forward_kwargs(self, dl_iter) -> dict:
data = next(dl_iter)
if isinstance(data, tuple):
forward_kwargs = dict(zip(self.data_kwargs, data))
elif isinstance(data, dict):
forward_kwargs = data
return forward_kwargs
def save(self, path, overwrite = True):
path = Path(path)
assert overwrite or not path.exists()
pkg = dict(
model = self.unwrapped_model.state_dict(),
optimizer = self.optimizer.state_dict(),
step = self.step.item(),
version = __version__
)
torch.save(pkg, str(path))
def load(self, path):
path = Path(path)
assert path.exists()
pkg = torch.load(str(path))
if version.parse(__version__) != version.parse(pkg['version']):
self.print(f'loading saved mesh transformer at version {pkg["version"]}, but current package version is {__version__}')
self.model.load_state_dict(pkg['model'])
self.optimizer.load_state_dict(pkg['optimizer'])
self.step.copy_(pkg['step'])
def forward(self):
step = self.step.item()
dl_iter = cycle(self.dataloader)
if self.should_validate:
val_dl_iter = cycle(self.val_dataloader)
while step < self.num_train_steps:
for i in range(self.grad_accum_every):
is_last = i == (self.grad_accum_every - 1)
maybe_no_sync = partial(self.accelerator.no_sync, self.model) if not is_last else nullcontext
forward_kwargs = self.next_data_to_forward_kwargs(dl_iter)
with self.accelerator.autocast(), maybe_no_sync():
loss = self.model(**forward_kwargs)
self.accelerator.backward(loss / self.grad_accum_every)
self.print(f'step: {step} | loss: {loss.item():.3f} | lr: {self.optimizer.optimizer.param_groups[0]["lr"]}')
self.log(
loss = loss.item(),
lr = self.optimizer.optimizer.param_groups[0]['lr'],
)
self.optimizer.step()
self.optimizer.zero_grad()
step += 1
self.step.add_(1)
self.wait()
if self.is_main and self.should_validate and divisible_by(step, self.val_every):
total_val_loss = 0.
self.unwrapped_model.eval()
num_val_batches = self.val_num_batches * self.grad_accum_every
for _ in range(num_val_batches):
with self.accelerator.autocast(), torch.no_grad():
forward_kwargs = self.next_data_to_forward_kwargs(val_dl_iter)
val_loss = self.unwrapped_model(**forward_kwargs)
total_val_loss += (val_loss / num_val_batches)
self.print(f'valid recon loss: {total_val_loss:.3f}')
self.log(val_loss = total_val_loss)
self.unwrapped_model.train()
self.wait()
if self.is_main and divisible_by(step, self.checkpoint_every):
checkpoint_num = step // self.checkpoint_every
self.save(self.checkpoint_folder / f'mesh-transformer.ckpt.{checkpoint_num}.pt')
self.wait()
self.print('training complete')