-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathpredict.py
976 lines (873 loc) · 35.1 KB
/
predict.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
from abc import ABC, abstractmethod
import os
from typing import Tuple
import torch
from bfl_predictor import (
BflBf16Predictor,
BflControlNetFlux,
BflFillFlux,
BflFp8Flux,
BflReduxPredictor,
)
from diffusers_predictor import DiffusersFlux
from flux.modules.conditioner import PreLoadedHFEmbedder
from fp8.util import LoadedModels
torch.set_float32_matmul_precision("high")
torch.backends.cuda.matmul.allow_tf32 = True
torch.backends.cudnn.allow_tf32 = True
torch.backends.cudnn.benchmark = True
torch.backends.cudnn.benchmark_limit = 20
import logging
from dataclasses import dataclass
import numpy as np
from PIL import Image
from typing import List
from cog import BasePredictor, Input, Path # type: ignore
from flux.util import (
download_weights,
load_ae,
)
from diffusers.pipelines.stable_diffusion.safety_checker import (
StableDiffusionSafetyChecker,
)
from transformers import (
CLIPImageProcessor,
AutoModelForImageClassification,
ViTImageProcessor,
)
from weights import WeightsDownloadCache
SAFETY_CACHE = Path("./safety-cache")
FEATURE_EXTRACTOR = Path("./feature-extractor")
SAFETY_URL = "https://weights.replicate.delivery/default/sdxl/safety-1.0.tar"
MAX_IMAGE_SIZE = 1440
FALCON_MODEL_NAME = "Falconsai/nsfw_image_detection"
FALCON_MODEL_CACHE = Path("./falcon-cache")
FALCON_MODEL_URL = (
"https://weights.replicate.delivery/default/falconai/nsfw-image-detection.tar"
)
FLUX_DEV = "flux-dev"
FLUX_DEV_FP8 = "flux-dev-fp8"
FLUX_SCHNELL = "flux-schnell"
FLUX_SCHNELL_FP8 = "flux-schnell-fp8"
# Suppress diffusers nsfw warnings
logging.getLogger("diffusers").setLevel(logging.CRITICAL)
logging.getLogger("transformers").setLevel(logging.CRITICAL)
ASPECT_RATIOS = {
"1:1": (1024, 1024),
"16:9": (1344, 768),
"21:9": (1536, 640),
"3:2": (1216, 832),
"2:3": (832, 1216),
"4:5": (896, 1088),
"5:4": (1088, 896),
"3:4": (896, 1152),
"4:3": (1152, 896),
"9:16": (768, 1344),
"9:21": (640, 1536),
}
@dataclass(frozen=True)
class Inputs:
prompt = Input(description="Prompt for generated image")
aspect_ratio = Input(
description="Aspect ratio for the generated image",
choices=list(ASPECT_RATIOS.keys()),
default="1:1",
)
num_outputs = Input(
description="Number of outputs to generate", default=1, le=4, ge=1
)
seed = Input(
description="Random seed. Set for reproducible generation", default=None
)
output_format = Input(
description="Format of the output images",
choices=["webp", "jpg", "png"],
default="webp",
)
output_quality = Input(
description="Quality when saving the output images, from 0 to 100. 100 is best quality, 0 is lowest quality. Not relevant for .png outputs",
default=80,
ge=0,
le=100,
)
disable_safety_checker = Input(
description="Disable safety checker for generated images.",
default=False,
)
lora_weights: Input = Input(
description="Load LoRA weights. Supports Replicate models in the format <owner>/<username> or <owner>/<username>/<version>, HuggingFace URLs in the format huggingface.co/<owner>/<model-name>, CivitAI URLs in the format civitai.com/models/<id>[/<model-name>], or arbitrary .safetensors URLs from the Internet. For example, 'fofr/flux-pixar-cars'",
default=None,
)
lora_scale = Input(
description="Determines how strongly the main LoRA should be applied. Sane results between 0 and 1 for base inference. For go_fast we apply a 1.5x multiplier to this value; we've generally seen good performance when scaling the base value by that amount. You may still need to experiment to find the best value for your particular lora.",
default=1.0,
le=3.0,
ge=-1.0,
)
megapixels = Input(
description="Approximate number of megapixels for generated image",
choices=["1", "0.25"],
default="1",
)
megapixels_with_match_input = Input(
description="Approximate number of megapixels for generated image. Use match_input to match the size of the input (with an upper limit of 1440x1440 pixels)",
choices=["1", "0.25", "match_input"],
default="1",
)
@staticmethod
def go_fast_with_default(default: bool) -> Input:
return Input(
description="Run faster predictions with model optimized for speed (currently fp8 quantized); disable to run in original bf16",
default=default,
)
@staticmethod
def guidance_with(default: float, le: float) -> Input:
return Input(
description="Guidance for generated image", ge=0, le=le, default=default
)
@staticmethod
def num_inference_steps_with(
default: int, le: int, recommended: int | tuple[int, int]
) -> Input:
description = "Number of denoising steps. "
if isinstance(recommended, tuple):
description += f"Recommended range is {recommended[0]}-{recommended[1]}, and lower number of steps produce lower quality outputs, faster."
else:
description += f"{recommended} is recommended, and lower number of steps produce lower quality outputs, faster."
return Input(
description=description,
ge=1,
le=le,
default=default,
)
class Predictor(BasePredictor, ABC):
"""
Base object with shared flux pre & post-processing functionality (e.g. safety checking) that doesn't depend on implementation.
The goal here, broadly, is to decouple the Cog and therefore Replicate API for flux (defined by this object and its child classes)
from the actual inference implementation (defined by the various objects that the child classes instantiate in their setup() fucntions).
This enables decoupling bf16 implementations from fp8 implementations, and hosting multiple implementations in the same model.
All child objects need to implement the Cog `setup` and `predict` functions.
"""
def setup(self) -> None:
return
def base_setup(
self,
) -> None:
gpu_name = (
os.popen("nvidia-smi --query-gpu=name --format=csv,noheader,nounits")
.read()
.strip()
)
print("Detected GPU:", gpu_name)
if not SAFETY_CACHE.exists():
download_weights(SAFETY_URL, SAFETY_CACHE)
print("Loading Safety Checker to GPU")
self.safety_checker = StableDiffusionSafetyChecker.from_pretrained(
SAFETY_CACHE, torch_dtype=torch.float16
).to("cuda") # type: ignore
self.feature_extractor = CLIPImageProcessor.from_pretrained(FEATURE_EXTRACTOR)
print("Loading Falcon safety checker...")
if not FALCON_MODEL_CACHE.exists():
download_weights(FALCON_MODEL_URL, FALCON_MODEL_CACHE)
self.falcon_model = AutoModelForImageClassification.from_pretrained(
FALCON_MODEL_NAME,
cache_dir=FALCON_MODEL_CACHE,
)
self.falcon_processor = ViTImageProcessor.from_pretrained(FALCON_MODEL_NAME)
def should_offload(self):
# need > 48 GB of ram to store all models in VRAM
total_mem = torch.cuda.get_device_properties(0).total_memory
self.offload = total_mem < 48 * 1024**3
if self.offload:
print("GPU memory is:", total_mem / 1024**3, ", offloading models")
return self.offload
@abstractmethod
def predict(self):
pass
def size_from_aspect_megapixels(
self, aspect_ratio: str, megapixels: str = "1"
) -> Tuple[int, int]:
width, height = ASPECT_RATIOS[aspect_ratio]
if megapixels == "0.25":
width, height = width // 2, height // 2
return (width, height)
def postprocess(
self,
images: List[Image.Image],
disable_safety_checker: bool,
output_format: str,
output_quality: int,
np_images: List[np.ndarray],
) -> List[Path]:
has_nsfw_content = [False] * len(images)
if not disable_safety_checker:
_, has_nsfw_content = self.run_safety_checker(images, np_images)
output_paths = []
for i, (img, is_nsfw) in enumerate(zip(images, has_nsfw_content)):
if is_nsfw:
try:
falcon_is_safe = self.run_falcon_safety_checker(img)
except Exception as e:
print(f"Error running safety checker: {e}")
falcon_is_safe = False
if not falcon_is_safe:
print(f"NSFW content detected in image {i}")
continue
output_path = f"out-{i}.{output_format}"
save_params = (
{"quality": output_quality, "optimize": True}
if output_format != "png"
else {}
)
img.save(output_path, **save_params)
output_paths.append(Path(output_path))
if not output_paths:
raise Exception(
"All generated images contained NSFW content. Try running it again with a different prompt."
)
print(f"Total safe images: {len(output_paths)} out of {len(images)}")
return output_paths
def run_safety_checker(self, images, np_images):
safety_checker_input = self.feature_extractor(images, return_tensors="pt").to( # type: ignore
"cuda"
)
image, has_nsfw_concept = self.safety_checker(
images=np_images,
clip_input=safety_checker_input.pixel_values.to(torch.float16),
)
return image, has_nsfw_concept
def run_falcon_safety_checker(self, image):
with torch.no_grad():
inputs = self.falcon_processor(images=image, return_tensors="pt") # type: ignore
outputs = self.falcon_model(**inputs)
logits = outputs.logits
predicted_label = logits.argmax(-1).item()
result = self.falcon_model.config.id2label[predicted_label]
return result == "normal"
def size_maybe_match_input(
self, image_path: Path, megapixels: str
) -> tuple[int, int]:
image = Image.open(image_path)
width, height = image.size
# 32 since that's what fill is using
def round_to_nearest_multiple_of_32(width: int, height: int) -> tuple[int, int]:
return int(width / 32) * 32, int(height / 32) * 32
if megapixels == "match_input":
# scale down if needed to fit within MAX_IMAGE_SIZE
scale = min(MAX_IMAGE_SIZE / width, MAX_IMAGE_SIZE / height, 1)
if scale < 1:
width = int(width * scale)
height = int(height * scale)
return round_to_nearest_multiple_of_32(width, height)
target_pixels = int(float(megapixels) * 1024 * 1024)
current_pixels = width * height
scale = (target_pixels / current_pixels) ** 0.5
width = int(width * scale)
height = int(height * scale)
return round_to_nearest_multiple_of_32(width, height)
class SchnellPredictor(Predictor):
def setup(self) -> None:
self.base_setup()
self.bf16_model = BflBf16Predictor(FLUX_SCHNELL, offload=self.should_offload())
self.fp8_model = BflFp8Flux(
FLUX_SCHNELL_FP8,
loaded_models=self.bf16_model.get_shared_models(),
torch_compile=True,
compilation_aspect_ratios=ASPECT_RATIOS,
offload=self.should_offload(),
)
def predict(
self,
prompt: str = Inputs.prompt,
aspect_ratio: str = Inputs.aspect_ratio,
num_outputs: int = Inputs.num_outputs,
num_inference_steps: int = Inputs.num_inference_steps_with(
le=4, default=4, recommended=4
),
seed: int = Inputs.seed,
output_format: str = Inputs.output_format,
output_quality: int = Inputs.output_quality,
disable_safety_checker: bool = Inputs.disable_safety_checker,
go_fast: bool = Inputs.go_fast_with_default(True),
megapixels: str = Inputs.megapixels,
) -> List[Path]:
model = self.fp8_model if go_fast else self.bf16_model
width, height = self.size_from_aspect_megapixels(aspect_ratio, megapixels)
imgs, np_imgs = model.predict(
prompt,
num_outputs,
num_inference_steps=num_inference_steps,
seed=seed,
width=width,
height=height,
)
return self.postprocess(
imgs,
disable_safety_checker,
output_format,
output_quality,
np_images=np_imgs,
)
class DevPredictor(Predictor):
def setup(self) -> None:
self.base_setup()
self.bf16_model = BflBf16Predictor(FLUX_DEV, offload=self.should_offload())
self.fp8_model = BflFp8Flux(
FLUX_DEV_FP8,
loaded_models=self.bf16_model.get_shared_models(),
torch_compile=True,
compilation_aspect_ratios=ASPECT_RATIOS,
offload=self.should_offload(),
)
def predict(
self,
prompt: str = Inputs.prompt,
aspect_ratio: str = Inputs.aspect_ratio,
image: Path = Input(
description="Input image for image to image mode. The aspect ratio of your output will match this image",
default=None,
),
prompt_strength: float = Input(
description="Prompt strength when using img2img. 1.0 corresponds to full destruction of information in image",
ge=0.0,
le=1.0,
default=0.80,
),
num_outputs: int = Inputs.num_outputs,
num_inference_steps: int = Inputs.num_inference_steps_with(
le=50, default=28, recommended=(28, 50)
),
guidance: float = Inputs.guidance_with(default=3, le=10),
seed: int = Inputs.seed,
output_format: str = Inputs.output_format,
output_quality: int = Inputs.output_quality,
disable_safety_checker: bool = Inputs.disable_safety_checker,
go_fast: bool = Inputs.go_fast_with_default(True),
megapixels: str = Inputs.megapixels,
) -> List[Path]:
if image and go_fast:
print("img2img not supported with fp8 quantization; running with bf16")
go_fast = False
width, height = self.size_from_aspect_megapixels(aspect_ratio, megapixels)
model = self.fp8_model if go_fast else self.bf16_model
imgs, np_imgs = model.predict(
prompt,
num_outputs,
num_inference_steps,
guidance=guidance,
legacy_image_path=image,
prompt_strength=prompt_strength,
seed=seed,
width=width,
height=height,
)
return self.postprocess(
imgs,
disable_safety_checker,
output_format,
output_quality,
np_images=np_imgs,
)
class SchnellLoraPredictor(Predictor):
def setup(self) -> None:
self.base_setup()
cache = WeightsDownloadCache()
self.bf16_model = BflBf16Predictor(
FLUX_SCHNELL,
offload=self.should_offload(),
weights_download_cache=cache,
restore_lora_from_cloned_weights=True,
)
self.fp8_model = BflFp8Flux(
FLUX_SCHNELL_FP8,
loaded_models=self.bf16_model.get_shared_models(),
torch_compile=True,
compilation_aspect_ratios=ASPECT_RATIOS,
offload=self.should_offload(),
weights_download_cache=cache,
restore_lora_from_cloned_weights=True,
)
def predict(
self,
prompt: str = Inputs.prompt,
aspect_ratio: str = Inputs.aspect_ratio,
num_outputs: int = Inputs.num_outputs,
num_inference_steps: int = Inputs.num_inference_steps_with(
le=4, default=4, recommended=4
),
seed: int = Inputs.seed,
output_format: str = Inputs.output_format,
output_quality: int = Inputs.output_quality,
disable_safety_checker: bool = Inputs.disable_safety_checker,
go_fast: bool = Inputs.go_fast_with_default(True),
lora_weights: str = Inputs.lora_weights,
lora_scale: float = Inputs.lora_scale,
megapixels: str = Inputs.megapixels,
) -> List[Path]:
model = self.fp8_model if go_fast else self.bf16_model
model.handle_loras(lora_weights, lora_scale)
width, height = self.size_from_aspect_megapixels(aspect_ratio, megapixels)
imgs, np_imgs = model.predict(
prompt,
num_outputs,
num_inference_steps=num_inference_steps,
seed=seed,
width=width,
height=height,
)
return self.postprocess(
imgs,
disable_safety_checker,
output_format,
output_quality,
np_images=np_imgs,
)
class DevLoraPredictor(Predictor):
def setup(self) -> None:
self.base_setup()
cache = WeightsDownloadCache()
self.bf16_model = BflBf16Predictor(
FLUX_DEV,
offload=self.should_offload(),
weights_download_cache=cache,
restore_lora_from_cloned_weights=True,
)
self.fp8_model = BflFp8Flux(
FLUX_DEV_FP8,
loaded_models=self.bf16_model.get_shared_models(),
torch_compile=True,
compilation_aspect_ratios=ASPECT_RATIOS,
offload=self.should_offload(),
weights_download_cache=cache,
restore_lora_from_cloned_weights=True,
)
def predict(
self,
prompt: str = Inputs.prompt,
aspect_ratio: str = Inputs.aspect_ratio,
image: Path = Input(
description="Input image for image to image mode. The aspect ratio of your output will match this image",
default=None,
),
prompt_strength: float = Input(
description="Prompt strength when using img2img. 1.0 corresponds to full destruction of information in image",
ge=0.0,
le=1.0,
default=0.80,
),
num_outputs: int = Inputs.num_outputs,
num_inference_steps: int = Inputs.num_inference_steps_with(
le=50, default=28, recommended=(28, 50)
),
guidance: float = Inputs.guidance_with(default=3, le=10),
seed: int = Inputs.seed,
output_format: str = Inputs.output_format,
output_quality: int = Inputs.output_quality,
disable_safety_checker: bool = Inputs.disable_safety_checker,
go_fast: bool = Inputs.go_fast_with_default(True),
lora_weights: str = Inputs.lora_weights,
lora_scale: float = Inputs.lora_scale,
megapixels: str = Inputs.megapixels,
) -> List[Path]:
if image and go_fast:
print("img2img not supported with fp8 quantization; running with bf16")
go_fast = False
model = self.fp8_model if go_fast else self.bf16_model
model.handle_loras(lora_weights, lora_scale)
width, height = self.size_from_aspect_megapixels(aspect_ratio, megapixels)
imgs, np_imgs = model.predict(
prompt,
num_outputs,
num_inference_steps,
guidance=guidance,
legacy_image_path=image,
prompt_strength=prompt_strength,
seed=seed,
width=width,
height=height,
)
return self.postprocess(
imgs,
disable_safety_checker,
output_format,
output_quality,
np_images=np_imgs,
)
class SchnellReduxPredictor(Predictor):
def setup(self):
self.base_setup()
self.model = BflReduxPredictor(FLUX_SCHNELL, offload=self.should_offload())
def predict(
self,
redux_image: Path = Input(
description="Input image to condition your output on. This replaces prompt for FLUX.1 Redux models",
),
aspect_ratio: str = Inputs.aspect_ratio,
num_outputs: int = Inputs.num_outputs,
num_inference_steps: int = Input(
description="Number of denoising steps. 4 is recommended, and lower number of steps produce lower quality outputs, faster.",
ge=1,
le=4,
default=4,
),
seed: int = Inputs.seed,
output_format: str = Inputs.output_format,
output_quality: int = Inputs.output_quality,
disable_safety_checker: bool = Inputs.disable_safety_checker,
megapixels: str = Inputs.megapixels,
) -> List[Path]:
prompt = ""
width, height = self.size_from_aspect_megapixels(aspect_ratio, megapixels)
imgs, np_imgs = self.model.predict(
prompt,
num_outputs,
num_inference_steps=num_inference_steps,
seed=seed,
width=width,
height=height,
prepare_kwargs={"redux_img_path": redux_image},
)
return self.postprocess(
imgs,
disable_safety_checker,
output_format,
output_quality,
np_images=np_imgs,
)
class DevReduxPredictor(Predictor):
def setup(self):
self.base_setup()
self.model = BflReduxPredictor(FLUX_DEV, offload=self.should_offload())
def predict(
self,
redux_image: Path = Input(
description="Input image to condition your output on. This replaces prompt for FLUX.1 Redux models",
),
aspect_ratio: str = Inputs.aspect_ratio,
num_outputs: int = Inputs.num_outputs,
num_inference_steps: int = Input(
description="Number of denoising steps. Recommended range is 28-50",
ge=1,
le=50,
default=28,
),
guidance: float = Input(
description="Guidance for generated image", ge=0, le=10, default=3
),
seed: int = Inputs.seed,
output_format: str = Inputs.output_format,
output_quality: int = Inputs.output_quality,
disable_safety_checker: bool = Inputs.disable_safety_checker,
megapixels: str = Inputs.megapixels,
) -> List[Path]:
prompt = ""
width, height = self.size_from_aspect_megapixels(aspect_ratio, megapixels)
imgs, np_imgs = self.model.predict(
prompt,
num_outputs,
num_inference_steps=num_inference_steps,
guidance=guidance,
seed=seed,
width=width,
height=height,
prepare_kwargs={"redux_img_path": redux_image},
)
return self.postprocess(
imgs,
disable_safety_checker,
output_format,
output_quality,
np_images=np_imgs,
)
class FillDevPredictor(Predictor):
def setup(self) -> None:
self.base_setup()
cache = WeightsDownloadCache()
self.model = BflFillFlux(
"flux-fill-dev",
offload=self.should_offload(),
weights_download_cache=cache,
restore_lora_from_cloned_weights=True,
)
def predict(
self,
prompt: str = Inputs.prompt,
image: Path = Input(
description=f"The image to inpaint. Can contain alpha mask. If the image width or height are not multiples of 32, they will be scaled to the closest multiple of 32. If the image dimensions don't fit within {MAX_IMAGE_SIZE}x{MAX_IMAGE_SIZE}, it will be scaled down to fit."
),
mask: Path = Input(
description="A black-and-white image that describes the part of the image to inpaint. Black areas will be preserved while white areas will be inpainted.",
default=None,
),
num_outputs: int = Inputs.num_outputs,
num_inference_steps: int = Inputs.num_inference_steps_with(
le=50, default=28, recommended=(28, 50)
),
guidance: float = Inputs.guidance_with(default=30, le=100),
seed: int = Inputs.seed,
megapixels: str = Inputs.megapixels_with_match_input,
output_format: str = Inputs.output_format,
output_quality: int = Inputs.output_quality,
lora_weights: str = Inputs.lora_weights,
lora_scale: float = Inputs.lora_scale,
disable_safety_checker: bool = Inputs.disable_safety_checker,
) -> List[Path]:
# TODO(andreas): This means we're reading the image twice
# which is a bit inefficient.
width, height = self.size_maybe_match_input(image, megapixels)
self.model.handle_loras(lora_weights, lora_scale)
imgs, np_imgs = self.model.predict(
prompt=prompt,
num_outputs=num_outputs,
num_inference_steps=num_inference_steps,
guidance=guidance,
seed=seed,
width=width,
height=height,
conditioning_kwargs={"image_path": image, "mask_path": mask},
)
return self.postprocess(
imgs,
disable_safety_checker,
output_format,
output_quality,
np_images=np_imgs,
)
class HotswapPredictor(Predictor):
def setup(self) -> None:
self.base_setup()
shared_cache = WeightsDownloadCache()
self.bf16_dev = DiffusersFlux(FLUX_DEV, shared_cache)
shared_models = self.bf16_dev.get_models()
# hack to get around delta in vae code
bfl_ae = load_ae(FLUX_DEV)
shared_models_for_fp8 = LoadedModels(
ae=bfl_ae,
clip=PreLoadedHFEmbedder(
True, 77, shared_models.tokenizer, shared_models.text_encoder
),
t5=PreLoadedHFEmbedder(
False, 512, shared_models.tokenizer_2, shared_models.text_encoder_2
),
flow=None,
config=None,
)
self.fp8_dev = BflFp8Flux(
FLUX_DEV_FP8,
shared_models_for_fp8,
torch_compile=True,
compilation_aspect_ratios=ASPECT_RATIOS,
weights_download_cache=shared_cache,
restore_lora_from_cloned_weights=True,
)
self.bf16_schnell = DiffusersFlux(FLUX_SCHNELL, shared_cache, shared_models)
shared_models_for_fp8.t5 = PreLoadedHFEmbedder(
False, 256, shared_models.tokenizer_2, shared_models.text_encoder_2
)
self.fp8_schnell = BflFp8Flux(
FLUX_SCHNELL_FP8,
shared_models_for_fp8,
torch_compile=True,
compilation_aspect_ratios=ASPECT_RATIOS,
weights_download_cache=shared_cache,
restore_lora_from_cloned_weights=True,
)
def predict(
self,
prompt: str = Input(
description="Prompt for generated image. If you include the `trigger_word` used in the training process you are more likely to activate the trained object, style, or concept in the resulting image."
),
image: Path = Input(
description="Input image for image to image or inpainting mode. If provided, aspect_ratio, width, and height inputs are ignored.",
default=None,
),
mask: Path = Input(
description="Image mask for image inpainting mode. If provided, aspect_ratio, width, and height inputs are ignored.",
default=None,
),
aspect_ratio: str = Input(
description="Aspect ratio for the generated image. If custom is selected, uses height and width below & will run in bf16 mode",
choices=list(ASPECT_RATIOS.keys()) + ["custom"],
default="1:1",
),
height: int = Input(
description="Height of generated image. Only works if `aspect_ratio` is set to custom. Will be rounded to nearest multiple of 16. Incompatible with fast generation",
ge=256,
le=1440,
default=None,
),
width: int = Input(
description="Width of generated image. Only works if `aspect_ratio` is set to custom. Will be rounded to nearest multiple of 16. Incompatible with fast generation",
ge=256,
le=1440,
default=None,
),
prompt_strength: float = Input(
description="Prompt strength when using img2img. 1.0 corresponds to full destruction of information in image",
ge=0.0,
le=1.0,
default=0.80,
),
model: str = Input(
description="Which model to run inference with. The dev model performs best with around 28 inference steps but the schnell model only needs 4 steps.",
choices=["dev", "schnell"],
default="dev",
),
num_outputs: int = Inputs.num_outputs,
num_inference_steps: int = Input(
description="Number of denoising steps. More steps can give more detailed images, but take longer.",
ge=1,
le=50,
default=28,
),
guidance_scale: float = Input(
description="Guidance scale for the diffusion process. Lower values can give more realistic images. Good values to try are 2, 2.5, 3 and 3.5",
ge=0,
le=10,
default=3,
),
seed: int = Inputs.seed,
output_format: str = Inputs.output_format,
output_quality: int = Inputs.output_quality,
disable_safety_checker: bool = Inputs.disable_safety_checker,
go_fast: bool = Inputs.go_fast_with_default(False),
megapixels: str = Inputs.megapixels,
replicate_weights: str = Inputs.lora_weights,
lora_scale: float = Inputs.lora_scale,
extra_lora: str = Input(
description="Load LoRA weights. Supports Replicate models in the format <owner>/<username> or <owner>/<username>/<version>, HuggingFace URLs in the format huggingface.co/<owner>/<model-name>, CivitAI URLs in the format civitai.com/models/<id>[/<model-name>], or arbitrary .safetensors URLs from the Internet. For example, 'fofr/flux-pixar-cars'",
default=None,
),
extra_lora_scale: float = Input(
description="Determines how strongly the extra LoRA should be applied. Sane results between 0 and 1 for base inference. For go_fast we apply a 1.5x multiplier to this value; we've generally seen good performance when scaling the base value by that amount. You may still need to experiment to find the best value for your particular lora.",
default=1.0,
le=3.0,
ge=-1,
),
) -> List[Path]:
if aspect_ratio == "custom":
if go_fast:
print(
"Custom aspect ratios not supported with fast fp8 inference; will run in bf16"
)
go_fast = False
width = make_multiple_of_16(width)
height = make_multiple_of_16(height)
else:
width, height = self.size_from_aspect_megapixels(
aspect_ratio, megapixels=megapixels
)
if image and go_fast:
print(
"Img2img and inpainting not supported with fast fp8 inference; will run in bf16"
)
go_fast = False
if model == "dev":
model = self.fp8_dev if go_fast else self.bf16_dev
else:
model = self.fp8_schnell if go_fast else self.bf16_schnell
model.handle_loras(replicate_weights, lora_scale, extra_lora, extra_lora_scale)
imgs, np_imgs = model.predict(
prompt,
num_outputs,
num_inference_steps,
guidance=guidance_scale,
legacy_image_path=image,
legacy_mask_path=mask,
prompt_strength=prompt_strength,
seed=seed,
width=width,
height=height,
)
return self.postprocess(
imgs,
disable_safety_checker,
output_format,
output_quality,
np_images=np_imgs,
)
class CannyDevPredictor(Predictor):
def setup(self) -> None:
self.base_setup()
self.model = BflControlNetFlux("flux-canny-dev", offload=self.should_offload())
def predict(
self,
prompt: str = Inputs.prompt,
control_image: Path = Input(
description="Image used to control the generation. The canny edge detection will be automatically generated."
),
num_outputs: int = Inputs.num_outputs,
num_inference_steps: int = Inputs.num_inference_steps_with(
le=50, default=28, recommended=(28, 50)
),
guidance: float = Inputs.guidance_with(default=30, le=100),
seed: int = Inputs.seed,
output_format: str = Inputs.output_format,
output_quality: int = Inputs.output_quality,
disable_safety_checker: bool = Inputs.disable_safety_checker,
megapixels: str = Inputs.megapixels_with_match_input,
) -> List[Path]:
# TODO(andreas): This means we're reading the image twice
# which is a bit inefficient.
width, height = self.size_maybe_match_input(control_image, megapixels)
imgs, np_imgs = self.model.predict(
prompt=prompt,
num_outputs=num_outputs,
num_inference_steps=num_inference_steps,
guidance=guidance,
seed=seed,
width=width,
height=height,
conditioning_kwargs={"image_path": control_image},
)
return self.postprocess(
imgs,
disable_safety_checker,
output_format,
output_quality,
np_images=np_imgs,
)
class DepthDevPredictor(Predictor):
def setup(self) -> None:
self.base_setup()
self.model = BflControlNetFlux("flux-depth-dev", offload=self.should_offload())
def predict(
self,
prompt: str = Inputs.prompt,
control_image: Path = Input(
description="Image used to control the generation. The depth map will be automatically generated."
),
num_outputs: int = Inputs.num_outputs,
num_inference_steps: int = Inputs.num_inference_steps_with(
le=50, default=28, recommended=(28, 50)
),
guidance: float = Inputs.guidance_with(default=10, le=100),
seed: int = Inputs.seed,
output_format: str = Inputs.output_format,
output_quality: int = Inputs.output_quality,
disable_safety_checker: bool = Inputs.disable_safety_checker,
megapixels: str = Inputs.megapixels_with_match_input,
) -> List[Path]:
# TODO(andreas): This means we're reading the image twice
# which is a bit inefficient.
width, height = self.size_maybe_match_input(control_image, megapixels)
imgs, np_imgs = self.model.predict(
prompt=prompt,
num_outputs=num_outputs,
num_inference_steps=num_inference_steps,
guidance=guidance,
seed=seed,
width=width,
height=height,
conditioning_kwargs={"image_path": control_image},
)
return self.postprocess(
imgs,
disable_safety_checker,
output_format,
output_quality,
np_images=np_imgs,
)
def make_multiple_of_16(n):
# Rounds up to the next multiple of 16, or returns n if already a multiple of 16
return ((n + 15) // 16) * 16