gradio_demo.py 56.2 KB
Newer Older
gushiqiao's avatar
gushiqiao committed
1
2
import argparse
import gc
PengGao's avatar
PengGao committed
3
4
5
6
import glob
import importlib.util
import json
import os
Gu Shiqiao's avatar
Gu Shiqiao committed
7

Gu Shiqiao's avatar
Gu Shiqiao committed
8
os.environ["PROFILING_DEBUG_LEVEL"] = "2"
Gu Shiqiao's avatar
Gu Shiqiao committed
9
10
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
os.environ["DTYPE"] = "BF16"
PengGao's avatar
PengGao committed
11
import random
gushiqiao's avatar
gushiqiao committed
12
13
from datetime import datetime

PengGao's avatar
PengGao committed
14
import gradio as gr
gushiqiao's avatar
gushiqiao committed
15
import psutil
PengGao's avatar
PengGao committed
16
17
import torch
from loguru import logger
gushiqiao's avatar
gushiqiao committed
18

Gu Shiqiao's avatar
Gu Shiqiao committed
19
20
21
22
23
24
25
26
27
from lightx2v.utils.input_info import set_input_info
from lightx2v.utils.set_config import get_default_config

try:
    from flashinfer.rope import apply_rope_with_cos_sin_cache_inplace
except ImportError:
    apply_rope_with_cos_sin_cache_inplace = None


gushiqiao's avatar
gushiqiao committed
28
29
30
31
32
33
34
35
36
logger.add(
    "inference_logs.log",
    rotation="100 MB",
    encoding="utf-8",
    enqueue=True,
    backtrace=True,
    diagnose=True,
)

gushiqiao's avatar
gushiqiao committed
37
38
39
MAX_NUMPY_SEED = 2**32 - 1


Gu Shiqiao's avatar
Gu Shiqiao committed
40
41
42
43
def scan_model_path_contents(model_path):
    """Scan model_path directory and return available files and subdirectories"""
    if not model_path or not os.path.exists(model_path):
        return {"dirs": [], "files": [], "safetensors_dirs": [], "pth_files": []}
gushiqiao's avatar
gushiqiao committed
44

Gu Shiqiao's avatar
Gu Shiqiao committed
45
46
47
48
    dirs = []
    files = []
    safetensors_dirs = []
    pth_files = []
gushiqiao's avatar
gushiqiao committed
49

Gu Shiqiao's avatar
Gu Shiqiao committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
    try:
        for item in os.listdir(model_path):
            item_path = os.path.join(model_path, item)
            if os.path.isdir(item_path):
                dirs.append(item)
                # Check if directory contains safetensors files
                if glob.glob(os.path.join(item_path, "*.safetensors")):
                    safetensors_dirs.append(item)
            elif os.path.isfile(item_path):
                files.append(item)
                if item.endswith(".pth"):
                    pth_files.append(item)
    except Exception as e:
        logger.warning(f"Failed to scan directory: {e}")
gushiqiao's avatar
gushiqiao committed
64

Gu Shiqiao's avatar
Gu Shiqiao committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
    return {
        "dirs": sorted(dirs),
        "files": sorted(files),
        "safetensors_dirs": sorted(safetensors_dirs),
        "pth_files": sorted(pth_files),
    }


def get_dit_choices(model_path, model_type="wan2.1"):
    """Get Diffusion model options (filtered by model type)"""
    contents = scan_model_path_contents(model_path)
    excluded_keywords = ["vae", "tae", "clip", "t5", "high_noise", "low_noise"]
    fp8_supported = is_fp8_supported_gpu()

    if model_type == "wan2.1":
        # wan2.1: filter files/dirs containing wan2.1 or Wan2.1
        def is_valid(name):
            name_lower = name.lower()
            if "wan2.1" not in name_lower:
                return False
            if not fp8_supported and "fp8" in name_lower:
                return False
            return not any(kw in name_lower for kw in excluded_keywords)
gushiqiao's avatar
gushiqiao committed
88
    else:
Gu Shiqiao's avatar
Gu Shiqiao committed
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
        # wan2.2: filter files/dirs containing wan2.2 or Wan2.2
        def is_valid(name):
            name_lower = name.lower()
            if "wan2.2" not in name_lower:
                return False
            if not fp8_supported and "fp8" in name_lower:
                return False
            return not any(kw in name_lower for kw in excluded_keywords)

    # Filter matching directories and files
    dir_choices = [d for d in contents["dirs"] if is_valid(d)]
    file_choices = [f for f in contents["files"] if is_valid(f)]
    choices = dir_choices + file_choices
    return choices if choices else [""]


def get_high_noise_choices(model_path):
    """Get high noise model options (files/dirs containing high_noise)"""
    contents = scan_model_path_contents(model_path)
    fp8_supported = is_fp8_supported_gpu()

    def is_valid(name):
        name_lower = name.lower()
        if not fp8_supported and "fp8" in name_lower:
            return False
        return "high_noise" in name_lower or "high-noise" in name_lower

    dir_choices = [d for d in contents["dirs"] if is_valid(d)]
    file_choices = [f for f in contents["files"] if is_valid(f)]
    choices = dir_choices + file_choices
    return choices if choices else [""]


def get_low_noise_choices(model_path):
    """Get low noise model options (files/dirs containing low_noise)"""
    contents = scan_model_path_contents(model_path)
    fp8_supported = is_fp8_supported_gpu()

    def is_valid(name):
        name_lower = name.lower()
        if not fp8_supported and "fp8" in name_lower:
            return False
        return "low_noise" in name_lower or "low-noise" in name_lower

    dir_choices = [d for d in contents["dirs"] if is_valid(d)]
    file_choices = [f for f in contents["files"] if is_valid(f)]
    choices = dir_choices + file_choices
    return choices if choices else [""]


def get_t5_choices(model_path):
    """Get T5 model options (.pth or .safetensors files containing t5 keyword)"""
    contents = scan_model_path_contents(model_path)
    fp8_supported = is_fp8_supported_gpu()

    # Filter from .pth files
    pth_choices = [f for f in contents["pth_files"] if "t5" in f.lower() and (fp8_supported or "fp8" not in f.lower())]

    # Filter from .safetensors files
    safetensors_choices = [f for f in contents["files"] if f.endswith(".safetensors") and "t5" in f.lower() and (fp8_supported or "fp8" not in f.lower())]

    # Filter from directories containing safetensors
    safetensors_dir_choices = [d for d in contents["safetensors_dirs"] if "t5" in d.lower() and (fp8_supported or "fp8" not in d.lower())]

    choices = pth_choices + safetensors_choices + safetensors_dir_choices
    return choices if choices else [""]


def get_clip_choices(model_path):
    """Get CLIP model options (.pth or .safetensors files containing clip keyword)"""
    contents = scan_model_path_contents(model_path)
    fp8_supported = is_fp8_supported_gpu()

    # Filter from .pth files
    pth_choices = [f for f in contents["pth_files"] if "clip" in f.lower() and (fp8_supported or "fp8" not in f.lower())]

    # Filter from .safetensors files
    safetensors_choices = [f for f in contents["files"] if f.endswith(".safetensors") and "clip" in f.lower() and (fp8_supported or "fp8" not in f.lower())]

    # Filter from directories containing safetensors
    safetensors_dir_choices = [d for d in contents["safetensors_dirs"] if "clip" in d.lower() and (fp8_supported or "fp8" not in d.lower())]

    choices = pth_choices + safetensors_choices + safetensors_dir_choices
    return choices if choices else [""]


def get_vae_choices(model_path):
    """Get VAE model options (.pth or .safetensors files containing vae/VAE/tae keyword)"""
    contents = scan_model_path_contents(model_path)
    fp8_supported = is_fp8_supported_gpu()

    # Filter from .pth files
    pth_choices = [f for f in contents["pth_files"] if any(kw in f.lower() for kw in ["vae", "tae"]) and (fp8_supported or "fp8" not in f.lower())]

    # Filter from .safetensors files
    safetensors_choices = [f for f in contents["files"] if f.endswith(".safetensors") and any(kw in f.lower() for kw in ["vae", "tae"]) and (fp8_supported or "fp8" not in f.lower())]

    # Filter from directories containing safetensors
    safetensors_dir_choices = [d for d in contents["safetensors_dirs"] if any(kw in d.lower() for kw in ["vae", "tae"]) and (fp8_supported or "fp8" not in d.lower())]

    choices = pth_choices + safetensors_choices + safetensors_dir_choices
    return choices if choices else [""]


