-
Notifications
You must be signed in to change notification settings - Fork 11
/
train.py
485 lines (412 loc) · 20.4 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
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
import argparse
import os
from datetime import datetime, timezone
import shutil
import glob
import time
import random
import json
import inspect
import toml
import deepspeed
from deepspeed import comm as dist
from deepspeed.runtime.pipe import module as ds_pipe_module
import torch
from torch import nn
from torch.utils.tensorboard import SummaryWriter
from tqdm import tqdm
import multiprocess as mp
import numpy as np
from utils import dataset as dataset_util
from utils import common
from utils.common import is_main_process, get_rank, DTYPE_MAP
import utils.saver
from utils.isolate_rng import isolate_rng
from utils.patches import apply_patches
TIMESTEP_QUANTILES_FOR_EVAL = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
parser = argparse.ArgumentParser()
parser.add_argument('--config', help='Path to TOML configuration file.')
parser.add_argument('--local_rank', type=int, default=-1,
help='local rank passed from distributed launcher')
parser.add_argument('--resume_from_checkpoint', action='store_true', default=None, help='resume training from the most recent checkpoint')
parser.add_argument('--regenerate_cache', action='store_true', default=None, help='Force regenerate cache. Useful if none of the files have changed but their contents have, e.g. modified captions.')
parser.add_argument('--cache_only', action='store_true', default=None, help='Cache model inputs then exit.')
parser = deepspeed.add_config_arguments(parser)
args = parser.parse_args()
# Monkeypatch this so it counts all layer parameters, not just trainable parameters.
# This helps it divide the layers between GPUs more evenly when training a LoRA.
def _count_all_layer_params(self):
param_counts = [0] * len(self._layer_specs)
for idx, layer in enumerate(self._layer_specs):
if isinstance(layer, ds_pipe_module.LayerSpec):
l = layer.build()
param_counts[idx] = sum(p.numel() for p in l.parameters())
elif isinstance(layer, nn.Module):
param_counts[idx] = sum(p.numel() for p in layer.parameters())
return param_counts
ds_pipe_module.PipelineModule._count_layer_params = _count_all_layer_params
def set_config_defaults(config):
# Force the user to set this. If we made it a default of 1, it might use a lot of disk space.
assert 'save_every_n_epochs' in config
config.setdefault('pipeline_stages', 1)
config.setdefault('activation_checkpointing', False)
config.setdefault('warmup_steps', 0)
if 'save_dtype' in config:
config['save_dtype'] = DTYPE_MAP[config['save_dtype']]
model_config = config['model']
model_dtype_str = model_config['dtype']
model_config['dtype'] = DTYPE_MAP[model_dtype_str]
if 'transformer_dtype' in model_config:
model_config['transformer_dtype'] = DTYPE_MAP[model_config['transformer_dtype']]
model_config.setdefault('guidance', 1.0)
if 'adapter' in config:
adapter_config = config['adapter']
adapter_type = adapter_config['type']
if adapter_config['type'] == 'lora':
if 'alpha' in adapter_config:
raise NotImplementedError(
'This script forces alpha=rank to make the saved LoRA format simpler and more predictable with downstream inference programs. Please remove alpha from the config.'
)
adapter_config['alpha'] = adapter_config['rank']
adapter_config.setdefault('dropout', 0.0)
adapter_config.setdefault('dtype', model_dtype_str)
adapter_config['dtype'] = DTYPE_MAP[adapter_config['dtype']]
else:
raise NotImplementedError(f'Adapter type {adapter_type} is not implemented')
config.setdefault('logging_steps', 1)
config.setdefault('eval_datasets', [])
config.setdefault('eval_gradient_accumulation_steps', 1)
config.setdefault('eval_every_n_steps', None)
config.setdefault('eval_every_n_epochs', None)
config.setdefault('eval_before_first_step', True)
def get_most_recent_run_dir(output_dir):
return list(sorted(glob.glob(os.path.join(output_dir, '*'))))[-1]
def print_model_info(model):
if not is_main_process():
return
print(model)
for name, module in model.named_modules():
print(f'{type(module)}: {name}')
for pname, p in module.named_parameters(recurse=False):
print(pname)
print(p.dtype)
print(p.device)
print(p.requires_grad)
print()
def evaluate_single(model_engine, eval_dataloader, eval_gradient_accumulation_steps, quantile, pbar=None):
eval_dataloader.set_eval_quantile(quantile)
orig_micro_batches = model_engine.micro_batches
model_engine.micro_batches = eval_gradient_accumulation_steps
iterator = iter(eval_dataloader)
total_loss = 0
count = 0
while True:
model_engine.reset_activation_shape()
loss = model_engine.eval_batch(iterator).item()
eval_dataloader.sync_epoch()
if pbar:
pbar.update(1)
total_loss += loss
count += 1
if eval_dataloader.epoch == 2:
break
eval_dataloader.reset()
model_engine.micro_batches = orig_micro_batches
return total_loss / count
def _evaluate(model_engine, eval_dataloaders, tb_writer, step, eval_gradient_accumulation_steps):
pbar_total = 0
for eval_dataloader in eval_dataloaders.values():
pbar_total += len(eval_dataloader) * len(TIMESTEP_QUANTILES_FOR_EVAL) // eval_gradient_accumulation_steps
if is_main_process():
print('Running eval')
pbar = tqdm(total=pbar_total)
else:
pbar = None
start = time.time()
for name, eval_dataloader in eval_dataloaders.items():
losses = []
for quantile in TIMESTEP_QUANTILES_FOR_EVAL:
loss = evaluate_single(model_engine, eval_dataloader, eval_gradient_accumulation_steps, quantile, pbar=pbar)
losses.append(loss)
if is_main_process():
tb_writer.add_scalar(f'{name}/loss_quantile_{quantile:.2f}', loss, step)
avg_loss = sum(losses) / len(losses)
if is_main_process():
tb_writer.add_scalar(f'{name}/loss', avg_loss, step)
duration = time.time() - start
if is_main_process():
tb_writer.add_scalar('eval/eval_time_sec', duration, step)
pbar.close()
def evaluate(model_engine, eval_dataloaders, tb_writer, step, eval_gradient_accumulation_steps):
if len(eval_dataloaders) == 0:
return
with torch.no_grad(), isolate_rng():
seed = get_rank()
random.seed(seed)
torch.manual_seed(seed)
np.random.seed(seed)
_evaluate(model_engine, eval_dataloaders, tb_writer, step, eval_gradient_accumulation_steps)
if __name__ == '__main__':
apply_patches()
# needed for broadcasting Queue in dataset.py
mp.current_process().authkey = b'afsaskgfdjh4'
with open(args.config) as f:
# Inline TOML tables are not pickleable, which messes up the multiprocessing dataset stuff. This is a workaround.
config = json.loads(json.dumps(toml.load(f)))
set_config_defaults(config)
common.AUTOCAST_DTYPE = config['model']['dtype']
resume_from_checkpoint = (
args.resume_from_checkpoint if args.resume_from_checkpoint is not None
else config.get('resume_from_checkpoint', False)
)
regenerate_cache = (
args.regenerate_cache if args.regenerate_cache is not None
else config.get('regenerate_cache', False)
)
deepspeed.init_distributed()
# needed for broadcasting Queue in dataset.py (because we haven't called deepspeed.initialize() yet?)
torch.cuda.set_device(dist.get_rank())
model_type = config['model']['type']
if model_type == 'flux':
from models import flux
model = flux.FluxPipeline(config)
elif model_type == 'ltx-video':
from models import ltx_video
model = ltx_video.LTXVideoPipeline(config)
elif model_type == 'hunyuan-video':
from models import hunyuan_video
model = hunyuan_video.HunyuanVideoPipeline(config)
else:
raise NotImplementedError(f'Model type {model_type} is not implemented')
# import sys, PIL
# test_image = sys.argv[1]
# with torch.no_grad():
# vae = model.get_vae().to('cuda')
# latents = dataset.encode_pil_to_latents(PIL.Image.open(test_image), vae)
# pil_image = dataset.decode_latents_to_pil(latents, vae)
# pil_image.save('test.jpg')
# quit()
with open(config['dataset']) as f:
dataset_config = toml.load(f)
ds_config = {
'train_micro_batch_size_per_gpu': config.get('micro_batch_size_per_gpu', 1),
'gradient_accumulation_steps': config.get('gradient_accumulation_steps', 1),
'gradient_clipping': config.get('gradient_clipping', 1.0),
'steps_per_print': config.get('steps_per_print', 1),
}
caching_batch_size = config.get('caching_batch_size', 1)
dataset_manager = dataset_util.DatasetManager(model, regenerate_cache=regenerate_cache, caching_batch_size=caching_batch_size)
train_data = dataset_util.Dataset(dataset_config, model.name)
dataset_manager.register(train_data)
eval_data_map = {}
for i, eval_dataset in enumerate(config['eval_datasets']):
if type(eval_dataset) == str:
name = f'eval{i}'
config_path = eval_dataset
else:
name = eval_dataset['name']
config_path = eval_dataset['config']
with open(config_path) as f:
eval_dataset_config = toml.load(f)
eval_data_map[name] = dataset_util.Dataset(eval_dataset_config, model.name)
dataset_manager.register(eval_data_map[name])
dataset_manager.cache()
if args.cache_only:
quit()
model.load_diffusion_model()
if adapter_config := config.get('adapter', None):
peft_config = model.configure_adapter(adapter_config)
if init_from_existing := adapter_config.get('init_from_existing', None):
model.load_adapter_weights(init_from_existing)
else:
peft_config = None
# if this is a new run, create a new dir for it
if not resume_from_checkpoint and is_main_process():
run_dir = os.path.join(config['output_dir'], datetime.now(timezone.utc).strftime('%Y%m%d_%H-%M-%S'))
os.makedirs(run_dir, exist_ok=True)
shutil.copy(args.config, run_dir)
# wait for all processes then get the most recent dir (may have just been created)
dist.barrier()
run_dir = get_most_recent_run_dir(config['output_dir'])
layers = model.to_layers()
additional_pipeline_module_kwargs = {}
if config['activation_checkpointing']:
checkpoint_func = deepspeed.checkpointing.checkpoint
additional_pipeline_module_kwargs.update({
'activation_checkpoint_interval': 1,
'checkpointable_layers': model.checkpointable_layers,
'activation_checkpoint_func': checkpoint_func,
})
pipeline_model = deepspeed.pipe.PipelineModule(
layers=layers,
num_stages=config['pipeline_stages'],
partition_method=config.get('partition_method', 'parameters'),
**additional_pipeline_module_kwargs
)
parameters_to_train = [p for p in pipeline_model.parameters() if p.requires_grad]
def get_optimizer(model_parameters):
optim_config = config['optimizer']
optim_type = optim_config['type'].lower()
args = []
kwargs = {k: v for k, v in optim_config.items() if k not in ['type', 'gradient_release']}
if optim_type == 'adamw':
# TODO: fix this. I'm getting "fatal error: cuda_runtime.h: No such file or directory"
# when Deepspeed tries to build the fused Adam extension.
# klass = deepspeed.ops.adam.FusedAdam
klass = torch.optim.AdamW
elif optim_type == 'adamw8bit':
import bitsandbytes
klass = bitsandbytes.optim.AdamW8bit
elif optim_type == 'adamw_optimi':
import optimi
klass = optimi.AdamW
elif optim_type == 'stableadamw':
import optimi
klass = optimi.StableAdamW
elif optim_type == 'sgd':
klass = torch.optim.SGD
elif optim_type == 'adamw8bitkahan':
from optimizers import adamw_8bit
klass = adamw_8bit.AdamW8bitKahan
elif optim_type == 'offload':
from torchao.prototype.low_bit_optim import CPUOffloadOptimizer
klass = CPUOffloadOptimizer
args.append(torch.optim.AdamW)
kwargs['fused'] = True
else:
raise NotImplementedError(optim_type)
if optim_config.get('gradient_release', False):
# Prevent deepspeed from logging every single param group lr
def _report_progress(self, step):
lr = self.get_lr()
mom = self.get_mom()
deepspeed.utils.logging.log_dist(f"step={step}, skipped={self.skipped_steps}, lr={lr[0]}, mom={mom[0]}", ranks=[0])
deepspeed.runtime.engine.DeepSpeedEngine._report_progress = _report_progress
# Deepspeed executes all the code to reduce grads across data parallel ranks even if the DP world size is 1.
# As part of this, any grads that are None are set to zeros. We're doing gradient release to save memory,
# so we have to avoid this.
def _exec_reduce_grads(self):
assert self.mpu.get_data_parallel_world_size() == 1, 'Data parallel world size must be 1. Make sure pipeline_stages = num_gpus.'
return
deepspeed.runtime.pipe.engine.PipelineEngine._INSTRUCTION_MAP[deepspeed.runtime.pipe.schedule.ReduceGrads] = _exec_reduce_grads
# When pipelining multiple forward and backward passes, normally updating the parameter in-place causes an error when calling
# backward() on future micro-batches. But we can modify .data directly so the autograd engine doesn't detect in-place modifications.
# TODO: this is unbelievably hacky and not mathematically sound, I'm just seeing if it works at all.
def add_(self, *args, **kwargs):
self.data.add_(*args, **kwargs)
for p in model_parameters:
p.add_ = add_.__get__(p)
if 'foreach' in inspect.signature(klass).parameters:
kwargs['foreach'] = False
# We're doing an optimizer step for each micro-batch. Scale momentum and EMA betas so that the contribution
# decays at the same rate it would if we were doing one step per batch like normal.
# Reference: https://alexeytochin.github.io/posts/batch_size_vs_momentum/batch_size_vs_momentum.html
gas = ds_config['gradient_accumulation_steps']
if 'betas' in kwargs:
for i in range(len(kwargs['betas'])):
kwargs['betas'][i] = kwargs['betas'][i] ** (1/gas)
if 'momentum' in kwargs:
kwargs['momentum'] = kwargs['momentum'] ** (1/gas)
optimizer_dict = {p: klass([p], **kwargs) for p in model_parameters}
def optimizer_hook(p):
optimizer_dict[p].step()
optimizer_dict[p].zero_grad()
for p in model_parameters:
p.register_post_accumulate_grad_hook(optimizer_hook)
from optimizers import gradient_release
return gradient_release.GradientReleaseOptimizerWrapper(list(optimizer_dict.values()))
else:
return klass(model_parameters, *args, **kwargs)
model_engine, optimizer, _, _ = deepspeed.initialize(
args=args,
model=pipeline_model,
model_parameters=parameters_to_train,
optimizer=get_optimizer,
config=ds_config,
)
lr_scheduler = torch.optim.lr_scheduler.ConstantLR(optimizer, factor=1.0)
if config['warmup_steps'] > 0:
warmup_steps = config['warmup_steps']
warmup_scheduler = torch.optim.lr_scheduler.LinearLR(optimizer, start_factor=1/warmup_steps, total_iters=warmup_steps)
lr_scheduler = torch.optim.lr_scheduler.SequentialLR(optimizer, schedulers=[warmup_scheduler, lr_scheduler], milestones=[warmup_steps])
model_engine.lr_scheduler = lr_scheduler
train_data.post_init(
model_engine.grid.get_data_parallel_rank(),
model_engine.grid.get_data_parallel_world_size(),
model_engine.train_micro_batch_size_per_gpu(),
model_engine.gradient_accumulation_steps(),
)
for eval_data in eval_data_map.values():
eval_data.post_init(
model_engine.grid.get_data_parallel_rank(),
model_engine.grid.get_data_parallel_world_size(),
config.get('eval_micro_batch_size_per_gpu', model_engine.train_micro_batch_size_per_gpu()),
config['eval_gradient_accumulation_steps'],
)
# Might be useful because we set things in fp16 / bf16 without explicitly enabling Deepspeed fp16 mode.
# Unsure if really needed.
communication_data_type = config['lora']['dtype'] if 'lora' in config else config['model']['dtype']
model_engine.communication_data_type = communication_data_type
train_dataloader = dataset_util.PipelineDataLoader(train_data, model_engine.gradient_accumulation_steps(), model)
step = 1
# make sure to do this before calling model_engine.set_dataloader(), as that method creates an iterator
# which starts creating dataloader internal state
if resume_from_checkpoint:
load_path, client_state = model_engine.load_checkpoint(
run_dir,
load_module_strict=False,
load_lr_scheduler_states='force_constant_lr' not in config,
)
dist.barrier() # just so the print below doesn't get swamped
assert load_path is not None
train_dataloader.load_state_dict(client_state['custom_loader'])
step = client_state['step'] + 1
del client_state
if is_main_process():
print(f'Resuming training from checkpoint. Resuming at epoch: {train_dataloader.epoch}, step: {step}')
if 'force_constant_lr' in config:
model_engine.lr_scheduler = torch.optim.lr_scheduler.ConstantLR(optimizer, factor=1.0)
for pg in optimizer.param_groups:
pg['lr'] = config['force_constant_lr']
model_engine.set_dataloader(train_dataloader)
steps_per_epoch = len(train_dataloader) // model_engine.gradient_accumulation_steps()
model_engine.total_steps = steps_per_epoch * config['epochs']
eval_dataloaders = {
# Set num_dataloader_workers=0 so dataset iteration is completely deterministic.
# We want the exact same noise for each image, each time, for a stable validation loss.
name: dataset_util.PipelineDataLoader(eval_data, config['eval_gradient_accumulation_steps'], model, num_dataloader_workers=0)
for name, eval_data in eval_data_map.items()
}
epoch = train_dataloader.epoch
tb_writer = SummaryWriter(log_dir=run_dir) if is_main_process() else None
saver = utils.saver.Saver(args, config, peft_config, run_dir, model, train_dataloader, model_engine, pipeline_model)
if config['eval_before_first_step'] and not resume_from_checkpoint:
evaluate(model_engine, eval_dataloaders, tb_writer, 0, config['eval_gradient_accumulation_steps'])
# TODO: this is state we need to save and resume when resuming from checkpoint. It only affects logging.
epoch_loss = 0
num_steps = 0
while True:
#empty_cuda_cache()
model_engine.reset_activation_shape()
loss = model_engine.train_batch().item()
epoch_loss += loss
num_steps += 1
train_dataloader.sync_epoch()
new_epoch = saver.process_epoch(epoch, step)
finished_epoch = True if new_epoch != epoch else False
if is_main_process() and step % config['logging_steps'] == 0:
tb_writer.add_scalar(f'train/loss', loss, step)
if (config['eval_every_n_steps'] and step % config['eval_every_n_steps'] == 0) or (finished_epoch and config['eval_every_n_epochs'] and epoch % config['eval_every_n_epochs'] == 0):
evaluate(model_engine, eval_dataloaders, tb_writer, step, config['eval_gradient_accumulation_steps'])
if finished_epoch:
if is_main_process():
tb_writer.add_scalar(f'train/epoch_loss', epoch_loss/num_steps, epoch)
epoch_loss = 0
num_steps = 0
epoch = new_epoch
if epoch is None:
break
saver.process_step(step)
step += 1
if is_main_process():
print('TRAINING COMPLETE!')