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.

Spec policy: cap the draft depth by the number of requests decoding together

YanissAmzc842e91008b9
diff --git a/tools/qwen/spec_policy.py b/tools/qwen/spec_policy.py
index 0df15a6..a22bee0 100644
--- a/tools/qwen/spec_policy.py
+++ b/tools/qwen/spec_policy.py
@@ -355,6 +355,30 @@ def ngram_from_env(default=(2, 7, 16, 3)):
if not (3 <= len(f) <= 4 and 1 <= g <= LMAX and k >= 1 and m >= g and min(f) >= 0): raise ValueError(f"QWSPEC_NGRAM={v}")
return tuple(f)
+def load_caps_from_env():
+ """(table, rows) from QWSPEC_LOAD_CAPS = "N:D,N:D,..." (draft depth cap D, 0 = no draft, when N requests decode together)
+ and QWSPEC_LOAD_ROWS = R (no table entry for N -> cap R // N - 1, the depth at which the N jobs' verify rows, N * (D + 1),
+ fit one forward of the row-invariant grouped step: generator/row_groups.py runs one forward per 8 rows). Unset / 0 = no
+ load cap, the solo depth rule at every N. Unset QWSPEC_LOAD_ROWS = EXL3_ROW_INV_GROUP_ROWS (8) when EXL3_ROW_INV_GROUPS=1,
+ else 0; QWSPEC_LOAD_ROWS=0 turns the cap off."""
+ v = os.environ.get("QWSPEC_LOAD_CAPS", "").strip()
+ table = {}
+ if v:
+ for f in v.split(","):
+ n, d = f.split(":"); table[int(n)] = int(d)
+ rows = os.environ.get("QWSPEC_LOAD_ROWS", "")
+ if rows.strip() == "": # default: on exactly when the grouped step is on, with its row cap
+ on = all(os.environ.get(k, d) not in ("", "0") for k, d in (("EXL3_ROW_INV_GROUPS", "0"), ("EXL3_ROW_INV", "1")))
+ rows = os.environ.get("EXL3_ROW_INV_GROUP_ROWS", "") or "8" if on else "0"
+ return table, int(rows)
+
+def load_depth_cap(n, table, rows):
+ """Deepest draft allowed while n requests decode together (None = no cap). One request keeps its own rule."""
+ if n <= 1: return None
+ if n in table: return table[n]
+ if rows <= 0: return None
+ return max(0, rows // n - 1)
+
def _unparked(g):
"""Active jobs minus those EXL3_SCHED_CHUNK_IL skips this step (Generator._cil_parked): a short job decoding next to a parked
big prefill still counts as alone, so its lookup/draft policy is the solo one."""
@@ -536,6 +560,7 @@ def install(g, dm, model, thf=0.6, thv=0.3, maxd=3, ngram=None, stats=None, rule
log = list: logging arm. Drafts log_depth depths whatever the rule says, appends one dict per round
(n, ng, ps, ids, keep) and verifies what `rule` would have kept (the stream is the baseline stream)."""
lm = model.modules[model.logit_layer_idx]
+ load_table, load_rows = load_caps_from_env()
if rule is None: rule = ProductRule(thf, thv, maxd)
maxd = rule.maxd # the rule owns the depth cap
st = {"i": 0, "stop": False, "keep": None, "ps": [], "job": None, "n": None, "drafted": 0}
@@ -634,6 +659,10 @@ def install(g, dm, model, thf=0.6, thv=0.3, maxd=3, ngram=None, stats=None, rule
if a < len(c):
cont = c[:a] if a >= ngram[3] else None
if stats is not None: stats["ng_cut" if cont else "ng_skip"] = stats.get("ng_cut" if cont else "ng_skip", 0) + 1
+ # Draft depth per load: with several requests decoding together the verify rows add up (N * (D + 1)) and the grouped
+ # step runs one forward per 8 rows; the cap keeps the step in one forward (cap 0 = a plain step, no draft)
+ lcap = load_depth_cap(sum(1 for j in _unparked(g) if j.is_prefill_done()), load_table, load_rows)
+ if lcap == 0: return None
if cont is not None:
st["mode"] = "ng"; g.num_draft_tokens = 1
try: out = orig_draft(results)
@@ -643,6 +672,8 @@ def install(g, dm, model, thf=0.6, thv=0.3, maxd=3, ngram=None, stats=None, rule
if stats is not None: stats["ng"] = stats.get("ng", 0) + 1; stats["rows"] = stats.get("rows", 0) + 1 + len(cont)
return torch.tensor([cont], dtype=torch.long)
w = rule.choose() if hasattr(rule, "choose") else win
+ rule_maxd = rule.maxd
+ if lcap is not None: w = min(w, lcap); rule.maxd = min(rule_maxd, lcap)
g.num_draft_tokens = w
try:
out = orig_draft(results)
@@ -650,6 +681,7 @@ def install(g, dm, model, thf=0.6, thv=0.3, maxd=3, ngram=None, stats=None, rule
out = g.draft_ids_pinned[:, :st["keep"]]
finally:
g.num_draft_tokens = win # Job.prepare / reply_room reserve against this value: keep it at the deepest window
+ rule.maxd = rule_maxd
if out is None: return None
if fixed is not None:
ps = torch.stack(st["ps"]).tolist() # the one host read of the round
diff --git a/tools/qwen/test_spec_rule.py b/tools/qwen/test_spec_rule.py
index 9dba5bc..4296c56 100644
--- a/tools/qwen/test_spec_rule.py
+++ b/tools/qwen/test_spec_rule.py
@@ -353,4 +353,38 @@ class TI(unittest.TestCase):
self.assertEqual(g.num_draft_tokens, 7)
out, g = run_install([0.9] * 8, rule=sp.ProductRule(0.6, 0.3, 3), ndt=3); self.assertEqual(g.num_draft_tokens, 3)
+ def test_load_depth_cap(self):
+ self.assertEqual(sp.load_depth_cap(1, {}, 8), None) # solo keeps its rule
+ self.assertEqual(sp.load_depth_cap(2, {}, 0), None) # no budget, no table: off
+ self.assertEqual([sp.load_depth_cap(n, {}, 8) for n in (2, 3, 4, 8)], [3, 1, 1, 0])
+ self.assertEqual(sp.load_depth_cap(4, {4: 2}, 8), 2) # table wins
+ os.environ["QWSPEC_LOAD_CAPS"] = "2:5, 4:0"; os.environ["QWSPEC_LOAD_ROWS"] = "8"
+ try: self.assertEqual(sp.load_caps_from_env(), ({2: 5, 4: 0}, 8))
+ finally: del os.environ["QWSPEC_LOAD_CAPS"], os.environ["QWSPEC_LOAD_ROWS"]
+
+ def test_load_rows_default_follows_groups(self):
+ keep = {k: os.environ.pop(k, None) for k in ("QWSPEC_LOAD_ROWS", "QWSPEC_LOAD_CAPS", "EXL3_ROW_INV_GROUPS", "EXL3_ROW_INV_GROUP_ROWS")}
+ try:
+ self.assertEqual(sp.load_caps_from_env(), ({}, 0))
+ os.environ["EXL3_ROW_INV_GROUPS"] = "1"; self.assertEqual(sp.load_caps_from_env(), ({}, 8))
+ os.environ["EXL3_ROW_INV_GROUP_ROWS"] = "6"; self.assertEqual(sp.load_caps_from_env(), ({}, 6))
+ os.environ["QWSPEC_LOAD_ROWS"] = "0"; self.assertEqual(sp.load_caps_from_env(), ({}, 0))
+ finally:
+ for k, v in keep.items():
+ os.environ.pop(k, None)
+ if v is not None: os.environ[k] = v
+
+ def test_load_cap_through_install(self):
+ os.environ["QWSPEC_LOAD_CAPS"] = "2:2,3:0"
+ try:
+ for n, want in ((1, 7), (2, 2), (3, None)):
+ g = FakeGen(7); g.active_jobs = [FakeJob() for _ in range(n)]; g.job = g.active_jobs[0]
+ dm = FakeDM(g, [0.99] * 8); lm = FakeLM(dm); g.dm, g.lm = dm, lm; dm.chain = [0.99] * 8
+ rule = sp.ProductRule(0.0, 0.0, 7)
+ unin = sp.install(g, dm, FakeModel(lm), rule=rule)
+ out = g.iterate_draftmodel_mtp_gen([])
+ self.assertEqual(None if out is None else out.shape[-1], want)
+ self.assertEqual(rule.maxd, 7); self.assertEqual(g.num_draft_tokens, 7) # restored after the round
+ finally: del os.environ["QWSPEC_LOAD_CAPS"]
+
if __name__ == "__main__": unittest.main(verbosity=1)