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): stop MLX native controls from duplicating in streamed replies (#10905)

Long Yixingde1f4a743409
diff --git a/studio/backend/core/inference/mlx_inference.py b/studio/backend/core/inference/mlx_inference.py
index ad58408e5a0..df73cd57d1b 100644
--- a/studio/backend/core/inference/mlx_inference.py
+++ b/studio/backend/core/inference/mlx_inference.py
@@ -19,6 +19,7 @@ from core.inference.native_tool_tokens import (
closes_an_open_envelope,
decoder_preserves_token,
reasoning_control_tokens,
+ stop_token_text,
)
from core.inference.runtime_context import (
MAX_REQUESTABLE_CONTEXT,
@@ -1094,6 +1095,144 @@ def _mlx_stop_sequences(stop):
return [x for x in ([stop] if isinstance(stop, str) else stop or []) if x]
+def _rebuilt_detokenizer(detokenizer):
+ """A fresh one of ``detokenizer``'s kind, for the versions whose own cannot be copied.
+
+ Only the naive detokenizer needs this, and it is also the only one that keeps the tokenizer it
+ wraps -- SPM and BPE build decoded tables in ``__init__`` and copy cleanly -- so that attribute
+ doubles as the test for "can be rebuilt at all"."""
+ tokenizer = getattr(detokenizer, "_tokenizer", None)
+ if tokenizer is None:
+ return None
+ try:
+ return type(detokenizer)(tokenizer)
+ except Exception as exc: # noqa: BLE001 -- third-party detokenizers vary; caller falls back
+ logger.debug("MLX streaming detokenizer could not be rebuilt (%s)", exc)
+ return None
+
+
+def _mlx_stream_detokenizer(source):
+ """One of ``source``'s own kind, independent of the detokenizer the runtime drives.
+
+ Copying is the fallback, not the rule: it raises on the naive detokenizer, whose ``text`` is a
+ property over an inherited slot. mlx-lm builds one per read, so a second read is already
+ independent; mlx-vlm's processor hands back the one its older versions stream through."""
+ detokenizer = getattr(source, "detokenizer", None)
+ if detokenizer is None:
+ return None
+ independent = getattr(source, "detokenizer", None)
+ if independent is None or independent is detokenizer:
+ try:
+ independent = copy.copy(detokenizer)
+ except Exception as exc: # noqa: BLE001 -- third-party detokenizers vary; caller falls back
+ # mlx-vlm gained ``__copy__`` only at 0.6.0, and below it the naive detokenizer --
+ # what every model resolves to whose decoder is neither SPM nor a top-level ByteLevel
+ # -- cannot be copied at all. Rebuilding one is what that ``__copy__`` does. Giving up
+ # here instead leaves the turn with no detokenizer, and the branch that handles that
+ # can only pass the runtime's text through, so every control the allowlist suppresses
+ # would reach the reply on the whole supported floor.
+ independent = _rebuilt_detokenizer(detokenizer)
+ if independent is None:
+ logger.debug("MLX streaming detokenizer unavailable (%s)", exc)
+ return None
+ try:
+ independent.reset()
+ except Exception as exc: # noqa: BLE001 -- as above
+ logger.debug("MLX streaming detokenizer unavailable (%s)", exc)
+ return None
+ return independent
+
+
+class _MlxStreamedText:
+ """A reply rebuilt from the ids the runtime sampled, for turns carrying native controls.
+
+ A streaming detokenizer holds a piece until the ones after it settle, so a control reaches
+ ``response.text`` inside a later segment, with nothing left to say which characters came from
+ it: deciding suppression there deletes prose that merely spells a control, and re-decoding the
+ id on its own step writes a second copy ahead of the text still held.
+
+ ``generation_tokens``, not a finish reason, says which ids the reply owes -- the supported
+ mlx-vlm floor has no such field, and a turn broken out of on a stop token or the token limit
+ reports a position no yield covered. Hence also ``finish``, run by the caller once the runtime
+ stops yielding.
+
+ Stop ids are held rather than written, since both runtimes stop before their detokenizer sees
+ one, and released only to close a tool envelope. The run is held whole: the ids read as ending
+ a turn come from the model config, which lists more than the one the runtime stopped on.
+
+ Neither a denoised block, which names one id for many, nor a missing detokenizer leaves ids to
+ rebuild from; the runtime's own text stands and the allowlist cannot speak for it."""
+
+ def __init__(self, detokenizer, decoder, tokenizer, stop_ids):
+ self._detokenizer = detokenizer
+ self._decoder = decoder
+ self._tokenizer = tokenizer
+ self._stop_ids = stop_ids
+ self._held_stops = []
+ self._filled = 0
+
+ def feed(self, response) -> str:
+ if self._detokenizer is None:
+ # Every id but the one generation stopped before is already in that text, so only the
+ # last can still be a closer the reply owes.
+ stop_id = getattr(response, "token", None)
+ self._held_stops = (
+ [int(stop_id)] if stop_id is not None and int(stop_id) in self._stop_ids else []
+ )
+ return getattr(response, "text", None) or ""
+ filled = getattr(response, "generation_tokens", None)
+ if filled is not None:
+ filled = int(filled)
+ if filled <= self._filled:
+ return ""
+ several = filled > self._filled + 1
+ self._filled = filled
+ if several:
+ return self._drain() + (getattr(response, "text", None) or "")
+ token_id = getattr(response, "token", None)
+ if token_id is None:
+ return ""
+ token_id = int(token_id)
+ if token_id in self._stop_ids:
+ self._held_stops.append(token_id)
+ return ""
+ if self._decoder.suppresses(token_id):
+ return ""
+ if self._held_stops:
+ # Text followed them, so they ended nothing.
+ held, self._held_stops = self._held_stops, []
+ for stop_id in held:
+ if not self._decoder.suppresses(stop_id):
+ self._detokenizer.add_token(stop_id)
+ self._detokenizer.add_token(token_id)
+ return self._detokenizer.last_segment
+
+ def finish(self, sampled: str) -> str:
+ """The text still held once the runtime stops yielding."""
+ if self._detokenizer is None:
+ return self._closing_stop(sampled)
+ segment = self._drain()
+ return segment + self._closing_stop(sampled + segment)
+
+ def _drain(self) -> str:
+ self._detokenizer.finalize()
+ return self._detokenizer.last_segment
+
+ def _closing_stop(self, settled: str) -> str:
+ held, self._held_stops = self._held_stops, []
+ closing = ""
+ for stop_id in held:
+ if not self._decoder.keeps(stop_id):
+ continue
+ text = stop_token_text(self._tokenizer, stop_id) or ""
+ if settled.endswith(text):
+ continue
+ # Nested markup ends on a closer each, so the run can owe more than one.
+ if closes_an_open_envelope(settled + text, text):
+ closing += text
+ return closing
+
+
def _mlx_stop_cut(text: str, stops) -> tuple[int, bool]:
"""How much of a cumulative reply may be shown, and whether a stop ended it. Text that could
still grow into a sequence is held back, as llama-server holds it: a client cannot unsee a
@@ -3145,6 +3284,16 @@ class MLXInferenceBackend:
if native_token_decoder is not None
else ()
)
+ streamed_text = (
+ _MlxStreamedText(
+ _mlx_stream_detokenizer(self._tokenizer),
+ native_token_decoder,
+ self._tokenizer,
+ stop_token_ids,
+ )
+ if preserve_native_channels
+ else None
+ )
token_ids = []
normalizer = (
make_reasoning_normalizer(
@@ -3213,18 +3362,7 @@ class MLXInferenceBackend:
final_response = response
token_ids.append(response.token)
if preserve_native_channels:
- _tok = native_token_decoder.decode_stream_token(
- response.token, getattr(response, "text", None) or ""
- )
- # Generation ends on a stop id, so this one is trailing. Same rule as
- # the non-reasoning branch: drop it unless it closes a tool envelope.
- if (
- _tok
- and response.token in stop_token_ids
- and not closes_an_open_envelope(sampled + _tok, _tok)
- ):
- _tok = ""
- sampled += _tok
+ sampled += streamed_text.feed(response)
if sequences:
cut, stopped = _mlx_stop_cut(sampled, sequences)
else:
@@ -3306,7 +3444,12 @@ class MLXInferenceBackend:
# The turn's settled text: delivered once for the plain path, as the tail for the native-channel one. Every
# snapshot was matched as it arrived, so a turn no sequence ended owes all of its text, held-back partial
# included.
- if sequences:
+ if streamed_text is not None and not stopped:
+ # The flush can carry a whole sequence: it was held back unmatched, not cleared.
+ sampled += streamed_text.finish(sampled)
+ if sequences:
+ cut, stopped = _mlx_stop_cut(sampled, sequences)
+ if sequences or streamed_text is not None:
if not stopped:
cut = len(sampled)
if normalizer is None:
@@ -3567,19 +3710,14 @@ class MLXInferenceBackend:
elif _rep_active:
vlm_kwargs["repetition_penalty"] = float(repetition_penalty)
- # Same provenance the text path recovers: mlx-vlm's ``response.text`` has dropped the
- # native tool controls, so a genuine wrapped call would reach the parser markerless and
- # be refused. Text-only requests on a VLM come here too, and reasoning delimiters that
- # are special ids need preserving as well.
+ # The allowlist the text path applies: a native tool control or reasoning delimiter stays
+ # so the parser can tell a wrapped call from markerless prose and the snapshot normalizer
+ # can find the thought channel. Text-only requests on a VLM come here too.
vlm_token_decoder = (
NativeToolTokenDecoder(
self._tokenizer,
preserved_tokens = reasoning_control_tokens(vlm_reasoning_markers),
)
- # ``vlm_reasoning_markers`` too, matching the text path: mlx-vlm has already
- # dropped those controls from ``response.text``, so without the decoder the
- # snapshot normaliser never sees the opener or closer and the reasoning surfaces
- # as ordinary answer text on a no-tools request.
if (tools or tool_protocol_active or vlm_reasoning_markers is not None)
and self._tokenizer
else None
@@ -3591,6 +3729,16 @@ class MLXInferenceBackend:
if vlm_token_decoder is not None
else ()
)
+ vlm_detokenizer = (
+ _mlx_stream_detokenizer(self._processor) or _mlx_stream_detokenizer(self._tokenizer)
+ if vlm_token_decoder is not None
+ else None
+ )
+ vlm_streamed_text = (
+ _MlxStreamedText(vlm_detokenizer, vlm_token_decoder, self._tokenizer, vlm_stop_ids)
+ if vlm_token_decoder is not None
+ else None
+ )
session = self._vlm_prompt_cache_session(
_adapter_state, images, prompt, has_video = video is not None
@@ -3644,17 +3792,11 @@ class MLXInferenceBackend:
**vlm_kwargs,
):
final_response = response
- token_text = response.text if hasattr(response, "text") else str(response)
- token_id = getattr(response, "token", None)
- if vlm_token_decoder is not None and token_id is not None:
- # Only a special id is re-decoded; ordinary ids keep mlx-vlm's text.
- _decoded = vlm_token_decoder.decode_stream_token(token_id, token_text)
- # A stop token is dropped unless it closes an open envelope.
+ if vlm_streamed_text is not None:
+ token_text = vlm_streamed_text.feed(response)
+ else:
token_text = (
- ""
- if int(token_id) in vlm_stop_ids
- and not closes_an_open_envelope(sampled + _decoded, _decoded)
- else _decoded
+ response.text if hasattr(response, "text") else str(response)
)
sampled += token_text
if not sequences:
@@ -3669,6 +3811,17 @@ class MLXInferenceBackend:
break
if cancel_event and cancel_event.is_set():
break
+ if vlm_streamed_text is not None and not stopped:
+ tail = vlm_streamed_text.finish(sampled)
+ if tail:
+ sampled += tail
+ if not sequences:
+ yield prefill + sampled
+ else:
+ cut, stopped = _mlx_stop_cut(sampled, sequences)
+ if cut > released:
+ released = cut
+ yield prefill + sampled[:cut]
# As in _generate_text: what was withheld is ordinary text now.
if sequences and not stopped and released < len(sampled):
yield prefill + sampled
diff --git a/studio/backend/core/inference/native_tool_tokens.py b/studio/backend/core/inference/native_tool_tokens.py
index 955343454c7..acea3498911 100644
--- a/studio/backend/core/inference/native_tool_tokens.py
+++ b/studio/backend/core/inference/native_tool_tokens.py
@@ -285,16 +285,12 @@ class NativeToolTokenDecoder:
self._tokenizer, token_ids, self._special_ids, self._tool_ids
)
- def decode_stream_token(self, token_id, fallback_text: str) -> str:
- """Decode one streamed id, using MLX text only for ordinary ids."""
+ def suppresses(self, token_id) -> bool:
token_id = int(token_id)
- if token_id not in self._special_ids:
- return fallback_text
- if token_id not in self._tool_ids:
- return ""
- return _decode_without_special_spacing(
- self._tokenizer, [token_id], skip_special_tokens = False
- )
+ return token_id in self._special_ids and token_id not in self._tool_ids
+
+ def keeps(self, token_id) -> bool:
+ return int(token_id) in self._tool_ids
def __getattr__(self, name):
return getattr(self._tokenizer, name)
diff --git a/studio/backend/tests/data/refactor_guard/patch_targets.json b/studio/backend/tests/data/refactor_guard/patch_targets.json
index 9b9c9494209..954bec4d13e 100644
--- a/studio/backend/tests/data/refactor_guard/patch_targets.json
+++ b/studio/backend/tests/data/refactor_guard/patch_targets.json
@@ -35,13 +35,9 @@
"tests/test_mlx_inference_backend.py",
"tests/test_mlx_inference_backend.py",
"tests/test_mlx_inference_backend.py",
- "tests/test_mlx_inference_backend.py",
"tests/test_mlx_inference_backend.py"
],
"core.inference.chat_template_helpers.detect_think_prefill": [
- "tests/test_mlx_inference_backend.py",
- "tests/test_mlx_inference_backend.py",
- "tests/test_mlx_inference_backend.py",
"tests/test_mlx_inference_backend.py",
"tests/test_mlx_inference_backend.py",
"tests/test_mlx_inference_backend.py",
diff --git a/studio/backend/tests/test_markerless_exec_tool_guard.py b/studio/backend/tests/test_markerless_exec_tool_guard.py
index db6e7044ce8..f86f9f3ad85 100644
--- a/studio/backend/tests/test_markerless_exec_tool_guard.py
+++ b/studio/backend/tests/test_markerless_exec_tool_guard.py
@@ -940,10 +940,8 @@ class _ReasoningChannelTokenizer:
def test_the_mlx_vlm_decoder_keeps_the_reasoning_protocol_delimiters():
- """``decode_stream_token`` drops any special id outside the preserved set, so a VLM turn
- combining tools with a native reasoning protocol loses the delimiters
- ``normalize_reasoning_snapshots`` waits for and emits the reasoning as answer text. The whole
- stream is exercised in ``test_mlx_vlm_keeps_the_reasoning_protocol_delimiters_on_a_tool_turn``."""
+ """Without these delimiters ``normalize_reasoning_snapshots`` emits the reasoning as answer
+ text. The decision is about the id, not the characters it renders as."""
from core.inference.native_tool_tokens import (
NativeToolTokenDecoder,
reasoning_control_tokens,
@@ -955,12 +953,12 @@ def test_the_mlx_vlm_decoder_keeps_the_reasoning_protocol_delimiters():
with_markers = NativeToolTokenDecoder(
tokenizer, preserved_tokens = reasoning_control_tokens(markers)
)
- for token_id, token in ((1, "<|channel>"), (2, "<channel|>")):
- assert without.decode_stream_token(token_id, token) == ""
- assert with_markers.decode_stream_token(token_id, token) == token
+ for token_id in (1, 2): # <|channel>, <channel|>
+ assert without.suppresses(token_id) and not without.keeps(token_id)
+ assert with_markers.keeps(token_id) and not with_markers.suppresses(token_id)
# Neither the tool control nor the suppression of an ordinary special token moves.
- assert with_markers.decode_stream_token(3, "<tool_call>") == "<tool_call>"
- assert with_markers.decode_stream_token(4, "<eos>") == ""
+ assert with_markers.keeps(3) and not with_markers.suppresses(3) # <tool_call>
+ assert with_markers.suppresses(4) and not with_markers.keeps(4) # <eos>
def test_a_gemma_peer_behind_a_blocked_json_object_is_held():
diff --git a/studio/backend/tests/test_mlx_inference_backend.py b/studio/backend/tests/test_mlx_inference_backend.py
index 935c3e1809a..342eb547b9f 100644
--- a/studio/backend/tests/test_mlx_inference_backend.py
+++ b/studio/backend/tests/test_mlx_inference_backend.py
@@ -1220,7 +1220,36 @@ def test_mlx_tool_turn_preserves_native_gemma_special_token_ids(monkeypatch):
assert [call["function"]["name"] for call in calls] == ["terminal"]
-def test_mlx_reasoning_tool_turn_filters_special_ids_by_provenance(monkeypatch):
+def test_mlx_reasoning_tool_turn_keeps_provenance_across_buffered_segments(monkeypatch):
+ """A kept control has to reach the parser once, where the model wrote it, and a suppressed one
+ must not ride along inside the segment that carries it."""
+ # Built before the fake mlx modules land, so the real detokenizer still imports.
+ turn = _SpmTurn(
+ (
+ "<|channel>",
+ "thought",
+ "\n",
+ "weighing",
+ "<pad>",
+ "▁it",
+ "<channel|>",
+ "<|tool_call>",
+ "call:terminal{command:id}",
+ "<tool_call|>",
+ "<eos>",
+ ),
+ specials = (
+ "<|channel>",
+ "<channel|>",
+ "<|tool_call>",
+ "<tool_call|>",
+ "<pad>",
+ "<eos>",
+ ),
+ eos = "<eos>",
+ chat_template = "x",
+ ends = "stop",
+ )
_install_fake_mlx(monkeypatch)
from core.inference import mlx_inference
from core.inference.tool_call_parser import parse_tool_calls_from_text
@@ -1243,65 +1272,28 @@ def test_mlx_reasoning_tool_turn_filters_special_ids_by_provenance(monkeypatch):
mlx_lm_sample = types.ModuleType("mlx_lm.sample_utils")
mlx_lm_sample.make_sampler = lambda **_kwargs: object()
mlx_lm_sample.make_logits_processors = lambda **_kwargs: None
-
- pieces = {
- 1: "<|channel>",
- 2: "thought\n",
- 3: "reasoned",
- 4: "<channel|>",
- 5: "<|tool_call>",
- 6: "call:terminal{command:id}",
- 7: "<tool_call|>",
- 8: "<eos>",
- }
- special_ids = {1, 4, 5, 7, 8}
-
- def _stream_generate(_model, _tokenizer, **_kwargs):
- for token_id, piece in pieces.items():
- # Special-token text is deliberately wrong: production must trust
- # response.token provenance and decode that id itself.
- text = "untrusted-special-text" if token_id in special_ids else piece
- yield SimpleNamespace(token = token_id, text = text)
-
- mlx_lm_pkg.stream_generate = _stream_generate
+ mlx_lm_pkg.stream_generate = lambda _model, _tokenizer, **_kwargs: turn.stream()
monkeypatch.setitem(sys.modules, "mlx_lm", mlx_lm_pkg)
monkeypatch.setitem(sys.modules, "mlx_lm.sample_utils", mlx_lm_sample)
- class _Tokenizer:
- chat_template = "x"
- all_special_ids = list(special_ids)
- all_special_tokens = [pieces[token_id] for token_id in special_ids]
-
- def convert_ids_to_tokens(self, token_id):
- return pieces[token_id]
-
- def decode(
- self,
- ids,
- *,
- skip_special_tokens = False,
- **_kwargs,
- ):
- skipped = special_ids if skip_special_tokens else set()
- return "".join(pieces[int(token_id)] for token_id in ids if token_id not in skipped)
-
backend = mlx_inference.MLXInferenceBackend()
backend._model = object()
- backend._tokenizer = _Tokenizer()
+ backend._tokenizer = turn
backend._is_vlm = False
snapshots = list(
backend.generate_chat_response(
messages = [{"role": "user", "content": "run it"}],
tools = [{"type": "function", "function": {"name": "terminal"}}],
- max_new_tokens = len(pieces),
+ max_new_tokens = len(turn.ids),
)
)
assert snapshots[-1] == (
- "<think>reasoned</think><|tool_call>call:terminal{command:id}<tool_call|>"
+ "<think>weighing it</think><|tool_call>call:terminal{command:id}<tool_call|>"
)
- assert "<eos>" not in snapshots[-1]
- assert "untrusted-special-text" not in snapshots[-1]
+ # The suppressed pieces rode in on ordinary text; the kept one is written once, not twice.
+ assert "<pad>" not in snapshots[-1] and "<eos>" not in snapshots[-1]
+ assert snapshots[-1].count("<|tool_call>") == 1
calls = parse_tool_calls_from_text(snapshots[-1], enabled_tool_names = {"terminal"})
assert [call["function"]["name"] for call in calls] == ["terminal"]
@@ -4553,132 +4545,121 @@ def test_the_mlx_mcp_snapshot_is_taken_under_the_same_guard_the_gguf_count_uses(
assert guard < snapshot, "the guard must be held across the snapshot, not after it"
-def test_mlx_vlm_recovers_native_tool_tokens_like_the_text_path(monkeypatch):
- """mlx-vlm's ``response.text`` has already dropped the native tool controls. Without recovering
- them the wrapper never reaches the parser, so a genuine
- ``<|tool_call>call:terminal{..}<tool_call|>`` arrives markerless and the execution guard
- refuses it. Text-only requests on a model classified as a VLM use this route too."""
- from core.inference import mlx_inference
-
- MLXInferenceBackend = mlx_inference.MLXInferenceBackend
-
- @contextmanager
- def _adapter_state(_model, _state):
- yield
-
- monkeypatch.setattr(mlx_inference, "_temporary_mlx_adapter_state", _adapter_state)
- monkeypatch.setattr(
- "core.inference.chat_template_helpers.detect_think_prefill", lambda *_a, **_k: ""
- )
-
- class _Tok:
- # 7/8 are the Gemma wrapper; mlx-vlm reports them as empty text.
- _IDS = {7: "<|tool_call>", 8: "<tool_call|>", 9: "<eos>"}
- all_special_ids = tuple(_IDS)
+def _uncopyable_naive_detokenizer(detokenizers):
+ """mlx-vlm's naive detokenizer as it behaves BELOW 0.6.0, which is where ``__copy__`` arrived.
- def convert_ids_to_tokens(self, token_id):
- return self._IDS[token_id]
+ Pinning the behaviour rather than the installed version: on 0.6.0 and later ``copy.copy``
+ succeeds, so a test that let the real class decide passed only on an older wheel and said
+ nothing about the branch it meant to cover."""
- def decode(
- self,
- token_ids,
- skip_special_tokens = False,
- **_k,
- ):
- return "".join(
- self._IDS.get(i, "")
- for i in token_ids
- if not (skip_special_tokens and i in self.all_special_ids)
+ class _Uncopyable(detokenizers.NaiveStreamingDetokenizer):
+ def __copy__(self):
+ raise AttributeError(
+ "property 'text' of 'NaiveStreamingDetokenizer' object has no setter"
)
- prompt_utils = SimpleNamespace(
- MODEL_CONFIG = {"deepseek_vl_v2": object()},
- apply_chat_template = lambda *_a, **_k: "<image> model-aware",
- )
- mlx_vlm = types.ModuleType("mlx_vlm")
- mlx_vlm.prompt_utils = prompt_utils
+ return _Uncopyable
- def _vlm_stream(*_a, **_k):
- for token_id, text in (
- (7, ""),
- (None, 'call:terminal{command:"id"}'),
- (8, ""),
- ):
- yield SimpleNamespace(text = text, token = token_id, prompt_tokens = 3, generation_tokens = 1)
- mlx_vlm.stream_generate = _vlm_stream
- monkeypatch.setitem(sys.modules, "mlx_vlm", mlx_vlm)
- monkeypatch.setattr(
- "core.inference.chat_template_helpers.apply_chat_template_for_generation",
- lambda _t, _m, **_k: "<image> model-aware",
- )
+class _SpmTurn:
+ """One generated turn, standing in for both the tokenizer and the runtime's detokenizer.
- backend = MLXInferenceBackend()
- backend._model = SimpleNamespace(config = {"model_type": "deepseek_vl_v2"})
- backend._processor = SimpleNamespace(tokenizer = _Tok())
- backend._tokenizer = _Tok()
- args = ([{"role": "user", "content": [{"type": "image"}]}], object(), 0, 1, 0, 0, 1, 1, None)
+ The SPM detokenizer releases text only when a piece begins with the SPM space marker, so a
+ control reaches ``response.text`` inside a later segment. Fakes handing each piece back on its
+ own step hide that, and under that shape re-decoding a control on its own step reads correct."""
- snapshots = list(
- backend._generate_vlm(
- *args,
- _adapter_state = False,
- tools = [{"type": "function", "function": {"name": "terminal"}}],
+ def __init__(
+ self,
+ pieces,
+ specials = (),
+ eos = None,
+ chat_template = None,
+ ends = "exhausted",
+ reports_finish_reason = True,
+ block = False,
+ shares_detokenizer = False,
+ detokenizer_class = "spm",
+ ):
+ detokenizers = pytest.importorskip("mlx_vlm.tokenizer_utils")
+ self.vocab = {}
+ self.ids = [self.vocab.setdefault(piece, len(self.vocab)) for piece in pieces]
+ self._by_id = {token_id: piece for piece, token_id in self.vocab.items()}
+ self.all_special_ids = tuple(self.vocab[piece] for piece in specials)
+ self.all_special_tokens = tuple(specials)
+ self.chat_template = chat_template
+ self._ends = ends
+ self._reports_finish_reason = reports_finish_reason
+ self._block = block
+ self._shares_detokenizer = shares_detokenizer
+ if eos is not None:
+ eos_ids = [self.vocab[piece] for piece in ((eos,) if isinstance(eos, str) else eos)]
+ self.eos_token_ids = tuple(eos_ids)
+ self.eos_token_id = eos_ids[-1]
+ self.detokenizer = (
+ _uncopyable_naive_detokenizer(detokenizers)(self)
+ if detokenizer_class == "naive"
+ else detokenizers.SPMStreamingDetokenizer(self, trim_space = False)
)
- )
- assert snapshots[-1] == '<|tool_call>call:terminal{command:"id"}<tool_call|>'
-
-
-def test_mlx_drops_a_trailing_stop_token_from_the_preserved_decode(monkeypatch):
- """``skip_special_tokens`` used to swallow the stop id; the decoder keeps controls. Some
- runtimes stop on an allowlisted one (TML Inkling's ``<|end_message|>``), so without filtering
- it an ordinary answer is delivered with the protocol marker appended."""
- from core.inference import mlx_inference
-
- MLXInferenceBackend = mlx_inference.MLXInferenceBackend
-
- class _Tok:
- _IDS = {5: "<|end_message|>"}
- all_special_ids = (5,)
- eos_token_id = 5
-
- def convert_ids_to_tokens(self, token_id):
- return self._IDS[token_id]
- def decode(
- self,
- token_ids,
- skip_special_tokens = False,
- **_kwargs,
- ):
- return "".join(
- ""
- if (skip_special_tokens and i in self.all_special_ids)
- else self._IDS.get(i, chr(i))
- for i in token_ids
- )
+ def convert_ids_to_tokens(self, token_id):
+ return self._by_id[token_id]
- tok = _Tok()
- decoder = mlx_inference.NativeToolTokenDecoder(tok)
- stop_ids = mlx_inference._mlx_stop_token_ids(tok, None)
- assert 5 in stop_ids, stop_ids
+ def decode(
+ self,
+ token_ids,
+ skip_special_tokens = False,
+ **_kwargs,
+ ):
+ return "".join(
+ "" if (skip_special_tokens and i in self.all_special_ids) else self._by_id.get(i, "")
+ for i in token_ids
+ ).replace("▁", " ")
+
+ def stream(self, *_a, **_k):
+ """A segment per step from the runtime's own detokenizer, then the flush ending the turn.
+
+ ``ends`` picks that last yield's shape: exhausting the sampler repeats the position already
+ reported, while breaking out reports one never yielded -- on a stop token the detokenizer
+ never saw, or on the token limit, which mlx-lm reaches after feeding it.
+ ``reports_finish_reason`` is off for the supported mlx-vlm floor, which has no such field."""
+ if self._block:
+ # What mlx-vlm's diffusion generators report for a denoised block: every id at once,
+ # the count grown by all of them, only the last one named, and the block's own text.
+ yield self._yielded(self.decode(self.ids), self.ids[-1], len(self.ids), "stop")
+ return
+ # mlx-vlm's supported floor streams through the processor's own instance rather than a
+ # copy of it, so anything else driving that instance corrupts both.
+ detokenizer = self.detokenizer if self._shares_detokenizer else copy.copy(self.detokenizer)
+ detokenizer.reset()
+ streamed = self.ids if self._ends == "exhausted" else self.ids[:-1]
+ for position, token_id in enumerate(streamed, start = 1):
+ detokenizer.add_token(token_id, skip_special_token_ids = [])
+ yield self._yielded(detokenizer.last_segment, token_id, position, None)
+ if self._ends == "length":
+ detokenizer.add_token(self.ids[-1], skip_special_token_ids = [])
+ detokenizer.finalize()
+ yield self._yielded(
+ detokenizer.last_segment,
+ self.ids[-1],
+ len(self.ids),
+ "stop" if self._ends == "stop" else "length",
+ )
- ids = [ord("h"), ord("i"), 5]
- assert decoder.decode(ids) == "hi<|end_message|>"
- trimmed = ids[:-1] if ids[-1] in stop_ids else ids
- assert decoder.decode(trimmed) == "hi"
- # The marker still survives when it is not the turn's final token.
- assert decoder.decode([ord("h"), 5, ord("i")]) == "h<|end_message|>i"
+ def _yielded(self, text, token_id, position, finish_reason):
+ fields = dict(text = text, token = token_id, prompt_tokens = 3, generation_tokens = position)
+ if self._reports_finish_reason:
+ fields["finish_reason"] = finish_reason
+ return SimpleNamespace(**fields)
-def test_mlx_vlm_does_not_leak_a_preserved_stop_token(monkeypatch):
- """The VLM path appends each decoded token straight into the snapshot. So an allowlisted control
- used as the runtime EOS would trail every ordinary answer; ``_generate_text`` drops it at its
- final re-decode and this route needs the same."""
+def _run_spm_vlm_turn(
+ monkeypatch,
+ turn,
+ tool_name = "get_weather",
+ stop = None,
+):
from core.inference import mlx_inference
- MLXInferenceBackend = mlx_inference.MLXInferenceBackend
-
@contextmanager
def _adapter_state(_model, _state):
yield
@@ -4687,280 +4668,359 @@ def test_mlx_vlm_does_not_leak_a_preserved_stop_token(monkeypatch):
monkeypatch.setattr(
"core.inference.chat_template_helpers.detect_think_prefill", lambda *_a, **_k: ""
)
-
- class _Tok:
- _IDS = {5: "<|end_message|>"}
- all_special_ids = (5,)
- eos_token_id = 5
-
- def convert_ids_to_tokens(self, token_id):
- return self._IDS[token_id]
-
- def decode(
- self,
- token_ids,
- skip_special_tokens = False,
- **_kwargs,
- ):
- return "".join(
- "" if (skip_special_tokens and i in self.all_special_ids) else self._IDS.get(i, "")
- for i in token_ids
- )
-
- prompt_utils = SimpleNamespace(
+ mlx_vlm = types.ModuleType("mlx_vlm")
+ mlx_vlm.prompt_utils = SimpleNamespace(
MODEL_CONFIG = {"deepseek_vl_v2": object()},
apply_chat_template = lambda *_a, **_k: "<image> model-aware",
)
- mlx_vlm = types.ModuleType("mlx_vlm")
- mlx_vlm.prompt_utils = prompt_utils
-
- def _vlm_stream(*_a, **_k):
- for token_id, text in ((None, "hi"), (5, "")):
- yield SimpleNamespace(text = text, token = token_id, prompt_tokens = 3, generation_tokens = 1)
-
- mlx_vlm.stream_generate = _vlm_stream
+ mlx_vlm.stream_generate = turn.stream
monkeypatch.setitem(sys.modules, "mlx_vlm", mlx_vlm)
monkeypatch.setattr(
"core.inference.chat_template_helpers.apply_chat_template_for_generation",
lambda _t, _m, **_k: "<image> model-aware",
)
- backend = MLXInferenceBackend()
+ backend = mlx_inference.MLXInferenceBackend()
backend._model = SimpleNamespace(config = {"model_type": "deepseek_vl_v2"})
- backend._processor = SimpleNamespace(tokenizer = _Tok())
- backend._tokenizer = _Tok()
- args = ([{"role": "user", "content": [{"type": "image"}]}], object(), 0, 1, 0, 0, 1, 1, None)
-
- snapshots = list(
+ backend._processor = SimpleNamespace(
+ tokenizer = turn, chat_template = turn.chat_template, detokenizer = turn.detokenizer
+ )
+ backend._tokenizer = turn
+ return list(
backend._generate_vlm(
- *args,
+ [{"role": "user", "content": [{"type": "image"}]}],
+ object(),
+ 0,
+ 1,
+ 0,
+ 0,
+ 32,
+ 1,
+ None,
_adapter_state = False,
- tools = [{"type": "function", "function": {"name": "terminal"}}],
+ tools = [{"type": "function", "function": {"name": tool_name}}],
+ stop = stop,
)
)
- assert snapshots[-1] == "hi"
-
-def test_mlx_vlm_keeps_a_stop_token_that_closes_a_tool_envelope(monkeypatch):
- """The suppression is for a turn with NO tool markup. TML Inkling uses ``<|end_message|>`` both
- as the required close for ``<|content_invoke_tool_json|>`` and as a possible EOS, and strict
- parsing rejects the call without it, so blanking it unconditionally loses a complete call."""
- from core.inference import mlx_inference
- MLXInferenceBackend = mlx_inference.MLXInferenceBackend
+def test_mlx_vlm_keeps_a_native_tool_wrapper_exactly_once(monkeypatch):
+ """Strict parsing needs the wrapper to tell a native call from markerless prose, and a second
+ copy is no better: the parser reads the pair as a malformed envelope."""
+ turn = _SpmTurn(
+ (
+ "<|tool_call>",
+ "call",
+ ":",
+ "terminal",
+ "{",
+ "command",
+ ":",
+ '"id"',
+ "}",
+ "<tool_call|>",
+ ),
+ specials = ("<|tool_call>", "<tool_call|>"),
+ )
+ snapshots = _run_spm_vlm_turn(monkeypatch, turn, tool_name = "terminal")
+ assert snapshots[-1] == '<|tool_call>call:terminal{command:"id"}<tool_call|>'
- @contextmanager
- def _adapter_state(_model, _state):
- yield
- monkeypatch.setattr(mlx_inference, "_temporary_mlx_adapter_state", _adapter_state)
- monkeypatch.setattr(
- "core.inference.chat_template_helpers.detect_think_prefill", lambda *_a, **_k: ""
+def test_mlx_vlm_keeps_prose_that_spells_a_suppressed_control(monkeypatch):
+ """Suppression is about where a control came from, not what the text spells."""
+ turn = _SpmTurn(
+ ("Write", "▁`", "<", "pad", ">", "`", "▁literally.", "<pad>"),
+ specials = ("<pad>",),
)
+ assert _run_spm_vlm_turn(monkeypatch, turn)[-1] == "Write `<pad>` literally."
- class _Tok:
- _IDS = {5: "<|end_message|>"}
- all_special_ids = (5,)
- eos_token_id = 5
- def convert_ids_to_tokens(self, token_id):
- return self._IDS[token_id]
+def test_mlx_vlm_does_not_leak_a_preserved_stop_token(monkeypatch):
+ """This path appends each settled piece straight into the snapshot, so an allowlisted control
+ used as the runtime EOS would trail every ordinary answer."""
+ turn = _SpmTurn(
+ ("Hi", "▁there", "<|end_message|>"),
+ specials = ("<|end_message|>",),
+ eos = "<|end_message|>",
+ ends = "stop",
+ )
+ assert _run_spm_vlm_turn(monkeypatch, turn)[-1] == "Hi there"
- def decode(
- self,
- token_ids,
- skip_special_tokens = False,
- **_kwargs,
- ):
- return "".join(
- "" if (skip_special_tokens and i in self.all_special_ids) else self._IDS.get(i, "")
- for i in token_ids
- )
- prompt_utils = SimpleNamespace(
- MODEL_CONFIG = {"deepseek_vl_v2": object()},
- apply_chat_template = lambda *_a, **_k: "<image> model-aware",
+def test_mlx_vlm_settles_a_turn_a_runtime_ends_without_saying_so(monkeypatch):
+ """mlx-vlm gained a finish reason well after the floor Studio supports."""
+ turn = _SpmTurn(
+ ("Hi", "▁there", "<|end_message|>"),
+ specials = ("<|end_message|>",),
+ eos = "<|end_message|>",
+ ends = "stop",
+ reports_finish_reason = False,
)
- mlx_vlm = types.ModuleType("mlx_vlm")
- mlx_vlm.prompt_utils = prompt_utils
+ assert _run_spm_vlm_turn(monkeypatch, turn)[-1] == "Hi there"
+
+def test_mlx_vlm_keeps_a_stop_token_that_closes_a_tool_envelope(monkeypatch):
+ """TML Inkling uses ``<|end_message|>`` both as that closer and as a possible EOS, and the
+ runtime stops before its detokenizer sees that id."""
+ turn = _SpmTurn(
+ (
+ "<|content_invoke_tool_json|>",
+ '{"name":',
+ '▁"get_weather",',
+ '▁"args":',
+ "▁{}}",
+ "<|end_message|>",
+ ),
+ specials = ("<|content_invoke_tool_json|>", "<|end_message|>"),
+ eos = "<|end_message|>",
+ ends = "stop",
+ )
envelope = '<|content_invoke_tool_json|>{"name": "get_weather", "args": {}}'
+ assert _run_spm_vlm_turn(monkeypatch, turn)[-1] == envelope + "<|end_message|>"
- def _vlm_stream(*_a, **_k):
- for token_id, text in ((None, envelope), (5, "")):
- yield SimpleNamespace(text = text, token = token_id, prompt_tokens = 3, generation_tokens = 1)
- mlx_vlm.stream_generate = _vlm_stream
- monkeypatch.setitem(sys.modules, "mlx_vlm", mlx_vlm)
- monkeypatch.setattr(
- "core.inference.chat_template_helpers.apply_chat_template_for_generation",
- lambda _t, _m, **_k: "<image> model-aware",
+def test_mlx_vlm_keeps_a_stop_id_the_runtime_generated_straight_through(monkeypatch):
+ """``_mlx_stop_token_ids`` reads the config before the tokenizer attribute and the two disagree
+ on some repos, so a held id can be one the runtime generated and carried on from."""
+ turn = _SpmTurn(
+ (
+ "<|content_invoke_tool_json|>",
+ '{"name":',
+ '▁"get_weather",',
+ '▁"args":',
+ "▁{}}",
+ "<|end_message|>",
+ "<eos>",
+ "▁Anything",
+ "▁else?",
+ ),
+ specials = ("<|content_invoke_tool_json|>", "<|end_message|>", "<eos>"),
+ eos = ("<|end_message|>", "<eos>"),
)
+ envelope = '<|content_invoke_tool_json|>{"name": "get_weather", "args": {}}'
+ snapshot = _run_spm_vlm_turn(monkeypatch, turn)[-1]
+ assert snapshot == envelope + "<|end_message|> Anything else?"
- backend = MLXInferenceBackend()
- backend._model = SimpleNamespace(config = {"model_type": "deepseek_vl_v2"})
- backend._processor = SimpleNamespace(tokenizer = _Tok())
- backend._tokenizer = _Tok()
- args = ([{"role": "user", "content": [{"type": "image"}]}], object(), 0, 1, 0, 0, 1, 1, None)
- snapshots = list(
- backend._generate_vlm(
- *args,
- _adapter_state = False,
- tools = [{"type": "function", "function": {"name": "get_weather"}}],
- )
+def test_mlx_vlm_drops_a_generated_stop_id_outside_the_allowlist(monkeypatch):
+ """Carrying on past a stop id says it ended nothing, not that the reply may show it."""
+ turn = _SpmTurn(
+ ("Nearly", "▁done", "<eos>", "▁and", "▁back"),
+ specials = ("<eos>",),
+ eos = "<eos>",
)
- assert snapshots[-1] == envelope + "<|end_message|>"
-
+ assert _run_spm_vlm_turn(monkeypatch, turn)[-1] == "Nearly done and back"
-def test_mlx_vlm_drops_an_orphan_closer_that_opened_nothing(monkeypatch):
- """ "the turn mentions a marker" is not enough to keep a closer. An ordinary answer that merely
- writes ``[ARGS]`` opened no envelope, so a trailing ``<|end_message|>`` is orphan markup and
- must not reach the user."""
- from core.inference import mlx_inference
- MLXInferenceBackend = mlx_inference.MLXInferenceBackend
+def test_mlx_vlm_keeps_a_closer_a_later_stop_id_followed(monkeypatch):
+ """The config lists more ids than the runtime stopped on, so a closer can be followed by
+ another stop id."""
+ turn = _SpmTurn(
+ (
+ "<|content_invoke_tool_json|>",
+ '{"name":"get_weather","args":{}}',
+ "<|end_message|>",
+ "<eos>",
+ ),
+ specials = ("<|content_invoke_tool_json|>", "<|end_message|>", "<eos>"),
+ eos = ("<|end_message|>", "<eos>"),
+ ends = "stop",
+ )
+ envelope = '<|content_invoke_tool_json|>{"name":"get_weather","args":{}}'
+ assert _run_spm_vlm_turn(monkeypatch, turn)[-1] == envelope + "<|end_message|>"
- @contextmanager
- def _adapter_state(_model, _state):
- yield
- monkeypatch.setattr(mlx_inference, "_temporary_mlx_adapter_state", _adapter_state)
- monkeypatch.setattr(
- "core.inference.chat_template_helpers.detect_think_prefill", lambda *_a, **_k: ""
+def test_mlx_vlm_keeps_every_closer_a_nested_call_needs(monkeypatch):
+ """Half-closed is as rejectable as unclosed."""
+ turn = _SpmTurn(
+ (
+ "<tool_call>",
+ "<function=get_weather>",
+ "<parameter=city>",
+ "Paris",
+ "</parameter>",
+ "</function>",
+ "</tool_call>",
+ ),
+ specials = ("<tool_call>", "</parameter>", "</function>", "</tool_call>"),
+ eos = ("</parameter>", "</function>", "</tool_call>"),
+ ends = "stop",
)
+ call = "<tool_call><function=get_weather><parameter=city>Paris"
+ snapshot = _run_spm_vlm_turn(monkeypatch, turn)[-1]
+ assert snapshot == call + "</parameter></function></tool_call>"
- class _Tok:
- _IDS = {5: "<|end_message|>"}
- all_special_ids = (5,)
- eos_token_id = 5
- def convert_ids_to_tokens(self, token_id):
- return self._IDS[token_id]
+def test_mlx_stream_detokenizer_handles_one_that_cannot_be_copied():
+ """Its ``text`` is a property over an inherited slot, so ``copy.copy`` raises. It is also the
+ one mlx-lm builds per read, which is what makes a second read independent."""
+ detokenizers = pytest.importorskip("mlx_lm.tokenizer_utils")
+ from core.inference.mlx_inference import _mlx_stream_detokenizer
- def decode(
- self,
- token_ids,
- skip_special_tokens = False,
- **_kwargs,
- ):
- return "".join(
- "" if (skip_special_tokens and i in self.all_special_ids) else self._IDS.get(i, "")
- for i in token_ids
- )
+ class _Tok:
+ def decode(self, token_ids):
+ return "".join(f"<{token_id}>" for token_id in token_ids)
- prompt_utils = SimpleNamespace(
- MODEL_CONFIG = {"deepseek_vl_v2": object()},
- apply_chat_template = lambda *_a, **_k: "<image> model-aware",
- )
- mlx_vlm = types.ModuleType("mlx_vlm")
- mlx_vlm.prompt_utils = prompt_utils
+ class _PerRead:
+ """Stands in for ``TokenizerWrapper``, whose ``detokenizer`` is a property."""
- def _vlm_stream(*_a, **_k):
- for token_id, text in ((None, "The [ARGS] marker is neat"), (5, "")):
- yield SimpleNamespace(text = text, token = token_id, prompt_tokens = 3, generation_tokens = 1)
+ @property
+ def detokenizer(self):
+ return detokenizers.NaiveStreamingDetokenizer(_Tok())
+
+ source = _PerRead()
+ with pytest.raises(AttributeError):
+ copy.copy(source.detokenizer)
+ own = _mlx_stream_detokenizer(source)
+ assert own is not None
+ own.add_token(7)
+ own.finalize()
+ assert own.text == "<7>"
+
+
+def test_mlx_stream_detokenizer_rebuilds_the_one_a_retained_source_cannot_copy():
+ """mlx-vlm's processor hands back a single retained instance, and below 0.6.0 -- which is
+ where ``__copy__`` arrives -- copying the naive one raises. Falling back to no detokenizer
+ there would pass the runtime's text through unfiltered for the whole turn, so a control the
+ allowlist suppresses would reach the reply."""
+ detokenizers = pytest.importorskip("mlx_vlm.tokenizer_utils")
+ from core.inference.mlx_inference import _mlx_stream_detokenizer
- mlx_vlm.stream_generate = _vlm_stream
- monkeypatch.setitem(sys.modules, "mlx_vlm", mlx_vlm)
+ class _Tok:
+ def decode(self, token_ids, **_kwargs):
+ return "".join(f"<{token_id}>" for token_id in token_ids)
+
+ class _Retained:
+ def __init__(self):
+ self.detokenizer = _uncopyable_naive_detokenizer(detokenizers)(_Tok())
+
+ source = _Retained()
+ with pytest.raises(AttributeError):
+ copy.copy(source.detokenizer)
+
+ own = _mlx_stream_detokenizer(source)
+ assert own is not None, "a detokenizer that cannot be copied can still be rebuilt"
+ assert own is not source.detokenizer
+
+ own.add_token(7)
+ own.finalize()
+ assert own.text == "<7>"
+ # and driving ours left the one the runtime streams through alone
+ source.detokenizer.add_token(9)
+ source.detokenizer.finalize()
+ assert source.detokenizer.text == "<9>"
+
+
+def test_mlx_vlm_suppresses_a_control_on_a_runtime_whose_detokenizer_cannot_be_copied(monkeypatch):
+ """The end of that: on the supported mlx-vlm floor the reply still owes only what the
+ allowlist keeps, rather than every control the runtime rendered."""
+ turn = _SpmTurn(
+ ("▁Hello", "<pad>", "▁world"),
+ specials = ("<pad>",),
+ ends = "exhausted",
+ shares_detokenizer = True,
+ detokenizer_class = "naive",
+ )
+ assert _run_spm_vlm_turn(monkeypatch, turn)[-1] == " Hello world"
+
+
[email protected]("ends", ("stop", "exhausted"))
+def test_mlx_vlm_owes_a_closer_exactly_once_without_a_detokenizer_of_its_own(monkeypatch, ends):
+ """A runtime that stopped before rendering the stop id never wrote it; one that did has already
+ said it."""
+ turn = _SpmTurn(
+ (
+ "<|content_invoke_tool_json|>",
+ '{"name":"get_weather","args":{}}',
+ "<|end_message|>",
+ ),
+ specials = ("<|content_invoke_tool_json|>", "<|end_message|>"),
+ eos = "<|end_message|>",
+ ends = ends,
+ )
monkeypatch.setattr(
- "core.inference.chat_template_helpers.apply_chat_template_for_generation",
- lambda _t, _m, **_k: "<image> model-aware",
+ "core.inference.mlx_inference._mlx_stream_detokenizer", lambda _source: None
)
+ envelope = '<|content_invoke_tool_json|>{"name":"get_weather","args":{}}'
+ assert _run_spm_vlm_turn(monkeypatch, turn)[-1] == envelope + "<|end_message|>"
- backend = MLXInferenceBackend()
- backend._model = SimpleNamespace(config = {"model_type": "deepseek_vl_v2"})
- backend._processor = SimpleNamespace(tokenizer = _Tok())
- backend._tokenizer = _Tok()
- args = ([{"role": "user", "content": [{"type": "image"}]}], object(), 0, 1, 0, 0, 1, 1, None)
- snapshots = list(
- backend._generate_vlm(
- *args,
- _adapter_state = False,
- tools = [{"type": "function", "function": {"name": "get_weather"}}],
- )
+ "trailing, expected_tail", ((("▁Done.",), " Done."), (("\n", "Done."), "\nDone."))
+)
+def test_mlx_vlm_does_not_repeat_a_closer_the_runtime_already_wrote(
+ monkeypatch, trailing, expected_tail
+):
+ """The runtime's text already carries every control but the one it stopped before -- whether an
+ SPM space marker released it mid-turn or the whole reply arrived in the final flush."""
+ turn = _SpmTurn(
+ ("<tool_call>", '{"name":"get_weather","arguments":{}}', "</tool_call>")
+ + trailing
+ + ("<eos>",),
+ specials = ("<tool_call>", "</tool_call>", "<eos>"),
+ eos = ("</tool_call>", "<eos>"),
+ ends = "stop",
)
- assert snapshots[-1] == "The [ARGS] marker is neat"
-
-
-def test_mlx_vlm_keeps_the_reasoning_protocol_delimiters_on_a_tool_turn(monkeypatch):
- """The VLM decoder drops every special id outside its preserved set. A turn that combines tools
- with a native reasoning protocol therefore loses the delimiters
- ``normalize_reasoning_snapshots`` is waiting for, and the thought is emitted as ordinary
- answer text. The text path already passes the protocol's controls."""
- from core.inference import mlx_inference
-
- MLXInferenceBackend = mlx_inference.MLXInferenceBackend
-
- @contextmanager
- def _adapter_state(_model, _state):
- yield
-
- monkeypatch.setattr(mlx_inference, "_temporary_mlx_adapter_state", _adapter_state)
monkeypatch.setattr(
- "core.inference.chat_template_helpers.detect_think_prefill", lambda *_a, **_k: ""
+ "core.inference.mlx_inference._mlx_stream_detokenizer", lambda _source: None
)
+ call = '<tool_call>{"name":"get_weather","arguments":{}}</tool_call>'
+ assert _run_spm_vlm_turn(monkeypatch, turn)[-1] == call + expected_tail
- class _Tok:
- # The delimiters are special-token ids, as they are in the real vocabulary.
- _IDS = {1: "<|channel>", 2: "<channel|>", 9: "<eos>"}
- all_special_ids = (1, 2, 9)
- eos_token_id = 9
- chat_template = "...<|channel>thought\n...<channel|>"
- def convert_ids_to_tokens(self, token_id):
- return self._IDS[token_id]
+def test_mlx_vlm_does_not_drive_the_detokenizer_the_runtime_streams_through(monkeypatch):
+ """The supported mlx-vlm floor streams through the processor's own instance."""
+ turn = _SpmTurn(
+ ("<|channel>", "thought", "\n", "weighing", "▁it", "<channel|>", "▁the", "▁answer"),
+ specials = ("<|channel>", "<channel|>"),
+ chat_template = "...<|channel>thought\n...<channel|>",
+ shares_detokenizer = True,
+ )
+ assert _run_spm_vlm_turn(monkeypatch, turn)[-1] == "<think>weighing it</think> the answer"
- def decode(
- self,
- token_ids,
- skip_special_tokens = False,
- **_kwargs,
- ):
- return "".join(
- "" if (skip_special_tokens and i in self.all_special_ids) else self._IDS.get(i, "")
- for i in token_ids
- )
- prompt_utils = SimpleNamespace(
- MODEL_CONFIG = {"deepseek_vl_v2": object()},
- apply_chat_template = lambda *_a, **_k: "<image> model-aware",
+def test_mlx_vlm_drops_an_orphan_closer_that_opened_nothing(monkeypatch):
+ """Mentioning a marker is not opening an envelope."""
+ turn = _SpmTurn(
+ ("The", "▁[ARGS]", "▁marker", "▁is", "▁neat", "<|end_message|>"),
+ specials = ("<|end_message|>",),
+ eos = "<|end_message|>",
+ ends = "stop",
)
- mlx_vlm = types.ModuleType("mlx_vlm")
- mlx_vlm.prompt_utils = prompt_utils
+ assert _run_spm_vlm_turn(monkeypatch, turn)[-1] == "The [ARGS] marker is neat"
- def _vlm_stream(*_a, **_k):
- for token_id, text in (
- (1, "<|channel>"),
- (None, "thought\n"),
- (None, "weighing it"),
- (2, "<channel|>"),
- (None, " the answer"),
- ):
- yield SimpleNamespace(text = text, token = token_id, prompt_tokens = 3, generation_tokens = 1)
- mlx_vlm.stream_generate = _vlm_stream
- monkeypatch.setitem(sys.modules, "mlx_vlm", mlx_vlm)
- monkeypatch.setattr(
- "core.inference.chat_template_helpers.apply_chat_template_for_generation",
- lambda _t, _m, **_k: "<image> model-aware",
- )
+def test_mlx_vlm_keeps_a_block_the_runtime_settled_in_one_step(monkeypatch):
+ """A diffusion generator denoises a whole block and names only its last id."""
+ turn = _SpmTurn(("Hello", "\u2581world"), block = True)
+ assert _run_spm_vlm_turn(monkeypatch, turn)[-1] == "Hello world"
- backend = MLXInferenceBackend()
- backend._model = SimpleNamespace(config = {"model_type": "deepseek_vl_v2"})
- backend._processor = SimpleNamespace(tokenizer = _Tok(), chat_template = _Tok.chat_template)
- backend._tokenizer = _Tok()
- args = ([{"role": "user", "content": [{"type": "image"}]}], object(), 0, 1, 0, 0, 8, 1, None)
- snapshots = list(
- backend._generate_vlm(
- *args,
- _adapter_state = False,
- tools = [{"type": "function", "function": {"name": "get_weather"}}],
- )
+def test_mlx_vlm_matches_a_stop_sequence_the_detokenizer_held_to_the_end(monkeypatch):
+ """Text the cut held back is unmatched, not cleared."""
+ turn = _SpmTurn(("Hi", "\u2581STOP"))
+ assert _run_spm_vlm_turn(monkeypatch, turn, stop = "STOP")[-1] == "Hi "
+
+
+def test_mlx_vlm_writes_a_buffered_reasoning_delimiter_once(monkeypatch):
+ """The opener is buffered with the pieces after it, so a second copy written on its own step
+ lands ahead of them: the normalizer then matches the later copy and emits the earlier one as
+ answer text, which is how a gemma-4 thought leaked ``<|channel>`` into the reply."""
+ turn = _SpmTurn(
+ (
+ "<|channel>",
+ "thought",
+ "\n",
+ "weighing",
+ "▁it",
+ "<channel|>",
+ "▁the",
+ "▁answer",
+ ),
+ specials = ("<|channel>", "<channel|>"),
+ chat_template = "...<|channel>thought\n...<channel|>",
)
- # The thought is a closed reasoning block, not answer text.
- assert snapshots[-1] == "<think>weighing it</think> the answer"
+ assert _run_spm_vlm_turn(monkeypatch, turn)[-1] == "<think>weighing it</think> the answer"
# --- VLM prompt snapshots -------------------------------------------------------
@@ -5005,8 +5065,11 @@ def test_vlm_add_special_tokens_falls_back_to_the_inline_rule(monkeypatch):
assert rule("gemma4", template) == "mlx-vlm's answer"
-def _run_mlx_reasoning_stream(monkeypatch, pieces, special_ids, eos_id):
- """Drive the MLX text reasoning path over ``pieces``, stopping on ``eos_id``."""
+def _run_mlx_reasoning_stream(
+ monkeypatch,
+ turn,
+ stop = None,
+):
_install_fake_mlx(monkeypatch)
from core.inference import mlx_inference
@@ -5029,74 +5092,83 @@ def _run_mlx_reasoning_stream(monkeypatch, pieces, special_ids, eos_id):
mlx_lm_sample.make_sampler = lambda **_kwargs: object()
mlx_lm_sample.make_logits_processors = lambda **_kwargs: None
- def _stream_generate(_model, _tokenizer, **_kwargs):
- for token_id, piece in pieces.items():
- yield SimpleNamespace(token = token_id, text = piece)
-
- mlx_lm_pkg.stream_generate = _stream_generate
+ mlx_lm_pkg.stream_generate = lambda _model, _tokenizer, **_kwargs: turn.stream()
monkeypatch.setitem(sys.modules, "mlx_lm", mlx_lm_pkg)
monkeypatch.setitem(sys.modules, "mlx_lm.sample_utils", mlx_lm_sample)
- class _Tokenizer:
- chat_template = "x"
- all_special_ids = list(special_ids)
- all_special_tokens = [pieces[token_id] for token_id in special_ids]
- eos_token_id = eos_id
-
- def convert_ids_to_tokens(self, token_id):
- return pieces[token_id]
-
- def decode(
- self,
- ids,
- *,
- skip_special_tokens = False,
- **_kwargs,
- ):
- skipped = special_ids if skip_special_tokens else set()
- return "".join(pieces[int(t)] for t in ids if t not in skipped)
-
backend = mlx_inference.MLXInferenceBackend()
backend._model = object()
- backend._tokenizer = _Tokenizer()
+ backend._tokenizer = turn
backend._is_vlm = False
return list(
backend.generate_chat_response(
messages = [{"role": "user", "content": "hi"}],
tools = [{"type": "function", "function": {"name": "terminal"}}],
- max_new_tokens = len(pieces),
+ max_new_tokens = len(turn.ids),
+ stop = stop,
)
)
def test_mlx_reasoning_reply_does_not_end_in_a_preserved_eos_control(monkeypatch):
- """The non-reasoning branch trims a trailing stop id that is also an allowlisted
- control; the reasoning branch appended it, ending an ordinary reply in raw markup."""
- pieces = {
- 1: "<|channel>",
- 2: "thought",
- 3: "reasoned",
- 4: "<channel|>",
- 5: "Done.",
- 6: "<|end_message|>",
- }
- snapshots = _run_mlx_reasoning_stream(monkeypatch, pieces, {1, 4, 6}, 6)
+ """The reasoning branch appended it, ending an ordinary reply in raw markup."""
+ turn = _SpmTurn(
+ ("<|channel>", "thought", "▁reasoned", "<channel|>", "▁Done.", "<|end_message|>"),
+ specials = ("<|channel>", "<channel|>", "<|end_message|>"),
+ eos = "<|end_message|>",
+ chat_template = "x",
+ ends = "stop",
+ )
+ snapshots = _run_mlx_reasoning_stream(monkeypatch, turn)
assert "<|end_message|>" not in snapshots[-1]
assert snapshots[-1].endswith("Done.")
+def test_mlx_reasoning_keeps_the_token_the_limit_cut_the_turn_after(monkeypatch):
+ """mlx-lm reports that token without ever yielding it on its own step."""
+ turn = _SpmTurn(
+ ("<|channel>", "thought", "▁reasoned", "<channel|>", "▁the", "▁answer"),
+ specials = ("<|channel>", "<channel|>"),
+ chat_template = "x",
+ ends = "length",
+ )
+ assert _run_mlx_reasoning_stream(monkeypatch, turn)[-1].endswith(" the answer")
+
+
+def test_mlx_reasoning_matches_a_stop_sequence_the_detokenizer_held_to_the_end(monkeypatch):
+ """As on the VLM path: the flush ending the turn is the first the sequence is visible in."""
+ turn = _SpmTurn(
+ ("<|channel>", "thought", "\u2581reasoned", "<channel|>", "\u2581Answer", "\u2581STOP"),
+ specials = ("<|channel>", "<channel|>"),
+ chat_template = "x",
+ )
+ snapshots = _run_mlx_reasoning_stream(monkeypatch, turn, stop = "STOP")
+ assert snapshots[-1] == "<think> reasoned</think> Answer "
+
+
def test_mlx_reasoning_keeps_an_eos_control_that_closes_a_tool_envelope(monkeypatch):
"""The trim is envelope-aware: the same marker terminates a real Inkling call."""
- pieces = {
- 1: "<|channel>",
- 2: "thought",
- 3: "reasoned",
- 4: "<channel|>",
- 5: "<|content_invoke_tool_json|>",
- 6: '{"name":"terminal","args":{}}',
- 7: "<|end_message|>",
- }
- snapshots = _run_mlx_reasoning_stream(monkeypatch, pieces, {1, 4, 5, 7}, 7)
+ turn = _SpmTurn(
+ (
+ "<|channel>",
+ "thought",
+ "▁reasoned",
+ "<channel|>",
+ "<|content_invoke_tool_json|>",
+ '{"name":"terminal","args":{}}',
+ "<|end_message|>",
+ ),
+ specials = (
+ "<|channel>",
+ "<channel|>",
+ "<|content_invoke_tool_json|>",
+ "<|end_message|>",
+ ),
+ eos = "<|end_message|>",
+ chat_template = "x",
+ ends = "stop",
+ )
+ snapshots = _run_mlx_reasoning_stream(monkeypatch, turn)
assert snapshots[-1].endswith("<|end_message|>")