Update optimization.py

#1
by linoyts HF Staff - opened
Files changed (2) hide show
  1. app.py +28 -64
  2. optimization.py +16 -16
app.py CHANGED
@@ -19,8 +19,8 @@ from optimization import optimize_pipeline_
19
 
20
  MODEL_ID = "Wan-AI/Wan2.2-T2V-A14B-Diffusers"
21
 
22
- LANDSCAPE_WIDTH = 832
23
- LANDSCAPE_HEIGHT = 480
24
  MAX_SEED = np.iinfo(np.int32).max
25
 
26
  FIXED_FPS = 16
@@ -46,34 +46,6 @@ pipe = WanPipeline.from_pretrained(MODEL_ID,
46
  torch_dtype=torch.bfloat16,
47
  ).to('cuda')
48
 
49
- # load, fuse, unload before compilation
50
- # pipe.load_lora_weights(
51
- # "vrgamedevgirl84/Wan14BT2VFusioniX",
52
- # weight_name="FusionX_LoRa/Phantom_Wan_14B_FusionX_LoRA.safetensors",
53
- # adapter_name="phantom"
54
- # )
55
-
56
- # pipe.set_adapters(["phantom"], adapter_weights=[0.95])
57
- # pipe.fuse_lora(adapter_names=["phantom"], lora_scale=1.0)
58
- # pipe.unload_lora_weights()
59
-
60
-
61
- # pipe.load_lora_weights(
62
- # "vrgamedevgirl84/Wan14BT2VFusioniX",
63
- # weight_name="FusionX_LoRa/Phantom_Wan_14B_FusionX_LoRA.safetensors",
64
- # adapter_name="phantom"
65
- # )
66
- # kwargs = {}
67
- # kwargs["load_into_transformer_2"] = True
68
- # pipe.load_lora_weights(
69
- # "vrgamedevgirl84/Wan14BT2VFusioniX",
70
- # weight_name="FusionX_LoRa/Phantom_Wan_14B_FusionX_LoRA.safetensors",
71
- # adapter_name="phantom_2", **kwargs
72
- # )
73
- # pipe.set_adapters(["phantom", "phantom_2"], adapter_weights=[1., 1.])
74
- # pipe.fuse_lora(adapter_names=["phantom"], lora_scale=3., components=["transformer"])
75
- # pipe.fuse_lora(adapter_names=["phantom_2"], lora_scale=1., components=["transformer_2"])
76
- # pipe.unload_lora_weights()
77
 
78
  for i in range(3):
79
  gc.collect()
