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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio Qwen-Image-2.1: encode a condition image again after load_state_dict(assign=True) on its VAE (#17197)
Daniel Han8429c9aa4274
diff --git a/studio/backend/core/inference/diffusion_reference_cache.py b/studio/backend/core/inference/diffusion_reference_cache.pyindex 00cf1fb931e..f853c16540b 100644--- a/studio/backend/core/inference/diffusion_reference_cache.py+++ b/studio/backend/core/inference/diffusion_reference_cache.py@@ -127,8 +127,37 @@ class _Cache:self.entries: "OrderedDict[tuple, Any]" = OrderedDict()self.bytes = 0self.vae_ref: Any = None+ # bumped on every VAE load_state_dict: assign=True can keep version counters equal+ self.generation: Optional[int] = 0+ self.hooked: "weakref.WeakSet[Any]" = weakref.WeakSet()self.lock = threading.Lock()+ def bind(self, vae: Any) -> None:+ with self.lock:+ self.clear()+ self.vae_ref = weakref.ref(vae)+ self.generation = (self.generation or 0) + 1+ if vae in self.hooked:+ return+ cache_ref = weakref.ref(self)+ vae_ref = weakref.ref(vae)++ def _loaded(module: Any, incompatible_keys: Any) -> None:+ cache = cache_ref()+ if cache is not None and cache.vae_ref is not None and cache.vae_ref() is vae_ref():+ with cache.lock:+ cache.clear()+ if cache.generation is not None:+ cache.generation += 1++ try:+ for module in vae.modules():+ module.register_load_state_dict_post_hook(_loaded)+ self.hooked.add(vae)+ except Exception: # noqa: BLE001 - a VAE that takes no hooks is never cached+ with self.lock:+ self.generation = None+def clear(self) -> None:self.entries.clear()self.bytes = 0@@ -185,10 +214,12 @@ def install(pipe: Any, logger: Any = None) -> bool:STATS["bypassed"] += 1return stock(image, generator)if cache.vae_ref is None or cache.vae_ref() is not vae:- with cache.lock:- cache.clear()- cache.vae_ref = weakref.ref(vae)- key = (pixels, vae_key)+ cache.bind(vae)+ generation = cache.generation+ if generation is None:+ STATS["bypassed"] += 1+ return stock(image, generator)+ key = (pixels, vae_key, generation)hit = cache.get(key)if hit is not None:STATS["hits"] += 1diff --git a/studio/backend/tests/test_diffusion_reference_cache.py b/studio/backend/tests/test_diffusion_reference_cache.pyindex 59618ed6117..70344e48c98 100644--- a/studio/backend/tests/test_diffusion_reference_cache.py+++ b/studio/backend/tests/test_diffusion_reference_cache.py@@ -198,3 +198,34 @@ def test_reloaded_weights_and_config_encode_again():got, want = _encode(pipe, image)assert torch.equal(got, want)assert rc.STATS["misses"] == 3 and rc.STATS["hits"] == 1+++def test_assigned_weights_encode_again():+ pipe = _pipe()+ image = _images(n = 1)[0]+ rc.install(pipe)+ _encode(pipe, image)+ before = rc._weights_revision(pipe.vae)+ pipe.vae.load_state_dict(_vae(seed = 9).state_dict(), assign = True)+ assert rc._weights_revision(pipe.vae) == before # the revision alone cannot see the swap+ got, want = _encode(pipe, image)+ assert torch.equal(got, want)+ pipe.vae.decoder.load_state_dict(_vae(seed = 11).decoder.state_dict(), assign = True)+ got, want = _encode(pipe, image)+ assert torch.equal(got, want)+ _encode(pipe, image)+ assert rc.STATS["misses"] == 3 and rc.STATS["hits"] == 1+++def test_a_vae_that_takes_no_hooks_is_never_cached(monkeypatch):+ pipe = _pipe()+ image = _images(n = 1)[0]++ def _refuse(*a, **k):+ raise RuntimeError("no hooks")++ monkeypatch.setattr(torch.nn.Module, "register_load_state_dict_post_hook", _refuse)+ rc.install(pipe)+ got, want = _encode(pipe, image)+ _encode(pipe, image)+ assert torch.equal(got, want) and rc.STATS["hits"] == 0 and rc.STATS["bypassed"] == 2