radames commited on
Commit
0b2b63e
·
1 Parent(s): 9f0c5bb

Hyper-SD bytedance SDXL

Browse files
server/pipelines/controlnetHyperSDXL.py ADDED
@@ -0,0 +1,299 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from diffusers import (
2
+ StableDiffusionXLControlNetImg2ImgPipeline,
3
+ ControlNetModel,
4
+ AutoencoderKL,
5
+ DDIMScheduler,
6
+ )
7
+ from compel import Compel, ReturnedEmbeddingsType
8
+ import torch
9
+ from pipelines.utils.canny_gpu import SobelOperator
10
+ from huggingface_hub import hf_hub_download
11
+
12
+ try:
13
+ import intel_extension_for_pytorch as ipex # type: ignore
14
+ except:
15
+ pass
16
+
17
+ import psutil
18
+ from config import Args
19
+ from pydantic import BaseModel, Field
20
+ from PIL import Image
21
+ import math
22
+
23
+ controlnet_model = "diffusers/controlnet-canny-sdxl-1.0"
24
+ model_id = "stabilityai/stable-diffusion-xl-base-1.0"
25
+ taesd_model = "madebyollin/taesdxl"
26
+
27
+ default_prompt = "Portrait of The Terminator with , glare pose, detailed, intricate, full of colour, cinematic lighting, trending on artstation, 8k, hyperrealistic, focused, extreme details, unreal engine 5 cinematic, masterpiece"
28
+ default_negative_prompt = "blurry, low quality, render, 3D, oversaturated"
29
+ page_content = """
30
+ <h1 class="text-3xl font-bold">Real-Time SDXL Turbo</h1>
31
+ <h3 class="text-xl font-bold">Image-to-Image ControlNet</h3>
32
+ <p class="text-sm">
33
+ This demo showcases
34
+ <a
35
+ href="https://huggingface.co/stabilityai/sdxl-turbo"
36
+ target="_blank"
37
+ class="text-blue-500 underline hover:no-underline">SDXL Turbo</a>
38
+ Image to Image pipeline using
39
+ <a
40
+ href="https://huggingface.co/docs/diffusers/main/en/using-diffusers/sdxl_turbo"
41
+ target="_blank"
42
+ class="text-blue-500 underline hover:no-underline">Diffusers</a
43
+ > with a MJPEG stream server.
44
+ </p>
45
+ <p class="text-sm text-gray-500">
46
+ Change the prompt to generate different images, accepts <a
47
+ href="https://github.com/damian0815/compel/blob/main/doc/syntax.md"
48
+ target="_blank"
49
+ class="text-blue-500 underline hover:no-underline">Compel</a
50
+ > syntax.
51
+ </p>
52
+ """
53
+
54
+
55
+ class Pipeline:
56
+ class Info(BaseModel):
57
+ name: str = "controlnet+SDXL+Turbo"
58
+ title: str = "SDXL Turbo + Controlnet"
59
+ description: str = "Generates an image from a text prompt"
60
+ input_mode: str = "image"
61
+ page_content: str = page_content
62
+
63
+ class InputParams(BaseModel):
64
+ prompt: str = Field(
65
+ default_prompt,
66
+ title="Prompt",
67
+ field="textarea",
68
+ id="prompt",
69
+ )
70
+ negative_prompt: str = Field(
71
+ default_negative_prompt,
72
+ title="Negative Prompt",
73
+ field="textarea",
74
+ id="negative_prompt",
75
+ hide=True,
76
+ )
77
+ seed: int = Field(
78
+ 2159232, min=0, title="Seed", field="seed", hide=True, id="seed"
79
+ )
80
+ steps: int = Field(
81
+ 2, min=1, max=15, title="Steps", field="range", hide=True, id="steps"
82
+ )
83
+ width: int = Field(
84
+ 1024, min=2, max=15, title="Width", disabled=True, hide=True, id="width"
85
+ )
86
+ height: int = Field(
87
+ 1024, min=2, max=15, title="Height", disabled=True, hide=True, id="height"
88
+ )
89
+ guidance_scale: float = Field(
90
+ 0.0,
91
+ min=0,
92
+ max=10,
93
+ step=0.001,
94
+ title="Guidance Scale",
95
+ field="range",
96
+ hide=True,
97
+ id="guidance_scale",
98
+ )
99
+ strength: float = Field(
100
+ 0.5,
101
+ min=0.25,
102
+ max=1.0,
103
+ step=0.001,
104
+ title="Strength",
105
+ field="range",
106
+ hide=True,
107
+ id="strength",
108
+ )
109
+ controlnet_scale: float = Field(
110
+ 0.5,
111
+ min=0,
112
+ max=1.0,
113
+ step=0.001,
114
+ title="Controlnet Scale",
115
+ field="range",
116
+ hide=True,
117
+ id="controlnet_scale",
118
+ )
119
+ controlnet_start: float = Field(
120
+ 0.0,
121
+ min=0,
122
+ max=1.0,
123
+ step=0.001,
124
+ title="Controlnet Start",
125
+ field="range",
126
+ hide=True,
127
+ id="controlnet_start",
128
+ )
129
+ controlnet_end: float = Field(
130
+ 1.0,
131
+ min=0,
132
+ max=1.0,
133
+ step=0.001,
134
+ title="Controlnet End",
135
+ field="range",
136
+ hide=True,
137
+ id="controlnet_end",
138
+ )
139
+ canny_low_threshold: float = Field(
140
+ 0.31,
141
+ min=0,
142
+ max=1.0,
143
+ step=0.001,
144
+ title="Canny Low Threshold",
145
+ field="range",
146
+ hide=True,
147
+ id="canny_low_threshold",
148
+ )
149
+ canny_high_threshold: float = Field(
150
+ 0.125,
151
+ min=0,
152
+ max=1.0,
153
+ step=0.001,
154
+ title="Canny High Threshold",
155
+ field="range",
156
+ hide=True,
157
+ id="canny_high_threshold",
158
+ )
159
+ debug_canny: bool = Field(
160
+ False,
161
+ title="Debug Canny",
162
+ field="checkbox",
163
+ hide=True,
164
+ id="debug_canny",
165
+ )
166
+
167
+ def __init__(self, args: Args, device: torch.device, torch_dtype: torch.dtype):
168
+ controlnet_canny = ControlNetModel.from_pretrained(
169
+ controlnet_model, torch_dtype=torch_dtype
170
+ )
171
+ vae = AutoencoderKL.from_pretrained(
172
+ "madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch_dtype
173
+ )
174
+
175
+ if args.safety_checker:
176
+ self.pipe = StableDiffusionXLControlNetImg2ImgPipeline.from_pretrained(
177
+ model_id, controlnet=controlnet_canny, vae=vae, torch_dtype=torch_dtype
178
+ )
179
+ else:
180
+ self.pipe = StableDiffusionXLControlNetImg2ImgPipeline.from_pretrained(
181
+ model_id,
182
+ safety_checker=None,
183
+ controlnet=controlnet_canny,
184
+ vae=vae,
185
+ torch_dtype=torch_dtype,
186
+ )
187
+
188
+ self.pipe.load_lora_weights(
189
+ hf_hub_download("ByteDance/Hyper-SD", "Hyper-SDXL-2steps-lora.safetensors")
190
+ )
191
+ self.pipe.scheduler = DDIMScheduler.from_config(
192
+ self.pipe.scheduler.config, timestep_spacing="trailing"
193
+ )
194
+ self.pipe.fuse_lora()
195
+ self.canny_torch = SobelOperator(device=device)
196
+
197
+ if args.sfast:
198
+ from sfast.compilers.stable_diffusion_pipeline_compiler import (
199
+ compile,
200
+ CompilationConfig,
201
+ )
202
+
203
+ config = CompilationConfig.Default()
204
+ config.enable_xformers = True
205
+ config.enable_triton = True
206
+ config.enable_cuda_graph = True
207
+ self.pipe = compile(self.pipe, config=config)
208
+
209
+ self.pipe.set_progress_bar_config(disable=True)
210
+ self.pipe.to(device=device)
211
+ if device.type != "mps":
212
+ self.pipe.unet.to(memory_format=torch.channels_last)
213
+
214
+ if args.compel:
215
+ self.pipe.compel_proc = Compel(
216
+ tokenizer=[self.pipe.tokenizer, self.pipe.tokenizer_2],
217
+ text_encoder=[self.pipe.text_encoder, self.pipe.text_encoder_2],
218
+ returned_embeddings_type=ReturnedEmbeddingsType.PENULTIMATE_HIDDEN_STATES_NON_NORMALIZED,
219
+ requires_pooled=[False, True],
220
+ )
221
+
222
+ if args.torch_compile:
223
+ self.pipe.unet = torch.compile(
224
+ self.pipe.unet, mode="reduce-overhead", fullgraph=True
225
+ )
226
+ self.pipe.vae = torch.compile(
227
+ self.pipe.vae, mode="reduce-overhead", fullgraph=True
228
+ )
229
+ self.pipe(
230
+ prompt="warmup",
231
+ image=[Image.new("RGB", (768, 768))],
232
+ control_image=[Image.new("RGB", (768, 768))],
233
+ )
234
+
235
+ def predict(self, params: "Pipeline.InputParams") -> Image.Image:
236
+ generator = torch.manual_seed(params.seed)
237
+
238
+ prompt = params.prompt
239
+ negative_prompt = params.negative_prompt
240
+ prompt_embeds = None
241
+ pooled_prompt_embeds = None
242
+ negative_prompt_embeds = None
243
+ negative_pooled_prompt_embeds = None
244
+ if hasattr(self.pipe, "compel_proc"):
245
+ _prompt_embeds, pooled_prompt_embeds = self.pipe.compel_proc(
246
+ [params.prompt, params.negative_prompt]
247
+ )
248
+ prompt = None
249
+ negative_prompt = None
250
+ prompt_embeds = _prompt_embeds[0:1]
251
+ pooled_prompt_embeds = pooled_prompt_embeds[0:1]
252
+ negative_prompt_embeds = _prompt_embeds[1:2]
253
+ negative_pooled_prompt_embeds = pooled_prompt_embeds[1:2]
254
+
255
+ control_image = self.canny_torch(
256
+ params.image, params.canny_low_threshold, params.canny_high_threshold
257
+ )
258
+ steps = params.steps
259
+ strength = params.strength
260
+ if int(steps * strength) < 1:
261
+ steps = math.ceil(1 / max(0.10, strength))
262
+
263
+ results = self.pipe(
264
+ image=params.image,
265
+ control_image=control_image,
266
+ prompt=prompt,
267
+ negative_prompt=negative_prompt,
268
+ prompt_embeds=prompt_embeds,
269
+ pooled_prompt_embeds=pooled_prompt_embeds,
270
+ negative_prompt_embeds=negative_prompt_embeds,
271
+ negative_pooled_prompt_embeds=negative_pooled_prompt_embeds,
272
+ generator=generator,
273
+ strength=strength,
274
+ num_inference_steps=steps,
275
+ guidance_scale=params.guidance_scale,
276
+ width=params.width,
277
+ height=params.height,
278
+ output_type="pil",
279
+ controlnet_conditioning_scale=params.controlnet_scale,
280
+ control_guidance_start=params.controlnet_start,
281
+ control_guidance_end=params.controlnet_end,
282
+ )
283
+
284
+ nsfw_content_detected = (
285
+ results.nsfw_content_detected[0]
286
+ if "nsfw_content_detected" in results
287
+ else False
288
+ )
289
+ if nsfw_content_detected:
290
+ return None
291
+ result_image = results.images[0]
292
+ if params.debug_canny:
293
+ # paste control_image on top of result_image
294
+ w0, h0 = (200, 200)
295
+ control_image = control_image.resize((w0, h0))
296
+ w1, h1 = result_image.size
297
+ result_image.paste(control_image, (w1 - w0, h1 - h0))
298
+
299
+ return result_image