def detect_quant_scheme(model_name):
    """Automatically detect quantization scheme from model name
    - If model name contains "int8" → "int8"
    - If model name contains "fp8" and device supports → "fp8"
    - Otherwise return None (no quantization)
    """
    if not model_name:
        return None
    name_lower = model_name.lower()
    if "int8" in name_lower:
        return "int8"
    elif "fp8" in name_lower:
        if is_fp8_supported_gpu():
            return "fp8"
        else:
            # Device doesn't support fp8, return None (use default precision)
            return None
    return None


def update_model_path_options(model_path, model_type="wan2.1"):
    """Update all model path selectors when model_path or model_type changes"""
    dit_choices = get_dit_choices(model_path, model_type)
    high_noise_choices = get_high_noise_choices(model_path)
    low_noise_choices = get_low_noise_choices(model_path)
    t5_choices = get_t5_choices(model_path)
    clip_choices = get_clip_choices(model_path)
    vae_choices = get_vae_choices(model_path)

    return (
        gr.update(choices=dit_choices, value=dit_choices[0] if dit_choices else ""),
        gr.update(choices=high_noise_choices, value=high_noise_choices[0] if high_noise_choices else ""),
        gr.update(choices=low_noise_choices, value=low_noise_choices[0] if low_noise_choices else ""),
        gr.update(choices=t5_choices, value=t5_choices[0] if t5_choices else ""),
        gr.update(choices=clip_choices, value=clip_choices[0] if clip_choices else ""),
        gr.update(choices=vae_choices, value=vae_choices[0] if vae_choices else ""),
    )
gushiqiao's avatar
gushiqiao committed
230
231


gushiqiao's avatar
gushiqiao committed
232
233
234
def generate_random_seed():
    return random.randint(0, MAX_NUMPY_SEED)

gushiqiao's avatar
gushiqiao committed
235

gushiqiao's avatar
gushiqiao committed
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
def is_module_installed(module_name):
    try:
        spec = importlib.util.find_spec(module_name)
        return spec is not None
    except ModuleNotFoundError:
        return False


def get_available_quant_ops():
    available_ops = []

    vllm_installed = is_module_installed("vllm")
    if vllm_installed:
        available_ops.append(("vllm", True))
    else:
        available_ops.append(("vllm", False))

    sgl_installed = is_module_installed("sgl_kernel")
    if sgl_installed:
        available_ops.append(("sgl", True))
    else:
        available_ops.append(("sgl", False))

    q8f_installed = is_module_installed("q8_kernels")
    if q8f_installed:
        available_ops.append(("q8f", True))
    else:
        available_ops.append(("q8f", False))

    return available_ops


def get_available_attn_ops():
    available_ops = []

    vllm_installed = is_module_installed("flash_attn")
    if vllm_installed:
        available_ops.append(("flash_attn2", True))
    else:
        available_ops.append(("flash_attn2", False))

    sgl_installed = is_module_installed("flash_attn_interface")
    if sgl_installed:
        available_ops.append(("flash_attn3", True))
    else:
        available_ops.append(("flash_attn3", False))

Gu Shiqiao's avatar
Gu Shiqiao committed
283
284
    sage_installed = is_module_installed("sageattention")
    if sage_installed:
gushiqiao's avatar
gushiqiao committed
285
286
287
288
        available_ops.append(("sage_attn2", True))
    else:
        available_ops.append(("sage_attn2", False))

Gu Shiqiao's avatar
Gu Shiqiao committed
289
290
291
292
293
294
    sage3_installed = is_module_installed("sageattn3")
    if sage3_installed:
        available_ops.append(("sage_attn3", True))
    else:
        available_ops.append(("sage_attn3", False))

gushiqiao's avatar
gushiqiao committed
295
296
297
298
299
300
    torch_installed = is_module_installed("torch")
    if torch_installed:
        available_ops.append(("torch_sdpa", True))
    else:
        available_ops.append(("torch_sdpa", False))

gushiqiao's avatar
gushiqiao committed
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
    return available_ops


def get_gpu_memory(gpu_idx=0):
    if not torch.cuda.is_available():
        return 0
    try:
        with torch.cuda.device(gpu_idx):
            memory_info = torch.cuda.mem_get_info()
            total_memory = memory_info[1] / (1024**3)  # Convert bytes to GB
            return total_memory
    except Exception as e:
        logger.warning(f"Failed to get GPU memory: {e}")
        return 0


def get_cpu_memory():
    available_bytes = psutil.virtual_memory().available
    return available_bytes / 1024**3
gushiqiao's avatar
gushiqiao committed
320
321


gushiqiao's avatar
gushiqiao committed
322
323
324
325
326
327
328
329
def cleanup_memory():
    gc.collect()

    if torch.cuda.is_available():
        torch.cuda.empty_cache()
        torch.cuda.synchronize()

    try:
Gu Shiqiao's avatar
Gu Shiqiao committed
330
331
        import psutil

gushiqiao's avatar
gushiqiao committed
332
333
334
335
336
337
338
339
340
341
        if hasattr(psutil, "virtual_memory"):
            if os.name == "posix":
                try:
                    os.system("sync")
                except:  # noqa
                    pass
    except:  # noqa
        pass


gushiqiao's avatar
gushiqiao committed
342
343
def generate_unique_filename(output_dir):
    os.makedirs(output_dir, exist_ok=True)
gushiqiao's avatar
gushiqiao committed
344
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
Gu Shiqiao's avatar
Gu Shiqiao committed
345
    return os.path.join(output_dir, f"{timestamp}.mp4")
gushiqiao's avatar
gushiqiao committed
346
347


gushiqiao's avatar
gushiqiao committed
348
349
350
351
352
353
354
355
def is_fp8_supported_gpu():
    if not torch.cuda.is_available():
        return False
    compute_capability = torch.cuda.get_device_capability(0)
    major, minor = compute_capability
    return (major == 8 and minor == 9) or (major >= 9)


gushiqiao's avatar
gushiqiao committed
356
357
358
359
360
361
362
363
364
365
366
367
def is_ada_architecture_gpu():
    if not torch.cuda.is_available():
        return False
    try:
        gpu_name = torch.cuda.get_device_name(0).upper()
        ada_keywords = ["RTX 40", "RTX40", "4090", "4080", "4070", "4060"]
        return any(keyword in gpu_name for keyword in ada_keywords)
    except Exception as e:
        logger.warning(f"Failed to get GPU name: {e}")
        return False


gushiqiao's avatar
gushiqiao committed
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
def get_quantization_options(model_path):
    """Get quantization options dynamically based on model_path"""
    import os

    # Check subdirectories
    subdirs = ["original", "fp8", "int8"]
    has_subdirs = {subdir: os.path.exists(os.path.join(model_path, subdir)) for subdir in subdirs}

    # Check original files in root directory
    t5_bf16_exists = os.path.exists(os.path.join(model_path, "models_t5_umt5-xxl-enc-bf16.pth"))
    clip_fp16_exists = os.path.exists(os.path.join(model_path, "models_clip_open-clip-xlm-roberta-large-vit-huge-14.pth"))

    # Generate options
    def get_choices(has_subdirs, original_type, fp8_type, int8_type, fallback_type, has_original_file=False):
        choices = []
        if has_subdirs["original"]:
            choices.append(original_type)
        if has_subdirs["fp8"]:
            choices.append(fp8_type)
        if has_subdirs["int8"]:
            choices.append(int8_type)

        # If no subdirectories but original file exists, add original type
gushiqiao's avatar
gushiqiao committed
391
392
393
        if has_original_file:
            if not choices or "original" not in choices:
                choices.append(original_type)
gushiqiao's avatar
gushiqiao committed
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412

        # If no options at all, use default value
        if not choices:
            choices = [fallback_type]

        return choices, choices[0]

    # DIT options
    dit_choices, dit_default = get_choices(has_subdirs, "bf16", "fp8", "int8", "bf16")

    # T5 options - check if original file exists
    t5_choices, t5_default = get_choices(has_subdirs, "bf16", "fp8", "int8", "bf16", t5_bf16_exists)

    # CLIP options - check if original file exists
    clip_choices, clip_default = get_choices(has_subdirs, "fp16", "fp8", "int8", "fp16", clip_fp16_exists)

    return {"dit_choices": dit_choices, "dit_default": dit_default, "t5_choices": t5_choices, "t5_default": t5_default, "clip_choices": clip_choices, "clip_default": clip_default}


