File size: 64,041 Bytes
3b2b77a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa91ca4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b2b77a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7a40ad
3b2b77a
 
 
 
 
 
 
6fe0b16
3b2b77a
 
123f4ac
3b2b77a
 
 
 
 
 
6fe0b16
3b2b77a
 
123f4ac
3b2b77a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fe0b16
3b2b77a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fe0b16
 
 
 
f343ea1
6fe0b16
3b2b77a
 
6fe0b16
3b2b77a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f343ea1
3b2b77a
 
 
6fe0b16
3b2b77a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fe0b16
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
import torch.nn.functional as F
import os
from typing import List

import torch
from diffusers import StableDiffusionPipeline
from diffusers.pipelines.controlnet import MultiControlNetModel
from PIL import Image
from safetensors import safe_open
from transformers import CLIPImageProcessor, CLIPVisionModelWithProjection
from torchvision import transforms
from .utils import is_torch2_available, get_generator

if is_torch2_available():
    from .attention_processor import (
        AttnProcessor2_0 as AttnProcessor,
    )
    from .attention_processor import (
        CNAttnProcessor2_0 as CNAttnProcessor,
    )
    from .attention_processor import (
        IPAttnProcessor2_0 as IPAttnProcessor,
    )
    from .attention_processor import IP_CS_AttnProcessor2_0 as IP_CS_AttnProcessor
    from .attention_processor import AttnProcessor2_0_hijack as AttnProcessor_hijack
    from .attention_processor import IPAttnProcessor2_0_cross_modal as IPAttnProcessor_cross_modal
else:
    from .attention_processor import AttnProcessor, CNAttnProcessor, IPAttnProcessor

from .resampler import Resampler

from transformers import AutoImageProcessor, AutoModel


class ImageProjModel(torch.nn.Module):
    """Projection Model"""

    def __init__(self, cross_attention_dim=1024, clip_embeddings_dim=1024, clip_extra_context_tokens=4):
        super().__init__()

        self.generator = None
        self.cross_attention_dim = cross_attention_dim
        self.clip_extra_context_tokens = clip_extra_context_tokens
        # print(clip_embeddings_dim, self.clip_extra_context_tokens, cross_attention_dim)
        self.proj = torch.nn.Linear(clip_embeddings_dim, self.clip_extra_context_tokens * cross_attention_dim)
        self.norm = torch.nn.LayerNorm(cross_attention_dim)

    def forward(self, image_embeds):
        embeds = image_embeds
        clip_extra_context_tokens = self.proj(embeds).reshape(
            -1, self.clip_extra_context_tokens, self.cross_attention_dim
        )
        clip_extra_context_tokens = self.norm(clip_extra_context_tokens)
        return clip_extra_context_tokens


class MLPProjModel(torch.nn.Module):
    """SD model with image prompt"""

    def __init__(self, cross_attention_dim=1024, clip_embeddings_dim=1024):
        super().__init__()

        self.proj = torch.nn.Sequential(
            torch.nn.Linear(clip_embeddings_dim, clip_embeddings_dim),
            torch.nn.GELU(),
            torch.nn.Linear(clip_embeddings_dim, cross_attention_dim),
            torch.nn.LayerNorm(cross_attention_dim)
        )

    def forward(self, image_embeds):
        clip_extra_context_tokens = self.proj(image_embeds)
        return clip_extra_context_tokens


class IPAdapter:
    def __init__(self, sd_pipe, image_encoder_path, ip_ckpt, device, num_tokens=4, target_blocks=["block"]):
        self.device = device
        self.image_encoder_path = image_encoder_path
        self.ip_ckpt = ip_ckpt
        self.num_tokens = num_tokens
        self.target_blocks = target_blocks

        self.pipe = sd_pipe.to(self.device)
        self.set_ip_adapter()

        # load image encoder
        self.image_encoder = CLIPVisionModelWithProjection.from_pretrained(self.image_encoder_path).to(
            self.device, dtype=torch.float16
        )
        self.clip_image_processor = CLIPImageProcessor()
        # image proj model
        self.image_proj_model = self.init_proj()

        self.load_ip_adapter()

    def init_proj(self):
        image_proj_model = ImageProjModel(
            cross_attention_dim=self.pipe.unet.config.cross_attention_dim,
            clip_embeddings_dim=self.image_encoder.config.projection_dim,
            clip_extra_context_tokens=self.num_tokens,
        ).to(self.device, dtype=torch.float16)
        return image_proj_model

    def set_ip_adapter(self):
        unet = self.pipe.unet
        attn_procs = {}
        for name in unet.attn_processors.keys():
            cross_attention_dim = None if name.endswith("attn1.processor") else unet.config.cross_attention_dim
            if name.startswith("mid_block"):
                hidden_size = unet.config.block_out_channels[-1]
            elif name.startswith("up_blocks"):
                block_id = int(name[len("up_blocks.")])
                hidden_size = list(reversed(unet.config.block_out_channels))[block_id]
            elif name.startswith("down_blocks"):
                block_id = int(name[len("down_blocks.")])
                hidden_size = unet.config.block_out_channels[block_id]
            if cross_attention_dim is None:
                attn_procs[name] = AttnProcessor()
            else:
                selected = False
                for block_name in self.target_blocks:
                    if block_name in name:
                        selected = True
                        break
                if selected:
                    attn_procs[name] = IPAttnProcessor(
                        hidden_size=hidden_size,
                        cross_attention_dim=cross_attention_dim,
                        scale=1.0,
                        num_tokens=self.num_tokens,
                    ).to(self.device, dtype=torch.float16)
                else:
                    attn_procs[name] = IPAttnProcessor(
                        hidden_size=hidden_size,
                        cross_attention_dim=cross_attention_dim,
                        scale=1.0,
                        num_tokens=self.num_tokens,
                        skip=True
                    ).to(self.device, dtype=torch.float16)
        unet.set_attn_processor(attn_procs)
        if hasattr(self.pipe, "controlnet"):
            if isinstance(self.pipe.controlnet, MultiControlNetModel):
                for controlnet in self.pipe.controlnet.nets:
                    controlnet.set_attn_processor(CNAttnProcessor(num_tokens=self.num_tokens))
            else:
                self.pipe.controlnet.set_attn_processor(CNAttnProcessor(num_tokens=self.num_tokens))

    def load_ip_adapter(self):
        if os.path.splitext(self.ip_ckpt)[-1] == ".safetensors":
            state_dict = {"image_proj": {}, "ip_adapter": {}}
            with safe_open(self.ip_ckpt, framework="pt", device="cpu") as f:
                for key in f.keys():
                    if key.startswith("image_proj."):
                        state_dict["image_proj"][key.replace("image_proj.", "")] = f.get_tensor(key)
                    elif key.startswith("ip_adapter."):
                        state_dict["ip_adapter"][key.replace("ip_adapter.", "")] = f.get_tensor(key)
        else:
            state_dict = torch.load(self.ip_ckpt, map_location="cpu")
        self.image_proj_model.load_state_dict(state_dict["image_proj"])
        ip_layers = torch.nn.ModuleList(self.pipe.unet.attn_processors.values())
        ip_layers.load_state_dict(state_dict["ip_adapter"], strict=False)

    @torch.inference_mode()
    def get_image_embeds(self, pil_image=None, clip_image_embeds=None, content_prompt_embeds=None):
        if pil_image is not None:
            if isinstance(pil_image, Image.Image):
                pil_image = [pil_image]
            clip_image = self.clip_image_processor(images=pil_image, return_tensors="pt").pixel_values
            clip_image_embeds = self.image_encoder(clip_image.to(self.device, dtype=torch.float16)).image_embeds
        else:
            clip_image_embeds = clip_image_embeds.to(self.device, dtype=torch.float16)

        if content_prompt_embeds is not None:
            clip_image_embeds = clip_image_embeds - content_prompt_embeds

        image_prompt_embeds = self.image_proj_model(clip_image_embeds)
        uncond_image_prompt_embeds = self.image_proj_model(torch.zeros_like(clip_image_embeds))
        return image_prompt_embeds, uncond_image_prompt_embeds

    def set_scale(self, scale):
        for attn_processor in self.pipe.unet.attn_processors.values():
            if isinstance(attn_processor, IPAttnProcessor):
                attn_processor.scale = scale

    def generate(
            self,
            pil_image=None,
            clip_image_embeds=None,
            prompt=None,
            negative_prompt=None,
            scale=1.0,
            num_samples=4,
            seed=None,
            guidance_scale=7.5,
            num_inference_steps=30,
            neg_content_emb=None,
            **kwargs,
    ):
        self.set_scale(scale)

        if pil_image is not None:
            num_prompts = 1 if isinstance(pil_image, Image.Image) else len(pil_image)
        else:
            num_prompts = clip_image_embeds.size(0)

        if prompt is None:
            prompt = "best quality, high quality"
        if negative_prompt is None:
            negative_prompt = "monochrome, lowres, bad anatomy, worst quality, low quality"

        if not isinstance(prompt, List):
            prompt = [prompt] * num_prompts
        if not isinstance(negative_prompt, List):
            negative_prompt = [negative_prompt] * num_prompts

        image_prompt_embeds, uncond_image_prompt_embeds = self.get_image_embeds(
            pil_image=pil_image, clip_image_embeds=clip_image_embeds, content_prompt_embeds=neg_content_emb
        )
        bs_embed, seq_len, _ = image_prompt_embeds.shape
        image_prompt_embeds = image_prompt_embeds.repeat(1, num_samples, 1)
        image_prompt_embeds = image_prompt_embeds.view(bs_embed * num_samples, seq_len, -1)
        uncond_image_prompt_embeds = uncond_image_prompt_embeds.repeat(1, num_samples, 1)
        uncond_image_prompt_embeds = uncond_image_prompt_embeds.view(bs_embed * num_samples, seq_len, -1)

        with torch.inference_mode():
            prompt_embeds_, negative_prompt_embeds_ = self.pipe.encode_prompt(
                prompt,
                device=self.device,
                num_images_per_prompt=num_samples,
                do_classifier_free_guidance=True,
                negative_prompt=negative_prompt,
            )
            prompt_embeds = torch.cat([prompt_embeds_, image_prompt_embeds], dim=1)
            negative_prompt_embeds = torch.cat([negative_prompt_embeds_, uncond_image_prompt_embeds], dim=1)

        generator = get_generator(seed, self.device)

        images = self.pipe(
            prompt_embeds=prompt_embeds,
            negative_prompt_embeds=negative_prompt_embeds,
            guidance_scale=guidance_scale,
            num_inference_steps=num_inference_steps,
            generator=generator,
            **kwargs,
        ).images

        return images

