radames commited on
Commit
65694f3
·
1 Parent(s): 38b725a

ByteDance/Hyper-SD: Hyper-SD15 Unified

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