Upload code/faz3_v6/meshai_api/faz3_lora_bind.py with huggingface_hub
Browse files
code/faz3_v6/meshai_api/faz3_lora_bind.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Resolve / apply Faz3 TRELLIS + Paint LoRA checkpoints at inference."""
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import os
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
from typing import Any
|
| 8 |
+
|
| 9 |
+
import torch
|
| 10 |
+
|
| 11 |
+
ROOT = Path(__file__).resolve().parents[1]
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def resolve_faz3_geo_ckpt() -> Path | None:
|
| 15 |
+
raw = os.environ.get("MESHAI_FAZ3_LORA", "").strip()
|
| 16 |
+
for p in (
|
| 17 |
+
Path(raw) if raw else None,
|
| 18 |
+
ROOT / "checkpoints" / "latest_faz3_lora.pt",
|
| 19 |
+
):
|
| 20 |
+
if p is not None and p.is_file() and p.stat().st_size > 10_000:
|
| 21 |
+
return p
|
| 22 |
+
return None
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def resolve_faz3_paint_ckpt() -> Path | None:
|
| 26 |
+
raw = os.environ.get("MESHAI_FAZ3_PAINT_LORA", "").strip()
|
| 27 |
+
for p in (
|
| 28 |
+
Path(raw) if raw else None,
|
| 29 |
+
ROOT / "checkpoints" / "latest_faz3_paint_lora.pt",
|
| 30 |
+
):
|
| 31 |
+
if p is not None and p.is_file() and p.stat().st_size > 10_000:
|
| 32 |
+
return p
|
| 33 |
+
return None
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def apply_faz3_lora_state(root: Any, blob: dict[str, Any], *, log: Any = print) -> int:
|
| 37 |
+
from meshai_train.base_weights import FrozenLinearLoRA
|
| 38 |
+
from meshai_train.lora_inject import inject_lora_into_linears
|
| 39 |
+
|
| 40 |
+
state = blob.get("lora_state") or {}
|
| 41 |
+
if not state:
|
| 42 |
+
return 0
|
| 43 |
+
inject_lora_into_linears(root, rank=int(blob.get("lora_rank") or 8), max_layers=64, log_fn=log)
|
| 44 |
+
n = 0
|
| 45 |
+
for name, mod in root.named_modules():
|
| 46 |
+
if name not in state or not isinstance(mod, FrozenLinearLoRA):
|
| 47 |
+
continue
|
| 48 |
+
slot = state[name]
|
| 49 |
+
with torch.no_grad():
|
| 50 |
+
mod.lora_A.copy_(slot["lora_A"].to(device=mod.lora_A.device, dtype=mod.lora_A.dtype))
|
| 51 |
+
mod.lora_B.copy_(slot["lora_B"].to(device=mod.lora_B.device, dtype=mod.lora_B.dtype))
|
| 52 |
+
n += 1
|
| 53 |
+
log(f"[faz3] applied LoRA slots={n}")
|
| 54 |
+
return n
|