Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files74 unmodified lines757677787980818283848586878889909192939495969798991007879805 unmodified lines86878811211389909167 unmodified lines1591601611871621631641655 unmodified lines17117217319917417517617785 unmodified lines26326426529129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948026626726874 unmodified lines# for two subjects should not open with nine boxes.
MAX_IMAGE_SLOTS, OPEN_IMAGE_SLOTS = 9, 2
# How many LoRA slots the UI offers, and the range each strength slider covers.
LORA_SLOTS = 3
LORA_MIN_SCALE, LORA_MAX_SCALE = -2.0, 2.0
# Pre-wired Turbo LoRAs from `larryvrh/MiniMax-H3-Turbo-Lora`: a few-step distillation that renders joint video +
# soundtrack in 4–8 steps instead of the usual ~20. Each entry is `(repo reference, recommended steps, blurb)`. The
# reference is the `owner/repo/filename.safetensors` form `resolve_lora` accepts, so it downloads on first use and is
# cached by `huggingface_hub` thereafter — nothing is bundled in this Space.
LORA_PRESETS = {
"Turbo v4 (step 600) · 6–8 steps": (
"larryvrh/MiniMax-H3-Turbo-Lora/minimax_h3_turbo_v4_step600_ema.safetensors",
8,
"Recommended for most work. Strong static / small-motion, good micro-detail, no over-sharpening. "
"Use 6–8 steps; 4 steps can smear on heavy motion.",
),
"Turbo v1 (ckpt 850) · 4 steps": (
"larryvrh/MiniMax-H3-Turbo-Lora/minimax_h3_turbo_4step_ema_ckpt850.safetensors",
4,
"The friendlier pick for 4-step heavy / fast motion, where v4 can trail. Over-sharpens at higher step counts, "
"so keep it at 4 steps.",
),
}
# The lowest step count the model's own schedulers accept; the Turbo LoRAs are tuned for 4.
MIN_STEPS = 4
# Seconds of GPU one request needs, from the packed sequence it is about to denoise: linear in the rows for the5 unmodified linesAUDIO_LATENTS_PER_SECOND, AUDIO_CHANNELS = 40, 2
REFERENCE_IMAGE_SHORT_EDGE, CANVAS_MULTIPLE = 2048, 32
DECODE_BASE, DECODE_PER_DEFAULT_CANVAS, DEFAULT_CANVAS_PIXELS = 15, 25, 960 * 544 * 124
# Reading one adapter off local disk and injecting it across the 33B transformer's linear layers.
LORA_ALLOWANCE = 12
def snap_frames(seconds: float) -> int:67 unmodified lines
def get_duration(
prompt_embeds, text_token_tags, references, height, width, num_frames, steps, seed, loras=(), **_
prompt_embeds, text_token_tags, references, height, width, num_frames, steps, seed, **_
):
"""Seconds of GPU to reserve for one request. Takes the arguments of the `@spaces.GPU` function it decorates, and
tolerates the `gr.Progress` `spaces` injects."""5 unmodified lines # they are handed rather than with the step count.
encode = 5 + reference_rows(references, num_frames) * 1e-3
decode = DECODE_BASE + DECODE_PER_DEFAULT_CANVAS * (height * width * num_frames) / DEFAULT_CANVAS_PIXELS
total = PLACEMENT_ALLOWANCE + encode + denoise + decode + 10 + LORA_ALLOWANCE * len(loras or ())
total = PLACEMENT_ALLOWANCE + encode + denoise + decode + 10
duration = max(MIN_GPU_DURATION, min(MAX_GPU_DURATION, int(total)))
print(f"[ref2va] S={sequence} -> reserving {duration}s ({denoise:.0f}s of denoise at {steps} steps)", flush=True)
return duration85 unmodified lines setattr(module, method, armed)
# ----------------------------------------------------------------------------------------------------------------
# LoRA
# ----------------------------------------------------------------------------------------------------------------
# There is no `MiniMaxH3LoraLoaderMixin` in the diffusers integration, so adapters are attached at the *model* level,
# through the `PeftAdapterMixin` the transformer carries. That is the whole API this needs: `load_lora_adapter` for
# each file and one `set_adapters` call to give them their strengths. Here the model is `transformer_ref`, so the
# adapters have to be trained against the `transformer_ref/` partition — a `transformer/` adapter is a different
# partition and will not match.
def _hub_url_parts(url: str) -> tuple[str, str]:
"""Split a huggingface.co `blob`/`resolve` URL into its repo id and the file path inside it."""
from urllib.parse import unquote, urlparse
parts = unquote(urlparse(url).path).strip("/").split("/")
if len(parts) < 5 or parts[2] not in ("resolve", "blob"):
raise gr.Error(f"Не разпознавам този адрес като файл в Hugging Face: `{url}`")
return "/".join(parts[:2]), "/".join(parts[4:])
def resolve_lora(reference: str) -> str:
"""Turn what the user typed into a local `.safetensors` path.
Accepts a local path, a huggingface.co file URL, `owner/repo/path/to/file.safetensors`, or a bare `owner/repo`
whose single `.safetensors` is then picked for them. Runs outside the GPU call, so the download costs no GPU time.
"""
from huggingface_hub import hf_hub_download, list_repo_files
reference = (reference or "").strip()
if not reference:
return ""
if os.path.exists(reference):
return reference
if reference.startswith(("http://", "https://")):
repo_id, filename = _hub_url_parts(reference)
return hf_hub_download(repo_id, filename)
parts = [part for part in reference.split("/") if part]
if len(parts) > 2 and parts[-1].endswith(".safetensors"):
return hf_hub_download("/".join(parts[:2]), "/".join(parts[2:]))
if len(parts) != 2:
raise gr.Error(
f"`{reference}` не е нито съществуващ файл, нито `автор/хранилище`, нито адрес към Hugging Face."
)
candidates = [name for name in list_repo_files(reference) if name.endswith(".safetensors")]
if not candidates:
raise gr.Error(f"В `{reference}` няма `.safetensors` файл.")
if len(candidates) > 1:
preferred = [name for name in candidates if "lora" in name.lower()]
if len(preferred) != 1:
listed = ", ".join(f"`{name}`" for name in sorted(candidates)[:8])
raise gr.Error(f"`{reference}` съдържа няколко файла. Напиши `{reference}/име.safetensors`. Има: {listed}")
candidates = preferred
return hf_hub_download(reference, candidates[0])
def _lora_prefix(state_dict) -> str | None:
"""The prefix `load_lora_adapter` has to strip before the keys match the transformer's own module names."""
key = next(iter(state_dict))
for prefix in ("model.diffusion_model", "diffusion_model", "transformer_ref", "transformer"):
if key.startswith(f"{prefix}."):
return prefix
return None
def _is_comfyui_lora(state_dict) -> bool:
"""Whether a LoRA state dict is in ComfyUI's MiniMax-H3 naming rather than diffusers'.
ComfyUI names the block stack `blocks.N.*` and the token refiner `token_refiner.blocks.N.*`; diffusers names them
`transformer_blocks.N.*` and `token_refiner.refiner_blocks.N.*`. A key starting with `blocks.` is the tell.
"""
for key in state_dict:
if key.startswith(("blocks.", "token_refiner.blocks.", "final_layer.")):
return True
return False
def _convert_comfyui_lora(state_dict) -> dict:
"""Remap a ComfyUI-format MiniMax-H3 Turbo LoRA to the diffusers `transformer_ref` module names.
The Turbo LoRA ([`larryvrh/MiniMax-H3-Turbo-Lora`](https://huggingface.co/larryvrh/MiniMax-H3-Turbo-Lora)) is trained
against the ComfyUI checkpoint, whose module names differ from diffusers' in four ways:
* the block stack is `blocks.N` in ComfyUI but `transformer_blocks.N` in diffusers,
* the token refiner is `token_refiner.blocks.N` but `token_refiner.refiner_blocks.N`,
* the final AdaLN is `final_layer.adaln_proj.linear` but `norm_out.linear`,
* attention QKV is one fused `attn.qkv_proj` in ComfyUI but three separate `attn.to_q` / `to_k` / `to_v` in
diffusers, and the output projection is `attn.out_proj` but `attn.to_out.0`,
* the feed-forward is `mlp.fc1` / `mlp.fc2` but `ff.fc1` / `ff.fc2`.
The fused QKV `lora_B` is `[3 * inner_dim, rank]`; splitting it into three along dim 0 gives the three separate
`lora_B` matrices, and `lora_A` (which is `[rank, hidden_size]`) is shared verbatim across the three. The metadata
says `W_eff = W + lora_B @ lora_A` with alpha = rank, so the scaling is 1.0 and no alpha key is added.
"""
import torch
converted = {}
for key, value in state_dict.items():
# `blocks.N.` -> `transformer_blocks.N.`
if key.startswith("blocks."):
new_key = "transformer_blocks." + key[len("blocks."):]
elif key.startswith("token_refiner.blocks."):
new_key = "token_refiner.refiner_blocks." + key[len("token_refiner.blocks."):]
elif key.startswith("final_layer.adaln_proj.linear."):
new_key = "norm_out.linear." + key[len("final_layer.adaln_proj.linear."):]
else:
converted[key] = value
continue
# At this point `new_key` is a diffusers block path. Remap the leaf module names.
if ".attn.qkv_proj." in new_key:
# Fused QKV: split `lora_B` along dim 0 into q/k/v, duplicate `lora_A` verbatim.
leaf = new_key.split(".attn.qkv_proj.")[-1] # `lora_A.weight` or `lora_B.weight`
stem = new_key[: new_key.index(".attn.qkv_proj.")]
if leaf == "lora_A.weight":
for proj in ("to_q", "to_k", "to_v"):
converted[f"{stem}.attn.{proj}.lora_A.weight"] = value
else: # lora_B.weight
q_b, k_b, v_b = value.chunk(3, dim=0)
converted[f"{stem}.attn.to_q.lora_B.weight"] = q_b
converted[f"{stem}.attn.to_k.lora_B.weight"] = k_b
converted[f"{stem}.attn.to_v.lora_B.weight"] = v_b
elif ".attn.out_proj." in new_key:
converted[new_key.replace(".attn.out_proj.", ".attn.to_out.0.")] = value
elif ".mlp." in new_key:
converted[new_key.replace(".mlp.", ".ff.")] = value
else:
# `adaln_proj.linear` and the token refiner's attention/ff already match diffusers' names after the
# block-prefix rename above.
converted[new_key] = value
return converted
def apply_loras(transformer, loras) -> list[str]:
"""Attach `loras` (local path, strength) to `transformer` and give each its strength, replacing whatever was on it.
Every adapter already on the model is removed first, so a request is never affected by the one before it — which
matters when a worker is reused rather than forked fresh. A LoRA in ComfyUI's MiniMax-H3 naming is remapped to
diffusers' module names on the fly, so the Turbo LoRA works without a separate conversion step.
"""
import torch
from safetensors.torch import load_file
for name in list(getattr(transformer, "peft_config", None) or {}):
transformer.delete_adapters(name)
names, scales = [], []
for index, (path, scale) in enumerate(loras):
state_dict = load_file(path)
if _is_comfyui_lora(state_dict):
state_dict = _convert_comfyui_lora(state_dict)
name = f"lora{index}"
transformer.load_lora_adapter(state_dict, adapter_name=name, prefix=_lora_prefix(state_dict))
names.append(name)
scales.append(float(scale))
if not names:
return []
# PEFT builds the new layers on its own default device/dtype; the base weights are the truth here, under either
# placement mode (`offload` keeps them on the host and moves whole modules by hook).
base = next(param for key, param in transformer.named_parameters() if ".lora_" not in key)
with torch.no_grad():
for key, param in transformer.named_parameters():
if ".lora_" in key and (param.device != base.device or param.dtype != base.dtype):
param.data = param.data.to(device=base.device, dtype=base.dtype)
transformer.set_adapters(names, scales)
return names
def collect_loras(lora_fields, progress) -> tuple[list[tuple[str, float]], list[str]]:
"""Resolve the UI's `reference, strength, reference, strength, ...` into `(local path, strength)` pairs.
Resolved before the booking: a download that happens inside `@spaces.GPU` is billed as GPU time.
"""
loras, labels = [], []
for reference, scale in zip(lora_fields[::2], lora_fields[1::2]):
reference = (reference or "").strip()
if not reference or abs(float(scale)) < 1e-6:
continue
progress(0.0, desc=f"Fetching LoRA {reference} ...")
loras.append((resolve_lora(reference), float(scale)))
labels.append(f"{os.path.basename(reference)} @ {float(scale):g}")
if loras and os.environ.get("H3_AOTI") == "1":
raise gr.Error("LoRA не може да се приложи върху AoTI компилиран трансформър. Изключи `H3_AOTI`.")
return loras, labels
@cache
def conditioner():app.py+2-217