Spaces:
Sleeping
Sleeping
add initial stuff
Browse files- Dockerfile +25 -0
- app.py +182 -0
- requirements.txt +2 -0
Dockerfile
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM diffusers/diffusers-pytorch-compile-cuda
|
2 |
+
|
3 |
+
WORKDIR /code
|
4 |
+
|
5 |
+
COPY ./requirements.txt /code/requirements.txt
|
6 |
+
|
7 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
8 |
+
|
9 |
+
# Set up a new user named "user" with user ID 1000
|
10 |
+
RUN useradd -m -u 1000 user
|
11 |
+
|
12 |
+
# Switch to the "user" user
|
13 |
+
USER user
|
14 |
+
|
15 |
+
# Set home to the user's home directory
|
16 |
+
ENV HOME=/home/user \
|
17 |
+
PATH=/home/user/.local/bin:$PATH
|
18 |
+
|
19 |
+
# Set the working directory to the user's home directory
|
20 |
+
WORKDIR $HOME/app
|
21 |
+
|
22 |
+
# Copy the current directory contents into the container at $HOME/app setting the owner to the user
|
23 |
+
COPY --chown=user . $HOME/app
|
24 |
+
|
25 |
+
CMD ["python", "app.py"]
|
app.py
ADDED
@@ -0,0 +1,182 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from diffusers import (
|
4 |
+
StableDiffusionXLControlNetPipeline,
|
5 |
+
DiffusionPipeline,
|
6 |
+
StableDiffusionImg2ImgPipeline,
|
7 |
+
StableDiffusionInpaintPipeline,
|
8 |
+
StableDiffusionAdapterPipeline,
|
9 |
+
StableDiffusionControlNetPipeline,
|
10 |
+
StableDiffusionXLAdapterPipeline,
|
11 |
+
StableDiffusionXLImg2ImgPipeline,
|
12 |
+
StableDiffusionXLInpaintPipeline,
|
13 |
+
ControlNetModel,
|
14 |
+
T2IAdapter,
|
15 |
+
)
|
16 |
+
import time
|
17 |
+
|
18 |
+
|
19 |
+
dtype = torch.float16
|
20 |
+
device = torch.device("cuda")
|
21 |
+
|
22 |
+
pipeline_mapping = {
|
23 |
+
"SD T2I": (DiffusionPipeline, "runwayml/stable-diffusion-v1-5"),
|
24 |
+
"SD I2I": (StableDiffusionImg2ImgPipeline, "runwayml/stable-diffusion-v1-5"),
|
25 |
+
"SD Inpainting": (
|
26 |
+
StableDiffusionInpaintPipeline,
|
27 |
+
"runwayml/stable-diffusion-inpainting",
|
28 |
+
),
|
29 |
+
"SD ControlNet": (
|
30 |
+
StableDiffusionControlNetPipeline,
|
31 |
+
"runwayml/stable-diffusion-v1-5",
|
32 |
+
"lllyasviel/sd-controlnet-canny",
|
33 |
+
),
|
34 |
+
"SD T2I Adapters": (
|
35 |
+
StableDiffusionAdapterPipeline,
|
36 |
+
"CompVis/stable-diffusion-v1-4" "TencentARC/t2iadapter_canny_sd14v1",
|
37 |
+
),
|
38 |
+
"SDXL T2I": (DiffusionPipeline, "stabilityai/stable-diffusion-xl-base-1.0"),
|
39 |
+
"SDXL I2I": (
|
40 |
+
StableDiffusionXLImg2ImgPipeline,
|
41 |
+
"stabilityai/stable-diffusion-xl-base-1.0",
|
42 |
+
),
|
43 |
+
"SDXL Inpainting": (
|
44 |
+
StableDiffusionXLInpaintPipeline,
|
45 |
+
"diffusers/stable-diffusion-xl-1.0-inpainting-0.1",
|
46 |
+
),
|
47 |
+
"SDXL ControlNet": (
|
48 |
+
StableDiffusionXLControlNetPipeline,
|
49 |
+
"stabilityai/stable-diffusion-xl-base-1.0",
|
50 |
+
"diffusers/controlnet-canny-sdxl-1.0",
|
51 |
+
),
|
52 |
+
"SDXL T2I Adapters": (
|
53 |
+
StableDiffusionXLAdapterPipeline,
|
54 |
+
"stabilityai/stable-diffusion-xl-base-1.0",
|
55 |
+
"TencentARC/t2i-adapter-canny-sdxl-1.0",
|
56 |
+
),
|
57 |
+
}
|
58 |
+
|
59 |
+
|
60 |
+
def load_pipeline(
|
61 |
+
pipeline_to_benchmark: str,
|
62 |
+
use_channels_last: bool = False,
|
63 |
+
do_torch_compile: bool = False,
|
64 |
+
):
|
65 |
+
# Get pipeline details.
|
66 |
+
pipeline_details = pipeline_mapping[pipeline_to_benchmark]
|
67 |
+
pipeline_cls = pipeline_details[0]
|
68 |
+
pipeline_ckpt = pipeline_details[1]
|
69 |
+
|
70 |
+
# Load adapter if needed.
|
71 |
+
if "ControlNet" in pipeline_to_benchmark:
|
72 |
+
controlnet_ckpt = pipeline_details[2]
|
73 |
+
controlnet = ControlNetModel.from_pretrained(
|
74 |
+
controlnet_ckpt, variant="fp16", torch_dtype=torch.float16
|
75 |
+
).to(device)
|
76 |
+
elif "Adapters" in pipeline_to_benchmark:
|
77 |
+
adapter_clpt = pipeline_details[2]
|
78 |
+
adapter = T2IAdapter.from_pretrained(
|
79 |
+
adapter_clpt, variant="fp16", torch_dtype=torch.float16
|
80 |
+
).to(device)
|
81 |
+
|
82 |
+
# Load pipeline.
|
83 |
+
if (
|
84 |
+
"ControlNet" not in pipeline_to_benchmark
|
85 |
+
or "Adapters" not in pipeline_to_benchmark
|
86 |
+
):
|
87 |
+
pipeline = pipeline_cls.from_pretrained(
|
88 |
+
pipeline_ckpt, variant="fp16", torch_dtype=dtype
|
89 |
+
)
|
90 |
+
|
91 |
+
elif "ControlNet" in pipeline_to_benchmark:
|
92 |
+
pipeline = pipeline_cls.from_pretrained(pipeline_ckpt, controlnet=controlnet)
|
93 |
+
elif "Adapters" in pipeline_to_benchmark:
|
94 |
+
pipeline = pipeline_cls.from_pretrained(pipeline_ckpt, adapter=adapter)
|
95 |
+
pipeline.to(device)
|
96 |
+
|
97 |
+
# Optionally set memory layout.
|
98 |
+
if use_channels_last:
|
99 |
+
pipeline.unet.to(memory_format=torch.channels_last)
|
100 |
+
|
101 |
+
if hasattr(pipeline, "controlnet"):
|
102 |
+
pipeline.controlnet.to(memory_format=torch.channels_last)
|
103 |
+
elif hasattr(pipeline, "adapter"):
|
104 |
+
pipeline.adapter.to(memory_format=torch.channels_last)
|
105 |
+
|
106 |
+
# Optional torch compilation.
|
107 |
+
if do_torch_compile:
|
108 |
+
pipeline.unet = torch.compile(
|
109 |
+
pipeline.unet, mode="reduce-overhead", fullgraph=True
|
110 |
+
)
|
111 |
+
if hasattr(pipeline, "controlnet"):
|
112 |
+
pipeline.controlnet = torch.compile(
|
113 |
+
pipeline.controlnet, mode="reduce-overhead", fullgraph=True
|
114 |
+
)
|
115 |
+
elif hasattr(pipeline, "adapter"):
|
116 |
+
pipeline.adapter = torch.compile(
|
117 |
+
pipeline.adapter, mode="reduce-overhead", fullgraph=True
|
118 |
+
)
|
119 |
+
|
120 |
+
return pipeline
|
121 |
+
|
122 |
+
|
123 |
+
def generate(
|
124 |
+
pipeline_to_benchmark: str,
|
125 |
+
num_images_per_prompt: int = 1,
|
126 |
+
use_channels_last: bool = False,
|
127 |
+
do_torch_compile: bool = False,
|
128 |
+
):
|
129 |
+
print("Start...")
|
130 |
+
print("Torch version", torch.__version__)
|
131 |
+
print("Torch CUDA version", torch.version.cuda)
|
132 |
+
|
133 |
+
pipeline = load_pipeline(
|
134 |
+
pipeline_to_benchmark=pipeline_to_benchmark,
|
135 |
+
use_channels_last=use_channels_last,
|
136 |
+
do_torch_compile=do_torch_compile,
|
137 |
+
)
|
138 |
+
for _ in range(3):
|
139 |
+
prompt = 77 * "a"
|
140 |
+
num_inference_steps = 20
|
141 |
+
start_time = time.time()
|
142 |
+
_ = pipeline(
|
143 |
+
prompt,
|
144 |
+
num_images_per_prompt=num_images_per_prompt,
|
145 |
+
num_inference_steps=num_inference_steps,
|
146 |
+
).images
|
147 |
+
end_time = time.time()
|
148 |
+
|
149 |
+
print(f"For {num_inference_steps} steps", end_time - start_time)
|
150 |
+
print("Avg per step", (end_time - start_time) / num_inference_steps)
|
151 |
+
|
152 |
+
|
153 |
+
with gr.Blocks() as demo:
|
154 |
+
do_torch_compile = gr.Checkbox(label="Enable torch.compile()?")
|
155 |
+
use_channels_last = gr.Checkbox(label="Use `channels_last` memory layout?")
|
156 |
+
pipeline_to_benchmark = (
|
157 |
+
gr.Dropdown(
|
158 |
+
list(pipeline_mapping.keys()),
|
159 |
+
value=["Stable Diffusion V1.5"],
|
160 |
+
multiselect=False,
|
161 |
+
label="Pipeline to benchmark",
|
162 |
+
),
|
163 |
+
)
|
164 |
+
batch_size = gr.Slider(
|
165 |
+
label="Number of images per prompt",
|
166 |
+
minimum=1,
|
167 |
+
maximum=16,
|
168 |
+
step=1,
|
169 |
+
value=1,
|
170 |
+
)
|
171 |
+
btn = gr.Button("Benchmark!").style(
|
172 |
+
margin=False,
|
173 |
+
rounded=(False, True, True, False),
|
174 |
+
full_width=False,
|
175 |
+
)
|
176 |
+
|
177 |
+
btn.click(
|
178 |
+
fn=generate,
|
179 |
+
inputs=[pipeline_to_benchmark, batch_size, use_channels_last, do_torch_compile],
|
180 |
+
)
|
181 |
+
|
182 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
diffusers @ git+https://github.com/huggingface/diffusers
|