Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -109,15 +109,16 @@ def lower_duration_floor(seconds: float = MIN_UI_DURATION) -> None:
|
|
| 109 |
|
| 110 |
MiniMaxH3ModularPipeline.min_duration = property(lambda self: float(seconds))
|
| 111 |
|
| 112 |
-
def _convert_diffusion_model_lora(raw: dict,
|
| 113 |
"""Rename a `diffusion_model.blocks.*` (original-checkpoint) LoRA state dict onto
|
| 114 |
`MiniMaxH3Transformer3DModel`'s (`transformer_blocks.*`) naming, so `load_lora_adapter` can attach it.
|
| 115 |
-
`raw` maps original key -> tensor. `
|
| 116 |
-
|
|
|
|
|
|
|
| 117 |
"""
|
| 118 |
import re
|
| 119 |
|
| 120 |
-
base = transformer.state_dict()
|
| 121 |
out = {}
|
| 122 |
pattern = re.compile(r"^diffusion_model\.blocks\.(\d+)\.(attn|mlp)\.(\w+)\.(lora_[AB])\.weight$")
|
| 123 |
|
|
@@ -136,9 +137,9 @@ def _convert_diffusion_model_lora(raw: dict, transformer) -> dict:
|
|
| 136 |
out[f"{prefix}attn.to_k.{ab}.weight"] = tensor
|
| 137 |
out[f"{prefix}attn.to_v.{ab}.weight"] = tensor
|
| 138 |
else:
|
| 139 |
-
q_out =
|
| 140 |
-
k_out =
|
| 141 |
-
v_out =
|
| 142 |
assert tensor.shape[0] == q_out + k_out + v_out, (
|
| 143 |
f"{key}: expected {q_out + k_out + v_out} rows (q{q_out}+k{k_out}+v{v_out}), "
|
| 144 |
f"got {tensor.shape[0]}"
|
|
@@ -260,12 +261,15 @@ def load_models() -> str | None:
|
|
| 260 |
from safetensors import safe_open
|
| 261 |
|
| 262 |
try:
|
|
|
|
|
|
|
|
|
|
| 263 |
counts = {}
|
| 264 |
for name, filename in LORA_FILES.items():
|
| 265 |
path = hf_hub_download(LORA_REPO, filename)
|
| 266 |
with safe_open(path, framework="pt") as handle:
|
| 267 |
raw = {k: handle.get_tensor(k) for k in handle.keys()}
|
| 268 |
-
converted = _convert_diffusion_model_lora(raw,
|
| 269 |
pipe.transformer.load_lora_adapter(converted, adapter_name=name, prefix=None)
|
| 270 |
# `load_lora_adapter` warns-and-continues on a zero-key match instead of raising, so count
|
| 271 |
# matched layers ourselves and fail loudly if a file attached nothing.
|
|
|
|
| 109 |
|
| 110 |
MiniMaxH3ModularPipeline.min_duration = property(lambda self: float(seconds))
|
| 111 |
|
| 112 |
+
def _convert_diffusion_model_lora(raw: dict, base_shapes: dict) -> dict:
|
| 113 |
"""Rename a `diffusion_model.blocks.*` (original-checkpoint) LoRA state dict onto
|
| 114 |
`MiniMaxH3Transformer3DModel`'s (`transformer_blocks.*`) naming, so `load_lora_adapter` can attach it.
|
| 115 |
+
`raw` maps original key -> tensor. `base_shapes` maps the *unwrapped* base model's parameter names to their
|
| 116 |
+
shapes — captured once before any adapter is attached, since `load_lora_adapter` wraps each target Linear in
|
| 117 |
+
a PEFT layer and renames its weight to `<name>.base_layer.weight`, so a live `transformer.state_dict()` call
|
| 118 |
+
after the first adapter attaches would no longer have `to_q.weight` etc. under their original names.
|
| 119 |
"""
|
| 120 |
import re
|
| 121 |
|
|
|
|
| 122 |
out = {}
|
| 123 |
pattern = re.compile(r"^diffusion_model\.blocks\.(\d+)\.(attn|mlp)\.(\w+)\.(lora_[AB])\.weight$")
|
| 124 |
|
|
|
|
| 137 |
out[f"{prefix}attn.to_k.{ab}.weight"] = tensor
|
| 138 |
out[f"{prefix}attn.to_v.{ab}.weight"] = tensor
|
| 139 |
else:
|
| 140 |
+
q_out = base_shapes[f"{prefix}attn.to_q.weight"][0]
|
| 141 |
+
k_out = base_shapes[f"{prefix}attn.to_k.weight"][0]
|
| 142 |
+
v_out = base_shapes[f"{prefix}attn.to_v.weight"][0]
|
| 143 |
assert tensor.shape[0] == q_out + k_out + v_out, (
|
| 144 |
f"{key}: expected {q_out + k_out + v_out} rows (q{q_out}+k{k_out}+v{v_out}), "
|
| 145 |
f"got {tensor.shape[0]}"
|
|
|
|
| 261 |
from safetensors import safe_open
|
| 262 |
|
| 263 |
try:
|
| 264 |
+
# Snapshot once, before either adapter attaches and wraps the target Linears — see the
|
| 265 |
+
# docstring on `_convert_diffusion_model_lora` for why this can't be read fresh per-file.
|
| 266 |
+
base_shapes = {k: tuple(v.shape) for k, v in pipe.transformer.state_dict().items()}
|
| 267 |
counts = {}
|
| 268 |
for name, filename in LORA_FILES.items():
|
| 269 |
path = hf_hub_download(LORA_REPO, filename)
|
| 270 |
with safe_open(path, framework="pt") as handle:
|
| 271 |
raw = {k: handle.get_tensor(k) for k in handle.keys()}
|
| 272 |
+
converted = _convert_diffusion_model_lora(raw, base_shapes)
|
| 273 |
pipe.transformer.load_lora_adapter(converted, adapter_name=name, prefix=None)
|
| 274 |
# `load_lora_adapter` warns-and-continues on a zero-key match instead of raising, so count
|
| 275 |
# matched layers ourselves and fail loudly if a file attached nothing.
|