supported_models.py 13.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
import torch
from . import model_base
from . import utils

from . import sd1_clip
from . import sd2_clip
from . import sdxl_clip

from . import supported_models_base
10
from . import latent_formats
11

12
13
from . import diffusers_convert

14
15
16
17
18
19
class SD15(supported_models_base.BASE):
    unet_config = {
        "context_dim": 768,
        "model_channels": 320,
        "use_linear_in_transformer": False,
        "adm_in_channels": None,
comfyanonymous's avatar
comfyanonymous committed
20
        "use_temporal_attention": False,
21
22
23
24
25
26
27
    }

    unet_extra_config = {
        "num_heads": 8,
        "num_head_channels": -1,
    }

28
    latent_format = latent_formats.SD15
29
30
31
32
33
34
35
36
37
38
39
40
41

    def process_clip_state_dict(self, state_dict):
        k = list(state_dict.keys())
        for x in k:
            if x.startswith("cond_stage_model.transformer.") and not x.startswith("cond_stage_model.transformer.text_model."):
                y = x.replace("cond_stage_model.transformer.", "cond_stage_model.transformer.text_model.")
                state_dict[y] = state_dict.pop(x)

        if 'cond_stage_model.transformer.text_model.embeddings.position_ids' in state_dict:
            ids = state_dict['cond_stage_model.transformer.text_model.embeddings.position_ids']
            if ids.dtype == torch.float32:
                state_dict['cond_stage_model.transformer.text_model.embeddings.position_ids'] = ids.round()

42
        replace_prefix = {}
43
44
        replace_prefix["cond_stage_model."] = "clip_l."
        state_dict = utils.state_dict_prefix_replace(state_dict, replace_prefix, filter_keys=True)
45
46
        return state_dict

47
48
49
50
    def process_clip_state_dict_for_saving(self, state_dict):
        replace_prefix = {"clip_l.": "cond_stage_model."}
        return utils.state_dict_prefix_replace(state_dict, replace_prefix)

51
52
53
54
55
56
57
58
59
    def clip_target(self):
        return supported_models_base.ClipTarget(sd1_clip.SD1Tokenizer, sd1_clip.SD1ClipModel)

class SD20(supported_models_base.BASE):
    unet_config = {
        "context_dim": 1024,
        "model_channels": 320,
        "use_linear_in_transformer": True,
        "adm_in_channels": None,
comfyanonymous's avatar
comfyanonymous committed
60
        "use_temporal_attention": False,
61
62
    }

63
    latent_format = latent_formats.SD15
64

65
    def model_type(self, state_dict, prefix=""):
66
        if self.unet_config["in_channels"] == 4: #SD2.0 inpainting models are not v prediction
67
            k = "{}output_blocks.11.1.transformer_blocks.0.norm1.bias".format(prefix)
68
69
            out = state_dict[k]
            if torch.std(out, unbiased=False) > 0.09: # not sure how well this will actually work. I guess we will find out.
70
71
                return model_base.ModelType.V_PREDICTION
        return model_base.ModelType.EPS
72
73

    def process_clip_state_dict(self, state_dict):
74
        replace_prefix = {}
75
76
77
        replace_prefix["conditioner.embedders.0.model."] = "clip_h." #SD2 in sgm format
        replace_prefix["cond_stage_model.model."] = "clip_h."
        state_dict = utils.state_dict_prefix_replace(state_dict, replace_prefix, filter_keys=True)
78
        state_dict = utils.clip_text_transformers_convert(state_dict, "clip_h.", "clip_h.transformer.")
79
80
        return state_dict

81
82
    def process_clip_state_dict_for_saving(self, state_dict):
        replace_prefix = {}
83
        replace_prefix["clip_h"] = "cond_stage_model.model"
84
        state_dict = utils.state_dict_prefix_replace(state_dict, replace_prefix)
85
86
87
        state_dict = diffusers_convert.convert_text_enc_state_dict_v20(state_dict)
        return state_dict

88
89
90
91
92
93
94
95
96
    def clip_target(self):
        return supported_models_base.ClipTarget(sd2_clip.SD2Tokenizer, sd2_clip.SD2ClipModel)