class IPAdapterPlus(IPAdapter):
    """IP-Adapter with fine-grained features"""

    def init_proj(self):
        image_proj_model = Resampler(
            dim=self.pipe.unet.config.cross_attention_dim,
            depth=4,
            dim_head=64,
            heads=12,
            num_queries=self.num_tokens,
            embedding_dim=self.image_encoder.config.hidden_size,
            output_dim=self.pipe.unet.config.cross_attention_dim,
            ff_mult=4,
        ).to(self.device, dtype=torch.float16)
        return image_proj_model

    @torch.inference_mode()
    def get_image_embeds(self, pil_image=None, clip_image_embeds=None):
        if isinstance(pil_image, Image.Image):
            pil_image = [pil_image]
        clip_image = self.clip_image_processor(images=pil_image, return_tensors="pt").pixel_values
        clip_image = clip_image.to(self.device, dtype=torch.float16)
        clip_image_embeds = self.image_encoder(clip_image, output_hidden_states=True).hidden_states[-2]
        image_prompt_embeds = self.image_proj_model(clip_image_embeds)
        uncond_clip_image_embeds = self.image_encoder(
            torch.zeros_like(clip_image), output_hidden_states=True
        ).hidden_states[-2]
        uncond_image_prompt_embeds = self.image_proj_model(uncond_clip_image_embeds)
        return image_prompt_embeds, uncond_image_prompt_embeds

