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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: track MPS denoise progress with torch.mps events (#15323)
Long Yixingf2bc9d53656d
diff --git a/studio/backend/core/inference/diffusion.py b/studio/backend/core/inference/diffusion.pyindex dc530e224bc..66c3f1fc405 100644--- a/studio/backend/core/inference/diffusion.py+++ b/studio/backend/core/inference/diffusion.py@@ -13878,7 +13878,7 @@ class DiffusionBackend:total_steps = progress_steps,)- # Count GPU-finished steps: under CUDA graphs the host enqueues all steps early.+ # Count GPU-finished steps: under CUDA graphs or on MPS the host enqueues all steps early.from .video import (_BOUNDARY_MARK_ATTEMPTS,_BOUNDARY_MARK_RETRY_SECONDS,@@ -14141,7 +14141,7 @@ class DiffusionBackend:def _enter_decode_phase() -> None:"""The decoder was entered: a HOST position, possibly far ahead of the GPU. Mark theboundary in the stream and let the poller flip once the GPU reaches it; with no event- to wait on (CPU, MPS) there is no queue to be ahead of, so flip now."""+ to wait on (CPU) there is no queue to be ahead of, so flip now."""if peak_probe is not None:peak_probe.mark_decode()_release_whole_denoiser()diff --git a/studio/backend/core/inference/video.py b/studio/backend/core/inference/video.pyindex c71178bf4ca..00043e3a19a 100644--- a/studio/backend/core/inference/video.py+++ b/studio/backend/core/inference/video.py@@ -1009,16 +1009,19 @@ def _scheduler_step_progress(scheduler.step = original-def _record_cuda_event() -> Any:- """Returns a CUDA event or None; never synchronises, and never records inside a graph capture."""+def _record_device_event() -> Any:+ """Returns a CUDA or MPS event or None; never synchronises, and never records inside a graph capture."""try:import torch- if not torch.cuda.is_available():- return None- if torch.cuda.is_current_stream_capturing():+ if torch.cuda.is_available():+ if torch.cuda.is_current_stream_capturing():+ return None+ event = torch.cuda.Event()+ elif torch.backends.mps.is_available():+ event = torch.mps.Event()+ else:return None- event = torch.cuda.Event()event.record()return eventexcept Exception: # noqa: BLE001 -- progress reporting must never fail a render@@ -1040,9 +1043,8 @@ class _CompletedStepTicker:point: a ``synchronize()`` per step would give the same honest number and hand back theCUDA-graph win that the video speed path exists for.- Falls back to the enqueued count on any host without usable CUDA events (CPU, MPS), which is- exactly today's behaviour and is correct there -- without a graph to run ahead of, the host- count IS the progress.+ MPS runs ahead without any graph (torch >= 2.13), so it gets ``torch.mps.Event`` markers too.+ With no event (CPU, where work is synchronous) the enqueued count is the progress."""def __init__(self, total: int) -> None:@@ -1065,7 +1067,7 @@ class _CompletedStepTicker:def mark_boundary(self) -> bool:"""Records the denoise end in the stream; True means wait on boundary_reached(), else it isreached."""- event = _record_cuda_event()+ event = _record_device_event()with self._lock:self._boundary = eventself._boundary_marked = True@@ -1099,7 +1101,7 @@ class _CompletedStepTicker:self._enqueued = enqueuedif len(self._events) >= self.total:return- event = _record_cuda_event()+ event = _record_device_event()if event is None:returnwith self._lock:@@ -12336,7 +12338,7 @@ class VideoBackend:)def _tick(enqueued: int) -> None:- """Mark submitted steps with a CUDA event; skip while another thread captures (events unsafe then)."""+ """Mark submitted steps with a CUDA/MPS event; skip while another thread captures (events unsafe then)."""_enter_denoise()with _hold_off_cuda_graph_capture() as clear:if not clear:diff --git a/studio/backend/tests/test_video_backend.py b/studio/backend/tests/test_video_backend.pyindex bc611c83e2b..dfb4d57d270 100644--- a/studio/backend/tests/test_video_backend.py+++ b/studio/backend/tests/test_video_backend.py@@ -11094,7 +11094,7 @@ def _ticker(monkeypatch, total):events.append(event)return event- monkeypatch.setattr(video_mod, "_record_cuda_event", _fake_event)+ monkeypatch.setattr(video_mod, "_record_device_event", _fake_event)return video_mod._CompletedStepTicker(total), events@@ -11133,11 +11133,11 @@ def test_ticker_never_walks_backwards_or_past_the_total(monkeypatch):assert ticker.completed() == 4-def test_ticker_falls_back_to_the_host_count_without_cuda_events(monkeypatch):- # CPU/MPS have no events and no run-ahead, so the host count IS the progress.+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- monkeypatch.setattr(video_mod, "_record_cuda_event", lambda: None)+ monkeypatch.setattr(video_mod, "_record_device_event", lambda: None)ticker = video_mod._CompletedStepTicker(5)for step in range(1, 6):ticker.record(step)@@ -11145,6 +11145,104 @@ def test_ticker_falls_back_to_the_host_count_without_cuda_events(monkeypatch):assert ticker.event_backed is False[email protected]("has_event, mps_available", [(True, True), (False, True), (True, False)])+def test_ticker_follows_mps_events(monkeypatch, has_event, mps_available):+ # MPS events back the ticker and boundary; no torch.mps.Event or no MPS keeps the host count.+ import core.inference.video as video_mod++ events: list = []++ class _MpsEvent(_FakeCudaEvent):+ def __init__(self) -> None:+ super().__init__()+ self.recorded = False+ events.append(self)++ def record(self) -> None:+ self.recorded = True++ torch = types.ModuleType("torch")+ torch.cuda = types.SimpleNamespace(is_available = lambda: False)+ torch.backends = types.SimpleNamespace(+ mps = types.SimpleNamespace(is_available = lambda: mps_available)+ )+ torch.mps = types.SimpleNamespace(**({"Event": _MpsEvent} if has_event else {}))+ monkeypatch.setitem(sys.modules, "torch", torch)++ ticker = video_mod._CompletedStepTicker(3)+ for step in range(1, 4):+ ticker.record(step)+ if not (has_event and mps_available):+ assert ticker.completed() == 3+ assert ticker.event_backed is False+ assert ticker.mark_boundary() is False+ return+ assert [event.recorded for event in events] == [True] * 3+ assert ticker.completed() == 0+ events[0].done = True+ assert ticker.completed() == 1+ assert ticker.mark_boundary() is True+ assert ticker.boundary_reached() is False+ for event in events:+ event.done = True+ assert ticker.completed() == 3+ assert ticker.boundary_reached() is True+++def test_ticker_tracks_real_mps_work(monkeypatch):+ # Real Apple Silicon: the host enqueues every step long before the GPU runs them, so the bar+ # must follow the events. The CUDA-only helper this replaced reports all steps done at once.+ torch = pytest.importorskip("torch")+ if not torch.backends.mps.is_available():+ pytest.skip("needs an MPS device")+ import core.inference.video as video_mod++ x = torch.randn(2048, 2048, device = "mps")+ torch.mps.synchronize()++ def _run(ticker, steps):+ y = x+ for step in range(1, steps + 1):+ for _ in range(12):+ y = (y @ x) * 1e-3+ ticker.record(step)+ sentinel = torch.mps.Event()+ sentinel.record()+ return y, sentinel++ def _cuda_only_event():+ if not torch.cuda.is_available():+ return None+ event = torch.cuda.Event()+ event.record()+ return event++ with monkeypatch.context() as patch:+ patch.setattr(video_mod, "_record_device_event", _cuda_only_event)+ base = video_mod._CompletedStepTicker(4)+ _, sentinel = _run(base, 4)+ base_done = base.completed()+ base_behind = not sentinel.query()+ torch.mps.synchronize()++ head = video_mod._CompletedStepTicker(4)+ _, sentinel = _run(head, 4)+ head_done = head.completed()+ head_marked = head.mark_boundary()+ head_behind = not sentinel.query()+ head_boundary = head.boundary_reached()+ torch.mps.synchronize()+ if not (base_behind and head_behind):+ pytest.skip("the GPU caught up with the host before the probe")++ assert base_done == 4 and base.event_backed is False+ assert head.event_backed is True+ assert head_done < 4+ assert head_marked is True and head_boundary is False+ assert head.completed() == 4+ assert head.boundary_reached() is True++def test_ticker_unmarkable_step_does_not_shift_the_ones_after_it(monkeypatch):# A step whose event could not be recorded (a capture was in flight) costs its own tick and# nothing more: the step number travels with the event.@@ -11161,7 +11259,7 @@ def test_ticker_unmarkable_step_does_not_shift_the_ones_after_it(monkeypatch):events.append(event)return event- monkeypatch.setattr(video_mod, "_record_cuda_event", _fake_event)+ monkeypatch.setattr(video_mod, "_record_device_event", _fake_event)ticker = video_mod._CompletedStepTicker(4)for step in range(1, 5):ticker.record(step)@@ -11298,7 +11396,7 @@ def _patch_events(monkeypatch, *, done: bool = False):events.append(event)return event- monkeypatch.setattr(video_mod, "_record_cuda_event", _fake_event)+ monkeypatch.setattr(video_mod, "_record_device_event", _fake_event)return events@@ -11436,7 +11534,7 @@ def test_a_capture_cannot_start_while_an_event_query_is_in_flight(monkeypatch):finish_query.wait(5.0)return True- monkeypatch.setattr(video_mod, "_record_cuda_event", lambda: _BlockingEvent())+ monkeypatch.setattr(video_mod, "_record_device_event", lambda: _BlockingEvent())ticker = video_mod._CompletedStepTicker(1)ticker.record(1)