Fix Turbo: rename ComfyUI-style model.diffusion_model.* keys to net.* (cached copy) so DiffSynth detects it
Browse files
app.py
CHANGED
|
@@ -338,10 +338,60 @@ def _ensure_fixed_assets() -> FixedAssets:
|
|
| 338 |
return _FIXED_ASSETS
|
| 339 |
|
| 340 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 341 |
def _download_diffusion(diffusion_key: str) -> str:
|
| 342 |
filename = DIFFUSION_MODEL_CHOICES[diffusion_key]
|
| 343 |
anima_dir = _repo_local_dir(MODEL_ID)
|
| 344 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 345 |
|
| 346 |
|
| 347 |
def _load_pipe(diffusion_key: str, progress: Optional["gr.Progress"] = None):
|
|
@@ -1708,6 +1758,8 @@ if __name__ == "__main__":
|
|
| 1708 |
# Download SAM 3D Body code + weights on CPU in the background, so the first Pose 3D
|
| 1709 |
# GPU call only builds the model instead of spending its ZeroGPU slot on downloads.
|
| 1710 |
threading.Thread(target=mesh3d.prefetch, args=(_HF_TOKEN,), daemon=True).start()
|
|
|
|
|
|
|
| 1711 |
if CLASSIC_UI:
|
| 1712 |
demo.launch(share=False, ssr_mode=False, server_port=port)
|
| 1713 |
else:
|
|
|
|
| 338 |
return _FIXED_ASSETS
|
| 339 |
|
| 340 |
|
| 341 |
+
_COMFY_DIT_PREFIX = "model.diffusion_model."
|
| 342 |
+
_DIT_PREFIX = "net."
|
| 343 |
+
_DIT_CONVERT_LOCK = threading.Lock()
|
| 344 |
+
|
| 345 |
+
|
| 346 |
+
def _safetensors_keys(path: str) -> list:
|
| 347 |
+
import struct
|
| 348 |
+
with open(path, "rb") as fh:
|
| 349 |
+
n = struct.unpack("<Q", fh.read(8))[0]
|
| 350 |
+
return [k for k in json.loads(fh.read(n)) if k != "__metadata__"]
|
| 351 |
+
|
| 352 |
+
|
| 353 |
+
def _normalize_dit_checkpoint(path: str) -> str:
|
| 354 |
+
"""Anima Turbo ships with ComfyUI-style keys (``model.diffusion_model.*``) while the other
|
| 355 |
+
checkpoints use ``net.*``. DiffSynth identifies the model by a hash of its key set, so the
|
| 356 |
+
Turbo file fails with "Cannot detect the model type". Tensors and shapes are identical; write a
|
| 357 |
+
renamed copy once (next to the original, on persistent storage) and load that instead."""
|
| 358 |
+
keys = _safetensors_keys(path)
|
| 359 |
+
if not any(k.startswith(_COMFY_DIT_PREFIX) for k in keys):
|
| 360 |
+
return path
|
| 361 |
+
out = str(Path(path).with_suffix("")) + ".net.safetensors"
|
| 362 |
+
with _DIT_CONVERT_LOCK:
|
| 363 |
+
if os.path.exists(out):
|
| 364 |
+
return out
|
| 365 |
+
from safetensors.torch import load_file, save_file
|
| 366 |
+
print(f"[dit] renaming {len(keys)} keys {_COMFY_DIT_PREFIX}* -> {_DIT_PREFIX}* for {Path(path).name}")
|
| 367 |
+
sd = load_file(path, device="cpu")
|
| 368 |
+
sd = {(_DIT_PREFIX + k[len(_COMFY_DIT_PREFIX):]) if k.startswith(_COMFY_DIT_PREFIX) else k: v
|
| 369 |
+
for k, v in sd.items()}
|
| 370 |
+
tmp = out + ".tmp"
|
| 371 |
+
save_file(sd, tmp)
|
| 372 |
+
del sd
|
| 373 |
+
os.replace(tmp, out)
|
| 374 |
+
return out
|
| 375 |
+
|
| 376 |
+
|
| 377 |
def _download_diffusion(diffusion_key: str) -> str:
|
| 378 |
filename = DIFFUSION_MODEL_CHOICES[diffusion_key]
|
| 379 |
anima_dir = _repo_local_dir(MODEL_ID)
|
| 380 |
+
path = _download_file_from_hf(MODEL_ID, f"{DIFFUSION_SUBDIR}/{filename}", anima_dir)
|
| 381 |
+
return _normalize_dit_checkpoint(path)
|
| 382 |
+
|
| 383 |
+
|
| 384 |
+
def _prepare_downloaded_dits() -> None:
|
| 385 |
+
"""Startup (CPU, background): convert already-downloaded ComfyUI-keyed checkpoints so the
|
| 386 |
+
conversion never runs inside a ZeroGPU slot."""
|
| 387 |
+
anima_dir = _repo_local_dir(MODEL_ID)
|
| 388 |
+
for filename in DIFFUSION_MODEL_CHOICES.values():
|
| 389 |
+
p = anima_dir / DIFFUSION_SUBDIR / filename
|
| 390 |
+
if p.exists():
|
| 391 |
+
try:
|
| 392 |
+
_normalize_dit_checkpoint(str(p))
|
| 393 |
+
except Exception as exc: # noqa: BLE001 - retried lazily on first use
|
| 394 |
+
print(f"[dit] startup conversion of {filename} failed: {exc}")
|
| 395 |
|
| 396 |
|
| 397 |
def _load_pipe(diffusion_key: str, progress: Optional["gr.Progress"] = None):
|
|
|
|
| 1758 |
# Download SAM 3D Body code + weights on CPU in the background, so the first Pose 3D
|
| 1759 |
# GPU call only builds the model instead of spending its ZeroGPU slot on downloads.
|
| 1760 |
threading.Thread(target=mesh3d.prefetch, args=(_HF_TOKEN,), daemon=True).start()
|
| 1761 |
+
if not DEV_MODE:
|
| 1762 |
+
threading.Thread(target=_prepare_downloaded_dits, daemon=True).start()
|
| 1763 |
if CLASSIC_UI:
|
| 1764 |
demo.launch(share=False, ssr_mode=False, server_port=port)
|
| 1765 |
else:
|