-
Notifications
You must be signed in to change notification settings - Fork 5
/
latent_sdxl.py
1031 lines (868 loc) · 42.1 KB
/
latent_sdxl.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
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
from typing import Any, Optional, Tuple
import os
from safetensors.torch import load_file
import torch
from diffusers import AutoencoderKL, DDIMScheduler, StableDiffusionXLPipeline, UNet2DConditionModel, EulerDiscreteScheduler
from diffusers.models.attention_processor import (AttnProcessor2_0,
LoRAAttnProcessor2_0,
LoRAXFormersAttnProcessor,
XFormersAttnProcessor)
from tqdm import tqdm
from latent_diffusion import get_sigmas_karras, get_ancestral_step, append_zero
####### Factory #######
__SOLVER__ = {}
def register_solver(name: str):
def wrapper(cls):
if __SOLVER__.get(name, None) is not None:
raise ValueError(f"Solver {name} already registered.")
__SOLVER__[name] = cls
return cls
return wrapper
def get_solver(name: str, **kwargs):
if name not in __SOLVER__:
raise ValueError(f"Solver {name} does not exist.")
return __SOLVER__[name](**kwargs)
########################
class SDXL():
def __init__(self,
solver_config: dict,
model_key:str="stabilityai/stable-diffusion-xl-base-1.0",
dtype=torch.float16,
device='cuda'):
self.device = device
pipe = StableDiffusionXLPipeline.from_pretrained(model_key, torch_dtype=dtype).to(device)
self.dtype = dtype
# avoid overflow in float16
self.vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=dtype).to(device)
self.tokenizer_1 = pipe.tokenizer
self.tokenizer_2 = pipe.tokenizer_2
self.text_enc_1 = pipe.text_encoder
self.text_enc_2 = pipe.text_encoder_2
self.unet = pipe.unet
self.vae_scale_factor = 2 ** (len(self.vae.config.block_out_channels) - 1)
self.default_sample_size = self.unet.config.sample_size
# sampling parameters
self.scheduler = DDIMScheduler.from_pretrained(model_key, subfolder="scheduler")
self.total_alphas = self.scheduler.alphas_cumprod.clone()
self.sigmas = (1-self.total_alphas).sqrt() / self.total_alphas.sqrt()
self.log_sigmas = self.sigmas.log()
N_ts = len(self.scheduler.timesteps)
self.scheduler.set_timesteps(solver_config.num_sampling, device=device)
self.skip = N_ts // solver_config.num_sampling
self.final_alpha_cumprod = self.scheduler.final_alpha_cumprod.to(device)
self.scheduler.alphas_cumprod = torch.cat([torch.tensor([1.0]), self.scheduler.alphas_cumprod])
def __call__(self, *args: Any, **kwargs: Any) -> Any:
self.sample(*args, **kwargs)
def alpha(self, t):
at = self.scheduler.alphas_cumprod[t] if t >= 0 else self.final_alpha_cumprod
return at
@torch.no_grad()
def _text_embed(self, prompt, tokenizer, text_enc, clip_skip):
text_inputs = tokenizer(
prompt,
padding='max_length',
max_length=tokenizer.model_max_length,
truncation=True,
return_tensors='pt')
text_input_ids = text_inputs.input_ids
prompt_embeds = text_enc(text_input_ids.to(self.device), output_hidden_states=True)
pool_prompt_embeds = prompt_embeds[0]
if clip_skip is None:
prompt_embeds = prompt_embeds.hidden_states[-2]
else:
# +2 because SDXL always indexes from the penultimate layer.
prompt_embeds = prompt_embeds.hidden_states[-(clip_skip + 2)]
return prompt_embeds, pool_prompt_embeds
@torch.no_grad()
def get_text_embed(self, null_prompt_1, prompt_1, null_prompt_2=None, prompt_2=None, clip_skip=None):
'''
At this time, assume that batch_size = 1.
We should extend the code to batch_size > 1.
'''
# Encode the prompts
# if prompt_2 is None, set same as prompt_1
prompt_1 = [prompt_1] if isinstance(prompt_1, str) else prompt_1
null_prompt_1 = [null_prompt_1] if isinstance(null_prompt_1, str) else null_prompt_1
prompt_embed_1, pool_prompt_embed = self._text_embed(prompt_1, self.tokenizer_1, self.text_enc_1, clip_skip)
if prompt_2 is None:
prompt_embed = [prompt_embed_1]
else:
# Comment on diffusers' source code:
# "We are only ALWAYS interested in the pooled output of the final text encoder"
# i.e. we overwrite the pool_prompt_embed with the new one
prompt_embed_2, pool_prompt_embed = self._text_embed(prompt_2, self.tokenizer_2, self.text_enc_2, clip_skip)
prompt_embed = [prompt_embed_1, prompt_embed_2]
null_embed_1, pool_null_embed = self._text_embed(null_prompt_1, self.tokenizer_1, self.text_enc_1, clip_skip)
if null_prompt_2 is None:
null_embed = [null_embed_1]
else:
null_embed_2, pool_null_embed = self._text_embed(null_prompt_2, self.tokenizer_2, self.text_enc_2, clip_skip)
null_embed = [null_embed_1, null_embed_2]
# concat embeds from two encoders
null_prompt_embeds = torch.concat(null_embed, dim=-1)
prompt_embeds = torch.concat(prompt_embed, dim=-1)
return null_prompt_embeds, prompt_embeds, pool_null_embed, pool_prompt_embed
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_upscale.StableDiffusionUpscalePipeline.upcast_vae
def upcast_vae(self):
dtype = self.vae.dtype
self.vae.to(dtype=torch.float32)
use_torch_2_0_or_xformers = isinstance(
self.vae.decoder.mid_block.attentions[0].processor,
(
AttnProcessor2_0,
XFormersAttnProcessor,
LoRAXFormersAttnProcessor,
LoRAAttnProcessor2_0,
),
)
# if xformers or torch_2_0 is used attention block does not need
# to be in float32 which can save lots of memory
if use_torch_2_0_or_xformers:
self.vae.post_quant_conv.to(dtype)
self.vae.decoder.conv_in.to(dtype)
self.vae.decoder.mid_block.to(dtype)
@torch.no_grad()
def encode(self, x):
return self.vae.encode(x).latent_dist.sample() * self.vae.config.scaling_factor
# @torch.no_grad()
def decode(self, zt):
# make sure the VAE is in float32 mode, as it overflows in float16
# needs_upcasting = self.vae.dtype == torch.float16 and self.vae.config.force_upcast
# if needs_upcasting:
# self.upcast_vae()
# zt = zt.to(next(iter(self.vae.post_quant_conv.parameters())).dtype)
image = self.vae.decode(zt / self.vae.config.scaling_factor).sample.float()
return image
def predict_noise(self, zt, t, uc, c, added_cond_kwargs):
t_in = t.unsqueeze(0)
if uc is None:
noise_c = self.unet(zt, t_in, encoder_hidden_states=c,
added_cond_kwargs=added_cond_kwargs)['sample']
noise_uc = noise_c
elif c is None:
noise_uc = self.unet(zt, t_in, encoder_hidden_states=uc,
added_cond_kwargs=added_cond_kwargs)['sample']
noise_c = noise_uc
else:
c_embed = torch.cat([uc, c], dim=0)
z_in = torch.cat([zt] * 2)
t_in = torch.cat([t_in] * 2)
noise_pred = self.unet(z_in, t_in, encoder_hidden_states=c_embed,
added_cond_kwargs=added_cond_kwargs)['sample']
noise_uc, noise_c = noise_pred.chunk(2)
return noise_uc, noise_c
def _get_add_time_ids(self, original_size, crops_coords_top_left, target_size, dtype, text_encoder_projection_dim):
add_time_ids = list(original_size+crops_coords_top_left+target_size)
passed_add_embed_dim = (
self.unet.config.addition_time_embed_dim * len(add_time_ids) + text_encoder_projection_dim
)
expected_add_embed_dim = self.unet.add_embedding.linear_1.in_features
assert expected_add_embed_dim == passed_add_embed_dim, (
f"Model expects an added time embedding vector of length {expected_add_embed_dim}, but a vector of {passed_add_embed_dim} was created. The model has an incorrect config. Please check `unet.config.time_embedding_type` and `text_encoder_2.config.projection_dim`."
)
add_time_ids = torch.tensor([add_time_ids], dtype=dtype)
return add_time_ids
@torch.autocast(device_type='cuda', dtype=torch.float16)
def sample(self,
prompt1 = ["", ""],
prompt2 = ["", ""],
cfg_guidance:float=5.0,
original_size: Optional[Tuple[int, int]]=None,
crops_coords_top_left: Tuple[int, int]=(0, 0),
target_size: Optional[Tuple[int, int]]=None,
negative_original_size: Optional[Tuple[int, int]]=None,
negative_crops_coords_top_left: Tuple[int, int]=(0, 0),
negative_target_size: Optional[Tuple[int, int]]=None,
clip_skip: Optional[int]=None,
**kwargs):
# 0. Default height and width to unet
height = self.default_sample_size * self.vae_scale_factor
width = self.default_sample_size * self.vae_scale_factor
original_size = original_size or (height, width)
target_size = target_size or (height, width)
# embedding
(null_prompt_embeds,
prompt_embeds,
pool_null_embed,
pool_prompt_embed) = self.get_text_embed(prompt1[0], prompt1[1], prompt2[0], prompt2[1], clip_skip)
# prepare kwargs for SDXL
add_text_embeds = pool_prompt_embed
add_time_ids = self._get_add_time_ids(
original_size,
crops_coords_top_left,
target_size,
dtype=prompt_embeds.dtype,
text_encoder_projection_dim=int(pool_prompt_embed.shape[-1]),
)
if negative_original_size is not None and negative_target_size is not None:
negative_add_time_ids = self._get_add_time_ids(
negative_original_size,
negative_crops_coords_top_left,
negative_target_size,
dtype=prompt_embeds.dtype,
text_encoder_projection_dim=int(pool_prompt_embed.shape[-1]),
)
else:
negative_add_time_ids = add_time_ids
negative_text_embeds = pool_null_embed
if cfg_guidance != 0.0 and cfg_guidance != 1.0:
# do cfg
add_text_embeds = torch.cat([negative_text_embeds, add_text_embeds], dim=0)
add_time_ids = torch.cat([negative_add_time_ids, add_time_ids], dim=0)
add_cond_kwargs = {
'text_embeds': add_text_embeds.to(self.device),
'time_ids': add_time_ids.to(self.device)
}
# reverse sampling
zt = self.reverse_process(null_prompt_embeds, prompt_embeds, cfg_guidance, add_cond_kwargs, target_size, **kwargs)
# decode
with torch.no_grad():
img = self.decode(zt)
img = (img / 2 + 0.5).clamp(0, 1)
return img.detach().cpu()
def initialize_latent(self,
method: str='random',
src_img: Optional[torch.Tensor]=None,
add_cond_kwargs: Optional[dict]=None,
**kwargs):
if method == 'ddim':
assert src_img is not None, "src_img must be provided for inversion"
z = self.inversion(self.encode(src_img.to(self.dtype).to(self.device)),
kwargs.get('uc'),
kwargs.get('c'),
kwargs.get('cfg_guidance', 0.0),
add_cond_kwargs)
elif method == 'npi':
assert src_img is not None, "src_img must be provided for inversion"
z = self.inversion(self.encode(src_img.to(self.dtype).to(self.device)),
kwargs.get('c'),
kwargs.get('c'),
1.0,
add_cond_kwargs)
elif method == 'random':
size = kwargs.get('size', (1, 4, 128, 128))
z = torch.randn(size).to(self.device)
elif method == 'random_kdiffusion':
size = kwargs.get('latent_dim', (1, 4, 128, 128))
sigmas = kwargs.get('sigmas', [14.6146])
z = torch.randn(size).to(self.device)
z = z * (sigmas[0] ** 2 + 1) ** 0.5
#z = z * sigmas[0]
else:
raise NotImplementedError
return z.requires_grad_()
def inversion(self, z0, uc, c, cfg_guidance, add_cond_kwargs):
# if we use cfg_guidance=0.0 or 1.0 for inversion, add_cond_kwargs must be splitted.
if cfg_guidance == 0.0 or cfg_guidance == 1.0:
add_cond_kwargs['text_embeds'] = add_cond_kwargs['text_embeds'][-1].unsqueeze(0)
add_cond_kwargs['time_ids'] = add_cond_kwargs['time_ids'][-1].unsqueeze(0)
zt = z0.clone().to(self.device)
pbar = tqdm(reversed(self.scheduler.timesteps), desc='DDIM inversion')
for _, t in enumerate(pbar):
at = self.alpha(t)
at_prev = self.alpha(t - self.skip)
with torch.no_grad():
noise_uc, noise_c = self.predict_noise(zt, t, uc, c, add_cond_kwargs)
noise_pred = noise_uc + cfg_guidance * (noise_c - noise_uc)
z0t = (zt - (1-at_prev).sqrt() * noise_pred) / at_prev.sqrt()
zt = at.sqrt() * z0t + (1-at).sqrt() * noise_pred
return zt
def reverse_process(self, *args, **kwargs):
raise NotImplementedError
# Belows are for K-diffusion sampling (euler, etc)
def calculate_input(self, x, sigma):
return x / (sigma ** 2 + 1) ** 0.5
# Related to the Tweedie's formula in VE
def calculate_denoised(self, x, model_pred, sigma):
return x - model_pred * sigma
def sigma_to_t(self, sigma, quantize=None):
'''Taken from k_diffusion/external.py'''
quantize = self.quantize if quantize is None else quantize
total_sigmas = (1-self.total_alphas).sqrt() / self.total_alphas.sqrt()
dists = sigma - total_sigmas[:, None]
if quantize:
return dists.abs().argmin(dim=0).view(sigma.shape)
low_idx = dists.ge(0).cumsum(dim=0).argmax(dim=0).clamp(max=total_sigmas.shape[0] - 2)
high_idx = low_idx + 1
low, high = total_sigmas[low_idx], total_sigmas[high_idx]
w = (low - sigma) / (low - high)
w = w.clamp(0, 1)
t = (1 - w) * low_idx + w * high_idx
return t.view(sigma.shape)
def timestep(self, sigma):
log_sigma = sigma.log()
dists = log_sigma.to(self.log_sigmas.device) - self.log_sigmas[:, None]
return dists.abs().argmin(dim=0).view(sigma.shape).to(sigma.device)
def to_d(self, x, sigma, denoised):
'''converts a denoiser output to a Karras ODE derivative'''
return (x - denoised) / sigma.item()
def kdiffusion_zt_to_denoised(self, x, sigma, uc, c, cfg_guidance, t, add_cond_kwargs):
xc = self.calculate_input(x, sigma)
noise_uc, noise_c = self.predict_noise(xc, t, uc, c, add_cond_kwargs)
noise_pred = noise_uc + cfg_guidance * (noise_c - noise_uc)
denoised = self.calculate_denoised(x, noise_pred, sigma)
uncond_denoised = self.calculate_denoised(x, noise_uc, sigma)
return denoised, uncond_denoised
class SDXLLightning(SDXL):
def __init__(self,
solver_config: dict,
base_model_key:str="stabilityai/stable-diffusion-xl-base-1.0",
#light_model_ckpt:str="ckpt/sdxl_lightning_4step_unet.safetensors",
light_model_ckpt:str="ckpt/LEOSAM HelloWorld 极速版_6.0 Lightning.safetensors",
dtype=torch.float16,
device='cuda'):
self.device = device
# load the student model
"""
unet = UNet2DConditionModel.from_config(base_model_key, subfolder="unet").to("cuda", torch.float16)
ext = os.path.splitext(light_model_ckpt)[1]
if ext == ".safetensors":
state_dict = load_file(light_model_ckpt)
else:
state_dict = torch.load(light_model_ckpt, map_location="cpu")
print(unet.load_state_dict(state_dict, strict=True))
unet.requires_grad_(False)
self.unet = unet
"""
pipe = StableDiffusionXLPipeline.from_single_file(light_model_ckpt, torch_dtype=dtype).to(device)
self.unet = pipe.unet
#pipe = StableDiffusionXLPipeline.from_pretrained(base_model_key, unet=self.unet, torch_dtype=dtype).to(device)
self.dtype = dtype
# avoid overflow in float16
self.vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=dtype).to(device)
self.tokenizer_1 = pipe.tokenizer
self.tokenizer_2 = pipe.tokenizer_2
self.text_enc_1 = pipe.text_encoder
self.text_enc_2 = pipe.text_encoder_2
self.vae_scale_factor = 2 ** (len(self.vae.config.block_out_channels) - 1)
self.default_sample_size = self.unet.config.sample_size
# sampling parameters
self.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing")
self.total_alphas = self.scheduler.alphas_cumprod.clone()
self.sigmas = (1-self.total_alphas).sqrt() / self.total_alphas.sqrt()
self.log_sigmas = self.sigmas.log()
N_ts = len(self.scheduler.timesteps)
self.scheduler.set_timesteps(solver_config.num_sampling, device=device)
self.skip = N_ts // solver_config.num_sampling
#self.final_alpha_cumprod = self.scheduler.final_alpha_cumprod.to(device)
self.scheduler.alphas_cumprod = torch.cat([torch.tensor([1.0]), self.scheduler.alphas_cumprod]).to(device)
###########################################
# Base version
###########################################
@register_solver('ddim')
class BaseDDIM(SDXL):
def reverse_process(self,
null_prompt_embeds,
prompt_embeds,
cfg_guidance,
add_cond_kwargs,
shape=(1024, 1024),
callback_fn=None,
**kwargs):
#################################
# Sample region - where to change
#################################
# initialize zT
zt = self.initialize_latent(size=(1, 4, shape[1] // self.vae_scale_factor, shape[0] // self.vae_scale_factor))
# sampling
pbar = tqdm(self.scheduler.timesteps.int(), desc='SDXL')
for step, t in enumerate(pbar):
next_t = t - self.skip
at = self.scheduler.alphas_cumprod[t]
at_next = self.scheduler.alphas_cumprod[next_t]
with torch.no_grad():
noise_uc, noise_c = self.predict_noise(zt, t, null_prompt_embeds, prompt_embeds, add_cond_kwargs)
noise_pred = noise_uc + cfg_guidance * (noise_c - noise_uc)
# tweedie
z0t = (zt - (1-at).sqrt() * noise_pred) / at.sqrt()
# add noise
zt = at_next.sqrt() * z0t + (1-at_next).sqrt() * noise_pred
if callback_fn is not None:
callback_kwargs = { 'z0t': z0t.detach(),
'zt': zt.detach(),
'decode': self.decode}
callback_kwargs = callback_fn(step, t, callback_kwargs)
z0t = callback_kwargs["z0t"]
zt = callback_kwargs["zt"]
# for the last stpe, do not add noise
return z0t
@register_solver('euler')
class Euler(SDXL):
quantize = True
"""
Karras Euler (VE casted)
"""
@torch.autocast(device_type='cuda', dtype=torch.float16)
def reverse_process(self,
null_prompt_embeds,
prompt_embeds,
cfg_guidance,
add_cond_kwargs,
shape=(1024, 1024),
callback_fn=None,
**kwargs):
# convert to karras sigma scheduler
total_sigmas = (1-self.total_alphas).sqrt() / self.total_alphas.sqrt()
sigmas = get_sigmas_karras(len(self.scheduler.timesteps), total_sigmas.min(), total_sigmas.max(), rho=7.)
# initialize
zt_dim = (1, 4, shape[1] // self.vae_scale_factor, shape[0] // self.vae_scale_factor)
zt = self.initialize_latent(method="random_kdiffusion",
latent_dim=zt_dim,
sigmas=sigmas).to(torch.float16)
# sampling
pbar = tqdm(self.scheduler.timesteps.int(), desc='SDXL')
for step, t in enumerate(pbar):
sigma = sigmas[step]
t = self.timestep(sigma).to(self.device)
with torch.no_grad():
z0t, _ = self.kdiffusion_zt_to_denoised(zt, sigma, null_prompt_embeds, prompt_embeds, cfg_guidance, t, add_cond_kwargs)
d = self.to_d(zt, sigma, z0t)
# Euler method
zt = z0t + d * sigmas[step+1]
if callback_fn is not None:
callback_kwargs = { 'z0t': z0t.detach(),
'zt': zt.detach(),
'decode': self.decode}
callback_kwargs = callback_fn(step, t, callback_kwargs)
z0t = callback_kwargs["z0t"]
zt = callback_kwargs["zt"]
# for the last stpe, do not add noise
return z0t
@register_solver('ddim_lightning')
class BaseDDIMLight(BaseDDIM, SDXLLightning):
def __init__(self, **kwargs):
SDXLLightning.__init__(self, **kwargs)
def reverse_process(self,
null_prompt_embeds,
prompt_embeds,
cfg_guidance,
add_cond_kwargs,
shape=(1024, 1024),
callback_fn=None,
**kwargs):
assert cfg_guidance == 1.0, "CFG should be turned off in the lightning version"
return super().reverse_process(null_prompt_embeds,
prompt_embeds,
cfg_guidance,
add_cond_kwargs,
shape,
callback_fn,
**kwargs)
@register_solver('euler_lightning')
class EulerLight(Euler, SDXLLightning):
"""
Karras Euler (VE casted)
"""
quantize = True
def __init__(self, **kwargs):
SDXLLightning.__init__(self, **kwargs)
@torch.autocast(device_type='cuda', dtype=torch.float16)
def reverse_process(self,
null_prompt_embeds,
prompt_embeds,
cfg_guidance,
add_cond_kwargs,
shape=(1024, 1024),
callback_fn=None,
**kwargs):
assert cfg_guidance == 1.0, "CFG should be turned off in the lightning version"
return super().reverse_process(null_prompt_embeds,
prompt_embeds,
cfg_guidance,
add_cond_kwargs,
shape,
callback_fn,
**kwargs)
@register_solver("ddim_edit")
class EditWardSwapDDIM(BaseDDIM):
@torch.autocast(device_type='cuda', dtype=torch.float16)
def sample(self,
prompt1 = ["", "", ""],
prompt2 = ["", "", ""],
cfg_guidance:float=5.0,
original_size: Optional[Tuple[int, int]]=None,
crops_coords_top_left: Tuple[int, int]=(0, 0),
target_size: Optional[Tuple[int, int]]=None,
negative_original_size: Optional[Tuple[int, int]]=None,
negative_crops_coords_top_left: Tuple[int, int]=(0, 0),
negative_target_size: Optional[Tuple[int, int]]=None,
clip_skip: Optional[int]=None,
**kwargs):
# 0. Default height and width to unet
height = self.default_sample_size * self.vae_scale_factor
width = self.default_sample_size * self.vae_scale_factor
original_size = original_size or (height, width)
target_size = target_size or (height, width)
# embedding
(null_prompt_embeds,
src_prompt_embeds,
pool_null_embed,
pool_src_prompt_embed) = self.get_text_embed(prompt1[0], prompt1[1], prompt2[0], prompt2[1], clip_skip)
(_,
tgt_prompt_embeds,
_,
pool_tgt_prompt_embed) = self.get_text_embed(prompt1[0], prompt1[2], prompt2[0], prompt2[2], clip_skip)
# prepare kwargs for SDXL
add_src_text_embeds = pool_src_prompt_embed
add_tgt_text_embeds = pool_tgt_prompt_embed
add_time_ids = self._get_add_time_ids(
original_size,
crops_coords_top_left,
target_size,
dtype=src_prompt_embeds.dtype,
text_encoder_projection_dim=int(pool_src_prompt_embed.shape[-1]),
)
if negative_original_size is not None and negative_target_size is not None:
negative_add_time_ids = self._get_add_time_ids(
negative_original_size,
negative_crops_coords_top_left,
negative_target_size,
dtype=src_prompt_embeds.dtype,
text_encoder_projection_dim=int(pool_src_prompt_embed.shape[-1]),
)
else:
negative_add_time_ids = add_time_ids
negative_text_embeds = pool_null_embed
if cfg_guidance != 0.0 and cfg_guidance != 1.0:
# do cfg
add_src_text_embeds = torch.cat([negative_text_embeds, add_src_text_embeds], dim=0)
add_tgt_text_embeds = torch.cat([negative_text_embeds, add_tgt_text_embeds], dim=0)
add_time_ids = torch.cat([negative_add_time_ids, add_time_ids], dim=0)
add_src_cond_kwargs = {
'text_embeds': add_src_text_embeds.to(self.device),
'time_ids': add_time_ids.to(self.device)
}
add_tgt_cond_kwargs = {
'text_embeds': add_tgt_text_embeds.to(self.device),
'time_ids': add_time_ids.to(self.device)
}
# reverse sampling
zt = self.reverse_process(null_prompt_embeds,
src_prompt_embeds,
tgt_prompt_embeds,
cfg_guidance,
add_src_cond_kwargs,
add_tgt_cond_kwargs,
**kwargs)
# decode
with torch.no_grad():
img = self.decode(zt)
img = (img / 2 + 0.5).clamp(0, 1)
return img.detach().cpu()
def reverse_process(self,
null_prompt_embeds,
src_prompt_embeds,
tgt_prompt_embed,
cfg_guidance,
add_src_cond_kwargs,
add_tgt_cond_kwargs,
callback_fn=None,
**kwargs):
#################################
# Sample region - where to change
#################################
# initialize zT
zt = self.initialize_latent(method='ddim',
src_img=kwargs.get('src_img', None),
uc=null_prompt_embeds,
c=src_prompt_embeds,
cfg_guidance=cfg_guidance,
add_cond_kwargs=add_src_cond_kwargs)
# sampling
pbar = tqdm(self.scheduler.timesteps, desc='SDXL')
for step, t in enumerate(pbar):
at = self.alpha(t)
at_next = self.alpha(t - self.skip)
with torch.no_grad():
noise_uc, noise_c = self.predict_noise(zt, t,
null_prompt_embeds,
tgt_prompt_embed,
add_tgt_cond_kwargs)
noise_pred = noise_uc + cfg_guidance * (noise_c - noise_uc)
# tweedie
z0t = (zt - (1-at).sqrt() * noise_pred) / at.sqrt()
# add noise
zt = at_next.sqrt() * z0t + (1-at_next).sqrt() * noise_pred
if callback_fn is not None:
callback_kwargs = {'z0t': z0t.detach(),
'zt': zt.detach(),
'decode': self.decode}
callback_kwargs = callback_fn(step, t, callback_kwargs)
z0t = callback_kwargs["z0t"]
zt = callback_kwargs["zt"]
# for the last stpe, do not add noise
return z0t
###########################################
# CFG++ version
###########################################
@register_solver("ddim_cfg++")
class BaseDDIMCFGpp(SDXL):
def reverse_process(self,
null_prompt_embeds,
prompt_embeds,
cfg_guidance,
add_cond_kwargs,
shape=(1024, 1024),
callback_fn=None,
**kwargs):
#################################
# Sample region - where to change
#################################
# initialize zT
zt = self.initialize_latent(size=(1, 4, shape[1] // self.vae_scale_factor, shape[0] // self.vae_scale_factor))
# sampling
pbar = tqdm(self.scheduler.timesteps.int(), desc='SDXL')
for step, t in enumerate(pbar):
next_t = t - self.skip
at = self.scheduler.alphas_cumprod[t]
at_next = self.scheduler.alphas_cumprod[next_t]
with torch.no_grad():
noise_uc, noise_c = self.predict_noise(zt, t, null_prompt_embeds, prompt_embeds, add_cond_kwargs)
noise_pred = noise_uc + cfg_guidance * (noise_c - noise_uc)
# tweedie
z0t = (zt - (1-at).sqrt() * noise_pred) / at.sqrt()
# add noise
zt = at_next.sqrt() * z0t + (1-at_next).sqrt() * noise_uc
if callback_fn is not None:
callback_kwargs = { 'z0t': z0t.detach(),
'zt': zt.detach(),
'decode': self.decode}
callback_kwargs = callback_fn(step, t, callback_kwargs)
z0t = callback_kwargs["z0t"]
zt = callback_kwargs["zt"]
# for the last stpe, do not add noise
return z0t
@register_solver('euler_cfg++')
class EulerCFGpp(SDXL):
quantize = True
"""
Karras Euler (VE casted)
"""
@torch.autocast(device_type='cuda', dtype=torch.float16)
def reverse_process(self,
null_prompt_embeds,
prompt_embeds,
cfg_guidance,
add_cond_kwargs,
shape=(1024, 1024),
callback_fn=None,
**kwargs):
# convert to karras sigma scheduler
total_sigmas = (1-self.total_alphas).sqrt() / self.total_alphas.sqrt()
#sigmas = get_sigmas_karras(len(self.scheduler.timesteps), total_sigmas.min(), total_sigmas.max(), rho=7.)
print(self.scheduler.timesteps.cpu().int())
sigmas = total_sigmas[torch.round(self.scheduler.timesteps.cpu()).int()]
sigmas = torch.cat([sigmas, torch.tensor([0.0])])
# initialize
zt_dim = (1, 4, shape[1] // self.vae_scale_factor, shape[0] // self.vae_scale_factor)
zt = self.initialize_latent(method="random_kdiffusion",
latent_dim=zt_dim,
sigmas=sigmas).to(torch.float16)
# sampling
pbar = tqdm(self.scheduler.timesteps.int(), desc='SDXL')
for step, t in enumerate(pbar):
sigma = sigmas[step]
t = self.timestep(sigma).to(self.device)
with torch.no_grad():
z0t, z0t_uncond = self.kdiffusion_zt_to_denoised(zt, sigma, null_prompt_embeds, prompt_embeds, cfg_guidance, t, add_cond_kwargs)
d = self.to_d(zt, sigma, z0t_uncond)
# Euler method
zt = z0t + d * sigmas[step+1]
if callback_fn is not None:
callback_kwargs = {'z0t': z0t.detach(),
'zt': zt.detach(),
'decode': self.decode}
callback_kwargs = callback_fn(step, t, callback_kwargs)
z0t = callback_kwargs["z0t"]
zt = callback_kwargs["zt"]
# for the last stpe, do not add noise
return z0t
@register_solver('euler_cfg++_lightning')
class EulerCFGppLight(EulerCFGpp, SDXLLightning):
"""
Karras Euler (VE casted)
"""
quantize = True
def __init__(self, **kwargs):
SDXLLightning.__init__(self, **kwargs)
@torch.autocast(device_type='cuda', dtype=torch.float16)
def reverse_process(self,
null_prompt_embeds,
prompt_embeds,
cfg_guidance,
add_cond_kwargs,
shape=(1024, 1024),
callback_fn=None,
**kwargs):
assert cfg_guidance == 1.0, "CFG should be turned off in the lightning version"
return super().reverse_process(null_prompt_embeds,
prompt_embeds,
cfg_guidance,
add_cond_kwargs,
shape,
callback_fn,
**kwargs)
@register_solver('ddim_cfg++_lightning')
class BaseDDIMCFGppLight(BaseDDIMCFGpp, SDXLLightning):
def __init__(self, **kwargs):
SDXLLightning.__init__(self, **kwargs)
def reverse_process(self,
null_prompt_embeds,
prompt_embeds,
cfg_guidance,
add_cond_kwargs,
shape=(1024, 1024),
callback_fn=None,
**kwargs):
assert cfg_guidance == 1.0, "CFG should be turned off in the lightning version"
return super().reverse_process(null_prompt_embeds,
prompt_embeds,
cfg_guidance,
add_cond_kwargs,
shape,
callback_fn,
**kwargs)
@register_solver('dpm++_2m_cfgpp')
class DPMpp2mCFGppSolver(SDXL):
quantize = True
@torch.autocast("cuda")
def reverse_process(self,
null_prompt_embeds,
prompt_embeds,
cfg_guidance,
add_cond_kwargs,
shape=(1024, 1024),
callback_fn=None,
**kwargs):
#################################
# Sample region - where to change
#################################
# prepare alphas and sigmas
alphas = self.scheduler.alphas_cumprod[self.scheduler.timesteps.int().cpu()].cpu()
sigmas = (1-alphas).sqrt() / alphas.sqrt()
# initialize
x = self.initialize_latent(method='random',
size=(1, 4, shape[1] // self.vae_scale_factor, shape[0] // self.vae_scale_factor)).to(torch.float16)
x = x * sigmas[0]
t_fn = lambda sigma: sigma.log().neg()
old_denoised = None # initial value
# sampling
pbar = tqdm(self.scheduler.timesteps[:-1].int(), desc='SDXL')
for i, _ in enumerate(pbar):
at = alphas[i]
sigma = sigmas[i]
c_in = at.clone().sqrt()
c_out = -sigma.clone()
new_t = self.sigma_to_t(sigma).to(self.device)
with torch.no_grad():
noise_uc, noise_c = self.predict_noise(x * c_in, new_t, null_prompt_embeds, prompt_embeds, add_cond_kwargs)
noise_pred = noise_uc + cfg_guidance * (noise_c - noise_uc)
# tweedie, VE version
denoised = x + c_out * noise_pred
uncond_denoised = x + c_out * noise_uc
# solve ODE one step
t, t_next = t_fn(sigmas[i]), t_fn(sigmas[i+1])
h = t_next - t
if old_denoised is None or sigmas[i+1] == 0:
x = denoised + self.to_d(x, sigmas[i], uncond_denoised) * sigmas[i+1]
else:
h_last = t - t_fn(sigmas[i-1])
r = h_last / h
extra1 = -torch.exp(-h) * uncond_denoised - (-h).expm1() * (uncond_denoised - old_denoised) / (2*r)
extra2 = torch.exp(-h) * x
x = denoised + extra1 + extra2
old_denoised = uncond_denoised
if callback_fn is not None:
callback_kwargs = { 'z0t': denoised.detach(),
'zt': x.detach(),
'decode': self.decode}
callback_kwargs = callback_fn(i, new_t, callback_kwargs)
denoised = callback_kwargs["z0t"]
x = callback_kwargs["zt"]
# for the last stpe, do not add noise
return x
@register_solver('dpm++_2m_cfgpp_lightning')
class DPMpp2mCFGppLightningSolver(DPMpp2mCFGppSolver, SDXLLightning):
def __init__(self, **kwargs):
SDXLLightning.__init__(self, **kwargs)
def reverse_process(self,
null_prompt_embeds,
prompt_embeds,
cfg_guidance,
add_cond_kwargs,
shape=(1024, 1024),
callback_fn=None,
**kwargs):
assert cfg_guidance == 1.0, "CFG should be turned off in the lightning version"
return super().reverse_process(null_prompt_embeds,
prompt_embeds,
cfg_guidance,
add_cond_kwargs,
shape,
callback_fn,
**kwargs)
@register_solver("ddim_edit_cfg++")
class EditWardSwapDDIMCFGpp(EditWardSwapDDIM):
@torch.no_grad()
def inversion(self, z0, uc, c, cfg_guidance, add_cond_kwargs):
# if we use cfg_guidance=0.0 or 1.0 for inversion, add_cond_kwargs must be splitted.
if cfg_guidance == 0.0 or cfg_guidance == 1.0:
add_cond_kwargs['text_embeds'] = add_cond_kwargs['text_embeds'][-1].unsqueeze(0)
add_cond_kwargs['time_ids'] = add_cond_kwargs['time_ids'][-1].unsqueeze(0)
zt = z0.clone().to(self.device)
pbar = tqdm(reversed(self.scheduler.timesteps), desc='DDIM inversion')
for _, t in enumerate(pbar):
at = self.alpha(t)
at_prev = self.alpha(t - self.skip)
noise_uc, noise_c = self.predict_noise(zt, t, uc, c, add_cond_kwargs)
noise_pred = noise_uc + cfg_guidance * (noise_c - noise_uc)
z0t = (zt - (1-at_prev).sqrt() * noise_uc) / at_prev.sqrt()
zt = at.sqrt() * z0t + (1-at).sqrt() * noise_pred
return zt
def reverse_process(self,
null_prompt_embeds,
src_prompt_embeds,
tgt_prompt_embed,
cfg_guidance,
add_src_cond_kwargs,
add_tgt_cond_kwargs,
callback_fn=None,
**kwargs):
#################################
# Sample region - where to change
#################################
# initialize zT
zt = self.initialize_latent(method='ddim',
src_img=kwargs.get('src_img', None),
uc=null_prompt_embeds,
c=src_prompt_embeds,
cfg_guidance=cfg_guidance,
add_cond_kwargs=add_src_cond_kwargs)
# sampling
pbar = tqdm(self.scheduler.timesteps, desc='SDXL')
for step, t in enumerate(pbar):
at = self.alpha(t)