class SD21UnclipL(SD20):
    unet_config = {
        "context_dim": 1024,
        "model_channels": 320,
        "use_linear_in_transformer": True,
        "adm_in_channels": 1536,
comfyanonymous's avatar
comfyanonymous committed
97
        "use_temporal_attention": False,
98
99
100
101
102
103
104
105
106
107
108
109
    }

    clip_vision_prefix = "embedder.model.visual."
    noise_aug_config = {"noise_schedule_config": {"timesteps": 1000, "beta_schedule": "squaredcos_cap_v2"}, "timestep_dim": 768}


class SD21UnclipH(SD20):
    unet_config = {
        "context_dim": 1024,
        "model_channels": 320,
        "use_linear_in_transformer": True,
        "adm_in_channels": 2048,
comfyanonymous's avatar
comfyanonymous committed
110
        "use_temporal_attention": False,
111
112
113
114
115
116
117
118
119
120
121
    }

    clip_vision_prefix = "embedder.model.visual."
    noise_aug_config = {"noise_schedule_config": {"timesteps": 1000, "beta_schedule": "squaredcos_cap_v2"}, "timestep_dim": 1024}

class SDXLRefiner(supported_models_base.BASE):
    unet_config = {
        "model_channels": 384,
        "use_linear_in_transformer": True,
        "context_dim": 1280,
        "adm_in_channels": 2560,
122
        "transformer_depth": [0, 0, 4, 4, 4, 4, 0, 0],
comfyanonymous's avatar
comfyanonymous committed
123
        "use_temporal_attention": False,
124
125
    }

126
    latent_format = latent_formats.SDXL
127

128
129
    def get_model(self, state_dict, prefix="", device=None):
        return model_base.SDXLRefiner(self, device=device)
130
131
132
133

    def process_clip_state_dict(self, state_dict):
        keys_to_replace = {}
        replace_prefix = {}
134
135
        replace_prefix["conditioner.embedders.0.model."] = "clip_g."
        state_dict = utils.state_dict_prefix_replace(state_dict, replace_prefix, filter_keys=True)
136

137
        state_dict = utils.clip_text_transformers_convert(state_dict, "clip_g.", "clip_g.transformer.")
138
        state_dict = utils.state_dict_key_replace(state_dict, keys_to_replace)
139
140
        return state_dict

141
142
143
    def process_clip_state_dict_for_saving(self, state_dict):
        replace_prefix = {}
        state_dict_g = diffusers_convert.convert_text_enc_state_dict_v20(state_dict, "clip_g")
144
145
        if "clip_g.transformer.text_model.embeddings.position_ids" in state_dict_g:
            state_dict_g.pop("clip_g.transformer.text_model.embeddings.position_ids")
146
        replace_prefix["clip_g"] = "conditioner.embedders.0.model"
147
        state_dict_g = utils.state_dict_prefix_replace(state_dict_g, replace_prefix)
148
149
        return state_dict_g

150
151
152
153
154
155
156
    def clip_target(self):
        return supported_models_base.ClipTarget(sdxl_clip.SDXLTokenizer, sdxl_clip.SDXLRefinerClipModel)

class SDXL(supported_models_base.BASE):
    unet_config = {
        "model_channels": 320,
        "use_linear_in_transformer": True,
157
        "transformer_depth": [0, 0, 2, 2, 10, 10],
158
        "context_dim": 2048,
comfyanonymous's avatar
comfyanonymous committed
159
160
        "adm_in_channels": 2816,
        "use_temporal_attention": False,
161
162
    }

163
    latent_format = latent_formats.SDXL
164

165
166
167
168
169
170
    def model_type(self, state_dict, prefix=""):
        if "v_pred" in state_dict:
            return model_base.ModelType.V_PREDICTION
        else:
            return model_base.ModelType.EPS

171
    def get_model(self, state_dict, prefix="", device=None):
comfyanonymous's avatar
comfyanonymous committed
172
173
174
175
        out = model_base.SDXL(self, model_type=self.model_type(state_dict, prefix), device=device)
        if self.inpaint_model():
            out.set_inpaint()
        return out
176
177
178
179
180

    def process_clip_state_dict(self, state_dict):
        keys_to_replace = {}
        replace_prefix = {}

