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.

fix(studio): time a speculative vision batch row's prefill from before add() (#15517)

Long Yixingb6fc821e7fc4
diff --git a/studio/backend/core/inference/mlx_inference.py b/studio/backend/core/inference/mlx_inference.py
index 20ea47d0baa..d6d7a06a849 100644
--- a/studio/backend/core/inference/mlx_inference.py
+++ b/studio/backend/core/inference/mlx_inference.py
@@ -4248,6 +4248,7 @@ class _VisionBatchSession:
if state is None and plan.images:
backend._release_vlm_snapshots()
resume = {} if state is None else {"prompt_cache_state": state}
+ admitted_at = time.perf_counter()
try:
row_number = self.stream.add(
GenerationRequest(
@@ -4262,6 +4263,9 @@ class _VisionBatchSession:
except BatchRowRefused as refusal:
raise RowRefused(str(refusal)) from refusal
row = _VisionBatchRow(handle = handle, plan = plan, row = row_number)
+ if self._speculative is not None:
+ # A speculative batch prefills the row inside add(), so its clock starts before it.
+ row.admitted_at = admitted_at
self._rows[handle] = row
self._by_row[row_number] = row
logger.info(
diff --git a/studio/backend/tests/test_mlx_inference_backend.py b/studio/backend/tests/test_mlx_inference_backend.py
index c515533656b..6190cdd79b8 100644
--- a/studio/backend/tests/test_mlx_inference_backend.py
+++ b/studio/backend/tests/test_mlx_inference_backend.py
@@ -7751,6 +7751,74 @@ def test_a_text_load_batch_row_carries_one_bos_as_the_single_path_does(monkeypat
assert added == ["hi", "hi", "<s>hi", "<s>hi"]
[email protected]("speculative", (None, "draft"))
+def test_a_vision_row_times_its_prefill_wherever_the_batch_runs_it(monkeypatch, speculative):
+ from core.inference import mlx_inference
+
+ engine = _batch_engine(monkeypatch)
+ monkeypatch.setattr(engine, "GenerationRequest", SimpleNamespace, raising = False)
+ monkeypatch.setattr(engine, "SamplingParams", dict, raising = False)
+ monkeypatch.setattr(engine, "BatchRowRefused", RuntimeError, raising = False)
+ monkeypatch.setattr(engine, "vlm_batch_adds_special_tokens", lambda *a: True, raising = False)
+ backend = _vlm_backend(monkeypatch, markers = None)
+ plan = SimpleNamespace(
+ prompt = "hi",
+ images = None,
+ max_tokens = 16,
+ sampling = {},
+ processors = None,
+ think_prefix = "",
+ stream = _vlm_row(backend),
+ )
+ backend._plan_vlm_request = lambda request: plan
+ backend._reads_vision = False
+ session = _open_vision_session([])
+ session.backend, session._adapter_state, session._resumes_rows = backend, None, False
+ session._speculative = speculative
+ clock = [0.0]
+ monkeypatch.setattr(mlx_inference.time, "perf_counter", lambda: clock[0])
+
+ def prefill():
+ clock[0] += 2.0
+
+ steps = []
+
+ def events(waiting = None):
+ steps.append(None)
+ if len(steps) == 1:
+ if not speculative:
+ prefill()
+ yield SimpleNamespace(index = 0, delta = "a", result = None)
+ return
+ clock[0] += 1.0
+ result = SimpleNamespace(
+ finish_reason = "length", prompt_token_count = 1000, token_ids = [1] * 10
+ )
+ vars(result).update(_DRAFTS)
+ yield SimpleNamespace(index = 0, delta = "", result = result)
+
+ def add(request):
+ if speculative:
+ prefill()
+ else:
+ # A plain add() only prepares the row; that time is not its prefill.
+ clock[0] += 3.0
+ return 0
+
+ session.stream = SimpleNamespace(
+ add = add,
+ step = lambda: list(events()),
+ iter_step = events,
+ )
+ session.admit({}, "r")
+ while session.rows_in_flight:
+ list(session.step())
+
+ timings = session.take_stats("r")["timings"]
+ assert timings["prompt_per_second"] == pytest.approx(500.0)
+ assert timings["predicted_per_second"] == pytest.approx(10.0)
+
+
def test_a_resident_vision_row_resumes_from_the_snapshot_store_and_reports_it(monkeypatch):
from core.inference import mlx_inference