-
Notifications
You must be signed in to change notification settings - Fork 183
New optimizations #608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
New optimizations #608
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @vaskers5, 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 significantly enhances the fastvideo
library by integrating a suite of new optimization techniques designed to improve the performance and output quality of video generation. It provides the foundational code for these optimizations, exposes them through configurable parameters and command-line interfaces, and includes practical examples to guide users on their application.
Highlights
- New Optimization Techniques: This PR introduces several new optimization techniques for video generation, including Skip-Layer Guidance, Normalized Attention Guidance (NAG), Dynamic Convolution Module (DCM), and TaylorSeer optimization. These techniques aim to improve the efficiency or quality of the video generation process.
- Configurability and CLI Arguments: New parameters have been added to the
PipelineConfig
class infastvideo/v1/configs/pipelines/base.py
to control these optimizations. Corresponding command-line arguments have also been added, allowing users to easily enable and fine-tune these features. - Integration into Denoising Stage: The core logic for applying these optimizations (
apply_normalized_attention_guidance
,apply_dcm
,apply_taylor_seer
) has been implemented infastvideo/v1/pipelines/stages/denoising.py
. These optimizations are now conditionally applied within the denoising loop, both when classifier-free guidance (CFG) is enabled and disabled. - New Examples: Two new example scripts have been added:
examples/inference/lora/wan_multi_lora_inference.py
demonstrates multi-LoRA inference, andexamples/inference/optimizations/optimized_wan_i2v_example.py
showcases how to enable and configure the newly introduced optimization parameters.
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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
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 issue 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 is currently in preview and 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 to provide feedback.
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
-
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. ↩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
The code changes introduce several optimizations, including Normalized Attention Guidance (NAG), Dynamic Convolution Module (DCM), and TaylorSeer optimization. The changes also include adding command-line arguments for these optimizations and incorporating them into the denoising loop. There are opportunities to improve code clarity and stability, particularly around device management, division by small numbers, and timestep accuracy.
if dt == 0: | ||
return tensor | ||
|
||
diff = (tensor - cache["prev"]) / dt |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
noise_pred = apply_taylor_seer( | ||
noise_pred, | ||
i, | ||
order=fastvideo_args.pipeline_config.taylor_seer_order, | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The apply_taylor_seer
function is called with the current step i
as an argument. Ensure that the step
argument in apply_taylor_seer
is correctly representing the timestep for the Taylor series approximation. Mismatched timesteps could lead to incorrect derivative calculations.
noise_pred = apply_taylor_seer(
noise_pred,
i, # verify this is the correct timestep
order=fastvideo_args.pipeline_config.taylor_seer_order,
)
return out.view_as(pos) | ||
|
||
|
||
_dcm_modules: dict[torch.device, tuple[torch.nn.Conv3d, torch.nn.Conv3d, torch.nn.Conv3d]] = {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using torch.nn.ModuleDict
instead of a regular Python dict to store the conv modules. This will properly register the modules with PyTorch and ensure they are moved to the correct device when the model is moved.
_dcm_modules: dict[torch.device, tuple[torch.nn.Conv3d, torch.nn.Conv3d, torch.nn.Conv3d]] = {} | |
from torch import nn | |
_dcm_modules: nn.ModuleDict[torch.device, tuple[torch.nn.Conv3d, torch.nn.Conv3d, torch.nn.Conv3d]] = nn.ModuleDict() |
cache = _taylor_cache.setdefault(tensor.device, { | ||
"prev": None, | ||
"prev_diff": None, | ||
"prev_step": None, | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The cache is initialized without a specified type. It's recommended to explicitly define the type for clarity and to avoid potential issues with type checking.
cache = _taylor_cache.setdefault(tensor.device, { | |
"prev": None, | |
"prev_diff": None, | |
"prev_step": None, | |
}) | |
cache: dict[str, Any] = _taylor_cache.setdefault(tensor.device, { | |
"prev": None, | |
"prev_diff": None, | |
"prev_step": None, | |
}) |
if fastvideo_args.pipeline_config.use_normalized_attention: | ||
noise_pred = apply_normalized_attention_guidance( | ||
noise_pred_text, | ||
noise_pred_uncond, | ||
nag_scale=fastvideo_args.pipeline_config.nag_scale * batch.guidance_scale, | ||
nag_tau=fastvideo_args.pipeline_config.nag_tau, | ||
nag_alpha=fastvideo_args.pipeline_config.nag_alpha, | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider moving the nag_scale
multiplication into the apply_normalized_attention_guidance
function to encapsulate the logic within the function itself.
noise_pred = apply_normalized_attention_guidance(
noise_pred_text,
noise_pred_uncond,
nag_scale=fastvideo_args.pipeline_config.nag_scale, # move multiplication here
nag_tau=fastvideo_args.pipeline_config.nag_tau,
nag_alpha=fastvideo_args.pipeline_config.nag_alpha,
)
nag_alpha=fastvideo_args.pipeline_config.nag_alpha, | ||
) | ||
if fastvideo_args.pipeline_config.use_dcm: | ||
noise_pred = apply_dcm(noise_pred) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding a check to ensure that apply_dcm
and apply_taylor_seer
are only applied if the corresponding flags in fastvideo_args.pipeline_config
are set to True
. This can prevent unnecessary computations when these optimizations are not enabled.
if fastvideo_args.pipeline_config.use_dcm:
noise_pred = apply_dcm(noise_pred)
if fastvideo_args.pipeline_config.use_taylor_seer:
noise_pred = apply_normalized_attention_guidance( | ||
noise_pred, | ||
nag_scale=fastvideo_args.pipeline_config.nag_scale, | ||
nag_tau=fastvideo_args.pipeline_config.nag_tau, | ||
nag_alpha=fastvideo_args.pipeline_config.nag_alpha, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider moving the nag_scale
multiplication into the apply_normalized_attention_guidance
function to encapsulate the logic within the function itself.
noise_pred = apply_normalized_attention_guidance(
noise_pred,
nag_scale=fastvideo_args.pipeline_config.nag_scale, # move multiplication here
nag_tau=fastvideo_args.pipeline_config.nag_tau,
nag_alpha=fastvideo_args.pipeline_config.nag_alpha,
)
Hi @vaskers5 Thanks for your contributions! First, could you please run Second, for the example scripts in your PR, it would be great if you could update them to be directly runnable. Do you have a set of lora adapters you combined and tested? For your optimizations example, below is the version I modified to run (feel free to just copy my version for your PR) and observe some artifacts with your current settings. Do you have some guidance on configuring the optimizations arguments that you've found in your experiments? Below is the output and script I used: An.astronaut.in.a.spacesuit.is.emerging.from.an.eggshell.mp4
|
generator.set_lora_adapter("lora1", "path/to/first_lora") | ||
generator.set_lora_adapter("lora2", "path/to/second_lora") | ||
generator.set_lora_adapter("lora3", "path/to/third_lora") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use real paths?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel examples/inference/lora/wan_lora_inference.py
already includes this
No description provided.