|
| 1 | +""" |
| 2 | +AnimateDiff Legacy Backend — wraps existing V2/SDXL/Lightning/Legacy pipelines. |
| 3 | +
|
| 4 | +Provides backward compatibility with all existing AnimateDiff features: |
| 5 | +- SD1.5 text-to-video with motion modules |
| 6 | +- SDXL text-to-video |
| 7 | +- Lightning ultra-fast inference (1-8 steps) |
| 8 | +- SparseCtrl (legacy pipeline only) |
| 9 | +- FreeInit, FreeNoise, IP-Adapter, Prompt Travel (V2) |
| 10 | +""" |
| 11 | + |
| 12 | +import logging |
| 13 | +from typing import Optional |
| 14 | + |
| 15 | +import torch |
| 16 | +from PIL import Image |
| 17 | + |
| 18 | +from animatediff.core.base_pipeline import BasePipeline, VideoOutput |
| 19 | + |
| 20 | +logger = logging.getLogger(__name__) |
| 21 | + |
| 22 | + |
| 23 | +class AnimateDiffBackend(BasePipeline): |
| 24 | + """Wraps existing AnimateDiff pipelines (V2/SDXL/Lightning/Legacy).""" |
| 25 | + |
| 26 | + backend_name = "animatediff" |
| 27 | + |
| 28 | + def __init__(self, pipe, pipeline_type: str = "v2"): |
| 29 | + self.pipe = pipe |
| 30 | + self.pipeline_type = pipeline_type |
| 31 | + |
| 32 | + @classmethod |
| 33 | + def load( |
| 34 | + cls, |
| 35 | + model_path: Optional[str] = None, |
| 36 | + torch_dtype: torch.dtype = torch.float16, |
| 37 | + device: str = "cuda", |
| 38 | + quantization: str = "none", |
| 39 | + offload_strategy: str = "none", |
| 40 | + enable_vae_slicing: bool = True, |
| 41 | + enable_vae_tiling: bool = False, |
| 42 | + pipeline_type: str = "v2", |
| 43 | + scheduler: str = "ddim", |
| 44 | + motion_adapter: Optional[str] = None, |
| 45 | + lightning_steps: int = 4, |
| 46 | + **kwargs, |
| 47 | + ) -> "AnimateDiffBackend": |
| 48 | + if model_path is None: |
| 49 | + model_path = "runwayml/stable-diffusion-v1-5" |
| 50 | + |
| 51 | + if pipeline_type == "sdxl": |
| 52 | + from animatediff.pipelines.pipeline_sdxl import AnimateDiffSDXL |
| 53 | + pipe = AnimateDiffSDXL.from_pretrained( |
| 54 | + model_path=model_path or "stabilityai/stable-diffusion-xl-base-1.0", |
| 55 | + motion_adapter_path=motion_adapter or "guoyww/animatediff-motion-adapter-sdxl-beta", |
| 56 | + torch_dtype=torch_dtype, |
| 57 | + device=device, |
| 58 | + scheduler=scheduler, |
| 59 | + ) |
| 60 | + elif pipeline_type == "lightning": |
| 61 | + from animatediff.pipelines.pipeline_lightning import AnimateDiffLightning |
| 62 | + pipe = AnimateDiffLightning.from_pretrained( |
| 63 | + model_path=model_path or "emilianJR/epiCRealism", |
| 64 | + num_steps=lightning_steps, |
| 65 | + torch_dtype=torch_dtype, |
| 66 | + device=device, |
| 67 | + ) |
| 68 | + else: |
| 69 | + from animatediff.pipelines.pipeline_v2 import AnimateDiffV2Pipeline |
| 70 | + pipe = AnimateDiffV2Pipeline.from_pretrained( |
| 71 | + model_path=model_path, |
| 72 | + motion_adapter_path=motion_adapter or "guoyww/animatediff-motion-adapter-v1-5-3", |
| 73 | + torch_dtype=torch_dtype, |
| 74 | + device=device, |
| 75 | + scheduler=scheduler, |
| 76 | + enable_vae_slicing=enable_vae_slicing, |
| 77 | + enable_vae_tiling=enable_vae_tiling, |
| 78 | + ) |
| 79 | + |
| 80 | + return cls(pipe, pipeline_type=pipeline_type) |
| 81 | + |
| 82 | + @torch.no_grad() |
| 83 | + def generate( |
| 84 | + self, |
| 85 | + prompt: str, |
| 86 | + negative_prompt: str = "bad quality, worst quality", |
| 87 | + width: int = 512, |
| 88 | + height: int = 512, |
| 89 | + num_frames: int = 16, |
| 90 | + num_inference_steps: int = 25, |
| 91 | + guidance_scale: float = 7.5, |
| 92 | + seed: int = -1, |
| 93 | + image: Optional[Image.Image] = None, |
| 94 | + **kwargs, |
| 95 | + ) -> VideoOutput: |
| 96 | + output = self.pipe.generate( |
| 97 | + prompt=prompt, |
| 98 | + negative_prompt=negative_prompt, |
| 99 | + num_frames=num_frames, |
| 100 | + height=height, |
| 101 | + width=width, |
| 102 | + num_inference_steps=num_inference_steps, |
| 103 | + guidance_scale=guidance_scale, |
| 104 | + seed=seed, |
| 105 | + **kwargs, |
| 106 | + ) |
| 107 | + frames = output.frames[0] |
| 108 | + |
| 109 | + return VideoOutput( |
| 110 | + frames=frames, |
| 111 | + seed=seed, |
| 112 | + backend=self.backend_name, |
| 113 | + metadata={"pipeline_type": self.pipeline_type}, |
| 114 | + ) |
| 115 | + |
| 116 | + def save(self, output: VideoOutput, path: str, fps: int = 8): |
| 117 | + """Use existing pipeline save or default.""" |
| 118 | + if hasattr(self.pipe, "save"): |
| 119 | + # The V2/SDXL/Lightning pipelines have their own save method |
| 120 | + # but they expect their native output format, not VideoOutput. |
| 121 | + # Use base class save instead. |
| 122 | + pass |
| 123 | + super().save(output, path, fps=fps) |
0 commit comments