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.

Disengage FBCache on the word of whoever owns it (#11304)

Daniel Hanc7d8980a206d
diff --git a/studio/backend/core/inference/diffusion_cache.py b/studio/backend/core/inference/diffusion_cache.py
index 7dc356fee50..0e1e70c0014 100644
--- a/studio/backend/core/inference/diffusion_cache.py
+++ b/studio/backend/core/inference/diffusion_cache.py
@@ -426,6 +426,12 @@ def maybe_toggle_step_cache(
if not want and engaged:
disable_cache = getattr(transformer, "disable_cache", None)
if callable(disable_cache):
+ # Read BEFORE the teardown: it is what says whether diffusers' own disable_cache can
+ # do the removal, and disable_cache clears it.
+ ours = (
+ type(getattr(transformer, "_cache_config", None)).__name__
+ == "FirstBlockCacheConfig"
+ )
try:
# Restore before remove_hook splices original_forward back, so compiled wrappers do not leak onto the
# uncached path.
@@ -433,7 +439,14 @@ def maybe_toggle_step_cache(
disable_cache()
# disable_cache removes nothing when _cache_config is None, which is exactly an
# adopted low-level cache, so trusting it would clear the marker over live hooks.
- if not _unhook_first_block_cache(transformer):
+ # THAT is the case the verification exists for, so it only gets a veto there. A
+ # live FirstBlockCacheConfig means diffusers just removed both hooks and cleared
+ # the config itself, and disable_cache returning without raising is the evidence;
+ # demanding the private-name sweep succeed on top of it makes every disengage
+ # depend on `diffusers.hooks.first_block_cache` staying importable, and when it is
+ # not, FBCache stays engaged on short trajectories forever -- the exact quality
+ # regression the auto policy exists to avoid.
+ if not _unhook_first_block_cache(transformer) and not ours:
_warn(
logger,
"fbcache disable",
diff --git a/studio/backend/tests/test_diffusion_cache.py b/studio/backend/tests/test_diffusion_cache.py
index b60d356aaa2..a1908b2c20e 100644
--- a/studio/backend/tests/test_diffusion_cache.py
+++ b/studio/backend/tests/test_diffusion_cache.py
@@ -360,6 +360,67 @@ def test_toggle_disengages_below_the_bar(monkeypatch):
assert t.disables == 1
+class _DiffusersLikeTransformer(_ToggleTransformer):
+ """A toggle fake that records ``_cache_config`` the way diffusers itself does.
+
+ diffusers sets it inside ``enable_cache`` and clears it inside ``disable_cache``, and the cache
+ layer reads THAT (never our own marker) to tell a cache it installed from one adopted through
+ the low-level ``apply_first_block_cache``. A fake without it models a transformer whose hooks
+ can never be accounted for, which is not what a real DiT looks like after a successful engage.
+ """
+
+ def enable_cache(self, config):
+ super().enable_cache(config)
+ self._cache_config = config
+ self.is_cache_enabled = True
+
+ def disable_cache(self):
+ super().disable_cache()
+ self._cache_config = None
+ self.is_cache_enabled = False
+
+
+def _hide_private_hook_names(monkeypatch):
+ """Make ``diffusers.hooks.first_block_cache`` unimportable, as a shifted diffusers would."""
+ monkeypatch.setitem(sys.modules, "diffusers.hooks.first_block_cache", None)
+
+
+def test_toggle_disengages_even_when_the_private_hook_names_are_gone(monkeypatch):
+ """A cache diffusers owns comes off on diffusers' word, not on the private-name sweep.
+
+ The sweep exists for an ADOPTED low-level cache, where disable_cache removes nothing because
+ _cache_config is None. Requiring it for our own cache too makes every disengage depend on
+ `diffusers.hooks.first_block_cache` staying importable, and on the versions where it is not,
+ FBCache stays engaged on short trajectories forever -- the quality regression the auto policy
+ exists to prevent. That is what took test_video_backend's auto-toggle test red on main.
+ """
+ _stub_diffusers(monkeypatch)
+ t = _DiffusersLikeTransformer()
+ maybe_toggle_step_cache(_pipe(t), steps = 28)
+ assert t._cache_config is not None, "the engage did not leave a live config to key on"
+
+ _hide_private_hook_names(monkeypatch)
+ mode = maybe_toggle_step_cache(_pipe(t), steps = 8)
+ assert mode is None, "a cache diffusers removed itself is still being reported as engaged"
+ assert t.disables == 1
+ assert not t._unsloth_step_cache
+
+
+def test_toggle_keeps_an_adopted_cache_it_cannot_verify_removed(monkeypatch):
+ """The other half: with no live config, disable_cache removed nothing, so the marker stays.
+
+ Clearing it over live hooks is what would let the CUDA graph wrapper capture a graph across
+ them, so "cannot verify" has to mean "still engaged" here.
+ """
+ _stub_diffusers(monkeypatch)
+ t = _ToggleTransformer() # never sets _cache_config: the adopted low-level shape
+ maybe_toggle_step_cache(_pipe(t), steps = 28)
+
+ _hide_private_hook_names(monkeypatch)
+ assert maybe_toggle_step_cache(_pipe(t), steps = 8) == TC_FBCACHE
+ assert t._unsloth_step_cache, "the marker came off hooks that were never verified gone"
+
+
def test_toggle_reengages_after_a_disable(monkeypatch):
_stub_diffusers(monkeypatch)
t = _ToggleTransformer()
diff --git a/studio/backend/tests/test_video_backend.py b/studio/backend/tests/test_video_backend.py
index 18357c9713f..0855b22362f 100644
--- a/studio/backend/tests/test_video_backend.py
+++ b/studio/backend/tests/test_video_backend.py
@@ -250,6 +250,11 @@ class _FakeTransformer:
# experts. The MoE __call__ carries guidance_scale_2 and the single-DiT one omits it, so the cfg2 gate is exercised.
+class FirstBlockCacheConfig: # noqa: N801 - the name diffusers exports, and what the cache layer matches on
+ def __init__(self, threshold = None) -> None:
+ self.threshold = threshold
+
+
class _FakeWanDiT:
"""One Wan denoiser. Records which optimisation helpers touched it (the loader
applies each once per expert on an MoE load), so a test can prove BOTH experts
@@ -266,9 +271,18 @@ class _FakeWanDiT:
def enable_cache(self, config) -> None:
self.cache_config = config
+ # diffusers records the live config on the module itself, and the cache layer reads THAT
+ # (never our own marker) to tell a cache it installed from one adopted through the
+ # low-level apply_first_block_cache. A fake that skips it models a transformer whose
+ # teardown can never be accounted for, which is not what a real DiT looks like after a
+ # successful enable_cache.
+ self._cache_config = config
+ self.is_cache_enabled = True
def disable_cache(self) -> None:
self.cache_config = None
+ self._cache_config = None
+ self.is_cache_enabled = False
def set_attention_backend(self, backend) -> None:
self.attention = backend
@@ -633,7 +647,10 @@ def fake_runtime(monkeypatch):
diffusers.WanTransformer3DModel = _FakeTransformer
diffusers.HunyuanVideo15Pipeline = _FakeHV15Pipeline
diffusers.HunyuanVideo15Transformer3DModel = _FakeTransformer
- diffusers.FirstBlockCacheConfig = lambda threshold = None: ("fbcache", threshold)
+ # A CLASS, and named the way diffusers names it: the cache layer matches on
+ # type(...).__name__ to tell its own FBCache config from a MagCache / PAB one, so a lambda
+ # returning a tuple reads as "some other cache" and makes the teardown unverifiable.
+ diffusers.FirstBlockCacheConfig = FirstBlockCacheConfig
monkeypatch.setitem(sys.modules, "torch", torch)
monkeypatch.setitem(sys.modules, "diffusers", diffusers)