Spaces:
Running
on
Zero
Running
on
Zero
Wan 2.2
Browse files- app.py +214 -0
- optimization.py +114 -0
- optimization_utils.py +98 -0
- requirements.txt +10 -0
app.py
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# PyTorch 2.8 (temporary hack)
|
| 2 |
+
import os
|
| 3 |
+
os.system('pip install --upgrade --pre --extra-index-url https://download.pytorch.org/whl/nightly/cu126 "torch<2.9" spaces')
|
| 4 |
+
|
| 5 |
+
# Actual demo code
|
| 6 |
+
import spaces
|
| 7 |
+
import torch
|
| 8 |
+
from diffusers.pipelines.wan.pipeline_wan_i2v import WanImageToVideoPipeline
|
| 9 |
+
from diffusers.models.transformers.transformer_wan import WanTransformer3DModel
|
| 10 |
+
from diffusers.utils.export_utils import export_to_video
|
| 11 |
+
import gradio as gr
|
| 12 |
+
import tempfile
|
| 13 |
+
import numpy as np
|
| 14 |
+
from PIL import Image
|
| 15 |
+
import random
|
| 16 |
+
|
| 17 |
+
from optimization import optimize_pipeline_
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
MODEL_ID = "Wan-AI/Wan2.2-I2V-A14B-Diffusers"
|
| 21 |
+
|
| 22 |
+
LANDSCAPE_WIDTH = 832
|
| 23 |
+
LANDSCAPE_HEIGHT = 480
|
| 24 |
+
MAX_SEED = np.iinfo(np.int32).max
|
| 25 |
+
|
| 26 |
+
FIXED_FPS = 24
|
| 27 |
+
MIN_FRAMES_MODEL = 8
|
| 28 |
+
MAX_FRAMES_MODEL = 81
|
| 29 |
+
|
| 30 |
+
MIN_DURATION = round(MIN_FRAMES_MODEL/FIXED_FPS,1)
|
| 31 |
+
MAX_DURATION = round(MAX_FRAMES_MODEL/FIXED_FPS,1)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
transformer = WanTransformer3DModel.from_pretrained('cbensimon/Wan2.2-I2V-A14B-bf16-Diffusers',
|
| 35 |
+
subfolder='transformer',
|
| 36 |
+
torch_dtype=torch.bfloat16,
|
| 37 |
+
device_map='cuda',
|
| 38 |
+
)
|
| 39 |
+
transformer_2 = WanTransformer3DModel.from_pretrained('cbensimon/Wan2.2-I2V-A14B-bf16-Diffusers',
|
| 40 |
+
subfolder='transformer_2',
|
| 41 |
+
torch_dtype=torch.bfloat16,
|
| 42 |
+
device_map='cuda',
|
| 43 |
+
)
|
| 44 |
+
pipe = WanImageToVideoPipeline.from_pretrained(MODEL_ID,
|
| 45 |
+
transformer=transformer,
|
| 46 |
+
transformer_2=transformer,
|
| 47 |
+
torch_dtype=torch.bfloat16,
|
| 48 |
+
).to('cuda')
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
optimize_pipeline_(pipe,
|
| 52 |
+
image=Image.new('RGB', (LANDSCAPE_WIDTH, LANDSCAPE_HEIGHT)),
|
| 53 |
+
prompt='prompt',
|
| 54 |
+
height=LANDSCAPE_HEIGHT,
|
| 55 |
+
width=LANDSCAPE_WIDTH,
|
| 56 |
+
num_frames=MAX_FRAMES_MODEL,
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
default_prompt_i2v = "make this image come alive, cinematic motion, smooth animation"
|
| 61 |
+
default_negative_prompt = "色调艳丽, 过曝, 静态, 细节模糊不清, 字幕, 风格, 作品, 画作, 画面, 静止, 整体发灰, 最差质量, 低质量, JPEG压缩残留, 丑陋的, 残缺的, 多余的手指, 画得不好的手部, 画得不好的脸部, 畸形的, 毁容的, 形态畸形的肢体, 手指融合, 静止不动的画面, 杂乱的背景, 三条腿, 背景人很多, 倒着走"
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def resize_image(image: Image.Image) -> Image.Image:
|
| 65 |
+
if image.height > image.width:
|
| 66 |
+
transposed = image.transpose(Image.Transpose.ROTATE_90)
|
| 67 |
+
resized = resize_image_landscape(transposed)
|
| 68 |
+
return resized.transpose(Image.Transpose.ROTATE_270)
|
| 69 |
+
return resize_image_landscape(image)
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def resize_image_landscape(image: Image.Image) -> Image.Image:
|
| 73 |
+
target_aspect = LANDSCAPE_WIDTH / LANDSCAPE_HEIGHT
|
| 74 |
+
width, height = image.size
|
| 75 |
+
in_aspect = width / height
|
| 76 |
+
if in_aspect > target_aspect:
|
| 77 |
+
new_width = round(height * target_aspect)
|
| 78 |
+
left = (width - new_width) // 2
|
| 79 |
+
image = image.crop((left, 0, left + new_width, height))
|
| 80 |
+
else:
|
| 81 |
+
new_height = round(width / target_aspect)
|
| 82 |
+
top = (height - new_height) // 2
|
| 83 |
+
image = image.crop((0, top, width, top + new_height))
|
| 84 |
+
return image.resize((LANDSCAPE_WIDTH, LANDSCAPE_HEIGHT), Image.LANCZOS)
|
| 85 |
+
|
| 86 |
+
def get_duration(
|
| 87 |
+
input_image,
|
| 88 |
+
prompt,
|
| 89 |
+
negative_prompt,
|
| 90 |
+
duration_seconds,
|
| 91 |
+
guidance_scale,
|
| 92 |
+
steps,
|
| 93 |
+
seed,
|
| 94 |
+
randomize_seed,
|
| 95 |
+
progress,
|
| 96 |
+
):
|
| 97 |
+
return steps * 15
|
| 98 |
+
|
| 99 |
+
@spaces.GPU(duration=get_duration)
|
| 100 |
+
def generate_video(
|
| 101 |
+
input_image,
|
| 102 |
+
prompt,
|
| 103 |
+
negative_prompt=default_negative_prompt,
|
| 104 |
+
duration_seconds = MAX_DURATION,
|
| 105 |
+
guidance_scale = 1,
|
| 106 |
+
steps = 4,
|
| 107 |
+
seed = 42,
|
| 108 |
+
randomize_seed = False,
|
| 109 |
+
progress=gr.Progress(track_tqdm=True),
|
| 110 |
+
):
|
| 111 |
+
"""
|
| 112 |
+
Generate a video from an input image using the Wan 2.1 I2V model with CausVid LoRA.
|
| 113 |
+
|
| 114 |
+
This function takes an input image and generates a video animation based on the provided
|
| 115 |
+
prompt and parameters. It uses the Wan 2.1 14B Image-to-Video model with CausVid LoRA
|
| 116 |
+
for fast generation in 4-8 steps.
|
| 117 |
+
|
| 118 |
+
Args:
|
| 119 |
+
input_image (PIL.Image): The input image to animate. Will be resized to target dimensions.
|
| 120 |
+
prompt (str): Text prompt describing the desired animation or motion.
|
| 121 |
+
negative_prompt (str, optional): Negative prompt to avoid unwanted elements.
|
| 122 |
+
Defaults to default_negative_prompt (contains unwanted visual artifacts).
|
| 123 |
+
duration_seconds (float, optional): Duration of the generated video in seconds.
|
| 124 |
+
Defaults to 2. Clamped between MIN_FRAMES_MODEL/FIXED_FPS and MAX_FRAMES_MODEL/FIXED_FPS.
|
| 125 |
+
guidance_scale (float, optional): Controls adherence to the prompt. Higher values = more adherence.
|
| 126 |
+
Defaults to 1.0. Range: 0.0-20.0.
|
| 127 |
+
steps (int, optional): Number of inference steps. More steps = higher quality but slower.
|
| 128 |
+
Defaults to 4. Range: 1-30.
|
| 129 |
+
seed (int, optional): Random seed for reproducible results. Defaults to 42.
|
| 130 |
+
Range: 0 to MAX_SEED (2147483647).
|
| 131 |
+
randomize_seed (bool, optional): Whether to use a random seed instead of the provided seed.
|
| 132 |
+
Defaults to False.
|
| 133 |
+
progress (gr.Progress, optional): Gradio progress tracker. Defaults to gr.Progress(track_tqdm=True).
|
| 134 |
+
|
| 135 |
+
Returns:
|
| 136 |
+
tuple: A tuple containing:
|
| 137 |
+
- video_path (str): Path to the generated video file (.mp4)
|
| 138 |
+
- current_seed (int): The seed used for generation (useful when randomize_seed=True)
|
| 139 |
+
|
| 140 |
+
Raises:
|
| 141 |
+
gr.Error: If input_image is None (no image uploaded).
|
| 142 |
+
|
| 143 |
+
Note:
|
| 144 |
+
- The function automatically resizes the input image to the target dimensions
|
| 145 |
+
- Frame count is calculated as duration_seconds * FIXED_FPS (24)
|
| 146 |
+
- Output dimensions are adjusted to be multiples of MOD_VALUE (32)
|
| 147 |
+
- The function uses GPU acceleration via the @spaces.GPU decorator
|
| 148 |
+
- Generation time varies based on steps and duration (see get_duration function)
|
| 149 |
+
"""
|
| 150 |
+
if input_image is None:
|
| 151 |
+
raise gr.Error("Please upload an input image.")
|
| 152 |
+
|
| 153 |
+
num_frames = np.clip(int(round(duration_seconds * FIXED_FPS)), MIN_FRAMES_MODEL, MAX_FRAMES_MODEL)
|
| 154 |
+
current_seed = random.randint(0, MAX_SEED) if randomize_seed else int(seed)
|
| 155 |
+
resized_image = resize_image(input_image)
|
| 156 |
+
|
| 157 |
+
output_frames_list = pipe(
|
| 158 |
+
image=resized_image,
|
| 159 |
+
prompt=prompt,
|
| 160 |
+
negative_prompt=negative_prompt,
|
| 161 |
+
height=resized_image.height,
|
| 162 |
+
width=resized_image.width,
|
| 163 |
+
num_frames=num_frames,
|
| 164 |
+
guidance_scale=float(guidance_scale),
|
| 165 |
+
num_inference_steps=int(steps),
|
| 166 |
+
generator=torch.Generator(device="cuda").manual_seed(current_seed),
|
| 167 |
+
).frames[0]
|
| 168 |
+
|
| 169 |
+
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmpfile:
|
| 170 |
+
video_path = tmpfile.name
|
| 171 |
+
|
| 172 |
+
export_to_video(output_frames_list, video_path, fps=FIXED_FPS)
|
| 173 |
+
|
| 174 |
+
return video_path, current_seed
|
| 175 |
+
|
| 176 |
+
with gr.Blocks() as demo:
|
| 177 |
+
gr.Markdown("# Fast 4 steps Wan 2.1 I2V (14B) with CausVid LoRA")
|
| 178 |
+
gr.Markdown("[CausVid](https://github.com/tianweiy/CausVid) is a distilled version of Wan 2.1 to run faster in just 4-8 steps, [extracted as LoRA by Kijai](https://huggingface.co/Kijai/WanVideo_comfy/blob/main/Wan21_CausVid_14B_T2V_lora_rank32.safetensors) and is compatible with 🧨 diffusers")
|
| 179 |
+
with gr.Row():
|
| 180 |
+
with gr.Column():
|
| 181 |
+
input_image_component = gr.Image(type="pil", label="Input Image (auto-resized to target H/W)")
|
| 182 |
+
prompt_input = gr.Textbox(label="Prompt", value=default_prompt_i2v)
|
| 183 |
+
duration_seconds_input = gr.Slider(minimum=MIN_DURATION, maximum=MAX_DURATION, step=0.1, value=MAX_DURATION, label="Duration (seconds)", info=f"Clamped to model's {MIN_FRAMES_MODEL}-{MAX_FRAMES_MODEL} frames at {FIXED_FPS}fps.")
|
| 184 |
+
|
| 185 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 186 |
+
negative_prompt_input = gr.Textbox(label="Negative Prompt", value=default_negative_prompt, lines=3)
|
| 187 |
+
seed_input = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=42, interactive=True)
|
| 188 |
+
randomize_seed_checkbox = gr.Checkbox(label="Randomize seed", value=True, interactive=True)
|
| 189 |
+
steps_slider = gr.Slider(minimum=1, maximum=30, step=1, value=4, label="Inference Steps")
|
| 190 |
+
guidance_scale_input = gr.Slider(minimum=0.0, maximum=20.0, step=0.5, value=1.0, label="Guidance Scale", visible=False)
|
| 191 |
+
|
| 192 |
+
generate_button = gr.Button("Generate Video", variant="primary")
|
| 193 |
+
with gr.Column():
|
| 194 |
+
video_output = gr.Video(label="Generated Video", autoplay=True, interactive=False)
|
| 195 |
+
|
| 196 |
+
ui_inputs = [
|
| 197 |
+
input_image_component, prompt_input,
|
| 198 |
+
negative_prompt_input, duration_seconds_input,
|
| 199 |
+
guidance_scale_input, steps_slider, seed_input, randomize_seed_checkbox
|
| 200 |
+
]
|
| 201 |
+
generate_button.click(fn=generate_video, inputs=ui_inputs, outputs=[video_output, seed_input])
|
| 202 |
+
|
| 203 |
+
gr.Examples(
|
| 204 |
+
examples=[
|
| 205 |
+
[
|
| 206 |
+
"wan_i2v_input.JPG",
|
| 207 |
+
"Summer beach vacation style, a white cat wearing sunglasses sits on a surfboard. The fluffy-furred feline gazes directly at the camera with a relaxed expression. Blurred beach scenery forms the background featuring crystal-clear waters, distant green hills, and a blue sky dotted with white clouds. The cat assumes a naturally relaxed posture, as if savoring the sea breeze and warm sunlight. A close-up shot highlights the feline's intricate details and the refreshing atmosphere of the seaside.",
|
| 208 |
+
],
|
| 209 |
+
],
|
| 210 |
+
inputs=[input_image_component, prompt_input], outputs=[video_output, seed_input], fn=generate_video, cache_examples="lazy"
|
| 211 |
+
)
|
| 212 |
+
|
| 213 |
+
if __name__ == "__main__":
|
| 214 |
+
demo.queue().launch(mcp_server=True)
|
optimization.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
"""
|
| 3 |
+
|
| 4 |
+
from typing import Any
|
| 5 |
+
from typing import Callable
|
| 6 |
+
from typing import ParamSpec
|
| 7 |
+
|
| 8 |
+
import spaces
|
| 9 |
+
import torch
|
| 10 |
+
from torch.utils._pytree import tree_map_only
|
| 11 |
+
from torchao.quantization import quantize_
|
| 12 |
+
from torchao.quantization import Float8DynamicActivationFloat8WeightConfig
|
| 13 |
+
|
| 14 |
+
from optimization_utils import capture_component_call
|
| 15 |
+
from optimization_utils import aoti_compile
|
| 16 |
+
from optimization_utils import ZeroGPUCompiledModel
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
P = ParamSpec('P')
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
TRANSFORMER_NUM_FRAMES_DIM = torch.export.Dim('num_frames', min=3, max=21)
|
| 23 |
+
|
| 24 |
+
TRANSFORMER_DYNAMIC_SHAPES = {
|
| 25 |
+
'hidden_states': {
|
| 26 |
+
2: TRANSFORMER_NUM_FRAMES_DIM,
|
| 27 |
+
},
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
INDUCTOR_CONFIGS = {
|
| 31 |
+
'conv_1x1_as_mm': True,
|
| 32 |
+
'epilogue_fusion': False,
|
| 33 |
+
'coordinate_descent_tuning': True,
|
| 34 |
+
'coordinate_descent_check_all_directions': True,
|
| 35 |
+
'max_autotune': True,
|
| 36 |
+
'triton.cudagraphs': True,
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def optimize_pipeline_(pipeline: Callable[P, Any], *args: P.args, **kwargs: P.kwargs):
|
| 41 |
+
|
| 42 |
+
@spaces.GPU(duration=1500)
|
| 43 |
+
def compile_transformer():
|
| 44 |
+
|
| 45 |
+
with capture_component_call(pipeline, 'transformer') as call:
|
| 46 |
+
pipeline(*args, **kwargs)
|
| 47 |
+
|
| 48 |
+
dynamic_shapes = tree_map_only((torch.Tensor, bool), lambda t: None, call.kwargs)
|
| 49 |
+
dynamic_shapes |= TRANSFORMER_DYNAMIC_SHAPES
|
| 50 |
+
|
| 51 |
+
quantize_(pipeline.transformer, Float8DynamicActivationFloat8WeightConfig())
|
| 52 |
+
|
| 53 |
+
hidden_states: torch.Tensor = call.kwargs['hidden_states']
|
| 54 |
+
hidden_states_transposed = hidden_states.transpose(-1, -2).contiguous()
|
| 55 |
+
if hidden_states.shape[-1] > hidden_states.shape[-2]:
|
| 56 |
+
hidden_states_landscape = hidden_states
|
| 57 |
+
hidden_states_portrait = hidden_states_transposed
|
| 58 |
+
else:
|
| 59 |
+
hidden_states_landscape = hidden_states_transposed
|
| 60 |
+
hidden_states_portrait = hidden_states
|
| 61 |
+
|
| 62 |
+
exported_landscape_1 = torch.export.export(
|
| 63 |
+
mod=pipeline.transformer,
|
| 64 |
+
args=call.args,
|
| 65 |
+
kwargs=call.kwargs | {'hidden_states': hidden_states_landscape},
|
| 66 |
+
dynamic_shapes=dynamic_shapes,
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
exported_portrait_2 = torch.export.export(
|
| 70 |
+
mod=pipeline.transformer_2,
|
| 71 |
+
args=call.args,
|
| 72 |
+
kwargs=call.kwargs | {'hidden_states': hidden_states_portrait},
|
| 73 |
+
dynamic_shapes=dynamic_shapes,
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
compiled_landscape_1 = aoti_compile(exported_landscape_1, INDUCTOR_CONFIGS)
|
| 77 |
+
compiled_portrait_2 = aoti_compile(exported_portrait_2, INDUCTOR_CONFIGS)
|
| 78 |
+
|
| 79 |
+
compiled_landscape_2 = ZeroGPUCompiledModel(compiled_landscape_1.archive_file, compiled_portrait_2.weights)
|
| 80 |
+
compiled_portrait_1 = ZeroGPUCompiledModel(compiled_portrait_2.archive_file, compiled_landscape_1.weights)
|
| 81 |
+
|
| 82 |
+
return (
|
| 83 |
+
compiled_landscape_1,
|
| 84 |
+
compiled_landscape_2,
|
| 85 |
+
compiled_portrait_1,
|
| 86 |
+
compiled_portrait_2,
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
cl1, cl2, cp1, cp2 = compile_transformer()
|
| 90 |
+
|
| 91 |
+
def combined_transformer_1(*args, **kwargs):
|
| 92 |
+
hidden_states: torch.Tensor = kwargs['hidden_states']
|
| 93 |
+
if hidden_states.shape[-1] > hidden_states.shape[-2]:
|
| 94 |
+
return cl1(*args, **kwargs)
|
| 95 |
+
else:
|
| 96 |
+
return cp1(*args, **kwargs)
|
| 97 |
+
|
| 98 |
+
def combined_transformer_2(*args, **kwargs):
|
| 99 |
+
hidden_states: torch.Tensor = kwargs['hidden_states']
|
| 100 |
+
if hidden_states.shape[-1] > hidden_states.shape[-2]:
|
| 101 |
+
return cl2(*args, **kwargs)
|
| 102 |
+
else:
|
| 103 |
+
return cp2(*args, **kwargs)
|
| 104 |
+
|
| 105 |
+
transformer_config = pipeline.transformer.config
|
| 106 |
+
transformer_dtype = pipeline.transformer.dtype
|
| 107 |
+
|
| 108 |
+
pipeline.transformer = combined_transformer_1
|
| 109 |
+
pipeline.transformer.config = transformer_config # pyright: ignore[reportAttributeAccessIssue]
|
| 110 |
+
pipeline.transformer.dtype = transformer_dtype # pyright: ignore[reportAttributeAccessIssue]
|
| 111 |
+
|
| 112 |
+
pipeline.transformer_2 = combined_transformer_2
|
| 113 |
+
pipeline.transformer_2.config = transformer_config # pyright: ignore[reportAttributeAccessIssue]
|
| 114 |
+
pipeline.transformer_2.dtype = transformer_dtype # pyright: ignore[reportAttributeAccessIssue]
|
optimization_utils.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
"""
|
| 3 |
+
import contextlib
|
| 4 |
+
from contextvars import ContextVar
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
from typing import Any
|
| 7 |
+
from typing import cast
|
| 8 |
+
from unittest.mock import patch
|
| 9 |
+
|
| 10 |
+
import torch
|
| 11 |
+
from torch._inductor.package.package import package_aoti
|
| 12 |
+
from torch.export.pt2_archive._package import AOTICompiledModel
|
| 13 |
+
from torch.export.pt2_archive._package_weights import Weights
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
INDUCTOR_CONFIGS_OVERRIDES = {
|
| 17 |
+
'aot_inductor.package_constants_in_so': False,
|
| 18 |
+
'aot_inductor.package_constants_on_disk': True,
|
| 19 |
+
'aot_inductor.package': True,
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class ZeroGPUWeights:
|
| 24 |
+
def __init__(self, constants_map: dict[str, torch.Tensor], to_cuda: bool = False):
|
| 25 |
+
if to_cuda:
|
| 26 |
+
self.constants_map = {name: tensor.to('cuda') for name, tensor in constants_map.items()}
|
| 27 |
+
else:
|
| 28 |
+
self.constants_map = constants_map
|
| 29 |
+
def __reduce__(self):
|
| 30 |
+
constants_map: dict[str, torch.Tensor] = {}
|
| 31 |
+
for name, tensor in self.constants_map.items():
|
| 32 |
+
tensor_ = torch.empty_like(tensor, device='cpu').pin_memory()
|
| 33 |
+
constants_map[name] = tensor_.copy_(tensor).detach().share_memory_()
|
| 34 |
+
return ZeroGPUWeights, (constants_map, True)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
class ZeroGPUCompiledModel:
|
| 38 |
+
def __init__(self, archive_file: torch.types.FileLike, weights: ZeroGPUWeights):
|
| 39 |
+
self.archive_file = archive_file
|
| 40 |
+
self.weights = weights
|
| 41 |
+
self.compiled_model: ContextVar[AOTICompiledModel | None] = ContextVar('compiled_model', default=None)
|
| 42 |
+
def __call__(self, *args, **kwargs):
|
| 43 |
+
if (compiled_model := self.compiled_model.get()) is None:
|
| 44 |
+
compiled_model = cast(AOTICompiledModel, torch._inductor.aoti_load_package(self.archive_file))
|
| 45 |
+
compiled_model.load_constants(self.weights.constants_map, check_full_update=True, user_managed=True)
|
| 46 |
+
self.compiled_model.set(compiled_model)
|
| 47 |
+
return compiled_model(*args, **kwargs)
|
| 48 |
+
def __reduce__(self):
|
| 49 |
+
return ZeroGPUCompiledModel, (self.archive_file, self.weights)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def aoti_compile(
|
| 53 |
+
exported_program: torch.export.ExportedProgram,
|
| 54 |
+
inductor_configs: dict[str, Any] | None = None,
|
| 55 |
+
):
|
| 56 |
+
inductor_configs = (inductor_configs or {}) | INDUCTOR_CONFIGS_OVERRIDES
|
| 57 |
+
gm = cast(torch.fx.GraphModule, exported_program.module())
|
| 58 |
+
assert exported_program.example_inputs is not None
|
| 59 |
+
args, kwargs = exported_program.example_inputs
|
| 60 |
+
artifacts = torch._inductor.aot_compile(gm, args, kwargs, options=inductor_configs)
|
| 61 |
+
archive_file = BytesIO()
|
| 62 |
+
files: list[str | Weights] = [file for file in artifacts if isinstance(file, str)]
|
| 63 |
+
package_aoti(archive_file, files)
|
| 64 |
+
weights, = (artifact for artifact in artifacts if isinstance(artifact, Weights))
|
| 65 |
+
zerogpu_weights = ZeroGPUWeights({name: weights.get_weight(name)[0] for name in weights})
|
| 66 |
+
return ZeroGPUCompiledModel(archive_file, zerogpu_weights)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
@contextlib.contextmanager
|
| 70 |
+
def capture_component_call(
|
| 71 |
+
pipeline: Any,
|
| 72 |
+
component_name: str,
|
| 73 |
+
component_method='forward',
|
| 74 |
+
):
|
| 75 |
+
|
| 76 |
+
class CapturedCallException(Exception):
|
| 77 |
+
def __init__(self, *args, **kwargs):
|
| 78 |
+
super().__init__()
|
| 79 |
+
self.args = args
|
| 80 |
+
self.kwargs = kwargs
|
| 81 |
+
|
| 82 |
+
class CapturedCall:
|
| 83 |
+
def __init__(self):
|
| 84 |
+
self.args: tuple[Any, ...] = ()
|
| 85 |
+
self.kwargs: dict[str, Any] = {}
|
| 86 |
+
|
| 87 |
+
component = getattr(pipeline, component_name)
|
| 88 |
+
captured_call = CapturedCall()
|
| 89 |
+
|
| 90 |
+
def capture_call(*args, **kwargs):
|
| 91 |
+
raise CapturedCallException(*args, **kwargs)
|
| 92 |
+
|
| 93 |
+
with patch.object(component, component_method, new=capture_call):
|
| 94 |
+
try:
|
| 95 |
+
yield captured_call
|
| 96 |
+
except CapturedCallException as e:
|
| 97 |
+
captured_call.args = e.args
|
| 98 |
+
captured_call.kwargs = e.kwargs
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
git+https://github.com/huggingface/diffusers.git@5512d70f7fd89f69511d9c23f1473a49f7901bee
|
| 2 |
+
transformers
|
| 3 |
+
accelerate
|
| 4 |
+
safetensors
|
| 5 |
+
sentencepiece
|
| 6 |
+
peft
|
| 7 |
+
ftfy
|
| 8 |
+
imageio-ffmpeg
|
| 9 |
+
opencv-python
|
| 10 |
+
torchao==0.11.0
|