class IPAdapter_CS:
    def __init__(self, sd_pipe, image_encoder_path, ip_ckpt, device, num_content_tokens=4,
                 num_style_tokens=4,
                 target_content_blocks=["block"], target_style_blocks=["block"], content_image_encoder_path=None,
                 controlnet_adapter=False,
                 controlnet_target_content_blocks=None,
                 controlnet_target_style_blocks=None,
                 content_model_resampler=False,
                 style_model_resampler=False,
                ):
        self.device = device
        self.image_encoder_path = image_encoder_path
        self.ip_ckpt = ip_ckpt
        self.num_content_tokens = num_content_tokens
        self.num_style_tokens = num_style_tokens
        self.content_target_blocks = target_content_blocks
        self.style_target_blocks = target_style_blocks

        self.content_model_resampler = content_model_resampler
        self.style_model_resampler = style_model_resampler

        self.controlnet_adapter = controlnet_adapter
        self.controlnet_target_content_blocks = controlnet_target_content_blocks
        self.controlnet_target_style_blocks = controlnet_target_style_blocks

        self.pipe = sd_pipe.to(self.device)
        self.set_ip_adapter()
        self.content_image_encoder_path = content_image_encoder_path


        # load image encoder
        if content_image_encoder_path is not None:
            self.content_image_encoder = AutoModel.from_pretrained(content_image_encoder_path).to(self.device,
                                                                                                  dtype=torch.float16)
            self.content_image_processor = AutoImageProcessor.from_pretrained(content_image_encoder_path)
        else:
            self.content_image_encoder = CLIPVisionModelWithProjection.from_pretrained(self.image_encoder_path).to(
                self.device, dtype=torch.float16
            )
            self.content_image_processor = CLIPImageProcessor()
        # model.requires_grad_(False)

        self.image_encoder = CLIPVisionModelWithProjection.from_pretrained(self.image_encoder_path).to(
            self.device, dtype=torch.float16
        )
        # if self.use_CSD is not None:
        #     self.style_image_encoder = CSD_CLIP("vit_large", "default",self.use_CSD+"/ViT-L-14.pt")
        #     model_path = self.use_CSD+"/checkpoint.pth"
        #     checkpoint = torch.load(model_path, map_location="cpu")
        #     state_dict = convert_state_dict(checkpoint['model_state_dict'])
        #     self.style_image_encoder.load_state_dict(state_dict, strict=False)
        #
        #     normalize = transforms.Normalize((0.48145466, 0.4578275, 0.40821073), (0.26862954, 0.26130258, 0.27577711))
        #     self.style_preprocess = transforms.Compose([
        #         transforms.Resize(size=224, interpolation=Func.InterpolationMode.BICUBIC),
        #         transforms.CenterCrop(224),
        #         transforms.ToTensor(),
        #         normalize,
        #     ])

        self.clip_image_processor = CLIPImageProcessor()
        # image proj model
        self.content_image_proj_model = self.init_proj(self.num_content_tokens, content_or_style_='content',
                                                       model_resampler=self.content_model_resampler)
        self.style_image_proj_model = self.init_proj(self.num_style_tokens, content_or_style_='style',
                                                     model_resampler=self.style_model_resampler)

        self.load_ip_adapter()

    def init_proj(self, num_tokens, content_or_style_='content', model_resampler=False):

        # print('@@@@',self.pipe.unet.config.cross_attention_dim,self.image_encoder.config.projection_dim)
        if content_or_style_ == 'content' and self.content_image_encoder_path is not None:
            image_proj_model = ImageProjModel(
                cross_attention_dim=self.pipe.unet.config.cross_attention_dim,
                clip_embeddings_dim=self.content_image_encoder.config.projection_dim,
                clip_extra_context_tokens=num_tokens,
            ).to(self.device, dtype=torch.float16)
            return image_proj_model

        image_proj_model = ImageProjModel(
            cross_attention_dim=self.pipe.unet.config.cross_attention_dim,
            clip_embeddings_dim=self.image_encoder.config.projection_dim,
            clip_extra_context_tokens=num_tokens,
        ).to(self.device, dtype=torch.float16)
        return image_proj_model

    def set_ip_adapter(self):
        unet = self.pipe.unet
        attn_procs = {}
        for name in unet.attn_processors.keys():
            cross_attention_dim = None if name.endswith("attn1.processor") else unet.config.cross_attention_dim
            if name.startswith("mid_block"):
                hidden_size = unet.config.block_out_channels[-1]
            elif name.startswith("up_blocks"):
                block_id = int(name[len("up_blocks.")])
                hidden_size = list(reversed(unet.config.block_out_channels))[block_id]
            elif name.startswith("down_blocks"):
                block_id = int(name[len("down_blocks.")])
                hidden_size = unet.config.block_out_channels[block_id]
            if cross_attention_dim is None:
                attn_procs[name] = AttnProcessor()
            else:
                # layername_id += 1
                selected = False
                for block_name in self.style_target_blocks:
                    if block_name in name:
                        selected = True
                        # print(name)
                        attn_procs[name] = IP_CS_AttnProcessor(
                            hidden_size=hidden_size,
                            cross_attention_dim=cross_attention_dim,
                            style_scale=1.0,
                            style=True,
                            num_content_tokens=self.num_content_tokens,
                            num_style_tokens=self.num_style_tokens,
                        )
                for block_name in self.content_target_blocks:
                    if block_name in name:
                        # selected = True
                        if selected is False:
                            attn_procs[name] = IP_CS_AttnProcessor(
                                hidden_size=hidden_size,
                                cross_attention_dim=cross_attention_dim,
                                content_scale=1.0,
                                content=True,
                                num_content_tokens=self.num_content_tokens,
                                num_style_tokens=self.num_style_tokens,
                            )
                        else:
                            attn_procs[name].set_content_ipa(content_scale=1.0)
                            # attn_procs[name].content=True

                if selected is False:
                    attn_procs[name] = IP_CS_AttnProcessor(
                        hidden_size=hidden_size,
                        cross_attention_dim=cross_attention_dim,
                        num_content_tokens=self.num_content_tokens,
                        num_style_tokens=self.num_style_tokens,
                        skip=True,
                    )

                attn_procs[name].to(self.device, dtype=torch.float16)
        unet.set_attn_processor(attn_procs)
        if hasattr(self.pipe, "controlnet"):
            if self.controlnet_adapter is False:
                if isinstance(self.pipe.controlnet, MultiControlNetModel):
                    for controlnet in self.pipe.controlnet.nets:
                        controlnet.set_attn_processor(CNAttnProcessor(
                            num_tokens=self.num_content_tokens + self.num_style_tokens))
                else:
                    self.pipe.controlnet.set_attn_processor(CNAttnProcessor(
                        num_tokens=self.num_content_tokens + self.num_style_tokens))

            else:
                controlnet_attn_procs = {}
                controlnet_style_target_blocks = self.controlnet_target_style_blocks
                controlnet_content_target_blocks = self.controlnet_target_content_blocks
                for name in self.pipe.controlnet.attn_processors.keys():
                    # print(name)
                    cross_attention_dim = None if name.endswith(
                        "attn1.processor") else self.pipe.controlnet.config.cross_attention_dim
                    if name.startswith("mid_block"):
                        hidden_size = self.pipe.controlnet.config.block_out_channels[-1]
                    elif name.startswith("up_blocks"):
                        block_id = int(name[len("up_blocks.")])
                        hidden_size = list(reversed(self.pipe.controlnet.config.block_out_channels))[block_id]
                    elif name.startswith("down_blocks"):
                        block_id = int(name[len("down_blocks.")])
                        hidden_size = self.pipe.controlnet.config.block_out_channels[block_id]
                    if cross_attention_dim is None:
                        # layername_id += 1
                        controlnet_attn_procs[name] = AttnProcessor()

                    else:
                        # layername_id += 1
                        selected = False
                        for block_name in controlnet_style_target_blocks:
                            if block_name in name:
                                selected = True
                                # print(name)
                                controlnet_attn_procs[name] = IP_CS_AttnProcessor(
                                    hidden_size=hidden_size,
                                    cross_attention_dim=cross_attention_dim,
                                    style_scale=1.0,
                                    style=True,
                                    num_content_tokens=self.num_content_tokens,
                                    num_style_tokens=self.num_style_tokens,
                                )

                        for block_name in controlnet_content_target_blocks:
                            if block_name in name:
                                if selected is False:
                                    controlnet_attn_procs[name] = IP_CS_AttnProcessor(
                                        hidden_size=hidden_size,
                                        cross_attention_dim=cross_attention_dim,
                                        content_scale=1.0,
                                        content=True,
                                        num_content_tokens=self.num_content_tokens,
                                        num_style_tokens=self.num_style_tokens,
                                    )

                                    selected = True
                                elif selected is True:
                                    controlnet_attn_procs[name].set_content_ipa(content_scale=1.0)

                                # if args.content_image_encoder_type !='dinov2':
                                #     weights = {
                                #         "to_k_ip.weight": state_dict["ip_adapter"][str(layername_id) + ".to_k_ip.weight"],
                                #         "to_v_ip.weight": state_dict["ip_adapter"][str(layername_id) + ".to_v_ip.weight"],
                                #     }
                                #     attn_procs[name].load_state_dict(weights)
                        if selected is False:
                            controlnet_attn_procs[name] = IP_CS_AttnProcessor(
                                hidden_size=hidden_size,
                                cross_attention_dim=cross_attention_dim,
                                num_content_tokens=self.num_content_tokens,
                                num_style_tokens=self.num_style_tokens,
                                skip=True,
                            )
                        controlnet_attn_procs[name].to(self.device, dtype=torch.float16)
                        # layer_name = name.split(".processor")[0]
                        # # print(state_dict["ip_adapter"].keys())
                        # weights = {
                        #     "to_k_ip.weight": state_dict["ip_adapter"][str(layername_id) + ".to_k_ip.weight"],
                        #     "to_v_ip.weight": state_dict["ip_adapter"][str(layername_id) + ".to_v_ip.weight"],
                        # }
                        # attn_procs[name].load_state_dict(weights)
                self.pipe.controlnet.set_attn_processor(controlnet_attn_procs)

    def load_ip_adapter(self):
        if os.path.splitext(self.ip_ckpt)[-1] == ".safetensors":
            state_dict = {"content_image_proj": {}, "style_image_proj": {}, "ip_adapter": {}}
            with safe_open(self.ip_ckpt, framework="pt", device="cpu") as f:
                for key in f.keys():
                    if key.startswith("content_image_proj."):
                        state_dict["content_image_proj"][key.replace("content_image_proj.", "")] = f.get_tensor(key)
                    elif key.startswith("style_image_proj."):
                        state_dict["style_image_proj"][key.replace("style_image_proj.", "")] = f.get_tensor(key)
                    elif key.startswith("ip_adapter."):
                        state_dict["ip_adapter"][key.replace("ip_adapter.", "")] = f.get_tensor(key)
        else:
            state_dict = torch.load(self.ip_ckpt, map_location="cpu")
        self.content_image_proj_model.load_state_dict(state_dict["content_image_proj"])
        self.style_image_proj_model.load_state_dict(state_dict["style_image_proj"])

        if 'conv_in_unet_sd' in state_dict.keys():
            self.pipe.unet.conv_in.load_state_dict(state_dict["conv_in_unet_sd"], strict=True)
        ip_layers = torch.nn.ModuleList(self.pipe.unet.attn_processors.values())
        ip_layers.load_state_dict(state_dict["ip_adapter"], strict=False)

        if self.controlnet_adapter is True:
            print('loading controlnet_adapter')
            self.pipe.controlnet.load_state_dict(state_dict["controlnet_adapter_modules"], strict=False)

    @torch.inference_mode()
    def get_image_embeds(self, pil_image=None, clip_image_embeds=None, content_prompt_embeds=None,
                         content_or_style_=''):
        # if pil_image is not None:
        #     if isinstance(pil_image, Image.Image):
        #         pil_image = [pil_image]
        #     clip_image = self.clip_image_processor(images=pil_image, return_tensors="pt").pixel_values
        #     clip_image_embeds = self.image_encoder(clip_image.to(self.device, dtype=torch.float16)).image_embeds
        # else:
        #     clip_image_embeds = clip_image_embeds.to(self.device, dtype=torch.float16)

        # if content_prompt_embeds is not None:
        #     clip_image_embeds = clip_image_embeds - content_prompt_embeds

        if content_or_style_ == 'content':
            if pil_image is not None:
                if isinstance(pil_image, Image.Image):
                    pil_image = [pil_image]
                if self.content_image_proj_model is not None:
                    clip_image = self.content_image_processor(images=pil_image, return_tensors="pt").pixel_values
                    clip_image_embeds = self.content_image_encoder(
                        clip_image.to(self.device, dtype=torch.float16)).image_embeds
                else:
                    clip_image = self.clip_image_processor(images=pil_image, return_tensors="pt").pixel_values
                    clip_image_embeds = self.image_encoder(clip_image.to(self.device, dtype=torch.float16)).image_embeds
            else:
                clip_image_embeds = clip_image_embeds.to(self.device, dtype=torch.float16)

            image_prompt_embeds = self.content_image_proj_model(clip_image_embeds)
            uncond_image_prompt_embeds = self.content_image_proj_model(torch.zeros_like(clip_image_embeds))
            return image_prompt_embeds, uncond_image_prompt_embeds
        if content_or_style_ == 'style':
            if pil_image is not None:
                if self.use_CSD is not None:
                    clip_image = self.style_preprocess(pil_image).unsqueeze(0).to(self.device, dtype=torch.float32)
                    clip_image_embeds = self.style_image_encoder(clip_image)
                else:
                    if isinstance(pil_image, Image.Image):
                        pil_image = [pil_image]
                    clip_image = self.clip_image_processor(images=pil_image, return_tensors="pt").pixel_values
                    clip_image_embeds = self.image_encoder(clip_image.to(self.device, dtype=torch.float16)).image_embeds


            else:
                clip_image_embeds = clip_image_embeds.to(self.device, dtype=torch.float16)
            image_prompt_embeds = self.style_image_proj_model(clip_image_embeds)
            uncond_image_prompt_embeds = self.style_image_proj_model(torch.zeros_like(clip_image_embeds))
            return image_prompt_embeds, uncond_image_prompt_embeds

    def set_scale(self, content_scale, style_scale):
        for attn_processor in self.pipe.unet.attn_processors.values():
            if isinstance(attn_processor, IP_CS_AttnProcessor):
                if attn_processor.content is True:
                    attn_processor.content_scale = content_scale

                if attn_processor.style is True:
                    attn_processor.style_scale = style_scale
                    # print('style_scale:',style_scale)
        if self.controlnet_adapter is not None:
            for attn_processor in self.pipe.controlnet.attn_processors.values():

                if isinstance(attn_processor, IP_CS_AttnProcessor):
                    if attn_processor.content is True:
                        attn_processor.content_scale = content_scale
                        # print(content_scale)

                    if attn_processor.style is True:
                        attn_processor.style_scale = style_scale

    def generate(
            self,
            pil_content_image=None,
            pil_style_image=None,
            clip_content_image_embeds=None,
            clip_style_image_embeds=None,
            prompt=None,
            negative_prompt=None,
            content_scale=1.0,
            style_scale=1.0,
            num_samples=4,
            seed=None,
            guidance_scale=7.5,
            num_inference_steps=30,
            neg_content_emb=None,
            **kwargs,
    ):
        self.set_scale(content_scale, style_scale)

        if pil_content_image is not None:
            num_prompts = 1 if isinstance(pil_content_image, Image.Image) else len(pil_content_image)
        else:
            num_prompts = clip_content_image_embeds.size(0)

        if prompt is None:
            prompt = "best quality, high quality"
        if negative_prompt is None:
            negative_prompt = "monochrome, lowres, bad anatomy, worst quality, low quality"

        if not isinstance(prompt, List):
            prompt = [prompt] * num_prompts
        if not isinstance(negative_prompt, List):
            negative_prompt = [negative_prompt] * num_prompts

        content_image_prompt_embeds, uncond_content_image_prompt_embeds = self.get_image_embeds(
            pil_image=pil_content_image, clip_image_embeds=clip_content_image_embeds
        )
        style_image_prompt_embeds, uncond_style_image_prompt_embeds = self.get_image_embeds(
            pil_image=pil_style_image, clip_image_embeds=clip_style_image_embeds
        )

        bs_embed, seq_len, _ = content_image_prompt_embeds.shape
        content_image_prompt_embeds = content_image_prompt_embeds.repeat(1, num_samples, 1)
        content_image_prompt_embeds = content_image_prompt_embeds.view(bs_embed * num_samples, seq_len, -1)
        uncond_content_image_prompt_embeds = uncond_content_image_prompt_embeds.repeat(1, num_samples, 1)
        uncond_content_image_prompt_embeds = uncond_content_image_prompt_embeds.view(bs_embed * num_samples, seq_len,
                                                                                     -1)

        bs_style_embed, seq_style_len, _ = content_image_prompt_embeds.shape
        style_image_prompt_embeds = style_image_prompt_embeds.repeat(1, num_samples, 1)
        style_image_prompt_embeds = style_image_prompt_embeds.view(bs_embed * num_samples, seq_style_len, -1)
        uncond_style_image_prompt_embeds = uncond_style_image_prompt_embeds.repeat(1, num_samples, 1)
        uncond_style_image_prompt_embeds = uncond_style_image_prompt_embeds.view(bs_embed * num_samples, seq_style_len,
                                                                                 -1)

        with torch.inference_mode():
            prompt_embeds_, negative_prompt_embeds_ = self.pipe.encode_prompt(
                prompt,
                device=self.device,
                num_images_per_prompt=num_samples,
                do_classifier_free_guidance=True,
                negative_prompt=negative_prompt,
            )
            prompt_embeds = torch.cat([prompt_embeds_, content_image_prompt_embeds, style_image_prompt_embeds], dim=1)
            negative_prompt_embeds = torch.cat([negative_prompt_embeds_,
                                                uncond_content_image_prompt_embeds, uncond_style_image_prompt_embeds],
                                               dim=1)

        generator = get_generator(seed, self.device)

        images = self.pipe(
            prompt_embeds=prompt_embeds,
            negative_prompt_embeds=negative_prompt_embeds,
            guidance_scale=guidance_scale,
            num_inference_steps=num_inference_steps,
            generator=generator,
            **kwargs,
        ).images

        return images


