|
import torch |
|
import os |
|
import numpy as np |
|
import math |
|
import decord |
|
from tqdm import tqdm |
|
import pathlib |
|
from PIL import Image |
|
|
|
from diffusers_helper.models.hunyuan_video_packed import HunyuanVideoTransformer3DModelPacked |
|
from diffusers_helper.memory import DynamicSwapInstaller |
|
from diffusers_helper.utils import resize_and_center_crop |
|
from diffusers_helper.bucket_tools import find_nearest_bucket |
|
from diffusers_helper.hunyuan import vae_encode, vae_decode |
|
from .video_base_generator import VideoBaseModelGenerator |
|
|
|
class VideoF1ModelGenerator(VideoBaseModelGenerator): |
|
""" |
|
Model generator for the Video F1 (forward video) extension of the F1 HunyuanVideo model. |
|
These generators accept video input instead of a single image. |
|
""" |
|
|
|
def __init__(self, **kwargs): |
|
""" |
|
Initialize the Video F1 model generator. |
|
""" |
|
super().__init__(**kwargs) |
|
self.model_name = "Video F1" |
|
self.model_path = 'lllyasviel/FramePack_F1_I2V_HY_20250503' |
|
self.model_repo_id_for_cache = "models--lllyasviel--FramePack_F1_I2V_HY_20250503" |
|
|
|
def get_latent_paddings(self, total_latent_sections): |
|
""" |
|
Get the latent paddings for the Video model. |
|
|
|
Args: |
|
total_latent_sections: The total number of latent sections |
|
|
|
Returns: |
|
A list of latent paddings |
|
""" |
|
|
|
|
|
|
|
return [1] * (total_latent_sections - 1) + [0] |
|
|
|
def video_f1_prepare_clean_latents_and_indices(self, latent_window_size, video_latents, history_latents, num_cleaned_frames=5): |
|
""" |
|
Combined method to prepare clean latents and indices for the Video model. |
|
|
|
Args: |
|
Work in progress - better not to pass in latent_paddings and latent_padding. |
|
|
|
Returns: |
|
A tuple of (clean_latent_indices, latent_indices, clean_latent_2x_indices, clean_latent_4x_indices, clean_latents, clean_latents_2x, clean_latents_4x) |
|
""" |
|
|
|
num_clean_frames = num_cleaned_frames if num_cleaned_frames is not None else 5 |
|
|
|
|
|
|
|
start_latent = video_latents[:, :, -1:] |
|
|
|
available_frames = history_latents.shape[2] |
|
max_pixel_frames = min(latent_window_size * 4 - 3, available_frames * 4) |
|
adjusted_latent_frames = max(1, (max_pixel_frames + 3) // 4) |
|
|
|
effective_clean_frames = max(0, num_clean_frames - 1) if num_clean_frames > 1 else 0 |
|
effective_clean_frames = min(effective_clean_frames, available_frames - 2) if available_frames > 2 else 0 |
|
num_2x_frames = min(2, max(1, available_frames - effective_clean_frames - 1)) if available_frames > effective_clean_frames + 1 else 0 |
|
num_4x_frames = min(16, max(1, available_frames - effective_clean_frames - num_2x_frames)) if available_frames > effective_clean_frames + num_2x_frames else 0 |
|
|
|
total_context_frames = num_4x_frames + num_2x_frames + effective_clean_frames |
|
total_context_frames = min(total_context_frames, available_frames) |
|
|
|
indices = torch.arange(0, sum([1, num_4x_frames, num_2x_frames, effective_clean_frames, adjusted_latent_frames])).unsqueeze(0) |
|
clean_latent_indices_start, clean_latent_4x_indices, clean_latent_2x_indices, clean_latent_1x_indices, latent_indices = indices.split( |
|
[1, num_4x_frames, num_2x_frames, effective_clean_frames, adjusted_latent_frames], dim=1 |
|
) |
|
clean_latent_indices = torch.cat([clean_latent_indices_start, clean_latent_1x_indices], dim=1) |
|
|
|
|
|
fallback_frame_count = 2 |
|
context_frames = history_latents[:, :, -total_context_frames:, :, :] if total_context_frames > 0 else history_latents[:, :, :fallback_frame_count, :, :] |
|
if total_context_frames > 0: |
|
split_sizes = [num_4x_frames, num_2x_frames, effective_clean_frames] |
|
split_sizes = [s for s in split_sizes if s > 0] |
|
if split_sizes: |
|
splits = context_frames.split(split_sizes, dim=2) |
|
split_idx = 0 |
|
clean_latents_4x = splits[split_idx] if num_4x_frames > 0 else history_latents[:, :, :fallback_frame_count, :, :] |
|
if clean_latents_4x.shape[2] < 2: |
|
clean_latents_4x = torch.cat([clean_latents_4x, clean_latents_4x[:, :, -1:, :, :]], dim=2)[:, :, :2, :, :] |
|
split_idx += 1 if num_4x_frames > 0 else 0 |
|
clean_latents_2x = splits[split_idx] if num_2x_frames > 0 and split_idx < len(splits) else history_latents[:, :, :fallback_frame_count, :, :] |
|
if clean_latents_2x.shape[2] < 2: |
|
clean_latents_2x = torch.cat([clean_latents_2x, clean_latents_2x[:, :, -1:, :, :]], dim=2)[:, :, :2, :, :] |
|
split_idx += 1 if num_2x_frames > 0 else 0 |
|
clean_latents_1x = splits[split_idx] if effective_clean_frames > 0 and split_idx < len(splits) else history_latents[:, :, :fallback_frame_count, :, :] |
|
else: |
|
clean_latents_4x = clean_latents_2x = clean_latents_1x = history_latents[:, :, :fallback_frame_count, :, :] |
|
else: |
|
clean_latents_4x = clean_latents_2x = clean_latents_1x = history_latents[:, :, :fallback_frame_count, :, :] |
|
|
|
clean_latents = torch.cat([start_latent.to(history_latents), clean_latents_1x], dim=2) |
|
|
|
return clean_latent_indices, latent_indices, clean_latent_2x_indices, clean_latent_4x_indices, clean_latents, clean_latents_2x, clean_latents_4x |
|
|
|
def update_history_latents(self, history_latents, generated_latents): |
|
""" |
|
Forward Generation: Update the history latents with the generated latents for the Video F1 model. |
|
|
|
Args: |
|
history_latents: The history latents |
|
generated_latents: The generated latents |
|
|
|
Returns: |
|
The updated history latents |
|
""" |
|
|
|
|
|
|
|
return torch.cat([history_latents, generated_latents.to(history_latents)], dim=2) |
|
|
|
def get_real_history_latents(self, history_latents, total_generated_latent_frames): |
|
""" |
|
Get the real history latents for the backward Video model. For Video, this is the first |
|
`total_generated_latent_frames` frames of the history latents. |
|
|
|
Args: |
|
history_latents: The history latents |
|
total_generated_latent_frames: The total number of generated latent frames |
|
|
|
Returns: |
|
The real history latents |
|
""" |
|
|
|
return history_latents[:, :, -total_generated_latent_frames:, :, :] |
|
|
|
def update_history_pixels(self, history_pixels, current_pixels, overlapped_frames): |
|
""" |
|
Update the history pixels with the current pixels for the Video model. |
|
|
|
Args: |
|
history_pixels: The history pixels |
|
current_pixels: The current pixels |
|
overlapped_frames: The number of overlapped frames |
|
|
|
Returns: |
|
The updated history pixels |
|
""" |
|
from diffusers_helper.utils import soft_append_bcthw |
|
|
|
|
|
return soft_append_bcthw(history_pixels, current_pixels, overlapped_frames) |
|
|
|
def get_current_pixels(self, real_history_latents, section_latent_frames, vae): |
|
""" |
|
Get the current pixels for the Video model. |
|
|
|
Args: |
|
real_history_latents: The real history latents |
|
section_latent_frames: The number of section latent frames |
|
vae: The VAE model |
|
|
|
Returns: |
|
The current pixels |
|
""" |
|
|
|
return vae_decode(real_history_latents[:, :, -section_latent_frames:], vae).cpu() |
|
|
|
def format_position_description(self, total_generated_latent_frames, current_pos, original_pos, current_prompt): |
|
""" |
|
Format the position description for the Video model. |
|
|
|
Args: |
|
total_generated_latent_frames: The total number of generated latent frames |
|
current_pos: The current position in seconds (includes input video time) |
|
original_pos: The original position in seconds |
|
current_prompt: The current prompt |
|
|
|
Returns: |
|
The formatted position description |
|
""" |
|
|
|
return (f'Total generated frames: {int(max(0, total_generated_latent_frames * 4 - 3))}, ' |
|
f'Video length: {max(0, (total_generated_latent_frames * 4 - 3) / 30):.2f} seconds (FPS-30). ' |
|
f'Current position: {current_pos:.2f}s. ' |
|
f'using prompt: {current_prompt[:256]}...') |
|
|