Skip to content

Commit bfcac00

Browse files
fix(mac chat): use template enable_thinking instead of raw channel marker
Smoke run showed the injected <|channel>content marker leaked 'thought' text and caused greedy looping (turns 1/3 repeated). Replace with the chat template's enable_thinking flag (default off → clean direct answers), with a TypeError fallback for templates that don't accept the kwarg. Co-authored-by: FluffyAIcode <FluffyAIcode@users.noreply.github.com>
1 parent 6b36fa4 commit bfcac00

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

scripts/chat_mlx_kakeya.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,16 @@ def _resolve_eos(tok) -> set:
5252
return eos
5353

5454

55-
def _content_marker(tok) -> List[int]:
56-
"""gemma-4 emits a <|channel>thought ... reasoning preamble by default; this
57-
marker nudges it straight to the content channel for direct chat answers."""
55+
def _apply_template(tok, history, *, thinking: bool) -> List[int]:
56+
"""Encode the chat history. gemma-4 has a reasoning ("thought") channel; the
57+
clean way to get direct answers is the template's ``enable_thinking`` flag
58+
(NOT injecting a raw channel marker, which leaks 'thought' text and loops).
59+
Falls back gracefully if the template doesn't accept the kwarg."""
5860
try:
59-
ids = tok.encode("<|channel>content\n<channel|>", add_special_tokens=False)
61+
ids = tok.apply_chat_template(
62+
history, add_generation_prompt=True, enable_thinking=thinking)
6063
except TypeError:
61-
ids = tok.encode("<|channel>content\n<channel|>")
64+
ids = tok.apply_chat_template(history, add_generation_prompt=True)
6265
return ids.tolist() if hasattr(ids, "tolist") else list(ids)
6366

6467

@@ -98,7 +101,6 @@ def main() -> int:
98101
n_layers = len(text_model.layers)
99102
full_idx = set(mlx_full_attention_layer_indices(text_model))
100103
eos = _resolve_eos(tok)
101-
marker = [] if args.thinking else _content_marker(tok)
102104
_log(f"loaded in {time.time()-t_load:.1f}s | layers={n_layers} "
103105
f"exact(full-attn)={sorted(full_idx)} sink={args.sink} window={args.window} "
104106
f"eos={sorted(eos)}")
@@ -117,8 +119,7 @@ def new_cache() -> list:
117119
]
118120

119121
def build_prompt_ids(history: List[Dict[str, str]]) -> List[int]:
120-
ids = list(tok.apply_chat_template(history, add_generation_prompt=True))
121-
return ids + list(marker)
122+
return _apply_template(tok, history, thinking=args.thinking)
122123

123124
def generate_turn(prompt_ids: List[int], on_delta=None) -> Dict[str, Any]:
124125
"""Single-stream greedy decode over a FRESH Kakeya bounded cache."""

0 commit comments

Comments
 (0)