Spaces:
Running on Zero
Running on Zero
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -238,18 +238,44 @@ def _watchdog():
|
|
| 238 |
|
| 239 |
threading.Thread(target=_watchdog, daemon=True).start()
|
| 240 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
print("Loading Open-Sora v2 weights...", flush=True)
|
| 242 |
_t0 = time.perf_counter()
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
|
|
|
| 251 |
print(f"Weights read in {time.perf_counter() - _t0:.0f}s", flush=True)
|
| 252 |
|
|
|
|
| 253 |
_t0 = time.perf_counter()
|
| 254 |
for module in (model, model_ae, model_t5, model_clip, *optional_models.values()):
|
| 255 |
module.to("cuda")
|
|
|
|
| 238 |
|
| 239 |
threading.Thread(target=_watchdog, daemon=True).start()
|
| 240 |
|
| 241 |
+
@contextmanager
|
| 242 |
+
def _zerogpu_patching_off():
|
| 243 |
+
"""Load weights with the ZeroGPU torch hijack temporarily disabled.
|
| 244 |
+
|
| 245 |
+
`import spaces` installs a TorchFunctionMode + TorchDispatchMode over all of torch.
|
| 246 |
+
`safetensors.load_file` slices the file's storage once per tensor, and every one of
|
| 247 |
+
those slices was being routed through __torch_function__ -> dynamo ->
|
| 248 |
+
__torch_dispatch__. Across an 11B model that interception, not the I/O, is what
|
| 249 |
+
pushed a ~20s read past the 60-minute launch timeout. (The same file reads at
|
| 250 |
+
1.5 GB/s from this mount in a process that has not imported spaces.)
|
| 251 |
+
|
| 252 |
+
Reading happens entirely on CPU, so nothing here needs the hijack. It goes straight
|
| 253 |
+
back on afterwards, before any .to("cuda") — that call is what registers the fake
|
| 254 |
+
CUDA tensors and drives the pack step, and it must run patched.
|
| 255 |
+
"""
|
| 256 |
+
from spaces.zero.torch import patch as zerogpu_patch, unpatch as zerogpu_unpatch
|
| 257 |
+
|
| 258 |
+
zerogpu_unpatch()
|
| 259 |
+
try:
|
| 260 |
+
yield
|
| 261 |
+
finally:
|
| 262 |
+
zerogpu_patch()
|
| 263 |
+
|
| 264 |
+
|
| 265 |
print("Loading Open-Sora v2 weights...", flush=True)
|
| 266 |
_t0 = time.perf_counter()
|
| 267 |
+
with _zerogpu_patching_off():
|
| 268 |
+
model = _build("open-sora (11B)", cfg.model)
|
| 269 |
+
model_ae = _build("hunyuan vae", cfg.ae)
|
| 270 |
+
model_t5 = _build("t5-xxl", cfg.t5)
|
| 271 |
+
model_clip = _build("clip-l", cfg.clip)
|
| 272 |
+
optional_models = {
|
| 273 |
+
"img_flux": _build("flux (12B)", cfg.img_flux),
|
| 274 |
+
"img_flux_ae": _build("flux vae", cfg.img_flux_ae),
|
| 275 |
+
}
|
| 276 |
print(f"Weights read in {time.perf_counter() - _t0:.0f}s", flush=True)
|
| 277 |
|
| 278 |
+
# Patching is back on, so these are intercepted and packed as ZeroGPU expects.
|
| 279 |
_t0 = time.perf_counter()
|
| 280 |
for module in (model, model_ae, model_t5, model_clip, *optional_models.values()):
|
| 281 |
module.to("cuda")
|