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: Stop and eject interrupt a running image generation (#17559)

Nilay3b8286c7650e
diff --git a/studio/backend/core/inference/diffusion.py b/studio/backend/core/inference/diffusion.py
index 38852440220..752b8ff9da8 100644
--- a/studio/backend/core/inference/diffusion.py
+++ b/studio/backend/core/inference/diffusion.py
@@ -15547,6 +15547,7 @@ class DiffusionBackend:
_BOUNDARY_MARK_RETRY_SECONDS,
_CompletedStepTicker,
_completed_step_poller,
+ _hold_for_device,
_hold_off_cuda_graph_capture,
)
@@ -15598,6 +15599,7 @@ class DiffusionBackend:
if clear:
chunk_ticker[0].record(step_index + 1)
_report(chunk_ticker[0].completed())
+ _hold_for_device(chunk_ticker[0], cancel)
if cancel.is_set():
pipe._interrupt = True
cross_forward_step(pipe, step_index)
diff --git a/studio/backend/core/inference/video.py b/studio/backend/core/inference/video.py
index 45106a03a81..a952fab1ef1 100644
--- a/studio/backend/core/inference/video.py
+++ b/studio/backend/core/inference/video.py
@@ -1213,6 +1213,17 @@ class _CompletedStepTicker:
self._events.append((enqueued, event))
self._event_backed = True
+ def caught_up(self, lead: int) -> bool:
+ """Whether the device has finished every step but the last ``lead`` enqueued; never blocks."""
+ with self._lock:
+ if self._enqueued >= self.total or len(self._events) <= lead:
+ return True
+ event = self._events[-1 - lead][1]
+ try:
+ return bool(event.query())
+ except Exception: # noqa: BLE001 -- an unqueryable marker counts as finished
+ return True
+
def completed(self) -> int:
"""Steps the GPU has finished. Never decreases, never exceeds what was enqueued, never
blocks, and never raises."""
@@ -1248,6 +1259,19 @@ def _hold_off_cuda_graph_capture():
yield clear
+def _hold_for_device(
+ ticker: Any,
+ cancel: Any,
+ lead: int = 1,
+) -> None:
+ """Keeps the host at most ``lead`` steps ahead of the device: an enqueued step cannot be cancelled."""
+ while not cancel.is_set():
+ with _hold_off_cuda_graph_capture() as clear:
+ if not clear or ticker.caught_up(lead):
+ return
+ cancel.wait(0.002)
+
+
@contextlib.contextmanager
def _completed_step_poller(pump: Any, poll_seconds: float = 0.1):
"""The pump runs under the capture hold-off, since a query inside a capture invalidates the graph."""
@@ -14214,6 +14238,7 @@ class VideoBackend:
return
ticker.record(enqueued)
_report(ticker.completed())
+ _hold_for_device(ticker, cancel)
def _finish_denoise() -> None:
"""Finish the bar once the denoise is over: H3 makes 29 scheduler.step calls for
@@ -14261,15 +14286,14 @@ class VideoBackend:
if static_skip:
mark_step_end(_skip_pipe(state, pipe))
# Called after scheduler.step, so the step's latent update is already submitted.
+ if not cancel.is_set():
+ _tick(step_index + 1)
+ if previewer is not None:
+ previewer.on_step(
+ callback_kwargs.get("latents"), getattr(p, "scheduler", None)
+ )
if cancel.is_set():
p._interrupt = True
- cross_forward_step(p, step_index, timestep = timestep)
- return callback_kwargs
- _tick(step_index + 1)
- if previewer is not None:
- previewer.on_step(
- callback_kwargs.get("latents"), getattr(p, "scheduler", None)
- )
cross_forward_step(p, step_index, timestep = timestep)
return callback_kwargs
diff --git a/studio/backend/tests/test_diffusion_backend.py b/studio/backend/tests/test_diffusion_backend.py
index aea0b2ac083..4ddc3f49d01 100644
--- a/studio/backend/tests/test_diffusion_backend.py
+++ b/studio/backend/tests/test_diffusion_backend.py
@@ -11625,6 +11625,57 @@ def test_cancel_generate_lands_at_the_next_step_boundary(fake_runtime, tmp_path,
assert seen == [0, 1, 2]
+def test_cancel_generate_stops_a_denoise_the_device_has_not_caught_up_with(
+ fake_runtime, tmp_path, monkeypatch
+):
+ from core.inference.diffusion_families import DIFFUSION_CANCELLED_MSG
+
+ (tmp_path / "model.gguf").write_bytes(b"x")
+ backend = _loaded_backend(tmp_path)
+
+ class _UnfinishedStep:
+ def query(self):
+ return False
+
+ monkeypatch.setattr("core.inference.video._record_device_event", lambda: _UnfinishedStep())
+
+ issued: list[int] = []
+
+ def _call(
+ self,
+ *,
+ callback_on_step_end = None,
+ **kwargs,
+ ):
+ self._interrupt = False
+ for index in range(20):
+ if getattr(self, "_interrupt", False):
+ break
+ issued.append(index)
+ callback_on_step_end(self, index, index, {})
+ return types.SimpleNamespace(images = [_FakeImage()])
+
+ monkeypatch.setattr(_FakePipe, "__call__", _call)
+
+ outcome: dict = {}
+
+ def _run():
+ try:
+ outcome["result"] = backend.generate(prompt = "x", steps = 20)
+ except BaseException as exc: # noqa: BLE001 -- the test asserts on the exact type/text
+ outcome["error"] = exc
+
+ worker = threading.Thread(target = _run, daemon = True)
+ worker.start()
+ worker.join(0.5)
+
+ backend.cancel_generate()
+ worker.join(10)
+ assert not worker.is_alive()
+ assert len(issued) < 20, "every step was issued while the device was still on the first"
+ assert str(outcome.get("error")) == DIFFUSION_CANCELLED_MSG
+
+
def test_cancel_generate_during_the_post_denoise_save_still_cancels(
fake_runtime, tmp_path, monkeypatch
):
diff --git a/studio/backend/tests/test_video_backend.py b/studio/backend/tests/test_video_backend.py
index 1af003992b3..e9b94b21067 100644
--- a/studio/backend/tests/test_video_backend.py
+++ b/studio/backend/tests/test_video_backend.py
@@ -11228,6 +11228,20 @@ def test_ticker_never_walks_backwards_or_past_the_total(monkeypatch):
assert ticker.completed() == 4
+def test_ticker_caught_up_waits_only_for_steps_before_the_lead(monkeypatch):
+ ticker, events = _ticker(monkeypatch, 4)
+ ticker.record(1)
+ assert ticker.caught_up(1)
+ ticker.record(2)
+ assert not ticker.caught_up(1)
+ events[0].done = True
+ assert ticker.caught_up(1)
+ ticker.record(3)
+ ticker.record(4)
+ # The last step is enqueued: nothing is left to cancel, so the host is never held.
+ assert ticker.caught_up(1)
+
+
def test_ticker_falls_back_to_the_host_count_without_device_events(monkeypatch):
# A CPU host has no events and no run-ahead, so the host count IS the progress.
import core.inference.video as video_mod
@@ -11477,10 +11491,19 @@ def test_decode_phase_covers_the_audio_decoder_too():
assert "decode" not in audio_vae.__dict__
-def _patch_events(monkeypatch, *, done: bool = False):
- """Hand out fake CUDA events, optionally already complete. Returns the list they land in."""
+def _patch_events(
+ monkeypatch,
+ *,
+ done: bool = False,
+ run_ahead: bool = True,
+):
+ """Hand out fake CUDA events, optionally already complete. Returns the list they land in.
+
+ ``run_ahead`` lets the host outrun events that never finish, as it does when the hold is skipped."""
import core.inference.video as video_mod
+ if run_ahead:
+ monkeypatch.setattr(video_mod, "_hold_for_device", lambda ticker, cancel: None)
events: list = []
def _fake_event():
@@ -11528,6 +11551,38 @@ def test_cancellation_is_still_checked_before_the_step_runs(fake_runtime, monkey
assert len(events) == 1
+ "repo",
+ [
+ "Wan-AI/Wan2.2-TI2V-5B-Diffusers",
+ "hunyuanvideo-community/HunyuanVideo-1.5-Diffusers-480p_t2v",
+ ],
+)
+def test_a_cancel_stops_a_denoise_the_device_has_not_caught_up_with(
+ fake_runtime, monkeypatch, repo
+):
+ # A step the host has enqueued cannot be cancelled, so the host must not run ahead of the device.
+ events = _patch_events(monkeypatch, run_ahead = False)
+ backend = VideoBackend()
+ backend.load_pipeline(repo, model_kind = "pipeline")
+ outcome: dict = {}
+
+ def _run():
+ try:
+ backend.generate(prompt = "a fox", steps = 20, num_frames = 9, fps = 24)
+ except BaseException as exc: # noqa: BLE001 -- asserted below
+ outcome["error"] = exc
+
+ worker = threading.Thread(target = _run, daemon = True)
+ worker.start()
+ worker.join(0.5)
+ backend.cancel_generate()
+ worker.join(10)
+ assert not worker.is_alive()
+ assert VIDEO_CANCELLED_MSG in str(outcome.get("error"))
+ assert len(events) <= 3, len(events)
+
+
def test_phase_reads_encode_until_the_denoise_loop_starts(fake_runtime, monkeypatch):
# The prompt encode runs inside pipe() before the first step; the bar said "denoise" through it.
_patch_events(monkeypatch)