-
Notifications
You must be signed in to change notification settings - Fork 440
[feat]: Stable-Video-Infinity inference on Wan 2.1 I2V 14B 480P #1344
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
Open
H1yori233
wants to merge
15
commits into
hao-ai-lab:main
Choose a base branch
from
H1yori233:svi
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8f2a879
svi inference
H1yori233 2f61f43
inference with model variant configuration
H1yori233 869bd7c
motion frame handling
H1yori233 f8f49e7
update test, merge stage
H1yori233 189ab41
small fix
H1yori233 332f918
align scheduler
H1yori233 815da1f
Merge remote-tracking branch 'origin/main' into svi
H1yori233 971e230
Merge remote-tracking branch 'origin/main' into svi
H1yori233 d2c1944
some fix
H1yori233 4c312ad
Merge upstream/main into svi
H1yori233 5070bb3
Fix SVI multi-clip integration and review issues
H1yori233 083ad8a
Apply project YAPF formatting
H1yori233 cb3b9cf
Align SVI inference with official recipes
H1yori233 60b276d
Apply YAPF formatting to SVI stage
H1yori233 13f036b
Merge upstream/main into svi
H1yori233 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| from fastvideo import VideoGenerator | ||
|
|
||
| MODEL_VARIANT = "shot" | ||
|
|
||
| VARIANT_CONFIG = { | ||
| "shot": { | ||
| "lora_path": "vita-video-gen/svi-model/version-1.0/svi-shot.safetensors", | ||
| "image_url": | ||
| "https://raw.githubusercontent.com/vita-epfl/Stable-Video-Infinity/main/data/toy_test/shot/frame.jpg", | ||
| "prompts": [ | ||
| ("A sleek white motor yacht speeds across the turquoise blue sea, " | ||
| "leaving a dramatic wake of white foam behind it under a clear blue sky."), | ||
| ], | ||
| "num_clips": 2, | ||
| "num_motion_frames": 1, | ||
| "ref_pad_num": -1, | ||
| "height": 448, | ||
| }, | ||
| "film": { | ||
| "lora_path": "vita-video-gen/svi-model/version-1.0/svi-film-opt-10212025.safetensors", | ||
| "image_url": | ||
| "https://raw.githubusercontent.com/vita-epfl/Stable-Video-Infinity/main/data/toy_test/film/frame.jpg", | ||
| "prompts": [ | ||
| ("A Siamese kitten rests snugly inside a straw hat, its head slightly tilted " | ||
| "as it gazes curiously to the side."), | ||
| ("The Siamese kitten decides to explore the room and jumps out of the hat " | ||
| "onto the soft carpet below."), | ||
| ], | ||
| "num_clips": 2, | ||
| "num_motion_frames": 5, | ||
| "ref_pad_num": 0, | ||
| "height": 480, | ||
| }, | ||
| "tom": { | ||
| "lora_path": "vita-video-gen/svi-model/version-1.0/svi-tom.safetensors", | ||
| "image_url": | ||
| "https://raw.githubusercontent.com/vita-epfl/Stable-Video-Infinity/main/data/toy_test/tom/frame.png", | ||
| "prompts": [ | ||
| ("A static shot of the bright 1950s kitchen, turquoise cabinets and a chrome " | ||
| "sink glinting; Tom cat hovers over the counter, yellow eyes narrowed, while " | ||
| "Jerry mouse stands defiantly in a tiny milk puddle near a stack of purple plates."), | ||
| ("Close-up on Tom cat’s face: a wicked smirk creases his white muzzle; his black " | ||
| "brows angle into a sharp V as he crooks one claw toward Jerry mouse like a " | ||
| "menacing metronome."), | ||
| ], | ||
| "num_clips": 2, | ||
| "num_motion_frames": 1, | ||
| "ref_pad_num": 0, | ||
| "height": 560, | ||
| }, | ||
| } | ||
|
|
||
| OUTPUT_PATH = "video_samples_svi" | ||
|
|
||
|
|
||
| def main(): | ||
| config = VARIANT_CONFIG[MODEL_VARIANT] | ||
|
|
||
| generator = VideoGenerator.from_pretrained( | ||
| "Wan-AI/Wan2.1-I2V-14B-480P-Diffusers", | ||
| override_pipeline_cls_name="WanSVIImageToVideoPipeline", | ||
| lora_path=config["lora_path"], | ||
| lora_nickname=f"svi-{MODEL_VARIANT}", | ||
| num_gpus=1, | ||
| dit_cpu_offload=False, | ||
| vae_cpu_offload=False, | ||
| text_encoder_cpu_offload=True, | ||
| flow_shift=5.0, | ||
| # Set pin_cpu_memory to false if CPU RAM is limited and there're no frequent CPU-GPU transfer | ||
| pin_cpu_memory=True, | ||
| ) | ||
|
|
||
| generator.generate_video( | ||
| prompt=config["prompts"][0], | ||
| image_path=config["image_url"], | ||
| output_path=OUTPUT_PATH, | ||
| save_video=True, | ||
| height=config["height"], | ||
| width=832, | ||
| num_frames=81, | ||
| fps=24, | ||
| num_inference_steps=50, | ||
| guidance_scale=5.0, | ||
| seed=0, | ||
| svi_num_clips=config["num_clips"], | ||
| svi_clip_prompts=None if MODEL_VARIANT == "shot" else config["prompts"], | ||
| svi_num_motion_frames=config["num_motion_frames"], | ||
| svi_seed_stride=42, | ||
| svi_ref_pad_num=config["ref_pad_num"], | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,203 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| """ | ||
| Wan I2V pipeline variant for Stable-Video-Infinity multi-clip inference. | ||
|
|
||
| This module contains an implementation of the SVI-flavored Wan I2V | ||
| pipeline using the modular pipeline architecture. | ||
| """ | ||
|
|
||
| import dataclasses | ||
|
|
||
| import PIL.Image | ||
| import torch | ||
|
|
||
| from fastvideo.fastvideo_args import FastVideoArgs | ||
| from fastvideo.logger import init_logger | ||
| from fastvideo.models.schedulers.scheduling_flow_match_euler_discrete import FlowMatchEulerDiscreteScheduler | ||
| from fastvideo.pipelines.basic.wan.wan_i2v_pipeline import WanImageToVideoPipeline | ||
| from fastvideo.pipelines.pipeline_batch_info import ForwardBatch | ||
|
|
||
| # isort: off | ||
| from fastvideo.pipelines.stages import (ConditioningStage, DecodingStage, DenoisingStage, ImageEncodingStage, | ||
| InputValidationStage, LatentPreparationStage, SVIImageVAEEncodingStage, | ||
| TextEncodingStage, TimestepPreparationStage) | ||
| # isort: on | ||
|
|
||
| logger = init_logger(__name__) | ||
|
|
||
|
|
||
| def _tensor_to_pil_list(frames: torch.Tensor) -> list[PIL.Image.Image]: | ||
| """Convert a video tensor to a list of PIL frames.""" | ||
| arr = (frames.detach().to(torch.float32).cpu().clamp(0, 1) * 255.0).to(torch.uint8) | ||
| arr = arr.permute(1, 2, 3, 0).numpy() | ||
| return [PIL.Image.fromarray(a) for a in arr] | ||
|
|
||
|
|
||
| def _validate_multiclip_frames(num_motion: int, num_frames: int) -> None: | ||
| """num_motion must be < num_frames, else follow-up clips stitch to empty.""" | ||
| if num_motion >= num_frames: | ||
| raise ValueError(f"svi_num_motion_frames ({num_motion}) must be smaller than num_frames ({num_frames}) " | ||
| "for multi-clip generation; otherwise stitched follow-up clips would be empty.") | ||
|
|
||
|
|
||
| def _stitch_clip_outputs(clip_outputs: list[torch.Tensor], num_motion: int) -> torch.Tensor: | ||
| """Stitch clips using the SVI overlap convention.""" | ||
| clips = [video[:, :, :-num_motion] for video in clip_outputs[:-1]] | ||
| return torch.cat([*clips, clip_outputs[-1]], dim=2) | ||
|
|
||
|
|
||
| def _resolve_clip_prompts( | ||
| prompt: str | list[str] | None, | ||
| clip_prompts: list[str] | None, | ||
| num_clips: int, | ||
| ) -> list[str]: | ||
| """Return exactly one non-empty prompt per generated clip.""" | ||
| if clip_prompts is None: | ||
| if not isinstance(prompt, str) or not prompt.strip(): | ||
| raise ValueError("SVI requires a non-empty primary prompt") | ||
| return [prompt] * num_clips | ||
|
|
||
| if len(clip_prompts) != num_clips: | ||
| raise ValueError(f"svi_clip_prompts must contain exactly svi_num_clips entries " | ||
| f"({num_clips}), but got {len(clip_prompts)}") | ||
| if any(not isinstance(item, str) or not item.strip() for item in clip_prompts): | ||
| raise ValueError("svi_clip_prompts entries must be non-empty strings") | ||
| return list(clip_prompts) | ||
|
|
||
|
|
||
| def _clip_seed(seed: int | None, clip_idx: int, seed_stride: int) -> int: | ||
| """Match the SVI reference by varying diffusion noise per clip.""" | ||
| if seed is None: | ||
| raise ValueError("SVI requires a seed") | ||
| return int(seed) + clip_idx * seed_stride | ||
|
|
||
|
|
||
| class WanSVIImageToVideoPipeline(WanImageToVideoPipeline): | ||
| """ | ||
| Pipeline for Stable-Video-Infinity multi-clip I2V generation on Wan 2.1. | ||
| """ | ||
|
|
||
| def create_pipeline_stages(self, fastvideo_args: FastVideoArgs): | ||
| """Set up pipeline stages with proper dependency injection.""" | ||
| self.modules["scheduler"] = FlowMatchEulerDiscreteScheduler( | ||
| num_train_timesteps=1000, | ||
| shift=fastvideo_args.pipeline_config.flow_shift, | ||
| ) | ||
|
|
||
| self.add_stage(stage_name="input_validation_stage", stage=InputValidationStage()) | ||
| self.add_stage( | ||
| stage_name="prompt_encoding_stage", | ||
| stage=TextEncodingStage( | ||
| text_encoders=[self.get_module("text_encoder")], | ||
| tokenizers=[self.get_module("tokenizer")], | ||
| ), | ||
| ) | ||
| if (self.get_module("image_encoder") is not None and self.get_module("image_processor") is not None): | ||
| self.add_stage( | ||
| stage_name="image_encoding_stage", | ||
| stage=ImageEncodingStage( | ||
| image_encoder=self.get_module("image_encoder"), | ||
| image_processor=self.get_module("image_processor"), | ||
| ), | ||
| ) | ||
| self.add_stage(stage_name="conditioning_stage", stage=ConditioningStage()) | ||
| self.add_stage( | ||
| stage_name="timestep_preparation_stage", | ||
| stage=TimestepPreparationStage(scheduler=self.get_module("scheduler")), | ||
| ) | ||
| self.add_stage( | ||
| stage_name="latent_preparation_stage", | ||
| stage=LatentPreparationStage( | ||
| scheduler=self.get_module("scheduler"), | ||
| transformer=self.get_module("transformer"), | ||
| ), | ||
| ) | ||
| self.add_stage( | ||
| stage_name="image_latent_preparation_stage", | ||
| stage=SVIImageVAEEncodingStage(vae=self.get_module("vae")), | ||
| ) | ||
| self.add_stage( | ||
| stage_name="denoising_stage", | ||
| stage=DenoisingStage( | ||
| transformer=self.get_module("transformer"), | ||
| transformer_2=self.get_module("transformer_2"), | ||
| scheduler=self.get_module("scheduler"), | ||
| ), | ||
| ) | ||
| self.add_stage(stage_name="decoding_stage", stage=DecodingStage(vae=self.get_module("vae"))) | ||
|
|
||
| @torch.no_grad() | ||
| def forward(self, batch: ForwardBatch, fastvideo_args: FastVideoArgs) -> ForwardBatch: | ||
| if not self.post_init_called: | ||
| self.post_init() | ||
|
|
||
| n_steps = int(batch.num_inference_steps) | ||
| if batch.sigmas is None: | ||
| batch.sigmas = torch.linspace(1.0, 0.0, n_steps + 1)[:-1].tolist() | ||
|
|
||
| num_clips = max(1, int(batch.svi_num_clips or 1)) | ||
| num_motion = max(1, int(batch.svi_num_motion_frames or 1)) | ||
| seed_stride = int(batch.svi_seed_stride) | ||
|
|
||
| prompts = _resolve_clip_prompts(batch.prompt, batch.svi_clip_prompts, num_clips) | ||
| num_frames = int(batch.num_frames) if batch.num_frames is not None else 0 | ||
| if num_clips > 1: | ||
| _validate_multiclip_frames(num_motion, num_frames) | ||
|
|
||
| # Resolve image_path before constructing the first motion window. | ||
| self.input_validation_stage(batch, fastvideo_args) | ||
| assert isinstance(batch.pil_image, PIL.Image.Image) | ||
| assert isinstance(batch.height, int) and isinstance(batch.width, int) | ||
| reference_frame = batch.pil_image.resize((batch.width, batch.height)) | ||
| random_ref = batch.svi_random_ref_frame or reference_frame | ||
| motion_frames = batch.svi_first_frames or [reference_frame] | ||
|
|
||
| clip_outputs: list[torch.Tensor] = [] | ||
| for clip_idx in range(num_clips): | ||
| clip_batch = dataclasses.replace( | ||
| batch, | ||
| prompt=prompts[clip_idx], | ||
| seed=_clip_seed(batch.seed, clip_idx, seed_stride), | ||
| pil_image=motion_frames[0], | ||
| image_path=None, | ||
| svi_first_frames=motion_frames, | ||
| svi_random_ref_frame=random_ref, | ||
| prompt_embeds=[], | ||
| negative_prompt_embeds=None, | ||
| prompt_attention_mask=None, | ||
| negative_attention_mask=None, | ||
| clip_embedding_pos=None, | ||
| clip_embedding_neg=None, | ||
| image_embeds=[], | ||
| preprocessed_image=None, | ||
| latents=None, | ||
| image_latent=None, | ||
| noise_pred=None, | ||
| output=None, | ||
| timesteps=None, | ||
| timestep=None, | ||
| step_index=None, | ||
| is_prompt_processed=False, | ||
| ) | ||
| for stage in self.stages: | ||
| clip_batch = stage(clip_batch, fastvideo_args) | ||
|
|
||
| assert clip_batch.output is not None | ||
| clip_outputs.append(clip_batch.output) | ||
| logger.info( | ||
| "SVI clip %d/%d generated with seed=%d, frames shape=%s", | ||
| clip_idx + 1, | ||
| num_clips, | ||
| clip_batch.seed, | ||
| clip_batch.output.shape, | ||
| ) | ||
|
|
||
| if clip_idx + 1 < num_clips: | ||
| tail = clip_batch.output[0, :, -num_motion:, :, :] | ||
| motion_frames = _tensor_to_pil_list(tail) | ||
|
|
||
| batch.output = _stitch_clip_outputs(clip_outputs, num_motion) | ||
| return batch | ||
|
|
||
|
|
||
| EntryClass = WanSVIImageToVideoPipeline | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.