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: do not cache a VAE rescaled for fp16 decode (#15511)

Daniel Hancd120b29bd9d
diff --git a/studio/backend/core/inference/diffusion_cast_cache.py b/studio/backend/core/inference/diffusion_cast_cache.py
index c85ec9fa540..cadbcb9faca 100644
--- a/studio/backend/core/inference/diffusion_cast_cache.py
+++ b/studio/backend/core/inference/diffusion_cast_cache.py
@@ -264,6 +264,9 @@ def candidates(
"torch": _cache._minor(torch.__version__),
library: _minor(library),
}
+ if class_name == "AutoencoderKL":
+ # Retires entries written from an fp16-decode-rescaled VAE.
+ parts["vae_fp16_decode"] = 1
out.append(
CastCandidate(
component = component,
@@ -736,6 +739,12 @@ def save(
f"{cand.component}: not cached (a tensor changed dtype or shape after the load)",
)
return None
+ # Weights were rescaled in place; a cached copy would be rescaled again on the next load.
+ if getattr(module, "_unsloth_fp16_decode", False):
+ _log(
+ logger, "info", f"{cand.component}: not cached (rescaled in place for fp16 decode)"
+ )
+ return None
names = list(cand.names)
reader = _Reader(module, check)
why = _matches_source(cand, reader)
diff --git a/studio/backend/tests/test_diffusion_cast_cache_fp16_vae.py b/studio/backend/tests/test_diffusion_cast_cache_fp16_vae.py
new file mode 100644
index 00000000000..c98ffe25fdf
--- /dev/null
+++ b/studio/backend/tests/test_diffusion_cast_cache_fp16_vae.py
@@ -0,0 +1,90 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""A VAE rescaled in place for fp16 decode is never cached (a reload would rescale it twice)."""
+
+from __future__ import annotations
+
+import logging
+import sys
+import types
+from pathlib import Path
+
+import pytest
+
+torch = pytest.importorskip("torch")
+diffusers = pytest.importorskip("diffusers")
+pytest.importorskip("safetensors")
+
+BACKEND = Path(__file__).resolve().parents[1]
+if str(BACKEND) not in sys.path:
+ sys.path.insert(0, str(BACKEND))
+
+from core.inference import diffusion_cast_cache as CC # noqa: E402
+from core.inference import diffusion_prequant_cache as C # noqa: E402
+from core.inference.diffusion_vae_fp16 import enable_fp16_vae_decode # noqa: E402
+
+_CFG = dict(
+ latent_channels = 4,
+ layers_per_block = 1,
+ norm_num_groups = 8,
+ block_out_channels = [16, 32],
+ force_upcast = True,
+ down_block_types = ["DownEncoderBlock2D"] * 2,
+ up_block_types = ["UpDecoderBlock2D"] * 2,
+)
+
+
+def cache_root(tmp_path, monkeypatch):
+ monkeypatch.setenv(C.ENV_DIR, str(tmp_path / "cache"))
+ monkeypatch.delenv(CC.CAST_CACHE_ENV, raising = False)
+ monkeypatch.delenv(C.ENV_MODE, raising = False)
+
+
+def _candidate(tmp_path, vae):
+ from safetensors.torch import save_file
+
+ source = tmp_path / "vae"
+ source.mkdir()
+ shard = source / "m.safetensors"
+ src = diffusers.AutoencoderKL(**_CFG)
+ save_file({k: v.contiguous() for k, v in src.state_dict().items()}, str(shard))
+ (source / "config.json").write_text("{}", encoding = "utf-8")
+ vae.load_state_dict(src.state_dict())
+ cand = CC.CastCandidate(
+ component = "vae",
+ library = "diffusers",
+ class_name = "AutoencoderKL",
+ cls = diffusers.AutoencoderKL,
+ dtype = torch.float16,
+ source = str(source),
+ shards = [str(shard)],
+ config_path = str(source / "config.json"),
+ key = "testkey",
+ parts = {
+ "kind": "cast",
+ "layout": CC.CAST_LAYOUT_VERSION,
+ "component": "vae",
+ "library": "diffusers",
+ "class": "AutoencoderKL",
+ "dtype": "float16",
+ "keep_in_fp32": [],
+ },
+ )
+ return cand
+
+
+def test_a_vae_rescaled_for_fp16_decode_is_not_cached(cache_root, tmp_path):
+ vae = diffusers.AutoencoderKL(**_CFG)
+ cand = _candidate(tmp_path, vae)
+ vae = vae.half()
+ cand.names = CC._state_names(vae)
+ pipe = types.SimpleNamespace(vae = vae, unet = object())
+ assert enable_fp16_vae_decode(pipe, types.SimpleNamespace(device = "cuda", backend = "cuda"))
+ assert CC.save(cand, vae, logger = logging.getLogger("t")) is None
+
+
+def test_autoencoder_keys_retire_entries_written_before_the_fix():
+ import inspect
+ assert '"vae_fp16_decode"' in inspect.getsource(CC)