Gu Shiqiao's avatar
Gu Shiqiao committed
413
414
415
416
417
418
419
420
421
422
423
424
425
426
def determine_model_cls(model_type, dit_name, high_noise_name):
    """Determine model_cls based on model type and file name"""
    # Determine file name to check
    if model_type == "wan2.1":
        check_name = dit_name.lower() if dit_name else ""
        is_distill = "4step" in check_name
        return "wan2.1_distill" if is_distill else "wan2.1"
    else:
        # wan2.2
        check_name = high_noise_name.lower() if high_noise_name else ""
        is_distill = "4step" in check_name
        return "wan2.2_moe_distill" if is_distill else "wan2.2_moe"


gushiqiao's avatar
gushiqiao committed
427
428
global_runner = None
current_config = None
Gu Shiqiao's avatar
Gu Shiqiao committed
429
430
431
cur_dit_path = None
cur_t5_path = None
cur_clip_path = None
gushiqiao's avatar
gushiqiao committed
432
433
434
435
436
437
438
439
440

available_quant_ops = get_available_quant_ops()
quant_op_choices = []
for op_name, is_installed in available_quant_ops:
    status_text = "✅ Installed" if is_installed else "❌ Not Installed"
    display_text = f"{op_name} ({status_text})"
    quant_op_choices.append((op_name, display_text))

available_attn_ops = get_available_attn_ops()
Gu Shiqiao's avatar
Gu Shiqiao committed
441
442
443
# Priority order
attn_priority = ["sage_attn3", "sage_attn2", "flash_attn3", "flash_attn2", "torch_sdpa"]
# Sort by priority, installed ones first, uninstalled ones last
gushiqiao's avatar
gushiqiao committed
444
attn_op_choices = []
Gu Shiqiao's avatar
Gu Shiqiao committed
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
attn_op_dict = dict(available_attn_ops)

# Add installed ones first (by priority)
for op_name in attn_priority:
    if op_name in attn_op_dict and attn_op_dict[op_name]:
        status_text = "✅ Installed"
        display_text = f"{op_name} ({status_text})"
        attn_op_choices.append((op_name, display_text))

# Add uninstalled ones (by priority)
for op_name in attn_priority:
    if op_name in attn_op_dict and not attn_op_dict[op_name]:
        status_text = "❌ Not Installed"
        display_text = f"{op_name} ({status_text})"
        attn_op_choices.append((op_name, display_text))

# Add other operators not in priority list (installed ones first)
other_ops = [(op_name, is_installed) for op_name, is_installed in available_attn_ops if op_name not in attn_priority]
for op_name, is_installed in sorted(other_ops, key=lambda x: not x[1]):  # Installed ones first
gushiqiao's avatar
gushiqiao committed
464
465
466
467
468
    status_text = "✅ Installed" if is_installed else "❌ Not Installed"
    display_text = f"{op_name} ({status_text})"
    attn_op_choices.append((op_name, display_text))


gushiqiao's avatar
gushiqiao committed
469
470
471
def run_inference(
    prompt,
    negative_prompt,
472
    save_result_path,
gushiqiao's avatar
gushiqiao committed
473
474
475
476
477
478
479
480
481
482
483
484
    infer_steps,
    num_frames,
    resolution,
    seed,
    sample_shift,
    enable_cfg,
    cfg_scale,
    fps,
    use_tiling_vae,
    lazy_load,
    cpu_offload,
    offload_granularity,
gushiqiao's avatar
gushiqiao committed
485
    t5_cpu_offload,
Gu Shiqiao's avatar
Gu Shiqiao committed
486
487
    clip_cpu_offload,
    vae_cpu_offload,
gushiqiao's avatar
gushiqiao committed
488
    unload_modules,
gushiqiao's avatar
gushiqiao committed
489
490
    attention_type,
    quant_op,
Gu Shiqiao's avatar
Gu Shiqiao committed
491
492
    rope_chunk,
    rope_chunk_size,
gushiqiao's avatar
gushiqiao committed
493
    clean_cuda_cache,
Gu Shiqiao's avatar
Gu Shiqiao committed
494
495
496
497
498
499
500
501
502
    model_path_input,
    model_type_input,
    task_type_input,
    dit_path_input,
    high_noise_path_input,
    low_noise_path_input,
    t5_path_input,
    clip_path_input,
    vae_path_input,
gushiqiao's avatar
gushiqiao committed
503
    image_path=None,
gushiqiao's avatar
gushiqiao committed
504
):
gushiqiao's avatar
gushiqiao committed
505
506
    cleanup_memory()

gushiqiao's avatar
gushiqiao committed
507
508
509
    quant_op = quant_op.split("(")[0].strip()
    attention_type = attention_type.split("(")[0].strip()

Gu Shiqiao's avatar
Gu Shiqiao committed
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
    global global_runner, current_config, model_path, model_cls
    global cur_dit_path, cur_t5_path, cur_clip_path

    task = task_type_input
    model_cls = determine_model_cls(model_type_input, dit_path_input, high_noise_path_input)
    logger.info(f"Auto-determined model_cls: {model_cls} (Model type: {model_type_input})")

    if model_type_input == "wan2.1":
        dit_quant_detected = detect_quant_scheme(dit_path_input)
    else:
        dit_quant_detected = detect_quant_scheme(high_noise_path_input)
    t5_quant_detected = detect_quant_scheme(t5_path_input)
    clip_quant_detected = detect_quant_scheme(clip_path_input)
    logger.info(f"Auto-detected quantization scheme - DIT: {dit_quant_detected}, T5: {t5_quant_detected}, CLIP: {clip_quant_detected}")

    if model_path_input and model_path_input.strip():
        model_path = model_path_input.strip()
gushiqiao's avatar
gushiqiao committed
527
528
529
530

    if os.path.exists(os.path.join(model_path, "config.json")):
        with open(os.path.join(model_path, "config.json"), "r") as f:
            model_config = json.load(f)
gushiqiao's avatar
gushiqiao committed
531
532
    else:
        model_config = {}
gushiqiao's avatar
gushiqiao committed
533

Gu Shiqiao's avatar
Gu Shiqiao committed
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
    save_result_path = generate_unique_filename(output_dir)

    is_dit_quant = dit_quant_detected != "bf16"
    is_t5_quant = t5_quant_detected != "bf16"
    is_clip_quant = clip_quant_detected != "fp16"

    dit_quantized_ckpt = None
    dit_original_ckpt = None
    high_noise_quantized_ckpt = None
    low_noise_quantized_ckpt = None
    high_noise_original_ckpt = None
    low_noise_original_ckpt = None

    if is_dit_quant:
        dit_quant_scheme = f"{dit_quant_detected}-{quant_op}"
        if "wan2.1" in model_cls:
            dit_quantized_ckpt = os.path.join(model_path, dit_path_input)
gushiqiao's avatar
gushiqiao committed
551
        else:
Gu Shiqiao's avatar
Gu Shiqiao committed
552
553
554
555
556
557
            high_noise_quantized_ckpt = os.path.join(model_path, high_noise_path_input)
            low_noise_quantized_ckpt = os.path.join(model_path, low_noise_path_input)
    else:
        dit_quantized_ckpt = "Default"
        if "wan2.1" in model_cls:
            dit_original_ckpt = os.path.join(model_path, dit_path_input)
gushiqiao's avatar
gushiqiao committed
558
        else:
Gu Shiqiao's avatar
Gu Shiqiao committed
559
560
            high_noise_original_ckpt = os.path.join(model_path, high_noise_path_input)
            low_noise_original_ckpt = os.path.join(model_path, low_noise_path_input)
gushiqiao's avatar
gushiqiao committed
561

Gu Shiqiao's avatar
Gu Shiqiao committed
562
    # Use frontend-selected T5 path
gushiqiao's avatar
gushiqiao committed
563
    if is_t5_quant:
Gu Shiqiao's avatar
Gu Shiqiao committed
564
565
        t5_quantized_ckpt = os.path.join(model_path, t5_path_input)
        t5_quant_scheme = f"{t5_quant_detected}-{quant_op}"
gushiqiao's avatar
gushiqiao committed
566
        t5_original_ckpt = None
gushiqiao's avatar
gushiqiao committed
567
    else:
Gu Shiqiao's avatar
Gu Shiqiao committed
568
569
570
        t5_quantized_ckpt = None
        t5_quant_scheme = None
        t5_original_ckpt = os.path.join(model_path, t5_path_input)
gushiqiao's avatar
gushiqiao committed
571

Gu Shiqiao's avatar
Gu Shiqiao committed
572
    # Use frontend-selected CLIP path
gushiqiao's avatar
gushiqiao committed
573
    if is_clip_quant:
Gu Shiqiao's avatar
Gu Shiqiao committed
574
575
        clip_quantized_ckpt = os.path.join(model_path, clip_path_input)
        clip_quant_scheme = f"{clip_quant_detected}-{quant_op}"
gushiqiao's avatar
gushiqiao committed
576
        clip_original_ckpt = None
