|
1 | | -"""Test LoRA extraction, merging, and verification pipeline.""" |
2 | | -import sys |
| 1 | +"""Test extraction through the real FastVideo LoRA loading path.""" |
3 | 2 | from pathlib import Path |
| 3 | +import sys |
| 4 | +import tempfile |
4 | 5 |
|
5 | 6 | import pytest |
6 | 7 | import torch |
7 | 8 |
|
| 9 | +from fastvideo import VideoGenerator |
| 10 | +from fastvideo.api import ComponentConfig, EngineConfig, GeneratorConfig, OffloadConfig, ParallelismConfig, PipelineSelection |
| 11 | + |
8 | 12 | # Add scripts/lora_extraction to path for imports |
9 | 13 | repo_root = Path(__file__).parents[3] |
10 | 14 | lora_scripts = repo_root / "scripts" / "lora_extraction" |
11 | 15 | sys.path.insert(0, str(lora_scripts)) |
12 | 16 |
|
13 | | -# Import the core functions |
14 | | -from extract_lora import extract_lora_adapter |
15 | | -from merge_lora import merge_lora |
16 | | -from verify_lora import main as verify_lora_main |
| 17 | +from extract_lora import extract_lora_adapter # noqa: E402 |
17 | 18 |
|
18 | 19 |
|
19 | | -@pytest.mark.parametrize( |
20 | | - "extraction_device", |
21 | | - [ |
22 | | - pytest.param("cpu", id="cpu"), |
23 | | - pytest.param( |
24 | | - "cuda:0", |
25 | | - id="gpu", |
26 | | - marks=pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA is unavailable"), |
27 | | - ), |
28 | | - ], |
29 | | -) |
30 | | -def test_lora_extraction_pipeline(extraction_device: str): |
31 | | - """Test the existing Wan2.2 extraction workflow on CPU and GPU.""" |
32 | | - import tempfile |
| 20 | +def _collect_lora_application(worker) -> dict[str, object]: |
| 21 | + """Inspect the worker after the constructor applied its adapter.""" |
| 22 | + pipeline = worker.pipeline |
| 23 | + adapter = pipeline.lora_adapters[pipeline.cur_adapter_name] |
| 24 | + available: set[str] = set() |
| 25 | + adapted = 0 |
| 26 | + for transformer_layers in pipeline.lora_layers.values(): |
| 27 | + for _, layers in transformer_layers.lora_layers_by_block(): |
| 28 | + for name, layer in layers.items(): |
| 29 | + available.update((name + ".lora_A", name + ".lora_B", name + ".lora_alpha")) |
| 30 | + if layer.lora_A is not None and layer.lora_B is not None and not layer.disable_lora: |
| 31 | + adapted += 1 |
| 32 | + unmatched = sorted(set(adapter) - available) |
| 33 | + return { |
| 34 | + "adapted": adapted, |
| 35 | + "pipeline": type(pipeline).__name__, |
| 36 | + "unmatched": unmatched, |
| 37 | + } |
33 | 38 |
|
34 | | - # Use temp directory for outputs to avoid polluting repo |
35 | | - with tempfile.TemporaryDirectory() as tmpdir: |
36 | | - tmpdir_path = Path(tmpdir) |
37 | | - device_name = extraction_device.replace(":", "-") |
38 | | - adapter_path = tmpdir_path / f"adapter_r16_{device_name}.safetensors" |
39 | | - merged_dir = tmpdir_path / f"merged_r16_{device_name}" |
40 | 39 |
|
41 | | - # 1. Extract rank-16 adapter |
42 | | - print(f"\nExtracting rank-16 adapter on {extraction_device}") |
| 40 | +@pytest.mark.skipif(not torch.cuda.is_available(), reason="Wan2.2 integration requires a CUDA GPU") |
| 41 | +def test_lora_extraction_pipeline() -> None: |
| 42 | + """Extract Wan2.2 on a GPU and require every factor to reach the DMD pipeline.""" |
| 43 | + base = "Wan-AI/Wan2.2-TI2V-5B-Diffusers" |
| 44 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 45 | + adapter_path = Path(tmpdir) / "adapter_r16.safetensors" |
43 | 46 | extract_lora_adapter( |
44 | | - base="Wan-AI/Wan2.2-TI2V-5B-Diffusers", |
| 47 | + base=base, |
45 | 48 | ft="FastVideo/FastWan2.2-TI2V-5B-FullAttn-Diffusers", |
46 | 49 | out=str(adapter_path), |
47 | 50 | rank=16, |
48 | 51 | load_mode="indexed", |
49 | | - device=extraction_device, |
| 52 | + device="cuda:0", |
50 | 53 | svd_method="exact", |
| 54 | + exact_tensor_patterns=(r"^condition_embedder\.", r"^proj_out\.weight$"), |
51 | 55 | ) |
52 | | - assert adapter_path.exists(), "Adapter file was not created" |
53 | | - |
54 | | - # 2. Merge adapter |
55 | | - print("\nMerging adapter") |
56 | | - merge_lora( |
57 | | - base="Wan-AI/Wan2.2-TI2V-5B-Diffusers", |
58 | | - adapter=str(adapter_path), |
59 | | - ft="FastVideo/FastWan2.2-TI2V-5B-FullAttn-Diffusers", |
60 | | - output=str(merged_dir), |
61 | | - ) |
62 | | - assert merged_dir.exists(), "Merged model directory was not created" |
63 | 56 |
|
64 | | - # 3. Verify numerical accuracy |
65 | | - print("\nVerifying merged model") |
66 | | - # verify_lora uses sys.argv, so we need to mock it |
67 | | - old_argv = sys.argv |
| 57 | + generator = VideoGenerator.from_config( |
| 58 | + GeneratorConfig( |
| 59 | + model_path=base, |
| 60 | + pipeline=PipelineSelection( |
| 61 | + components=ComponentConfig( |
| 62 | + lora_path=str(adapter_path), |
| 63 | + override_pipeline_cls_name="WanDMDPipeline", |
| 64 | + ), |
| 65 | + experimental={ |
| 66 | + "dmd_denoising_steps": [1000, 757, 522], |
| 67 | + "flow_shift": 5.0, |
| 68 | + }, |
| 69 | + ), |
| 70 | + engine=EngineConfig( |
| 71 | + num_gpus=1, |
| 72 | + use_fsdp_inference=False, |
| 73 | + parallelism=ParallelismConfig(tp_size=1, sp_size=1), |
| 74 | + offload=OffloadConfig( |
| 75 | + dit=False, |
| 76 | + dit_layerwise=False, |
| 77 | + text_encoder=True, |
| 78 | + vae=True, |
| 79 | + pin_cpu_memory=False, |
| 80 | + ), |
| 81 | + ), |
| 82 | + )) |
68 | 83 | try: |
69 | | - sys.argv = [ |
70 | | - "verify_lora.py", |
71 | | - "--merged", |
72 | | - str(merged_dir), |
73 | | - "--ft", |
74 | | - "FastVideo/FastWan2.2-TI2V-5B-FullAttn-Diffusers", |
75 | | - ] |
76 | | - verify_lora_main() |
| 84 | + summaries = generator.executor.collective_rpc(_collect_lora_application) |
77 | 85 | finally: |
78 | | - sys.argv = old_argv |
| 86 | + generator.shutdown() |
79 | 87 |
|
80 | | - print("\nLoRA extraction pipeline test PASSED") |
| 88 | + assert summaries == [{ |
| 89 | + "adapted": 300, |
| 90 | + "pipeline": "WanDMDPipeline", |
| 91 | + "unmatched": [], |
| 92 | + }] |
0 commit comments