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.

GLM lookup: build the n-gram index when the request is queued

YanissAmz64855cb015b6
diff --git a/tools/qwen/spec_policy.py b/tools/qwen/spec_policy.py
index 734c0fb..fdf69d1 100644
--- a/tools/qwen/spec_policy.py
+++ b/tools/qwen/spec_policy.py
@@ -354,6 +354,15 @@ def install_lookup(g, ngram, stats=None):
needs max_history >= K. Returns an uninstall function."""
fol = SeqFollower(ngram[0]); K, lmin = ngram[1], ngram[2]; cmin = ngram[3] if len(ngram) > 3 else 0
orig = type(g).iterate_draftmodel_mtp_gen.__get__(g)
+ orig_enqueue = g.enqueue
+ def enq(job):
+ # prime the index at enqueue (as install()): its cold build lands in time-to-first-token, not in the first decode round
+ r = orig_enqueue(job)
+ if os.environ.get("QWSPEC_NGRAM_IDX", "1") != "0":
+ for j in (job if isinstance(job, list) else [job]):
+ if len(getattr(j, "sequences", ())) == 1: fol.sync(j.sequences[0].sequence_ids)
+ return r
+ g.enqueue = enq
def wrapped(results):
cont = None
if len(g.active_jobs) == 1 and g.active_jobs[0].is_prefill_done() and len(g.active_jobs[0].sequences) == 1:
@@ -372,7 +381,9 @@ def install_lookup(g, ngram, stats=None):
if stats is not None: stats["ng"] = stats.get("ng", 0) + 1; stats["ng_tokens"] = stats.get("ng_tokens", 0) + len(cont)
return torch.tensor([cont], dtype=torch.long)
g.iterate_draftmodel_mtp_gen = wrapped
- def uninstall(): g.__dict__.pop("iterate_draftmodel_mtp_gen", None)
+ def uninstall():
+ g.__dict__.pop("iterate_draftmodel_mtp_gen", None)
+ if g.__dict__.get("enqueue") is enq: g.__dict__.pop("enqueue")
return uninstall
def rule_from_env(thf, thv, maxd):
diff --git a/tools/qwen/test_spec_rule.py b/tools/qwen/test_spec_rule.py
index 7e05721..0e0dce9 100644
--- a/tools/qwen/test_spec_rule.py
+++ b/tools/qwen/test_spec_rule.py
@@ -177,6 +177,18 @@ class T(unittest.TestCase):
g.active_jobs = [g.job, FakeJob()]
self.assertEqual(g.iterate_draftmodel_mtp_gen([]).shape[-1], 2) # two requests: no lookup
un(); self.assertNotIn("iterate_draftmodel_mtp_gen", g.__dict__)
+ self.assertNotIn("enqueue", g.__dict__)
+ # the index is primed at enqueue: the first decode round finds it built (no rebuild)
+ g = gen(row(1000) + list(range(100, 118))); fols = []
+ orig_sf = sp.SeqFollower
+ class Spy(orig_sf):
+ def __init__(s, n): orig_sf.__init__(s, n); fols.append(s)
+ sp.SeqFollower = Spy
+ try: sp.install_lookup(g, (2, 3, 16, 1))
+ finally: sp.SeqFollower = orig_sf
+ g.enqueue(g.job)
+ self.assertEqual((fols[0].sid is g.job.sequences[0].sequence_ids, len(fols[0].idx)), (True, 42))
+ self.assertEqual(g.iterate_draftmodel_mtp_gen([]).tolist(), [[118, 119, 1000]])
def test_witness_len(self):
# a templated list: the constant part is copied, the copy stops at the value that changes from row to row