gushiqiao's avatar
gushiqiao committed
577
    else:
Gu Shiqiao's avatar
Gu Shiqiao committed
578
579
580
581
582
583
584
585
586
587
588
        clip_quantized_ckpt = None
        clip_quant_scheme = None
        clip_original_ckpt = os.path.join(model_path, clip_path_input)

    if model_type_input == "wan2.1":
        current_dit_path = dit_path_input
    else:
        current_dit_path = f"{high_noise_path_input}|{low_noise_path_input}" if high_noise_path_input and low_noise_path_input else None

    current_t5_path = t5_path_input
    current_clip_path = clip_path_input
gushiqiao's avatar
gushiqiao committed
589

gushiqiao's avatar
gushiqiao committed
590
591
    needs_reinit = (
        lazy_load
gushiqiao's avatar
gushiqiao committed
592
        or unload_modules
gushiqiao's avatar
gushiqiao committed
593
594
        or global_runner is None
        or current_config is None
Gu Shiqiao's avatar
Gu Shiqiao committed
595
596
597
598
599
600
        or cur_dit_path is None
        or cur_dit_path != current_dit_path
        or cur_t5_path is None
        or cur_t5_path != current_t5_path
        or cur_clip_path is None
        or cur_clip_path != current_clip_path
gushiqiao's avatar
gushiqiao committed
601
    )
gushiqiao's avatar
gushiqiao committed
602

Gu Shiqiao's avatar
Gu Shiqiao committed
603
604
    if cfg_scale == 1:
        enable_cfg = False
gushiqiao's avatar
gushiqiao committed
605
    else:
Gu Shiqiao's avatar
Gu Shiqiao committed
606
        enable_cfg = True
gushiqiao's avatar
gushiqiao committed
607

Gu Shiqiao's avatar
Gu Shiqiao committed
608
609
610
611
612
613
    vae_name_lower = vae_path_input.lower() if vae_path_input else ""
    use_tae = "tae" in vae_name_lower or "lighttae" in vae_name_lower
    use_lightvae = "lightvae" in vae_name_lower
    need_scaled = "lighttae" in vae_name_lower

    logger.info(f"VAE configuration - use_tae: {use_tae}, use_lightvae: {use_lightvae}, need_scaled: {need_scaled} (VAE: {vae_path_input})")
gushiqiao's avatar
gushiqiao committed
614

Gu Shiqiao's avatar
Gu Shiqiao committed
615
    config_graio = {
gushiqiao's avatar
gushiqiao committed
616
617
618
619
        "infer_steps": infer_steps,
        "target_video_length": num_frames,
        "target_width": int(resolution.split("x")[0]),
        "target_height": int(resolution.split("x")[1]),
gushiqiao's avatar
gushiqiao committed
620
621
622
        "self_attn_1_type": attention_type,
        "cross_attn_1_type": attention_type,
        "cross_attn_2_type": attention_type,
gushiqiao's avatar
gushiqiao committed
623
624
625
626
        "enable_cfg": enable_cfg,
        "sample_guide_scale": cfg_scale,
        "sample_shift": sample_shift,
        "fps": fps,
Gu Shiqiao's avatar
Gu Shiqiao committed
627
        "feature_caching": "NoCaching",
gushiqiao's avatar
gushiqiao committed
628
629
630
631
632
633
634
635
636
637
        "do_mm_calib": False,
        "parallel_attn_type": None,
        "parallel_vae": False,
        "max_area": False,
        "vae_stride": (4, 8, 8),
        "patch_size": (1, 2, 2),
        "lora_path": None,
        "strength_model": 1.0,
        "use_prompt_enhancer": False,
        "text_len": 512,
gushiqiao's avatar
gushiqiao committed
638
        "denoising_step_list": [1000, 750, 500, 250],
Gu Shiqiao's avatar
Gu Shiqiao committed
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
        "cpu_offload": True if "wan2.2" in model_cls else cpu_offload,
        "offload_granularity": "phase" if "wan2.2" in model_cls else offload_granularity,
        "t5_cpu_offload": t5_cpu_offload,
        "clip_cpu_offload": clip_cpu_offload,
        "vae_cpu_offload": vae_cpu_offload,
        "dit_quantized": is_dit_quant,
        "dit_quant_scheme": dit_quant_scheme,
        "dit_quantized_ckpt": dit_quantized_ckpt,
        "dit_original_ckpt": dit_original_ckpt,
        "high_noise_quantized_ckpt": high_noise_quantized_ckpt,
        "low_noise_quantized_ckpt": low_noise_quantized_ckpt,
        "high_noise_original_ckpt": high_noise_original_ckpt,
        "low_noise_original_ckpt": low_noise_original_ckpt,
        "t5_original_ckpt": t5_original_ckpt,
        "t5_quantized": is_t5_quant,
        "t5_quantized_ckpt": t5_quantized_ckpt,
        "t5_quant_scheme": t5_quant_scheme,
        "clip_original_ckpt": clip_original_ckpt,
        "clip_quantized": is_clip_quant,
        "clip_quantized_ckpt": clip_quantized_ckpt,
        "clip_quant_scheme": clip_quant_scheme,
        "vae_path": os.path.join(model_path, vae_path_input),
        "use_tiling_vae": use_tiling_vae,
        "use_tae": use_tae,
        "use_lightvae": use_lightvae,
        "need_scaled": need_scaled,
        "lazy_load": lazy_load,
        "rope_chunk": rope_chunk,
        "rope_chunk_size": rope_chunk_size,
        "clean_cuda_cache": clean_cuda_cache,
        "unload_modules": unload_modules,
        "seq_parallel": False,
        "warm_up_cpu_buffers": False,
        "boundary_step_index": 2,
        "boundary": 0.900,
        "use_image_encoder": False if "wan2.2" in model_cls else True,
        "rope_type": "flashinfer" if apply_rope_with_cos_sin_cache_inplace else "torch",
gushiqiao's avatar
gushiqiao committed
676
677
678
679
    }

    args = argparse.Namespace(
        model_cls=model_cls,
Gu Shiqiao's avatar
Gu Shiqiao committed
680
        seed=seed,
gushiqiao's avatar
gushiqiao committed
681
682
683
684
685
686
        task=task,
        model_path=model_path,
        prompt_enhancer=None,
        prompt=prompt,
        negative_prompt=negative_prompt,
        image_path=image_path,
687
        save_result_path=save_result_path,
Gu Shiqiao's avatar
Gu Shiqiao committed
688
        return_result_tensor=False,
gushiqiao's avatar
gushiqiao committed
689
690
    )

Gu Shiqiao's avatar
Gu Shiqiao committed
691
    config = get_default_config()
gushiqiao's avatar
gushiqiao committed
692
693
    config.update({k: v for k, v in vars(args).items()})
    config.update(model_config)
Gu Shiqiao's avatar
Gu Shiqiao committed
694
    config.update(config_graio)
gushiqiao's avatar
gushiqiao committed
695
696
697
698

    logger.info(f"Using model: {model_path}")
    logger.info(f"Inference configuration:\n{json.dumps(config, indent=4, ensure_ascii=False)}")

gushiqiao's avatar
gushiqiao committed
699
    # Initialize or reuse the runner
gushiqiao's avatar
gushiqiao committed
700
701
702
703
704
705
706
    runner = global_runner
    if needs_reinit:
        if runner is not None:
            del runner
            torch.cuda.empty_cache()
            gc.collect()

gushiqiao's avatar
gushiqiao committed
707
708
        from lightx2v.infer import init_runner  # noqa

gushiqiao's avatar
gushiqiao committed
709
        runner = init_runner(config)
Gu Shiqiao's avatar
Gu Shiqiao committed
710
711
        input_info = set_input_info(args)

gushiqiao's avatar
gushiqiao committed
712
        current_config = config
Gu Shiqiao's avatar
Gu Shiqiao committed
713
714
715
        cur_dit_path = current_dit_path
        cur_t5_path = current_t5_path
        cur_clip_path = current_clip_path
gushiqiao's avatar
gushiqiao committed
716
717
718

        if not lazy_load:
            global_runner = runner
gushiqiao's avatar
gushiqiao committed
719
720
    else:
        runner.config = config
gushiqiao's avatar
gushiqiao committed
721

Gu Shiqiao's avatar
Gu Shiqiao committed
722
    runner.run_pipeline(input_info)
gushiqiao's avatar
gushiqiao committed
723
    cleanup_memory()
gushiqiao's avatar
gushiqiao committed
724

725
    return save_result_path
gushiqiao's avatar
gushiqiao committed
726
727


