Spaces:
Sleeping
Sleeping
Update app.py
Browse files16 unmodified lines17181920212280 unmodified lines10310410510610710880 unmodified lines1891901911921931941951961971981992002012022032042052063 unmodified lines21021121221321421521621716 unmodified linesimport tracebackfrom functools import cache# Before anything that could initialize CUDA: `import spaces` patches `torch.cuda` so the 72 GiB load can happen at# startup rather than on GPU time.import spaces80 unmodified linesMiniMaxH3ModularPipeline.min_duration = property(lambda self: float(seconds))PIPE = NoneFILM = None80 unmodified linesfor k, shape in sorted(norm_out.items()):print(f"[lora-debug] {k} {shape}", flush=True)# Approach B: attach both LoRA adapters as PEFT layers on the transformer, inactive (weight 0) until a# request asks for them. `load_lora_adapter` is the model-level loader (`PeftAdapterMixin`), used because# `MiniMaxH3ModularPipeline` has no pipeline-level `load_lora_weights` of its own.if LORA_REPO.lower() not in ("", "off", "none"):from peft.tuners.tuners_utils import BaseTunerLayertry:counts = {}for name, filename in LORA_FILES.items():pipe.transformer.load_lora_adapter(LORA_REPO, weight_name=filename, adapter_name=name, prefix=None)# `load_lora_adapter` warns-and-continues on a zero-key match instead of raising, so count# matched layers ourselves and fail loudly if a file attached nothing.counts[name] = sum(3 unmodified lines)if counts[name] == 0:raise RuntimeError(f"'{filename}' matched 0 target modules on MiniMaxH3Transformer3DModel — its LoRA "f"keys don't line up with this transformer's naming. Inspect its safetensors keys.")pipe.transformer.set_adapters(list(LORA_FILES), weights=[0.0] * len(LORA_FILES))LORA_STATUS = "LoRAs loaded: " + ", ".join(16 unmodified lines171819202122232480 unmodified lines10510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416580 unmodified lines2462472482492502512522532542552562572582592602612622632642652662672683 unmodified lines27227327427527627727827916 unmodified linesimport tracebackfrom functools import cacheimport torch# Before anything that could initialize CUDA: `import spaces` patches `torch.cuda` so the 72 GiB load can happen at# startup rather than on GPU time.import spaces80 unmodified linesMiniMaxH3ModularPipeline.min_duration = property(lambda self: float(seconds))def _convert_diffusion_model_lora(raw: dict, transformer) -> dict:"""Rename a `diffusion_model.blocks.*` (original-checkpoint) LoRA state dict onto`MiniMaxH3Transformer3DModel`'s (`transformer_blocks.*`) naming, so `load_lora_adapter` can attach it.`raw` maps original key -> tensor. `transformer` is the already-loaded base model, used only to read itsreal to_q/to_k/to_v output sizes for the qkv split, rather than assuming an even three-way split."""import rebase = transformer.state_dict()out = {}pattern = re.compile(r"^diffusion_model\.blocks\.(\d+)\.(attn|mlp)\.(\w+)\.(lora_[AB])\.weight$")for key, tensor in raw.items():match = pattern.match(key)if not match:print(f"[lora-convert] skipping unrecognized key: {key}", flush=True)continueblock, kind, leaf, ab = match.groups()prefix = f"transformer_blocks.{block}."if kind == "attn" and leaf == "qkv_proj":if ab == "lora_A":# Shared low-rank input side — identical for q, k, v.out[f"{prefix}attn.to_q.{ab}.weight"] = tensorout[f"{prefix}attn.to_k.{ab}.weight"] = tensorout[f"{prefix}attn.to_v.{ab}.weight"] = tensorelse:q_out = base[f"{prefix}attn.to_q.weight"].shape[0]k_out = base[f"{prefix}attn.to_k.weight"].shape[0]v_out = base[f"{prefix}attn.to_v.weight"].shape[0]assert tensor.shape[0] == q_out + k_out + v_out, (f"{key}: expected {q_out + k_out + v_out} rows (q{q_out}+k{k_out}+v{v_out}), "f"got {tensor.shape[0]}")out[f"{prefix}attn.to_q.{ab}.weight"] = tensor[:q_out].clone()out[f"{prefix}attn.to_k.{ab}.weight"] = tensor[q_out:q_out + k_out].clone()out[f"{prefix}attn.to_v.{ab}.weight"] = tensor[q_out + k_out:].clone()elif kind == "attn" and leaf == "out_proj":out[f"{prefix}attn.to_out.0.{ab}.weight"] = tensorelif kind == "mlp" and leaf == "fc1":if ab == "lora_B" and SWAP_FC1_HALVES:# Some checkpoints store the SwiGLU gate/value halves in the opposite order diffusers expects.half = tensor.shape[0] // 2tensor = torch.cat([tensor[half:], tensor[:half]], dim=0)out[f"{prefix}ff.net.0.proj.{ab}.weight"] = tensorelif kind == "mlp" and leaf == "fc2":out[f"{prefix}ff.net.2.{ab}.weight"] = tensorelse:print(f"[lora-convert] no mapping for {key}, skipping", flush=True)return outPIPE = NoneFILM = None80 unmodified linesfor k, shape in sorted(norm_out.items()):print(f"[lora-debug] {k} {shape}", flush=True)# Approach B: convert each LoRA from its original `diffusion_model.blocks.*` naming onto this# transformer's `transformer_blocks.*` naming, then attach as PEFT layers, inactive (weight 0) until a# request asks for them. `load_lora_adapter` is the model-level loader (`PeftAdapterMixin`), used because# `MiniMaxH3ModularPipeline` has no pipeline-level `load_lora_weights` of its own.if LORA_REPO.lower() not in ("", "off", "none"):from huggingface_hub import hf_hub_downloadfrom peft.tuners.tuners_utils import BaseTunerLayerfrom safetensors import safe_opentry:counts = {}for name, filename in LORA_FILES.items():path = hf_hub_download(LORA_REPO, filename)with safe_open(path, framework="pt") as handle:raw = {k: handle.get_tensor(k) for k in handle.keys()}converted = _convert_diffusion_model_lora(raw, pipe.transformer)pipe.transformer.load_lora_adapter(converted, adapter_name=name, prefix=None)# `load_lora_adapter` warns-and-continues on a zero-key match instead of raising, so count# matched layers ourselves and fail loudly if a file attached nothing.counts[name] = sum(3 unmodified lines)if counts[name] == 0:raise RuntimeError(f"'{filename}' converted but matched 0 target modules on "f"MiniMaxH3Transformer3DModel — the rename map needs another look.")pipe.transformer.set_adapters(list(LORA_FILES), weights=[0.0] * len(LORA_FILES))LORA_STATUS = "LoRAs loaded: " + ", ".join(
app.py+68-6