Spaces:
Running
on
Zero
Running
on
Zero
Update optimized.py
Browse files- optimized.py +30 -12
optimized.py
CHANGED
@@ -4,28 +4,46 @@ import os
|
|
4 |
from diffusers.utils import load_image
|
5 |
from diffusers import FluxControlNetModel, FluxControlNetPipeline, AutoencoderKL
|
6 |
import gradio as gr
|
|
|
|
|
7 |
huggingface_token = os.getenv("HUGGINFACE_TOKEN")
|
8 |
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
# Load pipeline
|
12 |
controlnet = FluxControlNetModel.from_pretrained(
|
13 |
"jasperai/Flux.1-dev-Controlnet-Upscaler",
|
14 |
torch_dtype=torch.bfloat16
|
15 |
)
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
25 |
# Add to your pipeline initialization:
|
26 |
-
pipe.enable_xformers_memory_efficient_attention()
|
27 |
# pipe.enable_vae_slicing() # Batch processing of VAE
|
28 |
-
pipe.enable_model_cpu_offload() # Use with accelerate
|
29 |
|
30 |
# Convert all models to memory-efficient format
|
31 |
pipe.to(memory_format=torch.channels_last)
|
|
|
4 |
from diffusers.utils import load_image
|
5 |
from diffusers import FluxControlNetModel, FluxControlNetPipeline, AutoencoderKL
|
6 |
import gradio as gr
|
7 |
+
from accelerate import init_empty_weights
|
8 |
+
|
9 |
huggingface_token = os.getenv("HUGGINFACE_TOKEN")
|
10 |
|
11 |
+
try:
|
12 |
+
import xformers
|
13 |
+
pipe.enable_xformers_memory_efficient_attention()
|
14 |
+
except ImportError:
|
15 |
+
print("XFormers missing! Using PyTorch attention instead")
|
16 |
+
# Fallback to PyTorch 2.0+ memory efficient attention
|
17 |
+
pipe.enable_sdp_attention()
|
18 |
+
torch.backends.cuda.enable_flash_sdp(True)
|
19 |
+
|
20 |
+
good_vae = AutoencoderKL.from_pretrained("black-forest-labs/FLUX.1-dev", subfolder="vae",
|
21 |
+
torch_dtype=torch.bfloat16,
|
22 |
+
# variant="4bit",
|
23 |
+
device_map="balanced",
|
24 |
+
use_safetensors=True,
|
25 |
+
token=huggingface_token).to("cuda")
|
26 |
|
27 |
# Load pipeline
|
28 |
controlnet = FluxControlNetModel.from_pretrained(
|
29 |
"jasperai/Flux.1-dev-Controlnet-Upscaler",
|
30 |
torch_dtype=torch.bfloat16
|
31 |
)
|
32 |
+
with init_empty_weights():
|
33 |
+
pipe = FluxControlNetPipeline.from_pretrained(
|
34 |
+
"LPX55/FLUX.1-merged_uncensored",
|
35 |
+
controlnet=controlnet,
|
36 |
+
torch_dtype=torch.bfloat16,
|
37 |
+
device_map="balanced",
|
38 |
+
vae=good_vae,
|
39 |
+
use_safetensors=True,
|
40 |
+
token=huggingface_token
|
41 |
+
)
|
42 |
+
pipe.enable_model_cpu_offload(device="cuda")
|
43 |
# Add to your pipeline initialization:
|
44 |
+
# pipe.enable_xformers_memory_efficient_attention()
|
45 |
# pipe.enable_vae_slicing() # Batch processing of VAE
|
46 |
+
# pipe.enable_model_cpu_offload() # Use with accelerate
|
47 |
|
48 |
# Convert all models to memory-efficient format
|
49 |
pipe.to(memory_format=torch.channels_last)
|