gushiqiao's avatar
gushiqiao committed
728
729
730
731
732
def handle_lazy_load_change(lazy_load_enabled):
    """Handle lazy_load checkbox change to automatically enable unload_modules"""
    return gr.update(value=lazy_load_enabled)


Gu Shiqiao's avatar
Gu Shiqiao committed
733
734
def auto_configure(resolution):
    """Auto-configure inference options based on machine configuration and resolution"""
gushiqiao's avatar
gushiqiao committed
735
736
    default_config = {
        "lazy_load_val": False,
Gu Shiqiao's avatar
Gu Shiqiao committed
737
738
        "rope_chunk_val": False,
        "rope_chunk_size_val": 100,
gushiqiao's avatar
gushiqiao committed
739
740
741
        "clean_cuda_cache_val": False,
        "cpu_offload_val": False,
        "offload_granularity_val": "block",
gushiqiao's avatar
gushiqiao committed
742
        "t5_cpu_offload_val": False,
Gu Shiqiao's avatar
Gu Shiqiao committed
743
744
        "clip_cpu_offload_val": False,
        "vae_cpu_offload_val": False,
gushiqiao's avatar
gushiqiao committed
745
        "unload_modules_val": False,
gushiqiao's avatar
gushiqiao committed
746
747
748
749
        "attention_type_val": attn_op_choices[0][1],
        "quant_op_val": quant_op_choices[0][1],
        "use_tiling_vae_val": False,
    }
gushiqiao's avatar
gushiqiao committed
750

gushiqiao's avatar
gushiqiao committed
751
752
753
    gpu_memory = round(get_gpu_memory())
    cpu_memory = round(get_cpu_memory())

Gu Shiqiao's avatar
Gu Shiqiao committed
754
    attn_priority = ["sage_attn3", "sage_attn2", "flash_attn3", "flash_attn2", "torch_sdpa"]
gushiqiao's avatar
gushiqiao committed
755
756
757
758

    if is_ada_architecture_gpu():
        quant_op_priority = ["q8f", "vllm", "sgl"]
    else:
Gu Shiqiao's avatar
Gu Shiqiao committed
759
        quant_op_priority = ["vllm", "sgl", "q8f"]
gushiqiao's avatar
gushiqiao committed
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

    for op in attn_priority:
        if dict(available_attn_ops).get(op):
            default_config["attention_type_val"] = dict(attn_op_choices)[op]
            break

    for op in quant_op_priority:
        if dict(available_quant_ops).get(op):
            default_config["quant_op_val"] = dict(quant_op_choices)[op]
            break

    if resolution in [
        "1280x720",
        "720x1280",
        "1280x544",
        "544x1280",
        "1104x832",
        "832x1104",
        "960x960",
    ]:
        res = "720p"
    elif resolution in [
        "960x544",
        "544x960",
    ]:
        res = "540p"
    else:
        res = "480p"

Gu Shiqiao's avatar
Gu Shiqiao committed
789
    if res == "720p":
gushiqiao's avatar
gushiqiao committed
790
791
        gpu_rules = [
            (80, {}),
Gu Shiqiao's avatar
Gu Shiqiao committed
792
793
            (40, {"cpu_offload_val": False, "t5_cpu_offload_val": True, "vae_cpu_offload_val": True, "clip_cpu_offload_val": True}),
            (32, {"cpu_offload_val": True, "t5_cpu_offload_val": False, "vae_cpu_offload_val": False, "clip_cpu_offload_val": False}),
gushiqiao's avatar
gushiqiao committed
794
795
796
797
798
            (
                24,
                {
                    "cpu_offload_val": True,
                    "use_tiling_vae_val": True,
Gu Shiqiao's avatar
Gu Shiqiao committed
799
800
801
                    "t5_cpu_offload_val": True,
                    "vae_cpu_offload_val": True,
                    "clip_cpu_offload_val": True,
gushiqiao's avatar
gushiqiao committed
802
803
804
805
806
807
                },
            ),
            (
                16,
                {
                    "cpu_offload_val": True,
Gu Shiqiao's avatar
Gu Shiqiao committed
808
809
810
                    "t5_cpu_offload_val": True,
                    "vae_cpu_offload_val": True,
                    "clip_cpu_offload_val": True,
gushiqiao's avatar
gushiqiao committed
811
812
                    "use_tiling_vae_val": True,
                    "offload_granularity_val": "phase",
Gu Shiqiao's avatar
Gu Shiqiao committed
813
814
                    "rope_chunk_val": True,
                    "rope_chunk_size_val": 100,
gushiqiao's avatar
gushiqiao committed
815
816
817
818
819
820
                },
            ),
            (
                8,
                {
                    "cpu_offload_val": True,
Gu Shiqiao's avatar
Gu Shiqiao committed
821
822
823
                    "t5_cpu_offload_val": True,
                    "vae_cpu_offload_val": True,
                    "clip_cpu_offload_val": True,
gushiqiao's avatar
gushiqiao committed
824
825
                    "use_tiling_vae_val": True,
                    "offload_granularity_val": "phase",
Gu Shiqiao's avatar
Gu Shiqiao committed
826
827
                    "rope_chunk_val": True,
                    "rope_chunk_size_val": 100,
gushiqiao's avatar
gushiqiao committed
828
829
830
831
832
                    "clean_cuda_cache_val": True,
                },
            ),
        ]

Gu Shiqiao's avatar
Gu Shiqiao committed
833
    else:
gushiqiao's avatar
gushiqiao committed
834
835
        gpu_rules = [
            (80, {}),
Gu Shiqiao's avatar
Gu Shiqiao committed
836
837
            (40, {"cpu_offload_val": False, "t5_cpu_offload_val": True, "vae_cpu_offload_val": True, "clip_cpu_offload_val": True}),
            (32, {"cpu_offload_val": True, "t5_cpu_offload_val": False, "vae_cpu_offload_val": False, "clip_cpu_offload_val": False}),
gushiqiao's avatar
gushiqiao committed
838
            (
Gu Shiqiao's avatar
Gu Shiqiao committed
839
                24,
gushiqiao's avatar
gushiqiao committed
840
841
                {
                    "cpu_offload_val": True,
Gu Shiqiao's avatar
Gu Shiqiao committed
842
843
844
                    "t5_cpu_offload_val": True,
                    "vae_cpu_offload_val": True,
                    "clip_cpu_offload_val": True,
gushiqiao's avatar
gushiqiao committed
845
                    "use_tiling_vae_val": True,
gushiqiao's avatar
gushiqiao committed
846
847
                },
            ),
gushiqiao's avatar
gushiqiao committed
848
849
            (
                16,
gushiqiao's avatar
gushiqiao committed
850
                {
Gu Shiqiao's avatar
Gu Shiqiao committed
851
                    "cpu_offload_val": True,
Gu Shiqiao's avatar
Gu Shiqiao committed
852
853
854
                    "t5_cpu_offload_val": True,
                    "vae_cpu_offload_val": True,
                    "clip_cpu_offload_val": True,
Gu Shiqiao's avatar
Gu Shiqiao committed
855
856
                    "use_tiling_vae_val": True,
                    "offload_granularity_val": "phase",
gushiqiao's avatar
gushiqiao committed
857
                },
gushiqiao's avatar
gushiqiao committed
858
            ),
gushiqiao's avatar
gushiqiao committed
859
            (
Gu Shiqiao's avatar
Gu Shiqiao committed
860
                8,
gushiqiao's avatar
gushiqiao committed
861
                {
Gu Shiqiao's avatar
Gu Shiqiao committed
862
                    "cpu_offload_val": True,
Gu Shiqiao's avatar
Gu Shiqiao committed
863
864
865
                    "t5_cpu_offload_val": True,
                    "vae_cpu_offload_val": True,
                    "clip_cpu_offload_val": True,
Gu Shiqiao's avatar
Gu Shiqiao committed
866
867
                    "use_tiling_vae_val": True,
                    "offload_granularity_val": "phase",
gushiqiao's avatar
gushiqiao committed
868
869
870
                },
            ),
        ]
gushiqiao's avatar
gushiqiao committed
871

Gu Shiqiao's avatar
Gu Shiqiao committed
872
873
874
875
876
877
878
879
880
881
882
883
884
    cpu_rules = [
        (128, {}),
        (64, {}),
        (32, {"unload_modules_val": True}),
        (
            16,
            {
                "lazy_load_val": True,
                "unload_modules_val": True,
            },
        ),
    ]

gushiqiao's avatar
gushiqiao committed
885
886
887
888
889
890
891
892
893
894
    for threshold, updates in gpu_rules:
        if gpu_memory >= threshold:
            default_config.update(updates)
            break

    for threshold, updates in cpu_rules:
        if cpu_memory >= threshold:
            default_config.update(updates)
            break

