sayakpaul HF Staff commited on
Commit
f5a3617
·
1 Parent(s): 699b46e
Files changed (2) hide show
  1. app.py +2 -2
  2. optimization.py +15 -20
app.py CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
2
  import torch
3
  from diffusers import QwenImagePipeline
4
  from qwenimage.qwen_fa3_processor import QwenDoubleStreamAttnProcessorFA3
5
- from optimization import get_compiled_transformer
6
  from hub_utils import _push_compiled_graph_to_hub
7
  from huggingface_hub import whoami
8
  import spaces
@@ -16,7 +16,7 @@ pipe = QwenImagePipeline.from_pretrained("Qwen/Qwen-Image", torch_dtype=dtype).t
16
  pipe.transformer.set_attn_processor(QwenDoubleStreamAttnProcessorFA3())
17
 
18
  # --- Ahead-of-time compilation ---
19
- compiled_transformer = get_compiled_transformer(pipe, prompt="prompt")
20
 
21
  @spaces.GPU(duration=120)
22
  def push_to_hub(repo_id, filename, oauth_token: gr.OAuthToken):
 
2
  import torch
3
  from diffusers import QwenImagePipeline
4
  from qwenimage.qwen_fa3_processor import QwenDoubleStreamAttnProcessorFA3
5
+ from optimization import compile_transformer
6
  from hub_utils import _push_compiled_graph_to_hub
7
  from huggingface_hub import whoami
8
  import spaces
 
16
  pipe.transformer.set_attn_processor(QwenDoubleStreamAttnProcessorFA3())
17
 
18
  # --- Ahead-of-time compilation ---
19
+ compiled_transformer = compile_transformer(pipe, prompt="prompt")
20
 
21
  @spaces.GPU(duration=120)
22
  def push_to_hub(repo_id, filename, oauth_token: gr.OAuthToken):
optimization.py CHANGED
@@ -38,23 +38,18 @@ INDUCTOR_CONFIGS = {
38
  }
39
 
40
 
41
- def get_compiled_transformer(pipeline: Callable[P, Any], *args: P.args, **kwargs: P.kwargs):
42
-
43
- @spaces.GPU(duration=1500)
44
- def compile_transformer():
45
- with spaces.aoti_capture(pipeline.transformer) as call:
46
- pipeline(*args, **kwargs)
47
-
48
- dynamic_shapes = tree_map(lambda t: None, call.kwargs)
49
- dynamic_shapes |= TRANSFORMER_DYNAMIC_SHAPES
50
-
51
- exported = torch.export.export(
52
- mod=pipeline.transformer,
53
- args=call.args,
54
- kwargs=call.kwargs,
55
- dynamic_shapes=dynamic_shapes,
56
- )
57
- return spaces.aoti_compile(exported, INDUCTOR_CONFIGS)
58
-
59
- compiled = compile_transformer()
60
- return compiled
 
38
  }
39
 
40
 
41
+ @spaces.GPU(duration=1500)
42
+ def compile_transformer(pipeline: Callable[P, Any], *args: P.args, **kwargs: P.kwargs):
43
+ with spaces.aoti_capture(pipeline.transformer) as call:
44
+ pipeline(*args, **kwargs)
45
+
46
+ dynamic_shapes = tree_map(lambda t: None, call.kwargs)
47
+ dynamic_shapes |= TRANSFORMER_DYNAMIC_SHAPES
48
+
49
+ exported = torch.export.export(
50
+ mod=pipeline.transformer,
51
+ args=call.args,
52
+ kwargs=call.kwargs,
53
+ dynamic_shapes=dynamic_shapes,
54
+ )
55
+ return spaces.aoti_compile(exported, INDUCTOR_CONFIGS)