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.

Speculation: TierRule, default thresholds up to 4 rows and a stricter deep tier (QWSPEC_RULE=tier)

YanissAmza4aee9412346
diff --git a/tools/qwen/spec_policy.py b/tools/qwen/spec_policy.py
index fe59029..7f42d95 100644
--- a/tools/qwen/spec_policy.py
+++ b/tools/qwen/spec_policy.py
@@ -225,6 +225,21 @@ class ProductRule:
return True, not (q < self.thf or i + 1 >= self.maxd)
def end_round(self, drafted, accepted): pass
+class TierRule(ProductRule):
+ """ProductRule for rows <= dsplit + 1 (identical to the shipped rule there), stricter beyond: a draft deeper than dsplit
+ is kept only if its reach >= thvd, and drafting goes past depth dsplit only if the reach so far >= thfd.
+ Why (adapt, 07/10): one pair (thf, thv) serves two jobs. Raising thf to 0.8 to avoid unprofitable 5+ row rounds also cut the
+ 2..4 row rounds short, which cost 1-4 % where depth 2-4 acceptance is good (128K, chat). Verify rows cost the same at every
+ context (~10.5 ms per row incl. its draft step); a row beyond the 4th pays only when its chance of being accepted is above
+ ~0.5, and the drafter's reach overstates it, so the deep tier asks for reach >= 0.9 to start and >= 0.5 to keep."""
+ name = "tier"
+ def __init__(self, thf=0.6, thv=0.3, maxd=7, thfd=0.9, thvd=0.5, dsplit=3):
+ super().__init__(thf, thv, maxd); self.thfd, self.thvd, self.dsplit = thfd, thvd, dsplit
+ def step(self, i, p):
+ q = self.q(i, p); k = i + 1
+ if i > 0 and q < (self.thv if k <= self.dsplit else self.thvd): return False, False
+ return True, not (q < (self.thf if k < self.dsplit else self.thfd) or k >= self.maxd)
+
class CostRule(ProductRule):
"""Same shape, calibrated reach: q = min(1, scale * reach ** gamma). thv = cost of one verify row / ms per token
(keep a row when its chance to add a token pays its ~row_ms), thf = the same test one depth ahead (drafting also costs)."""
@@ -297,11 +312,12 @@ def env_defaults(ndt):
return _envf("QWSPEC_THF", 0.6), _envf("QWSPEC_THV", 0.3), _envf("QWSPEC_MAXD", int(ndt))
def rule_from_env(thf, thv, maxd):
- """QWSPEC_RULE = product (default) | cost | online | table (depth from the request's acceptance history, no probabilities); QWSPEC_GAMMA / QWSPEC_SCALE tune the calibrated reach.
+ """QWSPEC_RULE = product (default) | 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)."""
kind = os.environ.get("QWSPEC_RULE", "product")
if kind == "product": r = ProductRule(thf, thv, maxd)
elif kind == "cost": r = CostRule(thf, thv, maxd, _envf("QWSPEC_GAMMA", 1.0), _envf("QWSPEC_SCALE", 1.0))
+ elif kind == "tier": r = TierRule(thf, thv, maxd, _envf("QWSPEC_THFD", 0.9), _envf("QWSPEC_THVD", 0.5), _envf("QWSPEC_DSPLIT", 3))
elif kind == "table":
cyc = os.environ.get("QWSPEC_CYCLE"); r = TableRule(maxd, [float(x) for x in cyc.split(",")] if cyc else None, _envf("QWSPEC_DECAY", 0.75), _envf("QWSPEC_PRIOR", 0.75))
elif kind == "online": r = OnlineRule(thf, thv, maxd, _envf("QWSPEC_GAMMA", 1.0), _envf("QWSPEC_ALPHA", 0.15))
diff --git a/tools/qwen/test_spec_rule.py b/tools/qwen/test_spec_rule.py
index 6ab9926..91be725 100644
--- a/tools/qwen/test_spec_rule.py
+++ b/tools/qwen/test_spec_rule.py
@@ -104,6 +104,27 @@ class T(unittest.TestCase):
self.assertEqual(log[0]["keep"], ref); self.assertEqual(len(log[0]["ids"]), 7)
self.assertEqual(len(log[0]["ps"]), 7)
+ def test_tier_equals_product_up_to_four_rows(self):
+ rng = random.Random(7)
+ for _ in range(2000):
+ ps = [rng.choice([rng.random(), 1.0 - 1e-3 * rng.random()]) for _ in range(7)]
+ self.assertEqual(early(sp.TierRule(0.6, 0.3, 3, 0.9, 0.5), ps), early(sp.ProductRule(0.6, 0.3, 3), ps))
+ k, fw = early(sp.TierRule(0.6, 0.3, 7, 0.9, 0.5), ps)
+ kp, fwp = early(sp.ProductRule(0.6, 0.3, 3), ps)
+ if k <= 3: self.assertEqual(k, kp) # same rows as the shipped rule when the deep tier adds none
+ else: self.assertEqual(kp, 3) # a deep round is always the shipped rule's 4-row round plus extra rows
+ self.assertEqual(early(sp.TierRule(0.6, 0.3, 7, 0.6, 0.3), ps), early(sp.ProductRule(0.6, 0.3, 7), ps))
+
+ def test_tier_deep_gate(self):
+ r = sp.TierRule(0.6, 0.3, 7, 0.9, 0.5)
+ self.assertEqual(early(r, [0.99] * 7), (7, 7)) # confident chain goes to the cap
+ self.assertEqual(early(r, [0.95, 0.95, 0.95, 0.9]), (3, 3)) # reach 0.857 < thfd: stops at depth 3, like the default rule
+ self.assertEqual(early(r, [0.99, 0.99, 0.99, 0.5, 0.9]), (3, 4)) # depth 4 drafted, reach 0.48 < thvd: dropped
+ self.assertEqual(early(r, [0.99, 0.99, 0.99, 0.6, 0.9]), (4, 4)) # kept (0.58 >= 0.5), reach < thfd: stop
+ os.environ["QWSPEC_RULE"] = "tier"
+ try: self.assertEqual((sp.rule_from_env(0.6, 0.3, 7).name, sp.rule_from_env(0.6, 0.3, 7).thfd), ("tier", 0.9))
+ finally: del os.environ["QWSPEC_RULE"]
+
def test_env_defaults(self):
for k in ("QWSPEC_THF", "QWSPEC_THV", "QWSPEC_MAXD", "QWSPEC_RULE"): os.environ.pop(k, None)
self.assertEqual(sp.env_defaults(3), (0.6, 0.3, 3))