Gu Shiqiao's avatar
Gu Shiqiao committed
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
    return (
        gr.update(value=default_config["lazy_load_val"]),
        gr.update(value=default_config["rope_chunk_val"]),
        gr.update(value=default_config["rope_chunk_size_val"]),
        gr.update(value=default_config["clean_cuda_cache_val"]),
        gr.update(value=default_config["cpu_offload_val"]),
        gr.update(value=default_config["offload_granularity_val"]),
        gr.update(value=default_config["t5_cpu_offload_val"]),
        gr.update(value=default_config["clip_cpu_offload_val"]),
        gr.update(value=default_config["vae_cpu_offload_val"]),
        gr.update(value=default_config["unload_modules_val"]),
        gr.update(value=default_config["attention_type_val"]),
        gr.update(value=default_config["quant_op_val"]),
        gr.update(value=default_config["use_tiling_vae_val"]),
    )
gushiqiao's avatar
gushiqiao committed
910
911


Gu Shiqiao's avatar
Gu Shiqiao committed
912
css = """
Gu Shiqiao's avatar
Gu Shiqiao committed
913
        .main-content { max-width: 1600px; margin: auto; padding: 20px; }
gushiqiao's avatar
gushiqiao committed
914
        .warning { color: #ff6b6b; font-weight: bold; }
Gu Shiqiao's avatar
Gu Shiqiao committed
915
916
917
918
919
920
921
922

        /* Model configuration area styles */
        .model-config {
            margin-bottom: 20px !important;
            border: 1px solid #e0e0e0;
            border-radius: 12px;
            padding: 15px;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
gushiqiao's avatar
gushiqiao committed
923
        }
Gu Shiqiao's avatar
Gu Shiqiao committed
924
925
926
927
928
929
930
931

        /* Input parameters area styles */
        .input-params {
            margin-bottom: 20px !important;
            border: 1px solid #e0e0e0;
            border-radius: 12px;
            padding: 15px;
            background: linear-gradient(135deg, #fff5f5 0%, #ffeef0 100%);
gushiqiao's avatar
gushiqiao committed
932
        }
Gu Shiqiao's avatar
Gu Shiqiao committed
933
934
935
936
937
938
939
940

        /* Output video area styles */
        .output-video {
            border: 1px solid #e0e0e0;
            border-radius: 12px;
            padding: 20px;
            background: linear-gradient(135deg, #e0f2fe 0%, #bae6fd 100%);
            min-height: 400px;
gushiqiao's avatar
gushiqiao committed
941
        }
gushiqiao's avatar
gushiqiao committed
942

Gu Shiqiao's avatar
Gu Shiqiao committed
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
        /* Generate button styles */
        .generate-btn {
            width: 100%;
            margin-top: 20px;
            padding: 15px 30px !important;
            font-size: 18px !important;
            font-weight: bold !important;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
            border: none !important;
            border-radius: 10px !important;
            box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4) !important;
            transition: all 0.3s ease !important;
        }
        .generate-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(102, 126, 234, 0.6) !important;
        }
gushiqiao's avatar
gushiqiao committed
960

Gu Shiqiao's avatar
Gu Shiqiao committed
961
962
963
964
965
966
967
968
        /* Accordion header styles */
        .model-config .gr-accordion-header,
        .input-params .gr-accordion-header,
        .output-video .gr-accordion-header {
            font-size: 20px !important;
            font-weight: bold !important;
            padding: 15px !important;
        }
gushiqiao's avatar
gushiqiao committed
969

Gu Shiqiao's avatar
Gu Shiqiao committed
970
971
972
973
        /* Optimize spacing */
        .gr-row {
            margin-bottom: 15px;
        }
gushiqiao's avatar
gushiqiao committed
974

Gu Shiqiao's avatar
Gu Shiqiao committed
975
976
977
978
979
        /* Video player styles */
        .output-video video {
            border-radius: 10px;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
        }
Gu Shiqiao's avatar
Gu Shiqiao committed
980
    """
Gu Shiqiao's avatar
Gu Shiqiao committed
981

Gu Shiqiao's avatar
Gu Shiqiao committed
982
983
984
985
986

def main():
    with gr.Blocks(title="Lightx2v (Lightweight Video Inference and Generation Engine)") as demo:
        gr.Markdown(f"# 🎬 LightX2V Video Generator")
        gr.HTML(f"<style>{css}</style>")
Gu Shiqiao's avatar
Gu Shiqiao committed
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
        # Main layout: left and right columns
        with gr.Row():
            # Left: configuration and input area
            with gr.Column(scale=5):
                # Model configuration area
                with gr.Accordion("🗂️ Model Configuration", open=True, elem_classes=["model-config"]):
                    # FP8 support notice
                    if not is_fp8_supported_gpu():
                        gr.Markdown("⚠️ **Your device does not support FP8 inference**. Models containing FP8 have been automatically hidden.")

                    # Hidden state components
                    model_path_input = gr.Textbox(value=model_path, visible=False)

                    # Model type + Task type
gushiqiao's avatar
gushiqiao committed
1001
                    with gr.Row():
Gu Shiqiao's avatar
Gu Shiqiao committed
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
                        model_type_input = gr.Radio(
                            label="Model Type",
                            choices=["wan2.1", "wan2.2"],
                            value="wan2.1",
                            info="wan2.2 requires separate high noise and low noise models",
                        )
                        task_type_input = gr.Radio(
                            label="Task Type",
                            choices=["i2v", "t2v"],
                            value="i2v",
                            info="i2v: Image-to-video, t2v: Text-to-video",
gushiqiao's avatar
gushiqiao committed
1013
1014
                        )

Gu Shiqiao's avatar
Gu Shiqiao committed
1015
1016
1017
1018
1019
1020
1021
1022
                    # wan2.1: Diffusion model (single row)
                    with gr.Row() as wan21_row:
                        dit_path_input = gr.Dropdown(
                            label="🎨 Diffusion Model",
                            choices=get_dit_choices(model_path, "wan2.1"),
                            value=get_dit_choices(model_path, "wan2.1")[0] if get_dit_choices(model_path, "wan2.1") else "",
                            allow_custom_value=True,
                            visible=True,
gushiqiao's avatar
gushiqiao committed
1023
1024
                        )

Gu Shiqiao's avatar
Gu Shiqiao committed
1025
1026
1027
1028
1029
1030
1031
                    # wan2.2 specific: high noise model + low noise model (hidden by default)
                    with gr.Row(visible=False) as wan22_row:
                        high_noise_path_input = gr.Dropdown(
                            label="🔊 High Noise Model",
                            choices=get_high_noise_choices(model_path),
                            value=get_high_noise_choices(model_path)[0] if get_high_noise_choices(model_path) else "",
                            allow_custom_value=True,
gushiqiao's avatar
gushiqiao committed
1032
                        )
Gu Shiqiao's avatar
Gu Shiqiao committed
1033
1034
1035
1036
1037
                        low_noise_path_input = gr.Dropdown(
                            label="🔇 Low Noise Model",
                            choices=get_low_noise_choices(model_path),
                            value=get_low_noise_choices(model_path)[0] if get_low_noise_choices(model_path) else "",
                            allow_custom_value=True,
gushiqiao's avatar
gushiqiao committed
1038
1039
                        )

Gu Shiqiao's avatar
Gu Shiqiao committed
1040
                    # Text encoder (single row)
gushiqiao's avatar
gushiqiao committed
1041
                    with gr.Row():
Gu Shiqiao's avatar
Gu Shiqiao committed
1042
1043
1044
1045
1046
                        t5_path_input = gr.Dropdown(
                            label="📝 Text Encoder",
                            choices=get_t5_choices(model_path),
                            value=get_t5_choices(model_path)[0] if get_t5_choices(model_path) else "",
                            allow_custom_value=True,
gushiqiao's avatar
gushiqiao committed
1047
                        )
gushiqiao's avatar
gushiqiao committed
1048

Gu Shiqiao's avatar
Gu Shiqiao committed
1049
1050
1051
1052
1053
1054
1055
                    # Image encoder + VAE decoder
                    with gr.Row():
                        clip_path_input = gr.Dropdown(
                            label="🖼️ Image Encoder",
                            choices=get_clip_choices(model_path),
                            value=get_clip_choices(model_path)[0] if get_clip_choices(model_path) else "",
                            allow_custom_value=True,
gushiqiao's avatar
gushiqiao committed
1056
                        )
