Spaces:
Runtime error
Runtime error
jadechoghari
commited on
Commit
β’
730c74d
1
Parent(s):
0bb596a
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import spaces
|
4 |
+
from diffusers import FluxPipeline
|
5 |
+
|
6 |
+
|
7 |
+
try:
|
8 |
+
from torchao.quantization import autoquant
|
9 |
+
except ImportError:
|
10 |
+
raise ImportError("torchao is not installed. Please install it to use the optimized pipeline.")
|
11 |
+
|
12 |
+
# normal FluxPipeline
|
13 |
+
pipeline_normal = FluxPipeline.from_pretrained(
|
14 |
+
"black-forest-labs/FLUX.1-dev",
|
15 |
+
torch_dtype=torch.bfloat16
|
16 |
+
).to("cuda")
|
17 |
+
|
18 |
+
# optimized FluxPipeline
|
19 |
+
pipeline_optimized = FluxPipeline.from_pretrained(
|
20 |
+
"black-forest-labs/FLUX.1-dev",
|
21 |
+
torch_dtype=torch.bfloat16
|
22 |
+
).to("cuda")
|
23 |
+
|
24 |
+
pipeline_optimized.transformer.to(memory_format=torch.channels_last)
|
25 |
+
pipeline_optimized.transformer = torch.compile(
|
26 |
+
pipeline_optimized.transformer,
|
27 |
+
mode="max-autotune",
|
28 |
+
fullgraph=True
|
29 |
+
)
|
30 |
+
|
31 |
+
pipeline_optimized.transformer = autoquant(
|
32 |
+
pipeline_optimized.transformer,
|
33 |
+
error_on_unseen=False
|
34 |
+
)
|
35 |
+
|
36 |
+
@spaces.GPU
|
37 |
+
def generate_images(prompt, guidance_scale, num_inference_steps):
|
38 |
+
# generate image with normal pipeline
|
39 |
+
image_normal = pipeline_normal(
|
40 |
+
prompt=prompt,
|
41 |
+
guidance_scale=guidance_scale,
|
42 |
+
num_inference_steps=int(num_inference_steps)
|
43 |
+
).images[0]
|
44 |
+
|
45 |
+
# generate image with optimized pipeline
|
46 |
+
image_optimized = pipeline_optimized(
|
47 |
+
prompt=prompt,
|
48 |
+
guidance_scale=guidance_scale,
|
49 |
+
num_inference_steps=int(num_inference_steps)
|
50 |
+
).images[0]
|
51 |
+
|
52 |
+
return image_normal, image_optimized
|
53 |
+
|
54 |
+
# set up Gradio interface
|
55 |
+
demo = gr.Interface(
|
56 |
+
fn=generate_images,
|
57 |
+
inputs=[
|
58 |
+
gr.Textbox(lines=2, placeholder="Enter your prompt here...", label="Prompt"),
|
59 |
+
gr.Slider(1.0, 10.0, step=0.5, value=3.5, label="Guidance Scale"),
|
60 |
+
gr.Slider(10, 100, step=1, value=50, label="Number of Inference Steps")
|
61 |
+
],
|
62 |
+
outputs=[
|
63 |
+
gr.Image(type="pil", label="Normal FluxPipeline"),
|
64 |
+
gr.Image(type="pil", label="Optimized FluxPipeline")
|
65 |
+
],
|
66 |
+
title="FluxPipeline Comparison",
|
67 |
+
description="Compare images generated by the normal FluxPipeline and the optimized one using torchao and torch.compile()."
|
68 |
+
)
|
69 |
+
|
70 |
+
demo.launch()
|