|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +""" |
| 3 | +SSIM-based similarity tests for Lucy-Edit-Dev video editing. |
| 4 | +
|
| 5 | +Generates a video using the Lucy-Edit-Dev pipeline from a synthetic |
| 6 | +input video and compares the output against a pre-generated reference |
| 7 | +video using MS-SSIM. This tests self-consistency of the pipeline. |
| 8 | +
|
| 9 | +Note: num_inference_steps is reduced for CI speed (10 steps vs 50 recommended). |
| 10 | +""" |
| 11 | +import os |
| 12 | +import tempfile |
| 13 | + |
| 14 | +import imageio |
| 15 | +import numpy as np |
| 16 | +import pytest |
| 17 | +import torch |
| 18 | + |
| 19 | +from fastvideo import VideoGenerator |
| 20 | +from fastvideo.logger import init_logger |
| 21 | +from fastvideo.tests.utils import compute_video_ssim_torchvision, write_ssim_results |
| 22 | + |
| 23 | +logger = init_logger(__name__) |
| 24 | + |
| 25 | +# Device-specific reference folder |
| 26 | +device_name = torch.cuda.get_device_name() |
| 27 | +device_reference_folder_suffix = "_reference_videos" |
| 28 | + |
| 29 | +if "A40" in device_name: |
| 30 | + device_reference_folder = "A40" + device_reference_folder_suffix |
| 31 | +elif "L40S" in device_name: |
| 32 | + device_reference_folder = "L40S" + device_reference_folder_suffix |
| 33 | +elif "H100" in device_name: |
| 34 | + device_reference_folder = "H100" + device_reference_folder_suffix |
| 35 | +else: |
| 36 | + logger.warning(f"Unsupported device for ssim tests: {device_name}") |
| 37 | + |
| 38 | +# ============================================================================= |
| 39 | +# Lucy-Edit-Dev Parameters |
| 40 | +# ============================================================================= |
| 41 | +LUCY_EDIT_PARAMS = { |
| 42 | + "num_gpus": 1, |
| 43 | + "model_path": "official_weights/Lucy-Edit-Dev", |
| 44 | + "height": 480, |
| 45 | + "width": 832, |
| 46 | + "num_frames": 17, |
| 47 | + "num_inference_steps": 10, # Reduced from 50 for CI speed |
| 48 | + "guidance_scale": 5.0, |
| 49 | + "fps": 24, |
| 50 | + "seed": 42, |
| 51 | +} |
| 52 | + |
| 53 | +# Test prompt |
| 54 | +LUCY_EDIT_TEST_PROMPTS = [ |
| 55 | + "Change the shirt to a bright red leather jacket with a glossy finish, " |
| 56 | + "add aviator sunglasses.", |
| 57 | +] |
| 58 | + |
| 59 | + |
| 60 | +def _create_synthetic_video(num_frames, height, width, seed=0): |
| 61 | + """Create a deterministic synthetic video for the editing pipeline.""" |
| 62 | + rng = np.random.RandomState(seed) |
| 63 | + tmp = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) |
| 64 | + tmp.close() |
| 65 | + |
| 66 | + writer = imageio.get_writer(tmp.name, fps=24, codec="libx264", |
| 67 | + output_params=["-pix_fmt", "yuv420p"]) |
| 68 | + for _ in range(num_frames): |
| 69 | + frame = rng.randint(0, 256, (height, width, 3), dtype=np.uint8) |
| 70 | + writer.append_data(frame) |
| 71 | + writer.close() |
| 72 | + |
| 73 | + return tmp.name |
| 74 | + |
| 75 | + |
| 76 | +@pytest.mark.parametrize("prompt", LUCY_EDIT_TEST_PROMPTS) |
| 77 | +@pytest.mark.parametrize("ATTENTION_BACKEND", ["TORCH_SDPA"]) |
| 78 | +def test_lucy_edit_similarity(prompt: str, ATTENTION_BACKEND: str): |
| 79 | + """ |
| 80 | + Test Lucy-Edit-Dev inference and compare output to reference videos |
| 81 | + using MS-SSIM. |
| 82 | +
|
| 83 | + Parameters derived from examples/inference/basic/basic_lucy_edit.py |
| 84 | + """ |
| 85 | + os.environ["FASTVIDEO_ATTENTION_BACKEND"] = ATTENTION_BACKEND |
| 86 | + torch.backends.cuda.enable_flash_sdp(False) |
| 87 | + torch.backends.cuda.enable_mem_efficient_sdp(False) |
| 88 | + torch.backends.cuda.enable_math_sdp(True) |
| 89 | + |
| 90 | + weights_path = LUCY_EDIT_PARAMS["model_path"] |
| 91 | + if not os.path.isdir(weights_path): |
| 92 | + pytest.skip( |
| 93 | + f"Missing Lucy-Edit-Dev weights at {weights_path}. " |
| 94 | + "Download with: python scripts/huggingface/download_hf.py " |
| 95 | + "--repo_id decart-ai/Lucy-Edit-Dev " |
| 96 | + "--local_dir official_weights/Lucy-Edit-Dev --repo_type model" |
| 97 | + ) |
| 98 | + |
| 99 | + script_dir = os.path.dirname(os.path.abspath(__file__)) |
| 100 | + model_id = "Lucy-Edit-Dev" |
| 101 | + |
| 102 | + output_dir = os.path.join(script_dir, "generated_videos", model_id, |
| 103 | + ATTENTION_BACKEND) |
| 104 | + output_video_name = f"{prompt[:100].strip().rstrip(chr(46))}.mp4" |
| 105 | + os.makedirs(output_dir, exist_ok=True) |
| 106 | + |
| 107 | + # Create deterministic synthetic input video |
| 108 | + video_path = _create_synthetic_video( |
| 109 | + LUCY_EDIT_PARAMS["num_frames"], |
| 110 | + LUCY_EDIT_PARAMS["height"], |
| 111 | + LUCY_EDIT_PARAMS["width"], |
| 112 | + seed=0, |
| 113 | + ) |
| 114 | + |
| 115 | + try: |
| 116 | + generator = VideoGenerator.from_pretrained( |
| 117 | + model_path=weights_path, |
| 118 | + num_gpus=LUCY_EDIT_PARAMS["num_gpus"], |
| 119 | + use_fsdp_inference=False, |
| 120 | + dit_cpu_offload=False, |
| 121 | + vae_cpu_offload=False, |
| 122 | + text_encoder_cpu_offload=False, |
| 123 | + pin_cpu_memory=False, |
| 124 | + ) |
| 125 | + |
| 126 | + generator.generate_video( |
| 127 | + prompt, |
| 128 | + video_path=video_path, |
| 129 | + output_path=output_dir, |
| 130 | + output_video_name=output_video_name, |
| 131 | + save_video=True, |
| 132 | + height=LUCY_EDIT_PARAMS["height"], |
| 133 | + width=LUCY_EDIT_PARAMS["width"], |
| 134 | + num_frames=LUCY_EDIT_PARAMS["num_frames"], |
| 135 | + num_inference_steps=LUCY_EDIT_PARAMS["num_inference_steps"], |
| 136 | + guidance_scale=LUCY_EDIT_PARAMS["guidance_scale"], |
| 137 | + fps=LUCY_EDIT_PARAMS["fps"], |
| 138 | + seed=LUCY_EDIT_PARAMS["seed"], |
| 139 | + ) |
| 140 | + generator.shutdown() |
| 141 | + finally: |
| 142 | + os.unlink(video_path) |
| 143 | + |
| 144 | + generated_video_path = os.path.join(output_dir, output_video_name) |
| 145 | + assert os.path.exists(generated_video_path), ( |
| 146 | + f"Output video was not generated at {generated_video_path}" |
| 147 | + ) |
| 148 | + |
| 149 | + # Find reference video |
| 150 | + reference_folder = os.path.join( |
| 151 | + script_dir, device_reference_folder, model_id, ATTENTION_BACKEND |
| 152 | + ) |
| 153 | + if not os.path.exists(reference_folder): |
| 154 | + pytest.skip( |
| 155 | + f"Reference video folder does not exist: {reference_folder}. " |
| 156 | + "Generate reference videos first." |
| 157 | + ) |
| 158 | + |
| 159 | + reference_video_name = None |
| 160 | + for filename in os.listdir(reference_folder): |
| 161 | + if filename.endswith(".mp4") and prompt[:100].strip() in filename: |
| 162 | + reference_video_name = filename |
| 163 | + break |
| 164 | + |
| 165 | + if not reference_video_name: |
| 166 | + pytest.skip( |
| 167 | + f"Reference video not found for prompt: {prompt[:50]}... " |
| 168 | + f"with backend: {ATTENTION_BACKEND}" |
| 169 | + ) |
| 170 | + |
| 171 | + reference_video_path = os.path.join(reference_folder, reference_video_name) |
| 172 | + |
| 173 | + logger.info( |
| 174 | + f"Computing SSIM between {reference_video_path} and {generated_video_path}" |
| 175 | + ) |
| 176 | + ssim_values = compute_video_ssim_torchvision( |
| 177 | + reference_video_path, generated_video_path, use_ms_ssim=True |
| 178 | + ) |
| 179 | + |
| 180 | + mean_ssim = ssim_values[0] |
| 181 | + logger.info(f"SSIM mean value: {mean_ssim}") |
| 182 | + |
| 183 | + write_ssim_results( |
| 184 | + output_dir, ssim_values, reference_video_path, generated_video_path, |
| 185 | + LUCY_EDIT_PARAMS["num_inference_steps"], prompt |
| 186 | + ) |
| 187 | + |
| 188 | + min_acceptable_ssim = 0.80 |
| 189 | + assert mean_ssim >= min_acceptable_ssim, ( |
| 190 | + f"SSIM value {mean_ssim} is below threshold {min_acceptable_ssim} " |
| 191 | + f"for {model_id} with backend {ATTENTION_BACKEND}" |
| 192 | + ) |
0 commit comments