Gu Shiqiao's avatar
Gu Shiqiao committed
1057
1058
1059
1060
1061
                        vae_path_input = gr.Dropdown(
                            label="🎞️ VAE Decoder",
                            choices=get_vae_choices(model_path),
                            value=get_vae_choices(model_path)[0] if get_vae_choices(model_path) else "",
                            allow_custom_value=True,
gushiqiao's avatar
gushiqiao committed
1062
1063
                        )

Gu Shiqiao's avatar
Gu Shiqiao committed
1064
                    # Attention operator and quantization matrix multiplication operator
gushiqiao's avatar
gushiqiao committed
1065
1066
                    with gr.Row():
                        attention_type = gr.Dropdown(
Gu Shiqiao's avatar
Gu Shiqiao committed
1067
                            label="⚡ Attention Operator",
gushiqiao's avatar
gushiqiao committed
1068
                            choices=[op[1] for op in attn_op_choices],
Gu Shiqiao's avatar
Gu Shiqiao committed
1069
                            value=attn_op_choices[0][1] if attn_op_choices else "",
gushiqiao's avatar
gushiqiao committed
1070
                            info="Use appropriate attention operators to accelerate inference",
gushiqiao's avatar
gushiqiao committed
1071
1072
                        )
                        quant_op = gr.Dropdown(
gushiqiao's avatar
gushiqiao committed
1073
1074
1075
                            label="Quantization Matmul Operator",
                            choices=[op[1] for op in quant_op_choices],
                            value=quant_op_choices[0][1],
Gu Shiqiao's avatar
Gu Shiqiao committed
1076
                            info="Select quantization matrix multiplication operator to accelerate inference",
gushiqiao's avatar
gushiqiao committed
1077
                            interactive=True,
gushiqiao's avatar
gushiqiao committed
1078
                        )
Gu Shiqiao's avatar
Gu Shiqiao committed
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

                    # Determine if model is distill version
                    def is_distill_model(model_type, dit_path, high_noise_path):
                        """Determine if model is distill version based on model type and path"""
                        if model_type == "wan2.1":
                            check_name = dit_path.lower() if dit_path else ""
                        else:
                            check_name = high_noise_path.lower() if high_noise_path else ""
                        return "4step" in check_name

                    # Model type change event
                    def on_model_type_change(model_type, model_path_val):
                        if model_type == "wan2.2":
                            return gr.update(visible=False), gr.update(visible=True), gr.update()
                        else:
                            # Update wan2.1 Diffusion model options
                            dit_choices = get_dit_choices(model_path_val, "wan2.1")
                            return (
                                gr.update(visible=True),
                                gr.update(visible=False),
                                gr.update(choices=dit_choices, value=dit_choices[0] if dit_choices else ""),
                            )

                    model_type_input.change(
                        fn=on_model_type_change,
                        inputs=[model_type_input, model_path_input],
                        outputs=[wan21_row, wan22_row, dit_path_input],
                    )

                # Input parameters area
                with gr.Accordion("📥 Input Parameters", open=True, elem_classes=["input-params"]):
                    # Image input (shown for i2v)
                    with gr.Row(visible=True) as image_input_row:
                        image_path = gr.Image(
                            label="Input Image",
                            type="filepath",
                            height=300,
                            interactive=True,
gushiqiao's avatar
gushiqiao committed
1117
1118
                        )

Gu Shiqiao's avatar
Gu Shiqiao committed
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
                    # Task type change event
                    def on_task_type_change(task_type):
                        return gr.update(visible=(task_type == "i2v"))

                    task_type_input.change(
                        fn=on_task_type_change,
                        inputs=[task_type_input],
                        outputs=[image_input_row],
                    )

gushiqiao's avatar
gushiqiao committed
1129
                    with gr.Row():
Gu Shiqiao's avatar
Gu Shiqiao committed
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
                        with gr.Column():
                            prompt = gr.Textbox(
                                label="Prompt",
                                lines=3,
                                placeholder="Describe the video content...",
                                max_lines=5,
                            )
                        with gr.Column():
                            negative_prompt = gr.Textbox(
                                label="Negative Prompt",
                                lines=3,
                                placeholder="What you don't want to appear in the video...",
                                max_lines=5,
                                value="Camera shake, bright tones, overexposed, static, blurred details, subtitles, style, works, paintings, images, static, overall gray, worst quality, low quality, JPEG compression residue, ugly, incomplete, extra fingers, poorly drawn hands, poorly drawn faces, deformed, disfigured, misshapen limbs, fused fingers, still picture, messy background, three legs, many people in the background, walking backwards",
                            )
                        with gr.Column():
                            resolution = gr.Dropdown(
                                choices=[
                                    # 720p
                                    ("1280x720 (16:9, 720p)", "1280x720"),
                                    ("720x1280 (9:16, 720p)", "720x1280"),
                                    ("1280x544 (21:9, 720p)", "1280x544"),
                                    ("544x1280 (9:21, 720p)", "544x1280"),
                                    ("1104x832 (4:3, 720p)", "1104x832"),
                                    ("832x1104 (3:4, 720p)", "832x1104"),
                                    ("960x960 (1:1, 720p)", "960x960"),
                                    # 480p
                                    ("960x544 (16:9, 540p)", "960x544"),
                                    ("544x960 (9:16, 540p)", "544x960"),
                                    ("832x480 (16:9, 480p)", "832x480"),
                                    ("480x832 (9:16, 480p)", "480x832"),
                                    ("832x624 (4:3, 480p)", "832x624"),
                                    ("624x832 (3:4, 480p)", "624x832"),
                                    ("720x720 (1:1, 480p)", "720x720"),
                                    ("512x512 (1:1, 480p)", "512x512"),
                                ],
                                value="832x480",
                                label="Maximum Resolution",
                            )

                        with gr.Column(scale=9):
                            seed = gr.Slider(
                                label="Random Seed",
                                minimum=0,
                                maximum=MAX_NUMPY_SEED,
                                step=1,
                                value=generate_random_seed(),
                            )
                        with gr.Column():
                            default_dit = get_dit_choices(model_path, "wan2.1")[0] if get_dit_choices(model_path, "wan2.1") else ""
                            default_high_noise = get_high_noise_choices(model_path)[0] if get_high_noise_choices(model_path) else ""
                            default_is_distill = is_distill_model("wan2.1", default_dit, default_high_noise)

                            if default_is_distill:
                                infer_steps = gr.Slider(
                                    label="Inference Steps",
                                    minimum=1,
                                    maximum=100,
                                    step=1,
                                    value=4,
                                    info="Distill model inference steps default to 4.",
                                )
                            else:
                                infer_steps = gr.Slider(
                                    label="Inference Steps",
                                    minimum=1,
                                    maximum=100,
                                    step=1,
                                    value=40,
                                    info="Number of inference steps for video generation. Increasing steps may improve quality but reduce speed.",
                                )

                            # Dynamically update inference steps when model path changes
                            def update_infer_steps(model_type, dit_path, high_noise_path):
                                is_distill = is_distill_model(model_type, dit_path, high_noise_path)
                                if is_distill:
                                    return gr.update(minimum=1, maximum=100, value=4, interactive=True)
                                else:
                                    return gr.update(minimum=1, maximum=100, value=40, interactive=True)

                            # Listen to model path changes
                            dit_path_input.change(
                                fn=lambda mt, dp, hnp: update_infer_steps(mt, dp, hnp),
                                inputs=[model_type_input, dit_path_input, high_noise_path_input],
                                outputs=[infer_steps],
                            )
                            high_noise_path_input.change(
                                fn=lambda mt, dp, hnp: update_infer_steps(mt, dp, hnp),
                                inputs=[model_type_input, dit_path_input, high_noise_path_input],
                                outputs=[infer_steps],
                            )
                            model_type_input.change(
                                fn=lambda mt, dp, hnp: update_infer_steps(mt, dp, hnp),
                                inputs=[model_type_input, dit_path_input, high_noise_path_input],
                                outputs=[infer_steps],
                            )

                    # Set default CFG based on model class
                    # CFG scale factor: default to 1 for distill, otherwise 5
                    default_cfg_scale = 1 if default_is_distill else 5
                    # enable_cfg is not exposed to frontend, automatically set based on cfg_scale
                    # If cfg_scale == 1, then enable_cfg = False, otherwise enable_cfg = True
                    default_enable_cfg = False if default_cfg_scale == 1 else True
                    enable_cfg = gr.Checkbox(
                        label="Enable Classifier-Free Guidance",
                        value=default_enable_cfg,
                        visible=False,  # Hidden, not exposed to frontend
                    )

                    with gr.Row():
                        sample_shift = gr.Slider(
                            label="Distribution Shift",
                            value=5,
                            minimum=0,
                            maximum=10,
                            step=1,
                            info="Controls the degree of distribution shift for samples. Larger values indicate more significant shifts.",
gushiqiao's avatar
gushiqiao committed
1247
                        )
