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: keep Qwen-Image-2.1 scoped LoRA prompt reuse after the no-K/V wrapper, and key callable helpers fully (#17192)

Daniel Han864c2460f151
diff --git a/studio/backend/core/inference/diffusion_prompt_cache.py b/studio/backend/core/inference/diffusion_prompt_cache.py
index 482409ccc28..121a715aecd 100644
--- a/studio/backend/core/inference/diffusion_prompt_cache.py
+++ b/studio/backend/core/inference/diffusion_prompt_cache.py
@@ -226,6 +226,8 @@ _DENOISER_ATTRS = (
# Reading these off a denoiser runs none of its layers.
_INERT_MODULE_ATTRS = frozenset({"dtype", "device", "config"})
_MAX_FOLLOWED_METHODS = 64
+# Marks an instance wrapper that only forwards to the same-named class method and runs no denoiser
+TRANSPARENT_WRAPPER_ATTR = "_unsloth_prompt_cache_transparent"
def _scoped_lora_key() -> bool:
@@ -252,7 +254,12 @@ def _encode_skips_denoisers(
import ast
import textwrap
- own = getattr(instance, "__dict__", None) or {}
+ own = {
+ attr: value
+ for attr, value in (getattr(instance, "__dict__", None) or {}).items()
+ if not _transparent_wrapper(cls, attr, value, instance)
+ }
+ denoisers = [own.get(attr) for attr in _DENOISER_ATTRS if own.get(attr) is not None]
seen: set = set()
todo = [name]
while todo:
@@ -320,7 +327,7 @@ def _encode_skips_denoisers(
):
return False
elif node.attr in own:
- if _bound_code(own[node.attr]):
+ if _bound_code(own[node.attr]) or _opaque_callable(own[node.attr], denoisers):
return False
elif _followable(inspect.getattr_static(cls, node.attr, None)):
todo.append(node.attr)
@@ -333,6 +340,60 @@ def _bound_code(value: Any) -> bool:
return isinstance(value, (types.FunctionType, types.MethodType, functools.partial))
+def _transparent_wrapper(cls: Any, attr: str, value: Any, instance: Any) -> bool:
+ """``value`` is a ``TRANSPARENT_WRAPPER_ATTR`` wrapper of ``instance``'s own ``cls.<attr>``."""
+ if not callable(value):
+ return False
+ for _ in range(8):
+ if not getattr(value, TRANSPARENT_WRAPPER_ATTR, False):
+ break
+ value = getattr(value, "__wrapped__", None)
+ else:
+ return False
+ if value is getattr(instance, "__dict__", {}).get(attr):
+ return False
+ target = inspect.getattr_static(cls, attr, None)
+ return (
+ getattr(value, "__self__", None) is instance
+ and target is not None
+ and getattr(value, "__func__", None) is target
+ )
+
+
+def _opaque_callable(value: Any, denoisers: list) -> bool:
+ """Callable the walk cannot read; tokenizers/processors and denoiser-free modules are exempt."""
+ import types
+
+ if (
+ not callable(value)
+ or isinstance(value, type)
+ or isinstance(value, types.BuiltinFunctionType)
+ ):
+ return False
+ try:
+ import torch
+ if isinstance(value, torch.nn.Module):
+ ids = {id(m) for d in denoisers if isinstance(d, torch.nn.Module) for m in d.modules()}
+ return any(id(m) in ids for m in value.modules()) or any(
+ v is d for v in vars(value).values() for d in denoisers
+ )
+ except Exception: # noqa: BLE001 - unclassifiable
+ return True
+ try:
+ from transformers import PreTrainedTokenizerBase, ProcessorMixin
+ from transformers.image_processing_utils import ImageProcessingMixin
+ from transformers.feature_extraction_utils import FeatureExtractionMixin
+
+ if isinstance(
+ value,
+ (PreTrainedTokenizerBase, ProcessorMixin, ImageProcessingMixin, FeatureExtractionMixin),
+ ):
+ return False
+ except Exception: # noqa: BLE001 - no transformers: nothing to exempt
+ pass
+ return True
+
+
def _followable(value: Any) -> bool:
return value is not None and (callable(value) or hasattr(type(value), "__get__"))
diff --git a/studio/backend/core/inference/diffusion_qwenimage21_te.py b/studio/backend/core/inference/diffusion_qwenimage21_te.py
index 35147b15cda..49e75348cfa 100644
--- a/studio/backend/core/inference/diffusion_qwenimage21_te.py
+++ b/studio/backend/core/inference/diffusion_qwenimage21_te.py
@@ -59,6 +59,9 @@ def skip_text_encoder_kv_cache(pipe: Any, logger: Any = None) -> bool:
handle.remove()
setattr(_get_qwen_prompt_embeds, _MARK, True)
+ from .diffusion_prompt_cache import TRANSPARENT_WRAPPER_ATTR
+
+ setattr(_get_qwen_prompt_embeds, TRANSPARENT_WRAPPER_ATTR, True)
pipe._get_qwen_prompt_embeds = _get_qwen_prompt_embeds
if logger is not None:
logger.info(
diff --git a/studio/backend/tests/test_diffusion_prompt_cache.py b/studio/backend/tests/test_diffusion_prompt_cache.py
index 606850b7804..622e722f6cf 100644
--- a/studio/backend/tests/test_diffusion_prompt_cache.py
+++ b/studio/backend/tests/test_diffusion_prompt_cache.py
@@ -979,3 +979,160 @@ def test_a_property_running_no_denoiser_still_reuses_the_embeddings(monkeypatch)
pipe = _CleanPropertyPipe({"transformer": ["style"]})
prompt_cache.install(pipe)
assert _strength_sweep(pipe) == [1.0, 1.0] and pipe.calls == 1
+
+
+class Qwen3VLFakeEncoder(torch.nn.Module):
+ def __init__(self):
+ super().__init__()
+ self.proj = torch.nn.Linear(2, 2)
+ self.seen_use_cache = []
+
+ def forward(
+ self,
+ x,
+ use_cache = True,
+ ):
+ self.seen_use_cache.append(use_cache)
+ return self.proj(x)
+
+
+class _Qwen21LikePipe(_AdapterPipe):
+ def __init__(self, listed = None):
+ super().__init__(listed)
+ self.text_encoder = Qwen3VLFakeEncoder()
+
+ def _get_qwen_prompt_embeds(self, prompt):
+ return self.text_encoder(torch.full((1, 2), float(len(prompt))))
+
+ def encode_prompt(
+ self,
+ prompt,
+ device = None,
+ ):
+ self.calls += 1
+ return (self._get_qwen_prompt_embeds(prompt).detach(),)
+
+
+def test_qwen21_no_kv_wrapper_installed_first_keeps_the_scoped_key(monkeypatch):
+ # #17118 installs the no-K/V wrapper on the instance before the prompt cache: still denoiser-free
+ from core.inference.diffusion_qwenimage21_te import skip_text_encoder_kv_cache
+
+ monkeypatch.delenv(SCOPED_ENV, raising = False)
+ monkeypatch.delenv("UNSLOTH_QWEN21_TE_NO_KV", raising = False)
+ pipe = _Qwen21LikePipe({"transformer": ["style"]})
+ assert skip_text_encoder_kv_cache(pipe)
+ assert "_get_qwen_prompt_embeds" in pipe.__dict__
+ assert prompt_cache._encode_skips_denoisers(type(pipe), "encode_prompt", pipe)
+ prompt_cache.install(pipe)
+ calls = _sweep(pipe, *((("style", "/x/style.safetensors", w),) for w in (0.25, 0.5, 0.75)))
+ assert calls == 1 and pipe.text_encoder.seen_use_cache == [False]
+
+
+def test_an_unmarked_wrapper_of_the_same_helper_keeps_the_full_key(monkeypatch):
+ import functools
+
+ monkeypatch.delenv(SCOPED_ENV, raising = False)
+ pipe = _Qwen21LikePipe({"transformer": ["style"]})
+ original = pipe._get_qwen_prompt_embeds
+
+ @functools.wraps(original)
+ def _get_qwen_prompt_embeds(prompt):
+ return original(prompt) + pipe.transformer.weight.sum()
+
+ pipe._get_qwen_prompt_embeds = _get_qwen_prompt_embeds
+ assert not prompt_cache._encode_skips_denoisers(type(pipe), "encode_prompt", pipe)
+
+
+def test_a_marked_wrapper_around_a_foreign_callable_keeps_the_full_key(monkeypatch):
+ monkeypatch.delenv(SCOPED_ENV, raising = False)
+ pipe = _Qwen21LikePipe({"transformer": ["style"]})
+
+ def _get_qwen_prompt_embeds(prompt):
+ return pipe.transformer(torch.ones(1, 2))
+
+ _get_qwen_prompt_embeds.__wrapped__ = lambda prompt: None
+ setattr(_get_qwen_prompt_embeds, prompt_cache.TRANSPARENT_WRAPPER_ATTR, True)
+ pipe._get_qwen_prompt_embeds = _get_qwen_prompt_embeds
+ assert not prompt_cache._encode_skips_denoisers(type(pipe), "encode_prompt", pipe)
+
+
+class _CallableHelper:
+ def __init__(self, pipe):
+ self.pipe = pipe
+
+ def __call__(self):
+ return self.pipe.transformer.weight.detach().clone()
+
+
+class _CallableHelperPipe(_AdapterPipe):
+ def __init__(self, listed = None):
+ super().__init__(listed)
+ self.helper = _CallableHelper(self)
+
+ def encode_prompt(
+ self,
+ prompt,
+ device = None,
+ ):
+ self.calls += 1
+ return (self.helper(),)
+
+
+class _WrapsDenoiser(torch.nn.Module):
+ def __init__(self, inner):
+ super().__init__()
+ self.inner = inner
+
+ def forward(self, x):
+ return self.inner(x)
+
+
+class _ModuleHelperPipe(_AdapterPipe):
+ def __init__(self, listed = None):
+ super().__init__(listed)
+ self.helper = _WrapsDenoiser(self.transformer)
+
+ def encode_prompt(
+ self,
+ prompt,
+ device = None,
+ ):
+ self.calls += 1
+ return (self.helper(torch.ones(1, 2)).detach(),)
+
+
[email protected]("cls", [_CallableHelperPipe, _ModuleHelperPipe])
+def test_a_callable_instance_helper_reading_the_denoiser_keeps_the_full_key(monkeypatch, cls):
+ monkeypatch.delenv(SCOPED_ENV, raising = False)
+ pipe = cls({"transformer": ["style"]})
+ assert not prompt_cache._encode_skips_denoisers(cls, "encode_prompt", pipe)
+ prompt_cache.install(pipe)
+ out = _strength_sweep(pipe)
+ assert out[0] != out[1] and pipe.calls == 2
+
+
+def test_the_text_encoder_module_is_not_an_opaque_helper(monkeypatch):
+ monkeypatch.delenv(SCOPED_ENV, raising = False)
+ pipe = _Qwen21LikePipe({"transformer": ["style"]})
+ assert prompt_cache._encode_skips_denoisers(type(pipe), "encode_prompt", pipe)
+
+
+def test_this_module_is_the_tree_under_test():
+ import pathlib
+ here = pathlib.Path(__file__).resolve().parents[1]
+ assert pathlib.Path(prompt_cache.__file__).resolve().is_relative_to(here)
+
+
+def test_the_real_qwen21_pipeline_keeps_the_scoped_key_after_the_no_kv_wrapper(monkeypatch):
+ diffusers = pytest.importorskip("diffusers")
+ cls = getattr(diffusers, "QwenImage21Pipeline", None)
+ if cls is None:
+ pytest.skip("this diffusers has no QwenImage21Pipeline")
+ from core.inference.diffusion_qwenimage21_te import skip_text_encoder_kv_cache
+
+ monkeypatch.delenv("UNSLOTH_QWEN21_TE_NO_KV", raising = False)
+ pipe = cls.__new__(cls)
+ pipe.__dict__["text_encoder"] = Qwen3VLFakeEncoder()
+ assert prompt_cache._encode_skips_denoisers(cls, "encode_prompt", pipe)
+ assert skip_text_encoder_kv_cache(pipe) and "_get_qwen_prompt_embeds" in pipe.__dict__
+ assert prompt_cache._encode_skips_denoisers(cls, "encode_prompt", pipe)