Commit
·
f16af8d
1
Parent(s):
4052324
up
Browse files
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from diffusers import AutoPipelineForText2Image
|
4 |
+
import time
|
5 |
+
|
6 |
+
USE_TORCH_COMPILE = False
|
7 |
+
|
8 |
+
dtype = torch.float16
|
9 |
+
device = torch.device("cuda:0")
|
10 |
+
|
11 |
+
pipeline = AutoPipelineForText2Image.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", variant="fp16", torch_dtype=torch.float16)
|
12 |
+
pipeline.to("cuda")
|
13 |
+
|
14 |
+
|
15 |
+
if USE_TORCH_COMPILE:
|
16 |
+
pipeline.unet = torch.compile(pipeline.unet, mode="reduce-overhead", fullgraph=True)
|
17 |
+
|
18 |
+
def generate(prompt_len: int, num_images_per_prompt: int = 1):
|
19 |
+
prompt = prompt_len * "a"
|
20 |
+
num_inference_steps = 40
|
21 |
+
start_time = time.time()
|
22 |
+
pipeline(prompt, num_images_per_prompt=num_images_per_prompt, num_inference_steps=num_inference_steps).images
|
23 |
+
end_time = time.time()
|
24 |
+
|
25 |
+
|
26 |
+
print(f"For {num_inference_steps} steps", end_time - start_time)
|
27 |
+
print("Avg per step", (end_time - start_time) / num_inference_steps)
|
28 |
+
|
29 |
+
|
30 |
+
with gr.Blocks(css="style.css") as demo:
|
31 |
+
batch_size = gr.Slider(
|
32 |
+
label="Batch size",
|
33 |
+
minimum=0,
|
34 |
+
maximum=8,
|
35 |
+
step=1,
|
36 |
+
value=0,
|
37 |
+
)
|
38 |
+
prompt_len = gr.Slider(
|
39 |
+
label="Prompt len",
|
40 |
+
minimum=1,
|
41 |
+
maximum=77,
|
42 |
+
step=20,
|
43 |
+
value=1,
|
44 |
+
)
|
45 |
+
btn = gr.Button("Benchmark!").style(
|
46 |
+
margin=False,
|
47 |
+
rounded=(False, True, True, False),
|
48 |
+
full_width=False,
|
49 |
+
)
|
50 |
+
|
51 |
+
btn.click(fn=generate, inputs=[batch_size, prompt_len])
|