Skip to content

Add LoRA extraction, verification, and comparison scripts#865

Merged
SolitaryThinker merged 9 commits intohao-ai-lab:mainfrom
ShreejithSG:lora-extraction
Dec 9, 2025
Merged

Add LoRA extraction, verification, and comparison scripts#865
SolitaryThinker merged 9 commits intohao-ai-lab:mainfrom
ShreejithSG:lora-extraction

Conversation

@ShreejithSG
Copy link
Copy Markdown
Collaborator

This PR adds a set of scripts under scripts/lora_extraction for working with LoRA adapters in FastVideo models based on Wan 2.2 TI2V:

Description:

This PR adds a set of scripts under scripts/lora_extraction for working with LoRA adapters in FastVideo models based on Wan 2.2 TI2V:

  1. extract_lora.py – extracts LoRA weights from a fine-tuned model relative to a base model using low-rank decomposition. Supports checkpointing.
  2. verify_lora.py – reapplies extracted LoRA to the base model and compares per-layer transformer weights to verify extraction correctness.
  3. compare_lora_outputs.py – batch comparison of generated outputs between base, fine-tuned, and base+LoRA models. Outputs image/video metrics (MSE, MAE, LPIPS, SSIM).
  4. README.md – explains usage and workflow.

Notes:

Layer-level verification shows near-zero MSE (≈3.6e-08) between original and re-applied weights.

Preliminary perceptual checks show image-level differences (MSE ≈0.19, LPIPS ≈0.62, SSIM ≈0.128). The cause is under investigation.

Scripts are designed to be callable individually rather than a single pipeline, aligning with typical workflows (fused/finetuned checkpoint -> extract -> verify).

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @ShreejithSG, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a comprehensive set of utility scripts under "scripts/lora_extraction" to facilitate the management and validation of LoRA adapters within FastVideo models, specifically those built on the Wan 2.2 TI2V architecture. The scripts enable users to extract LoRA weights, verify their numerical accuracy, and perform perceptual comparisons of model outputs, providing essential tools for researchers and developers working with fine-tuned diffusion models.

Highlights

  • LoRA Extraction Script: Introduces "extract_lora.py" for extracting LoRA weights from fine-tuned models relative to a base model using low-rank decomposition, with checkpointing support for large model runs.
  • LoRA Verification Script: Adds "verify_lora.py" to reapply extracted LoRA to the base model and compare per-layer transformer weights to numerically verify extraction correctness, showing near-zero MSE.
  • Output Comparison Script: Includes "compare_lora_outputs.py" for batch comparison of generated outputs (images/videos) between base, fine-tuned, and base+LoRA models, providing metrics like MSE, MAE, LPIPS, and SSIM.
  • Documentation: A new "README.md" is added to provide a clear overview and usage instructions for the new LoRA utility scripts, explaining the workflow and default configurations.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a valuable set of scripts for LoRA extraction, verification, and comparison. The implementation is generally sound and serves its purpose well. My review focuses on enhancing the scripts' robustness, configurability, and adherence to Python best practices. The main suggestions include wrapping script logic in main functions guarded by if __name__ == "__main__":, using argparse for configuration instead of hardcoded constants, improving error handling with more specific exception catching, and addressing potential runtime errors. I have also noted a discrepancy between the documentation and script behavior for resuming extraction.

Comment thread scripts/lora_extraction/compare_lora_outputs.py Outdated
Comment thread scripts/lora_extraction/compare_lora_outputs.py Outdated
Comment thread scripts/lora_extraction/extract_lora.py Outdated
Comment on lines +7 to +12
# Configuration
BASE_MODEL = "Wan-AI/Wan2.2-TI2V-5B-Diffusers"
FINETUNED_MODEL = "FastVideo/FastWan2.2-TI2V-5B-FullAttn-Diffusers"
OUTPUT_PATH = "fastwan2.2_transformer_lora.pt"
CHECKPOINT_PATH = "lora_checkpoint.pt"
RANK = 16
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The script's logic is executed at the module's top level. This is not a good practice as it prevents the script from being imported without running its code, and makes it hard to reuse. All script logic should be encapsulated in a main function, called from an if __name__ == '__main__': block. This would also be a good place to introduce argparse to handle configuration parameters like model names, paths, and rank, instead of using global constants. This would also resolve the discrepancy with the README.md regarding the --resume flag.

Comment thread scripts/lora_extraction/extract_lora.py Outdated
Comment thread scripts/lora_extraction/extract_lora.py Outdated
Comment thread scripts/lora_extraction/README.md Outdated
Comment thread scripts/lora_extraction/compare_lora_outputs.py Outdated
Comment thread scripts/lora_extraction/compare_lora_outputs.py Outdated
Comment thread scripts/lora_extraction/extract_lora.py Outdated
Comment thread scripts/lora_extraction/verify_lora.py Outdated
@ShreejithSG
Copy link
Copy Markdown
Collaborator Author

@SolitaryThinker, I’ve raised a PR adding the LoRA extraction, verification, and comparison scripts under scripts/lora_extraction.

- Added test trigger in .buildkite/pipeline.yml for scripts/lora_extraction/
- Created fastvideo/tests/lora_extraction/test_lora_extraction.py
- Test validates: extract → merge → verify → inference (SSIM/LPIPS)
- Configured for L40S GPU with 45-min timeout
- Removed deprecated compare_lora_outputs.py
Copy link
Copy Markdown
Collaborator

@SolitaryThinker SolitaryThinker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, looks good overall

if ssim is None or lpips is None:
raise AssertionError("SSIM or LPIPS metrics missing")

print(f"\nMetrics: SSIM={ssim:.4f}, LPIPS={lpips:.4f}") No newline at end of file
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add newline char

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please check all the other files as well

@SolitaryThinker SolitaryThinker merged commit 92fb660 into hao-ai-lab:main Dec 9, 2025
1 check failed
shijiew555 pushed a commit to Gary-ChenJL/FastVideo that referenced this pull request Apr 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants