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: stop with a clear error when a pre-cast text encoder fails and its dense shards were skipped (#13117)

Dantea30a77d97aec
diff --git a/studio/backend/core/inference/diffusion.py b/studio/backend/core/inference/diffusion.py
index ca92fb61608..d0f6866f3d2 100644
--- a/studio/backend/core/inference/diffusion.py
+++ b/studio/backend/core/inference/diffusion.py
@@ -7000,6 +7000,13 @@ class DiffusionBackend:
hf_token = hf_token,
logger = logger,
local_files_only = local_files_only,
+ # krea assembles from fetch_base below, never the staged dir
+ dense_source = (
+ None
+ if fam.name == KREA2_FAMILY_NAME
+ else _base_local_dir
+ )
+ or (fetch_base if local_files_only else None),
)
)
if pipeline_seed_scheme is not None:
@@ -7287,6 +7294,7 @@ class DiffusionBackend:
hf_token = hf_token,
logger = logger,
local_files_only = local_files_only,
+ dense_source = fetch_base if local_files_only else None,
).get("text_encoder"),
)
else:
@@ -7328,6 +7336,8 @@ class DiffusionBackend:
hf_token = hf_token,
logger = logger,
local_files_only = local_files_only,
+ dense_source = _base_local_dir
+ or (fetch_base if local_files_only else None),
)
)
self._raise_if_load_cancelled(_load_token)
@@ -8670,6 +8680,7 @@ class DiffusionBackend:
hf_token = hf_token,
logger = logger,
local_files_only = local_files_only,
+ dense_source = base_local_dir or (base if local_files_only else None),
).get("text_encoder")
check_cancelled()
pipe = load_krea2_pipeline(
@@ -8717,6 +8728,7 @@ class DiffusionBackend:
hf_token = hf_token,
logger = logger,
local_files_only = local_files_only,
+ dense_source = base_local_dir or (base if local_files_only else None),
)
)
check_cancelled()
diff --git a/studio/backend/core/inference/diffusion_te_prequant.py b/studio/backend/core/inference/diffusion_te_prequant.py
index 3d3b7aedc78..d3522455839 100644
--- a/studio/backend/core/inference/diffusion_te_prequant.py
+++ b/studio/backend/core/inference/diffusion_te_prequant.py
@@ -507,6 +507,7 @@ def load_prequant_text_encoder(
config_overrides: Optional[dict] = None,
local_files_only: bool = False,
trim_lm_head: bool = False,
+ failures: Optional[list] = None,
) -> Optional[Any]:
"""Load the pre-cast text encoder described by ``source`` (on CPU, for pipeline
assembly to place), with the layerwise upcast hooks already installed.
@@ -521,7 +522,7 @@ def load_prequant_text_encoder(
the pipeline's assembly normally passes to ``from_pretrained`` (forward-behaviour
flags only; the state dict is unaffected by them).
``trim_lm_head`` builds the encoder without its untied ``lm_head`` and never reads that tensor
- (``diffusion_text_encoder_trim``)."""
+ (``diffusion_text_encoder_trim``). ``failures`` collects the error text of a load that raised."""
try:
if source.kind == "path" and not _local_prequant_path_allowed(source.location):
_warn(
@@ -682,6 +683,9 @@ def load_prequant_text_encoder(
return encoder
except Exception as exc: # noqa: BLE001 - fall back to the dense download + cast
_warn(logger, f"{scheme}:{component}:{source.kind}", exc)
+ if failures is not None:
+ # text only: the exception's traceback would pin this frame's checkpoint through the fp8 retry
+ failures.append(str(exc))
return None
@@ -733,6 +737,7 @@ def te_prequant_pipe_kwargs(
hf_token: Optional[str] = None,
logger: Any = None,
local_files_only: bool = False,
+ dense_source: Optional[str] = None,
) -> dict[str, Any]:
supplied = supplied_component_pipe_kwargs(
base,
@@ -752,6 +757,7 @@ def te_prequant_pipe_kwargs(
logger = logger,
local_files_only = local_files_only,
skip_components = tuple(supplied),
+ dense_source = dense_source,
)
injected.update(supplied)
return injected
@@ -768,6 +774,7 @@ def _te_prequant_pipe_kwargs(
logger: Any = None,
local_files_only: bool = False,
skip_components: tuple = (),
+ dense_source: Optional[str] = None,
) -> dict[str, Any]:
"""Component overrides for pipeline assembly: ``{<component>: <pre-cast encoder>}``
for every ``TE_PREQUANT_COMPONENTS`` attr the family hosts a pre-cast checkpoint for
@@ -777,10 +784,18 @@ def _te_prequant_pipe_kwargs(
Gated exactly like the runtime cast (mode normalized, device-supported, family not
denied), so injection can never engage where ``quantize_text_encoders`` would not.
The later ``quantize_text_encoders`` call re-applies the cast idempotently and keeps
- status reporting truthful."""
+ status reporting truthful.
+
+ ``dense_source`` is the local directory or repo id assembly reads without fetching (None when it can
+ still fetch). A pre-cast encoder that fails to load with no dense weights there raises: the plan left
+ those shards out of the prefetch, so assembly would die on a missing shard instead."""
+ unavailable: list[str] = []
+ failures: list = []
+ cause = ""
try:
from .diffusion_precision import TE_QUANT_FP8, normalize_te_quant, te_quant_supported
from .diffusion_text_encoder_trim import family_trims_lm_head
+ from .media_locality import _hosted_component_cached
sources = te_prequant_sources_for_base(
fam,
@@ -794,6 +809,7 @@ def _te_prequant_pipe_kwargs(
for component, source in sources.items():
if component in skip_components:
continue
+ failures.clear()
trim = component == "text_encoder" and family_trims_lm_head(getattr(fam, "name", None))
encoder = load_prequant_text_encoder(
base,
@@ -805,6 +821,7 @@ def _te_prequant_pipe_kwargs(
logger = logger,
local_files_only = local_files_only,
trim_lm_head = trim,
+ failures = failures,
)
fp8_names = tuple(
n for n in te_candidate_filenames(source) if n != getattr(source, "filename", None)
@@ -833,13 +850,24 @@ def _te_prequant_pipe_kwargs(
logger = logger,
local_files_only = local_files_only,
trim_lm_head = trim,
+ failures = failures,
)
if encoder is not None:
injected[component] = encoder
- return injected
+ elif dense_source and not _hosted_component_cached(dense_source, component, "", ""):
+ unavailable.append(component)
+ cause = f" ({failures[-1]})" if failures else cause
+ if not unavailable:
+ return injected
except Exception as exc: # noqa: BLE001 - injection is an optimisation, never a blocker
_warn(logger, "pipe_kwargs", exc)
return {}
+ raise RuntimeError(
+ f"The pre-quantized {', '.join(unavailable)} for {base} could not be loaded{cause}. Its dense "
+ "weights were not downloaded because the pre-quantized copy replaces them. If memory ran out "
+ "(os error 1455 means the Windows page file is too small), close other apps or enlarge the page "
+ "file and load again, or set Text encoder precision to Dense (bf16)."
+ )
def _held_locally(repo_id: str, name: Optional[str], hf_token: Optional[str]) -> bool:
diff --git a/studio/backend/tests/test_diffusion_te_prequant.py b/studio/backend/tests/test_diffusion_te_prequant.py
index f01a221e413..bc7b1d5d215 100644
--- a/studio/backend/tests/test_diffusion_te_prequant.py
+++ b/studio/backend/tests/test_diffusion_te_prequant.py
@@ -399,6 +399,51 @@ def test_pipe_kwargs_empty_when_load_fails(monkeypatch):
)
+def test_pipe_kwargs_raises_when_load_fails_and_dense_shards_were_skipped(monkeypatch, tmp_path):
+ """#12860: the plan skips the dense shards a pre-cast encoder replaces, so a failed pre-cast load
+ (os error 1455 on Windows) must stop with a clear error, not fall back to shards that are not there."""
+ pytest.importorskip("torch")
+ import json
+
+ import core.inference.diffusion_precision as precision
+ import core.inference.prequant_safetensors as prequant_safetensors
+
+ fam = _fam(te_prequant_repos = (("fp8", "text_encoder", "org/hosted"),), base_repo = str(tmp_path))
+ monkeypatch.setattr(precision, "te_quant_supported", lambda target, mode: True)
+ ckpt = tmp_path / "te.safetensors"
+ ckpt.write_bytes(b"")
+ monkeypatch.setattr(tpq, "_resolve_checkpoint_path", lambda *a, **k: str(ckpt))
+
+ def out_of_commit(*a, **k):
+ raise OSError(
+ "The paging file is too small for this operation to complete. (os error 1455)"
+ )
+
+ monkeypatch.setattr(prequant_safetensors, "load_plain_prequant_safetensors", out_of_commit)
+ encoder = tmp_path / "text_encoder"
+ encoder.mkdir()
+ (encoder / "config.json").write_text("{}")
+ shard = "model-00001-of-00001.safetensors"
+ (encoder / "model.safetensors.index.json").write_text(json.dumps({"weight_map": {"w": shard}}))
+
+ def load(dense_source):
+ return te_prequant_pipe_kwargs(
+ fam,
+ str(tmp_path),
+ te_quant_mode = "fp8",
+ target = _target(),
+ dtype = None,
+ dense_source = dense_source,
+ )
+
+ # assembly that can still fetch the shards keeps the dense fallback
+ assert load(None) == {}
+ with pytest.raises(RuntimeError, match = r"text_encoder .*\(os error 1455\).*Dense \(bf16\)"):
+ load(str(tmp_path))
+ (encoder / shard).write_bytes(b"")
+ assert load(str(tmp_path)) == {}
+
+
def test_pipe_kwargs_injects_every_hosted_component(monkeypatch):
"""A family hosting several TE components (flux.1: T5 as text_encoder_2) gets each
one injected under its own attr; unhosted components stay dense."""