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.

n-gram index: truncate(0) rebuilds the empty state directly (20 ms -> 1 ms per new conversation at 8K, state identical, fuzz 10000 OK)

YanissAmz5accdcdb94fc
diff --git a/tools/qwen/spec_policy.py b/tools/qwen/spec_policy.py
index 22ffc69..538f120 100644
--- a/tools/qwen/spec_policy.py
+++ b/tools/qwen/spec_policy.py
@@ -77,6 +77,15 @@ class NgramIndex:
def truncate(self, p):
n = len(self.toks)
assert 0 <= p <= n
+ if p == 0 and n:
+ # a whole new context (a new conversation): the walk below undoes every push one by one (11-23 ms at 8K tokens, inside the
+ # time to first token) and ends in the freshly built state, so build that state directly (checked by test_ngram_index)
+ lmax, nmin = self.lmax, self.nmin
+ self.toks = array("i")
+ self.key = [None] + [array("q", [-1]) for _ in range(lmax)]
+ self.prev = [None] + [array("i", [-1]) if m >= nmin else None for m in range(1, lmax + 1)]
+ self.latest = [None] + [{} if m >= nmin else None for m in range(1, lmax + 1)]
+ return
key, prev, latest = self.key, self.prev, self.latest
for e in range(n, p, -1):
for m in range(self.nmin, self.lmax + 1):