Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

supported_models.py 11 KB

You have to be logged in to leave a comment. Sign In
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
  1. import torch
  2. from . import model_base
  3. from . import utils
  4. from . import sd1_clip
  5. from . import sd2_clip
  6. from . import sdxl_clip
  7. from . import supported_models_base
  8. from . import latent_formats
  9. from . import diffusers_convert
  10. class SD15(supported_models_base.BASE):
  11. unet_config = {
  12. "context_dim": 768,
  13. "model_channels": 320,
  14. "use_linear_in_transformer": False,
  15. "adm_in_channels": None,
  16. "use_temporal_attention": False,
  17. }
  18. unet_extra_config = {
  19. "num_heads": 8,
  20. "num_head_channels": -1,
  21. }
  22. latent_format = latent_formats.SD15
  23. def process_clip_state_dict(self, state_dict):
  24. k = list(state_dict.keys())
  25. for x in k:
  26. if x.startswith("cond_stage_model.transformer.") and not x.startswith("cond_stage_model.transformer.text_model."):
  27. y = x.replace("cond_stage_model.transformer.", "cond_stage_model.transformer.text_model.")
  28. state_dict[y] = state_dict.pop(x)
  29. if 'cond_stage_model.transformer.text_model.embeddings.position_ids' in state_dict:
  30. ids = state_dict['cond_stage_model.transformer.text_model.embeddings.position_ids']
  31. if ids.dtype == torch.float32:
  32. state_dict['cond_stage_model.transformer.text_model.embeddings.position_ids'] = ids.round()
  33. replace_prefix = {}
  34. replace_prefix["cond_stage_model."] = "cond_stage_model.clip_l."
  35. state_dict = utils.state_dict_prefix_replace(state_dict, replace_prefix)
  36. return state_dict
  37. def process_clip_state_dict_for_saving(self, state_dict):
  38. replace_prefix = {"clip_l.": "cond_stage_model."}
  39. return utils.state_dict_prefix_replace(state_dict, replace_prefix)
  40. def clip_target(self):
  41. return supported_models_base.ClipTarget(sd1_clip.SD1Tokenizer, sd1_clip.SD1ClipModel)
  42. class SD20(supported_models_base.BASE):
  43. unet_config = {
  44. "context_dim": 1024,
  45. "model_channels": 320,
  46. "use_linear_in_transformer": True,
  47. "adm_in_channels": None,
  48. "use_temporal_attention": False,
  49. }
  50. latent_format = latent_formats.SD15
  51. def model_type(self, state_dict, prefix=""):
  52. if self.unet_config["in_channels"] == 4: #SD2.0 inpainting models are not v prediction
  53. k = "{}output_blocks.11.1.transformer_blocks.0.norm1.bias".format(prefix)
  54. out = state_dict[k]
  55. if torch.std(out, unbiased=False) > 0.09: # not sure how well this will actually work. I guess we will find out.
  56. return model_base.ModelType.V_PREDICTION
  57. return model_base.ModelType.EPS
  58. def process_clip_state_dict(self, state_dict):
  59. replace_prefix = {}
  60. replace_prefix["conditioner.embedders.0.model."] = "cond_stage_model.model." #SD2 in sgm format
  61. state_dict = utils.state_dict_prefix_replace(state_dict, replace_prefix)
  62. state_dict = utils.transformers_convert(state_dict, "cond_stage_model.model.", "cond_stage_model.clip_h.transformer.text_model.", 24)
  63. return state_dict
  64. def process_clip_state_dict_for_saving(self, state_dict):
  65. replace_prefix = {}
  66. replace_prefix["clip_h"] = "cond_stage_model.model"
  67. state_dict = utils.state_dict_prefix_replace(state_dict, replace_prefix)
  68. state_dict = diffusers_convert.convert_text_enc_state_dict_v20(state_dict)
  69. return state_dict
  70. def clip_target(self):
  71. return supported_models_base.ClipTarget(sd2_clip.SD2Tokenizer, sd2_clip.SD2ClipModel)
  72. class SD21UnclipL(SD20):
  73. unet_config = {
  74. "context_dim": 1024,
  75. "model_channels": 320,
  76. "use_linear_in_transformer": True,
  77. "adm_in_channels": 1536,
  78. "use_temporal_attention": False,
  79. }
  80. clip_vision_prefix = "embedder.model.visual."
  81. noise_aug_config = {"noise_schedule_config": {"timesteps": 1000, "beta_schedule": "squaredcos_cap_v2"}, "timestep_dim": 768}
  82. class SD21UnclipH(SD20):
  83. unet_config = {
  84. "context_dim": 1024,
  85. "model_channels": 320,
  86. "use_linear_in_transformer": True,
  87. "adm_in_channels": 2048,
  88. "use_temporal_attention": False,
  89. }
  90. clip_vision_prefix = "embedder.model.visual."
  91. noise_aug_config = {"noise_schedule_config": {"timesteps": 1000, "beta_schedule": "squaredcos_cap_v2"}, "timestep_dim": 1024}
  92. class SDXLRefiner(supported_models_base.BASE):
  93. unet_config = {
  94. "model_channels": 384,
  95. "use_linear_in_transformer": True,
  96. "context_dim": 1280,
  97. "adm_in_channels": 2560,
  98. "transformer_depth": [0, 0, 4, 4, 4, 4, 0, 0],
  99. "use_temporal_attention": False,
  100. }
  101. latent_format = latent_formats.SDXL
  102. def get_model(self, state_dict, prefix="", device=None):
  103. return model_base.SDXLRefiner(self, device=device)
  104. def process_clip_state_dict(self, state_dict):
  105. keys_to_replace = {}
  106. replace_prefix = {}
  107. state_dict = utils.transformers_convert(state_dict, "conditioner.embedders.0.model.", "cond_stage_model.clip_g.transformer.text_model.", 32)
  108. keys_to_replace["conditioner.embedders.0.model.text_projection"] = "cond_stage_model.clip_g.text_projection"
  109. keys_to_replace["conditioner.embedders.0.model.logit_scale"] = "cond_stage_model.clip_g.logit_scale"
  110. state_dict = utils.state_dict_key_replace(state_dict, keys_to_replace)
  111. return state_dict
  112. def process_clip_state_dict_for_saving(self, state_dict):
  113. replace_prefix = {}
  114. state_dict_g = diffusers_convert.convert_text_enc_state_dict_v20(state_dict, "clip_g")
  115. if "clip_g.transformer.text_model.embeddings.position_ids" in state_dict_g:
  116. state_dict_g.pop("clip_g.transformer.text_model.embeddings.position_ids")
  117. replace_prefix["clip_g"] = "conditioner.embedders.0.model"
  118. state_dict_g = utils.state_dict_prefix_replace(state_dict_g, replace_prefix)
  119. return state_dict_g
  120. def clip_target(self):
  121. return supported_models_base.ClipTarget(sdxl_clip.SDXLTokenizer, sdxl_clip.SDXLRefinerClipModel)
  122. class SDXL(supported_models_base.BASE):
  123. unet_config = {
  124. "model_channels": 320,
  125. "use_linear_in_transformer": True,
  126. "transformer_depth": [0, 0, 2, 2, 10, 10],
  127. "context_dim": 2048,
  128. "adm_in_channels": 2816,
  129. "use_temporal_attention": False,
  130. }
  131. latent_format = latent_formats.SDXL
  132. def model_type(self, state_dict, prefix=""):
  133. if "v_pred" in state_dict:
  134. return model_base.ModelType.V_PREDICTION
  135. else:
  136. return model_base.ModelType.EPS
  137. def get_model(self, state_dict, prefix="", device=None):
  138. out = model_base.SDXL(self, model_type=self.model_type(state_dict, prefix), device=device)
  139. if self.inpaint_model():
  140. out.set_inpaint()
  141. return out
  142. def process_clip_state_dict(self, state_dict):
  143. keys_to_replace = {}
  144. replace_prefix = {}
  145. replace_prefix["conditioner.embedders.0.transformer.text_model"] = "cond_stage_model.clip_l.transformer.text_model"
  146. state_dict = utils.transformers_convert(state_dict, "conditioner.embedders.1.model.", "cond_stage_model.clip_g.transformer.text_model.", 32)
  147. keys_to_replace["conditioner.embedders.1.model.text_projection"] = "cond_stage_model.clip_g.text_projection"
  148. keys_to_replace["conditioner.embedders.1.model.text_projection.weight"] = "cond_stage_model.clip_g.text_projection"
  149. keys_to_replace["conditioner.embedders.1.model.logit_scale"] = "cond_stage_model.clip_g.logit_scale"
  150. state_dict = utils.state_dict_prefix_replace(state_dict, replace_prefix)
  151. state_dict = utils.state_dict_key_replace(state_dict, keys_to_replace)
  152. return state_dict
  153. def process_clip_state_dict_for_saving(self, state_dict):
  154. replace_prefix = {}
  155. keys_to_replace = {}
  156. state_dict_g = diffusers_convert.convert_text_enc_state_dict_v20(state_dict, "clip_g")
  157. if "clip_g.transformer.text_model.embeddings.position_ids" in state_dict_g:
  158. state_dict_g.pop("clip_g.transformer.text_model.embeddings.position_ids")
  159. for k in state_dict:
  160. if k.startswith("clip_l"):
  161. state_dict_g[k] = state_dict[k]
  162. replace_prefix["clip_g"] = "conditioner.embedders.1.model"
  163. replace_prefix["clip_l"] = "conditioner.embedders.0"
  164. state_dict_g = utils.state_dict_prefix_replace(state_dict_g, replace_prefix)
  165. return state_dict_g
  166. def clip_target(self):
  167. return supported_models_base.ClipTarget(sdxl_clip.SDXLTokenizer, sdxl_clip.SDXLClipModel)
  168. class SSD1B(SDXL):
  169. unet_config = {
  170. "model_channels": 320,
  171. "use_linear_in_transformer": True,
  172. "transformer_depth": [0, 0, 2, 2, 4, 4],
  173. "context_dim": 2048,
  174. "adm_in_channels": 2816,
  175. "use_temporal_attention": False,
  176. }
  177. class Segmind_Vega(SDXL):
  178. unet_config = {
  179. "model_channels": 320,
  180. "use_linear_in_transformer": True,
  181. "transformer_depth": [0, 0, 1, 1, 2, 2],
  182. "context_dim": 2048,
  183. "adm_in_channels": 2816,
  184. "use_temporal_attention": False,
  185. }
  186. class SVD_img2vid(supported_models_base.BASE):
  187. unet_config = {
  188. "model_channels": 320,
  189. "in_channels": 8,
  190. "use_linear_in_transformer": True,
  191. "transformer_depth": [1, 1, 1, 1, 1, 1, 0, 0],
  192. "context_dim": 1024,
  193. "adm_in_channels": 768,
  194. "use_temporal_attention": True,
  195. "use_temporal_resblock": True
  196. }
  197. clip_vision_prefix = "conditioner.embedders.0.open_clip.model.visual."
  198. latent_format = latent_formats.SD15
  199. sampling_settings = {"sigma_max": 700.0, "sigma_min": 0.002}
  200. def get_model(self, state_dict, prefix="", device=None):
  201. out = model_base.SVD_img2vid(self, device=device)
  202. return out
  203. def clip_target(self):
  204. return None
  205. class Stable_Zero123(supported_models_base.BASE):
  206. unet_config = {
  207. "context_dim": 768,
  208. "model_channels": 320,
  209. "use_linear_in_transformer": False,
  210. "adm_in_channels": None,
  211. "use_temporal_attention": False,
  212. "in_channels": 8,
  213. }
  214. unet_extra_config = {
  215. "num_heads": 8,
  216. "num_head_channels": -1,
  217. }
  218. clip_vision_prefix = "cond_stage_model.model.visual."
  219. latent_format = latent_formats.SD15
  220. def get_model(self, state_dict, prefix="", device=None):
  221. out = model_base.Stable_Zero123(self, device=device, cc_projection_weight=state_dict["cc_projection.weight"], cc_projection_bias=state_dict["cc_projection.bias"])
  222. return out
  223. def clip_target(self):
  224. return None
  225. class SD_X4Upscaler(SD20):
  226. unet_config = {
  227. "context_dim": 1024,
  228. "model_channels": 256,
  229. 'in_channels': 7,
  230. "use_linear_in_transformer": True,
  231. "adm_in_channels": None,
  232. "use_temporal_attention": False,
  233. }
  234. unet_extra_config = {
  235. "disable_self_attentions": [True, True, True, False],
  236. "num_classes": 1000,
  237. "num_heads": 8,
  238. "num_head_channels": -1,
  239. }
  240. latent_format = latent_formats.SD_X4
  241. sampling_settings = {
  242. "linear_start": 0.0001,
  243. "linear_end": 0.02,
  244. }
  245. def get_model(self, state_dict, prefix="", device=None):
  246. out = model_base.SD_X4Upscaler(self, device=device)
  247. return out
  248. models = [Stable_Zero123, SD15, SD20, SD21UnclipL, SD21UnclipH, SDXLRefiner, SDXL, SSD1B, Segmind_Vega, SD_X4Upscaler]
  249. models += [SVD_img2vid]
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...