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 Images: detect v-prediction SDXL fine-tunes from their own config, not their name (#14746)

Daniel Han6267ef565c76
diff --git a/studio/backend/core/inference/diffusion.py b/studio/backend/core/inference/diffusion.py
index f87abe84ef9..1df95724c9f 100644
--- a/studio/backend/core/inference/diffusion.py
+++ b/studio/backend/core/inference/diffusion.py
@@ -42,6 +42,7 @@ from core._torchao_stub import (
install_xformers_windows_rocm_stub,
)
from hub.utils.hf_errors import modelscope_missing
+from hub.utils.hf_tokens import normalize_token
from loggers import get_logger
from utils.gpu_memory_events import invalidates_gpu_memory as _invalidates_gpu_memory
from utils.account_context import account_thread, current_account_id
@@ -358,6 +359,7 @@ from .diffusion_mask import (
from .diffusion_sampler import (
DEFAULT_SAMPLER,
apply_checkpoint_prediction_type,
+ apply_pipeline_prediction_type,
build_scheduler,
sampler_options,
)
@@ -4679,6 +4681,8 @@ class DiffusionBackend:
with self._load_cancel_lock:
entry_token = _load_token
self._raise_if_load_cancelled(entry_token)
+ # The probes this load adds read the Hub directly; they keep a managed account's False (anonymous) sentinel.
+ hub_token = normalize_token(hf_token)
hf_token = (hf_token.strip() if isinstance(hf_token, str) else hf_token) or None
# Resolved ONCE, here, and carried to the worker: outside it so a bad pick is the route's 400 rather than a
# load that dies mid-download, and only once so free VRAM cannot re-rank the choice after the weights land.
@@ -4790,6 +4794,7 @@ class DiffusionBackend:
_load_token = token,
_cancel_event = cancel_event,
_gpu_ordinals = gpu_ordinals,
+ _hub_token = hub_token,
),
daemon = True,
).start()
@@ -7444,11 +7449,14 @@ class DiffusionBackend:
_pipeline_prequant_skipped: tuple[str, ...] = (),
_prequant_unreachable: tuple[str, ...] = (),
_te_prequant_resolved: bool = True,
+ # begin_load's ``normalize_token`` of the request token: False stays the anonymous sentinel.
+ _hub_token: Any = None,
) -> dict[str, Any]:
with self._load_cancel_lock:
if _load_token is None:
_load_token = self._load_token
self._raise_if_load_cancelled(_load_token)
+ hub_token = _hub_token if _hub_token is not None else normalize_token(hf_token)
# A blank token must degrade to anonymous, not be passed as a credential. Normalize once.
hf_token = hf_token.strip() if isinstance(hf_token, str) else hf_token
hf_token = hf_token or None
@@ -8841,6 +8849,19 @@ class DiffusionBackend:
)
finally:
self._stop_load_prefetch()
+ if fam.single_file_is_pipeline:
+ # A v-pred SDXL repo can ship an epsilon scheduler config beside the root
+ # single file that declares v_pred / ztsnr (NoobAI-XL V-Pred 1.0).
+ # A staged snapshot omits that root file, so its header is read off the Hub.
+ apply_pipeline_prediction_type(
+ pipe,
+ _base_local_dir or fetch_base,
+ logger,
+ repo_id = fetch_base,
+ # Not hf_token: that collapsed False (anonymous) to None (ambient).
+ hf_token = hub_token,
+ allow_network = not local_files_only,
+ )
if small_host is not None and small_host.engaged:
plan = self._apply_small_host_route(
pipe,
diff --git a/studio/backend/core/inference/diffusion_families.py b/studio/backend/core/inference/diffusion_families.py
index 15c72e9a47c..88f14a445c6 100644
--- a/studio/backend/core/inference/diffusion_families.py
+++ b/studio/backend/core/inference/diffusion_families.py
@@ -1531,11 +1531,34 @@ def detect_family_for_pick(
fam = detect_family(repo_id, override)
if fam is None and gguf_filename and not override:
fam = detect_family(f"{repo_id}/{gguf_filename}", override)
+ if fam is None and not override:
+ fam = _family_from_hub_evidence(repo_id, gguf_filename)
if not override:
fam = _family_from_content(fam, repo_id, gguf_filename)
return fam
+def _family_from_hub_evidence(
+ repo_id: str, gguf_filename: Optional[str]
+) -> Optional[DiffusionFamily]:
+ """A Hub single-file pick named for no family (``Laxhar/noobai-XL-Vpred-1.0``): the family its
+ checkpoint header declares. Never overrides a name that said no or an excluded repo, and never
+ answers for a repo or file named for a variant (``edit`` / ``inpaint`` / ...): the header cannot
+ tell an SDXL inpaint checkpoint from a plain one, and the base pipeline would load it. No network."""
+ needles = [repo_id] + ([f"{repo_id}/{gguf_filename}"] if gguf_filename else [])
+ if any(_best_family_match(model_identity(n).lower()) is not None for n in needles):
+ return None
+ segments = [seg for n in needles for seg in re.split(r"[/\\]+", n.lower()) if seg]
+ if any(_token_in_needle(kw, seg) for kw in _EDIT_KEYWORDS for seg in segments):
+ return None
+ if excluded_model_reason(repo_id):
+ return None
+ from .diffusion_hub_evidence import cached_family
+
+ name = cached_family(repo_id, gguf_filename)
+ return detect_family("", override = name) if name else None
+
+
def _family_from_content(
fam: Optional[DiffusionFamily], repo_id: str, gguf_filename: Optional[str]
) -> Optional[DiffusionFamily]:
diff --git a/studio/backend/core/inference/diffusion_hub_evidence.py b/studio/backend/core/inference/diffusion_hub_evidence.py
new file mode 100644
index 00000000000..a462cf2b1eb
--- /dev/null
+++ b/studio/backend/core/inference/diffusion_hub_evidence.py
@@ -0,0 +1,148 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Family evidence for a Hub single-file pick whose id and filename name no family.
+
+Fine-tunes ship under their own names (``Laxhar/noobai-XL-Vpred-1.0``,
+``OnomaAIResearch/Illustrious-xl-early-release-v0``), so the name matcher answers None and the pick
+is refused as an unknown family although the checkpoint states what it is: its safetensors header
+holds a whole SDXL checkpoint (UNet, both text encoders, VAE). Only that is read: the picked file's
+header, and only for whole-pipeline families (SDXL), whose checkpoint is self-contained; a
+transformer-only family resolves its base and defaults from the name. A pipeline pick needs no
+evidence here: a remote one loads only from trusted (named) repos, and a local folder is already
+read through its ``model_index.json``.
+
+Evidence is advisory. ``prefetch_for_pick`` (the routes, user-initiated requests only) re-reads the
+header off the Hub on every request; ``cached_family`` hands that verdict, or a cached copy's header,
+to the network-free ``detect_family_for_pick``. The load opens the downloaded file itself, and that
+stays the authoritative check.
+"""
+
+from __future__ import annotations
+
+import threading
+import time
+from pathlib import Path
+from typing import Any, Optional
+
+_SINGLE_FILE_SUFFIXES = (".safetensors", ".sft")
+
+# (repo id lowercased, filename) -> (monotonic time read, family name or None for a header that names no family).
+# Only bridges a request's prefetch to the validation and load that follow it; a refresh REPLACES an entry (never
+# clears it first, so an overlapping request keeps seeing the last good read), and an entry expires on its own.
+_HEADER_VERDICTS: dict[tuple[str, str], tuple[float, Optional[str]]] = {}
+_LOCK = threading.Lock()
+_MAX_VERDICTS = 1024
+_VERDICT_TTL_S = 600.0
+
+
+def is_hub_id(repo_id: Optional[str]) -> bool:
+ """True for a bare ``owner/name`` that is not a path on this machine."""
+ rid = (repo_id or "").strip()
+ if rid.count("/") != 1 or "\\" in rid or rid.startswith((".", "/", "~")):
+ return False
+ try:
+ return not Path(rid).expanduser().exists()
+ except (OSError, RuntimeError, ValueError):
+ return True
+
+
+def _is_single_file(filename: Optional[str]) -> bool:
+ return bool(filename) and str(filename).lower().endswith(_SINGLE_FILE_SUFFIXES)
+
+
+def _key(repo_id: str, filename: str) -> tuple[str, str]:
+ return repo_id.strip().lower(), str(filename).strip()
+
+
+def _dit_family(info: Any) -> Optional[str]:
+ """The whole-pipeline family (SDXL) a header classifies as, or None. A denoiser-only file (a bare
+ UNet with no text encoders or VAE) is not evidence: SDXL loads its single file through the WHOLE
+ pipeline's ``from_single_file``."""
+ from .diffusion_content import ROLE_DIT
+ from .diffusion_families import detect_family
+
+ if info.role != ROLE_DIT or not info.whole_pipeline or not info.family:
+ return None
+ fam = detect_family("", override = info.family)
+ return info.family if fam is not None and fam.single_file_is_pipeline else None
+
+
+def _verdict(key: tuple[str, str]) -> tuple[bool, Optional[str]]:
+ """``(known, family)`` for a remembered header read that has not expired."""
+ with _LOCK:
+ hit = _HEADER_VERDICTS.get(key)
+ if hit is None:
+ return False, None
+ if time.monotonic() - hit[0] > _VERDICT_TTL_S:
+ del _HEADER_VERDICTS[key]
+ return False, None
+ return True, hit[1]
+
+
+def cached_family(repo_id: Optional[str], filename: Optional[str] = None) -> Optional[str]:
+ """The family a Hub single-file pick's header declares, from memory or disk only; None when
+ nothing is known. A header read off the Hub by this request's prefetch outranks a cached copy,
+ which may be an older revision of the file the load will fetch."""
+ if not is_hub_id(repo_id) or not _is_single_file(filename):
+ return None
+ repo_id = str(repo_id).strip()
+ known, family = _verdict(_key(repo_id, str(filename)))
+ if known:
+ return family
+ from .diffusion_compat import _local_gguf_path
+
+ local = _local_gguf_path(repo_id, str(filename))
+ if not local:
+ return None
+ from .diffusion_content import inspect_checkpoint
+
+ return _dit_family(inspect_checkpoint(local))
+
+
+def prefetch_for_pick(
+ repo_id: Optional[str],
+ filename: Optional[str],
+ family_override: Optional[str],
+ hf_token: Any,
+ allow_network: bool = True,
+) -> None:
+ """The route hook: read the picked single file's header off the Hub (a range read, no weights,
+ nothing cached) when neither the name, a local path nor the content answered. ``allow_network=
+ False`` (an automatic, cache-only load) does nothing. ``hf_token`` passes through as given apart
+ from trimming a string: ``False`` (a managed account with no token of its own) stays anonymous.
+ Fails open: offline, gated or odd keeps the previous verdict, if any."""
+ if (
+ not allow_network
+ or family_override
+ or not is_hub_id(repo_id)
+ or not _is_single_file(filename)
+ ):
+ return
+ repo_id = str(repo_id).strip()
+ filename = str(filename)
+ try:
+ from .diffusion_families import _family_from_hub_evidence, detect_family_for_pick
+
+ # Skip only when the name, a local path or the content answered; an answer from Hub evidence is re-read.
+ if (
+ detect_family_for_pick(repo_id, filename, None) is not None
+ and _family_from_hub_evidence(repo_id, filename) is None
+ ):
+ return
+ from huggingface_hub import HfApi
+
+ from .diffusion_content import classify_tensors
+
+ from hub.utils.hf_tokens import normalize_token
+
+ token = normalize_token(hf_token)
+ meta = HfApi().parse_safetensors_file_metadata(repo_id, filename, token = token)
+ shapes = {name: list(info.shape) for name, info in meta.tensors.items()}
+ family = _dit_family(classify_tensors(shapes, meta.metadata))
+ except Exception: # noqa: BLE001 - evidence is best effort; validation and the load still decide
+ return
+ with _LOCK:
+ if len(_HEADER_VERDICTS) >= _MAX_VERDICTS:
+ _HEADER_VERDICTS.clear()
+ _HEADER_VERDICTS[_key(repo_id, filename)] = (time.monotonic(), family)
diff --git a/studio/backend/core/inference/diffusion_sampler.py b/studio/backend/core/inference/diffusion_sampler.py
index fc4aeb1c253..6de4f1c7a88 100644
--- a/studio/backend/core/inference/diffusion_sampler.py
+++ b/studio/backend/core/inference/diffusion_sampler.py
@@ -196,12 +196,16 @@ def _safetensors_top_level_keys(path: str) -> frozenset[str]:
def checkpoint_prediction_overrides(path: Optional[str]) -> dict[str, Any]:
"""Scheduler overrides a single-file checkpoint asks for through its ``v_pred`` / ``ztsnr`` marker
tensors (ComfyUI's detection, comfy/supported_models.py SDXL.model_type). Empty when unmarked."""
- if not path or not str(path).endswith(".safetensors"):
+ if not path or not str(path).lower().endswith(".safetensors"):
return {}
try:
keys = _safetensors_top_level_keys(str(path))
except Exception: # noqa: BLE001 - an unreadable header keeps the shipped scheduler
return {}
+ return _marker_overrides(keys)
+
+
+def _marker_overrides(keys: Any) -> dict[str, Any]:
if "v_pred" not in keys:
return {}
overrides: dict[str, Any] = {"prediction_type": "v_prediction"}
@@ -211,13 +215,99 @@ def checkpoint_prediction_overrides(path: Optional[str]) -> dict[str, Any]:
return overrides
-def apply_checkpoint_prediction_type(
- pipe: Any,
- path: Optional[str],
- logger: Any = None,
+def _root_single_marks(
+ root: Optional[str],
+ repo_id: Optional[str] = None,
+ hf_token: Any = None,
+ allow_network: bool = False,
+) -> list[dict[str, Any]]:
+ """Marker overrides of each root ``.safetensors`` of a pipeline: off disk when the folder holds them,
+ else off the Hub headers of ``repo_id``. A staged Hub pipeline snapshot keeps only
+ ``model_index.json`` at its root (the packaged single file is never downloaded), so the folder
+ alone would miss the markers. Header range reads only; empty on anything unreadable."""
+ from pathlib import Path
+
+ try:
+ folder = Path(str(root)) if root else None
+ singles = (
+ sorted(
+ p for p in folder.iterdir() if p.suffix.lower() == ".safetensors" and p.is_file()
+ )
+ if folder is not None and folder.is_dir()
+ else []
+ )
+ except OSError:
+ singles = []
+ if singles:
+ return [checkpoint_prediction_overrides(str(p)) for p in singles]
+ rid = (repo_id or "").strip()
+ if not allow_network or rid.count("/") != 1:
+ return []
+ try:
+ if Path(rid).expanduser().exists():
+ return []
+ from huggingface_hub import HfApi
+
+ from .diffusion_families import pinned_revision_kwargs
+
+ from hub.utils.hf_tokens import normalize_token
+
+ # One normalisation for every Hub call here: False (anonymous) never becomes None (ambient).
+ hf_token = normalize_token(hf_token)
+ api = HfApi()
+ rev = pinned_revision_kwargs(rid)
+ names = sorted(
+ f
+ for f in api.list_repo_files(rid, token = hf_token, **rev)
+ if "/" not in f and f.lower().endswith(".safetensors")
+ )
+ return [
+ _marker_overrides(
+ {
+ k
+ for k in api.parse_safetensors_file_metadata(
+ rid, name, token = hf_token, **rev
+ ).tensors
+ if "." not in k
+ }
+ )
+ for name in names
+ ]
+ except Exception: # noqa: BLE001 - offline / gated / odd: the shipped scheduler config stands
+ return []
+
+
+def pipeline_prediction_overrides(
+ config: Any,
+ root: Optional[str],
+ repo_id: Optional[str] = None,
+ hf_token: Any = None,
+ allow_network: bool = False,
+) -> dict[str, Any]:
+ """Scheduler overrides a diffusers SDXL pipeline directory asks for. Empty when it asks for none.
+
+ Two sources. The repo's packaged root single file: NoobAI-XL V-Pred 1.0 ships an epsilon
+ ``scheduler_config.json`` beside a root ``.safetensors`` carrying ``v_pred`` / ``ztsnr``, which is
+ the file ComfyUI loads and samples as v-prediction with zero terminal SNR. Only when every root
+ single file agrees, so a repo shipping an eps and a v-pred variant side by side keeps its config.
+ And a config that declares v-prediction with ``rescale_betas_zero_snr`` but leading spacing,
+ which never samples the terminal timestep that zero-SNR training exists for (the marker path
+ sets ``trailing`` for the same reason)."""
+ overrides: dict[str, Any] = {}
+ marks = _root_single_marks(root, repo_id, hf_token, allow_network)
+ if marks and marks[0] and all(m == marks[0] for m in marks):
+ overrides.update(marks[0])
+ config = config or {}
+ prediction = overrides.get("prediction_type", config.get("prediction_type"))
+ zsnr = overrides.get("rescale_betas_zero_snr", config.get("rescale_betas_zero_snr"))
+ if prediction == "v_prediction" and zsnr and config.get("timestep_spacing") != "trailing":
+ overrides["timestep_spacing"] = "trailing"
+ return overrides
+
+
+def _apply_scheduler_overrides(
+ pipe: Any, overrides: dict[str, Any], logger: Any, source: str
) -> bool:
- """Rebuild ``pipe.scheduler`` for a v-prediction checkpoint. True when it changed; idempotent."""
- overrides = checkpoint_prediction_overrides(path)
scheduler = getattr(pipe, "scheduler", None)
config = getattr(scheduler, "config", None)
if not overrides or config is None:
@@ -228,5 +318,36 @@ def apply_checkpoint_prediction_type(
return False
pipe.scheduler = type(scheduler).from_config(config, **overrides)
if logger is not None:
- logger.info("scheduler set from checkpoint markers: %s", overrides)
+ logger.info("scheduler set from %s: %s", source, overrides)
return True
+
+
+def apply_checkpoint_prediction_type(
+ pipe: Any,
+ path: Optional[str],
+ logger: Any = None,
+) -> bool:
+ """Rebuild ``pipe.scheduler`` for a v-prediction checkpoint. True when it changed; idempotent."""
+ return _apply_scheduler_overrides(
+ pipe, checkpoint_prediction_overrides(path), logger, "checkpoint markers"
+ )
+
+
+def apply_pipeline_prediction_type(
+ pipe: Any,
+ root: Optional[str],
+ logger: Any = None,
+ *,
+ repo_id: Optional[str] = None,
+ hf_token: Any = None,
+ allow_network: bool = False,
+) -> bool:
+ """``apply_checkpoint_prediction_type`` for a diffusers pipeline directory
+ (``pipeline_prediction_overrides``). True when it changed; idempotent."""
+ config = getattr(getattr(pipe, "scheduler", None), "config", None)
+ return _apply_scheduler_overrides(
+ pipe,
+ pipeline_prediction_overrides(config, root, repo_id, hf_token, allow_network),
+ logger,
+ "pipeline v-prediction",
+ )
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index b3b33047671..8acafaa4091 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -51456,6 +51456,7 @@ async def diffusion_download_plan(
resolve_model_kind,
)
from core.inference.diffusion_engine_router import predict_engine
+ from core.inference.diffusion_hub_evidence import prefetch_for_pick
from core.inference.sd_cpp_engine import ENGINE_MLX, ENGINE_SD_CPP
from utils.native_path_leases import redact_native_paths
@@ -51476,6 +51477,15 @@ async def diffusion_download_plan(
if sole is not None:
request.gguf_filename = sole
kind = resolve_model_kind(sole)
+ # A Hub repo named for no family (an SDXL fine-tune) says what it is in its own model_index.json or checkpoint
+ # header; fetch that small evidence first so the network-free validation below can read it.
+ await asyncio.to_thread(
+ prefetch_for_pick,
+ request.model_path,
+ request.gguf_filename,
+ request.family_override,
+ request.hf_token,
+ )
fam = await asyncio.to_thread(
backend.validate_load_request,
request.model_path,
@@ -51757,6 +51767,7 @@ async def load_diffusion_model_gated(
selected_card_identity,
)
from core.inference.sd_cpp_engine import ENGINE_DIFFUSERS, ENGINE_MLX, ENGINE_SD_CPP
+ from core.inference.diffusion_hub_evidence import prefetch_for_pick
from utils.native_path_leases import redact_native_paths
backend = get_diffusion_backend()
@@ -51777,6 +51788,17 @@ async def load_diffusion_model_gated(
if sole is not None:
request.gguf_filename = sole
kind = resolve_model_kind(sole)
+ # A Hub repo named for no family (an SDXL fine-tune) says what it is in its own model_index.json or checkpoint
+ # header; fetch that small evidence first so the network-free validation below can read it.
+ await asyncio.to_thread(
+ prefetch_for_pick,
+ request.model_path,
+ request.gguf_filename,
+ request.family_override,
+ request.hf_token,
+ # An automatic (API auto-switch) load is cache-only, like the rest of this path.
+ user_initiated,
+ )
# Validate cheaply BEFORE touching the GPU: an unloadable pick must not evict a working chat model and then 400. The family also drives engine selection.
fam = await asyncio.to_thread(
backend.validate_load_request,
diff --git a/studio/backend/tests/test_diffusion_vpred_sdxl_detect.py b/studio/backend/tests/test_diffusion_vpred_sdxl_detect.py
new file mode 100644
index 00000000000..1ea9a0e7eaa
--- /dev/null
+++ b/studio/backend/tests/test_diffusion_vpred_sdxl_detect.py
@@ -0,0 +1,693 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""SDXL fine-tunes named for no family (NoobAI-XL V-Pred, Illustrious) are detected from their own
+model_index.json / checkpoint header, and load with the scheduler the checkpoint asks for.
+Config-only fixtures: safetensors headers with empty tensors, no weights."""
+
+from __future__ import annotations
+
+import json
+import struct
+import types
+from pathlib import Path
+
+import pytest
+
+import core.inference.diffusion_hub_evidence as evidence
+from core.inference.diffusion_families import detect_family_for_pick
+from core.inference.diffusion_sampler import (
+ checkpoint_prediction_overrides,
+ pipeline_prediction_overrides,
+)
+
+NOOB = "Laxhar/noobai-XL-Vpred-1.0"
+NOOB_FILE = "NoobAI-XL-Vpred-v1.0.safetensors"
+ILLU = "OnomaAIResearch/Illustrious-xl-early-release-v0"
+ILLU_FILE = "Illustrious-XL-v0.1.safetensors"
+SHA = "0123456789abcdef0123456789abcdef01234567"
+
+# Laxhar/noobai-XL-Vpred-1.0 model_index.json (trimmed) and scheduler_config.json: the repo's
+# diffusers scheduler says epsilon; only the root single file carries v_pred / ztsnr.
+SDXL_INDEX = {
+ "_class_name": "StableDiffusionXLPipeline",
+ "scheduler": ["diffusers", "EulerDiscreteScheduler"],
+ "unet": ["diffusers", "UNet2DConditionModel"],
+ "text_encoder_2": ["transformers", "CLIPTextModelWithProjection"],
+}
+NOOB_SCHEDULER = dict(
+ beta_end = 0.012,
+ beta_schedule = "scaled_linear",
+ beta_start = 0.00085,
+ num_train_timesteps = 1000,
+ prediction_type = "epsilon",
+ rescale_betas_zero_snr = False,
+ steps_offset = 1,
+ timestep_spacing = "leading",
+)
+# The ComfyUI / original-SD layout of an SDXL checkpoint (label_emb = SDXL's ADM conditioning).
+SDXL_UNET_KEYS = [
+ "model.diffusion_model.input_blocks.0.0.weight",
+ "model.diffusion_model.middle_block.0.in_layers.0.weight",
+ "model.diffusion_model.output_blocks.0.0.in_layers.0.weight",
+ "model.diffusion_model.label_emb.0.0.weight",
+ "model.diffusion_model.time_embed.0.weight",
+ "conditioner.embedders.1.model.transformer.resblocks.0.attn.in_proj_weight",
+ "first_stage_model.decoder.conv_in.weight",
+]
+SD15_UNET_KEYS = [
+ "model.diffusion_model.input_blocks.0.0.weight",
+ "model.diffusion_model.middle_block.0.in_layers.0.weight",
+ "model.diffusion_model.output_blocks.0.0.in_layers.0.weight",
+ "model.diffusion_model.time_embed.0.weight",
+]
+
+
+def _write_safetensors(path: Path, keys) -> Path:
+ header = {k: {"dtype": "F16", "shape": [0], "data_offsets": [0, 0]} for k in keys}
+ blob = json.dumps(header).encode()
+ path.parent.mkdir(parents = True, exist_ok = True)
+ path.write_bytes(struct.pack("<Q", len(blob)) + blob)
+ return path
+
+
+def hub_cache(tmp_path, monkeypatch):
+ """An empty HF hub cache root, pinned as the live one; ``put`` caches a repo file."""
+ root = tmp_path / "hub"
+ root.mkdir()
+ import utils.hf_cache_settings as settings
+
+ monkeypatch.setattr(settings, "active_hf_hub_cache", lambda: str(root))
+ # The import-time root is the shared HF cache; these fake ids are never in it, but keep it out anyway.
+ import huggingface_hub
+
+ real = huggingface_hub.try_to_load_from_cache
+
+ def only_tmp(
+ repo_id,
+ filename,
+ cache_dir = None,
+ **kw,
+ ):
+ return real(repo_id, filename, cache_dir = str(root), **kw)
+
+ monkeypatch.setattr(huggingface_hub, "try_to_load_from_cache", only_tmp)
+ evidence._HEADER_VERDICTS.clear()
+
+ def put(
+ repo_id: str,
+ filename: str,
+ *,
+ payload = None,
+ keys = None,
+ ) -> Path:
+ repo_dir = root / ("models--" + repo_id.replace("/", "--"))
+ (repo_dir / "refs").mkdir(parents = True, exist_ok = True)
+ (repo_dir / "refs" / "main").write_text(SHA)
+ target = repo_dir / "snapshots" / SHA / filename
+ target.parent.mkdir(parents = True, exist_ok = True)
+ if keys is not None:
+ return _write_safetensors(target, keys)
+ target.write_text(json.dumps(payload))
+ return target
+
+ yield types.SimpleNamespace(root = root, put = put)
+ evidence._HEADER_VERDICTS.clear()
+
+
+def _name(fam):
+ return fam.name if fam is not None else None
+
+
+# ---- detection ---------------------------------------------------------------------------------
+
+
+def test_a_cached_index_alone_is_not_evidence(hub_cache):
+ # The picked file's header is the evidence; a repo's model_index.json does not vouch for anything.
+ hub_cache.put(NOOB, "model_index.json", payload = SDXL_INDEX)
+ assert detect_family_for_pick(NOOB) is None
+ assert detect_family_for_pick(NOOB, NOOB_FILE) is None
+
+
+def test_vpred_single_file_detected_from_cached_header(hub_cache):
+ path = hub_cache.put(NOOB, NOOB_FILE, keys = SDXL_UNET_KEYS + ["v_pred", "ztsnr"])
+ assert _name(detect_family_for_pick(NOOB, NOOB_FILE)) == "sdxl"
+ # ComfyUI supported_models.SDXL.model_type: v_pred -> V_PREDICTION, ztsnr -> zsnr sampling.
+ assert checkpoint_prediction_overrides(str(path)) == {
+ "prediction_type": "v_prediction",
+ "rescale_betas_zero_snr": True,
+ "timestep_spacing": "trailing",
+ }
+
+
+def test_eps_sdxl_single_file_detected_and_left_epsilon(hub_cache):
+ path = hub_cache.put(ILLU, ILLU_FILE, keys = SDXL_UNET_KEYS)
+ assert _name(detect_family_for_pick(ILLU, ILLU_FILE)) == "sdxl"
+ assert checkpoint_prediction_overrides(str(path)) == {}
+
+
+def test_checkpoint_header_outranks_the_repo_index(hub_cache):
+ hub_cache.put(NOOB, "model_index.json", payload = SDXL_INDEX)
+ hub_cache.put(NOOB, "sd15.safetensors", keys = SD15_UNET_KEYS)
+ assert detect_family_for_pick(NOOB, "sd15.safetensors") is None
+
+
+def test_index_does_not_vouch_for_a_file_in_a_subfolder(hub_cache):
+ hub_cache.put(NOOB, "model_index.json", payload = SDXL_INDEX)
+ assert detect_family_for_pick(NOOB, "extras/other.safetensors") is None
+
+
+def test_fetch_reads_header_then_detection_is_offline(hub_cache, monkeypatch):
+ import huggingface_hub
+
+ calls = []
+
+ def parse(
+ self,
+ repo_id,
+ filename,
+ token = None,
+ **kw,
+ ):
+ calls.append((repo_id, filename))
+ tensors = {k: types.SimpleNamespace(shape = [0]) for k in SDXL_UNET_KEYS + ["v_pred"]}
+ return types.SimpleNamespace(tensors = tensors, metadata = {})
+
+ monkeypatch.setattr(huggingface_hub.HfApi, "parse_safetensors_file_metadata", parse)
+ monkeypatch.setattr(
+ huggingface_hub,
+ "hf_hub_download",
+ lambda *a, **k: pytest.fail("the header answered; no index fetch"),
+ )
+ assert detect_family_for_pick(NOOB, NOOB_FILE) is None
+ evidence.prefetch_for_pick(NOOB, NOOB_FILE, None, None)
+ assert calls == [(NOOB, NOOB_FILE)]
+ assert _name(detect_family_for_pick(NOOB, NOOB_FILE)) == "sdxl"
+ # Validation after the prefetch answers from the remembered header, with no further read.
+ assert _name(detect_family_for_pick(NOOB, NOOB_FILE)) == "sdxl"
+ assert calls == [(NOOB, NOOB_FILE)]
+
+
+def test_a_pipeline_pick_makes_no_hub_call(hub_cache, monkeypatch):
+ import huggingface_hub
+
+ def boom(*a, **k):
+ raise AssertionError("a pipeline pick reads no evidence")
+
+ monkeypatch.setattr(huggingface_hub, "hf_hub_download", boom)
+ monkeypatch.setattr(huggingface_hub.HfApi, "parse_safetensors_file_metadata", boom)
+ evidence.prefetch_for_pick(NOOB, None, None, "hf_x")
+ assert detect_family_for_pick(NOOB) is None
+
+
+def test_prefetch_is_skipped_for_named_overridden_local_and_gguf_picks(
+ hub_cache, monkeypatch, tmp_path
+):
+ import huggingface_hub
+
+ def boom(*a, **k):
+ raise AssertionError("no network for this pick")
+
+ monkeypatch.setattr(huggingface_hub, "hf_hub_download", boom)
+ monkeypatch.setattr(huggingface_hub.HfApi, "parse_safetensors_file_metadata", boom)
+ evidence.prefetch_for_pick("stabilityai/stable-diffusion-xl-base-1.0", None, None, None)
+ evidence.prefetch_for_pick(NOOB, NOOB_FILE, "sdxl", None)
+ evidence.prefetch_for_pick(NOOB, "noob-Q4_K.gguf", None, None)
+ evidence.prefetch_for_pick(str(tmp_path), None, None, None)
+
+
+def test_name_veto_is_not_overridden_by_the_header(hub_cache):
+ # The name matched SDXL and then refused the edit variant; a cached SDXL header must not undo that.
+ repo = "someone/neutral"
+ hub_cache.put(repo, "sdxl-edit.safetensors", keys = SDXL_UNET_KEYS)
+ assert detect_family_for_pick(repo, "sdxl-edit.safetensors") is None
+
+
+def test_named_sdxl_repos_never_read_evidence(monkeypatch):
+ monkeypatch.setattr(
+ evidence, "cached_family", lambda *a, **k: pytest.fail("name already answered")
+ )
+ assert _name(detect_family_for_pick("stabilityai/stable-diffusion-xl-base-1.0")) == "sdxl"
+ assert _name(detect_family_for_pick("stabilityai/sdxl-turbo")) == "sdxl"
+
+
+# ---- scheduler for a diffusers pipeline directory ----------------------------------------------
+
+
+def _pipeline_dir(tmp_path: Path, scheduler: dict, root_singles: dict) -> Path:
+ root = tmp_path / "pipe"
+ (root / "scheduler").mkdir(parents = True)
+ (root / "model_index.json").write_text(json.dumps(SDXL_INDEX))
+ (root / "scheduler" / "scheduler_config.json").write_text(json.dumps(scheduler))
+ for name, keys in root_singles.items():
+ _write_safetensors(root / name, keys)
+ return root
+
+
+def test_vpred_folder_with_eps_config_takes_the_root_file_markers(tmp_path):
+ root = _pipeline_dir(
+ tmp_path, NOOB_SCHEDULER, {NOOB_FILE: SDXL_UNET_KEYS + ["v_pred", "ztsnr"]}
+ )
+ assert pipeline_prediction_overrides(NOOB_SCHEDULER, str(root)) == {
+ "prediction_type": "v_prediction",
+ "rescale_betas_zero_snr": True,
+ "timestep_spacing": "trailing",
+ }
+
+
+def test_eps_sdxl_folder_is_unchanged(tmp_path):
+ root = _pipeline_dir(tmp_path, NOOB_SCHEDULER, {ILLU_FILE: SDXL_UNET_KEYS})
+ assert pipeline_prediction_overrides(NOOB_SCHEDULER, str(root)) == {}
+ assert pipeline_prediction_overrides(NOOB_SCHEDULER, None) == {}
+ assert pipeline_prediction_overrides(NOOB_SCHEDULER, "stabilityai/sdxl-turbo") == {}
+
+
+def test_disagreeing_root_files_keep_the_config(tmp_path):
+ root = _pipeline_dir(
+ tmp_path,
+ NOOB_SCHEDULER,
+ {"eps.safetensors": SDXL_UNET_KEYS, "vpred.safetensors": SDXL_UNET_KEYS + ["v_pred"]},
+ )
+ assert pipeline_prediction_overrides(NOOB_SCHEDULER, str(root)) == {}
+
+
+def test_declared_vpred_zsnr_config_samples_the_terminal_timestep():
+ config = {**NOOB_SCHEDULER, "prediction_type": "v_prediction", "rescale_betas_zero_snr": True}
+ assert pipeline_prediction_overrides(config, None) == {"timestep_spacing": "trailing"}
+ assert pipeline_prediction_overrides({**config, "timestep_spacing": "trailing"}, None) == {}
+ # v-prediction without zero SNR is honoured as declared.
+ assert (
+ pipeline_prediction_overrides({**NOOB_SCHEDULER, "prediction_type": "v_prediction"}, None)
+ == {}
+ )
+
+
+def test_apply_rebuilds_a_real_scheduler(tmp_path):
+ diffusers = pytest.importorskip("diffusers")
+ from core.inference.diffusion_sampler import apply_pipeline_prediction_type
+
+ root = _pipeline_dir(
+ tmp_path, NOOB_SCHEDULER, {NOOB_FILE: SDXL_UNET_KEYS + ["v_pred", "ztsnr"]}
+ )
+ pipe = types.SimpleNamespace(
+ scheduler = diffusers.EulerDiscreteScheduler.from_config(NOOB_SCHEDULER)
+ )
+ assert apply_pipeline_prediction_type(pipe, str(root)) is True
+ cfg = pipe.scheduler.config
+ assert (cfg.prediction_type, cfg.rescale_betas_zero_snr, cfg.timestep_spacing) == (
+ "v_prediction",
+ True,
+ "trailing",
+ )
+ pipe.scheduler.set_timesteps(25)
+ assert int(pipe.scheduler.timesteps[0]) == 999 # starts at the zero-SNR terminal step
+ assert apply_pipeline_prediction_type(pipe, str(root)) is False # idempotent
+
+ eps_root = _pipeline_dir(tmp_path / "eps", NOOB_SCHEDULER, {ILLU_FILE: SDXL_UNET_KEYS})
+ eps = types.SimpleNamespace(
+ scheduler = diffusers.EulerDiscreteScheduler.from_config(NOOB_SCHEDULER)
+ )
+ assert apply_pipeline_prediction_type(eps, str(eps_root)) is False
+ assert eps.scheduler.config.prediction_type == "epsilon"
+
+
+SDXL_DIFFUSERS_UNET_ONLY_KEYS = [
+ "conv_in.weight",
+ "time_embedding.linear_1.weight",
+ "add_embedding.linear_1.weight",
+ "down_blocks.0.resnets.0.conv1.weight",
+ "mid_block.resnets.0.conv1.weight",
+ "up_blocks.0.resnets.0.conv1.weight",
+]
+
+
+def _stub_header(monkeypatch, keys_by_call):
+ import huggingface_hub
+
+ calls = []
+
+ def parse(
+ self,
+ repo_id,
+ filename,
+ token = None,
+ **kw,
+ ):
+ keys = keys_by_call[min(len(calls), len(keys_by_call) - 1)]
+ calls.append((repo_id, filename))
+ return types.SimpleNamespace(
+ tensors = {k: types.SimpleNamespace(shape = [0]) for k in keys}, metadata = {}
+ )
+
+ monkeypatch.setattr(huggingface_hub.HfApi, "parse_safetensors_file_metadata", parse)
+ return calls
+
+
+def test_a_denoiser_only_unet_is_not_evidence_for_a_whole_pipeline_family(hub_cache, monkeypatch):
+ import huggingface_hub
+
+ _stub_header(monkeypatch, [SDXL_DIFFUSERS_UNET_ONLY_KEYS])
+ monkeypatch.setattr(
+ huggingface_hub, "hf_hub_download", lambda *a, **k: pytest.fail("header read")
+ )
+ evidence.prefetch_for_pick(NOOB, "unet.safetensors", None, None)
+ assert detect_family_for_pick(NOOB, "unet.safetensors") is None
+ hub_cache.put(ILLU, "unet.safetensors", keys = SDXL_DIFFUSERS_UNET_ONLY_KEYS)
+ assert detect_family_for_pick(ILLU, "unet.safetensors") is None
+
+
+def test_each_prefetch_rereads_a_replaced_header(hub_cache, monkeypatch):
+ calls = _stub_header(monkeypatch, [SD15_UNET_KEYS, SDXL_UNET_KEYS + ["v_pred"]])
+ evidence.prefetch_for_pick(NOOB, NOOB_FILE, None, None)
+ assert detect_family_for_pick(NOOB, NOOB_FILE) is None
+ # The repo replaced the file with an SDXL checkpoint: the next request judges the new header.
+ evidence.prefetch_for_pick(NOOB, NOOB_FILE, None, None)
+ assert len(calls) == 2
+ assert _name(detect_family_for_pick(NOOB, NOOB_FILE)) == "sdxl"
+
+
+def test_a_remembered_header_expires(hub_cache, monkeypatch):
+ _stub_header(monkeypatch, [SDXL_UNET_KEYS])
+ evidence.prefetch_for_pick(NOOB, NOOB_FILE, None, None)
+ assert _name(detect_family_for_pick(NOOB, NOOB_FILE)) == "sdxl"
+ now = evidence.time.monotonic()
+ monkeypatch.setattr(evidence.time, "monotonic", lambda: now + evidence._VERDICT_TTL_S + 1)
+ assert detect_family_for_pick(NOOB, NOOB_FILE) is None
+
+
+def test_the_prefetch_writes_nothing_to_the_shared_cache(hub_cache, monkeypatch):
+ import huggingface_hub
+
+ monkeypatch.setattr(
+ huggingface_hub, "hf_hub_download", lambda *a, **k: pytest.fail("header range read only")
+ )
+ _stub_header(monkeypatch, [SDXL_UNET_KEYS])
+ evidence.prefetch_for_pick(NOOB, NOOB_FILE, None, "hf_private")
+ assert _name(detect_family_for_pick(NOOB, NOOB_FILE)) == "sdxl"
+ assert list(hub_cache.root.iterdir()) == []
+
+
+def test_a_cache_only_prefetch_never_touches_the_hub(hub_cache, monkeypatch):
+ import huggingface_hub
+
+ def boom(*a, **k):
+ raise AssertionError("an automatic load is cache-only")
+
+ monkeypatch.setattr(huggingface_hub, "hf_hub_download", boom)
+ monkeypatch.setattr(huggingface_hub.HfApi, "parse_safetensors_file_metadata", boom)
+ evidence.prefetch_for_pick(NOOB, NOOB_FILE, None, None, allow_network = False)
+ evidence.prefetch_for_pick(NOOB, None, None, None, allow_network = False)
+ assert detect_family_for_pick(NOOB, NOOB_FILE) is None
+
+
+def test_the_load_route_passes_user_initiated_to_the_prefetch():
+ import ast
+
+ source = (Path(__file__).resolve().parents[1] / "routes" / "inference.py").read_text()
+ tree = ast.parse(source)
+ route = next(
+ n
+ for n in ast.walk(tree)
+ if isinstance(n, ast.AsyncFunctionDef) and n.name == "load_diffusion_model_gated"
+ )
+ calls = [
+ c
+ for c in ast.walk(route)
+ if isinstance(c, ast.Call)
+ and any(isinstance(a, ast.Name) and a.id == "prefetch_for_pick" for a in c.args)
+ ]
+ assert len(calls) == 1
+ passed = [a.id for a in calls[0].args if isinstance(a, ast.Name)] + [
+ k.value.id for k in calls[0].keywords if isinstance(k.value, ast.Name)
+ ]
+ assert "user_initiated" in passed
+
+
+FLUX1_DEV_KEYS = [
+ "transformer_blocks.0.attn.to_q.weight",
+ "single_transformer_blocks.0.attn.to_q.weight",
+ "x_embedder.weight",
+ "context_embedder.weight",
+ "time_text_embed.timestep_embedder.linear_1.weight",
+ "time_text_embed.guidance_embedder.linear_1.weight",
+]
+
+
+def test_a_flux_dev_header_is_not_evidence_for_an_unnamed_pick(hub_cache, monkeypatch):
+ # FLUX.1 dev and schnell share one layout; resolved by family alone it would get schnell's base and defaults.
+ _stub_header(monkeypatch, [FLUX1_DEV_KEYS])
+ evidence.prefetch_for_pick("someone/neutral-ft", "model.safetensors", None, None)
+ assert detect_family_for_pick("someone/neutral-ft", "model.safetensors") is None
+ hub_cache.put("someone/cached-ft", "model.safetensors", keys = FLUX1_DEV_KEYS)
+ assert detect_family_for_pick("someone/cached-ft", "model.safetensors") is None
+
+
+def test_a_transformer_family_index_is_not_evidence_for_an_unnamed_pick(hub_cache):
+ hub_cache.put(
+ "someone/neutral-pipe", "model_index.json", payload = {"_class_name": "FluxPipeline"}
+ )
+ assert detect_family_for_pick("someone/neutral-pipe") is None
+
+
+def test_the_anonymous_token_sentinel_reaches_every_hub_call(hub_cache, monkeypatch):
+ import huggingface_hub
+
+ seen = []
+
+ def parse(
+ self,
+ repo_id,
+ filename,
+ token = None,
+ **kw,
+ ):
+ seen.append(("header", token))
+ raise OSError("not a safetensors")
+
+ def size(
+ url,
+ token = None,
+ **kw,
+ ):
+ seen.append(("size", token))
+ return types.SimpleNamespace(size = 400)
+
+ def download(
+ repo_id,
+ filename,
+ token = None,
+ cache_dir = None,
+ **kw,
+ ):
+ seen.append(("download", token))
+ return str(hub_cache.put(repo_id, filename, payload = SDXL_INDEX))
+
+ monkeypatch.setattr(huggingface_hub.HfApi, "parse_safetensors_file_metadata", parse)
+ monkeypatch.setattr(huggingface_hub, "get_hf_file_metadata", size)
+ monkeypatch.setattr(huggingface_hub, "hf_hub_download", download)
+ evidence.prefetch_for_pick(NOOB, NOOB_FILE, None, False)
+ assert seen == [("header", False)]
+
+
+def test_a_positive_verdict_is_reread_when_the_hub_file_changes(hub_cache, monkeypatch):
+ calls = _stub_header(monkeypatch, [SDXL_UNET_KEYS, SD15_UNET_KEYS])
+ evidence.prefetch_for_pick(NOOB, NOOB_FILE, None, None)
+ assert _name(detect_family_for_pick(NOOB, NOOB_FILE)) == "sdxl"
+ # The repo replaced the SDXL checkpoint with an SD 1.5 one: the next request must see it.
+ evidence.prefetch_for_pick(NOOB, NOOB_FILE, None, None)
+ assert len(calls) == 2
+ assert detect_family_for_pick(NOOB, NOOB_FILE) is None
+
+
+def test_a_cached_copy_is_rechecked_against_the_current_hub_header(hub_cache, monkeypatch):
+ hub_cache.put(NOOB, NOOB_FILE, keys = SDXL_UNET_KEYS)
+ assert _name(detect_family_for_pick(NOOB, NOOB_FILE)) == "sdxl"
+ calls = _stub_header(monkeypatch, [SD15_UNET_KEYS])
+ evidence.prefetch_for_pick(NOOB, NOOB_FILE, None, None)
+ assert len(calls) == 1
+ assert detect_family_for_pick(NOOB, NOOB_FILE) is None
+
+
+def test_a_named_pick_never_reads_hub_evidence(hub_cache, monkeypatch):
+ calls = _stub_header(monkeypatch, [SDXL_UNET_KEYS])
+ evidence.prefetch_for_pick("someone/my-sdxl-ft", "model.safetensors", None, None)
+ assert calls == []
+
+
+def test_root_single_file_extensions_match_case_insensitively(tmp_path):
+ root = _pipeline_dir(
+ tmp_path,
+ NOOB_SCHEDULER,
+ {"NOOBAI-XL-VPRED.SAFETENSORS": SDXL_UNET_KEYS + ["v_pred", "ztsnr"]},
+ )
+ assert pipeline_prediction_overrides(NOOB_SCHEDULER, str(root)) == {
+ "prediction_type": "v_prediction",
+ "rescale_betas_zero_snr": True,
+ "timestep_spacing": "trailing",
+ }
+
+
+def test_a_staged_pipeline_snapshot_reads_the_root_markers_off_the_hub(tmp_path, monkeypatch):
+ import huggingface_hub
+
+ # The staged snapshot keeps only model_index.json at its root; the packaged single file stays on the Hub.
+ root = _pipeline_dir(tmp_path, NOOB_SCHEDULER, {})
+ seen = []
+
+ def files(
+ self,
+ repo_id,
+ token = None,
+ **kw,
+ ):
+ seen.append(("list", repo_id, token))
+ return ["model_index.json", NOOB_FILE, "unet/diffusion_pytorch_model.safetensors"]
+
+ def parse(
+ self,
+ repo_id,
+ filename,
+ token = None,
+ **kw,
+ ):
+ seen.append(("header", filename, token))
+ keys = SDXL_UNET_KEYS + ["v_pred", "ztsnr"]
+ return types.SimpleNamespace(tensors = {k: None for k in keys}, metadata = {})
+
+ monkeypatch.setattr(huggingface_hub.HfApi, "list_repo_files", files)
+ monkeypatch.setattr(huggingface_hub.HfApi, "parse_safetensors_file_metadata", parse)
+ want = {
+ "prediction_type": "v_prediction",
+ "rescale_betas_zero_snr": True,
+ "timestep_spacing": "trailing",
+ }
+ assert pipeline_prediction_overrides(NOOB_SCHEDULER, str(root), NOOB, "hf_x", True) == want
+ assert seen == [("list", NOOB, "hf_x"), ("header", NOOB_FILE, "hf_x")]
+ # A cache-only load never reaches the Hub; the shipped config stands.
+ seen.clear()
+ assert pipeline_prediction_overrides(NOOB_SCHEDULER, str(root), NOOB, None, False) == {}
+ assert seen == []
+
+
+def test_the_load_passes_the_repo_to_the_pipeline_marker_check():
+ import ast
+
+ source = (
+ Path(__file__).resolve().parents[1] / "core" / "inference" / "diffusion.py"
+ ).read_text()
+ calls = [
+ c
+ for c in ast.walk(ast.parse(source))
+ if isinstance(c, ast.Call)
+ and getattr(c.func, "id", None) == "apply_pipeline_prediction_type"
+ ]
+ assert len(calls) == 1
+ assert {k.arg for k in calls[0].keywords} >= {"repo_id", "hf_token", "allow_network"}
+
+
+ "repo, filename",
+ [
+ ("someone/neutral-inpaint", "model.safetensors"),
+ ("someone/neutral-ft", "model-inpainting-edit.safetensors"),
+ ("someone/neutral-ft", "inpaint/model.safetensors"),
+ ],
+)
+def test_a_variant_named_pick_is_not_answered_from_its_header(hub_cache, repo, filename):
+ hub_cache.put(repo, filename, keys = SDXL_UNET_KEYS)
+ assert detect_family_for_pick(repo, filename) is None
+
+
+def test_a_refresh_keeps_the_last_verdict_visible(hub_cache, monkeypatch):
+ import huggingface_hub
+
+ _stub_header(monkeypatch, [SDXL_UNET_KEYS])
+ evidence.prefetch_for_pick(NOOB, NOOB_FILE, None, None)
+ during = []
+
+ def parse(
+ self,
+ repo_id,
+ filename,
+ token = None,
+ **kw,
+ ):
+ # An overlapping request validating while this refresh reads the header.
+ during.append(_name(detect_family_for_pick(NOOB, NOOB_FILE)))
+ raise OSError("offline")
+
+ monkeypatch.setattr(huggingface_hub.HfApi, "parse_safetensors_file_metadata", parse)
+ evidence.prefetch_for_pick(NOOB, NOOB_FILE, None, None)
+ assert during == ["sdxl"]
+ # A failed refresh keeps the last good read rather than forgetting it.
+ assert _name(detect_family_for_pick(NOOB, NOOB_FILE)) == "sdxl"
+
+
[email protected]("given, sent", [(False, False), (" hf_x ", "hf_x"), ("", None)])
+def test_the_pipeline_marker_probe_keeps_the_anonymous_sentinel(tmp_path, monkeypatch, given, sent):
+ import huggingface_hub
+
+ root = _pipeline_dir(tmp_path, NOOB_SCHEDULER, {})
+ seen = []
+
+ def files(
+ self,
+ repo_id,
+ token = None,
+ **kw,
+ ):
+ seen.append(token)
+ return [NOOB_FILE]
+
+ def parse(
+ self,
+ repo_id,
+ filename,
+ token = None,
+ **kw,
+ ):
+ seen.append(token)
+ return types.SimpleNamespace(tensors = {"v_pred": None}, metadata = {})
+
+ monkeypatch.setattr(huggingface_hub.HfApi, "list_repo_files", files)
+ monkeypatch.setattr(huggingface_hub.HfApi, "parse_safetensors_file_metadata", parse)
+ pipeline_prediction_overrides(NOOB_SCHEDULER, str(root), NOOB, given, True)
+ assert seen == [sent, sent]
+
+
+def test_the_load_hands_the_uncollapsed_token_to_the_marker_probe():
+ import ast
+
+ source = (
+ Path(__file__).resolve().parents[1] / "core" / "inference" / "diffusion.py"
+ ).read_text()
+ tree = ast.parse(source)
+ methods = {
+ n.name: n
+ for n in ast.walk(tree)
+ if isinstance(n, ast.FunctionDef) and n.name in ("begin_load", "load_pipeline")
+ }
+ # begin_load forwards normalize_token(hf_token) to the worker before collapsing hf_token.
+ begin = methods["begin_load"]
+ assigns = [
+ a
+ for a in ast.walk(begin)
+ if isinstance(a, ast.Assign)
+ and any(isinstance(t, ast.Name) and t.id == "hub_token" for t in a.targets)
+ ]
+ assert assigns and isinstance(assigns[0].value, ast.Call)
+ assert getattr(assigns[0].value.func, "id", None) == "normalize_token"
+ assert any(isinstance(k, ast.keyword) and k.arg == "_hub_token" for k in ast.walk(begin))
+ # load_pipeline accepts it and the marker probe receives it, never the collapsed hf_token.
+ load = methods["load_pipeline"]
+ assert "_hub_token" in {a.arg for a in load.args.kwonlyargs}
+ probe = [
+ c
+ for c in ast.walk(load)
+ if isinstance(c, ast.Call)
+ and getattr(c.func, "id", None) == "apply_pipeline_prediction_type"
+ ]
+ assert len(probe) == 1
+ token_kw = next(k for k in probe[0].keywords if k.arg == "hf_token")
+ assert isinstance(token_kw.value, ast.Name) and token_kw.value.id == "hub_token"