Spaces:
Paused
Paused
File size: 10,302 Bytes
246b21d 9071d89 8565695 246b21d 2bc0193 246b21d 0fb224d 8565695 0fb224d 246b21d 1a00eda 246b21d 8565695 246b21d |
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 |
# app.py
import os
import sys
import subprocess
import importlib
import site
import warnings
import logging
import time
from pathlib import Path
import gradio as gr
import torch
from huggingface_hub import hf_hub_download
from PIL import Image
import spaces
import time
import time, random
# ---------------------------
# Environment flags (reduce fusion/compilation) β set early
# ---------------------------
# These help avoid some torchinductor/flash-attn fusion issues that provoke guard errors.
os.environ.setdefault("TORCHINDUCTOR_DISABLE", "1")
os.environ.setdefault("TORCHINDUCTOR_FUSION", "0")
os.environ.setdefault("USE_FLASH_ATTENTION", "0")
# Some environments check this; safe to set
os.environ.setdefault("XLA_IGNORE_ENV_VARS", "1")
# ---------------------------
# FlashAttention install (best-effort)
# ---------------------------
def try_install_flash_attention():
try:
print("Attempting to download and install FlashAttention wheel...")
wheel = hf_hub_download(
repo_id="rahul7star/flash-attn-3",
repo_type="model",
filename="128/flash_attn_3-3.0.0b1-cp39-abi3-linux_x86_64.whl",
)
subprocess.run([sys.executable, "-m", "pip", "install", wheel], check=True)
# refresh site-packages
site.addsitedir(site.getsitepackages()[0])
importlib.invalidate_caches()
print("β
FlashAttention installed.")
return True
except Exception as e:
print(f"β οΈ FlashAttention install failed: {e}")
return False
# ---------------------------
# Torch logging / warnings
# ---------------------------
warnings.filterwarnings("ignore")
logging.getLogger("torch").setLevel(logging.ERROR)
# reduce torch verbose logging
try:
torch._logging.set_logs(
dynamo=logging.ERROR,
dynamic=logging.ERROR,
aot=logging.ERROR,
inductor=logging.ERROR,
guards=False,
recompiles=False
)
except Exception:
pass
# Make Dynamo tolerant initially (we'll disable if it fails)
try:
import torch._dynamo as _dynamo
_dynamo.config.suppress_errors = True
_dynamo.config.cache_size_limit = 0 # avoid large guard caches
except Exception:
_dynamo = None
# ---------------------------
# Download models if needed
# ---------------------------
def ensure_models_downloaded(marker_file=".models_ready"):
marker = Path(marker_file)
if marker.exists():
print("Models already downloaded (marker found).")
return True
if not Path("download_models.py").exists():
print("download_models.py not found in repo.")
return False
try:
print("Running download_models.py ...")
subprocess.run([sys.executable, "download_models.py"], check=True)
marker.write_text("ok")
print("Models download finished.")
return True
except Exception as e:
print("Model download failed:", e)
return False
# ---------------------------
# Load Kandinsky pipeline with smart Dynamo handling
# ---------------------------
def load_pipeline(conf_path="./configs/config_5s_sft.yaml", move_to_cuda_if_available=True):
"""
Attempt to load the pipeline normally. If Dynamo/guard errors are raised,
disable torch._dynamo and reload in eager mode.
Returns pipeline or raises.
"""
from kandinsky import get_T2V_pipeline # import inside function to respect env changes
def _do_load():
print("Loading pipeline with device_map pointing to cuda if available...")
device_map = None
if torch.cuda.is_available():
# let the pipeline place modules onto CUDA by device_map
device_map = {"dit": "cuda:0", "vae": "cuda:0", "text_embedder": "cuda:0"}
else:
device_map = "cpu"
pipe = get_T2V_pipeline(device_map=device_map, conf_path=conf_path, offload=False, magcache=False)
# If pipeline has .to and CUDA is available, move it
if move_to_cuda_if_available and torch.cuda.is_available() and hasattr(pipe, "to"):
try:
pipe.to("cuda")
except Exception as e:
# fallback: ignore and continue (some pipelines handle own device_map)
print("Warning while moving pipeline to CUDA:", e)
return pipe
try:
# Try normal load first (Dynamo may be enabled but we've suppressed errors)
pipe = _do_load()
print("Pipeline loaded successfully (initial try).")
return pipe
except Exception as e:
# Detect Dynamo/guard-related signatures and fallback
msg = str(e).lower()
if "dynamo" in msg or "guard" in msg or "attributeerror" in msg or "caught" in msg:
print("β οΈ Dynamo/guard-related error detected while loading pipeline:", e)
# Disable torch dynamo and try again
try:
if _dynamo is not None:
print("Disabling torch._dynamo and retrying load in eager mode...")
_dynamo.disable()
else:
print("torch._dynamo not available; proceeding to retry load.")
except Exception as ex_disable:
print("Error disabling torch._dynamo:", ex_disable)
# Retry load
try:
pipe = _do_load()
print("Pipeline loaded successfully after disabling torch._dynamo.")
return pipe
except Exception as e2:
print("Failed to load pipeline even after disabling torch._dynamo:", e2)
raise
else:
# Not obviously a Dynamo issue β re-raise
raise
# ---------------------------
# Startup sequence
# ---------------------------
print("=== startup: installing optional FlashAttention (best-effort) ===")
try_install_flash_attention()
print("=== startup: ensuring models ===")
if not ensure_models_downloaded():
print("Models not available; app may fail at inference. Proceeding anyway.")
print("=== startup: loading pipeline (smart) ===")
pipe = None
try:
pipe = load_pipeline(conf_path="./configs/config_5s_sft.yaml", move_to_cuda_if_available=True)
except Exception as e:
print("Pipeline load ultimately failed:", e)
pipe = None
# ---------------------------
# Helper: ensure pipeline is on CUDA at generation time
# ---------------------------
def ensure_pipe_on_cuda(pipeline):
if pipeline is None:
raise RuntimeError("Pipeline is None")
# If CUDA not available, raise early
if not torch.cuda.is_available():
raise RuntimeError("CUDA not available on this machine")
# If pipeline supports .to, move it
if hasattr(pipeline, "to"):
try:
pipeline.to("cuda")
except Exception as e:
# Some pipelines use device_map placement β ignore move failure
print("Warning: pipeline.to('cuda') raised:", e)
# ---------------------------
# Generation function (runs on GPU when used)
# ---------------------------
@spaces.GPU(duration=60)
def generate_output(prompt, mode, duration, width, height, steps, guidance, scheduler):
"""
This generation function assumes the pipeline is already loaded (pipe variable).
It will raise a helpful error if the pipeline wasn't loaded at startup.
"""
print(prompt)
if pipe is None:
return None, "β Pipeline not initialized at startup. Check logs."
# Ensure CUDA available and pipeline on CUDA
if not torch.cuda.is_available():
return None, "β CUDA not available on this host."
try:
# If dynamo is still enabled and we suspect it can cause trouble during forward,
# run inference inside a context where dynamo is disabled to be safe.
try:
if _dynamo is not None:
_dynamo.disable()
except Exception:
pass
out_name = f"/tmp/{int(time.time())}_{random.randint(100,999)}.{'mp4' if mode == 'video' else 'png'}"
if mode == "image":
pipe(prompt, time_length=0, width=width, height=height, save_path=out_name)
return out_name, f"β
Image saved to {out_name}"
# video path
pipe(prompt,
time_length=duration,
width=width,
height=height,
num_steps=steps if steps else None,
guidance_weight=guidance if guidance else None,
scheduler_scale=scheduler if scheduler else None,
save_path=out_name)
return out_name, f"β
Video saved to {out_name}"
except torch.cuda.OutOfMemoryError:
return None, "β οΈ CUDA OOM β try reducing resolution/duration/steps."
except Exception as e:
return None, f"β Generation error: {e}"
# ---------------------------
# Gradio UI
# ---------------------------
with gr.Blocks(theme=gr.themes.Soft(), title="Kandinsky 5.0 T2V (robust load)") as demo:
gr.Markdown("## Kandinsky 5.0 β Robust pipeline loader (smart Dynamo fallback)")
with gr.Row():
with gr.Column(scale=2):
mode = gr.Radio(["video", "image"], value="video", label="Mode")
prompt = gr.Textbox(label="Prompt", value="A dog in red boots")
duration = gr.Slider(1, 10, step=1, value=2, label="Duration (s)")
width = gr.Radio([512, 768], value=768, label="Width")
height = gr.Radio([512, 768], value=512, label="Height")
steps = gr.Slider(4, 50, step=1, value=10, label="Sampling Steps")
guidance = gr.Slider(0.0, 20.0, step=0.5, value=8.0, label="Guidance Weight")
scheduler = gr.Slider(1.0, 10.0, step=0.5, value=5.0, label="Scheduler Scale")
btn = gr.Button("Generate", variant="primary")
with gr.Column(scale=3):
out_video = gr.Video(label="Output")
status = gr.Textbox(label="Status", lines=6)
btn.click(fn=generate_output,
inputs=[prompt, mode, duration, width, height, steps, guidance, scheduler],
outputs=[out_video, status])
# ---------------------------
# Launch
# ---------------------------
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))
|