class IPAdapterXL_CS(IPAdapter_CS):
    """SDXL"""

    def generate(
            self,
            pil_content_image,
            pil_style_image,
            prompt=None,
            negative_prompt=None,
            content_scale=1.0,
            style_scale=1.0,
            num_samples=4,
            seed=None,
            content_image_embeds=None,
            style_image_embeds=None,
            num_inference_steps=30,
            neg_content_emb=None,
            neg_content_prompt=None,
            neg_content_scale=1.0,

            **kwargs,
    ):
        self.set_scale(content_scale, style_scale)

        num_prompts = 1 if isinstance(pil_content_image, Image.Image) else len(pil_content_image)

        if prompt is None:
            prompt = "best quality, high quality"
        if negative_prompt is None:
            negative_prompt = "monochrome, lowres, bad anatomy, worst quality, low quality"

        if not isinstance(prompt, List):
            prompt = [prompt] * num_prompts
        if not isinstance(negative_prompt, List):
            negative_prompt = [negative_prompt] * num_prompts

        content_image_prompt_embeds, uncond_content_image_prompt_embeds = self.get_image_embeds(pil_content_image,
                                                                                                content_image_embeds,
                                                                                                content_or_style_='content')



        style_image_prompt_embeds, uncond_style_image_prompt_embeds = self.get_image_embeds(pil_style_image,
                                                                                            style_image_embeds,
                                                                                            content_or_style_='style')

    
        bs_embed, seq_len, _ = content_image_prompt_embeds.shape

        content_image_prompt_embeds = content_image_prompt_embeds.repeat(1, num_samples, 1)
        content_image_prompt_embeds = content_image_prompt_embeds.view(bs_embed * num_samples, seq_len, -1)

        uncond_content_image_prompt_embeds = uncond_content_image_prompt_embeds.repeat(1, num_samples, 1)
        uncond_content_image_prompt_embeds = uncond_content_image_prompt_embeds.view(bs_embed * num_samples, seq_len,
                                                                                     -1)
        bs_style_embed, seq_style_len, _ = style_image_prompt_embeds.shape
        style_image_prompt_embeds = style_image_prompt_embeds.repeat(1, num_samples, 1)
        style_image_prompt_embeds = style_image_prompt_embeds.view(bs_embed * num_samples, seq_style_len, -1)
        uncond_style_image_prompt_embeds = uncond_style_image_prompt_embeds.repeat(1, num_samples, 1)
        uncond_style_image_prompt_embeds = uncond_style_image_prompt_embeds.view(bs_embed * num_samples, seq_style_len,
                                                                                 -1)

        with torch.inference_mode():
            (
                prompt_embeds,
                negative_prompt_embeds,
                pooled_prompt_embeds,
                negative_pooled_prompt_embeds,
            ) = self.pipe.encode_prompt(
                prompt,
                num_images_per_prompt=num_samples,
                do_classifier_free_guidance=True,
                negative_prompt=negative_prompt,
            )
            prompt_embeds = torch.cat([prompt_embeds, content_image_prompt_embeds, style_image_prompt_embeds], dim=1)
            negative_prompt_embeds = torch.cat([negative_prompt_embeds,
                                                uncond_content_image_prompt_embeds, uncond_style_image_prompt_embeds],
                                               dim=1)

        # self.generator = get_generator(seed, self.device)
        # latents = torch.randn((1, 4, 128, 128), generator=self.generator, device="cuda", dtype=torch.float16).to("cuda")
        # latents = latents.repeat(2, 1, 1, 1)
        # print(latents.shape)
        images = self.pipe(
            prompt_embeds=prompt_embeds,
            negative_prompt_embeds=negative_prompt_embeds,
            pooled_prompt_embeds=pooled_prompt_embeds,
            negative_pooled_prompt_embeds=negative_pooled_prompt_embeds,
            num_inference_steps=num_inference_steps,
            # generator=self.generator,
            **kwargs,
        ).images
        return images


