-
Notifications
You must be signed in to change notification settings - Fork 4
/
sample_diffusion_ldm_church.py
641 lines (562 loc) · 23.5 KB
/
sample_diffusion_ldm_church.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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
import argparse, os, sys, gc, glob, datetime, yaml
import logging
import time
import numpy as np
from tqdm import trange
from pytorch_lightning import seed_everything
from omegaconf import OmegaConf
from PIL import Image
import torch
import torch.nn as nn
import sys
from ldm.models.diffusion.ddim import DDIMSampler
from ldm.models.diffusion.dpm_solver import DPMSolverSampler
from ldm.util import instantiate_from_config
from qdiff import (
QuantModel, QuantModule, BaseQuantBlock, QuantQKMatMul, QuantSMVMatMul, QuantBasicTransformerBlock,
block_reconstruction, layer_reconstruction,
)
from qdiff.adaptive_rounding import AdaRoundQuantizer
from qdiff.quant_layer import UniformAffineQuantizer, TimewiseUniformQuantizer
from qdiff.utils import resume_cali_model, get_train_samples
from qdiff.post_layer_recon import *
from collections import Counter
import shutil
import copy
from qdiff.post_layer_recon import *
logger = logging.getLogger(__name__)
rescale = lambda x: (x + 1.) / 2.
def custom_to_pil(x):
x = x.detach().cpu()
x = torch.clamp(x, -1., 1.)
x = (x + 1.) / 2.
x = x.permute(1, 2, 0).numpy()
x = (255 * x).astype(np.uint8)
x = Image.fromarray(x)
if not x.mode == "RGB":
x = x.convert("RGB")
return x
def custom_to_np(x):
# saves the batch in adm style as in https://github.com/openai/guided-diffusion/blob/main/scripts/image_sample.py
sample = x.detach().cpu()
sample = ((sample + 1) * 127.5).clamp(0, 255).to(torch.uint8)
sample = sample.permute(0, 2, 3, 1)
sample = sample.contiguous()
return sample
def logs2pil(logs, keys=["sample"]):
imgs = dict()
for k in logs:
try:
if len(logs[k].shape) == 4:
img = custom_to_pil(logs[k][0, ...])
elif len(logs[k].shape) == 3:
img = custom_to_pil(logs[k])
else:
print(f"Unknown format for key {k}. ")
img = None
except:
img = None
imgs[k] = img
return imgs
@torch.no_grad()
def convsample(model, shape, return_intermediates=True,
verbose=True,
make_prog_row=False):
if not make_prog_row:
return model.p_sample_loop(None, shape,
return_intermediates=return_intermediates, verbose=verbose)
else:
return model.progressive_denoising(
None, shape, verbose=True
)
@torch.no_grad()
def convsample_ddim(model, steps, shape, eta=1.0
):
ddim = DDIMSampler(model)
bs = shape[0]
shape = shape[1:]
samples, intermediates = ddim.sample(steps, batch_size=bs, shape=shape, eta=eta, verbose=False,)
return samples, intermediates
@torch.no_grad()
def convsample_dpm(model, steps, shape, eta=1.0
):
dpm = DPMSolverSampler(model)
bs = shape[0]
shape = shape[1:]
samples, intermediates = dpm.sample(steps, batch_size=bs, shape=shape, eta=eta, verbose=False,)
return samples, intermediates
@torch.no_grad()
def make_convolutional_sample(model, batch_size, vanilla=False, custom_steps=None, eta=1.0, dpm=False):
log = dict()
shape = [batch_size,
model.model.diffusion_model.in_channels,
model.model.diffusion_model.image_size,
model.model.diffusion_model.image_size]
# with model.ema_scope("Plotting"):
t0 = time.time()
if vanilla:
sample, progrow = convsample(model, shape,
make_prog_row=True)
elif dpm:
logger.info(f'Using DPM sampling with {custom_steps} sampling steps and eta={eta}')
sample, intermediates = convsample_dpm(model, steps=custom_steps, shape=shape,
eta=eta)
else:
sample, intermediates = convsample_ddim(model, steps=custom_steps, shape=shape,
eta=eta)
t1 = time.time()
x_sample = model.decode_first_stage(sample)
log["sample"] = x_sample
log["time"] = t1 - t0
log['throughput'] = sample.shape[0] / (t1 - t0)
logger.info(f'Throughput for this batch: {log["throughput"]}')
return log
def run(model, logdir, batch_size=50, vanilla=False, custom_steps=None, eta=None,
n_samples=50000, nplog=None, dpm=False):
if vanilla:
logger.info(f'Using Vanilla DDPM sampling with {model.num_timesteps} sampling steps.')
else:
logger.info(f'Using DDIM sampling with {custom_steps} sampling steps and eta={eta}')
tstart = time.time()
n_saved = len(glob.glob(os.path.join(logdir,'*.png')))-1
# path = logdir
if model.cond_stage_model is None:
all_images = []
logger.info(f"Running unconditional sampling for {n_samples} samples")
for _ in trange(n_samples // batch_size, desc="Sampling Batches (unconditional)"):
logs = make_convolutional_sample(model, batch_size=batch_size,
vanilla=vanilla, custom_steps=custom_steps,
eta=eta, dpm=dpm)
n_saved = save_logs(logs, logdir, n_saved=n_saved, key="sample")
all_images.extend([custom_to_np(logs["sample"])])
if n_saved >= n_samples:
logger.info(f'Finish after generating {n_saved} samples')
break
all_img = np.concatenate(all_images, axis=0)
all_img = all_img[:n_samples]
shape_str = "x".join([str(x) for x in all_img.shape])
nppath = os.path.join(nplog, f"{shape_str}-samples.npz")
np.savez(nppath, all_img)
else:
raise NotImplementedError('Currently only sampling for unconditional models supported.')
logger.info(f"sampling of {n_saved} images finished in {(time.time() - tstart) / 60.:.2f} minutes.")
def save_logs(logs, path, n_saved=0, key="sample", np_path=None):
for k in logs:
if k == key:
batch = logs[key]
if np_path is None:
for x in batch:
img = custom_to_pil(x)
imgpath = os.path.join(path, f"{key}_{n_saved:06}.png")
img.save(imgpath)
n_saved += 1
else:
npbatch = custom_to_np(batch)
shape_str = "x".join([str(x) for x in npbatch.shape])
nppath = os.path.join(np_path, f"{n_saved}-{shape_str}-samples.npz")
np.savez(nppath, npbatch)
n_saved += npbatch.shape[0]
return n_saved
def get_parser():
parser = argparse.ArgumentParser()
parser.add_argument(
"-r",
"--resume_base",
type=str,
nargs="?",
help="load fp32 base model from logdir or checkpoint in logdir (will deprecate after direct quantized model loading implemented)",
)
parser.add_argument(
"-n",
"--n_samples",
type=int,
nargs="?",
help="number of samples to draw",
default=50000
)
parser.add_argument(
"-e",
"--eta",
type=float,
nargs="?",
help="eta for ddim sampling (0.0 yields deterministic sampling)",
default=1.0
)
parser.add_argument(
"-v",
"--vanilla_sample",
default=False,
action='store_true',
help="vanilla sampling (default option is DDIM sampling)?",
)
parser.add_argument(
"--seed",
type=int,
# default=42,
required=True,
help="the seed (for reproducible sampling)",
)
parser.add_argument(
"-l",
"--logdir",
type=str,
nargs="?",
help="extra logdir",
default="none"
)
parser.add_argument(
"-c",
"--custom_steps",
type=int,
nargs="?",
help="number of steps for ddim and fast dpm sampling",
default=50
)
parser.add_argument(
"--batch_size",
type=int,
nargs="?",
help="the bs",
default=10
)
# linear quantization configs
parser.add_argument(
"--ptq", action="store_true", help="apply post-training quantization"
)
parser.add_argument(
"--quant_act", action="store_true",
help="if to quantize activations when ptq==True"
)
parser.add_argument(
"--weight_bit",
type=int,
default=8,
help="int bit for weight quantization",
)
parser.add_argument(
"--act_bit",
type=int,
default=8,
help="int bit for activation quantization",
)
parser.add_argument(
"--quant_mode", type=str, default="qdiff",
choices=["qdiff"],
help="quantization mode to use"
)
# qdiff specific configs
parser.add_argument(
"--cali_st", type=int, default=1,
help="number of timesteps used for calibration"
)
parser.add_argument(
"--cali_batch_size", type=int, default=32,
help="batch size for qdiff reconstruction"
)
parser.add_argument(
"--cali_n", type=int, default=1024,
help="number of samples for each timestep for qdiff reconstruction"
)
parser.add_argument(
"--cali_iters", type=int, default=20000,
help="number of iterations for each qdiff reconstruction"
)
parser.add_argument('--cali_iters_a', default=5000, type=int,
help='number of iteration for LSQ')
parser.add_argument('--cali_lr', default=4e-4, type=float,
help='learning rate for LSQ')
parser.add_argument('--cali_p', default=2.4, type=float,
help='L_p norm minimization for LSQ')
parser.add_argument(
"--cali_ckpt", type=str,
help="path for calibrated model ckpt"
)
parser.add_argument(
"--cali_data_path", type=str, default="sd_coco_sample1024_allst.pt",
help="calibration dataset name"
)
parser.add_argument(
"--resume", action="store_true",
help="resume the calibrated qdiff model"
)
parser.add_argument(
"--resume_w", action="store_true",
help="resume the calibrated qdiff model weights only"
)
parser.add_argument(
"--cond", action="store_true",
help="whether to use conditional guidance"
)
parser.add_argument(
"--a_sym", action="store_true",
help="act quantizers use symmetric quantization (empirically helpful in some cases)"
)
parser.add_argument(
"--a_min_max", action="store_true",
help="act quantizers initialize with min-max (empirically helpful in some cases)"
)
parser.add_argument(
"--running_stat", action="store_true",
help="use running statistics for act quantizers"
)
parser.add_argument(
"--rs_sm_only", action="store_true",
help="use running statistics only for softmax act quantizers"
)
parser.add_argument(
"--sm_abit",type=int, default=8,
help="attn softmax activation bit"
)
parser.add_argument(
"--dpm", action="store_true",
help="use dpm solver for sampling"
)
parser.add_argument(
"--verbose", action="store_true",
help="print out info like quantized model arch"
)
return parser
def load_model_from_config(config, sd):
model = instantiate_from_config(config)
model.load_state_dict(sd,strict=False)
model.cuda()
model.eval()
return model
def load_model(config, ckpt, gpu, eval_mode):
if ckpt:
logger.info(f"Loading model from {ckpt}")
pl_sd = torch.load(ckpt, map_location="cpu")
global_step = pl_sd["global_step"]
else:
pl_sd = {"state_dict": None}
global_step = None
model = load_model_from_config(config.model,
pl_sd["state_dict"])
return model, global_step
if __name__ == "__main__":
now = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
sys.path.append(os.getcwd())
command = " ".join(sys.argv)
parser = get_parser()
opt, unknown = parser.parse_known_args()
ckpt = None
# fix random seed
seed_everything(opt.seed)
if not os.path.exists(opt.resume_base):
raise ValueError("Cannot find {}".format(opt.resume_base))
if os.path.isfile(opt.resume_base):
# paths = opt.resume.split("/")
try:
logdir = '/'.join(opt.resume_base.split('/')[:-1])
# idx = len(paths)-paths[::-1].index("logs")+1
print(f'Logdir is {logdir}')
except ValueError:
paths = opt.resume_base.split("/")
idx = -2 # take a guess: path/to/logdir/checkpoints/model.ckpt
logdir = "/".join(paths[:idx])
ckpt = opt.resume_base
else:
assert os.path.isdir(opt.resume_base), f"{opt.resume_base} is not a directory"
logdir = opt.resume_base.rstrip("/")
ckpt = os.path.join(logdir, "model.ckpt")
base_configs = sorted(glob.glob(os.path.join(logdir, "config.yaml")))
opt.base = base_configs
configs = [OmegaConf.load(cfg) for cfg in opt.base]
cli = OmegaConf.from_dotlist(unknown)
config = OmegaConf.merge(*configs, cli)
gpu = True
eval_mode = True
if opt.logdir != "none":
locallog = logdir.split(os.sep)[-1]
if locallog == "": locallog = logdir.split(os.sep)[-2]
print(f"Switching logdir from '{logdir}' to '{os.path.join(opt.logdir, locallog)}'")
logdir = os.path.join(opt.logdir, locallog)
logdir = os.path.join(logdir, "samples", now)
os.makedirs(logdir)
log_path = os.path.join(logdir, "run.log")
logging.basicConfig(
format='%(asctime)s - %(levelname)s - %(name)s - %(message)s',
datefmt='%m/%d/%Y %H:%M:%S',
level=logging.INFO,
handlers=[
logging.FileHandler(log_path),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
print(config)
logger.info(75 * "=")
logger.info(f"Host {os.uname()[1]}")
logger.info("logging to:")
imglogdir = os.path.join(logdir, "img")
numpylogdir = os.path.join(logdir, "numpy")
os.makedirs(imglogdir)
os.makedirs(numpylogdir)
logger.info(logdir)
logger.info(75 * "=")
model, global_step = load_model(config, ckpt, gpu, eval_mode)
logger.info(f"global step: {global_step}")
logger.info("Switched to EMA weights")
model.model_ema.store(model.model.parameters())
model.model_ema.copy_to(model.model)
# print(model.model)
assert(not opt.cond)
if opt.ptq:
if opt.quant_mode == 'qdiff':
a_scale_method = 'mse' if not opt.a_min_max else 'max'
wq_params = {'n_bits': opt.weight_bit, 'channel_wise': True, 'scale_method': 'mse'}
aq_params = {
'n_bits': opt.act_bit, 'symmetric': opt.a_sym, 'channel_wise': True,
'scale_method': a_scale_method, 'leaf_param': opt.quant_act
}
if opt.resume:
logger.info('Load with min-max quick initialization')
wq_params['scale_method'] = 'max'
aq_params['scale_method'] = 'max'
if opt.resume_w:
wq_params['scale_method'] = 'max'
# with model.ema_scope("Quantizing", restore=False):
logger.info(f"Sampling data from {opt.cali_st} timesteps for calibration")
sample_data = torch.load(opt.cali_data_path)
cali_data = get_train_samples(opt, sample_data)
del(sample_data)
gc.collect()
logger.info(f"Calibration data shape: {cali_data[0].shape} {cali_data[1].shape}")
timesteps = [k for k, v in Counter(list(np.array(cali_data[1]))).items()]
print("Number of timesteps and values:", len(timesteps), timesteps)
qnn = QuantModel(
model=model.model.diffusion_model, weight_quant_params=wq_params, act_quant_params=aq_params,
sm_abit=opt.sm_abit, act_quant_mode="qdiff", timewise=True, list_timesteps=timesteps)
qnn.cuda()
qnn.eval()
# import torch
# import io
# buffer = io.BytesIO()
# torch.save(qnn.state_dict(), buffer)
# model_size = buffer.tell() # Size in bytes
# print(f'Model size: {model_size / 1024 / 1024:.2f} MB') # Convert to MB
# sys.exit(0)
# TODO: Crucial 1. Set the first and last layer to be 8 bit
for n, m in qnn.named_modules():
if isinstance(m, QuantModule):
if ".out.2" in n or "input_blocks.0.0" in n:
print(n)
for m_act in m.act_quantizer.quantizer_dict.values():
m_act.n_bits = 8
m_act.n_levels = 2 ** 8
is_recon = False
if opt.resume:
image_size = config.model.params.image_size
channels = config.model.params.channels
cali_data_resume = (torch.randn(1, channels, image_size, image_size), torch.randint(0, 1000, (1,)))
resume_cali_model(qnn, opt.cali_ckpt, cali_data_resume, opt.quant_act, "qdiff", cond=False, timesteps=timesteps)
else:
cali_xs, cali_ts = cali_data
if opt.resume_w:
resume_cali_model(qnn, opt.cali_ckpt, cali_data, False, cond=False, timesteps=timesteps)
else:
logger.info("Initializing weight quantization parameters")
qnn.set_quant_state(True, False) # enable weight quantization, disable act quantization
_ = qnn(cali_xs[:8].cuda(), cali_ts[:8].cuda())
logger.info("Initializing has done!")
recon_weight = True
if not opt.resume_w:
logger.info("Doing weight calibration")
recon_model(qnn)
qnn.set_quant_state(weight_quant=True, act_quant=False)
recon_weight = False
logger.info("Saving calibrated quantized UNet model")
for m in qnn.model.modules():
if isinstance(m, AdaRoundQuantizer):
m.zero_point = nn.Parameter(m.zero_point)
m.delta = nn.Parameter(m.delta)
elif isinstance(m, UniformAffineQuantizer) and opt.quant_act:
if m.zero_point is not None:
if not torch.is_tensor(m.zero_point):
m.zero_point = nn.Parameter(torch.tensor(float(m.zero_point)))
else:
m.zero_point = nn.Parameter(m.zero_point)
torch.save(qnn.state_dict(), os.path.join(logdir, "ckpt.pth"))
if opt.quant_act:
recon_weight = False
logger.info("UNet model")
# logger.info(model.model)
logger.info("Doing activation calibration")
# Initialize activation quantization parameters
qnn.set_quant_state(True, True)
# Timewise initialization
with torch.no_grad():
for i in trange(len(timesteps)):
t = timesteps[i]
qnn.set_timestep(t)
inds = torch.where(cali_ts == t)[0]
inds = inds[:64]
_ = qnn(cali_xs[inds].cuda(), cali_ts[inds].cuda())
if opt.running_stat:
logger.info('Running stat for activation quantization')
qnn.set_running_stat(True)
for i in trange(int(cali_xs.size(0) / 64)):
_ = qnn(cali_xs[i * 64:(i + 1) * 64].cuda(),
cali_ts[i * 64:(i + 1) * 64].cuda())
qnn.set_running_stat(False)
qnn.set_quant_state(weight_quant=True, act_quant=True)
logger.info("Saving calibrated quantized UNet model")
qnn.save_dict_params()
for m in qnn.model.modules():
if isinstance(m, AdaRoundQuantizer):
m.zero_point = nn.Parameter(m.zero_point)
m.delta = nn.Parameter(m.delta)
elif isinstance(m, UniformAffineQuantizer) and opt.quant_act:
if m.zero_point is not None:
if not torch.is_tensor(m.zero_point):
m.zero_point = nn.Parameter(torch.tensor(float(m.zero_point)))
else:
m.zero_point = nn.Parameter(m.zero_point)
elif isinstance(m, TimewiseUniformQuantizer) and opt.quant_act:
if m.zero_point_list is not None:
if not torch.is_tensor(m.zero_point_list):
m.zero_point_list = nn.Parameter(torch.tensor(float(m.zero_point_list)))
else:
m.zero_point_list = nn.Parameter(m.zero_point_list)
torch.save(qnn.state_dict(), os.path.join(logdir, "ckpt.pth"))
pd_optimize_timeembed(qnn, cali_data, opt, logger, iters=1000, timesteps=timesteps, outpath=logdir)
pd_optimize_timewise(qnn, cali_data, opt, logger, iters=1000, timesteps=timesteps, outpath=logdir)
logger.info("Saving calibrated quantized UNet model")
qnn.save_dict_params()
for m in qnn.model.modules():
if isinstance(m, AdaRoundQuantizer):
m.zero_point = nn.Parameter(m.zero_point)
m.delta = nn.Parameter(m.delta)
elif isinstance(m, UniformAffineQuantizer) and opt.quant_act:
if m.zero_point is not None:
if not torch.is_tensor(m.zero_point):
m.zero_point = nn.Parameter(torch.tensor(float(m.zero_point)))
else:
m.zero_point = nn.Parameter(m.zero_point)
elif isinstance(m, TimewiseUniformQuantizer) and opt.quant_act:
if m.zero_point_list is not None:
if not torch.is_tensor(m.zero_point_list):
m.zero_point_list = nn.Parameter(torch.tensor(float(m.zero_point_list)))
else:
m.zero_point_list = nn.Parameter(m.zero_point_list.float())
torch.save(qnn.state_dict(), os.path.join(logdir, "ckpt.pth"))
qnn.set_quant_state(True, True)
model.model.diffusion_model = qnn
if not opt.resume and is_recon:
logger.info("Delete cached data to save disk usage")
shutil.rmtree(os.path.join(logdir, "tmp_cached"))
# write config out
sampling_file = os.path.join(logdir, "sampling_config.yaml")
sampling_conf = vars(opt)
with open(sampling_file, 'a+') as f:
yaml.dump(sampling_conf, f, default_flow_style=False)
if opt.verbose:
print(sampling_conf)
logger.info("first_stage_model")
logger.info(model.first_stage_model)
logger.info("UNet model")
logger.info(model.model)
run(model, imglogdir, eta=opt.eta,
vanilla=opt.vanilla_sample, n_samples=opt.n_samples, custom_steps=opt.custom_steps,
batch_size=opt.batch_size, nplog=numpylogdir, dpm=opt.dpm)
logger.info("done.")