-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
337 lines (281 loc) · 11 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
import argparse
import numpy as np
import torch
import torch.nn.functional as F
from torch.distributions.categorical import Categorical
from PIL import Image
from torchvision.utils import save_image, make_grid
from torchvision.datasets import DatasetFolder
from taming.models.vqgan import VQModel
import toml
from types import SimpleNamespace
from pathlib import Path
from omegaconf import OmegaConf
import yaml
from ptpt.trainer import Trainer, TrainerConfig
from ptpt.log import debug, info, warning, error, critical
from ptpt.callbacks import CallbackType
from ptpt.utils import set_seed, get_parameter_count, get_device
from ptpt.wandb import WandbConfig
from x_transformers import TransformerWrapper, Encoder
from hourglass_transformer_pytorch import HourglassTransformerLM
import wandb
from tqdm import tqdm
from time import time
from math import sqrt
from shutil import copyfile
from einops import rearrange
from utils import decode_vqgan
class LatentDataset(torch.utils.data.Dataset):
def __init__(self, root_path, split_sizes, split="train"):
super().__init__()
if isinstance(root_path, str):
root_path = Path(root_path)
files = list(root_path.glob("*.npy"))
assert len(files) == sum(
split_sizes
), "sum of splits is not equal to number of files found!"
if split == "train":
self.files = files[: split_sizes[0]]
elif split == "eval":
self.files = files[split_sizes[0] : sum(split_sizes[:2])]
elif split == "test":
self.files = files[sum(split_sizes[:2]) :]
def __len__(self):
return len(self.files)
def __getitem__(self, idx):
return torch.from_numpy(np.load(self.files[idx]))
def load_config(config_path, display=False):
config = OmegaConf.load(config_path)
if display:
print(yaml.dump(OmegaConf.to_container(config)))
return config
def main(args):
cfg = SimpleNamespace(**toml.load(args.cfg_path))
seed = set_seed(args.seed)
info(f"random seed: {seed}")
cfg.net["conditional"] = (
cfg.net["conditional"] if "conditional" in cfg.net else False
)
if cfg.net["conditional"]:
loader_fn = lambda p: torch.from_numpy(np.load(p))
train_dataset = DatasetFolder(
cfg.data["train_root"], loader=loader_fn, extensions=".npy"
)
eval_dataset = DatasetFolder(
cfg.data["eval_root"], loader=loader_fn, extensions=".npy"
)
assert len(train_dataset.classes) == cfg.net["num_classes"]
else:
train_dataset = LatentDataset(
cfg.data["root"], cfg.data["split_sizes"], split="train"
)
eval_dataset = LatentDataset(
cfg.data["root"], cfg.data["split_sizes"], split="eval"
)
if cfg.net["type"] in ["transformer", "vanilla"]:
net = TransformerWrapper(
num_tokens=cfg.data["vocab_size"],
max_seq_len=cfg.data["sequence_length"],
attn_layers=Encoder(
dim=cfg.net["dim"],
depth=cfg.net["depth"],
head=cfg.net["nb_heads"],
use_scalenorm=cfg.net["use_scalenorm"],
ff_glu=cfg.net["use_glu"],
rotary_pos_emb=cfg.net["use_rotary"],
attn_dropout=cfg.net["attn_dropout"],
ff_dropout=cfg.net["ff_dropout"],
),
)
elif cfg.net["type"] in ["hourglass"]:
net = HourglassTransformerLM(
num_tokens=cfg.data["vocab_size"],
dim=cfg.net["dim"],
max_seq_len=cfg.data["sequence_length"],
heads=cfg.net["nb_heads"],
shorten_factor=cfg.net["shorten_factor"],
depth=tuple(cfg.net["depth"]),
causal=False,
rotary_pos_emb=cfg.net["use_rotary"],
attn_type="vanilla",
updown_sample_type="linear",
conditional=cfg.net["conditional"],
num_classes=cfg.net["num_classes"] if cfg.net["conditional"] else 0,
)
else:
msg = f"unrecogized model type '{cfg.net['type']}'"
error(msg)
raise ValueError(msg)
info(f"number of parameters: {get_parameter_count(net):,}")
def get_random_text(shape):
return torch.randint(cfg.data["vocab_size"], shape)
def corrupt_text(batched_text):
corruption_prob_per_sequence = torch.rand((batched_text.shape[0], 1))
rand = torch.rand(batched_text.shape)
mask = (rand < corruption_prob_per_sequence).to(batched_text.device)
random_text = get_random_text(batched_text.shape).to(batched_text.device)
return mask * random_text + ~mask * batched_text
def logits_fn(net, batched_text, c=None):
samples = corrupt_text(batched_text)
all_logits = []
for _ in range(cfg.unroll_steps):
logits = net(samples, c=c)
samples = Categorical(logits=logits).sample().detach()
all_logits.append(logits)
final_logits = torch.cat(all_logits, axis=0)
return final_logits
def loss_fn(net, batch):
if cfg.net["conditional"]:
batched_text, c = batch
else:
batched_text, c = batch, None
if args.mnist:
batched_text = rearrange(batched_text, "n c h w -> n (c h w)")
logits = logits_fn(net, batched_text, c=c)
targets = batched_text.repeat(cfg.unroll_steps, 1)
accuracy = (logits.argmax(dim=-1) == targets).sum() / targets.numel()
loss = F.cross_entropy(logits.permute(0, 2, 1), targets)
return loss, accuracy * 100.0
if not args.mnist:
vqgan_cfg = OmegaConf.load(cfg.vqgan["config"])
sd = torch.load(cfg.vqgan["checkpoint"], map_location="cpu")["state_dict"]
if args.legacy_cfg:
sd = {
k.replace("first_stage_model.", ""): v
for k, v in sd.items()
if "first_stage_model." in k
}
vqgan = VQModel(
**(
vqgan_cfg.model.params.first_stage_config.params
if args.legacy_cfg
else vqgan_cfg.model.params
)
)
vqgan.load_state_dict(sd)
@torch.inference_mode()
def sample_fn(
net,
steps,
nb_samples,
temperature,
sample_proportion,
end_temperature=None,
end_sample_proportion=None,
status=False,
history=False,
min_steps=10,
early_stop=True,
):
device = get_device(not args.no_cuda)
sample_history = [] if history else None
batched_text = get_random_text((nb_samples, cfg.data["sequence_length"])).to(
device
)
sample_mask = torch.zeros(nb_samples).bool().to(device)
c = None
if cfg.net["conditional"]:
c = torch.randint(0, cfg.net["num_classes"], (nb_samples,)).to(device)
temperatures = torch.linspace(temperature, end_temperature, steps)
proportions = torch.linspace(sample_proportion, end_sample_proportion, steps)
pb = tqdm(total=steps, disable=not status)
for n, (temperature, sample_proportion) in enumerate(
zip(temperatures, proportions)
):
iter_start_time = time()
old_sample_mask = sample_mask.clone()
logits = net(
batched_text[~sample_mask], c=c[~sample_mask] if c != None else None
)
sample = Categorical(logits=logits / temperature).sample()
mask = (torch.rand(sample.shape) > sample_proportion).to(
batched_text.device
)
sample[mask] = batched_text[~sample_mask][mask]
if n >= min_steps:
sample_mask[~sample_mask] = torch.all(
(sample == batched_text[~sample_mask]).view(sample.shape[0], -1),
dim=-1,
)
if early_stop and torch.all(sample_mask).item():
break
batched_text[~old_sample_mask] = sample
if history:
sample_history.append(batched_text.cpu().clone())
pb.set_description_str(
f"T: {temperature:.3f}, p: {sample_proportion:.3f}, [{sample_mask.long().sum().item()} / {batched_text.shape[0]}]"
)
pb.update(1)
debug(f"stopped sampling after {n+1} steps.")
if history:
return batched_text, sample_history
return batched_text
trainer_cfg = TrainerConfig(
**cfg.trainer,
nb_workers=args.nb_workers,
use_cuda=not args.no_cuda,
use_amp=not args.no_amp,
save_outputs=not args.no_save,
)
wandb_cfg = WandbConfig(
project="sundae-mnist" if args.mnist else "sundae-vqgan",
entity="afmck",
config={"cfg": cfg, "args": args},
)
trainer = Trainer(
net=net,
loss_fn=loss_fn,
train_dataset=train_dataset,
test_dataset=eval_dataset,
cfg=trainer_cfg,
wandb_cfg=wandb_cfg if args.wandb else None,
)
if not args.mnist:
vqgan_dim = (
vqgan_cfg.model.params.first_stage_config.params.embed_dim
if args.legacy_cfg
else vqgan_cfg.model.params.embed_dim
)
@torch.inference_mode()
def callback_sample(trainer):
samples = sample_fn(trainer.net, **cfg.sampling, status=False).cpu()
if args.mnist:
img = rearrange(samples, "n (h w) -> n 1 h w", h=28, w=28) / 255.0
else:
debug("decoding latents with VQ-GAN")
img = decode_vqgan(samples, vqgan, (*cfg.vqgan["latent_shape"], vqgan_dim))
debug("saving to file")
save_image(
img,
trainer.directories["root"]
/ f"sample-{str(trainer.nb_updates).zfill(6)}.jpg",
nrow=int(sqrt(samples.shape[0])),
)
if trainer.wandb:
wandb_img = wandb.Image(make_grid(img, nrow=int(sqrt(samples.shape[0]))))
trainer.wandb.log({"samples": wandb_img}, commit=False)
trainer.net.train()
if not args.no_save:
trainer.register_callback(CallbackType.EvalEpoch, callback_sample)
copyfile(args.cfg_path, trainer.directories["root"] / "config.toml")
if args.resume:
trainer.load_checkpoint(args.resume)
trainer.train()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--cfg-path", type=str, default="config/ffhq256/ffhq256-hourglass.toml"
)
parser.add_argument("--resume", type=str, default=None)
parser.add_argument("--seed", type=int, default=None)
parser.add_argument("--no-save", action="store_true")
parser.add_argument("--no-cuda", action="store_true")
parser.add_argument("--no-amp", action="store_true")
parser.add_argument("--nb-workers", type=int, default=4)
parser.add_argument("--temperature", type=float, default=0.7)
parser.add_argument("--wandb", action="store_true")
parser.add_argument("--legacy-cfg", action="store_true")
parser.add_argument("--mnist", action="store_true")
args = parser.parse_args()
main(args)