class CSGO(IPAdapterXL_CS):
    """SDXL"""

    def init_proj(self, num_tokens, content_or_style_='content', model_resampler=False):
        if content_or_style_ == 'content':
            if model_resampler:
                image_proj_model = Resampler(
                    dim=self.pipe.unet.config.cross_attention_dim,
                    depth=4,
                    dim_head=64,
                    heads=12,
                    num_queries=num_tokens,
                    embedding_dim=self.content_image_encoder.config.hidden_size,
                    output_dim=self.pipe.unet.config.cross_attention_dim,
                    ff_mult=4,
                ).to(self.device, dtype=torch.float16)
            else:
                image_proj_model = ImageProjModel(
                    cross_attention_dim=self.pipe.unet.config.cross_attention_dim,
                    clip_embeddings_dim=self.image_encoder.config.projection_dim,
                    clip_extra_context_tokens=num_tokens,
                ).to(self.device, dtype=torch.float16)
        if content_or_style_ == 'style':
            if model_resampler:
                image_proj_model = Resampler(
                    dim=self.pipe.unet.config.cross_attention_dim,
                    depth=4,
                    dim_head=64,
                    heads=12,
                    num_queries=num_tokens,
                    embedding_dim=self.content_image_encoder.config.hidden_size,
                    output_dim=self.pipe.unet.config.cross_attention_dim,
                    ff_mult=4,
                ).to(self.device, dtype=torch.float16)
            else:
                image_proj_model = ImageProjModel(
                    cross_attention_dim=self.pipe.unet.config.cross_attention_dim,
                    clip_embeddings_dim=self.image_encoder.config.projection_dim,
                    clip_extra_context_tokens=num_tokens,
                ).to(self.device, dtype=torch.float16)
        return image_proj_model

    @torch.inference_mode()
    def get_image_embeds(self, pil_image=None, clip_image_embeds=None, content_or_style_=''):
        if isinstance(pil_image, Image.Image):
            pil_image = [pil_image]
        if content_or_style_ == 'style':
            
            if self.style_model_resampler:
                clip_image = self.clip_image_processor(images=pil_image, return_tensors="pt").pixel_values
                clip_image_embeds = self.image_encoder(clip_image.to(self.device, dtype=torch.float16),
                                                       output_hidden_states=True).hidden_states[-2]
                image_prompt_embeds = self.style_image_proj_model(clip_image_embeds)
                uncond_image_prompt_embeds = self.style_image_proj_model(torch.zeros_like(clip_image_embeds))
            else:


                clip_image = self.clip_image_processor(images=pil_image, return_tensors="pt").pixel_values
                clip_image_embeds = self.image_encoder(clip_image.to(self.device, dtype=torch.float16)).image_embeds
                image_prompt_embeds = self.style_image_proj_model(clip_image_embeds)
                uncond_image_prompt_embeds = self.style_image_proj_model(torch.zeros_like(clip_image_embeds))
            return image_prompt_embeds, uncond_image_prompt_embeds


        else:

            if self.content_image_encoder_path is not None:
                clip_image = self.content_image_processor(images=pil_image, return_tensors="pt").pixel_values
                outputs = self.content_image_encoder(clip_image.to(self.device, dtype=torch.float16),
                                                     output_hidden_states=True)
                clip_image_embeds = outputs.last_hidden_state
                image_prompt_embeds = self.content_image_proj_model(clip_image_embeds)

                # uncond_clip_image_embeds = self.image_encoder(
                #     torch.zeros_like(clip_image), output_hidden_states=True
                # ).last_hidden_state
                uncond_image_prompt_embeds = self.content_image_proj_model(torch.zeros_like(clip_image_embeds))
                return image_prompt_embeds, uncond_image_prompt_embeds

            else:
                if self.content_model_resampler:

                    clip_image = self.clip_image_processor(images=pil_image, return_tensors="pt").pixel_values

                    clip_image = clip_image.to(self.device, dtype=torch.float16)
                    clip_image_embeds = self.image_encoder(clip_image, output_hidden_states=True).hidden_states[-2]
                    # clip_image_embeds = clip_image_embeds.to(self.device, dtype=torch.float16)
                    image_prompt_embeds = self.content_image_proj_model(clip_image_embeds)
                    # uncond_clip_image_embeds = self.image_encoder(
                    #             torch.zeros_like(clip_image), output_hidden_states=True
                    #         ).hidden_states[-2]
                    uncond_image_prompt_embeds = self.content_image_proj_model(torch.zeros_like(clip_image_embeds))
                else:
                    clip_image = self.clip_image_processor(images=pil_image, return_tensors="pt").pixel_values
                    clip_image_embeds = self.image_encoder(clip_image.to(self.device, dtype=torch.float16)).image_embeds
                    image_prompt_embeds = self.content_image_proj_model(clip_image_embeds)
                    uncond_image_prompt_embeds = self.content_image_proj_model(torch.zeros_like(clip_image_embeds))

                return image_prompt_embeds, uncond_image_prompt_embeds

        #     # clip_image = self.clip_image_processor(images=pil_image, return_tensors="pt").pixel_values
        #     clip_image = clip_image.to(self.device, dtype=torch.float16)
        #     clip_image_embeds = self.image_encoder(clip_image, output_hidden_states=True).hidden_states[-2]
        #     image_prompt_embeds = self.content_image_proj_model(clip_image_embeds)
        #     uncond_clip_image_embeds = self.image_encoder(
        #         torch.zeros_like(clip_image), output_hidden_states=True
        #     ).hidden_states[-2]
        #     uncond_image_prompt_embeds = self.content_image_proj_model(uncond_clip_image_embeds)
        # return image_prompt_embeds, uncond_image_prompt_embeds


