forked from dome272/Paella
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paella.py
396 lines (347 loc) · 14.8 KB
/
paella.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
import math
import os
import torch
from torch.utils.data import TensorDataset, DataLoader
import wandb
from torch import nn, optim
import torchvision
from tqdm import tqdm
import time
import numpy as np
import torch.multiprocessing as mp
import torch.distributed as dist
from torch.nn.parallel import DistributedDataParallel
from modules import DenoiseUNet
from utils import get_dataloader, sample, encode, decode
from t5 import FrozenT5Embedder
import open_clip
from open_clip import tokenizer
from rudalle import get_vae
def generate_clip_embeddings(model, text_tokens) -> torch.Tensor:
'''
Get the CLIP embedding before feature extraction/normalization.
TODO Alter the unet to use this instead of the final squished embedding.
'''
cast_dtype = model.transformer.get_cast_dtype()
x = model.token_embedding(text_tokens).to(cast_dtype) # [batch_size, n_ctx, d_model]
x = x + model.positional_embedding.to(cast_dtype)
x = x.permute(1, 0, 2) # NLD -> LND
x = model.transformer(x, attn_mask=model.attn_mask)
x = x.permute(1, 0, 2) # LND -> NLD
x = model.ln_final(x) # [batch_size, n_ctx, transformer.width]
return x
def train(proc_id, args):
if os.path.exists(f"results/{args.run_name}/log.pt"):
resume = True
else:
resume = False
if not proc_id and args.node_id == 0:
if resume:
wandb.init(
project="project",
name=args.run_name,
entity="your_entity",
config=vars(args),
)
else:
wandb.init(
project="project",
name=args.run_name,
entity="your_entity",
config=vars(args),
)
print(f"Starting run '{args.run_name}'....")
print(
f"Batch Size check: {args.n_nodes * args.batch_size * args.accum_grad * len(args.devices)}"
)
parallel = len(args.devices) > 1
device = torch.device(proc_id)
vqmodel = get_vae().to(device)
vqmodel.eval().requires_grad_(False)
if parallel:
torch.cuda.set_device(proc_id)
torch.backends.cudnn.benchmark = True
dist.init_process_group(
backend="nccl",
init_method="file://dist_file",
world_size=args.n_nodes * len(args.devices),
rank=proc_id + len(args.devices) * args.node_id,
)
torch.set_num_threads(6)
model = DenoiseUNet(num_labels=args.num_codebook_vectors, c_clip=2048).to(device)
if not proc_id and args.node_id == 0:
print(f"Number of Parameters: {sum([p.numel() for p in model.parameters()])}")
clip_model, _, _ = open_clip.create_model_and_transforms(
"ViT-H-14", pretrained="laion2b_s32b_b79k",
)
del clip_model.visual
clip_model = clip_model.to(device).eval().requires_grad_(False)
t5_model = FrozenT5Embedder(device=device).to(device)
lr = 3e-4
dataset = get_dataloader(args)
optimizer = optim.AdamW(model.parameters(), lr=lr)
criterion = nn.CrossEntropyLoss(label_smoothing=0.1)
if not proc_id and args.node_id == 0:
wandb.watch(model)
os.makedirs(f"results/{args.run_name}", exist_ok=True)
os.makedirs(f"models/{args.run_name}", exist_ok=True)
scheduler = optim.lr_scheduler.OneCycleLR(
optimizer,
total_steps=args.total_steps,
max_lr=lr,
pct_start=0.1 if not args.finetune else 0.0,
div_factor=25,
final_div_factor=1 / 25,
anneal_strategy="linear",
)
if resume:
if not proc_id and args.node_id == 0:
print("Loading last checkpoint....")
logs = torch.load(f"results/{args.run_name}/log.pt")
start_step = logs["step"] + 1
losses = logs["losses"]
accuracies = logs["accuracies"]
total_loss, total_acc = losses[-1] * start_step, accuracies[-1] * start_step
model.load_state_dict(
torch.load(f"models/{args.run_name}/model.pt", map_location=device)
)
if not proc_id and args.node_id == 0:
print("Loaded model.")
opt_state = torch.load(f"models/{args.run_name}/optim.pt", map_location=device)
last_lr = opt_state["param_groups"][0]["lr"]
with torch.no_grad():
for _ in range(logs["step"]):
scheduler.step()
if not proc_id and args.node_id == 0:
print(f"Initialized scheduler")
print(
f"Sanity check => Last-LR: {last_lr} == Current-LR: {optimizer.param_groups[0]['lr']} -> {last_lr == optimizer.param_groups[0]['lr']}"
)
optimizer.load_state_dict(opt_state)
del opt_state
else:
losses = []
accuracies = []
start_step, total_loss, total_acc = 0, 0, 0
if parallel:
model = DistributedDataParallel(
model, device_ids=[device], output_device=device
)
# pbar = tqdm(
# enumerate(dataset, start=start_step),
# total=args.total_steps,
# initial=start_step) \
# if args.node_id == 0 and proc_id == 0 \
# else enumerate(dataset, start=start_step)
model.train()
# iterator = enumerate(dataset, start=start_step)
pbar = tqdm(total=args.total_steps)
batch_iterator = iter(dataset)
step = 0
while step < args.total_steps:
try:
images, captions = next(batch_iterator)
except StopIteration:
print("hit stop iteration")
batch_iterator = iter(dataset)
images, captions = next(batch_iterator)
except Exception as e:
import traceback
traceback.print_exc()
continue
images = images.to(device)
with torch.no_grad():
image_indices = encode(vqmodel, images)
r = torch.rand(images.size(0), device=device)
noised_indices, mask = model.add_noise(image_indices, r)
if (
np.random.rand() < 0.1
): # 10% of the times -> unconditional training for classifier-free-guidance
text_embeddings = images.new_zeros(images.size(0), 2048)
text_embeddings_full = images.new_zeros(images.size(0), 77, 2048)
# text_embeddings = images.new_zeros(images.size(0), 77, 1024)
else:
text_tokens = tokenizer.tokenize(captions)
text_tokens = text_tokens.to(device)
clip_embeddings = clip_model.encode_text(text_tokens).float().to(device)
clip_embeddings_full = generate_clip_embeddings(clip_model, text_tokens).float().to(device)
t5_embeddings_full = t5_model(captions).to(device)
text_embeddings = torch.cat([clip_embeddings, torch.mean(t5_embeddings_full, dim=1)], 1)
text_embeddings_full = torch.cat([clip_embeddings_full, t5_embeddings_full], 2)
pred = model(noised_indices, text_embeddings, r, text_embeddings_full)
loss = criterion(pred, image_indices)
loss_adjusted = loss / args.accum_grad
loss_adjusted.backward()
grad_norm = torch.nn.utils.clip_grad_norm_(model.parameters(), 5).item()
if (step + 1) % args.accum_grad == 0:
optimizer.step()
scheduler.step()
optimizer.zero_grad()
acc = (pred.argmax(1) == image_indices).float()
acc = acc.mean()
total_loss += loss.item()
total_acc += acc.item()
if not proc_id and args.node_id == 0:
log = {
"loss": total_loss / (step + 1),
"acc": total_acc / (step + 1),
"curr_loss": loss.item(),
"curr_acc": acc.item(),
"ppx": np.exp(total_loss / (step + 1)),
"lr": optimizer.param_groups[0]["lr"],
"grad_norm": grad_norm,
}
pbar.set_postfix(log)
wandb.log(log)
if args.node_id == 0 and proc_id == 0 and step % args.log_period == 0:
print(
f"Step {step} - loss {total_loss / (step + 1)} - acc {total_acc / (step + 1)} - ppx {np.exp(total_loss / (step + 1))}"
)
losses.append(total_loss / (step + 1))
accuracies.append(total_acc / (step + 1))
model.eval()
with torch.no_grad():
n = 1
images = images[:10]
image_indices = image_indices[:10]
captions = captions[:10]
text_embeddings = text_embeddings[:10]
sampled = sample(model, c=text_embeddings,
c_full=text_embeddings_full) # [-1]
sampled = decode(vqmodel, sampled)
recon_images = decode(vqmodel, image_indices)
if args.log_captions:
# cool_captions_data = torch.load("cool_captions.pth")
# cool_captions_text = cool_captions_data["captions"]
cool_captions_text = [
"a furry cat",
"a red ball",
"a horse",
"a river bank at sunset",
]
text_tokens = tokenizer.tokenize(cool_captions_text)
text_tokens = text_tokens.to(device)
clip_embeddings = clip_model.encode_text(
text_tokens
).float().to(device)
clip_embeddings_full = generate_clip_embeddings(
clip_model, text_tokens).float().to(device)
t5_embeddings_full = t5_model(cool_captions_text).to(device)
cool_captions_embeddings = torch.cat(
[clip_embeddings, torch.mean(t5_embeddings_full, dim=1)], 1)
cool_captions_embeddings_full = torch.cat([clip_embeddings_full,
t5_embeddings_full], 2)
cool_captions = DataLoader(
TensorDataset(
cool_captions_embeddings.repeat_interleave(n, dim=0)
),
batch_size=11,
)
cool_captions_sampled = []
cool_captions_sampled_ema = []
st = time.time()
for caption_embedding in cool_captions:
caption_embedding = caption_embedding[0].float().to(device)
sampled_text = sample(model, c=caption_embedding,
c_full=cool_captions_embeddings_full) # [-1]
sampled_text = decode(vqmodel, sampled_text)
# sampled_text_ema = decode(vqmodel, sampled_text_ema)
for s in sampled_text:
cool_captions_sampled.append(s.cpu())
# cool_captions_sampled_ema.append(t.cpu())
print(
f"Took {time.time() - st} seconds to sample {len(cool_captions_text) * 2} captions."
)
cool_captions_sampled = torch.stack(cool_captions_sampled)
torchvision.utils.save_image(
torchvision.utils.make_grid(cool_captions_sampled, nrow=11),
os.path.join(
f"results/{args.run_name}", f"cool_captions_{step:03d}.png"
),
)
# cool_captions_sampled_ema = torch.stack(cool_captions_sampled_ema)
# torchvision.utils.save_image(
# torchvision.utils.make_grid(cool_captions_sampled_ema, nrow=11),
# os.path.join(f"results/{args.run_name}", f"cool_captions_{step:03d}_ema.png")
# )
log_images = torch.cat(
[
torch.cat([i for i in sampled.cpu()], dim=-1),
],
dim=-2,
)
model.train()
torchvision.utils.save_image(
log_images, os.path.join(f"results/{args.run_name}", f"{step:03d}.png")
)
log_data = [
[captions[i]]
+ [wandb.Image(sampled[i])]
+ [wandb.Image(images[i])]
+ [wandb.Image(recon_images[i])]
for i in range(len(captions))
]
log_table = wandb.Table(
data=log_data, columns=["Caption", "Image", "Orig", "Recon"]
)
wandb.log({"Log": log_table})
if args.log_captions:
log_data_cool = [
[cool_captions_text[i]] + [wandb.Image(cool_captions_sampled[i])]
for i in range(len(cool_captions_text))
]
log_table_cool = wandb.Table(
data=log_data_cool, columns=["Caption", "Image"]
)
wandb.log({"Log Cool": log_table_cool})
del sampled_text, log_data_cool
del sampled, log_data
if step % args.extra_ckpt == 0:
torch.save(
model.state_dict(), f"models/{args.run_name}/model_{step}.pt"
)
torch.save(
optimizer.state_dict(),
f"models/{args.run_name}/model_{step}_optim.pt",
)
torch.save(model.state_dict(), f"models/{args.run_name}/model.pt")
torch.save(optimizer.state_dict(), f"models/{args.run_name}/optim.pt")
torch.save(
{"step": step, "losses": losses, "accuracies": accuracies},
f"results/{args.run_name}/log.pt",
)
del images, image_indices, r, text_embeddings
del noised_indices, mask, pred, loss, loss_adjusted, acc
pbar.update(1)
step += 1
def launch(args):
os.environ["CUDA_VISIBLE_DEVICES"] = ",".join([str(d) for d in args.devices])
if len(args.devices) == 1:
train(0, args)
else:
os.environ["MASTER_ADDR"] = "localhost"
os.environ["MASTER_PORT"] = "33751"
p = mp.spawn(train, nprocs=len(args.devices), args=(args,))
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
args = parser.parse_args()
args.run_name = "run_name"
args.model = "UNet"
args.dataset_type = "webdataset"
args.total_steps = 501_000
args.batch_size = 16 # 22
args.image_size = 256
args.num_workers = 10
args.log_period = 1000 # 5000
args.extra_ckpt = 50_000
args.accum_grad = 1
args.num_codebook_vectors = 8192
args.log_captions = True
args.finetune = False
args.n_nodes = 1
args.node_id = 0 # int(os.environ["SLURM_PROCID"])
args.devices = [0] # [0, 1, 2, 3, 4, 5, 6, 7]
args.dataset_path = "gigant/oldbookillustrations_2"
print("Launching with args: ", args)
launch(args)