-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdmodel.py
More file actions
1022 lines (846 loc) · 37.2 KB
/
dmodel.py
File metadata and controls
1022 lines (846 loc) · 37.2 KB
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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import torch.nn.functional as F
import torch
import math
import numpy as np
from ldm.utils import *
from ldm.module import *
eps = 1e-8
def get_named_beta_schedule(schedule_name, num_diffusion_timesteps):
"""
Get a pre-defined beta schedule for the given name.
The beta schedule library consists of beta schedules which remain similar
in the limit of num_diffusion_timesteps.
Beta schedules may be added, but should not be removed or changed once
they are committed to maintain backwards compatibility.
"""
if schedule_name == "linear":
# Linear schedule from Ho et al, extended to work for any number of
# diffusion steps.
scale = 1000 / num_diffusion_timesteps
beta_start = scale * 0.0001
beta_end = scale * 0.02
return np.linspace(
beta_start, beta_end, num_diffusion_timesteps, dtype=np.float64
)
elif schedule_name == "cosine":
return betas_for_alpha_bar(
num_diffusion_timesteps,
lambda t: math.cos((t + 0.008) / 1.008 * math.pi / 2) ** 2,
)
else:
raise NotImplementedError(f"unknown beta schedule: {schedule_name}")
def betas_for_alpha_bar(num_diffusion_timesteps, alpha_bar, max_beta=0.999):
"""
Create a beta schedule that discretizes the given alpha_t_bar function,
which defines the cumulative product of (1-beta) over time from t = [0,1].
:param num_diffusion_timesteps: the number of betas to produce.
:param alpha_bar: a lambda that takes an argument t from 0 to 1 and
produces the cumulative product of (1-beta) up to that
part of the diffusion process.
:param max_beta: the maximum beta to use; use values lower than 1 to
prevent singularities.
"""
betas = []
for i in range(num_diffusion_timesteps):
t1 = i / num_diffusion_timesteps
t2 = (i + 1) / num_diffusion_timesteps
betas.append(min(1 - alpha_bar(t2) / alpha_bar(t1), max_beta))
return np.array(betas)
class GaussianMultinomialDiffusion(torch.nn.Module):
def __init__(
self,
opt,
num_classes: np.array,
num_numerical_features: int,
num_timesteps=1000,
gaussian_loss_type='mse',
gaussian_parametrization='eps',
multinomial_loss_type='vb_stochastic',
parametrization='x0',
scheduler='cosine',
device=torch.device('cuda:0')
):
super(GaussianMultinomialDiffusion, self).__init__() # 调用父类的初始化方法
assert multinomial_loss_type in ('vb_stochastic', 'vb_all') # 断言多项式损失类型在有效范围内
assert parametrization in ('x0', 'direct') # 断言扩散参数化方式在有效范围内
if multinomial_loss_type == 'vb_all':
print('Computing the loss using the bound on _all_ timesteps.'
' This is expensive both in terms of memory and computation.')
# 如果多项式损失类型为'vb_all',则打印警告信息
self.num_numerical_features = num_numerical_features # 将数值特征数目赋值给实例属性
self.num_classes = num_classes # 将类别数目赋值给实例属性
self.num_classes_expanded = torch.from_numpy(
np.concatenate([num_classes[i].repeat(num_classes[i]) for i in range(len(num_classes))])
).to(device)
# 将类别数目数组按照每个元素重复自身次数进行拼接,并转换为torch张量
self.slices_for_classes = [np.arange(self.num_classes[0])]
offsets = np.cumsum(self.num_classes)
for i in range(1, len(offsets)):
self.slices_for_classes.append(np.arange(offsets[i - 1], offsets[i]))
self.offsets = torch.from_numpy(np.append([0], offsets)).to(device)
# 计算每个类别对应的切片和偏移量,并转换为torch张量
# self._denoise_fn = denoise_fn
self.gaussian_loss_type = gaussian_loss_type
self.gaussian_parametrization = gaussian_parametrization
self.multinomial_loss_type = multinomial_loss_type
self.num_timesteps = num_timesteps
self.parametrization = parametrization
self.scheduler = scheduler
alphas = 1. - get_named_beta_schedule(scheduler, num_timesteps)
alphas = torch.tensor(alphas.astype('float64'))
betas = 1. - alphas
log_alpha = np.log(alphas)
log_cumprod_alpha = np.cumsum(log_alpha)
log_1_min_alpha = log_1_min_a(log_alpha)
log_1_min_cumprod_alpha = log_1_min_a(log_cumprod_alpha)
alphas_cumprod = np.cumprod(alphas, axis=0)
alphas_cumprod_prev = torch.tensor(np.append(1.0, alphas_cumprod[:-1]))
alphas_cumprod_next = torch.tensor(np.append(alphas_cumprod[1:], 0.0))
sqrt_alphas_cumprod = np.sqrt(alphas_cumprod)
sqrt_one_minus_alphas_cumprod = np.sqrt(1.0 - alphas_cumprod)
sqrt_recip_alphas_cumprod = np.sqrt(1.0 / alphas_cumprod)
sqrt_recipm1_alphas_cumprod = np.sqrt(1.0 / alphas_cumprod - 1)
# Gaussian diffusion
self.posterior_variance = (
betas * (1.0 - alphas_cumprod_prev) / (1.0 - alphas_cumprod)
)
self.posterior_log_variance_clipped = torch.from_numpy(
np.log(np.append(self.posterior_variance[1], self.posterior_variance[1:]))
).float().to(device)
self.posterior_mean_coef1 = (
betas * np.sqrt(alphas_cumprod_prev) / (1.0 - alphas_cumprod)
).float().to(device)
self.posterior_mean_coef2 = (
(1.0 - alphas_cumprod_prev)
* np.sqrt(alphas.numpy())
/ (1.0 - alphas_cumprod)
).float().to(device)
assert log_add_exp(log_alpha, log_1_min_alpha).abs().sum().item() < 1.e-5
assert log_add_exp(log_cumprod_alpha, log_1_min_cumprod_alpha).abs().sum().item() < 1e-5
assert (np.cumsum(log_alpha) - log_cumprod_alpha).abs().sum().item() < 1.e-5
# Convert to float32 and register buffers.
self.register_buffer('alphas', alphas.float().to(device))
self.register_buffer('log_alpha', log_alpha.float().to(device))
self.register_buffer('log_1_min_alpha', log_1_min_alpha.float().to(device))
self.register_buffer('log_1_min_cumprod_alpha', log_1_min_cumprod_alpha.float().to(device))
self.register_buffer('log_cumprod_alpha', log_cumprod_alpha.float().to(device))
self.register_buffer('alphas_cumprod', alphas_cumprod.float().to(device))
self.register_buffer('alphas_cumprod_prev', alphas_cumprod_prev.float().to(device))
self.register_buffer('alphas_cumprod_next', alphas_cumprod_next.float().to(device))
self.register_buffer('sqrt_alphas_cumprod', sqrt_alphas_cumprod.float().to(device))
self.register_buffer('sqrt_one_minus_alphas_cumprod', sqrt_one_minus_alphas_cumprod.float().to(device))
self.register_buffer('sqrt_recip_alphas_cumprod', sqrt_recip_alphas_cumprod.float().to(device))
self.register_buffer('sqrt_recipm1_alphas_cumprod', sqrt_recipm1_alphas_cumprod.float().to(device))
self.register_buffer('Lt_history', torch.zeros(num_timesteps))
self.register_buffer('Lt_count', torch.zeros(num_timesteps))
self._denoise_fn = DiffusionWrapper(opt)
print('Diffusion model ready!')
# Gaussian part
def gaussian_q_mean_variance(self, x_start, t):
mean = (
extract(self.sqrt_alphas_cumprod, t, x_start.shape) * x_start
)
variance = extract(1.0 - self.alphas_cumprod, t, x_start.shape)
log_variance = extract(
self.log_1_min_cumprod_alpha, t, x_start.shape
)
return mean, variance, log_variance
def gaussian_q_sample(self, x_start, t, noise=None):
if noise is None:
noise = torch.randn_like(x_start)
assert noise.shape == x_start.shape
return (
extract(self.sqrt_alphas_cumprod, t, x_start.shape) * x_start
+ extract(self.sqrt_one_minus_alphas_cumprod, t, x_start.shape)
* noise
)
def gaussian_q_posterior_mean_variance(self, x_start, x_t, t):
assert x_start.shape == x_t.shape
posterior_mean = (
extract(self.posterior_mean_coef1, t, x_t.shape) * x_start
+ extract(self.posterior_mean_coef2, t, x_t.shape) * x_t
)
posterior_variance = extract(self.posterior_variance, t, x_t.shape)
posterior_log_variance_clipped = extract(
self.posterior_log_variance_clipped, t, x_t.shape
)
assert (
posterior_mean.shape[0]
== posterior_variance.shape[0]
== posterior_log_variance_clipped.shape[0]
== x_start.shape[0]
)
return posterior_mean, posterior_variance, posterior_log_variance_clipped
def gaussian_p_mean_variance(
self, model_output, x, t, clip_denoised=False, denoised_fn=None, model_kwargs=None
):
if model_kwargs is None:
model_kwargs = {}
B, C = x.shape[:2]
assert t.shape == (B,)
model_variance = torch.cat([self.posterior_variance[1].unsqueeze(0).to(x.device), (1. - self.alphas)[1:]],
dim=0)
# model_variance = self.posterior_variance.to(x.device)
model_log_variance = torch.log(model_variance)
model_variance = extract(model_variance, t, x.shape)
model_log_variance = extract(model_log_variance, t, x.shape)
if self.gaussian_parametrization == 'eps':
pred_xstart = self._predict_xstart_from_eps(x_t=x, t=t, eps=model_output)
elif self.gaussian_parametrization == 'x0':
pred_xstart = model_output
else:
raise NotImplementedError
model_mean, _, _ = self.gaussian_q_posterior_mean_variance(
x_start=pred_xstart, x_t=x, t=t
)
assert (
model_mean.shape == model_log_variance.shape == pred_xstart.shape == x.shape
), f'{model_mean.shape}, {model_log_variance.shape}, {pred_xstart.shape}, {x.shape}'
return {
"mean": model_mean,
"variance": model_variance,
"log_variance": model_log_variance,
"pred_xstart": pred_xstart,
}
def _vb_terms_bpd(
self, model_output, x_start, x_t, t, clip_denoised=False, model_kwargs=None
):
true_mean, _, true_log_variance_clipped = self.gaussian_q_posterior_mean_variance(
x_start=x_start, x_t=x_t, t=t
)
out = self.gaussian_p_mean_variance(
model_output, x_t, t, clip_denoised=clip_denoised, model_kwargs=model_kwargs
)
kl = normal_kl(
true_mean, true_log_variance_clipped, out["mean"], out["log_variance"]
)
kl = mean_flat(kl) / np.log(2.0)
decoder_nll = -discretized_gaussian_log_likelihood(
x_start, means=out["mean"], log_scales=0.5 * out["log_variance"]
)
assert decoder_nll.shape == x_start.shape
decoder_nll = mean_flat(decoder_nll) / np.log(2.0)
# At the first timestep return the decoder NLL,
# otherwise return KL(q(x_{t-1}|x_t,x_0) || p(x_{t-1}|x_t))
output = torch.where((t == 0), decoder_nll, kl)
return {"output": output, "pred_xstart": out["pred_xstart"], "out_mean": out["mean"], "true_mean": true_mean}
def _prior_gaussian(self, x_start):
"""
Get the prior KL term for the variational lower-bound, measured in
bits-per-dim.
This term can't be optimized, as it only depends on the encoder.
:param x_start: the [N x C x ...] tensor of inputs.
:return: a batch of [N] KL values (in bits), one per batch element.
"""
batch_size = x_start.shape[0]
t = torch.tensor([self.num_timesteps - 1] * batch_size, device=x_start.device)
qt_mean, _, qt_log_variance = self.gaussian_q_mean_variance(x_start, t)
kl_prior = normal_kl(
mean1=qt_mean, logvar1=qt_log_variance, mean2=0.0, logvar2=0.0
)
return mean_flat(kl_prior) / np.log(2.0)
def _gaussian_loss(self, model_out, x_start, x_t, t, noise, model_kwargs=None):
if model_kwargs is None:
model_kwargs = {}
terms = {}
if self.gaussian_loss_type == 'mse':
terms["loss"] = mean_flat((noise - model_out) ** 2)
elif self.gaussian_loss_type == 'kl':
terms["loss"] = self._vb_terms_bpd(
model_output=model_out,
x_start=x_start,
x_t=x_t,
t=t,
clip_denoised=False,
model_kwargs=model_kwargs,
)["output"]
return terms['loss']
def _predict_xstart_from_eps(self, x_t, t, eps):
assert x_t.shape == eps.shape
return (
extract(self.sqrt_recip_alphas_cumprod, t, x_t.shape) * x_t
- extract(self.sqrt_recipm1_alphas_cumprod, t, x_t.shape) * eps
)
def _predict_eps_from_xstart(self, x_t, t, pred_xstart):
return (
extract(self.sqrt_recip_alphas_cumprod, t, x_t.shape) * x_t
- pred_xstart
) / extract(self.sqrt_recipm1_alphas_cumprod, t, x_t.shape)
def gaussian_p_sample(
self,
model_out,
x,
t,
clip_denoised=False,
denoised_fn=None,
model_kwargs=None,
):
out = self.gaussian_p_mean_variance(
model_out,
x,
t,
clip_denoised=clip_denoised,
denoised_fn=denoised_fn,
model_kwargs=model_kwargs,
)
noise = torch.randn_like(x)
nonzero_mask = (
(t != 0).float().view(-1, *([1] * (len(x.shape) - 1)))
) # no noise when t == 0
sample = out["mean"] + nonzero_mask * torch.exp(0.5 * out["log_variance"]) * noise
return {"sample": sample, "pred_xstart": out["pred_xstart"]}
# Multinomial part
def multinomial_kl(self, log_prob1, log_prob2):
kl = (log_prob1.exp() * (log_prob1 - log_prob2)).sum(dim=1)
return kl
def q_pred_one_timestep(self, log_x_t, t):
log_alpha_t = extract(self.log_alpha, t, log_x_t.shape)
log_1_min_alpha_t = extract(self.log_1_min_alpha, t, log_x_t.shape)
# alpha_t * E[xt] + (1 - alpha_t) 1 / K
log_probs = log_add_exp(
log_x_t + log_alpha_t,
log_1_min_alpha_t - torch.log(self.num_classes_expanded)
)
return log_probs
def q_pred(self, log_x_start, t):
log_cumprod_alpha_t = extract(self.log_cumprod_alpha, t, log_x_start.shape)
log_1_min_cumprod_alpha = extract(self.log_1_min_cumprod_alpha, t, log_x_start.shape)
log_probs = log_add_exp(
log_x_start + log_cumprod_alpha_t,
log_1_min_cumprod_alpha - torch.log(self.num_classes_expanded)
)
return log_probs
def predict_start(self, model_out, log_x_t, t, out_dict):
# model_out = self._denoise_fn(x_t, t.to(x_t.device), **out_dict)
assert model_out.size(0) == log_x_t.size(0)
assert model_out.size(1) == self.num_classes.sum(), f'{model_out.size()}'
log_pred = torch.empty_like(model_out)
for ix in self.slices_for_classes:
log_pred[:, ix] = F.log_softmax(model_out[:, ix], dim=1)
return log_pred
def q_posterior(self, log_x_start, log_x_t, t):
# q(xt-1 | xt, x0) = q(xt | xt-1, x0) * q(xt-1 | x0) / q(xt | x0)
# where q(xt | xt-1, x0) = q(xt | xt-1).
# EV_log_qxt_x0 = self.q_pred(log_x_start, t)
# print('sum exp', EV_log_qxt_x0.exp().sum(1).mean())
# assert False
# log_qxt_x0 = (log_x_t.exp() * EV_log_qxt_x0).sum(dim=1)
t_minus_1 = t - 1
# Remove negative values, will not be used anyway for final decoder
t_minus_1 = torch.where(t_minus_1 < 0, torch.zeros_like(t_minus_1), t_minus_1)
log_EV_qxtmin_x0 = self.q_pred(log_x_start, t_minus_1)
num_axes = (1,) * (len(log_x_start.size()) - 1)
t_broadcast = t.to(log_x_start.device).view(-1, *num_axes) * torch.ones_like(log_x_start)
log_EV_qxtmin_x0 = torch.where(t_broadcast == 0, log_x_start, log_EV_qxtmin_x0.to(torch.float32))
# unnormed_logprobs = log_EV_qxtmin_x0 +
# log q_pred_one_timestep(x_t, t)
# Note: _NOT_ x_tmin1, which is how the formula is typically used!!!
# Not very easy to see why this is true. But it is :)
unnormed_logprobs = log_EV_qxtmin_x0 + self.q_pred_one_timestep(log_x_t, t)
log_EV_xtmin_given_xt_given_xstart = \
unnormed_logprobs \
- sliced_logsumexp(unnormed_logprobs, self.offsets)
return log_EV_xtmin_given_xt_given_xstart
def p_pred(self, model_out, log_x, t, out_dict):
if self.parametrization == 'x0':
log_x_recon = self.predict_start(model_out, log_x, t=t, out_dict=out_dict)
log_model_pred = self.q_posterior(
log_x_start=log_x_recon, log_x_t=log_x, t=t)
elif self.parametrization == 'direct':
log_model_pred = self.predict_start(model_out, log_x, t=t, out_dict=out_dict)
else:
raise ValueError
return log_model_pred
@torch.no_grad()
def p_sample(self, model_out, log_x, t, out_dict):
model_log_prob = self.p_pred(model_out, log_x=log_x, t=t, out_dict=out_dict)
out = self.log_sample_categorical(model_log_prob)
return out
@torch.no_grad()
def p_sample_loop(self, shape, out_dict):
device = self.log_alpha.device
b = shape[0]
# start with random normal image.
img = torch.randn(shape, device=device)
for i in reversed(range(1, self.num_timesteps)):
img = self.p_sample(img, torch.full((b,), i, device=device, dtype=torch.long), out_dict)
return img
@torch.no_grad()
def _sample(self, image_size, out_dict, batch_size=16):
return self.p_sample_loop((batch_size, 3, image_size, image_size), out_dict)
@torch.no_grad()
def interpolate(self, x1, x2, t=None, lam=0.5):
b, *_, device = *x1.shape, x1.device
t = default(t, self.num_timesteps - 1)
assert x1.shape == x2.shape
t_batched = torch.stack([torch.tensor(t, device=device)] * b)
xt1, xt2 = map(lambda x: self.q_sample(x, t=t_batched), (x1, x2))
img = (1 - lam) * xt1 + lam * xt2
for i in reversed(range(0, t)):
img = self.p_sample(img, torch.full((b,), i, device=device, dtype=torch.long))
return img
def log_sample_categorical(self, logits):
full_sample = []
for i in range(len(self.num_classes)):
one_class_logits = logits[:, self.slices_for_classes[i]]
uniform = torch.rand_like(one_class_logits)
gumbel_noise = -torch.log(-torch.log(uniform + 1e-30) + 1e-30)
sample = (gumbel_noise + one_class_logits).argmax(dim=1)
full_sample.append(sample.unsqueeze(1))
full_sample = torch.cat(full_sample, dim=1)
log_sample = index_to_log_onehot(full_sample, self.num_classes)
return log_sample
def q_sample(self, log_x_start, t):
log_EV_qxt_x0 = self.q_pred(log_x_start, t)
log_sample = self.log_sample_categorical(log_EV_qxt_x0)
return log_sample
def nll(self, log_x_start, out_dict):
b = log_x_start.size(0)
device = log_x_start.device
loss = 0
for t in range(0, self.num_timesteps):
t_array = (torch.ones(b, device=device) * t).long()
kl = self.compute_Lt(
log_x_start=log_x_start,
log_x_t=self.q_sample(log_x_start=log_x_start, t=t_array),
t=t_array,
out_dict=out_dict)
loss += kl
loss += self.kl_prior(log_x_start)
return loss
def kl_prior(self, log_x_start):
b = log_x_start.size(0)
device = log_x_start.device
ones = torch.ones(b, device=device).long()
log_qxT_prob = self.q_pred(log_x_start, t=(self.num_timesteps - 1) * ones)
log_half_prob = -torch.log(self.num_classes_expanded * torch.ones_like(log_qxT_prob))
kl_prior = self.multinomial_kl(log_qxT_prob, log_half_prob)
return sum_except_batch(kl_prior)
def compute_Lt(self, model_out, log_x_start, log_x_t, t, out_dict, detach_mean=False):
log_true_prob = self.q_posterior(
log_x_start=log_x_start, log_x_t=log_x_t, t=t)
log_model_prob = self.p_pred(model_out, log_x=log_x_t, t=t, out_dict=out_dict)
if detach_mean:
log_model_prob = log_model_prob.detach()
kl = self.multinomial_kl(log_true_prob, log_model_prob)
kl = sum_except_batch(kl)
decoder_nll = -log_categorical(log_x_start, log_model_prob)
decoder_nll = sum_except_batch(decoder_nll)
mask = (t == torch.zeros_like(t)).float()
loss = mask * decoder_nll + (1. - mask) * kl
return loss
def sample_time(self, b, device, method='uniform'):
if method == 'importance':
if not (self.Lt_count > 10).all():
return self.sample_time(b, device, method='uniform')
Lt_sqrt = torch.sqrt(self.Lt_history + 1e-10) + 0.0001
Lt_sqrt[0] = Lt_sqrt[1] # Overwrite decoder term with L1.
pt_all = (Lt_sqrt / Lt_sqrt.sum()).to(device)
t = torch.multinomial(pt_all, num_samples=b, replacement=True).to(device)
pt = pt_all.gather(dim=0, index=t)
return t, pt
elif method == 'uniform':
t = torch.randint(0, self.num_timesteps, (b,), device=device).long()
pt = torch.ones_like(t).float() / self.num_timesteps
return t, pt
else:
raise ValueError
def _multinomial_loss(self, model_out, log_x_start, log_x_t, t, pt, out_dict):
if self.multinomial_loss_type == 'vb_stochastic':
kl = self.compute_Lt(
model_out, log_x_start, log_x_t, t, out_dict
)
kl_prior = self.kl_prior(log_x_start)
# Upweigh loss term of the kl
vb_loss = kl / pt + kl_prior
return vb_loss
elif self.multinomial_loss_type == 'vb_all':
# Expensive, dont do it ;).
# DEPRECATED
return -self.nll(log_x_start)
else:
raise ValueError()
def log_prob(self, x, out_dict):
b, device = x.size(0), x.device
if self.training:
return self._multinomial_loss(x, out_dict)
else:
log_x_start = index_to_log_onehot(x, self.num_classes)
t, pt = self.sample_time(b, device, 'importance')
kl = self.compute_Lt(
log_x_start, self.q_sample(log_x_start=log_x_start, t=t), t, out_dict)
kl_prior = self.kl_prior(log_x_start)
# Upweigh loss term of the kl
loss = kl / pt + kl_prior
return -loss
def mixed_loss(self, x, out_dict):
b = x.shape[0]
device = x.device
t, pt = self.sample_time(b, device, 'uniform')
x_num = x[:, :self.num_numerical_features]
x_cat = x[:, self.num_numerical_features:]
x_num_t = x_num
log_x_cat_t = x_cat
if x_num.shape[1] > 0:
noise = torch.randn_like(x_num)
x_num_t = self.gaussian_q_sample(x_num, t, noise=noise)
if x_cat.shape[1] > 0:
log_x_cat = index_to_log_onehot(x_cat.long(), self.num_classes)
# log_x_cat = x_cat
log_x_cat_t = self.q_sample(log_x_start=log_x_cat, t=t)
x_in = torch.cat([x_num_t, log_x_cat_t], dim=1)
model_out = self._denoise_fn(
x_in,
t,
**out_dict
)
model_out_num = model_out[:, :self.num_numerical_features]
model_out_cat = model_out[:, self.num_numerical_features:]
loss_multi = torch.zeros((1,)).float()
loss_gauss = torch.zeros((1,)).float()
if x_cat.shape[1] > 0:
loss_multi = self._multinomial_loss(model_out_cat, log_x_cat, log_x_cat_t, t, pt, out_dict) / len(
self.num_classes)
if x_num.shape[1] > 0:
loss_gauss = self._gaussian_loss(model_out_num, x_num, x_num_t, t, noise)
# loss_multi = torch.where(out_dict['y'] == 1, loss_multi, 2 * loss_multi)
# loss_gauss = torch.where(out_dict['y'] == 1, loss_gauss, 2 * loss_gauss)
return loss_multi.mean(), loss_gauss.mean()
@torch.no_grad()
def mixed_elbo(self, x0, out_dict):
b = x0.size(0)
device = x0.device
x_num = x0[:, :self.num_numerical_features]
x_cat = x0[:, self.num_numerical_features:]
has_cat = x_cat.shape[1] > 0
if has_cat:
log_x_cat = index_to_log_onehot(x_cat.long(), self.num_classes).to(device)
gaussian_loss = []
xstart_mse = []
mse = []
mu_mse = []
out_mean = []
true_mean = []
multinomial_loss = []
for t in range(self.num_timesteps):
t_array = (torch.ones(b, device=device) * t).long()
noise = torch.randn_like(x_num)
x_num_t = self.gaussian_q_sample(x_start=x_num, t=t_array, noise=noise)
if has_cat:
log_x_cat_t = self.q_sample(log_x_start=log_x_cat, t=t_array)
else:
log_x_cat_t = x_cat
model_out = self._denoise_fn(
torch.cat([x_num_t, log_x_cat_t], dim=1),
t_array,
**out_dict
)
model_out_num = model_out[:, :self.num_numerical_features]
model_out_cat = model_out[:, self.num_numerical_features:]
kl = torch.tensor([0.0])
if has_cat:
kl = self.compute_Lt(
model_out=model_out_cat,
log_x_start=log_x_cat,
log_x_t=log_x_cat_t,
t=t_array,
out_dict=out_dict
)
out = self._vb_terms_bpd(
model_out_num,
x_start=x_num,
x_t=x_num_t,
t=t_array,
clip_denoised=False
)
multinomial_loss.append(kl)
gaussian_loss.append(out["output"])
xstart_mse.append(mean_flat((out["pred_xstart"] - x_num) ** 2))
# mu_mse.append(mean_flat(out["mean_mse"]))
out_mean.append(mean_flat(out["out_mean"]))
true_mean.append(mean_flat(out["true_mean"]))
eps = self._predict_eps_from_xstart(x_num_t, t_array, out["pred_xstart"])
mse.append(mean_flat((eps - noise) ** 2))
gaussian_loss = torch.stack(gaussian_loss, dim=1)
multinomial_loss = torch.stack(multinomial_loss, dim=1)
xstart_mse = torch.stack(xstart_mse, dim=1)
mse = torch.stack(mse, dim=1)
# mu_mse = torch.stack(mu_mse, dim=1)
out_mean = torch.stack(out_mean, dim=1)
true_mean = torch.stack(true_mean, dim=1)
prior_gauss = self._prior_gaussian(x_num)
prior_multin = torch.tensor([0.0])
if has_cat:
prior_multin = self.kl_prior(log_x_cat)
total_gauss = gaussian_loss.sum(dim=1) + prior_gauss
total_multin = multinomial_loss.sum(dim=1) + prior_multin
return {
"total_gaussian": total_gauss,
"total_multinomial": total_multin,
"losses_gaussian": gaussian_loss,
"losses_multinimial": multinomial_loss,
"xstart_mse": xstart_mse,
"mse": mse,
# "mu_mse": mu_mse
"out_mean": out_mean,
"true_mean": true_mean
}
@torch.no_grad()
def gaussian_ddim_step(
self,
model_out_num,
x,
t,
clip_denoised=False,
denoised_fn=None,
eta=0.0
):
out = self.gaussian_p_mean_variance(
model_out_num,
x,
t,
clip_denoised=clip_denoised,
denoised_fn=denoised_fn,
model_kwargs=None,
)
eps = self._predict_eps_from_xstart(x, t, out["pred_xstart"])
alpha_bar = extract(self.alphas_cumprod, t, x.shape)
alpha_bar_prev = extract(self.alphas_cumprod_prev, t, x.shape)
sigma = (
eta
* torch.sqrt((1 - alpha_bar_prev) / (1 - alpha_bar))
* torch.sqrt(1 - alpha_bar / alpha_bar_prev)
)
noise = torch.randn_like(x)
mean_pred = (
out["pred_xstart"] * torch.sqrt(alpha_bar_prev)
+ torch.sqrt(1 - alpha_bar_prev - sigma ** 2) * eps
)
nonzero_mask = (
(t != 0).float().view(-1, *([1] * (len(x.shape) - 1)))
) # no noise when t == 0
sample = mean_pred + nonzero_mask * sigma * noise
return sample
@torch.no_grad()
def gaussian_ddim_sample(
self,
noise,
T,
out_dict,
eta=0.0
):
x = noise
b = x.shape[0]
device = x.device
for t in reversed(range(T)):
print(f'Sample timestep {t:4d}', end='\r')
t_array = (torch.ones(b, device=device) * t).long()
out_num = self._denoise_fn(x, t_array, **out_dict)
x = self.gaussian_ddim_step(
out_num,
x,
t_array
)
print()
return x
@torch.no_grad()
def gaussian_ddim_reverse_step(
self,
model_out_num,
x,
t,
clip_denoised=False,
eta=0.0
):
assert eta == 0.0, "Eta must be zero."
out = self.gaussian_p_mean_variance(
model_out_num,
x,
t,
clip_denoised=clip_denoised,
denoised_fn=None,
model_kwargs=None,
)
eps = (
extract(self.sqrt_recip_alphas_cumprod, t, x.shape) * x
- out["pred_xstart"]
) / extract(self.sqrt_recipm1_alphas_cumprod, t, x.shape)
alpha_bar_next = extract(self.alphas_cumprod_next, t, x.shape)
mean_pred = (
out["pred_xstart"] * torch.sqrt(alpha_bar_next)
+ torch.sqrt(1 - alpha_bar_next) * eps
)
return mean_pred
@torch.no_grad()
def gaussian_ddim_reverse_sample(
self,
x,
T,
out_dict,
):
b = x.shape[0]
device = x.device
for t in range(T):
print(f'Reverse timestep {t:4d}', end='\r')
t_array = (torch.ones(b, device=device) * t).long()
out_num = self._denoise_fn(x, t_array, **out_dict)
x = self.gaussian_ddim_reverse_step(
out_num,
x,
t_array,
eta=0.0
)
print()
return x
@torch.no_grad()
def multinomial_ddim_step(
self,
model_out_cat,
log_x_t,
t,
out_dict,
eta=0.0
):
# not ddim, essentially
log_x0 = self.predict_start(model_out_cat, log_x_t=log_x_t, t=t, out_dict=out_dict)
alpha_bar = extract(self.alphas_cumprod, t, log_x_t.shape)
alpha_bar_prev = extract(self.alphas_cumprod_prev, t, log_x_t.shape)
sigma = (
eta
* torch.sqrt((1 - alpha_bar_prev) / (1 - alpha_bar))
* torch.sqrt(1 - alpha_bar / alpha_bar_prev)
)
coef1 = sigma
coef2 = alpha_bar_prev - sigma * alpha_bar
coef3 = 1 - coef1 - coef2
log_ps = torch.stack([
torch.log(coef1) + log_x_t,
torch.log(coef2) + log_x0,
torch.log(coef3) - torch.log(self.num_classes_expanded)
], dim=2)
log_prob = torch.logsumexp(log_ps, dim=2)
out = self.log_sample_categorical(log_prob)
return out
@torch.no_grad()
def sample_ddim(self, num_samples, y_dist):
b = num_samples
device = self.log_alpha.device
z_norm = torch.randn((b, self.num_numerical_features), device=device)
has_cat = self.num_classes[0] != 0
log_z = torch.zeros((b, 0), device=device).float()
if has_cat:
uniform_logits = torch.zeros((b, len(self.num_classes_expanded)), device=device)
log_z = self.log_sample_categorical(uniform_logits)
y = torch.multinomial(
y_dist,
num_samples=b,
replacement=True
)
out_dict = {'y': y.long().to(device)}
for i in reversed(range(0, self.num_timesteps)):
print(f'Sample timestep {i:4d}', end='\r')
t = torch.full((b,), i, device=device, dtype=torch.long)
model_out = self._denoise_fn(
torch.cat([z_norm, log_z], dim=1).float(),
t,
**out_dict
)
model_out_num = model_out[:, :self.num_numerical_features]
model_out_cat = model_out[:, self.num_numerical_features:]
z_norm = self.gaussian_ddim_step(model_out_num, z_norm, t, clip_denoised=False)
if has_cat:
log_z = self.multinomial_ddim_step(model_out_cat, log_z, t, out_dict)
print()
z_ohe = torch.exp(log_z).round()
z_cat = log_z
if has_cat:
z_cat = ohe_to_categories(z_ohe, self.num_classes)
sample = torch.cat([z_norm, z_cat], dim=1).cpu()
return sample, out_dict
@torch.no_grad()
def sample(self, num_samples, y_dist, allow_cat = True):
b = num_samples
device = self.log_alpha.device
z_norm = torch.randn((b, self.num_numerical_features), device=device)
has_cat = self.num_classes[0] != 0
log_z = torch.zeros((b, 0), device=device).float()
if has_cat and allow_cat:
uniform_logits = torch.zeros((b, len(self.num_classes_expanded)), device=device)
log_z = self.log_sample_categorical(uniform_logits)
# y = torch.multinomial(
# y_dist,
# num_samples=b,
# replacement=True
# )
out_dict = {'y': y_dist.to(device)}
for i in reversed(range(0, self.num_timesteps)):
print(f'Sample timestep {i:4d}', end='\r')
t = torch.full((b,), i, device=device, dtype=torch.long)
model_out = self._denoise_fn(
torch.cat([z_norm, log_z], dim=1).float(),
t,
**out_dict
)
model_out_num = model_out[:, :self.num_numerical_features]
model_out_cat = model_out[:, self.num_numerical_features:]
z_norm = self.gaussian_p_sample(model_out_num, z_norm, t, clip_denoised=False)['sample']
if has_cat and allow_cat:
log_z = self.p_sample(model_out_cat, log_z, t, out_dict)
print()
z_ohe = torch.exp(log_z).round()
z_cat = log_z
if has_cat and allow_cat:
z_cat = ohe_to_categories(z_ohe, self.num_classes)
sample = torch.cat([z_norm, z_cat], dim=1).cpu()
return sample, out_dict
def sample_all(self, num_samples, batch_size, y_dist, ddim=False):
if ddim:
print('Sample using DDIM.')
sample_fn = self.sample_ddim
else:
sample_fn = self.sample
b = batch_size
all_y = []
all_c = []
all_samples = []
num_generated = 0
time_gen = 0
while num_generated < num_samples:
# while time_gen*b < num_samples:
b = batch_size
if time_gen*b < num_samples:
y_dist_ = y_dist[time_gen*b:b*(time_gen+1)]
else:
y_dist_ = y_dist[:num_samples-num_generated]
b = num_samples-num_generated
sample, out_dict = sample_fn(b, y_dist_)
mask_nan = torch.any(sample.isnan(), dim=1)
sample = sample[~mask_nan]
out_dict['y'] = out_dict['y'][~mask_nan]
all_samples.append(sample[:,:self.num_numerical_features])
all_c.append(sample[:,self.num_numerical_features:])
all_y.append(out_dict['y'].cpu())
# if sample.shape[0] != b:
# raise FoundNANsError
num_generated += sample.shape[0]
time_gen += 1
print('DM gen number is {}'.format(num_generated))
x_gen = torch.cat(all_samples, dim=0)[:num_samples]
c_gen = torch.cat(all_c, dim=0)[:num_samples]