Gu Shiqiao's avatar
Gu Shiqiao committed
1248
1249
1250
1251
1252
1253
1254
                        cfg_scale = gr.Slider(
                            label="CFG Scale Factor",
                            minimum=1,
                            maximum=10,
                            step=1,
                            value=default_cfg_scale,
                            info="Controls the influence strength of the prompt. Higher values give more influence to the prompt. When value is 1, CFG is automatically disabled.",
gushiqiao's avatar
gushiqiao committed
1255
1256
                        )

Gu Shiqiao's avatar
Gu Shiqiao committed
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
                    # Update enable_cfg based on cfg_scale
                    def update_enable_cfg(cfg_scale_val):
                        """Automatically set enable_cfg based on cfg_scale value"""
                        if cfg_scale_val == 1:
                            return gr.update(value=False)
                        else:
                            return gr.update(value=True)

                    # Dynamically update CFG scale factor and enable_cfg when model path changes
                    def update_cfg_scale(model_type, dit_path, high_noise_path):
                        is_distill = is_distill_model(model_type, dit_path, high_noise_path)
                        if is_distill:
                            new_cfg_scale = 1
                        else:
                            new_cfg_scale = 5
                        new_enable_cfg = False if new_cfg_scale == 1 else True
                        return gr.update(value=new_cfg_scale), gr.update(value=new_enable_cfg)

                    dit_path_input.change(
                        fn=lambda mt, dp, hnp: update_cfg_scale(mt, dp, hnp),
                        inputs=[model_type_input, dit_path_input, high_noise_path_input],
                        outputs=[cfg_scale, enable_cfg],
                    )
                    high_noise_path_input.change(
                        fn=lambda mt, dp, hnp: update_cfg_scale(mt, dp, hnp),
                        inputs=[model_type_input, dit_path_input, high_noise_path_input],
                        outputs=[cfg_scale, enable_cfg],
                    )
                    model_type_input.change(
                        fn=lambda mt, dp, hnp: update_cfg_scale(mt, dp, hnp),
                        inputs=[model_type_input, dit_path_input, high_noise_path_input],
                        outputs=[cfg_scale, enable_cfg],
                    )

                    cfg_scale.change(
                        fn=update_enable_cfg,
                        inputs=[cfg_scale],
                        outputs=[enable_cfg],
                    )

gushiqiao's avatar
gushiqiao committed
1297
                    with gr.Row():
Gu Shiqiao's avatar
Gu Shiqiao committed
1298
1299
1300
1301
1302
1303
1304
                        fps = gr.Slider(
                            label="Frames Per Second (FPS)",
                            minimum=8,
                            maximum=30,
                            step=1,
                            value=16,
                            info="Frames per second of the video. Higher FPS results in smoother videos.",
gushiqiao's avatar
gushiqiao committed
1305
                        )
Gu Shiqiao's avatar
Gu Shiqiao committed
1306
1307
1308
1309
1310
1311
1312
                        num_frames = gr.Slider(
                            label="Total Frames",
                            minimum=16,
                            maximum=120,
                            step=1,
                            value=81,
                            info="Total number of frames in the video. More frames result in longer videos.",
gushiqiao's avatar
gushiqiao committed
1313
1314
                        )

Gu Shiqiao's avatar
Gu Shiqiao committed
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
                    save_result_path = gr.Textbox(
                        label="Output Video Path",
                        value=generate_unique_filename(output_dir),
                        info="Must include .mp4 extension. If left blank or using the default value, a unique filename will be automatically generated.",
                        visible=False,  # Hide output path, auto-generated
                    )

            with gr.Column(scale=4):
                with gr.Accordion("📤 Generated Video", open=True, elem_classes=["output-video"]):
                    output_video = gr.Video(
                        label="",
                        height=600,
                        autoplay=True,
                        show_label=False,
                    )

                    infer_btn = gr.Button("🎬 Generate Video", variant="primary", size="lg", elem_classes=["generate-btn"])

            rope_chunk = gr.Checkbox(label="Chunked Rotary Position Embedding", value=False, visible=False)
            rope_chunk_size = gr.Slider(label="Rotary Embedding Chunk Size", value=100, minimum=100, maximum=10000, step=100, visible=False)
            unload_modules = gr.Checkbox(label="Unload Modules", value=False, visible=False)
            clean_cuda_cache = gr.Checkbox(label="Clean CUDA Memory Cache", value=False, visible=False)
            cpu_offload = gr.Checkbox(label="CPU Offloading", value=False, visible=False)
            lazy_load = gr.Checkbox(label="Enable Lazy Loading", value=False, visible=False)
            offload_granularity = gr.Dropdown(label="Dit Offload Granularity", choices=["block", "phase"], value="phase", visible=False)
            t5_cpu_offload = gr.Checkbox(label="T5 CPU Offloading", value=False, visible=False)
            clip_cpu_offload = gr.Checkbox(label="CLIP CPU Offloading", value=False, visible=False)
            vae_cpu_offload = gr.Checkbox(label="VAE CPU Offloading", value=False, visible=False)
            use_tiling_vae = gr.Checkbox(label="VAE Tiling Inference", value=False, visible=False)

        resolution.change(
            fn=auto_configure,
            inputs=[resolution],
            outputs=[
                lazy_load,
                rope_chunk,
                rope_chunk_size,
                clean_cuda_cache,
                cpu_offload,
                offload_granularity,
                t5_cpu_offload,
                clip_cpu_offload,
                vae_cpu_offload,
                unload_modules,
                attention_type,
                quant_op,
                use_tiling_vae,
            ],
        )

        demo.load(
            fn=lambda res: auto_configure(res),
            inputs=[resolution],
            outputs=[
                lazy_load,
                rope_chunk,
                rope_chunk_size,
                clean_cuda_cache,
                cpu_offload,
                offload_granularity,
                t5_cpu_offload,
                clip_cpu_offload,
                vae_cpu_offload,
                unload_modules,
                attention_type,
                quant_op,
                use_tiling_vae,
            ],
        )

        infer_btn.click(
            fn=run_inference,
            inputs=[
                prompt,
                negative_prompt,
                save_result_path,
                infer_steps,
                num_frames,
                resolution,
                seed,
                sample_shift,
                enable_cfg,
                cfg_scale,
                fps,
                use_tiling_vae,
                lazy_load,
                cpu_offload,
                offload_granularity,
                t5_cpu_offload,
                clip_cpu_offload,
                vae_cpu_offload,
                unload_modules,
                attention_type,
                quant_op,
                rope_chunk,
                rope_chunk_size,
                clean_cuda_cache,
                model_path_input,
                model_type_input,
                task_type_input,
                dit_path_input,
                high_noise_path_input,
                low_noise_path_input,
                t5_path_input,
                clip_path_input,
                vae_path_input,
                image_path,
            ],
            outputs=output_video,
        )
gushiqiao's avatar
gushiqiao committed
1425

gushiqiao's avatar
gushiqiao committed
1426
    demo.launch(share=True, server_port=args.server_port, server_name=args.server_name, inbrowser=True, allowed_paths=[output_dir])
gushiqiao's avatar
gushiqiao committed
1427
1428
1429


if __name__ == "__main__":
Gu Shiqiao's avatar
Gu Shiqiao committed
1430
    parser = argparse.ArgumentParser(description="Lightweight Video Generation")
gushiqiao's avatar
gushiqiao committed
1431
1432
    parser.add_argument("--model_path", type=str, required=True, help="Model folder path")
    parser.add_argument("--server_port", type=int, default=7862, help="Server port")
Gu Shiqiao's avatar
Gu Shiqiao committed
1433
    parser.add_argument("--server_name", type=str, default="0.0.0.0", help="Server IP")
gushiqiao's avatar
gushiqiao committed
1434
    parser.add_argument("--output_dir", type=str, default="./outputs", help="Output video save directory")
gushiqiao's avatar
gushiqiao committed
1435
1436
    args = parser.parse_args()

Gu Shiqiao's avatar
Gu Shiqiao committed
1437
    global model_path, model_cls, output_dir
gushiqiao's avatar
gushiqiao committed
1438
    model_path = args.model_path
Gu Shiqiao's avatar
Gu Shiqiao committed
1439
    model_cls = "wan2.1"
gushiqiao's avatar
gushiqiao committed
1440
    output_dir = args.output_dir
gushiqiao's avatar
gushiqiao committed
1441

gushiqiao's avatar
gushiqiao committed
1442
    main()