-
Notifications
You must be signed in to change notification settings - Fork 3
/
train_t2i_discrete_wds.py
307 lines (254 loc) · 11.7 KB
/
train_t2i_discrete_wds.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
import builtins
import datetime
import os
import time
from argparse import Namespace
import accelerate
import einops
import ml_collections
import torch
from datasets import get_dataset
from loguru import logger
from torch import multiprocessing as mp
from torch.utils._pytree import tree_map
from torchvision.utils import make_grid, save_image
from tqdm import tqdm
import taming.models.vqgan
import wandb
import torch.utils.data
from libs.muse import MUSE
from tools.fid_score import calculate_fid_given_paths
import utils
logging = logger
torch.multiprocessing.set_sharing_strategy('file_system') # todo
def convert_model_dtype(models, dtype):
logging.info(f'Converting model to {dtype}')
if not isinstance(models, (list, tuple)):
models = [models]
for model in models:
if model is None:
continue
if dtype == torch.float16:
model.half()
elif dtype == torch.bfloat16:
model.bfloat16()
def LSimple(x0, nnet, schedule, **kwargs):
labels, masked_ids = schedule.sample(x0)
logits = nnet(masked_ids, **kwargs)
# b (h w) c, b (h w)
loss = schedule.loss(logits, labels)
return loss
def train(config):
if config.get('benchmark', False):
torch.backends.cudnn.benchmark = True
torch.backends.cudnn.deterministic = False
mp.set_start_method('spawn')
accelerator = accelerate.Accelerator()
device = accelerator.device
accelerate.utils.set_seed(config.seed, device_specific=True)
logging.info(f'Process {accelerator.process_index} using device: {device}')
config.mixed_precision = accelerator.mixed_precision
config = ml_collections.ConfigDict(config)
assert config.train.batch_size % accelerator.num_processes == 0
mini_batch_size = config.train.batch_size // accelerator.num_processes
if accelerator.is_main_process:
os.makedirs(config.ckpt_root, exist_ok=True)
os.makedirs(config.sample_dir, exist_ok=True)
accelerator.wait_for_everyone()
if accelerator.is_main_process:
logging.info(config)
wandb.init(dir=os.path.abspath(config.workdir), project=f'cc3m', config=config.to_dict(),
job_type='train', mode='online', settings=wandb.Settings(start_method='fork'))
else:
logging.remove()
logger.add(sys.stderr, level='ERROR')
builtins.print = lambda *args: None
logging.info(f'Run on {accelerator.num_processes} devices')
ckpts = list(filter(lambda x: '.ckpt' in x, os.listdir(config.ckpt_root)))
if not ckpts:
resume_step = 0
else:
steps = map(lambda x: int(x.split(".")[0]), ckpts)
resume_step = max(steps)
logger.info(f'world size is {accelerator.num_processes}')
dist_eval = config.wds.dist_eval
webdataset_args = Namespace(
train_data=config.wds.train_data,
val_data=config.wds.val_data,
dist_eval=dist_eval,
ctx_path=config.wds.ctx_path,
seed=config.seed,
batch_size=mini_batch_size,
val_batch_size=config.sample.mini_batch_size,
workers=config.train.num_workers,
world_size=accelerator.num_processes,
train_num_samples=getattr(config, 'wds.train_num_samples', 6091948),
val_num_samples=getattr(config, 'wds.val_num_samples', 13818),
dataset_type='webdataset',
)
dataset = get_dataset(**config.dataset,
args=webdataset_args,
step=resume_step)
# assert os.path.exists(dataset.fid_stat)
train_dataset = dataset.get_split(split='train', labeled=True)
test_dataset = dataset.get_split(split='test', labeled=True) # for sampling
train_dataset_loader = train_dataset.dataloader
test_dataset_loader = test_dataset.dataloader
autoencoder = taming.models.vqgan.get_model(**config.autoencoder)
autoencoder.to(device)
train_state = utils.initialize_train_state(config, device)
nnet, nnet_ema, optimizer = accelerator.prepare(
train_state.nnet, train_state.nnet_ema, train_state.optimizer)
lr_scheduler = train_state.lr_scheduler
train_state.resume(config.ckpt_root)
@torch.cuda.amp.autocast()
def encode(_batch):
res = autoencoder.encode(_batch)[-1][-1].reshape(len(_batch), -1)
return res
@torch.cuda.amp.autocast()
def decode(_batch):
return autoencoder.decode_code(_batch)
def get_data_generator():
while True:
for data in tqdm(train_dataset_loader, disable=not accelerator.is_main_process, desc='epoch'):
yield data
data_generator = get_data_generator()
def get_context_generator():
while True:
for data in test_dataset_loader:
_, _context = data
yield _context
context_generator = get_context_generator()
muse = MUSE(codebook_size=autoencoder.n_embed, device=device, **config.muse)
def cfg_nnet(x, context, scale=None):
_cond = nnet_ema(x, context=context)
_empty_context = torch.tensor(dataset.empty_context, device=device)
_empty_context = einops.repeat(_empty_context, 'L D -> B L D', B=x.size(0))
_uncond = nnet_ema(x, context=_empty_context)
res = _cond + scale * (_cond - _uncond)
return res
def train_step(_batch):
_metrics = dict()
optimizer.zero_grad()
_z, context = proc_batch_feat(_batch)
loss = LSimple(_z, nnet, muse, context=context) # currently only support the extracted feature version
metric_logger.update(loss=accelerator.gather(loss.detach()).mean())
accelerator.backward(loss.mean())
optimizer.step()
lr_scheduler.step()
train_state.ema_update(config.get('ema_rate', 0.9999))
train_state.step += 1
loss_scale, grad_norm = accelerator.scaler.get_scale(), utils.get_grad_norm_(nnet.parameters())
metric_logger.update(loss_scale=loss_scale)
metric_logger.update(grad_norm=grad_norm)
return dict(lr=train_state.optimizer.param_groups[0]['lr'],
**{k: v.value for k, v in metric_logger.meters.items()})
def proc_batch_feat(_batch):
_z = _batch[0].reshape(-1, 256)
context = _batch[1].reshape(_z.shape[0], 77, -1)
assert context.shape[-1] == config.nnet.clip_dim
return _z, context
def eval_step(n_samples, sample_steps):
logging.info(f'eval_step: n_samples={n_samples}, sample_steps={sample_steps}'
f'mini_batch_size={config.sample.mini_batch_size}')
def sample_fn(_n_samples):
_context = next(context_generator)
_context = _context.to(device).reshape(-1, 77, config.nnet.clip_dim)
kwargs = dict(context=_context)
return muse.generate(config, _n_samples, cfg_nnet, decode, **kwargs)
if accelerator.is_main_process:
path = f'{config.workdir}/eval_samples/{train_state.step}_{datetime.datetime.now().strftime("%m%d_%H%M%S")}'
logging.info(f'Path for FID images: {path}')
else:
path = None
utils.sample2dir(accelerator, path, n_samples, config.sample.mini_batch_size, sample_fn,
dataset.unpreprocess, dist=dist_eval)
_fid = 0
if accelerator.is_main_process:
_fid = calculate_fid_given_paths((dataset.fid_stat, path))
logging.info(f'step={train_state.step} fid{n_samples}={_fid}')
with open(os.path.join(config.workdir, 'eval.log'), 'a') as f:
print(f'step={train_state.step} fid{n_samples}={_fid}', file=f)
wandb.log({f'fid{n_samples}': _fid}, step=train_state.step)
_fid = torch.tensor(_fid, device=device)
_fid = accelerator.reduce(_fid, reduction='sum')
return _fid.item()
if eval_ckpt_path := os.getenv('EVAL_CKPT', ''):
nnet.eval()
train_state.resume(eval_ckpt_path)
logging.info(f'Eval {train_state.step}...')
eval_step(n_samples=config.sample.n_samples, sample_steps=config.sample.sample_steps)
return
logging.info(f'Start fitting, step={train_state.step}, mixed_precision={config.mixed_precision}')
step_fid = []
metric_logger = utils.MetricLogger()
while train_state.step < config.train.n_steps:
nnet.train()
data_time_start = time.time()
batch = tree_map(lambda x: x.to(device), next(data_generator))
metric_logger.update(data_time=time.time() - data_time_start)
metrics = train_step(batch)
nnet.eval()
if train_state.step % config.train.save_interval == 0 or train_state.step == config.train.n_steps:
torch.cuda.empty_cache()
logging.info(f'Save checkpoint {train_state.step}...')
if accelerator.local_process_index == 0:
train_state.save(os.path.join(config.ckpt_root, f'{train_state.step}.ckpt'))
accelerator.wait_for_everyone()
if accelerator.is_main_process and train_state.step % config.train.log_interval == 0:
logger.info(f'step: {train_state.step} {metric_logger}')
wandb.log(metrics, step=train_state.step)
if train_state.step % config.train.eval_interval == 0:
torch.cuda.empty_cache()
logging.info('Save a grid of images...')
contexts = torch.tensor(dataset.contexts, device=device)[: 2 * 5]
samples = muse.generate(config, 2 * 5, cfg_nnet, decode, context=contexts)
samples = make_grid(dataset.unpreprocess(samples), 5)
save_image(samples, os.path.join(config.sample_dir, f'{train_state.step}_{accelerator.process_index}.png'))
if accelerator.is_main_process:
wandb.log({'samples': wandb.Image(samples)}, step=train_state.step)
torch.cuda.empty_cache()
accelerator.wait_for_everyone()
if train_state.step % config.train.fid_interval == 0 or train_state.step == config.train.n_steps:
torch.cuda.empty_cache()
logging.info(f'Eval {train_state.step}...')
fid = eval_step(n_samples=config.eval.n_samples,
sample_steps=config.eval.sample_steps) # calculate fid of the saved checkpoint
step_fid.append((train_state.step, fid))
torch.cuda.empty_cache()
accelerator.wait_for_everyone()
logging.info(f'Finish fitting, step={train_state.step}')
logging.info(f'step_fid: {step_fid}')
step_best = sorted(step_fid, key=lambda x: x[1])[0][0]
logging.info(f'step_best: {step_best}')
train_state.load(os.path.join(config.ckpt_root, f'{step_best}.ckpt'))
del metrics
accelerator.wait_for_everyone()
eval_step(n_samples=config.sample.n_samples, sample_steps=config.sample.sample_steps)
from absl import flags
from absl import app
from ml_collections import config_flags
import sys
from pathlib import Path
FLAGS = flags.FLAGS
config_flags.DEFINE_config_file(
"config", None, "Training configuration.", lock_config=False)
flags.mark_flags_as_required(["config"])
flags.DEFINE_bool("disable_val", False, 'help')
def get_config_name():
argv = sys.argv
for i in range(1, len(argv)):
if argv[i].startswith('--config='):
return Path(argv[i].split('=')[-1]).stem
def main(argv):
config = FLAGS.config
config.config_name = get_config_name()
config.workdir = os.getenv('OUTPUT_DIR',
Path.home() / 'exp/default' / datetime.datetime.now().strftime("%m%d_%H%M%S"))
config.disable_val = FLAGS.disable_val
config.ckpt_root = os.path.join(config.workdir, 'ckpts')
config.sample_dir = os.path.join(config.workdir, 'samples')
train(config)
if __name__ == "__main__":
__spec__ = "ModuleSpec(name='builtins', loader=<class '_frozen_importlib.BuiltinImporter'>)"
app.run(main)