forked from NUS-HPC-AI-Lab/VideoSys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
380 lines (335 loc) · 15.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
# Modified from Meta DiT: https://github.com/facebookresearch/DiT
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
import argparse
import json
import os
from glob import glob
import colossalai
import torch
import torch.distributed as dist
from colossalai.booster import Booster
from colossalai.booster.plugin import LowLevelZeroPlugin, TorchDDPPlugin
from colossalai.cluster import DistCoordinator
from colossalai.nn.optimizer import HybridAdam
from colossalai.utils import get_current_device
from diffusers.models import AutoencoderKL
from torch.utils.tensorboard import SummaryWriter
from torchvision.datasets import CIFAR10
from tqdm import tqdm
from opendit.diffusion import create_diffusion
from opendit.models.dit import DiT_models
from opendit.models.latte import Latte_models
from opendit.utils.ckpt_utils import create_logger, load, record_model_param_shape, save
from opendit.utils.data_utils import prepare_dataloader
from opendit.utils.operation import model_sharding
from opendit.utils.pg_utils import ProcessGroupManager
from opendit.utils.train_utils import all_reduce_mean, format_numel_str, get_model_numel, requires_grad, update_ema
from opendit.utils.video_utils import DatasetFromCSV, get_transforms_image, get_transforms_video
from opendit.vae.wrapper import AutoencoderKLWrapper
# the first flag below was False when we tested this script but True makes A100 training a lot faster:
torch.backends.cuda.matmul.allow_tf32 = True
torch.backends.cudnn.allow_tf32 = True
def main(args):
"""
Trains a new DiT model.
"""
assert torch.cuda.is_available(), "Training currently requires at least one GPU."
# ==============================
# Initialize Distributed Training
# ==============================
colossalai.launch_from_torch({})
coordinator = DistCoordinator()
device = get_current_device()
# ==============================
# Setup an experiment folder
# ==============================
# Make outputs folder (holds all experiment subfolders)
os.makedirs(args.outputs, exist_ok=True)
experiment_index = len(glob(f"{args.outputs}/*"))
# e.g., DiT-XL/2 --> DiT-XL-2 (for naming folders)
model_string_name = args.model.replace("/", "-")
# Create an experiment folder
experiment_dir = f"{args.outputs}/{experiment_index:03d}-{model_string_name}"
dist.barrier()
if coordinator.is_master():
os.makedirs(experiment_dir, exist_ok=True)
with open(f"{experiment_dir}/config.txt", "w") as f:
json.dump(args.__dict__, f, indent=4)
logger = create_logger(experiment_dir)
logger.info(f"Experiment directory created at {experiment_dir}")
else:
logger = create_logger(None)
# ==============================
# Initialize Tensorboard
# ==============================
if coordinator.is_master():
tensorboard_dir = f"{experiment_dir}/tensorboard"
os.makedirs(tensorboard_dir, exist_ok=True)
writer = SummaryWriter(tensorboard_dir)
# ==============================
# Initialize Booster
# ==============================
if args.plugin == "zero2":
plugin = LowLevelZeroPlugin(
stage=2,
precision=args.mixed_precision,
initial_scale=2**16,
max_norm=args.grad_clip,
)
elif args.plugin == "ddp":
plugin = TorchDDPPlugin()
else:
raise ValueError(f"Unknown plugin {args.plugin}")
booster = Booster(plugin=plugin)
# ==============================
# Initialize Process Group
# ==============================
sp_size = args.sequence_parallel_size
dp_size = dist.get_world_size() // sp_size
pg_manager = ProcessGroupManager(dp_size, sp_size, dp_axis=0, sp_axis=1)
# ======================================================
# Initialize Model, Objective, Optimizer
# ======================================================
# Create VAE encoder
vae = AutoencoderKL.from_pretrained(f"stabilityai/sd-vae-ft-{args.vae}").to(device)
# Configure input size
assert args.image_size % 8 == 0, "Image size must be divisible by 8 (for the VAE encoder)."
if args.use_video:
# Wrap the VAE in a wrapper that handles video data
# We use 2d vae from stableai instead of 3d vqvae from videogpt because it has better results
vae = AutoencoderKLWrapper(vae)
# Use 3d patch size that is divisible by the input size
input_size = (args.num_frames, args.image_size, args.image_size)
for i in range(3):
assert input_size[i] % vae.patch_size[i] == 0, "Input size must be divisible by patch size"
input_size = [input_size[i] // vae.patch_size[i] for i in range(3)]
else:
input_size = args.image_size // 8
# Set mixed precision
if args.mixed_precision == "bf16" and args.plugin != "ddp":
dtype = torch.bfloat16
elif args.mixed_precision == "fp16" and args.plugin != "ddp":
dtype = torch.float16
elif args.mixed_precision == "fp32" and args.plugin == "ddp":
dtype = torch.float32
else:
raise ValueError(f"Unknown mixed precision {args.mixed_precision}")
# Shared model config for two models
model_config = {
"input_size": input_size,
"num_classes": args.num_classes,
"enable_layernorm_kernel": args.enable_layernorm_kernel,
"enable_modulate_kernel": args.enable_modulate_kernel,
"sequence_parallel_size": args.sequence_parallel_size,
"sequence_parallel_type": args.sequence_parallel_type,
"text_encoder": args.text_encoder,
}
# Create DiT model
if "DiT" in args.model:
if "VDiT" in args.model:
assert args.use_video, "VDiT model requires video data"
else:
assert not args.use_video, "DiT model requires image data"
model_class = DiT_models[args.model]
elif "Latte" in args.model:
assert args.use_video, "Latte model requires video data"
model_class = Latte_models[args.model]
else:
raise ValueError(f"Unknown model {args.model}")
model = (
model_class(
enable_flashattn=args.enable_flashattn,
sequence_parallel_group=pg_manager.sp_group,
dtype=dtype,
**model_config,
)
.to(device)
.to(dtype)
)
model_numel = get_model_numel(model)
logger.info(f"Model params: {format_numel_str(model_numel)}")
if args.grad_checkpoint:
model.enable_gradient_checkpointing()
# Create ema and vae model
# Note that parameter initialization is done within the DiT constructor
# Create an EMA of the model for use after training
ema = model_class(**model_config).to(device)
ema = ema.to(torch.float32)
ema.load_state_dict(model.state_dict())
requires_grad(ema, False)
ema_shape_dict = record_model_param_shape(ema)
# Create diffusion
# default: 1000 steps, linear noise schedule
diffusion = create_diffusion(timestep_respacing="")
# Setup optimizer
# We used default Adam betas=(0.9, 0.999) and a constant learning rate of 1e-4 in our paper
optimizer = HybridAdam(
filter(lambda p: p.requires_grad, model.parameters()), lr=args.lr, weight_decay=0, adamw_mode=True
)
# You can use a lr scheduler if you want
# Recommend if you continue training from a model
lr_scheduler = None
# Prepare models for training
# Ensure EMA is initialized with synced weights
update_ema(ema, model, decay=0, sharded=False)
# important! This enables embedding dropout for classifier-free guidance
model.train()
# EMA model should always be in eval mode
ema.eval()
# Setup data:
if args.use_video:
dataset = DatasetFromCSV(
args.data_path,
transform=get_transforms_video(args.image_size),
num_frames=args.num_frames,
frame_interval=args.frame_interval,
)
else:
# master process goes first
if not coordinator.is_master():
dist.barrier()
dataset = CIFAR10(args.data_path, transform=get_transforms_image(args.image_size), download=True)
if coordinator.is_master():
dist.barrier()
dataloader = prepare_dataloader(
dataset,
batch_size=args.batch_size,
shuffle=True,
drop_last=True,
pin_memory=True,
num_workers=args.num_workers,
pg_manager=pg_manager,
)
logger.info(f"Dataset contains {len(dataset):,} images ({args.data_path})")
# Boost model for distributed training
torch.set_default_dtype(dtype)
model, optimizer, _, dataloader, lr_scheduler = booster.boost(
model=model, optimizer=optimizer, lr_scheduler=lr_scheduler, dataloader=dataloader
)
torch.set_default_dtype(torch.float)
logger.info("Boost model for distributed training")
# Variables for monitoring/logging purposes:
start_epoch = 0
start_step = 0
sampler_start_idx = 0
if args.load is not None:
logger.info("Loading checkpoint")
start_epoch, start_step, sampler_start_idx = load(
booster, model, ema, optimizer, lr_scheduler, args.load, args.sequence_parallel_type
)
logger.info(f"Loaded checkpoint {args.load} at epoch {start_epoch} step {start_step}")
# Only shard ema model when using zero2 plugin
shard_ema = True if args.plugin == "zero2" else False
if shard_ema:
model_sharding(ema)
num_steps_per_epoch = len(dataloader)
logger.info(f"Training for {args.epochs} epochs...")
# if resume training, set the sampler start index to the correct value
dataloader.sampler.set_start_index(sampler_start_idx)
for epoch in range(start_epoch, args.epochs):
dataloader.sampler.set_epoch(epoch)
dataloader_iter = iter(dataloader)
logger.info(f"Beginning epoch {epoch}...")
with tqdm(
range(start_step, num_steps_per_epoch),
desc=f"Epoch {epoch}",
disable=not coordinator.is_master(),
total=num_steps_per_epoch,
initial=start_step,
) as pbar:
for step in pbar:
if args.use_video:
batch = next(dataloader_iter)
x = batch["video"].to(device)
y = batch["text"]
else:
x, y = next(dataloader_iter)
x = x.to(device)
y = y.to(device)
# VAE encode
with torch.no_grad():
# Map input images to latent space + normalize latents:
x = vae.encode(x)
if not args.use_video:
x = x.latent_dist.sample().mul_(0.18215)
# Diffusion
t = torch.randint(0, diffusion.num_timesteps, (x.shape[0],), device=device)
model_kwargs = dict(y=y)
loss_dict = diffusion.training_losses(model, x, t, model_kwargs)
loss = loss_dict["loss"].mean()
booster.backward(loss=loss, optimizer=optimizer)
optimizer.step()
optimizer.zero_grad()
# Update EMA
update_ema(ema, model.module, optimizer=optimizer, sharded=shard_ema)
# Log loss values:
all_reduce_mean(loss)
global_step = epoch * num_steps_per_epoch + step
pbar.set_postfix({"loss": loss.item(), "step": step, "global_step": global_step})
# Log to tensorboard
if coordinator.is_master() and (global_step + 1) % args.log_every == 0:
writer.add_scalar("loss", loss.item(), global_step)
# Save checkpoint
if args.ckpt_every > 0 and (global_step + 1) % args.ckpt_every == 0:
logger.info(f"Saving checkpoint...")
save(
booster,
model,
ema,
optimizer,
lr_scheduler,
epoch,
step + 1,
global_step + 1,
args.batch_size,
coordinator,
experiment_dir,
ema_shape_dict,
args.sequence_parallel_type,
shard_ema,
)
logger.info(
f"Saved checkpoint at epoch {epoch} step {step + 1} global_step {global_step + 1} to {experiment_dir}"
)
# the continue epochs are not resumed, so we need to reset the sampler start index and start step
dataloader.sampler.set_start_index(0)
start_step = 0
model.eval() # important! This disables randomized embedding dropout
# do any sampling/FID calculation/etc. with ema (or model) in eval mode ...
logger.info("Done!")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--model", type=str, choices=list(DiT_models.keys()) + list(Latte_models.keys()), default="DiT-XL/2"
)
parser.add_argument("--vae", type=str, choices=["ema", "mse"], default="ema") # Choice doesn't affect training
parser.add_argument("--use_video", action="store_true", help="Use video data instead of images")
parser.add_argument("--plugin", type=str, default="zero2")
parser.add_argument("--outputs", type=str, default="./outputs", help="Path to the output directory")
parser.add_argument("--load", type=str, default=None, help="Path to a checkpoint dir to load")
parser.add_argument("--num_frames", type=int, default=16)
parser.add_argument("--frame_interval", type=int, default=1)
parser.add_argument("--text_encoder", type=str, default="openai/clip-vit-base-patch32")
parser.add_argument("--data_path", type=str, default="./datasets", help="Path to the dataset")
parser.add_argument("--image_size", type=int, choices=[256, 512], default=256)
parser.add_argument("--num_classes", type=int, default=1000)
parser.add_argument("--epochs", type=int, default=1400)
parser.add_argument("--batch_size", type=int, default=2)
parser.add_argument("--global_seed", type=int, default=42)
parser.add_argument("--num_workers", type=int, default=4)
parser.add_argument("--log_every", type=int, default=10)
parser.add_argument("--ckpt_every", type=int, default=1000)
parser.add_argument("--mixed_precision", type=str, default="bf16", choices=["bf16", "fp16", "fp32"])
parser.add_argument("--grad_clip", type=float, default=1.0, help="Gradient clipping value")
parser.add_argument("--lr", type=float, default=1e-4, help="Gradient clipping value")
parser.add_argument("--grad_checkpoint", action="store_true", help="Use gradient checkpointing")
parser.add_argument("--enable_modulate_kernel", action="store_true", help="Enable triton modulate kernel")
parser.add_argument("--enable_layernorm_kernel", action="store_true", help="Enable apex layernorm kernel")
parser.add_argument("--enable_flashattn", action="store_true", help="Enable flashattn kernel")
parser.add_argument("--sequence_parallel_size", type=int, default=1, help="Sequence parallel size, enable if > 1")
parser.add_argument("--sequence_parallel_type", type=str)
args = parser.parse_args()
main(args)