-
Notifications
You must be signed in to change notification settings - Fork 3
/
get_calibration_set_imagenet_ddim.py
487 lines (427 loc) · 15.5 KB
/
get_calibration_set_imagenet_ddim.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
"""
This is an example script to generate calibration set for ImageNet using DDIM sampling.
"""
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,
block_reconstruction, layer_reconstruction,
)
from qdiff.adaptive_rounding import AdaRoundQuantizer
from qdiff.quant_layer import UniformAffineQuantizer
from qdiff.utils import resume_cali_model, get_train_samples
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, shape, class_label, eta=1.0):
ddim = DDIMSampler(model)
n_samples_per_class = shape[0]
ddim_steps = 20
ddim_eta = 0.0
scale = 3.0
with model.ema_scope():
uc = model.get_learned_conditioning(
{model.cond_stage_key: torch.tensor(n_samples_per_class*[1000]).to(model.device)}
)
print(f"rendering {n_samples_per_class} examples of class '{class_label}' in {ddim_steps} steps and using s={scale:.2f}.")
xc = torch.tensor(n_samples_per_class*[class_label])
c = model.get_learned_conditioning({model.cond_stage_key: xc.to(model.device)})
samples_ddim, intermediates = ddim.sample(S=ddim_steps,
conditioning=c,
batch_size=n_samples_per_class,
shape=[3, 64, 64],
verbose=False,
unconditional_guidance_scale=scale,
unconditional_conditioning=uc,
eta=ddim_eta,
log_every_t=1)
return samples_ddim, intermediates, c, uc
@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, class_label, vanilla=False, custom_steps=None, eta=1.0, dpm=False, logs=None):
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, c, uc = convsample_ddim(model, shape=shape, class_label=class_label, eta=eta)
xs = torch.stack(intermediates['x_inter'])[1:]
ts = torch.stack(intermediates['ts'])
c = c.unsqueeze(0).repeat(xs.shape[0], 1, 1, 1)
uc = uc.unsqueeze(0).repeat(xs.shape[0], 1, 1, 1)
if 'xs' in logs:
logs['xs'] = torch.cat([logs['xs'], xs], dim=1)
logs['ts'] = torch.cat([logs['ts'], ts], dim=1)
logs['cs'] = torch.cat([logs['cs'], c], dim=1)
logs['ucs'] = torch.cat([logs['ucs'], uc], dim=1)
else:
logs['xs'] = xs
logs['ts'] = ts
logs['cs'] = c
logs['ucs'] = uc
def run(model, logdir, label, batch_size=50, vanilla=False, custom_steps=None, eta=None,
n_samples=50000, nplog=None, dpm=False, logs=None):
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
if model.cond_stage_model:
all_images = []
logger.info(f"Running class-conditional sampling for {n_samples} samples")
for _ in trange(n_samples // batch_size, desc="Sampling Batches (conditional)"):
make_convolutional_sample(model, batch_size=batch_size, class_label=label,
vanilla=vanilla, custom_steps=custom_steps,
eta=eta, dpm=dpm, logs=logs)
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 = None
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(opt.cond)
# 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)
logs = dict()
for c in range(1000):
run(model, imglogdir, label=c, 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, logs=logs)
for k in logs.keys():
print(logs[k].shape)
torch.save(logs, os.path.join(logdir, "imagenet_sample10000_allst.pt"))
logger.info("done.")