class StyleStudio_Adapter(CSGO):
    def __init__(self, sd_pipe, image_encoder_path, ip_ckpt, device,
                 num_style_tokens=4,
                 target_style_blocks=["block"], 
                 controlnet_adapter=False,
                 controlnet_target_content_blocks=None,
                 controlnet_target_style_blocks=None,
                 style_model_resampler=False,
                 fuAttn=False,
                 fuSAttn=False,
                 fuIPAttn=False,
                 fuScale=0,
                 adainIP=False,
                 end_fusion=0,
                 save_attn_map=False,
                ):
        self.fuAttn = fuAttn
        self.fuSAttn = fuSAttn
        self.fuIPAttn = fuIPAttn
        self.adainIP = adainIP
        self.fuScale = fuScale
        # if self.adainIP:
        #     print("use the cross modal adain")
        if self.fuSAttn:
            print(f"hijack Self AttnMap in {end_fusion} steps", "fuScale is: ", fuScale)
        if self.fuAttn:
            print(f"hijack Cross AttnMap in {end_fusion} steps", "fuScale is: ", fuScale)
        if self.fuIPAttn:
            print(f"hijack IP AttnMap in {end_fusion} steps", "fuScale is: ", fuScale)
        self.end_fusion = end_fusion
        self.save_attn_map = save_attn_map

        self.device = device
        self.image_encoder_path = image_encoder_path
        self.ip_ckpt = ip_ckpt
        self.num_style_tokens = num_style_tokens
        self.style_target_blocks = target_style_blocks

        self.style_model_resampler = style_model_resampler

        self.controlnet_adapter = controlnet_adapter
        self.controlnet_target_content_blocks = controlnet_target_content_blocks
        self.controlnet_target_style_blocks = controlnet_target_style_blocks

        self.pipe = sd_pipe.to(self.device)
        self.set_ip_adapter()


        # load image encoder
        # model.requires_grad_(False)

        self.image_encoder = CLIPVisionModelWithProjection.from_pretrained(self.image_encoder_path).to(
            self.device, dtype=torch.float16
        )

        self.clip_image_processor = CLIPImageProcessor()
        # image proj model
        self.style_image_proj_model = self.init_proj(self.num_style_tokens, content_or_style_='style',
                                                     model_resampler=self.style_model_resampler)
        self.load_ip_adapter()

    def set_ip_adapter(self):
        unet = self.pipe.unet
        attn_procs = {}
        for name in unet.attn_processors.keys():
            cross_attention_dim = None if name.endswith("attn1.processor") else unet.config.cross_attention_dim
            if name.startswith("mid_block"):
                hidden_size = unet.config.block_out_channels[-1]
            elif name.startswith("up_blocks"):
                block_id = int(name[len("up_blocks.")])
                hidden_size = list(reversed(unet.config.block_out_channels))[block_id]
            elif name.startswith("down_blocks"):
                block_id = int(name[len("down_blocks.")])
                hidden_size = unet.config.block_out_channels[block_id]
            if cross_attention_dim is None:
                attn_procs[name] = AttnProcessor_hijack(
                                        fuSAttn=self.fuSAttn,
                                        end_fusion=self.end_fusion,)
            else:
                # layername_id += 1
                selected = False
                for block_name in self.style_target_blocks:
                    if block_name in name:
                        selected = True
                        # print(name)
                        attn_procs[name] = IPAttnProcessor_cross_modal(
                            hidden_size=hidden_size,
                            cross_attention_dim=cross_attention_dim,
                            num_tokens=self.num_style_tokens,
                            fuAttn=self.fuAttn,
                            fuIPAttn=self.fuIPAttn,
                            adainIP=self.adainIP,
                            end_fusion=self.end_fusion,
                        )
                if selected is False:
                    attn_procs[name] = IPAttnProcessor_cross_modal(
                        hidden_size=hidden_size,
                        cross_attention_dim=cross_attention_dim,
                        num_tokens=self.num_style_tokens,
                        skip=True,
                        fuAttn=self.fuAttn,
                        fuIPAttn=self.fuIPAttn,
                        adainIP=self.adainIP,
                        end_fusion=self.end_fusion,
                    )

                attn_procs[name].to(self.device, dtype=torch.float16)
        unet.set_attn_processor(attn_procs)
        if hasattr(self.pipe, "controlnet"):
            if self.controlnet_adapter is False:
                if isinstance(self.pipe.controlnet, MultiControlNetModel):
                    for controlnet in self.pipe.controlnet.nets:
                        controlnet.set_attn_processor(CNAttnProcessor(
                            num_tokens=self.num_content_tokens + self.num_style_tokens))
                else:
                    self.pipe.controlnet.set_attn_processor(CNAttnProcessor(
                        num_tokens=self.num_content_tokens + self.num_style_tokens))

    def load_ip_adapter(self):
        if os.path.splitext(self.ip_ckpt)[-1] == ".safetensors":
            state_dict = {"content_image_proj": {}, "style_image_proj": {}, "ip_adapter": {}}
            with safe_open(self.ip_ckpt, framework="pt", device="cpu") as f:
                for key in f.keys():
                    if key.startswith("content_image_proj."):
                        state_dict["content_image_proj"][key.replace("content_image_proj.", "")] = f.get_tensor(key)
                    elif key.startswith("style_image_proj."):
                        state_dict["style_image_proj"][key.replace("style_image_proj.", "")] = f.get_tensor(key)
                    elif key.startswith("ip_adapter."):
                        state_dict["ip_adapter"][key.replace("ip_adapter.", "")] = f.get_tensor(key)
        else:
            state_dict = torch.load(self.ip_ckpt, map_location="cpu")
        self.style_image_proj_model.load_state_dict(state_dict["style_image_proj"])

        if 'conv_in_unet_sd' in state_dict.keys():
            self.pipe.unet.conv_in.load_state_dict(state_dict["conv_in_unet_sd"], strict=True)
        ip_layers = torch.nn.ModuleList(self.pipe.unet.attn_processors.values())
        ip_layers.load_state_dict(state_dict["ip_adapter"], strict=False)
    
    def set_scale(self, style_scale):
        for attn_processor in self.pipe.unet.attn_processors.values():
            if isinstance(attn_processor, IPAttnProcessor_cross_modal):
                if attn_processor.style is True:
                    attn_processor.style_scale = style_scale
                    # print('style_scale:',style_scale)
    
    def init_proj(self, num_tokens, content_or_style_='content', model_resampler=False):
        if content_or_style_ == 'content':
            if model_resampler:
                image_proj_model = Resampler(
                    dim=self.pipe.unet.config.cross_attention_dim,
                    depth=4,
                    dim_head=64,
                    heads=12,
                    num_queries=num_tokens,
                    embedding_dim=self.image_encoder.config.hidden_size,
                    output_dim=self.pipe.unet.config.cross_attention_dim,
                    ff_mult=4,
                ).to(self.device, dtype=torch.float16)
            else:
                image_proj_model = ImageProjModel(
                    cross_attention_dim=self.pipe.unet.config.cross_attention_dim,
                    clip_embeddings_dim=self.image_encoder.config.projection_dim,
                    clip_extra_context_tokens=num_tokens,
                ).to(self.device, dtype=torch.float16)
        if content_or_style_ == 'style':
            if model_resampler:
                image_proj_model = Resampler(
                    dim=self.pipe.unet.config.cross_attention_dim,
                    depth=4,
                    dim_head=64,
                    heads=12,
                    num_queries=num_tokens,
                    embedding_dim=self.image_encoder.config.hidden_size,
                    output_dim=self.pipe.unet.config.cross_attention_dim,
                    ff_mult=4,
                ).to(self.device, dtype=torch.float16)
            else:
                image_proj_model = ImageProjModel(
                    cross_attention_dim=self.pipe.unet.config.cross_attention_dim,
                    clip_embeddings_dim=self.image_encoder.config.projection_dim,
                    clip_extra_context_tokens=num_tokens,
                ).to(self.device, dtype=torch.float16)
        return image_proj_model

    @torch.inference_mode()
    def get_image_embeds(self, pil_image=None, clip_image_embeds=None):
        if isinstance(pil_image, Image.Image):
            pil_image = [pil_image]
        if self.style_model_resampler:
            clip_image = self.clip_image_processor(images=pil_image, return_tensors="pt").pixel_values
            clip_image_embeds = self.image_encoder(clip_image.to(self.device, dtype=torch.float16),
                                                    output_hidden_states=True).hidden_states[-2]
            image_prompt_embeds = self.style_image_proj_model(clip_image_embeds)
            uncond_image_prompt_embeds = self.style_image_proj_model(torch.zeros_like(clip_image_embeds))
        else:


            clip_image = self.clip_image_processor(images=pil_image, return_tensors="pt").pixel_values
            clip_image_embeds = self.image_encoder(clip_image.to(self.device, dtype=torch.float16)).image_embeds
            image_prompt_embeds = self.style_image_proj_model(clip_image_embeds)
            uncond_image_prompt_embeds = self.style_image_proj_model(torch.zeros_like(clip_image_embeds))
        return image_prompt_embeds, uncond_image_prompt_embeds
        
    @torch.inference_mode()
    def get_neg_image_embeds(self, pil_image=None, clip_image_embeds=None):
        if isinstance(pil_image, Image.Image):
            pil_image = [pil_image]

        if self.style_model_resampler:
            clip_image = self.clip_image_processor(images=pil_image, return_tensors="pt").pixel_values
            clip_image_embeds = self.image_encoder(clip_image.to(self.device, dtype=torch.float16),
                                                    output_hidden_states=True).hidden_states[-2]     
            neg_image_prompt_embeds = self.style_image_proj_model(clip_image_embeds)
        else:
            clip_image = self.clip_image_processor(images=pil_image, return_tensors="pt").pixel_values
            clip_image_embeds = self.image_encoder(clip_image.to(self.device, dtype=torch.float16)).image_embeds
            neg_image_prompt_embeds = self.style_image_proj_model(clip_image_embeds)
        return neg_image_prompt_embeds
    
    def set_endFusion(self, end_T):
        for attn_processor in self.pipe.unet.attn_processors.values():
            if isinstance(attn_processor, AttnProcessor_hijack):
                attn_processor.end_fusion = end_T
    
    def set_SAttn(self, use_SAttn):
        for attn_processor in self.pipe.unet.attn_processors.values():
            if isinstance(attn_processor, AttnProcessor_hijack):
                attn_processor.fuSAttn = use_SAttn
    
    def set_num_inference_step(self, num_T):
        for attn_processor in self.pipe.unet.attn_processors.values():
            if isinstance(attn_processor, AttnProcessor_hijack) or isinstance(attn_processor, IPAttnProcessor_cross_modal):
                attn_processor.num_inference_step = num_T
                attn_processor.denoise_step = 0

    def set_adain(self, use_CMA):
        for attn_processor in self.pipe.unet.attn_processors.values():
            if isinstance(attn_processor, IPAttnProcessor_cross_modal):
                attn_processor.adainIP = use_CMA

    def generate(
            self,
            pil_style_image,

            neg_pil_style_image=None,
            
            prompt=None,
            negative_prompt=None,
            num_samples=2,
            style_image_embeds=None,
            num_inference_steps=30,
            end_fusion=20,
            cross_modal_adain=True,
            use_SAttn=True,
            **kwargs,
    ):
        print(end_fusion)
        self.set_endFusion(end_T = end_fusion)
        self.set_adain(use_CMA=cross_modal_adain)
        self.set_SAttn(use_SAttn=use_SAttn)
        self.set_num_inference_step(num_T=num_inference_steps)
        
        # self.set_scale(style_scale=style_scale)
        num_prompts = 1 if isinstance(pil_style_image, Image.Image) else len(pil_style_image)

        if prompt is None:
            prompt = "best quality, high quality"
        if negative_prompt is None:
            negative_prompt = "monochrome, lowres, bad anatomy, worst quality, low quality"

        if not isinstance(prompt, List):
            prompt = [prompt] * num_prompts
        if not isinstance(negative_prompt, List):
            negative_prompt = [negative_prompt] * num_prompts

        style_image_prompt_embeds, uncond_style_image_prompt_embeds = self.get_image_embeds(
            pil_style_image,
            style_image_embeds,
        )

        if neg_pil_style_image is not None:
            print("using neg style image")
            neg_style_image_prompt_embeds = self.get_neg_image_embeds(neg_pil_style_image,
                                                                        style_image_embeds,)
            cos_sim_neg = F.cosine_similarity(style_image_prompt_embeds, neg_style_image_prompt_embeds.squeeze(0).unsqueeze(1), dim=-1)
            cos_sim_uncond = F.cosine_similarity(style_image_prompt_embeds, uncond_style_image_prompt_embeds.squeeze(0).unsqueeze(1), dim=-1)
            print(f"neg cos sim is: {cos_sim_neg.diagonal()}")
            print(f"uncond cos sim is: {cos_sim_uncond.diagonal()}")
            uncond_style_image_prompt_embeds = neg_style_image_prompt_embeds

        bs_embed, seq_style_len, _ = style_image_prompt_embeds.shape
        style_image_prompt_embeds = style_image_prompt_embeds.repeat(1, num_samples, 1)
        style_image_prompt_embeds = style_image_prompt_embeds.view(bs_embed * num_samples, seq_style_len, -1)
        uncond_style_image_prompt_embeds = uncond_style_image_prompt_embeds.repeat(1, num_samples, 1)
        uncond_style_image_prompt_embeds = uncond_style_image_prompt_embeds.view(bs_embed * num_samples, seq_style_len,
                                                                                 -1)

        with torch.inference_mode():
            (
                prompt_embeds,
                negative_prompt_embeds,
                pooled_prompt_embeds,
                negative_pooled_prompt_embeds,
            ) = self.pipe.encode_prompt(
                prompt,
                num_images_per_prompt=num_samples,
                do_classifier_free_guidance=True,
                negative_prompt=negative_prompt,
            )
            prompt_embeds = torch.cat([prompt_embeds, style_image_prompt_embeds], dim=1)
            negative_prompt_embeds = torch.cat([negative_prompt_embeds,
                                                uncond_style_image_prompt_embeds],
                                               dim=1)

        images = self.pipe(
            prompt_embeds=prompt_embeds,
            negative_prompt_embeds=negative_prompt_embeds,
            pooled_prompt_embeds=pooled_prompt_embeds,
            negative_pooled_prompt_embeds=negative_pooled_prompt_embeds,
            num_inference_steps=num_inference_steps,
            **kwargs,
        ).images
        return images

