Spaces:
Running on Zero
Running on Zero
Alexander Claude Opus 5.5 commited on
Commit ·
b21f6a7
1
Parent(s): 8deb59f
LoRA loads in the GPU fork: skip the detach-per-parameter state_dict diffusers never reads, preload lazy imports, arm the stack dump first
Browse files- lora_stack.py +36 -9
lora_stack.py
CHANGED
|
@@ -11,10 +11,14 @@ A slot resolves in the main process (`LoraSpec.fetch`, which downloads) and is a
|
|
| 11 |
|
| 12 |
from __future__ import annotations
|
| 13 |
|
|
|
|
|
|
|
| 14 |
import hashlib
|
| 15 |
import os
|
| 16 |
import re
|
|
|
|
| 17 |
import tempfile
|
|
|
|
| 18 |
import urllib.request
|
| 19 |
from urllib.parse import unquote
|
| 20 |
from dataclasses import dataclass, replace
|
|
@@ -184,6 +188,28 @@ def about(key: str) -> tuple[str, str, str]:
|
|
| 184 |
LOCAL_ROOTS: list[str] = []
|
| 185 |
|
| 186 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
def parse_source(raw: str) -> tuple[str, str, str | None]:
|
| 188 |
"""Any LoRA reference into `(kind, source, weight_name)`, `kind` being hf / civitai / direct / local."""
|
| 189 |
raw = raw.strip()
|
|
@@ -511,24 +537,25 @@ def apply(pipe, specs, allow_fold: bool = True, fold_now: bool = False, on_stage
|
|
| 511 |
if spec.path is None:
|
| 512 |
raise ValueError("this slot was never fetched")
|
| 513 |
if spec.adapter not in _resident(pipe):
|
| 514 |
-
|
| 515 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 516 |
stage(f"loading LoRA {spec.label[:60]}")
|
| 517 |
started = time.time()
|
| 518 |
directory, filename = os.path.split(spec.path)
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
# stage. Every thread's stack goes to the log if it takes this long again, to show where.
|
| 522 |
-
import faulthandler
|
| 523 |
-
import sys
|
| 524 |
-
|
| 525 |
-
faulthandler.dump_traceback_later(30, repeat=True, file=sys.stderr)
|
| 526 |
try:
|
| 527 |
pipe.load_lora_weights(directory, weight_name=filename, adapter_name=spec.adapter)
|
| 528 |
print(f"[lora] read {spec.adapter} in {time.time() - started:.1f}s", flush=True)
|
| 529 |
# DiffSynth-Studio's fp32 factors would otherwise drag the unfused LoRA path into fp32.
|
| 530 |
pipe.transformer.to(torch.bfloat16)
|
| 531 |
finally:
|
|
|
|
| 532 |
faulthandler.cancel_dump_traceback_later()
|
| 533 |
# Timed because the booking prices a load: the figure the next large adapter is booked by.
|
| 534 |
print(f"[lora] loaded {spec.label} as `{spec.adapter}` in {time.time() - started:.1f}s", flush=True)
|
|
|
|
| 11 |
|
| 12 |
from __future__ import annotations
|
| 13 |
|
| 14 |
+
import faulthandler
|
| 15 |
+
import functools
|
| 16 |
import hashlib
|
| 17 |
import os
|
| 18 |
import re
|
| 19 |
+
import sys
|
| 20 |
import tempfile
|
| 21 |
+
import time
|
| 22 |
import urllib.request
|
| 23 |
from urllib.parse import unquote
|
| 24 |
from dataclasses import dataclass, replace
|
|
|
|
| 188 |
LOCAL_ROOTS: list[str] = []
|
| 189 |
|
| 190 |
|
| 191 |
+
def preload_modules() -> None:
|
| 192 |
+
"""Import, in the main process, everything `load_lora_weights` would otherwise import on first use.
|
| 193 |
+
|
| 194 |
+
ZeroGPU forks the main process for each GPU call. A module another thread of the main process was halfway
|
| 195 |
+
through importing at that instant is left locked in the fork, and the fork's first import of it waits forever:
|
| 196 |
+
loads hung that way with nothing in the log (2026-11-07). Imported here, the fork finds them all done.
|
| 197 |
+
"""
|
| 198 |
+
for name in (
|
| 199 |
+
"peft", "peft.tuners.tuners_utils", "peft.tuners.lora", "peft.utils", "peft.utils.hotswap",
|
| 200 |
+
"diffusers.loaders.peft", "diffusers.loaders.lora_pipeline", "diffusers.loaders.lora_base",
|
| 201 |
+
"diffusers.loaders.lora_conversion_utils", "diffusers.utils.peft_utils", "diffusers.hooks.group_offloading",
|
| 202 |
+
"safetensors.torch",
|
| 203 |
+
):
|
| 204 |
+
try:
|
| 205 |
+
__import__(name)
|
| 206 |
+
except Exception as error: # a module this diffusers build does not have is not worth failing over
|
| 207 |
+
print(f"[lora] could not preload {name}: {type(error).__name__}", flush=True)
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
preload_modules()
|
| 211 |
+
|
| 212 |
+
|
| 213 |
def parse_source(raw: str) -> tuple[str, str, str | None]:
|
| 214 |
"""Any LoRA reference into `(kind, source, weight_name)`, `kind` being hf / civitai / direct / local."""
|
| 215 |
raw = raw.strip()
|
|
|
|
| 537 |
if spec.path is None:
|
| 538 |
raise ValueError("this slot was never fetched")
|
| 539 |
if spec.adapter not in _resident(pipe):
|
| 540 |
+
# Loads hung inside the GPU worker until ZeroGPU aborted the call, regularly, on any adapter
|
| 541 |
+
# (2026-11-07). The one stack caught sat in diffusers' `load_lora_adapter` calling
|
| 542 |
+
# `self.state_dict()` over the whole transformer, stuck in a `param.detach()` - for a dict whose
|
| 543 |
+
# values this diffusers build never reads (`_create_lora_config` takes it and ignores it). The
|
| 544 |
+
# transformer hands out its parameters as they are for the length of the load, with no tensor op
|
| 545 |
+
# per parameter. Every thread's stack goes to the log if a load still takes 30 s, to show where.
|
| 546 |
+
faulthandler.dump_traceback_later(30, repeat=True, file=sys.stderr)
|
| 547 |
stage(f"loading LoRA {spec.label[:60]}")
|
| 548 |
started = time.time()
|
| 549 |
directory, filename = os.path.split(spec.path)
|
| 550 |
+
transformer = pipe.transformer
|
| 551 |
+
transformer.state_dict = functools.partial(type(transformer).state_dict, transformer, keep_vars=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 552 |
try:
|
| 553 |
pipe.load_lora_weights(directory, weight_name=filename, adapter_name=spec.adapter)
|
| 554 |
print(f"[lora] read {spec.adapter} in {time.time() - started:.1f}s", flush=True)
|
| 555 |
# DiffSynth-Studio's fp32 factors would otherwise drag the unfused LoRA path into fp32.
|
| 556 |
pipe.transformer.to(torch.bfloat16)
|
| 557 |
finally:
|
| 558 |
+
transformer.__dict__.pop("state_dict", None)
|
| 559 |
faulthandler.cancel_dump_traceback_later()
|
| 560 |
# Timed because the booking prices a load: the figure the next large adapter is booked by.
|
| 561 |
print(f"[lora] loaded {spec.label} as `{spec.adapter}` in {time.time() - started:.1f}s", flush=True)
|