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: cache Edit-mode text encoding by reference image content (#13683)

kevina8585676d4e6
diff --git a/studio/backend/core/inference/diffusion_prompt_cache.py b/studio/backend/core/inference/diffusion_prompt_cache.py
index 02572af0757..e3ad3eb032c 100644
--- a/studio/backend/core/inference/diffusion_prompt_cache.py
+++ b/studio/backend/core/inference/diffusion_prompt_cache.py
@@ -78,9 +78,23 @@ def _plain(value: Any) -> Any:
torch = _torch()
if isinstance(value, (torch.dtype, torch.device)):
return str(value)
+ image = _image_fingerprint(value)
+ if image is not None:
+ return image
raise TypeError(type(value).__name__)
+def _image_fingerprint(value: Any) -> Optional[list]:
+ """Edit / reference images key by pixel content, so a repeat edit reuses the encoder output."""
+ if not (hasattr(value, "tobytes") and hasattr(value, "mode") and hasattr(value, "size")):
+ return None
+ module = type(value).__module__ or ""
+ if not module.startswith("PIL."):
+ return None
+ digest = hashlib.sha256(value.tobytes()).hexdigest()
+ return ["PIL.Image", str(value.mode), list(value.size), digest]
+
+
def _is_tensor(obj: Any) -> bool:
return hasattr(obj, "detach") and hasattr(obj, "clone") and hasattr(obj, "element_size")
@@ -269,6 +283,7 @@ def _wrap_encode_prompt(
return False
def cached_encode_prompt(*args: Any, **kwargs: Any) -> Any:
+ key = None
try:
bound = signature.bind(*args, **kwargs)
bound.apply_defaults()
@@ -286,9 +301,12 @@ def _wrap_encode_prompt(
hit = cache.get(key, bound.arguments.get("device"))
except TypeError:
cache.stats["bypassed"] += 1
- return encode(*args, **kwargs)
+ key = None
except Exception as exc: # noqa: BLE001 - never fail a render over the cache
_warn(logger, "lookup", exc)
+ key = None
+ # Encode outside the handlers so an encoder failure is not chained onto the lookup error.
+ if key is None:
return encode(*args, **kwargs)
if hit is not None:
return hit
@@ -367,6 +385,7 @@ def _make_h3_shim(original: Any, logger: Any) -> Any:
if registered is None:
return original(*args, **kwargs)
cache, load_fp = registered
+ key = None
try:
if arguments.get("vision_inputs"):
raise TypeError("vision inputs")
@@ -382,9 +401,11 @@ def _make_h3_shim(original: Any, logger: Any) -> Any:
hit = cache.get(key, arguments.get("device"))
except TypeError:
cache.stats["bypassed"] += 1
- return original(*args, **kwargs)
+ key = None
except Exception as exc: # noqa: BLE001
_warn(logger, "h3 lookup", exc)
+ key = None
+ if key is None:
return original(*args, **kwargs)
if hit is not None:
return hit
diff --git a/studio/backend/tests/test_diffusion_prompt_cache.py b/studio/backend/tests/test_diffusion_prompt_cache.py
index b3c2434b1ac..ad8a6119897 100644
--- a/studio/backend/tests/test_diffusion_prompt_cache.py
+++ b/studio/backend/tests/test_diffusion_prompt_cache.py
@@ -389,3 +389,106 @@ def test_controlnet_pipe_gets_the_prompt_cache(monkeypatch):
first = pipe.encode_prompt("a sloth", device = "cpu")
again = pipe.encode_prompt("a sloth", device = "cpu")
assert calls == ["a sloth"] and torch.equal(first, again)
+
+
+def _image(
+ color,
+ size = (8, 6),
+ mode = "RGB",
+):
+ Image = pytest.importorskip("PIL.Image")
+ return Image.new(mode, size, color)
+
+
+def test_edit_reference_image_is_cached_by_pixel_content():
+ pipe = _EncodePipe()
+ assert prompt_cache.install(pipe)
+ first = pipe.encode_prompt("a cat", image = _image((10, 20, 30)))
+ again = pipe.encode_prompt("a cat", image = _image((10, 20, 30)))
+ as_list = pipe.encode_prompt("a cat", image = [_image((10, 20, 30))])
+ assert pipe.calls == 2
+ assert torch.equal(first[0], again[0])
+ assert torch.equal(first[0], as_list[0])
+ stats = pipe._unsloth_prompt_cache.describe()
+ assert stats["bypassed"] == 0 and stats["hits"] == 1
+
+
+ "other",
+ [
+ lambda: _image((10, 20, 31)),
+ lambda: _image((10, 20, 30), size = (6, 8)),
+ lambda: _image((10, 20, 30, 255), mode = "RGBA"),
+ ],
+)
+def test_a_different_reference_image_misses(other):
+ pipe = _EncodePipe()
+ assert prompt_cache.install(pipe)
+ pipe.encode_prompt("a cat", image = _image((10, 20, 30)))
+ pipe.encode_prompt("a cat", image = other())
+ assert pipe.calls == 2
+ assert pipe._unsloth_prompt_cache.describe()["hits"] == 0
+
+
+def test_an_edited_image_object_misses_after_in_place_change():
+ pipe = _EncodePipe()
+ assert prompt_cache.install(pipe)
+ image = _image((10, 20, 30))
+ pipe.encode_prompt("a cat", image = image)
+ image.putpixel((0, 0), (255, 0, 0))
+ pipe.encode_prompt("a cat", image = image)
+ assert pipe.calls == 2
+
+
+def test_bypassed_encoder_error_is_not_chained_onto_the_key_error():
+ class Boom(_EncodePipe):
+ def encode_prompt(
+ self,
+ prompt,
+ image = None,
+ device = None,
+ ):
+ raise RuntimeError("encoder failed")
+
+ pipe = Boom()
+ assert prompt_cache.install(pipe)
+ with pytest.raises(RuntimeError) as info:
+ pipe.encode_prompt("a cat", image = object())
+ assert info.value.__context__ is None
+ assert pipe._unsloth_prompt_cache.describe()["bypassed"] == 1
+
+
+def test_numpy_like_object_with_tobytes_still_bypasses():
+ class FakeImage:
+ mode = "RGB"
+ size = (1, 1)
+
+ def tobytes(self):
+ return b"\0\0\0"
+
+ pipe = _EncodePipe()
+ assert prompt_cache.install(pipe)
+ pipe.encode_prompt("a cat", image = FakeImage())
+ assert pipe._unsloth_prompt_cache.describe()["bypassed"] == 1
+
+
+def test_failed_lookup_encoder_error_is_not_chained(monkeypatch):
+ class Boom(_EncodePipe):
+ def encode_prompt(
+ self,
+ prompt,
+ image = None,
+ device = None,
+ ):
+ raise RuntimeError("encoder failed")
+
+ pipe = Boom()
+ assert prompt_cache.install(pipe)
+
+ def broken_get(*_args, **_kwargs):
+ raise OSError("lookup failed")
+
+ monkeypatch.setattr(pipe._unsloth_prompt_cache, "get", broken_get)
+ with pytest.raises(RuntimeError) as info:
+ pipe.encode_prompt("a cat")
+ assert info.value.__context__ is None