forked from replicate/cog-sdxl
-
Notifications
You must be signed in to change notification settings - Fork 3
/
predict_deprecated.py
executable file
·426 lines (374 loc) · 15.5 KB
/
predict_deprecated.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import json
import os
import re
import shutil
import subprocess
import time
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
import numpy as np
import torch
from cog import BasePredictor, Input, Path
from diffusers import (
DDIMScheduler,
DiffusionPipeline,
DPMSolverMultistepScheduler,
EulerAncestralDiscreteScheduler,
EulerDiscreteScheduler,
HeunDiscreteScheduler,
PNDMScheduler,
StableDiffusionXLImg2ImgPipeline,
StableDiffusionXLInpaintPipeline,
)
from diffusers.models.attention_processor import LoRAAttnProcessor2_0
from diffusers.pipelines.stable_diffusion.safety_checker import (
StableDiffusionSafetyChecker,
)
from diffusers.utils import load_image
from safetensors import safe_open
from safetensors.torch import load_file
from transformers import CLIPImageProcessor
from dataset_and_utils import TokenEmbeddingsHandler
SDXL_MODEL_CACHE = "./sdxl-cache"
REFINER_MODEL_CACHE = "./refiner-cache"
SAFETY_CACHE = "./safety-cache"
FEATURE_EXTRACTOR = "./feature-extractor"
SDXL_URL = "https://weights.replicate.delivery/default/sdxl/sdxl-vae-fix-1.0.tar"
REFINER_URL = (
"https://weights.replicate.delivery/default/sdxl/refiner-no-vae-no-encoder-1.0.tar"
)
SAFETY_URL = "https://weights.replicate.delivery/default/sdxl/safety-1.0.tar"
SDXL_MODEL_CACHE = "./models/juggernaut_v6.safetensors"
SDXL_URL = "https://edenartlab-lfs.s3.amazonaws.com/models/checkpoints/juggernautXL_v6.safetensors"
class KarrasDPM:
def from_config(config):
return DPMSolverMultistepScheduler.from_config(config, use_karras_sigmas=True)
SCHEDULERS = {
"DDIM": DDIMScheduler,
"DPMSolverMultistep": DPMSolverMultistepScheduler,
"HeunDiscrete": HeunDiscreteScheduler,
"KarrasDPM": KarrasDPM,
"K_EULER_ANCESTRAL": EulerAncestralDiscreteScheduler,
"K_EULER": EulerDiscreteScheduler,
"PNDM": PNDMScheduler,
}
def download_weights(url, dest):
start = time.time()
print("downloading url: ", url)
print("downloading to: ", dest)
try:
if url.endswith(".tar"):
subprocess.check_call(["pget", "-q", "-x", url, dest])
else:
subprocess.check_call(["wget", "-q", "-O", dest, url])
except subprocess.CalledProcessError as e:
print("Error occurred while downloading:")
print("Exit status:", e.returncode)
print("Output:", e.output)
except Exception as e:
print("An unexpected error occurred:", e)
print(f"Downloading {url} took {time.time() - start} seconds")
class Predictor(BasePredictor):
def load_trained_weights(self, weights, pipe):
local_weights_cache = "./trained-model"
if not os.path.exists(local_weights_cache):
# pget -x doesn't like replicate.delivery
weights = str(weights)
weights = weights.replace(
"replicate.delivery/pbxt", "storage.googleapis.com/replicate-files"
)
download_weights(weights, local_weights_cache)
# load UNET
print("Loading fine-tuned model")
self.is_lora = False
maybe_unet_path = os.path.join(local_weights_cache, "unet.safetensors")
if not os.path.exists(maybe_unet_path):
print("Does not have Unet. assume we are using LoRA")
self.is_lora = True
if not self.is_lora:
print("Loading Unet")
new_unet_params = load_file(
os.path.join(local_weights_cache, "unet.safetensors")
)
sd = pipe.unet.state_dict()
sd.update(new_unet_params)
pipe.unet.load_state_dict(sd)
else:
print("Loading Unet LoRA")
unet = pipe.unet
tensors = load_file(os.path.join(local_weights_cache, "lora.safetensors"))
unet = pipe.unet
unet_lora_attn_procs = {}
name_rank_map = {}
for tk, tv in tensors.items():
# up is N, d
if tk.endswith("up.weight"):
proc_name = ".".join(tk.split(".")[:-3])
r = tv.shape[1]
name_rank_map[proc_name] = r
for name, attn_processor in unet.attn_processors.items():
cross_attention_dim = (
None
if name.endswith("attn1.processor")
else unet.config.cross_attention_dim
)
if name.startswith("mid_block"):
hidden_size = unet.config.block_out_channels[-1]
elif name.startswith("up_blocks"):
block_id = int(name[len("up_blocks.")])
hidden_size = list(reversed(unet.config.block_out_channels))[
block_id
]
elif name.startswith("down_blocks"):
block_id = int(name[len("down_blocks.")])
hidden_size = unet.config.block_out_channels[block_id]
module = LoRAAttnProcessor2_0(
hidden_size=hidden_size,
cross_attention_dim=cross_attention_dim,
rank=name_rank_map[name],
)
unet_lora_attn_procs[name] = module.to("cuda")
unet.set_attn_processor(unet_lora_attn_procs)
unet.load_state_dict(tensors, strict=False)
# load text
handler = TokenEmbeddingsHandler(
[pipe.text_encoder, pipe.text_encoder_2], [pipe.tokenizer, pipe.tokenizer_2]
)
handler.load_embeddings(os.path.join(local_weights_cache, "embeddings.pti"))
# load params
with open(os.path.join(local_weights_cache, "special_params.json"), "r") as f:
params = json.load(f)
self.token_map = params
self.tuned_model = True
def setup(self, weights: Optional[Path] = None):
"""Load the model into memory to make running multiple predictions efficient"""
start = time.time()
self.tuned_model = False
print("Loading safety checker...")
if not os.path.exists(SAFETY_CACHE):
download_weights(SAFETY_URL, SAFETY_CACHE)
self.safety_checker = StableDiffusionSafetyChecker.from_pretrained(
SAFETY_CACHE, torch_dtype=torch.float16
).to("cuda")
self.feature_extractor = CLIPImageProcessor.from_pretrained(FEATURE_EXTRACTOR)
if not os.path.exists(SDXL_MODEL_CACHE):
download_weights(SDXL_URL, SDXL_MODEL_CACHE)
print("Loading sdxl txt2img pipeline...")
self.txt2img_pipe = DiffusionPipeline.from_pretrained(
SDXL_MODEL_CACHE,
torch_dtype=torch.float16,
use_safetensors=True,
variant="fp16",
)
self.is_lora = False
if weights or os.path.exists("./trained-model"):
self.load_trained_weights(weights, self.txt2img_pipe)
self.txt2img_pipe.to("cuda")
print("Loading SDXL img2img pipeline...")
self.img2img_pipe = StableDiffusionXLImg2ImgPipeline(
vae=self.txt2img_pipe.vae,
text_encoder=self.txt2img_pipe.text_encoder,
text_encoder_2=self.txt2img_pipe.text_encoder_2,
tokenizer=self.txt2img_pipe.tokenizer,
tokenizer_2=self.txt2img_pipe.tokenizer_2,
unet=self.txt2img_pipe.unet,
scheduler=self.txt2img_pipe.scheduler,
)
self.img2img_pipe.to("cuda")
print("Loading SDXL inpaint pipeline...")
self.inpaint_pipe = StableDiffusionXLInpaintPipeline(
vae=self.txt2img_pipe.vae,
text_encoder=self.txt2img_pipe.text_encoder,
text_encoder_2=self.txt2img_pipe.text_encoder_2,
tokenizer=self.txt2img_pipe.tokenizer,
tokenizer_2=self.txt2img_pipe.tokenizer_2,
unet=self.txt2img_pipe.unet,
scheduler=self.txt2img_pipe.scheduler,
)
self.inpaint_pipe.to("cuda")
print("Loading SDXL refiner pipeline...")
# FIXME(ja): should the vae/text_encoder_2 be loaded from SDXL always?
# - in the case of fine-tuned SDXL should we still?
# FIXME(ja): if the answer to above is use VAE/Text_Encoder_2 from fine-tune
# what does this imply about lora + refiner? does the refiner need to know about
if not os.path.exists(REFINER_MODEL_CACHE):
download_weights(REFINER_URL, REFINER_MODEL_CACHE)
print("Loading refiner pipeline...")
self.refiner = DiffusionPipeline.from_pretrained(
REFINER_MODEL_CACHE,
text_encoder_2=self.txt2img_pipe.text_encoder_2,
vae=self.txt2img_pipe.vae,
torch_dtype=torch.float16,
use_safetensors=True,
variant="fp16",
)
self.refiner.to("cuda")
print("setup took: ", time.time() - start)
# self.txt2img_pipe.__class__.encode_prompt = new_encode_prompt
def load_image(self, path):
shutil.copyfile(path, "/tmp/image.png")
return load_image("/tmp/image.png").convert("RGB")
def run_safety_checker(self, image):
safety_checker_input = self.feature_extractor(image, return_tensors="pt").to(
"cuda"
)
np_image = [np.array(val) for val in image]
image, has_nsfw_concept = self.safety_checker(
images=np_image,
clip_input=safety_checker_input.pixel_values.to(torch.float16),
)
return image, has_nsfw_concept
@torch.inference_mode()
def predict(
self,
prompt: str = Input(
description="Input prompt",
default="An astronaut riding a rainbow unicorn",
),
negative_prompt: str = Input(
description="Input Negative Prompt",
default="",
),
image: Path = Input(
description="Input image for img2img or inpaint mode",
default=None,
),
mask: Path = Input(
description="Input mask for inpaint mode. Black areas will be preserved, white areas will be inpainted.",
default=None,
),
width: int = Input(
description="Width of output image",
default=1024,
),
height: int = Input(
description="Height of output image",
default=1024,
),
num_outputs: int = Input(
description="Number of images to output.",
ge=1,
le=4,
default=1,
),
scheduler: str = Input(
description="scheduler",
choices=SCHEDULERS.keys(),
default="K_EULER",
),
num_inference_steps: int = Input(
description="Number of denoising steps", ge=1, le=500, default=50
),
guidance_scale: float = Input(
description="Scale for classifier-free guidance", ge=1, le=50, default=7.5
),
prompt_strength: float = Input(
description="Prompt strength when using img2img / inpaint. 1.0 corresponds to full destruction of information in image",
ge=0.0,
le=1.0,
default=0.8,
),
seed: int = Input(
description="Random seed. Leave blank to randomize the seed", default=None
),
refine: str = Input(
description="Which refine style to use",
choices=["no_refiner", "expert_ensemble_refiner", "base_image_refiner"],
default="no_refiner",
),
high_noise_frac: float = Input(
description="For expert_ensemble_refiner, the fraction of noise to use",
default=0.8,
le=1.0,
ge=0.0,
),
refine_steps: int = Input(
description="For base_image_refiner, the number of steps to refine, defaults to num_inference_steps",
default=None,
),
apply_watermark: bool = Input(
description="Applies a watermark to enable determining if an image is generated in downstream applications. If you have other provisions for generating or deploying images safely, you can use this to disable watermarking.",
default=True,
),
lora_scale: float = Input(
description="LoRA additive scale. Only applicable on trained models.",
ge=0.0,
le=1.0,
default=0.6,
),
) -> List[Path]:
"""Run a single prediction on the model"""
if seed is None:
seed = int.from_bytes(os.urandom(2), "big")
print(f"Using seed: {seed}")
sdxl_kwargs = {}
if self.tuned_model:
# consistency with fine-tuning API
for k, v in self.token_map.items():
prompt = prompt.replace(k, v)
print(f"Prompt: {prompt}")
if image and mask:
print("inpainting mode")
sdxl_kwargs["image"] = self.load_image(image)
sdxl_kwargs["mask_image"] = self.load_image(mask)
sdxl_kwargs["strength"] = prompt_strength
sdxl_kwargs["width"] = width
sdxl_kwargs["height"] = height
pipe = self.inpaint_pipe
elif image:
print("img2img mode")
sdxl_kwargs["image"] = self.load_image(image)
sdxl_kwargs["strength"] = prompt_strength
pipe = self.img2img_pipe
else:
print("txt2img mode")
sdxl_kwargs["width"] = width
sdxl_kwargs["height"] = height
pipe = self.txt2img_pipe
if refine == "expert_ensemble_refiner":
sdxl_kwargs["output_type"] = "latent"
sdxl_kwargs["denoising_end"] = high_noise_frac
elif refine == "base_image_refiner":
sdxl_kwargs["output_type"] = "latent"
if not apply_watermark:
# toggles watermark for this prediction
watermark_cache = pipe.watermark
pipe.watermark = None
self.refiner.watermark = None
pipe.scheduler = SCHEDULERS[scheduler].from_config(pipe.scheduler.config)
generator = torch.Generator("cuda").manual_seed(seed)
common_args = {
"prompt": [prompt] * num_outputs,
"negative_prompt": [negative_prompt] * num_outputs,
"guidance_scale": guidance_scale,
"generator": generator,
"num_inference_steps": num_inference_steps,
}
if self.is_lora:
sdxl_kwargs["cross_attention_kwargs"] = {"scale": lora_scale}
output = pipe(**common_args, **sdxl_kwargs)
if refine in ["expert_ensemble_refiner", "base_image_refiner"]:
refiner_kwargs = {
"image": output.images,
}
if refine == "expert_ensemble_refiner":
refiner_kwargs["denoising_start"] = high_noise_frac
if refine == "base_image_refiner" and refine_steps:
common_args["num_inference_steps"] = refine_steps
output = self.refiner(**common_args, **refiner_kwargs)
if not apply_watermark:
pipe.watermark = watermark_cache
self.refiner.watermark = watermark_cache
_, has_nsfw_content = self.run_safety_checker(output.images)
output_paths = []
for i, nsfw in enumerate(has_nsfw_content):
if nsfw:
print(f"NSFW content detected in image {i}")
continue
output_path = f"/tmp/out-{i}.png"
output.images[i].save(output_path)
output_paths.append(Path(output_path))
if len(output_paths) == 0:
raise Exception(
f"NSFW content detected. Try running it again, or try a different prompt."
)
return output_paths