Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio diffusion: match family keywords only on a local model's own path, and undo the torch 2.13 slice patch in the speed tests (#14435)

Daniel Han684e0adbfd56
diff --git a/studio/backend/core/inference/diffusion_families.py b/studio/backend/core/inference/diffusion_families.py
index 1f165a24fcb..cd6672dfc79 100644
--- a/studio/backend/core/inference/diffusion_families.py
+++ b/studio/backend/core/inference/diffusion_families.py
@@ -24,6 +24,7 @@ from utils.paths.path_utils import is_appledouble_metadata
from .diffusion_flow_shift import flux_mu_shift
from .family_name_match import (
+ model_identity,
name_key_in,
normalize_family_name,
token_in_name,
@@ -1321,7 +1322,7 @@ def detect_family(repo_id: str, override: Optional[str] = None) -> Optional[Diff
if any(normalize_family_name(t) == norm for t in (fam.name, *fam.aliases)):
return fam
return None
- needle = repo_id.lower()
+ needle = model_identity(repo_id).lower()
match = _best_family_match(needle)
if match is not None:
# Do not let a generic family (qwen-image) swallow a variant it cannot run (qwen-image-LAYERED). Scoped to the
@@ -1476,7 +1477,9 @@ def _family_from_content(
if not path:
return fam
needles = [repo_id] + ([f"{repo_id}/{gguf_filename}"] if gguf_filename else [])
- vetoed = fam is None and any(_best_family_match(n.lower()) is not None for n in needles)
+ vetoed = fam is None and any(
+ _best_family_match(model_identity(n).lower()) is not None for n in needles
+ )
name, _ = resolve_family_with_content(fam.name if fam else None, path, "image", vetoed)
if name is None:
return None
@@ -2087,7 +2090,7 @@ def transformer_variant_differs_from_base(
def named_generation_params(*identifiers: Optional[str]) -> Optional[tuple[int, float]]:
"""``(steps, guidance)`` of the first identifier naming a known model (a local path resolves via its base), or None."""
for identifier in identifiers:
- needle = (identifier or "").lower()
+ needle = model_identity(identifier).lower()
for key, steps, guidance in _GENERATION_DEFAULTS:
if name_key_in(key, needle):
return steps, guidance
diff --git a/studio/backend/core/inference/family_name_match.py b/studio/backend/core/inference/family_name_match.py
index c2058b201f7..0325460d32a 100644
--- a/studio/backend/core/inference/family_name_match.py
+++ b/studio/backend/core/inference/family_name_match.py
@@ -44,3 +44,39 @@ def name_key_in(key: str, needle: str) -> bool:
return True
nk = normalize_family_name(k)
return bool(nk) and nk in normalize_family_name(low)
+
+
+_LOCAL_PATH = re.compile(r"^(?:[/\\~]|\.{1,2}[/\\]|[a-zA-Z]:[/\\])")
+_PATH_SPLIT = re.compile(r"[/\\]+")
+_WEIGHT_FILE_SUFFIXES = (".gguf", ".safetensors", ".sft", ".ckpt", ".pt", ".pth", ".bin")
+# Folders holding a model's files without naming it (diffusers component, ComfyUI kind folder, quant tier).
+_CONTAINER_SEGMENT = re.compile(
+ r"^(?:transformer(?:_\d)?|unet|vae|text_encoder(?:_\d)?|tokenizer(?:_\d)?|scheduler|"
+ r"diffusion_models|split_files|gguf|weights?|checkpoints?|ckpts?|models?|files|safetensors|original|"
+ r"(?:u?d-)?(?:i?q\d\w*|f16|bf16|f32|fp8\w*|fp16|nvfp4|mxfp4))$",
+ re.IGNORECASE,
+)
+
+
+def model_identity(text: Optional[str]) -> str:
+ """The part of ``text`` naming the model: repo ids unchanged; a local path drops parent folders (an arbitrary
+ keyword there must not pick a family), and an HF cache path ``models--org--name/snapshots/<sha>/f`` -> ``org/name/f``."""
+ text = text or ""
+ if not _LOCAL_PATH.match(text):
+ return text
+ parts = [p for p in _PATH_SPLIT.split(text) if p]
+ for i in range(len(parts) - 1, -1, -1):
+ seg = parts[i]
+ if seg.lower().startswith("models--") and len(seg) > len("models--"):
+ rest = parts[i + 1 :]
+ if len(rest) >= 2 and rest[0].lower() == "snapshots":
+ rest = rest[2:]
+ return "/".join([seg[len("models--") :].replace("--", "/"), *rest])
+ if not parts:
+ return ""
+ start = len(parts) - 1
+ if start > 0 and parts[-1].lower().endswith(_WEIGHT_FILE_SUFFIXES):
+ start -= 1
+ while 0 < start and _CONTAINER_SEGMENT.match(parts[start]):
+ start -= 1
+ return "/".join(parts[start:])
diff --git a/studio/backend/core/inference/video_families.py b/studio/backend/core/inference/video_families.py
index f6da110806d..96c0279b6aa 100644
--- a/studio/backend/core/inference/video_families.py
+++ b/studio/backend/core/inference/video_families.py
@@ -23,7 +23,7 @@ from dataclasses import dataclass, field
from typing import Optional
from .diffusion_nvfp4_flag import nvfp4_blocked, without_nvfp4
-from .family_name_match import normalize_family_name, token_in_name, token_length
+from .family_name_match import model_identity, normalize_family_name, token_in_name, token_length
from .family_profile import AutoPrefer, ConvRotSpec, FamilyProfile, UntiledVae
# The request model's ceiling on num_frames, declared HERE so the shape gate and the bound cannot drift: the gate's
@@ -1062,7 +1062,7 @@ def detect_video_family(repo_id: str, override: Optional[str] = None) -> Optiona
# I2V / VACE / FastWan names can carry a T2V token (Wan2.1_T2V_14B_FusionX_VACE): no T2V pipeline over those weights.
if unsupported_video_checkpoint_reason(repo_id):
return None
- needle = repo_id.lower()
+ needle = model_identity(repo_id).lower()
best: Optional[tuple[VideoFamily, int]] = None
for fam in _FAMILIES:
for token in (fam.name, *fam.aliases):
@@ -1706,7 +1706,7 @@ def video_generation_variant(
``default_video_generation_params`` uses, so every per-variant decision agrees with the defaults. Family-scoped
keys (``_FAMILY_SCOPED_DEFAULT_KEYS``) match only for their own family."""
for identifier in identifiers:
- whole = (identifier or "").lower()
+ whole = model_identity(identifier).lower()
name = whole.replace("\\", "/").rstrip("/").rsplit("/", 1)[-1]
for key, _steps, _guidance in _VIDEO_GENERATION_DEFAULTS:
scope = _FAMILY_SCOPED_DEFAULT_KEYS.get(key)
diff --git a/studio/backend/tests/test_diffusion_more_families.py b/studio/backend/tests/test_diffusion_more_families.py
index 10e861e243b..57344d8a553 100644
--- a/studio/backend/tests/test_diffusion_more_families.py
+++ b/studio/backend/tests/test_diffusion_more_families.py
@@ -1056,3 +1056,32 @@ def test_an_opaque_local_load_with_a_shipped_grid_runs_its_step_count():
# A name still decides first, and no grid keeps the fallback.
assert generation_params_with_grid(grid, "Qwen/Qwen-Image-2.1-Turbo") == (8, 1.0)
assert generation_params_with_grid(None, "/models/opaque") == (9, 0.0)
+
+
+ "parent",
+ [
+ "/home/flux.1/models",
+ "/data/sdxl_backup",
+ "D:\\qwen-image\\archive",
+ "/srv/2.14_flux.1/test0",
+ ],
+)
+def test_a_parent_folder_keyword_does_not_pick_the_image_family(parent):
+ sep = "\\" if "\\" in parent else "/"
+ assert detect_family(sep.join([parent, "plain"])) is None
+ assert detect_family(sep.join([parent, "plain", "model.safetensors"])) is None
+ assert detect_family(sep.join([parent, "plain", "transformer", "model.safetensors"])) is None
+ assert default_generation_params(sep.join([parent, "plain", "model.safetensors"])) == (
+ default_generation_params("/data/plain/model.safetensors")
+ )
+ fam = detect_family(sep.join([parent, "FLUX.1-dev"]))
+ assert fam is not None and fam.name == "flux.1"
+ fam = detect_family(sep.join([parent, "plain", "flux1-schnell-Q4_K_M.gguf"]))
+ assert fam is not None and fam.name == "flux.1"
+ for sub in ("weights", "checkpoints", "models", "ckpt"):
+ nested = sep.join([parent, "Qwen-Image-Edit", sub, "model.safetensors"])
+ assert detect_family(nested).name == "qwen-image-edit"
+ hf = "/home/sdxl/.cache/huggingface/hub/models--black-forest-labs--FLUX.1-dev/snapshots/0123abc"
+ assert detect_family(hf).name == "flux.1"
+ assert detect_family(hf + "/flux1-dev.safetensors").name == "flux.1"
diff --git a/studio/backend/tests/test_diffusion_speed.py b/studio/backend/tests/test_diffusion_speed.py
index 3d220e4626b..1c6d0a766b7 100644
--- a/studio/backend/tests/test_diffusion_speed.py
+++ b/studio/backend/tests/test_diffusion_speed.py
@@ -9,6 +9,7 @@ gating logic and the best-effort applier run without a GPU or real diffusers.
from __future__ import annotations
+import importlib.util
import sys
import types
@@ -30,6 +31,20 @@ from core.inference.diffusion_speed import (
)
+_needs_torch = pytest.mark.skipif(importlib.util.find_spec("torch") is None, reason = "needs torch")
+
+
[email protected](autouse = True)
+def _undo_slice_identity_fix():
+ """Undo the process-wide torch 2.13+ SliceVariable patch apply_speed_optims may install."""
+ from core.inference import diffusion_dynamic_text as dt
+
+ was_installed = "original" in dt._SLICE_STATE
+ yield
+ if not was_installed and "original" in dt._SLICE_STATE:
+ dt.uninstall_slice_identity_fix()
+
+
def _stub_gguf_accel(monkeypatch):
"""Replace the real compiled-dequant installer (which touches torch.compile /
diffusers) with a recorder, so the tier-gating logic in apply_speed_optims is tested
@@ -2541,6 +2556,7 @@ def test_pinned_denoiser_engages_the_int8_gemm_after_placement(monkeypatch):
assert calls == [] and not eager["int8_gemm"]
+@_needs_torch
@pytest.mark.parametrize(
"offload_active, denoiser_offloaded, expected",
[(True, False, False), (True, True, True), (True, None, True), (False, None, False)],
@@ -2822,6 +2838,7 @@ def test_native_quant_never_runs_the_rocm_probe():
assert guarded
+@_needs_torch
def test_rocm_kill_switch_declines_the_planned_dequant_step(monkeypatch):
import torch
@@ -3079,6 +3096,7 @@ def test_rocm_probe_still_runs_for_a_compilable_model(monkeypatch):
assert len(calls) == 1
+@_needs_torch
def test_rocm_probe_answer_is_read_without_waiting_for_another_probe(monkeypatch):
import threading
diff --git a/studio/backend/tests/test_video_families.py b/studio/backend/tests/test_video_families.py
index 0742f4f82ab..30007c95baa 100644
--- a/studio/backend/tests/test_video_families.py
+++ b/studio/backend/tests/test_video_families.py
@@ -562,3 +562,73 @@ def test_the_a14b_row_prices_both_experts_not_one():
measured = video_family_prequant_resident_gb(fam, "nvfp4")
assert measured == pytest.approx(16.2)
assert measured > 0.25 * fam.bf16_components_gb[0]
+
+
+ "parent",
+ [
+ "/data/h3_backup/models",
+ "/home/wan/models",
+ "/mnt/ltx-2/archive",
+ "/srv/flux.1/cache",
+ "C:\\Users\\h3\\models",
+ "/tmp/pytest-of-u/2.14_h3/test_x0",
+ ],
+)
+def test_a_parent_folder_keyword_does_not_pick_the_video_family(parent):
+ sep = "\\" if "\\" in parent else "/"
+ assert detect_video_family(sep.join([parent, "my-videos"])) is None
+ assert detect_video_family(sep.join([parent, "my-videos", "model-Q4_K_M.gguf"])) is None
+ assert (
+ detect_video_family(sep.join([parent, "my-videos", "transformer", "model.safetensors"]))
+ is None
+ )
+ ltx = detect_video_family(sep.join([parent, "ltx-2.3-gguf", "ltx-2.3-22b-dev-Q4_K_M.gguf"]))
+ assert ltx is not None and ltx.name == "ltx-2"
+
+
+def test_the_model_own_folder_or_file_still_names_the_video_family():
+ def name(path):
+ fam = detect_video_family(path)
+ return fam.name if fam else None
+
+ assert name("/data/models/plain/ltx-2-19b-dev-Q4_K_M.gguf") == "ltx-2"
+ assert name("/data/models/LTX-2-GGUF/model-Q4_K_M.gguf") == "ltx-2"
+ assert name("/data/models/LTX-2-GGUF/Q4_K_M/model.gguf") == "ltx-2"
+ assert name("/data/models/LTX-2") == "ltx-2"
+ assert name("/data/models/LTX-2/") == "ltx-2"
+ assert name("/data/models/LTX-2/weights/model.safetensors") == "ltx-2"
+ assert name("/data/models/LTX-2/checkpoints/model.safetensors") == "ltx-2"
+ assert (
+ name("/data/models/MiniMax-H3/transformer/diffusion_pytorch_model.safetensors")
+ == "minimax-h3"
+ )
+ assert name("/data/models/Wan2.2-T2V-A14B-Diffusers") == "wan2.2-t2v-a14b"
+ cache = "/home/h3/.cache/huggingface/hub/models--Lightricks--LTX-2/snapshots/0123abc"
+ assert name(cache) == "ltx-2"
+ assert name(cache + "/ltx-2-19b-dev-fp8.safetensors") == "ltx-2"
+ assert (
+ name(
+ "/home/ltx/.cache/huggingface/hub/models--org--clip/snapshots/0123abc/model.safetensors"
+ )
+ is None
+ )
+ assert name("MiniMaxAI/MiniMax-H3") == "minimax-h3"
+ assert name("unsloth/LTX-2.3-GGUF/ltx-2.3-22b-dev-Q4_K_M.gguf") == "ltx-2"
+
+
+def test_a_parent_folder_keyword_does_not_pick_the_video_defaults():
+ plain = default_video_generation_params("/data/plain/model.gguf", fallback = (7, 7.0))
+ assert plain == (7, 7.0)
+ for parent in ("/data/wan2.1/x", "/home/ltx/x", "/srv/distilled/x"):
+ assert (
+ default_video_generation_params(parent + "/plain/model.gguf", fallback = (7, 7.0))
+ == plain
+ )
+ assert default_video_generation_params(
+ "/data/x/wan2.1-t2v-14b-Q4_K_M.gguf", fallback = (7, 7.0)
+ ) == (20, 6.0)
+ assert (
+ default_video_generation_params("/data/x/ltx-2-19b-distilled/model.gguf", fallback = (7, 7.0))
+ != plain
+ )