Skip to content

Kyojin: open-source engine that runs 100 GB-class open models (GLM-5.3, Qwen3.8, DeepSeek V4.1) on one desktop machine. AMD Strix Halo, NVIDIA RTX and Apple Mac. OpenAI API, built on ExLlamaV3.

Relaxed draft acceptance only after the think block closes (EXL3_SPEC_TYPICAL_PHASE=answer) (#183)

Yaniss Amazouz2484e3172bf2
diff --git a/exllamav3/generator/generator.py b/exllamav3/generator/generator.py
index d72b5c2..8e9cc94 100644
--- a/exllamav3/generator/generator.py
+++ b/exllamav3/generator/generator.py
@@ -2167,7 +2167,8 @@ class Generator:
typ = None
# EXL3_SPEC_TYPICAL_LOOKUP: arm "0" keeps copied drafts exact; default "rep" refuses the relaxed rule (rep_refused, below) for a draft that
# completes an n-gram the job already repeated in its own output. The probe still logs every draft.
- typ_relax = draft_tokens is not None and spec_sample.relax_allowed(self, draft_tokens, j)
+ typ_relax = draft_tokens is not None and spec_sample.relax_allowed(self, draft_tokens, j) \
+ and not (spec_sample.phase_answer_only() and spec_sample.in_think(self, job))
if draft_tokens is not None and spec is None and (spec_sample.typical_on() or spec_sample.PTAP) \
and npos > 1 and not self.dflash_draft:
_tp = spec_sample.eligible(job)
diff --git a/exllamav3/generator/spec_sample.py b/exllamav3/generator/spec_sample.py
index 4847052..be62dd9 100644
--- a/exllamav3/generator/spec_sample.py
+++ b/exllamav3/generator/spec_sample.py
@@ -182,6 +182,47 @@ def draft_is_copy(gen, draft_tokens: torch.Tensor, row: int) -> bool:
return draft_tokens.data_ptr() != gen.draft_ids_pinned.data_ptr()
+# EXL3_SPEC_TYPICAL_PHASE (read per round): "answer" = the relaxed rule applies only once the job's think block is closed (drafts
+# inside thinking are verified exactly: free-text loops start there, and relaxed acceptance of copied/argmax drafts feeds them);
+# "all" (default) = the relaxed rule everywhere, as before. Same rule as the MLX engine's spec_typical_phase.
+def phase_answer_only() -> bool:
+ return os.environ.get("EXL3_SPEC_TYPICAL_PHASE", "all").strip().lower() == "answer"
+
+
+def _think_ids(gen) -> tuple:
+ ids = getattr(gen, "_typ_think_ids", None)
+ if ids is None:
+ def one(t):
+ try:
+ v = gen.tokenizer.single_id(t)
+ return v if isinstance(v, int) and v >= 0 else -1
+ except Exception:
+ return -1
+ ids = (one("<think>"), one("</think>"))
+ gen._typ_think_ids = ids
+ return ids
+
+
+def in_think(gen, job) -> bool:
+ """True while the last think marker of the job's sequence (prompt + output) is an open <think>. Incremental: reads only the new
+ tail each round (state on the job); a shorter sequence (rewind) rescans from the start. False when the model has no think tokens."""
+ o, c = _think_ids(gen)
+ if o < 0 or c < 0:
+ return False
+ seq = job.sequences[0].sequence_ids
+ n = len(seq)
+ lo, state = getattr(job, "_typ_think", (0, False))
+ if n < lo:
+ lo, state = 0, False
+ if n > lo:
+ for t in reversed(seq.torch_slice(lo, n).reshape(-1).tolist()):
+ if t == o or t == c:
+ state = t == o
+ break
+ job._typ_think = (n, state)
+ return state
+
+
def relax_allowed(gen, draft_tokens: torch.Tensor, row: int) -> bool:
"""Source gate: False only in arm "0" for a copied draft. The repetition check (rep_refused) is separate."""
return TYPICAL_LOOKUP != "0" or not draft_is_copy(gen, draft_tokens, row)
diff --git a/tools/qwen/test_spec_typical.py b/tools/qwen/test_spec_typical.py
index 8dd304a..c8679f8 100644
--- a/tools/qwen/test_spec_typical.py
+++ b/tools/qwen/test_spec_typical.py
@@ -347,5 +347,52 @@ class CooldownRule(unittest.TestCase):
self.assertEqual(tr.first_repeat(999), -1)
+
+class _Seq:
+ def __init__(self, ids): self.ids = list(ids)
+ def __len__(self): return len(self.ids)
+ def torch_slice(self, a, b): return torch.tensor(self.ids[a:b])
+
+
+class _Tok:
+ def single_id(self, t): return {"<think>": 101, "</think>": 102}[t]
+
+
+class _Job:
+ def __init__(self, ids):
+ self.sequences = [type("S", (), {})()]
+ self.sequences[0].sequence_ids = _Seq(ids)
+
+
+class TypicalPhase(unittest.TestCase):
+ def test_in_think_tracks_markers_incrementally(self):
+ gen = type("G", (), {"tokenizer": _Tok()})()
+ job = _Job([1, 2, 101, 5, 6]) # prompt ends with an open <think>
+ self.assertTrue(ss.in_think(gen, job))
+ job.sequences[0].sequence_ids.ids += [7, 102, 8] # thinking closed: answer phase
+ self.assertFalse(ss.in_think(gen, job))
+ job.sequences[0].sequence_ids.ids += [9]
+ self.assertFalse(ss.in_think(gen, job))
+ job.sequences[0].sequence_ids.ids = [1, 2, 101, 5] # rewound below the close: rescan
+ self.assertTrue(ss.in_think(gen, job))
+ nothink = _Job([1, 101, 102, 3]) # thinking disabled in the prompt (empty think block)
+ self.assertFalse(ss.in_think(gen, nothink))
+
+ def test_no_think_tokens_never_in_think(self):
+ class T:
+ def single_id(self, t): raise KeyError(t)
+ gen = type("G", (), {"tokenizer": T()})()
+ self.assertFalse(ss.in_think(gen, _Job([1, 2, 3])))
+
+ def test_phase_flag(self):
+ old = os.environ.pop("EXL3_SPEC_TYPICAL_PHASE", None)
+ try:
+ self.assertFalse(ss.phase_answer_only())
+ os.environ["EXL3_SPEC_TYPICAL_PHASE"] = "answer"
+ self.assertTrue(ss.phase_answer_only())
+ finally:
+ os.environ.pop("EXL3_SPEC_TYPICAL_PHASE", None)
+ if old is not None: os.environ["EXL3_SPEC_TYPICAL_PHASE"] = old
+
if __name__ == "__main__":
unittest.main()