@@ -95,7 +67,6 @@ default_negative_prompt = "色调艳丽, 过曝, 静态, 细节模糊不清, 字
95
  def get_duration(
96
  prompt,
97
  negative_prompt,
98
- duration_seconds,
99
  guidance_scale,
100
  guidance_scale_2,
101
  steps,
@@ -106,13 +77,12 @@ def get_duration(
106
  return steps * 15
107
 
108
  @spaces.GPU(duration=get_duration)
109
- def generate_video(
110
  prompt,
111
  negative_prompt=default_negative_prompt,
112
- duration_seconds = MAX_DURATION,
113
- guidance_scale = 1,
114
- guidance_scale_2 = 3,
115
- steps = 4,
116
  seed = 42,
117
  randomize_seed = False,
118
  progress=gr.Progress(track_tqdm=True),
@@ -128,8 +98,6 @@ def generate_video(
128
  prompt (str): Text prompt describing the desired animation or motion.
129
  negative_prompt (str, optional): Negative prompt to avoid unwanted elements.
130
  Defaults to default_negative_prompt (contains unwanted visual artifacts).
131
- duration_seconds (float, optional): Duration of the generated video in seconds.
132
- Defaults to 2. Clamped between MIN_FRAMES_MODEL/FIXED_FPS and MAX_FRAMES_MODEL/FIXED_FPS.
133
  guidance_scale (float, optional): Controls adherence to the prompt. Higher values = more adherence.
134
  Defaults to 1.0. Range: 0.0-20.0.
135
  guidance_scale_2 (float, optional): Controls adherence to the prompt. Higher values = more adherence.
@@ -158,62 +126,58 @@ def generate_video(
158
  - Generation time varies based on steps and duration (see get_duration function)
159
  """
160
 
161
- num_frames = np.clip(int(round(duration_seconds * FIXED_FPS)), MIN_FRAMES_MODEL, MAX_FRAMES_MODEL)
162
  current_seed = random.randint(0, MAX_SEED) if randomize_seed else int(seed)
163
 
164
- output_frames_list = pipe(
165
  prompt=prompt,
166
  negative_prompt=negative_prompt,
167
- height=480,
168
- width=832,
169
- num_frames=num_frames,
170
  guidance_scale=float(guidance_scale),
171
  guidance_scale_2=float(guidance_scale_2),
172
  num_inference_steps=int(steps),
 
173
  generator=torch.Generator(device="cuda").manual_seed(current_seed),
174
- ).frames[0]
175
 
176
- with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmpfile:
177
- video_path = tmpfile.name
178
-
179
- export_to_video(output_frames_list, video_path, fps=FIXED_FPS)
180
-
181
- return video_path, current_seed
182
 
183
  with gr.Blocks() as demo:
184
- gr.Markdown("# Fast 6 steps Wan 2.2 I2V (14B) with Phantom LoRA")
185
- gr.Markdown("run Wan 2.2 in just 6-8 steps, with [FusionX Phantom LoRA by DeeJayT](https://huggingface.co/vrgamedevgirl84/Wan14BT2VFusioniX/tree/main/FusionX_LoRa), compatible with 🧨 diffusers")
186
  with gr.Row():
187
  with gr.Column():
188
  prompt_input = gr.Textbox(label="Prompt", value=default_prompt_i2v)
189
- 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.")
190
 
191
  with gr.Accordion("Advanced Settings", open=False):
192
  negative_prompt_input = gr.Textbox(label="Negative Prompt", value=default_negative_prompt, lines=3)
193
  seed_input = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=42, interactive=True)
194
  randomize_seed_checkbox = gr.Checkbox(label="Randomize seed", value=True, interactive=True)
195
- steps_slider = gr.Slider(minimum=1, maximum=30, step=1, value=6, label="Inference Steps")
196
- guidance_scale_input = gr.Slider(minimum=0.0, maximum=10.0, step=0.5, value=1, label="Guidance Scale - high noise stage")
197
- guidance_scale_2_input = gr.Slider(minimum=0.0, maximum=10.0, step=0.5, value=3, label="Guidance Scale 2 - low noise stage")
198
 
199
- generate_button = gr.Button("Generate Video", variant="primary")
200
  with gr.Column():
201
- video_output = gr.Video(label="Generated Video", autoplay=True, interactive=False)
202
 
203
- ui_inputs = [
204
- input_image_component, prompt_input,
205
- negative_prompt_input, duration_seconds_input,
206
  guidance_scale_input, guidance_scale_2_input, steps_slider, seed_input, randomize_seed_checkbox
207
  ]
208
- generate_button.click(fn=generate_video, inputs=ui_inputs, outputs=[video_output, seed_input])
209
 
210
  gr.Examples(
211
  examples=[
212
  [
213
- "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.",
214
  ],
215
  ],
216
- inputs=[prompt_input], outputs=[video_output, seed_input], fn=generate_video, cache_examples="lazy"
217
  )
218
 
219
  if __name__ == "__main__":
 
19
 
20
  MODEL_ID = "Wan-AI/Wan2.2-T2V-A14B-Diffusers"
21
 
22
+ LANDSCAPE_WIDTH = 1024
23
+ LANDSCAPE_HEIGHT = 1024
24
  MAX_SEED = np.iinfo(np.int32).max
25
 
26
  FIXED_FPS = 16
 
46
  torch_dtype=torch.bfloat16,
47
  ).to('cuda')
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
  for i in range(3):
51
  gc.collect()
 
67
  def get_duration(
68
  prompt,
69
  negative_prompt,
 
70
  guidance_scale,
71
  guidance_scale_2,
72
  steps,
 
77
  return steps * 15
78
 
79
  @spaces.GPU(duration=get_duration)
80
+ def generate_image(
81
  prompt,
82
  negative_prompt=default_negative_prompt,
83
+ guidance_scale = 3.5,
84
+ guidance_scale_2 = 4,
85
+ steps = 27,
 
86
  seed = 42,
87
  randomize_seed = False,
88
  progress=gr.Progress(track_tqdm=True),
 
98
  prompt (str): Text prompt describing the desired animation or motion.
99
  negative_prompt (str, optional): Negative prompt to avoid unwanted elements.
100
  Defaults to default_negative_prompt (contains unwanted visual artifacts).
 
 
101
  guidance_scale (float, optional): Controls adherence to the prompt. Higher values = more adherence.
102
  Defaults to 1.0. Range: 0.0-20.0.
103
  guidance_scale_2 (float, optional): Controls adherence to the prompt. Higher values = more adherence.
 
126
  - Generation time varies based on steps and duration (see get_duration function)
127
  """
128
 
129
+
130
  current_seed = random.randint(0, MAX_SEED) if randomize_seed else int(seed)
131
 
132
+ out_img = pipe(
133
  prompt=prompt,
134
  negative_prompt=negative_prompt,
135
+ height=1024,
136
+ width=1024,
137
+ num_frames=1,
138
  guidance_scale=float(guidance_scale),
139
  guidance_scale_2=float(guidance_scale_2),
140
  num_inference_steps=int(steps),
141
+ output_type="pil",
142
  generator=torch.Generator(device="cuda").manual_seed(current_seed),
143
+ ).frames[0][0]
144
 
145
+ return out_img, current_seed
 
 
 
 
 
146
 
147
  with gr.Blocks() as demo:
148
+ gr.Markdown("# Wan 2.2 T2I (14B)")
149
+ #gr.Markdown("run Wan 2.2 in just 6-8 steps, with [FusionX Phantom LoRA by DeeJayT](https://huggingface.co/vrgamedevgirl84/Wan14BT2VFusioniX/tree/main/FusionX_LoRa), compatible with 🧨 diffusers")
150
  with gr.Row():
151
  with gr.Column():
152
  prompt_input = gr.Textbox(label="Prompt", value=default_prompt_i2v)
153
+ #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.")
154
 
155
  with gr.Accordion("Advanced Settings", open=False):
156
  negative_prompt_input = gr.Textbox(label="Negative Prompt", value=default_negative_prompt, lines=3)
157
  seed_input = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=42, interactive=True)
158
  randomize_seed_checkbox = gr.Checkbox(label="Randomize seed", value=True, interactive=True)
159
+ steps_slider = gr.Slider(minimum=1, maximum=30, step=1, value=27, label="Inference Steps")
160
+ guidance_scale_input = gr.Slider(minimum=0.0, maximum=10.0, step=0.5, value=3.5, label="Guidance Scale - high noise stage")
161
+ guidance_scale_2_input = gr.Slider(minimum=0.0, maximum=10.0, step=0.5, value=4, label="Guidance Scale 2 - low noise stage")
162
 
163
+ generate_button = gr.Button("Generate Image", variant="primary")
164
  with gr.Column():
165
+ img_output = gr.Image(label="Generated Image", interactive=False)
166
 
167
+ ui_inputs = [
168
+ prompt_input,
169
+ negative_prompt_input,
170
  guidance_scale_input, guidance_scale_2_input, steps_slider, seed_input, randomize_seed_checkbox
171
  ]
172
+ generate_button.click(fn=generate_image, inputs=ui_inputs, outputs=[img_output, seed_input])
173
 
174
  gr.Examples(
175
  examples=[
176
  [
177
+ "Two anthropomorphic cats in comfy boxing gear and bright gloves fight intensely on a spotlighted stage."
178
  ],
179
  ],
180
+ inputs=[prompt_input], outputs=[img_output, seed_input], fn=generate_image, cache_examples="lazy"
181
  )
182
 
183
  if __name__ == "__main__":
optimization.py CHANGED
@@ -43,22 +43,22 @@ def optimize_pipeline_(pipeline: Callable[P, Any], *args: P.args, **kwargs: P.kw
43
  @spaces.GPU(duration=1500)
44
  def compile_transformer():
45
 
46
- pipeline.load_lora_weights(
47
- "Kijai/WanVideo_comfy",
48
- weight_name="Lightx2v/lightx2v_I2V_14B_480p_cfg_step_distill_rank128_bf16.safetensors",
49
- adapter_name="lightx2v"
50
- )
51
- kwargs_lora = {}
52
- kwargs_lora["load_into_transformer_2"] = True
53
- pipeline.load_lora_weights(
54
- "Kijai/WanVideo_comfy",
55
- weight_name="Wan22-Lightning/Wan2.2-Lightning_T2V-A14B-4steps-lora_LOW_fp16.safetensors",
56
- adapter_name="lightx2v_2", **kwargs_lora
57
- )
58
- pipeline.set_adapters(["lightx2v", "lightx2v_2"], adapter_weights=[1., 1.])
59
- pipeline.fuse_lora(adapter_names=["lightx2v"], lora_scale=3., components=["transformer"])
60
- pipeline.fuse_lora(adapter_names=["lightx2v_2"], lora_scale=1., components=["transformer_2"])
61
- pipeline.unload_lora_weights()
62
 
63
  with capture_component_call(pipeline, 'transformer') as call:
64
  pipeline(*args, **kwargs)
 
43
  @spaces.GPU(duration=1500)
44
  def compile_transformer():
45
 
46
+ # pipeline.load_lora_weights(
47
+ # "Kijai/WanVideo_comfy",
48
+ # weight_name="Lightx2v/lightx2v_I2V_14B_480p_cfg_step_distill_rank128_bf16.safetensors",
49
+ # adapter_name="lightx2v"
50
+ # )
51
+ # kwargs_lora = {}
52
+ # kwargs_lora["load_into_transformer_2"] = True
53
+ # pipeline.load_lora_weights(
54
+ # "Kijai/WanVideo_comfy",
55
+ # weight_name="Wan22-Lightning/Wan2.2-Lightning_T2V-A14B-4steps-lora_LOW_fp16.safetensors",
56
+ # adapter_name="lightx2v_2", **kwargs_lora
57
+ # )
58
+ # pipeline.set_adapters(["lightx2v", "lightx2v_2"], adapter_weights=[1., 1.])
59
+ # pipeline.fuse_lora(adapter_names=["lightx2v"], lora_scale=3., components=["transformer"])
60
+ # pipeline.fuse_lora(adapter_names=["lightx2v_2"], lora_scale=1., components=["transformer_2"])
61
+ # pipeline.unload_lora_weights()
62
 
63
  with capture_component_call(pipeline, 'transformer') as call:
64
  pipeline(*args, **kwargs)