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 CFG batching off for a resident denoiser with no device timer (CPU, MPS) (#14652)

Daniel Hancc1070715bb6
diff --git a/studio/backend/core/inference/diffusion_cfg_batch.py b/studio/backend/core/inference/diffusion_cfg_batch.py
index 90a8b1d27d1..9c97a123a15 100644
--- a/studio/backend/core/inference/diffusion_cfg_batch.py
+++ b/studio/backend/core/inference/diffusion_cfg_batch.py
@@ -499,6 +499,15 @@ class CfgBatchRouter:
if first is None:
return
if not self._sync(latent):
+ if not self.offloaded and cfg_batch_mode() != "force":
+ # A resident denoiser off CUDA (CPU, MPS): nothing can time the doubled call, and the gate only turns
+ # batching on where it was measured to win, so the shape stays plain unless forced.
+ self._shape_off(
+ shape,
+ f"no device timer on {getattr(latent.device, 'type', 'this device')} to price the doubled call "
+ f"({CFG_BATCH_ENV}=force batches anyway)",
+ )
+ return
self._judge(shape)
return
if not self._memory_ok(latent, shape):
diff --git a/studio/backend/tests/test_diffusion_cfg_batch.py b/studio/backend/tests/test_diffusion_cfg_batch.py
index 65b4e70bf33..f4e2d5f2f78 100644
--- a/studio/backend/tests/test_diffusion_cfg_batch.py
+++ b/studio/backend/tests/test_diffusion_cfg_batch.py
@@ -319,6 +319,30 @@ def test_a_resident_denoiser_is_armed_by_default(monkeypatch):
)
+def test_a_resident_denoiser_off_cuda_stays_plain_unless_forced(monkeypatch):
+ """CPU / MPS loads have no device timer, so the trial cannot price the doubled call: the shape stays plain and the
+ render matches two plain calls exactly. ``force`` still batches."""
+ model = Flux2Transformer2DModel()
+ pipe = _Pipe(model)
+ assert cb.install_cfg_batch(pipe, offload_policy = "none") is None
+ router = cb.router_for(pipe)
+ ref, _ = _flux_loop(Flux2Transformer2DModel())
+ for _ in range(2):
+ got, _ = _flux_loop(model)
+ cb.end_cfg_batch_render(pipe)
+ torch.testing.assert_close(got, ref, rtol = 0, atol = 0)
+ assert set(model.calls) == {1}
+ assert router.stats["batched_steps"] == 0
+ assert "no device timer on cpu" in cb.cfg_batch_status(pipe)["shape_off"]
+
+ monkeypatch.setenv(cb.CFG_BATCH_ENV, "force")
+ forced = Flux2Transformer2DModel()
+ fpipe = _Pipe(forced)
+ assert cb.install_cfg_batch(fpipe, offload_policy = "none") is None
+ _flux_loop(forced)
+ assert 2 in forced.calls and cb.router_for(fpipe).stats["batched_steps"] > 0
+
+
class _Clock:
"""A fake device timer: each forward costs ``cost(batch)`` seconds of the router's perf_counter."""