class IPAdapterXL(IPAdapter):
    """SDXL"""

    def generate(
            self,
            pil_image,
            prompt=None,
            negative_prompt=None,
            scale=1.0,
            num_samples=4,
            seed=None,
            num_inference_steps=30,
            neg_content_emb=None,
            neg_content_prompt=None,
            neg_content_scale=1.0,
            **kwargs,
    ):
        self.set_scale(scale)

        num_prompts = 1 if isinstance(pil_image, Image.Image) else len(pil_image)

        if prompt is None:
            prompt = "best quality, high quality"
        if negative_prompt is None:
            negative_prompt = "monochrome, lowres, bad anatomy, worst quality, low quality"

        if not isinstance(prompt, List):
            prompt = [prompt] * num_prompts
        if not isinstance(negative_prompt, List):
            negative_prompt = [negative_prompt] * num_prompts

        if neg_content_emb is None:
            if neg_content_prompt is not None:
                with torch.inference_mode():
                    (
                        prompt_embeds_,  # torch.Size([1, 77, 2048])
                        negative_prompt_embeds_,
                        pooled_prompt_embeds_,  # torch.Size([1, 1280])
                        negative_pooled_prompt_embeds_,
                    ) = self.pipe.encode_prompt(
                        neg_content_prompt,
                        num_images_per_prompt=num_samples,
                        do_classifier_free_guidance=True,
                        negative_prompt=negative_prompt,
                    )
                    pooled_prompt_embeds_ *= neg_content_scale
            else:
                pooled_prompt_embeds_ = neg_content_emb
        else:
            pooled_prompt_embeds_ = None

        image_prompt_embeds, uncond_image_prompt_embeds = self.get_image_embeds(pil_image,
                                                                                content_prompt_embeds=pooled_prompt_embeds_)
        bs_embed, seq_len, _ = image_prompt_embeds.shape
        image_prompt_embeds = image_prompt_embeds.repeat(1, num_samples, 1)
        image_prompt_embeds = image_prompt_embeds.view(bs_embed * num_samples, seq_len, -1)
        uncond_image_prompt_embeds = uncond_image_prompt_embeds.repeat(1, num_samples, 1)
        uncond_image_prompt_embeds = uncond_image_prompt_embeds.view(bs_embed * num_samples, seq_len, -1)

        with torch.inference_mode():
            (
                prompt_embeds,
                negative_prompt_embeds,
                pooled_prompt_embeds,
                negative_pooled_prompt_embeds,
            ) = self.pipe.encode_prompt(
                prompt,
                num_images_per_prompt=num_samples,
                do_classifier_free_guidance=True,
                negative_prompt=negative_prompt,
            )
            prompt_embeds = torch.cat([prompt_embeds, image_prompt_embeds], dim=1)
            negative_prompt_embeds = torch.cat([negative_prompt_embeds, uncond_image_prompt_embeds], dim=1)

        self.generator = get_generator(seed, self.device)

        images = self.pipe(
            prompt_embeds=prompt_embeds,
            negative_prompt_embeds=negative_prompt_embeds,
            pooled_prompt_embeds=pooled_prompt_embeds,
            negative_pooled_prompt_embeds=negative_pooled_prompt_embeds,
            num_inference_steps=num_inference_steps,
            generator=self.generator,
            **kwargs,
        ).images

        return images