Spaces:
Running
on
Zero
Running
on
Zero
Support zeroGPU env
Browse files
app.py
CHANGED
|
@@ -30,6 +30,10 @@ from torchvision.transforms.functional import to_pil_image
|
|
| 30 |
# import pillow_heif # HEIC μ΄λ―Έμ§ μ²λ¦¬μ© (μμ΄ν° 촬μ μ¬μ§ ν¬λ§·)
|
| 31 |
from urllib.parse import urlparse
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
# SSL κ²½κ³ μ΅μ
|
| 34 |
warnings.filterwarnings("ignore", message=".*OpenSSL.*")
|
| 35 |
warnings.filterwarnings("ignore", category=UserWarning, module="urllib3")
|
|
@@ -68,14 +72,17 @@ unet = UNet2DConditionModel.from_pretrained(
|
|
| 68 |
unet.requires_grad_(False)
|
| 69 |
# torch.compile() μ μ© - μΆλ‘ μλ 20-40% ν₯μ (PyTorch 2.0+)
|
| 70 |
# μ£Όμ: 첫 λ²μ§Έ μΆλ‘ μ μ»΄νμΌλ‘ μΈν΄ λ릴 μ μμ
|
| 71 |
-
if
|
| 72 |
-
try:
|
| 73 |
-
unet = torch.compile(unet, mode="reduce-overhead")
|
| 74 |
-
print("β UNet model loaded and compiled successfully")
|
| 75 |
-
except Exception as e:
|
| 76 |
-
print(f"β UNet model loaded (compile skipped: {e})")
|
| 77 |
-
else:
|
| 78 |
print("β UNet model loaded successfully")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
print("\n[2/10] Loading tokenizers...")
|
| 81 |
tokenizer_one = AutoTokenizer.from_pretrained(
|
|
@@ -123,14 +130,17 @@ vae = AutoencoderKL.from_pretrained(base_path,
|
|
| 123 |
torch_dtype=torch.float16,
|
| 124 |
)
|
| 125 |
# torch.compile() μ μ© - VAE μΈμ½λ©/λμ½λ© μλ ν₯μ
|
| 126 |
-
if
|
| 127 |
-
try:
|
| 128 |
-
vae = torch.compile(vae, mode="reduce-overhead")
|
| 129 |
-
print("β VAE loaded and compiled successfully")
|
| 130 |
-
except Exception as e:
|
| 131 |
-
print(f"β VAE loaded (compile skipped: {e})")
|
| 132 |
-
else:
|
| 133 |
print("β VAE loaded successfully")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
print("\n[7/10] Loading UNet Encoder...")
|
| 136 |
UNet_Encoder = UNet2DConditionModel_ref.from_pretrained(
|
|
@@ -139,14 +149,17 @@ UNet_Encoder = UNet2DConditionModel_ref.from_pretrained(
|
|
| 139 |
torch_dtype=torch.float16,
|
| 140 |
)
|
| 141 |
# torch.compile() μ μ© - UNet Encoder μλ ν₯μ
|
| 142 |
-
if
|
| 143 |
-
try:
|
| 144 |
-
UNet_Encoder = torch.compile(UNet_Encoder, mode="reduce-overhead")
|
| 145 |
-
print("β UNet Encoder loaded and compiled successfully")
|
| 146 |
-
except Exception as e:
|
| 147 |
-
print(f"β UNet Encoder loaded (compile skipped: {e})")
|
| 148 |
-
else:
|
| 149 |
print("β UNet Encoder loaded successfully")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
print("\n[8/10] Initializing parsing and openpose models...")
|
| 152 |
parsing_model = Parsing(0)
|
|
@@ -243,12 +256,15 @@ print("=" * 60 + "\n")
|
|
| 243 |
|
| 244 |
# torch.compile μ€λ₯ μ eager λͺ¨λλ‘ ν΄λ°± μ€μ
|
| 245 |
# 컀μ€ν
UNet forward λ©μλ νΈνμ± λ¬Έμ λμ
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
|
|
|
|
|
|
|
|
|
| 252 |
|
| 253 |
# GPU Warm-up ν¨μ (μ± λ‘λ μ μλ μ€ν)
|
| 254 |
# Text Encoder, VAE GPU λ‘λ© λ° CUDA 컀λ μ΄κΈ°ν
|
|
@@ -752,11 +768,14 @@ with image_blocks as demo:
|
|
| 752 |
warmup_status = gr.Textbox(visible=False)
|
| 753 |
|
| 754 |
# μ± λ‘λ μ GPU Warm-up μλ μ€ν (torch.compile 첫 μ»΄νμΌ)
|
| 755 |
-
|
| 756 |
-
|
| 757 |
-
|
| 758 |
-
|
| 759 |
-
|
|
|
|
|
|
|
|
|
|
| 760 |
|
| 761 |
print("β Gradio interface components created")
|
| 762 |
print("β Event handlers configured")
|
|
|
|
| 30 |
# import pillow_heif # HEIC μ΄λ―Έμ§ μ²λ¦¬μ© (μμ΄ν° 촬μ μ¬μ§ ν¬λ§·)
|
| 31 |
from urllib.parse import urlparse
|
| 32 |
|
| 33 |
+
|
| 34 |
+
# zeroGPU νκ²½μμ compile μ¬μ© μ¬λΆ
|
| 35 |
+
is_compile_for_zeroGPU = True # True: compile μ¬μ©, False: compile μ¬μ© μ ν¨
|
| 36 |
+
|
| 37 |
# SSL κ²½κ³ μ΅μ
|
| 38 |
warnings.filterwarnings("ignore", message=".*OpenSSL.*")
|
| 39 |
warnings.filterwarnings("ignore", category=UserWarning, module="urllib3")
|
|
|
|
| 72 |
unet.requires_grad_(False)
|
| 73 |
# torch.compile() μ μ© - μΆλ‘ μλ 20-40% ν₯μ (PyTorch 2.0+)
|
| 74 |
# μ£Όμ: 첫 λ²μ§Έ μΆλ‘ μ μ»΄νμΌλ‘ μΈν΄ λ릴 μ μμ
|
| 75 |
+
if is_compile_for_zeroGPU == True:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
print("β UNet model loaded successfully")
|
| 77 |
+
else:
|
| 78 |
+
if hasattr(torch, 'compile'):
|
| 79 |
+
try:
|
| 80 |
+
unet = torch.compile(unet, mode="reduce-overhead")
|
| 81 |
+
print("β UNet model loaded and compiled successfully")
|
| 82 |
+
except Exception as e:
|
| 83 |
+
print(f"β UNet model loaded (compile skipped: {e})")
|
| 84 |
+
else:
|
| 85 |
+
print("β UNet model loaded successfully")
|
| 86 |
|
| 87 |
print("\n[2/10] Loading tokenizers...")
|
| 88 |
tokenizer_one = AutoTokenizer.from_pretrained(
|
|
|
|
| 130 |
torch_dtype=torch.float16,
|
| 131 |
)
|
| 132 |
# torch.compile() μ μ© - VAE μΈμ½λ©/λμ½λ© μλ ν₯μ
|
| 133 |
+
if is_compile_for_zeroGPU == True:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
print("β VAE loaded successfully")
|
| 135 |
+
else:
|
| 136 |
+
if hasattr(torch, 'compile'):
|
| 137 |
+
try:
|
| 138 |
+
vae = torch.compile(vae, mode="reduce-overhead")
|
| 139 |
+
print("β VAE loaded and compiled successfully")
|
| 140 |
+
except Exception as e:
|
| 141 |
+
print(f"β VAE loaded (compile skipped: {e})")
|
| 142 |
+
else:
|
| 143 |
+
print("β VAE loaded successfully")
|
| 144 |
|
| 145 |
print("\n[7/10] Loading UNet Encoder...")
|
| 146 |
UNet_Encoder = UNet2DConditionModel_ref.from_pretrained(
|
|
|
|
| 149 |
torch_dtype=torch.float16,
|
| 150 |
)
|
| 151 |
# torch.compile() μ μ© - UNet Encoder μλ ν₯μ
|
| 152 |
+
if is_compile_for_zeroGPU == True:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
print("β UNet Encoder loaded successfully")
|
| 154 |
+
else:
|
| 155 |
+
if hasattr(torch, 'compile'):
|
| 156 |
+
try:
|
| 157 |
+
UNet_Encoder = torch.compile(UNet_Encoder, mode="reduce-overhead")
|
| 158 |
+
print("β UNet Encoder loaded and compiled successfully")
|
| 159 |
+
except Exception as e:
|
| 160 |
+
print(f"β UNet Encoder loaded (compile skipped: {e})")
|
| 161 |
+
else:
|
| 162 |
+
print("β UNet Encoder loaded successfully")
|
| 163 |
|
| 164 |
print("\n[8/10] Initializing parsing and openpose models...")
|
| 165 |
parsing_model = Parsing(0)
|
|
|
|
| 256 |
|
| 257 |
# torch.compile μ€λ₯ μ eager λͺ¨λλ‘ ν΄λ°± μ€μ
|
| 258 |
# 컀μ€ν
UNet forward λ©μλ νΈνμ± λ¬Έμ λμ
|
| 259 |
+
if is_compile_for_zeroGPU == True:
|
| 260 |
+
print("β torch.compile is disabled for ZeroGPU")
|
| 261 |
+
else:
|
| 262 |
+
try:
|
| 263 |
+
import torch._dynamo
|
| 264 |
+
torch._dynamo.config.suppress_errors = True
|
| 265 |
+
print("β torch._dynamo.config.suppress_errors enabled (fallback to eager mode on error)")
|
| 266 |
+
except Exception as e:
|
| 267 |
+
print(f"β torch._dynamo config not available: {e}")
|
| 268 |
|
| 269 |
# GPU Warm-up ν¨μ (μ± λ‘λ μ μλ μ€ν)
|
| 270 |
# Text Encoder, VAE GPU λ‘λ© λ° CUDA 컀λ μ΄κΈ°ν
|
|
|
|
| 768 |
warmup_status = gr.Textbox(visible=False)
|
| 769 |
|
| 770 |
# μ± λ‘λ μ GPU Warm-up μλ μ€ν (torch.compile 첫 μ»΄νμΌ)
|
| 771 |
+
if is_compile_for_zeroGPU == True:
|
| 772 |
+
print("β GPU warm-up is disabled for ZeroGPU")
|
| 773 |
+
else:
|
| 774 |
+
demo.load(
|
| 775 |
+
fn=warmup_gpu,
|
| 776 |
+
inputs=None,
|
| 777 |
+
outputs=warmup_status,
|
| 778 |
+
)
|
| 779 |
|
| 780 |
print("β Gradio interface components created")
|
| 781 |
print("β Event handlers configured")
|