-
Notifications
You must be signed in to change notification settings - Fork 3
/
mz_cogvideox_core.py
445 lines (388 loc) · 14.2 KB
/
mz_cogvideox_core.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
from contextlib import nullcontext
import os
import torch
import comfy.supported_models
import comfy.model_base
import comfy.ldm.flux.model
import comfy.model_patcher
import comfy.model_management
import folder_paths
import safetensors.torch
from .pipeline_cogvideox import CogVideoXPipeline
from .cogvideox_fun.transformer_3d import CogVideoXTransformer3DModel as CogVideoXTransformer3DModelFun
from .cogvideox_fun.fun_pab_transformer_3d import CogVideoXTransformer3DModel as CogVideoXTransformer3DModelFunPAB
from .cogvideox_fun.autoencoder_magvit import AutoencoderKLCogVideoX as AutoencoderKLCogVideoXFun
from .cogvideox_fun.utils import get_image_to_video_latent, ASPECT_RATIO_512, get_closest_ratio, to_pil
from .cogvideox_fun.pipeline_cogvideox_inpaint import CogVideoX_Fun_Pipeline_Inpaint
# from diffusers.models import AutoencoderKLCogVideoX, CogVideoXTransformer3DModel
from diffusers.models import AutoencoderKLCogVideoX
from .custom_cogvideox_transformer_3d import CogVideoXTransformer3DModel
from diffusers.schedulers import CogVideoXDDIMScheduler
cogVideoXVaeConfig = {
"act_fn": "silu",
"block_out_channels": [
128,
256,
256,
512
],
"down_block_types": [
"CogVideoXDownBlock3D",
"CogVideoXDownBlock3D",
"CogVideoXDownBlock3D",
"CogVideoXDownBlock3D"
],
"force_upcast": True,
"in_channels": 3,
"latent_channels": 16,
"latents_mean": None,
"latents_std": None,
"layers_per_block": 3,
"mid_block_add_attention": True,
"norm_eps": 1e-06,
"norm_num_groups": 32,
"out_channels": 3,
"sample_size": 256,
"scaling_factor": 1.15258426,
"shift_factor": None,
"temporal_compression_ratio": 4,
"up_block_types": [
"CogVideoXUpBlock3D",
"CogVideoXUpBlock3D",
"CogVideoXUpBlock3D",
"CogVideoXUpBlock3D"
],
"use_post_quant_conv": False,
"use_quant_conv": False
}
cogVideoXVaeConfig5B = {
"act_fn": "silu",
"block_out_channels": [
128,
256,
256,
512
],
"down_block_types": [
"CogVideoXDownBlock3D",
"CogVideoXDownBlock3D",
"CogVideoXDownBlock3D",
"CogVideoXDownBlock3D"
],
"force_upcast": True,
"in_channels": 3,
"latent_channels": 16,
"latents_mean": None,
"latents_std": None,
"layers_per_block": 3,
"norm_eps": 1e-06,
"norm_num_groups": 32,
"out_channels": 3,
"sample_height": 480,
"sample_width": 720,
"scaling_factor": 0.7,
"shift_factor": None,
"temporal_compression_ratio": 4,
"up_block_types": [
"CogVideoXUpBlock3D",
"CogVideoXUpBlock3D",
"CogVideoXUpBlock3D",
"CogVideoXUpBlock3D"
],
"use_post_quant_conv": False,
"use_quant_conv": False
}
cogVideoXTransformerConfig = {
"activation_fn": "gelu-approximate",
"attention_bias": True,
"attention_head_dim": 64,
"dropout": 0.0,
"flip_sin_to_cos": True,
"freq_shift": 0,
"in_channels": 16,
"max_text_seq_length": 226,
"norm_elementwise_affine": True,
"norm_eps": 1e-05,
"num_attention_heads": 30,
"num_layers": 30,
"out_channels": 16,
"patch_size": 2,
"sample_frames": 49,
"sample_height": 60,
"sample_width": 90,
"spatial_interpolation_scale": 1.875,
"temporal_compression_ratio": 4,
"temporal_interpolation_scale": 1.0,
"text_embed_dim": 4096,
"time_embed_dim": 512,
"timestep_activation_fn": "silu",
"use_rotary_positional_embeddings": False
}
cogVideoXTransformerConfig5B = {
"activation_fn": "gelu-approximate",
"attention_bias": True,
"attention_head_dim": 64,
"dropout": 0.0,
"flip_sin_to_cos": True,
"freq_shift": 0,
"in_channels": 16,
"max_text_seq_length": 226,
"norm_elementwise_affine": True,
"norm_eps": 1e-05,
"num_attention_heads": 48,
"num_layers": 42,
"out_channels": 16,
"patch_size": 2,
"sample_frames": 49,
"sample_height": 60,
"sample_width": 90,
"spatial_interpolation_scale": 1.875,
"temporal_compression_ratio": 4,
"temporal_interpolation_scale": 1.0,
"text_embed_dim": 4096,
"time_embed_dim": 512,
"timestep_activation_fn": "silu",
"use_rotary_positional_embeddings": True
}
cogVideoXDDIMSchedulerConfig = {
"beta_end": 0.012,
"beta_schedule": "scaled_linear",
"beta_start": 0.00085,
"clip_sample": False,
"clip_sample_range": 1.0,
"num_train_timesteps": 1000,
"prediction_type": "v_prediction",
"rescale_betas_zero_snr": True,
"sample_max_value": 1.0,
"set_alpha_to_one": True,
"snr_shift_scale": 3.0,
"steps_offset": 0,
"timestep_spacing": "linspace",
"trained_betas": None,
}
cogVideoXDDIMSchedulerConfig5B = {
"beta_end": 0.012,
"beta_schedule": "scaled_linear",
"beta_start": 0.00085,
"clip_sample": False,
"clip_sample_range": 1.0,
"num_train_timesteps": 1000,
"prediction_type": "v_prediction",
"rescale_betas_zero_snr": True,
"sample_max_value": 1.0,
"set_alpha_to_one": True,
"snr_shift_scale": 1.0,
"steps_offset": 0,
"timestep_spacing": "trailing",
"trained_betas": None,
}
def gen_fp8_linear_forward(cast_dtype):
def fp8_linear_forward(cls, x):
original_dtype = cls.weight.dtype
if original_dtype == torch.float8_e4m3fn or original_dtype == torch.float8_e5m2:
if len(x.shape) == 3:
with torch.no_grad():
if original_dtype == torch.float8_e4m3fn:
inn = x.reshape(-1, x.shape[2]).to(torch.float8_e5m2)
else:
inn = x.reshape(-1, x.shape[2]).to(torch.float8_e4m3fn)
w = cls.weight.t()
scale_weight = torch.ones(
(1), device=x.device, dtype=torch.float32)
scale_input = scale_weight
bias = cls.bias.to(
cast_dtype) if cls.bias is not None else None
if bias is not None:
o = torch._scaled_mm(
inn, w, out_dtype=cast_dtype, bias=bias, scale_a=scale_input, scale_b=scale_weight)
else:
o = torch._scaled_mm(
inn, w, out_dtype=cast_dtype, scale_a=scale_input, scale_b=scale_weight)
if isinstance(o, tuple):
o = o[0]
return o.reshape((-1, x.shape[1], cls.weight.shape[0]))
else:
cls.to(cast_dtype)
out = cls.original_forward(x.to(
cast_dtype
))
cls.to(original_dtype)
return out
else:
return cls.original_forward(x)
return fp8_linear_forward
import torch.nn as nn
from types import MethodType
def convert_fp8_linear(module, dtype, cast_dtype):
for name, module in module.named_modules():
if isinstance(module, nn.Linear):
module.to(dtype)
original_forward = module.forward
setattr(module, "original_forward", original_forward)
setattr(module, "forward", MethodType(
gen_fp8_linear_forward(cast_dtype), module))
def MZ_CogVideoXLoader_call(args={}):
unet_name = args.get("unet_name")
unet_path = folder_paths.get_full_path("unet", unet_name)
enable_sequential_cpu_offload = args.get(
"enable_sequential_cpu_offload", False)
device = comfy.model_management.get_torch_device()
comfy.model_management.soft_empty_cache()
unet_sd = safetensors.torch.load_file(unet_path)
unet_sd_keys = list(unet_sd.keys())
transformer_type = ""
if unet_sd["patch_embed.proj.weight"].shape == (3072, 33, 2, 2):
transformer_type = "fun_5b"
elif unet_sd["patch_embed.proj.weight"].shape == (3072, 16, 2, 2):
transformer_type = "5b"
elif unet_sd["patch_embed.proj.weight"].shape == (1920, 33, 2, 2):
transformer_type = "fun_2b"
elif unet_sd["patch_embed.proj.weight"].shape == (1920, 16, 2, 2):
transformer_type = "2b"
elif unet_sd["patch_embed.proj.weight"].shape == (3072, 32, 2, 2):
transformer_type = "i2v_5b"
else:
raise Exception("This model is not supported")
is_GGUF = False
if len([k for k in unet_sd_keys if "Q4_0_qweight" in k]) > 0:
is_GGUF = True
print(f"transformer type: {transformer_type}")
print(f"GGUF: {is_GGUF}")
transformer_config = None
vae_config = None
scheduler_config = None
base_path = None
if transformer_type.endswith("5b"):
transformer_config = cogVideoXTransformerConfig5B
vae_config = cogVideoXVaeConfig5B
scheduler_config = cogVideoXDDIMSchedulerConfig5B
base_path = os.path.join(
os.path.dirname(__file__),
"configs5b",
)
if transformer_type == "fun_5b":
transformer_config["in_channels"] = 33
base_path = os.path.join(
os.path.dirname(__file__),
"configs5b-Fun",
)
elif transformer_type == "i2v_5b":
transformer_config["in_channels"] = 32
transformer_config["use_learned_positional_embeddings"] = True
base_path = os.path.join(
os.path.dirname(__file__),
"configs5b-i2v",
)
if transformer_type.endswith("2b"):
transformer_config = cogVideoXTransformerConfig
vae_config = cogVideoXVaeConfig
scheduler_config = cogVideoXDDIMSchedulerConfig
base_path = os.path.join(
os.path.dirname(__file__),
"configs",
)
if transformer_type == "fun_2b":
transformer_config["in_channels"] = 33
base_path = os.path.join(
os.path.dirname(__file__),
"configs2b-Fun",
)
weight_dtype = None
manual_cast_dtype = None
_weight_dtype = args.get("weight_dtype")
if _weight_dtype == "fp8_e4m3fn":
weight_dtype = torch.float8_e4m3fn
manual_cast_dtype = torch.float16
elif _weight_dtype == "fp8_e5m2":
weight_dtype = torch.float8_e5m2
manual_cast_dtype = torch.bfloat16
elif _weight_dtype == "fp16":
weight_dtype = torch.float16
manual_cast_dtype = torch.float16
elif _weight_dtype == "bf16":
weight_dtype = torch.bfloat16
manual_cast_dtype = torch.bfloat16
else:
weight_dtype = torch.float32
manual_cast_dtype = torch.float32
print(
f"model weight dtype: {weight_dtype} manual cast dtype: {manual_cast_dtype}")
pab_config = args.get("pab_config", None)
transformer = None
CogVideoXTransformer3DModelImp = None
if pab_config is not None:
CogVideoXTransformer3DModelImp = CogVideoXTransformer3DModelFunPAB
elif transformer_type.startswith("fun"):
CogVideoXTransformer3DModelImp = CogVideoXTransformer3DModelFun
else:
CogVideoXTransformer3DModelImp = CogVideoXTransformer3DModel
from . import mz_gguf_loader
import importlib
importlib.reload(mz_gguf_loader)
with mz_gguf_loader.quantize_lazy_load() if is_GGUF else nullcontext():
transformer = CogVideoXTransformer3DModelImp.from_config(
transformer_config)
transformer.to(weight_dtype)
block_edit = args.get("block_edit", None)
if block_edit is not None:
transformer = remove_specific_blocks(transformer, block_edit)
if is_GGUF:
transformer = mz_gguf_loader.quantize_load_state_dict(
transformer, unet_sd, device="cpu", cast_dtype=manual_cast_dtype)
transformer.to(device)
else:
transformer.load_state_dict(unet_sd)
if weight_dtype == torch.float8_e4m3fn or weight_dtype == torch.float8_e5m2:
fp8_fast_mode = args.get("fp8_fast_mode", False)
if fp8_fast_mode:
print("convert to fp8 linear")
convert_fp8_linear(transformer, weight_dtype, manual_cast_dtype)
if transformer_type.endswith("2b") or transformer_type == "i2v_5b":
if hasattr(transformer, "pos_embedding"):
transformer.pos_embedding = transformer.pos_embedding.to(
manual_cast_dtype)
if hasattr(transformer, "patch_embed") and hasattr(transformer.patch_embed, "pos_embedding"):
transformer.patch_embed.pos_embedding = transformer.patch_embed.pos_embedding.to(
manual_cast_dtype)
transformer.to(device)
vae_name = args.get("vae_name")
vae_path = folder_paths.get_full_path("vae", vae_name)
if transformer_type.startswith("fun"):
vae = AutoencoderKLCogVideoXFun.from_config(vae_config)
else:
vae = AutoencoderKLCogVideoX.from_config(vae_config)
vae_sd = safetensors.torch.load_file(vae_path)
vae.load_state_dict(vae_sd)
vae.to(device)
enable_vae_encode_tiling = args.get("enable_vae_encode_tiling", False)
if enable_vae_encode_tiling:
from .mz_enable_vae_encode_tiling import enable_vae_encode_tiling
enable_vae_encode_tiling(vae)
# from .mz_dyn_cpu_offload import dyn_cpu_offload_model_vae
# vae = dyn_cpu_offload_model_vae(vae)
scheduler = CogVideoXDDIMScheduler.from_config(
scheduler_config)
if transformer_type.startswith("fun"):
pipe = CogVideoX_Fun_Pipeline_Inpaint(
vae, transformer, scheduler, pab_config=pab_config)
else:
pipe = CogVideoXPipeline(
vae, transformer, scheduler, pab_config=pab_config)
if enable_sequential_cpu_offload:
pipe.enable_sequential_cpu_offload()
pipeline = {
"pipe": pipe,
"dtype": manual_cast_dtype,
"base_path": base_path,
"onediff": False,
"cpu_offloading": enable_sequential_cpu_offload,
"scheduler_config": scheduler_config,
}
return (pipeline, )
def remove_specific_blocks(model, block_indices_to_remove):
import torch.nn as nn
transformer_blocks = model.transformer_blocks
new_blocks = [block for i, block in enumerate(
transformer_blocks) if i not in block_indices_to_remove]
model.transformer_blocks = nn.ModuleList(new_blocks)
return model