-
Notifications
You must be signed in to change notification settings - Fork 184
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from fastvideo import VideoGenerator | ||
|
||
OUTPUT_PATH = "./multi_lora" | ||
|
||
|
||
def main(): | ||
# Create a generator for WanVideo2.1 I2V | ||
generator = VideoGenerator.from_pretrained( | ||
"Wan-AI/Wan2.1-I2V-14B-480P", | ||
num_gpus=1, | ||
) | ||
|
||
# Load three LoRA adapters into the pipeline | ||
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") | ||
|
||
# The last call activates lora3. Generate a video with it | ||
prompt = "An astronaut explores a strange new world, cinematic scene" | ||
generator.generate_video(prompt, output_path=OUTPUT_PATH, save_video=True) | ||
|
||
# Switch to lora1 and generate another video | ||
generator.set_lora_adapter("lora1") | ||
generator.generate_video(prompt, output_path=OUTPUT_PATH, save_video=True) | ||
|
||
# Switch to lora2 and generate one more video | ||
generator.set_lora_adapter("lora2") | ||
generator.generate_video(prompt, output_path=OUTPUT_PATH, save_video=True) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from fastvideo import VideoGenerator | ||
from fastvideo.v1.configs.sample import SamplingParam | ||
|
||
|
||
OUTPUT_PATH = "./optimized_output" | ||
|
||
|
||
def main(): | ||
"""Run WanVideo2.1 I2V pipeline with all optimizations enabled.""" | ||
generator = VideoGenerator.from_pretrained( | ||
"Wan-AI/Wan2.1-I2V-14B-480P", | ||
num_gpus=1, | ||
skip_layer_guidance=0.2, | ||
use_normalized_attention=True, | ||
nag_scale=1.5, | ||
nag_tau=2.5, | ||
nag_alpha=0.125, | ||
use_dcm=True, | ||
use_taylor_seer=True, | ||
taylor_seer_order=2, | ||
) | ||
|
||
sampling = SamplingParam.from_pretrained("Wan-AI/Wan2.1-I2V-14B-480P") | ||
|
||
prompt = "A lone explorer crosses a vast alien desert under twin moons" | ||
generator.generate_video( | ||
prompt, | ||
sampling_param=sampling, | ||
output_path=OUTPUT_PATH, | ||
save_video=True, | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -45,6 +45,99 @@ | |||||||||||||||||||||
logger = init_logger(__name__) | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
def apply_normalized_attention_guidance( | ||||||||||||||||||||||
pos: torch.Tensor, | ||||||||||||||||||||||
neg: torch.Tensor | None = None, | ||||||||||||||||||||||
nag_scale: float = 1.5, | ||||||||||||||||||||||
nag_tau: float = 2.5, | ||||||||||||||||||||||
nag_alpha: float = 0.125, | ||||||||||||||||||||||
) -> torch.Tensor: | ||||||||||||||||||||||
"""Apply Normalized Attention Guidance (NAG) to noise predictions. | ||||||||||||||||||||||
|
||||||||||||||||||||||
This implementation follows the formula from the official NAG repository | ||||||||||||||||||||||
and operates on the positive and negative noise predictions. | ||||||||||||||||||||||
""" | ||||||||||||||||||||||
|
||||||||||||||||||||||
if neg is None: | ||||||||||||||||||||||
flat = pos.flatten(2) | ||||||||||||||||||||||
mean = flat.mean(dim=-1, keepdim=True) | ||||||||||||||||||||||
var = flat.var(dim=-1, unbiased=False, keepdim=True) | ||||||||||||||||||||||
normalized = (flat - mean) / (var + 1e-6).sqrt() | ||||||||||||||||||||||
return normalized.view_as(pos) | ||||||||||||||||||||||
|
||||||||||||||||||||||
pos_flat = pos.flatten(2) | ||||||||||||||||||||||
neg_flat = neg.flatten(2) | ||||||||||||||||||||||
|
||||||||||||||||||||||
guidance = pos_flat * nag_scale - neg_flat * (nag_scale - 1) | ||||||||||||||||||||||
norm_pos = pos_flat.norm(p=2, dim=-1, keepdim=True) | ||||||||||||||||||||||
norm_guidance = guidance.norm(p=2, dim=-1, keepdim=True) | ||||||||||||||||||||||
scale = norm_guidance / (norm_pos + 1e-7) | ||||||||||||||||||||||
guidance = guidance * torch.minimum(scale, scale.new_ones(1) * nag_tau) / ( | ||||||||||||||||||||||
scale + 1e-7 | ||||||||||||||||||||||
) | ||||||||||||||||||||||
|
||||||||||||||||||||||
out = guidance * nag_alpha + pos_flat * (1 - nag_alpha) | ||||||||||||||||||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more. Consider using
Suggested change
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
def apply_dcm(tensor: torch.Tensor) -> torch.Tensor: | ||||||||||||||||||||||
"""Apply Dynamic Convolution Module (DCM).""" | ||||||||||||||||||||||
global _dcm_modules | ||||||||||||||||||||||
conv_offset, conv_weight, conv_gate = _dcm_modules.get(tensor.device, (None, None, None)) | ||||||||||||||||||||||
if conv_offset is None: | ||||||||||||||||||||||
channels = tensor.size(1) | ||||||||||||||||||||||
conv_offset = torch.nn.Conv3d(channels, channels, kernel_size=3, padding=1, bias=False).to( | ||||||||||||||||||||||
tensor.device, tensor.dtype | ||||||||||||||||||||||
) | ||||||||||||||||||||||
conv_weight = torch.nn.Conv3d(channels, channels, kernel_size=3, padding=1, bias=False).to( | ||||||||||||||||||||||
tensor.device, tensor.dtype | ||||||||||||||||||||||
) | ||||||||||||||||||||||
conv_gate = torch.nn.Conv3d(channels, channels, kernel_size=3, padding=1).to( | ||||||||||||||||||||||
tensor.device, tensor.dtype | ||||||||||||||||||||||
) | ||||||||||||||||||||||
_dcm_modules[tensor.device] = (conv_offset, conv_weight, conv_gate) | ||||||||||||||||||||||
|
||||||||||||||||||||||
offset = conv_offset(tensor) | ||||||||||||||||||||||
out = conv_weight(tensor + offset) | ||||||||||||||||||||||
gate = torch.sigmoid(conv_gate(tensor)) | ||||||||||||||||||||||
return tensor + gate * out | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
_taylor_cache: dict[torch.device, dict[str, Any]] = {} | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
def apply_taylor_seer(tensor: torch.Tensor, step: int, order: int = 2) -> torch.Tensor: | ||||||||||||||||||||||
"""Apply TaylorSeer optimization using a simple derivative cache.""" | ||||||||||||||||||||||
cache = _taylor_cache.setdefault(tensor.device, { | ||||||||||||||||||||||
"prev": None, | ||||||||||||||||||||||
"prev_diff": None, | ||||||||||||||||||||||
"prev_step": None, | ||||||||||||||||||||||
}) | ||||||||||||||||||||||
Comment on lines
+114
to
+118
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Suggested change
|
||||||||||||||||||||||
|
||||||||||||||||||||||
if cache["prev"] is None: | ||||||||||||||||||||||
cache["prev"] = tensor.detach() | ||||||||||||||||||||||
cache["prev_step"] = step | ||||||||||||||||||||||
return tensor | ||||||||||||||||||||||
|
||||||||||||||||||||||
dt = step - cache["prev_step"] | ||||||||||||||||||||||
if dt == 0: | ||||||||||||||||||||||
return tensor | ||||||||||||||||||||||
|
||||||||||||||||||||||
diff = (tensor - cache["prev"]) / dt | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||||||||
result = cache["prev"] + diff * dt | ||||||||||||||||||||||
if order >= 2 and cache["prev_diff"] is not None: | ||||||||||||||||||||||
second = (diff - cache["prev_diff"]) / dt | ||||||||||||||||||||||
result = result + 0.5 * second * dt * dt | ||||||||||||||||||||||
|
||||||||||||||||||||||
cache["prev"] = tensor.detach() | ||||||||||||||||||||||
cache["prev_diff"] = diff | ||||||||||||||||||||||
cache["prev_step"] = step | ||||||||||||||||||||||
return result | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
class DenoisingStage(PipelineStage): | ||||||||||||||||||||||
""" | ||||||||||||||||||||||
Stage for running the denoising loop in diffusion pipelines. | ||||||||||||||||||||||
|
@@ -83,6 +176,9 @@ def forward( | |||||||||||||||||||||
Returns: | ||||||||||||||||||||||
The batch with denoised latents. | ||||||||||||||||||||||
""" | ||||||||||||||||||||||
# Reset caches for optional optimizations | ||||||||||||||||||||||
_taylor_cache.clear() | ||||||||||||||||||||||
|
||||||||||||||||||||||
# Prepare extra step kwargs for scheduler | ||||||||||||||||||||||
extra_step_kwargs = self.prepare_extra_func_kwargs( | ||||||||||||||||||||||
self.scheduler.step, | ||||||||||||||||||||||
|
@@ -264,8 +360,31 @@ def forward( | |||||||||||||||||||||
**neg_cond_kwargs, | ||||||||||||||||||||||
) | ||||||||||||||||||||||
noise_pred_text = noise_pred | ||||||||||||||||||||||
noise_pred = noise_pred_uncond + batch.guidance_scale * ( | ||||||||||||||||||||||
noise_pred_text - noise_pred_uncond) | ||||||||||||||||||||||
if fastvideo_args.pipeline_config.skip_layer_guidance and ( | ||||||||||||||||||||||
i / len(timesteps) | ||||||||||||||||||||||
< fastvideo_args.pipeline_config.skip_layer_guidance | ||||||||||||||||||||||
): | ||||||||||||||||||||||
noise_pred = noise_pred_text | ||||||||||||||||||||||
else: | ||||||||||||||||||||||
noise_pred = noise_pred_uncond + batch.guidance_scale * ( | ||||||||||||||||||||||
noise_pred_text - noise_pred_uncond) | ||||||||||||||||||||||
|
||||||||||||||||||||||
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, | ||||||||||||||||||||||
) | ||||||||||||||||||||||
Comment on lines
+372
to
+379
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider moving the 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,
) |
||||||||||||||||||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding a check to ensure that if fastvideo_args.pipeline_config.use_dcm:
noise_pred = apply_dcm(noise_pred)
if fastvideo_args.pipeline_config.use_taylor_seer: |
||||||||||||||||||||||
if fastvideo_args.pipeline_config.use_taylor_seer: | ||||||||||||||||||||||
noise_pred = apply_taylor_seer( | ||||||||||||||||||||||
noise_pred, | ||||||||||||||||||||||
i, | ||||||||||||||||||||||
order=fastvideo_args.pipeline_config.taylor_seer_order, | ||||||||||||||||||||||
) | ||||||||||||||||||||||
Comment on lines
+383
to
+387
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The noise_pred = apply_taylor_seer(
noise_pred,
i, # verify this is the correct timestep
order=fastvideo_args.pipeline_config.taylor_seer_order,
) |
||||||||||||||||||||||
|
||||||||||||||||||||||
# Apply guidance rescale if needed | ||||||||||||||||||||||
if batch.guidance_rescale > 0.0: | ||||||||||||||||||||||
|
@@ -276,6 +395,23 @@ def forward( | |||||||||||||||||||||
guidance_rescale=batch.guidance_rescale, | ||||||||||||||||||||||
) | ||||||||||||||||||||||
|
||||||||||||||||||||||
if not batch.do_classifier_free_guidance: | ||||||||||||||||||||||
if fastvideo_args.pipeline_config.use_normalized_attention: | ||||||||||||||||||||||
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, | ||||||||||||||||||||||
Comment on lines
+400
to
+404
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider moving the 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,
) |
||||||||||||||||||||||
) | ||||||||||||||||||||||
if fastvideo_args.pipeline_config.use_dcm: | ||||||||||||||||||||||
noise_pred = apply_dcm(noise_pred) | ||||||||||||||||||||||
if fastvideo_args.pipeline_config.use_taylor_seer: | ||||||||||||||||||||||
noise_pred = apply_taylor_seer( | ||||||||||||||||||||||
noise_pred, | ||||||||||||||||||||||
i, | ||||||||||||||||||||||
order=fastvideo_args.pipeline_config.taylor_seer_order, | ||||||||||||||||||||||
) | ||||||||||||||||||||||
|
||||||||||||||||||||||
# Compute the previous noisy sample | ||||||||||||||||||||||
latents = self.scheduler.step(noise_pred, | ||||||||||||||||||||||
t, | ||||||||||||||||||||||
|
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