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.

Qwen draft policy: take the n-gram lookup only on a 16-token match and draft 7 tokens from it

YanissAmzc1b8e1dd82d7
diff --git a/tools/qwen/serve.py b/tools/qwen/serve.py
index 0a8f924..3aca19e 100644
--- a/tools/qwen/serve.py
+++ b/tools/qwen/serve.py
@@ -728,7 +728,7 @@ class QwenEngine:
thf, thv, maxd = spec_policy.env_defaults(self.ndt) # 0.6 / 0.3 / ndt unless QWSPEC_THF / THV / MAXD are set
fx = os.environ.get("QWSPEC_FIXD")
self.uninstall = spec_policy.install(self.generator, self.draft_model, self.model,
- thf, thv, maxd, (2, 3, 5), {},
+ thf, thv, maxd, spec_policy.ngram_from_env(), {},
rule=spec_policy.rule_from_env(thf, thv, maxd), fixed=int(fx) if fx else None)
self.spec_on = spec
if self.slot_store is not None:
diff --git a/tools/qwen/spec_policy.py b/tools/qwen/spec_policy.py
index 63d3cc4..3442ed7 100644
--- a/tools/qwen/spec_policy.py
+++ b/tools/qwen/spec_policy.py
@@ -313,6 +313,20 @@ def env_defaults(ndt):
"""(thf, thv, maxd) for install(): today's 0.6 / 0.3 / ndt unless QWSPEC_THF / QWSPEC_THV / QWSPEC_MAXD are set."""
return _envf("QWSPEC_THF", 0.6), _envf("QWSPEC_THV", 0.3), _envf("QWSPEC_MAXD", int(ndt))
+def ngram_from_env(default=(2, 7, 16)):
+ """n-gram lookup setting for install(): (gram, K, match) from QWSPEC_NGRAM = "gram,K,match"; "off" = no lookup (None).
+ Default (2, 7, 16): the lookup replaces the drafter only on a match of 16 tokens or more and then drafts 7 tokens.
+ The former (2, 3, 5) dates from the time the drafter stopped at 3 drafts. With the deep tier the drafter keeps 4-5 drafts
+ on JSON and code where a 5-token match (keys, punctuation, idioms) copies the wrong values: lookup rounds accepted
+ 1.7 of 3 drafts there against 5.1 of 5.5 for drafter rounds, and on a plain copy 3 drafts per round is less than the
+ drafter gives alone. QWSPEC_NGRAM=2,3,5 restores the former setting."""
+ v = (os.environ.get("QWSPEC_NGRAM") or "").strip().lower()
+ if v == "": return default
+ if v in ("off", "0", "none"): return None
+ g, k, m = (int(x) for x in v.split(","))
+ if not (1 <= g <= LMAX and k >= 1 and m >= g): raise ValueError(f"QWSPEC_NGRAM={v}")
+ return (g, k, m)
+
def rule_from_env(thf, thv, maxd):
"""QWSPEC_RULE = tier (default) | product (the former 0.6/0.3/3 rule when maxd=3) | tier (default thresholds below depth 4, stricter 5+ row tier: QWSPEC_THFD/THVD/DSPLIT) | cost | online | table (depth from the request's acceptance history, no probabilities); QWSPEC_GAMMA / QWSPEC_SCALE tune the calibrated reach.
QWSPEC_FIXD = N: draft N depths with no host read and truncate once per round (rule applied on the stacked probabilities)."""
diff --git a/tools/qwen/test_spec_rule.py b/tools/qwen/test_spec_rule.py
index af79894..1037113 100644
--- a/tools/qwen/test_spec_rule.py
+++ b/tools/qwen/test_spec_rule.py
@@ -136,6 +136,19 @@ class T(unittest.TestCase):
try: self.assertEqual(sp.env_defaults(3), (0.5, 0.3, 5))
finally: del os.environ["QWSPEC_THF"]; del os.environ["QWSPEC_MAXD"]
+ def test_ngram_from_env(self):
+ os.environ.pop("QWSPEC_NGRAM", None)
+ self.assertEqual(sp.ngram_from_env(), (2, 7, 16))
+ for v, want in (("2,3,5", (2, 3, 5)), ("off", None), ("0", None), (" 3, 7, 16 ", (3, 7, 16))):
+ os.environ["QWSPEC_NGRAM"] = v
+ try: self.assertEqual(sp.ngram_from_env(), want)
+ finally: del os.environ["QWSPEC_NGRAM"]
+ for v in ("2,7", "17,7,32", "2,0,5", "4,7,3"):
+ os.environ["QWSPEC_NGRAM"] = v
+ try:
+ with self.assertRaises(ValueError): sp.ngram_from_env()
+ finally: del os.environ["QWSPEC_NGRAM"]
+
def test_online_scale(self):
r = sp.OnlineRule(0.5, 0.3, 6)
for _ in range(60):