-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsemanticdiffusion.py
More file actions
1220 lines (996 loc) · 49.5 KB
/
semanticdiffusion.py
File metadata and controls
1220 lines (996 loc) · 49.5 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
from torch import autocast, inference_mode
import torchvision.transforms as T
import numpy as np
from dataclasses import dataclass
from tqdm.auto import tqdm
from utils import image_grid
from diffusers import (
UNet2DModel,
VQModel,
DDIMScheduler,
UNet2DConditionModel,
StableDiffusionPipeline
)
from utils import pil_to_tensor
from PIL import Image
from glob import glob
import copy
from transformers import CLIPTextModel, CLIPTokenizer
from typing import Any, Dict, List, Optional, Tuple, Union
import logging
def forward_ResnetBlock2D(self, input_tensor, temb, inject_into_botleneck = None):
## From https://github.com/huggingface/diffusers/blob/fc94c60c8373862c509e388f3f4065d98cedf589/src/diffusers/models/resnet.py#L367
hidden_states = input_tensor
hidden_states = self.norm1(hidden_states)
hidden_states = self.nonlinearity(hidden_states)
if self.upsample is not None:
# upsample_nearest_nhwc fails with large batch sizes. see https://github.com/huggingface/diffusers/issues/984
if hidden_states.shape[0] >= 64:
input_tensor = input_tensor.contiguous()
hidden_states = hidden_states.contiguous()
input_tensor = self.upsample(input_tensor)
hidden_states = self.upsample(hidden_states)
elif self.upsample is not None:
input_tensor = self.downsample(input_tensor)
hidden_states = self.downsample(hidden_states)
hidden_states = self.conv1(hidden_states)
if temb is not None:
temb = self.time_emb_proj(self.nonlinearity(temb))[:, :, None, None]
hidden_states = hidden_states + temb
hidden_states = self.norm2(hidden_states)
### Middle of block injection
bottleneck = hidden_states.clone()
if not inject_into_botleneck is None:
hidden_states = bottleneck + inject_into_botleneck
hidden_states = self.nonlinearity(hidden_states) #### <----
hidden_states = self.dropout(hidden_states)
hidden_states = self.conv2(hidden_states)
# if self.conv_shortcut is not None:
# input_tensor = self.conv_shortcut(input_tensor)
output_tensor = (input_tensor + hidden_states)
return output_tensor, bottleneck
def mid_block_forward(self, hidden_states, temb=None, encoder_states=None, inject_into_botleneck=None):
## https://github.com/huggingface/diffusers/blob/fc94c60c8373862c509e388f3f4065d98cedf589/src/diffusers/models/unet_2d_blocks.py#L246
hidden_states = self.resnets[0](hidden_states, temb)
for attn, resnet in zip(self.attentions, self.resnets[1:]):
hidden_states = attn(hidden_states)
hidden_states, bottleneck = forward_ResnetBlock2D(resnet,hidden_states, temb, inject_into_botleneck = inject_into_botleneck)
return hidden_states, bottleneck
@dataclass
class UNetOutput:
"""
Args:
sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
Hidden states output. Output of last layer of model.
"""
out: torch.FloatTensor
h: torch.FloatTensor
class UNet:
def __init__(self, model: UNet2DModel, h_space = None):
assert h_space in [None, "before","after", "middle"]
self.h_space = h_space
self.model = model
self.device = model.device
#self.sanity_check()
def sanity_check(self):
assert self.model.config.center_input_sample == False
assert self.model.config.time_embedding_type == 'positional'
def time_embedding(self, timestep, batch_dim):
# 1. time
timesteps = timestep
if not torch.is_tensor(timesteps):
timesteps = torch.tensor([timesteps], dtype=torch.long, device=self.device)
elif torch.is_tensor(timesteps) and len(timesteps.shape) == 0:
timesteps = timesteps[None].to(self.device)
# broadcast to batch dimension in a way that's compatible with ONNX/Core ML
timesteps = timesteps * torch.ones(batch_dim, dtype=timesteps.dtype, device=timesteps.device)
t_emb = self.model.time_proj(timesteps)
emb = self.model.time_embedding(t_emb)
return emb
def forward(self, sample, timestep, delta_h = None):
# Modified from From: https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/unet_2d.py
# 1. Positional embedding
emb = self.time_embedding( timestep, batch_dim = sample.shape[0])
# 2. pre-process
skip_sample = sample
sample = self.model.conv_in(sample)
# 3. down
down_block_res_samples = (sample,)
for downsample_block in self.model.down_blocks:
if hasattr(downsample_block, "skip_conv"):
sample, res_samples, skip_sample = downsample_block(
hidden_states=sample, temb=emb, skip_sample=skip_sample)
else:
sample, res_samples = downsample_block(hidden_states=sample, temb=emb)
down_block_res_samples += res_samples
# 4. mid
if self.h_space == "before":
bottleneck = sample.clone()
if not delta_h is None:
sample = bottleneck + delta_h
if self.h_space == "middle":
sample, bottleneck = mid_block_forward(self.model.mid_block, sample, temb=emb,
encoder_states=None,
inject_into_botleneck = delta_h)
else:
sample = self.model.mid_block(sample, emb)
if self.h_space == "after":
bottleneck = sample.clone()
if not delta_h is None:
sample = bottleneck + delta_h
# 5. up
skip_sample = None
for upsample_block in self.model.up_blocks:
res_samples = down_block_res_samples[-len(upsample_block.resnets) :]
down_block_res_samples = down_block_res_samples[: -len(upsample_block.resnets)]
if hasattr(upsample_block, "skip_conv"):
sample, skip_sample = upsample_block(sample, res_samples, emb, skip_sample)
else:
sample = upsample_block(sample, res_samples, emb)
# 6. post-process make sure hidden states is in float32 when running in half-precision
sample = self.model.conv_norm_out(sample.float()).type(sample.dtype)
sample = self.model.conv_act(sample)
sample = self.model.conv_out(sample)
if skip_sample is not None:
sample += skip_sample
return UNetOutput(out = sample, h = None if self.h_space == None else bottleneck)
def sample(self, num_samples = 1, seed=None):
"""
Samples random noise in the dimensions of the Unet
"""
if seed is None: seed = torch.randint(int(1e6), (1,))
return torch.randn(num_samples,
self.model.in_channels,
self.model.sample_size,
self.model.sample_size,
generator=torch.manual_seed(seed)
).to(self.device)
class ConditionalUnet(UNet):
def __init__(self, model, h_space = "after"):
assert h_space in [None, "before","after" ] #"middle"
self.h_space = h_space
self.model = model
self.device = model.device
# def forward(self,sample,timestep, prompt=""):
# return ss
def forward(
self,
sample: torch.FloatTensor,
timestep: Union[torch.Tensor, float, int],
encoder_hidden_states: torch.Tensor,
delta_h: Optional[torch.Tensor] = None, ## hspace activation
):
default_overall_up_factor = 2**self.model.num_upsamplers
# upsample size should be forwarded when sample is not a multiple of `default_overall_up_factor`
forward_upsample_size = False
upsample_size = None
if any(s % default_overall_up_factor != 0 for s in sample.shape[-2:]):
# logger.info("Forward upsample size to force interpolation output size.")
forward_upsample_size = True
# 0. center input if necessary
if self.model.config.center_input_sample:
sample = 2 * sample - 1.0
print("[WARNING], self.config.center_input_sample")
emb = self.time_embedding(timestep, batch_dim = sample.shape[0])
sample = self.model.conv_in(sample)
# 3. down
down_block_res_samples = (sample,)
for downsample_block in self.model.down_blocks:
if hasattr(downsample_block, "has_cross_attention") and downsample_block.has_cross_attention:
sample, res_samples = downsample_block(
hidden_states=sample,
temb=emb,
encoder_hidden_states=encoder_hidden_states,
# attention_mask=attention_mask,
# cross_attention_kwargs=cross_attention_kwargs,
)
else:
sample, res_samples = downsample_block(hidden_states=sample, temb=emb)
down_block_res_samples += res_samples
if self.h_space == "before":
bottleneck = sample.clone()
if not delta_h is None:
sample = bottleneck + delta_h
# 4. mid
sample = self.model.mid_block(
sample,
emb,
encoder_hidden_states=encoder_hidden_states,
# attention_mask=attention_mask,
# cross_attention_kwargs=cross_attention_kwargs,
)
if self.h_space == "after":
bottleneck = sample.clone()
if not delta_h is None:
sample = bottleneck + delta_h
# 5. up
for i, upsample_block in enumerate(self.model.up_blocks):
is_final_block = i == len(self.model.up_blocks) - 1
res_samples = down_block_res_samples[-len(upsample_block.resnets) :]
down_block_res_samples = down_block_res_samples[: -len(upsample_block.resnets)]
# if we have not reached the final block and need to forward the
# upsample size, we do it here
if not is_final_block and forward_upsample_size:
upsample_size = down_block_res_samples[-1].shape[2:]
if hasattr(upsample_block, "has_cross_attention") and upsample_block.has_cross_attention:
sample = upsample_block(
hidden_states=sample,
temb=emb,
res_hidden_states_tuple=res_samples,
encoder_hidden_states=encoder_hidden_states,
# cross_attention_kwargs=cross_attention_kwargs,
upsample_size=upsample_size,
# attention_mask=attention_mask,
)
else:
sample = upsample_block(
hidden_states=sample, temb=emb, res_hidden_states_tuple=res_samples, upsample_size=upsample_size
)
# 6. post-process
sample = self.model.conv_norm_out(sample)
sample = self.model.conv_act(sample)
sample = self.model.conv_out(sample)
return UNetOutput(out=sample, h= None if self.h_space == None else bottleneck)
class Diffusion:
def __init__(self, unet: UNet2DModel, scheduler: DDIMScheduler, diffusers_default_scheduler = False):
self.unet = unet
self.device = self.unet.device
self.scheduler = scheduler
self.diffusers_default_scheduler = diffusers_default_scheduler
self.sanity_check()
self.update_inference_steps(num_inference_steps = 50)
def update_inference_steps(self, num_inference_steps = 50):
self.num_inference_steps = num_inference_steps
self.scheduler.set_timesteps(num_inference_steps = num_inference_steps)
self.timesteps = self.scheduler.timesteps.to(self.device)
self.t_to_idx = {int(v):k for k,v in enumerate(self.timesteps)}
self.h_shape = self.get_h_shape()
self.variance_noise_shape = (
self.num_inference_steps,
self.unet.model.in_channels,
self.unet.model.sample_size,
self.unet.model.sample_size)
def get_h_shape(self):
"""
Return the shape fo the h tensors
"""
xT = self.unet.sample()
with torch.no_grad():
out = self.unet.forward(xT, timestep = self.timesteps[-1])
return (self.num_inference_steps,) + tuple(out.h.shape[1:])
def sanity_check(self):
if self.scheduler.clip_sample:
print("[Warning] Scheduler assumes clipping, setting to false")
self.scheduler.clip_sample = False
self.scheduler.config.clip_sample = False
def get_variance(self, timestep): #, prev_timestep):
prev_timestep = timestep - self.scheduler.config.num_train_timesteps // self.scheduler.num_inference_steps
alpha_prod_t = self.scheduler.alphas_cumprod[timestep]
alpha_prod_t_prev = self.scheduler.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.scheduler.final_alpha_cumprod
beta_prod_t = 1 - alpha_prod_t
beta_prod_t_prev = 1 - alpha_prod_t_prev
variance = (beta_prod_t_prev / beta_prod_t) * (1 - alpha_prod_t / alpha_prod_t_prev)
return variance
# def get_variance(self, t):
# # DDPM papger eq 7
# alpha_bar = self.scheduler.alphas_cumprod
# betas = self.scheduler.betas
# alpha_bar_t = alpha_bar[t]
# alpha_bar_t_prev = alpha_bar[t - 1] if t > 0 else self.scheduler.one
# variance_t = ((1 - alpha_bar_t_prev) / (1 - alpha_bar_t)) * betas[t]
# return variance_t
def sample_variance_noise(self, seed=None):
"""
Samples variance noise
"""
if seed is None: seed = torch.randint(int(1e6), (1,))
return torch.randn( self.variance_noise_shape,
generator=torch.manual_seed(seed)
).to(self.device)
def reverse_step(self, model_output, timestep, sample, eta = 0, asyrp = None, variance_noise=None):
# 1. get previous step value (=t-1)
prev_timestep = timestep - self.scheduler.config.num_train_timesteps // self.scheduler.num_inference_steps
# 2. compute alphas, betas
alpha_prod_t = self.scheduler.alphas_cumprod[timestep]
alpha_prod_t_prev = self.scheduler.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.scheduler.final_alpha_cumprod
beta_prod_t = 1 - alpha_prod_t
# 3. compute predicted original sample from predicted noise also called
# "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf
pred_original_sample = (sample - beta_prod_t ** (0.5) * model_output) / alpha_prod_t ** (0.5)
# 5. compute variance: "sigma_t(η)" -> see formula (16)
# σ_t = sqrt((1 − α_t−1)/(1 − α_t)) * sqrt(1 − α_t/α_t−1)
# variance = self.scheduler._get_variance(timestep, prev_timestep)
variance = self.get_variance(timestep) #, prev_timestep)
std_dev_t = eta * variance ** (0.5)
# Take care of asymetric reverse process (asyrp)
model_output_direction = model_output if asyrp is None else asyrp
# 6. compute "direction pointing to x_t" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf
pred_sample_direction = (1 - alpha_prod_t_prev - std_dev_t**2) ** (0.5) * model_output_direction
# 7. compute x_t without "random noise" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf
prev_sample = alpha_prod_t_prev ** (0.5) * pred_original_sample + pred_sample_direction
# 8. Add noice if eta > 0
if eta > 0:
if variance_noise is None:
variance_noise = torch.randn(model_output.shape, device=self.device)
sigma_z = eta * variance ** (0.5) * variance_noise
prev_sample = prev_sample + sigma_z
return prev_sample
# Equivalent to https://github.com/google/prompt-to-prompt/blob/main/null_text_w_ptp.ipynb
# def reverse_step(self, model_output, timestep, sample):
# prev_timestep = timestep - self.scheduler.config.num_train_timesteps // self.scheduler.num_inference_steps
# alpha_prod_t = self.scheduler.alphas_cumprod[timestep]
# alpha_prod_t_prev = self.scheduler.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.scheduler.final_alpha_cumprod
# beta_prod_t = 1 - alpha_prod_t
# pred_original_sample = (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5
# pred_sample_direction = (1 - alpha_prod_t_prev) ** 0.5 * model_output
# prev_sample = alpha_prod_t_prev ** 0.5 * pred_original_sample + pred_sample_direction
# return prev_sample
def reverse_process(self, xT,
etas = 0,
prog_bar = False,
zs = None,
delta_hs = None,
asyrp = False):
if etas is None: etas = 0
if type(etas) in [int, float]: etas = [etas]*self.num_inference_steps
assert len(etas) == self.num_inference_steps
xt = xT
hs = torch.zeros(self.h_shape).to(self.device)
op = tqdm(self.timesteps) if prog_bar else self.timesteps
for t in op:
idx = self.t_to_idx[int(t)]
delta_h = None if delta_hs is None else delta_hs[idx][None]
with torch.no_grad():
out = self.unet.forward(xt, timestep = t, delta_h = delta_h)
hs[idx] = out.h.squeeze()
# Support for asyrp
# ++++++++++++++++++++++++++++++++++++++++
if asyrp and not delta_hs is None:
with torch.no_grad():
out_asyrp = self.unet.forward(xt, timestep = t)
residual_d = out_asyrp.out
else:
residual_d = None
# ----------------------------------------------------
z = zs[idx] if not zs is None else None
# 2. compute less noisy image and set x_t -> x_t-1
xt = self.reverse_step(out.out, t, xt, asyrp = residual_d, eta = etas[idx], variance_noise = z)
return xt, hs, zs
def add_noise(self, original_samples, noise, timesteps):
# Make sure alphas_cumprod and timestep have same device and dtype as original_samples
self.alphas_cumprod = self.scheduler.alphas_cumprod.to(device=original_samples.device, dtype=original_samples.dtype)
#timesteps = timesteps.to(original_samples.device)
sqrt_alpha_prod = self.scheduler.alphas_cumprod[timesteps] ** 0.5
# sqrt_alpha_prod = sqrt_alpha_prod.flatten()
# while len(sqrt_alpha_prod.shape) < len(original_samples.shape):
# sqrt_alpha_prod = sqrt_alpha_prod.unsqueeze(-1)
sqrt_one_minus_alpha_prod = (1 - self.scheduler.alphas_cumprod[timesteps]) ** 0.5
# sqrt_one_minus_alpha_prod = sqrt_one_minus_alpha_prod.flatten()
# while len(sqrt_one_minus_alpha_prod.shape) < len(original_samples.shape):
# sqrt_one_minus_alpha_prod = sqrt_one_minus_alpha_prod.unsqueeze(-1)
noisy_samples = sqrt_alpha_prod * original_samples + sqrt_one_minus_alpha_prod * noise
return noisy_samples
def forward_step(self, model_output, timestep, sample):
next_timestep = min(self.scheduler.config.num_train_timesteps - 2,
timestep + self.scheduler.config.num_train_timesteps // self.scheduler.num_inference_steps)
# 2. compute alphas, betas
alpha_prod_t = self.scheduler.alphas_cumprod[timestep]
# alpha_prod_t_next = self.scheduler.alphas_cumprod[next_timestep] if next_ltimestep >= 0 else self.scheduler.final_alpha_cumprod
beta_prod_t = 1 - alpha_prod_t
# 3. compute predicted original sample from predicted noise also called
# "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf
pred_original_sample = (sample - beta_prod_t ** (0.5) * model_output) / alpha_prod_t ** (0.5)
next_sample = self.scheduler.add_noise(pred_original_sample,
model_output,
torch.LongTensor([next_timestep]))
return next_sample
# def forward_step(self, model_output, timestep, sample):
# # https://github.com/google/prompt-to-prompt/blob/main/null_text_w_ptp.ipynb
# timestep, next_timestep = min(timestep - self.scheduler.config.num_train_timesteps // self.scheduler.num_inference_steps, 999), timestep
# alpha_prod_t = self.scheduler.alphas_cumprod[timestep] if timestep >= 0 else self.scheduler.final_alpha_cumprod
# alpha_prod_t_next = self.scheduler.alphas_cumprod[next_timestep]
# beta_prod_t = 1 - alpha_prod_t
# next_original_sample = (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5
# next_sample_direction = (1 - alpha_prod_t_next) ** 0.5 * model_output
# next_sample = alpha_prod_t_next ** 0.5 * next_original_sample + next_sample_direction
# return next_sample
# def forward(self, x0, prog_bar = False):
# xt = x0
# zs = None
# hs = torch.zeros(self.h_shape).to(self.device)
# op = tqdm(reversed(self.timesteps)) if prog_bar else reversed(self.timesteps)
# for t in op:
# idx = self.t_to_idx[int(t)]
# # 1. predict noise residual
# with torch.no_grad():
# out = self.unet.forward(xt, timestep = t)
# hs[idx] = out.h.squeeze()
# # 2. compute more noisy image and set x_t -> x_t+1
# xt = self.forward_step(out.out, t, xt)
# return xt, hs, zs
def forward(self, x0,
etas = None,
method_from = "x0",
prog_bar = False):
if etas is None or (type(etas) in [int, float] and etas == 0):
eta_is_zero = True
zs = None
else:
eta_is_zero = False
if type(etas) in [int, float]: etas = [etas]*self.num_inference_steps
xts = self.sample_xts_from_x0(x0, method_from = method_from)
alpha_bar = self.scheduler.alphas_cumprod
zs = torch.zeros_like(self.sample_variance_noise())
xt = x0
hs = torch.zeros(self.h_shape).to(self.device)
op = tqdm(reversed(self.timesteps)) if prog_bar else reversed(self.timesteps)
for t in op:
idx = self.t_to_idx[int(t)]
# 1. predict noise residual
if not eta_is_zero:
xt = xts[idx][None]
with torch.no_grad():
out = self.unet.forward(xt, timestep = t)
hs[idx] = out.h.squeeze()
if eta_is_zero:
# 2. compute more noisy image and set x_t -> x_t+1
xt = self.forward_step(out.out, t, xt)
else:
xtm1 = xts[idx+1][None]
# pred of x0
pred_original_sample = (xt - (1-alpha_bar[t]) ** 0.5 * out.out ) / alpha_bar[t] ** 0.5
# direction to xt
prev_timestep = t - self.scheduler.config.num_train_timesteps // self.scheduler.num_inference_steps
alpha_prod_t_prev = self.scheduler.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.scheduler.final_alpha_cumprod
variance = self.get_variance(t)
pred_sample_direction = (1 - alpha_prod_t_prev - etas[idx] * variance ) ** (0.5) * out.out
mu_xt = alpha_prod_t_prev ** (0.5) * pred_original_sample + pred_sample_direction
z = (xtm1 - mu_xt ) / ( etas[idx] * variance ** 0.5 )
zs[idx] = z
if not zs is None:
zs[-1] = torch.zeros_like(zs[-1])
return xt, hs, zs
def sample_xts_from_x0(self, x0, method_from = "x0"):
"""
Samples from P(x_1:T|x_0)
"""
assert method_from in ["x0", "x_prev", "dpm"]
# torch.manual_seed(43256465436)
alpha_bar = self.scheduler.alphas_cumprod
sqrt_one_minus_alpha_bar = (1-alpha_bar) ** 0.5
alphas = self.scheduler.alphas
betas = 1 - alphas
if method_from == "x0":
xts = torch.zeros(self.variance_noise_shape).to(x0.device)
for t in reversed(self.timesteps):
idx = self.t_to_idx[int(t)]
xts[idx] = x0 * (alpha_bar[t] ** 0.5) + torch.randn_like(x0) * sqrt_one_minus_alpha_bar[t]
xts = torch.cat([xts, x0],dim = 0)
if method_from == "x_prev":
xts = torch.zeros(self.variance_noise_shape).to(x0.device)
x_next = x0.clone()
for t in reversed(self.timesteps):
noise = torch.randn_like(x0)
idx = self.t_to_idx[int(t)]
xt = ((1 - betas[t]) ** 0.5) * x_next + noise * (betas[t] ** 0.5)
x_next = xt
xts[idx] = xt
xts = torch.cat([xts, x0],dim = 0)
if method_from == "dpm":
xts = torch.zeros(self.variance_noise_shape).to(x0.device)
x0.clone()
t_final = self.timesteps[0]
xT = x0 * (alpha_bar[t_final] ** 0.5) + torch.randn_like(x0) * sqrt_one_minus_alpha_bar[t_final]
xt = xT.clone()
for t in self.timesteps:
idx = self.t_to_idx[int(t)]
xtm1 = self.sample_xtm1_from_xt_x0(xt,x0,t)
xt = xtm1
xts[idx] = xt
xts = torch.cat([xts, x0],dim = 0)
return xts
def mu_tilde(self, xt,x0, timestep):
"mu_tilde(x_t, x_0) DDPM paper eq. 7"
prev_timestep = timestep - self.scheduler.config.num_train_timesteps // self.scheduler.num_inference_steps
alpha_prod_t_prev = self.scheduler.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.scheduler.final_alpha_cumprod
alpha_t = self.scheduler.alphas[timestep]
beta_t = 1 - alpha_t
alpha_bar = self.scheduler.alphas_cumprod[timestep]
return ((alpha_prod_t_prev ** 0.5 * beta_t) / (1-alpha_bar)) * x0 + ((alpha_t**0.5 *(1-alpha_prod_t_prev)) / (1- alpha_bar))*xt
def sample_xtm1_from_xt_x0(self, xt, x0, t):
"DDPM paper equation 6"
prev_timestep = t - self.scheduler.config.num_train_timesteps // self.scheduler.num_inference_steps
alpha_prod_t_prev = self.scheduler.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.scheduler.final_alpha_cumprod
alpha_t = self.scheduler.alphas[t]
beta_t = 1 - alpha_t
alpha_bar = self.scheduler.alphas_cumprod[t]
beta_tilde_t = ((1-alpha_prod_t_prev) / (1-alpha_bar)) * beta_t
return self.mu_tilde(xt,x0, t) + beta_tilde_t**0.5 * torch.randn_like(x0)
class StableDiffusion(Diffusion):
def __init__(self, unet, scheduler, tokenizer, text_encoder):
self.unet = unet
self.device = self.unet.device
self.scheduler = scheduler
self.tokenizer_id = "openai/clip-vit-base-patch32"
self.tokenizer = tokenizer
self.text_encoder = text_encoder
# self.tokenizer = CLIPTokenizer.from_pretrained(self.tokenizer_id) #CLIPTokenizer.from_pretrained("openai/clip-vit-large-patch14")
# self.text_encoder = CLIPTextModel.from_pretrained(self.tokenizer_id).to(self.device)
self.uncond_embedding = self.encode_text("")
self.update_inference_steps(num_inference_steps = 50)
def encode_text(self, prompt):
text_input = self.tokenizer(
[prompt],
padding="max_length",
max_length=self.tokenizer.model_max_length,
truncation=True,
return_tensors="pt",
)
with torch.no_grad():
text_encoding = self.text_encoder(text_input.input_ids.to(self.device))[0]
return text_encoding
def get_h_shape(self):
"""
Return the shape fo the h tensors
"""
xT = self.unet.sample()
text_embeddings = self.encode_text("")
with torch.no_grad():
out = self.unet.forward(xT, timestep = self.timesteps[-1], encoder_hidden_states = self.uncond_embedding)
return (self.num_inference_steps,) + tuple(out.h.shape[1:])
def update_inference_steps(self, num_inference_steps = 50):
self.num_inference_steps = num_inference_steps
self.scheduler.set_timesteps(num_inference_steps = num_inference_steps)
self.timesteps = self.scheduler.timesteps.to(self.device)
self.t_to_idx = {int(v):k for k,v in enumerate(self.timesteps)}
self.h_shape = self.get_h_shape()
self.variance_noise_shape = (
self.num_inference_steps,
self.unet.model.in_channels,
self.unet.model.sample_size,
self.unet.model.sample_size)
def reverse_process(self, xT,
etas = 0,
prompt = "",
cfg_scale = 7.5,
prog_bar = False,
zs = None,
delta_hs = None,
asyrp = False):
text_embeddings = self.encode_text(prompt)
if etas is None: etas = 0
if type(etas) in [int, float]: etas = [etas]*self.num_inference_steps
assert len(etas) == self.num_inference_steps
xt = xT
hs = torch.zeros(self.h_shape).to(self.device)
op = tqdm(self.timesteps) if prog_bar else self.timesteps
for t in op:
idx = self.t_to_idx[int(t)]
delta_h = None if delta_hs is None else delta_hs[idx][None]
## Unconditional embedding
with torch.no_grad():
uncond_out = self.unet.forward(xt, timestep = t,
encoder_hidden_states = self.uncond_embedding, delta_h = delta_h)
hs[idx] = uncond_out.h.squeeze()
## Conditional embedding
if prompt:
with torch.no_grad():
cond_out = self.unet.forward(xt, timestep = t,
encoder_hidden_states = text_embeddings, delta_h = delta_h)
# Support for asyrp
# ++++++++++++++++++++++++++++++++++++++++
if asyrp and not delta_hs is None:
with torch.no_grad():
out_asyrp = self.unet.forward(xt, timestep = t, encoder_hidden_states = text_embeddings)
residual_d = out_asyrp.out
else:
residual_d = None
# ----------------------------------------------------
z = zs[idx] if not zs is None else None
if prompt:
## classifier free guidance
noise_pred = uncond_out.out + cfg_scale * (cond_out.out - uncond_out.out)
else:
noise_pred = uncond_out.out
# 2. compute less noisy image and set x_t -> x_t-1
xt = self.reverse_step(noise_pred, t, xt, asyrp = residual_d, eta = etas[idx], variance_noise = z)
return xt, hs, zs
def forward(self, x0,
etas = None,
method_from = "x0",
prog_bar = False):
if etas is None or (type(etas) in [int, float] and etas == 0):
eta_is_zero = True
zs = None
else:
eta_is_zero = False
if type(etas) in [int, float]: etas = [etas]*self.num_inference_steps
xts = self.sample_xts_from_x0(x0, method_from = method_from)
alpha_bar = self.scheduler.alphas_cumprod
zs = torch.zeros_like(self.sample_variance_noise())
xt = x0
hs = torch.zeros(self.h_shape).to(self.device)
op = tqdm(reversed(self.timesteps)) if prog_bar else reversed(self.timesteps)
for t in op:
idx = self.t_to_idx[int(t)]
# 1. predict noise residual
if not eta_is_zero:
xt = xts[idx][None]
with torch.no_grad():
out = self.unet.forward(xt, timestep = t, encoder_hidden_states = self.uncond_embedding)
hs[idx] = out.h.squeeze()
if eta_is_zero:
# 2. compute more noisy image and set x_t -> x_t+1
xt = self.forward_step(out.out, t, xt)
else:
xtm1 = xts[idx+1][None]
# pred of x0
pred_original_sample = (xt - (1-alpha_bar[t]) ** 0.5 * out.out ) / alpha_bar[t] ** 0.5
# direction to xt
prev_timestep = t - self.scheduler.config.num_train_timesteps // self.scheduler.num_inference_steps
alpha_prod_t_prev = self.scheduler.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.scheduler.final_alpha_cumprod
variance = self.get_variance(t)
pred_sample_direction = (1 - alpha_prod_t_prev - etas[idx] * variance ) ** (0.5) * out.out
mu_xt = alpha_prod_t_prev ** (0.5) * pred_original_sample + pred_sample_direction
z = (xtm1 - mu_xt ) / ( etas[idx] * variance ** 0.5 )
zs[idx] = z
if not zs is None:
zs[-1] = torch.zeros_like(zs[-1])
return xt, hs, zs
class Interpolations:
def interpolation(self, z1, z2,
numsteps = 5,
t_min = 0, t_max = 1,
method = "lerp"):
return torch.cat([self.interp(z1,z2,t, method = method) for t in torch.linspace(t_min,t_max,numsteps)])
def interp(self, z1, z2, t, method = "lerp"):
if method == "lerp":
return self.lerp(z1,z2,t)
elif method == "slerp":
return self.slerp(z1,z2,t)
elif method == "sqlerp":
return self.sqlerp(z1,z2,t)
else:
raise NotImplementedError("only lerp and slerp implemented")
def slerp(self, v0, v1, t, DOT_THRESHOLD=0.9995):
"""helper function to spherically interpolate two arrays v1 v2"""
if not isinstance(v0, np.ndarray):
inputs_are_torch = True
input_device = v0.device
v0 = v0.cpu().numpy()
v1 = v1.cpu().numpy()
t = t.cpu().numpy()
dot = np.sum(v0 * v1 / (np.linalg.norm(v0) * np.linalg.norm(v1)))
if np.abs(dot) > DOT_THRESHOLD:
v2 = (1 - t) * v0 + t * v1
else:
theta_0 = np.arccos(dot)
sin_theta_0 = np.sin(theta_0)
theta_t = theta_0 * t
sin_theta_t = np.sin(theta_t)
s0 = np.sin(theta_0 - theta_t) / sin_theta_0
s1 = sin_theta_t / sin_theta_0
v2 = s0 * v0 + s1 * v1
if inputs_are_torch:
v2 = torch.from_numpy(v2).to(input_device)
return v2
def lerp(self, z1,z2,t):
return z1*(1-t) + z2*t
def sqlerp(self, z1,z2,t):
return z1*(1-t)**0.5 + z2*t**0.5
class Q:
"""
Representation state class
"""
def __init__(self, x0 = None, xT = None, w0 = None, wT = None, hs = None, zs = None, etas = None, delta_hs = None, seed = None, asyrp = None, prompt = "", cfg_scale = None, label = None):
self.x0 = x0 # clean img
self.xT = xT # noise img
self.w0 = w0 # clean latent (in case is_vq=True)
self.wT = wT # noise latent
self.zs = zs # Variance noise added (pre-sampled)
self.hs = hs # h-space representation
# Modyfiers
self.etas = etas #eta schedule
self.delta_hs = delta_hs # delta hs to be injected during decoding
self.seed = seed # seed for random number generators
self.asyrp = asyrp # if delta hs injection is asymetrical
# Prompts for Stable Diffusion support
self.prompt = prompt
self.cfg_scale = cfg_scale # classifier free guidance scale
self.label = label #optional label
def copy(self):
# return Q(**self.__dict__.copy())
return Q(**copy.deepcopy(self.__dict__))
def set_delta_hs(self, delta_hs):
self.delta_hs = delta_hs
return self
def to_string(self):
string = f"seed={self.seed}-etas={self.etas}"
if not self.label is None:
string += f"label={self.label}"
string += self.prompt
return string
def __add__(self, other):
return Q(delta_hs=self.delta_hs + other.delta_hs)
def __sub__(self, other):
return Q(delta_hs=self.delta_hs - other.delta_hs)
class SemanticDiffusion(Interpolations):
def __init__(self, unet, scheduler,
vqvae = None, model_id = None, num_inference_steps = 25, diffusers_default_scheduler = False, resize_to = 256):
self.vqvae = vqvae
self.is_vq = False if self.vqvae is None else True
self.diff = Diffusion(scheduler=scheduler, unet = unet, diffusers_default_scheduler = diffusers_default_scheduler)
self.device = unet.device
self.model_id = model_id
self.is_conditional = False
self.h_space = self.diff.unet.h_space
self.resize_to = resize_to
if self.is_vq:
self.img_size = 256
else:
self.img_size = self.diff.unet.model.sample_size
self.set_inference_steps(num_inference_steps = num_inference_steps)
def get_eta_schedule(self, fraction = .2,
eta_scale = 1,
where = "end"):
T = self.num_inference_steps
nr_on = int(T*fraction)
nr_off = T - nr_on
if where == "end":
etas = torch.tensor([0]*nr_off+[1]*nr_on)*eta_scale
elif where == "end":
etas = torch.tensor([1]*nr_on+[0]*nr_off)*eta_scale
else:
raise NotImplementedError
return etas
def load_model():
pass
def set_inference_steps(self, num_inference_steps = 50):
self.num_inference_steps = num_inference_steps
self.diff.update_inference_steps(num_inference_steps)
self.model_label = self.model_id.replace("/","-")+f"steps{self.num_inference_steps}-hspace-{self.h_space}"
def encode(self, q, method_from = "x0", **kwargs):
with autocast("cuda"), inference_mode():
if not self.vqvae is None:
q.w0 = self.vqvae.encode(q.x0).latents
q.wT, q.hs, q.zs = self.diff.forward(q.w0, etas = q.etas,method_from = method_from, **kwargs)
else:
q.xT, q.hs, q.zs = self.diff.forward(q.x0, etas = q.etas,method_from = method_from, **kwargs)
return q
def decode(self, q, **kwargs):
if self.is_vq : #not self.vqvae is None:
q.w0, q.hs, q.zs= self.diff.reverse_process(q.wT,
zs = q.zs,
etas= q.etas,
delta_hs = q.delta_hs,
asyrp=q.asyrp,
**kwargs)
with autocast("cuda"), inference_mode():
q.x0 = self.vqvae.decode(q.w0).sample
else:
q.x0, q.hs, q.zs = self.diff.reverse_process(q.xT,
zs = q.zs,
etas = q.etas,
delta_hs = q.delta_hs,
asyrp=q.asyrp,
**kwargs)
return q
def sample(self, decode = True,
seed=None,
prompt = "",
variance_seed = None,
etas=None, **kwargs):
"""
Samples random noise in the dimensions of the Unet
"""
if seed is None: seed = torch.randint(int(1e6), (1,))
q = Q(seed = seed, etas = etas, prompt = prompt)
sample = self.diff.unet.sample(seed = seed)
if not self.vqvae is None: q.wT = sample
else: q.xT = sample
if not etas is None:
if variance_seed is None:
variance_seed = seed + 1
## Very important the the first zt is not equal to xT
## if seed xT and seed zs is equal horrible stuff happens
q.zs = self.sample_variance_noise(seed = variance_seed)
if decode:
q = self.decode(q,**kwargs)
return q
def sample_seeds(self, etas = None, num_imgs = 25, imsize = None, plot_seed_nr = True, rows = 5, cols = 5 ):
qs = [self.sample(etas = etas) for _ in tqdm(range(num_imgs))]
imgs = [q.x0 for q in qs]
labels = [str(int(q.seed)) for q in qs] if plot_seed_nr else None
return image_grid(imgs, titles=labels,size=imsize, rows = rows, cols = cols)
def sample_variance_noise(self, seed=None):
return self.diff.sample_variance_noise(seed = seed)
def apply_direction(self, q, n, scale = 1, space = "hspace"):
q_edit = q.copy()
if space == "noise":
if self.is_vq:
q_edit.wT = q_edit.wT + scale*n.wT.to(self.device)