LPX55 commited on
Commit
b1c8464
·
verified ·
1 Parent(s): cfab240

Update optimized.py

Browse files
Files changed (1) hide show
  1. optimized.py +20 -20
optimized.py CHANGED
@@ -8,14 +8,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,
@@ -29,24 +22,31 @@ 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)
50
  pipe.to("cuda")
51
 
52
  @spaces.GPU
 
8
 
9
  huggingface_token = os.getenv("HUGGINFACE_TOKEN")
10
 
11
+
 
 
 
 
 
 
 
12
 
13
  good_vae = AutoencoderKL.from_pretrained("black-forest-labs/FLUX.1-dev", subfolder="vae",
14
  torch_dtype=torch.bfloat16,
 
22
  "jasperai/Flux.1-dev-Controlnet-Upscaler",
23
  torch_dtype=torch.bfloat16
24
  )
25
+ #with init_empty_weights():
26
+ pipe = FluxControlNetPipeline.from_pretrained(
27
+ "LPX55/FLUX.1-merged_uncensored",
28
+ controlnet=controlnet,
29
+ torch_dtype=torch.bfloat16,
30
+ device_map="balanced",
31
+ vae=good_vae,
32
+ use_safetensors=True,
33
+ token=huggingface_token
34
+ )
35
  pipe.enable_model_cpu_offload(device="cuda")
36
  # Add to your pipeline initialization:
37
  # pipe.enable_xformers_memory_efficient_attention()
38
  # pipe.enable_vae_slicing() # Batch processing of VAE
39
  # pipe.enable_model_cpu_offload() # Use with accelerate
40
+ try:
41
+ import xformers
42
+ pipe.enable_xformers_memory_efficient_attention()
43
+ except ImportError:
44
+ print("XFormers missing! Using PyTorch attention instead")
45
+ # Fallback to PyTorch 2.0+ memory efficient attention
46
+ pipe.enable_sdp_attention()
47
+ torch.backends.cuda.enable_flash_sdp(True)
48
  # Convert all models to memory-efficient format
49
+ #pipe.to(memory_format=torch.channels_last)
50
  pipe.to("cuda")
51
 
52
  @spaces.GPU