181
182
183
184
        replace_prefix["conditioner.embedders.0.transformer.text_model"] = "clip_l.transformer.text_model"
        replace_prefix["conditioner.embedders.1.model."] = "clip_g."
        state_dict = utils.state_dict_prefix_replace(state_dict, replace_prefix, filter_keys=True)

185
        state_dict = utils.state_dict_key_replace(state_dict, keys_to_replace)
186
        state_dict = utils.clip_text_transformers_convert(state_dict, "clip_g.", "clip_g.transformer.")
187
188
        return state_dict

189
190
191
192
193
194
195
196
    def process_clip_state_dict_for_saving(self, state_dict):
        replace_prefix = {}
        keys_to_replace = {}
        state_dict_g = diffusers_convert.convert_text_enc_state_dict_v20(state_dict, "clip_g")
        for k in state_dict:
            if k.startswith("clip_l"):
                state_dict_g[k] = state_dict[k]

197
198
199
200
201
202
        state_dict_g["clip_l.transformer.text_model.embeddings.position_ids"] = torch.arange(77).expand((1, -1))
        pop_keys = ["clip_l.transformer.text_projection.weight", "clip_l.logit_scale"]
        for p in pop_keys:
            if p in state_dict_g:
                state_dict_g.pop(p)

203
204
        replace_prefix["clip_g"] = "conditioner.embedders.1.model"
        replace_prefix["clip_l"] = "conditioner.embedders.0"
205
        state_dict_g = utils.state_dict_prefix_replace(state_dict_g, replace_prefix)
206
207
        return state_dict_g

208
209
210
    def clip_target(self):
        return supported_models_base.ClipTarget(sdxl_clip.SDXLTokenizer, sdxl_clip.SDXLClipModel)

211
212
213
214
215
216
class SSD1B(SDXL):
    unet_config = {
        "model_channels": 320,
        "use_linear_in_transformer": True,
        "transformer_depth": [0, 0, 2, 2, 4, 4],
        "context_dim": 2048,
comfyanonymous's avatar
comfyanonymous committed
217
218
        "adm_in_channels": 2816,
        "use_temporal_attention": False,
219
220
    }

comfyanonymous's avatar
comfyanonymous committed
221
222
223
224
225
226
227
228
229
230
class Segmind_Vega(SDXL):
    unet_config = {
        "model_channels": 320,
        "use_linear_in_transformer": True,
        "transformer_depth": [0, 0, 1, 1, 2, 2],
        "context_dim": 2048,
        "adm_in_channels": 2816,
        "use_temporal_attention": False,
    }

comfyanonymous's avatar
comfyanonymous committed
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
class SVD_img2vid(supported_models_base.BASE):
    unet_config = {
        "model_channels": 320,
        "in_channels": 8,
        "use_linear_in_transformer": True,
        "transformer_depth": [1, 1, 1, 1, 1, 1, 0, 0],
        "context_dim": 1024,
        "adm_in_channels": 768,
        "use_temporal_attention": True,
        "use_temporal_resblock": True
    }

    clip_vision_prefix = "conditioner.embedders.0.open_clip.model.visual."

    latent_format = latent_formats.SD15

    sampling_settings = {"sigma_max": 700.0, "sigma_min": 0.002}

    def get_model(self, state_dict, prefix="", device=None):
        out = model_base.SVD_img2vid(self, device=device)
        return out

    def clip_target(self):
        return None
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
class Stable_Zero123(supported_models_base.BASE):
    unet_config = {
        "context_dim": 768,
        "model_channels": 320,
        "use_linear_in_transformer": False,
        "adm_in_channels": None,
        "use_temporal_attention": False,
        "in_channels": 8,
    }

    unet_extra_config = {
        "num_heads": 8,
        "num_head_channels": -1,
    }

    clip_vision_prefix = "cond_stage_model.model.visual."

    latent_format = latent_formats.SD15

    def get_model(self, state_dict, prefix="", device=None):
        out = model_base.Stable_Zero123(self, device=device, cc_projection_weight=state_dict["cc_projection.weight"], cc_projection_bias=state_dict["cc_projection.bias"])
        return out

    def clip_target(self):
        return None

282
283
284
285
286
287
288
289
290
291
292
293
class SD_X4Upscaler(SD20):
    unet_config = {
        "context_dim": 1024,
        "model_channels": 256,
        'in_channels': 7,
        "use_linear_in_transformer": True,
        "adm_in_channels": None,
        "use_temporal_attention": False,
    }

    unet_extra_config = {
        "disable_self_attentions": [True, True, True, False],
294
        "num_classes": 1000,
295
296
297
298
299
300
301
302
303
304
305
306
307
308
        "num_heads": 8,
        "num_head_channels": -1,
    }

    latent_format = latent_formats.SD_X4

    sampling_settings = {
        "linear_start": 0.0001,
        "linear_end": 0.02,
    }

    def get_model(self, state_dict, prefix="", device=None):
        out = model_base.SD_X4Upscaler(self, device=device)
        return out
309

comfyanonymous's avatar
comfyanonymous committed
310
311
312
313
314
315
316
317
318
319
class Stable_Cascade_C(supported_models_base.BASE):
    unet_config = {
        "stable_cascade_stage": 'c',
    }

    unet_extra_config = {}

    latent_format = latent_formats.SC_Prior
    supported_inference_dtypes = [torch.bfloat16, torch.float32]

320
321
322
323
    sampling_settings = {
        "shift": 2.0,
    }

324
325
326
327
    vae_key_prefix = ["vae."]
    text_encoder_key_prefix = ["text_encoder."]
    clip_vision_prefix = "clip_l_vision."

comfyanonymous's avatar
comfyanonymous committed
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
    def process_unet_state_dict(self, state_dict):
        key_list = list(state_dict.keys())
        for y in ["weight", "bias"]:
            suffix = "in_proj_{}".format(y)
            keys = filter(lambda a: a.endswith(suffix), key_list)
            for k_from in keys:
                weights = state_dict.pop(k_from)
                prefix = k_from[:-(len(suffix) + 1)]
                shape_from = weights.shape[0] // 3
                for x in range(3):
                    p = ["to_q", "to_k", "to_v"]
                    k_to = "{}.{}.{}".format(prefix, p[x], y)
                    state_dict[k_to] = weights[shape_from*x:shape_from*(x + 1)]
        return state_dict

343
344
345
346
347
348
    def process_clip_state_dict(self, state_dict):
        state_dict = utils.state_dict_prefix_replace(state_dict, {k: "" for k in self.text_encoder_key_prefix}, filter_keys=True)
        if "clip_g.text_projection" in state_dict:
            state_dict["clip_g.transformer.text_projection.weight"] = state_dict.pop("clip_g.text_projection").transpose(0, 1)
        return state_dict

comfyanonymous's avatar
comfyanonymous committed
349
350
351
352
353
    def get_model(self, state_dict, prefix="", device=None):
        out = model_base.StableCascade_C(self, device=device)
        return out

    def clip_target(self):
354
        return supported_models_base.ClipTarget(sdxl_clip.StableCascadeTokenizer, sdxl_clip.StableCascadeClipModel)
comfyanonymous's avatar
comfyanonymous committed
355

comfyanonymous's avatar
comfyanonymous committed
356
357
358
359
360
361
362
363
364
365
class Stable_Cascade_B(Stable_Cascade_C):
    unet_config = {
        "stable_cascade_stage": 'b',
    }

    unet_extra_config = {}

    latent_format = latent_formats.SC_B
    supported_inference_dtypes = [torch.float16, torch.bfloat16, torch.float32]

366
367
368
369
    sampling_settings = {
        "shift": 1.0,
    }

370
371
    clip_vision_prefix = None

comfyanonymous's avatar
comfyanonymous committed
372
373
374
375
    def get_model(self, state_dict, prefix="", device=None):
        out = model_base.StableCascade_B(self, device=device)
        return out

comfyanonymous's avatar
comfyanonymous committed
376

comfyanonymous's avatar
comfyanonymous committed
377
models = [Stable_Zero123, SD15, SD20, SD21UnclipL, SD21UnclipH, SDXLRefiner, SDXL, SSD1B, Segmind_Vega, SD_X4Upscaler, Stable_Cascade_C, Stable_Cascade_B]
comfyanonymous's avatar
comfyanonymous committed
378
models += [SVD_img2vid]