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.
Python amdapple-siliconcudadeepseekexl3exllamav3glminferencellmllm-inferencelocal-llmnvidiaopenai-apiquantizationqwenrocmstrix-halo
Hy4 engine: prompt-lookup n-gram rows on top of the MTP drafts (HY4_NGRAM_EXT/MIN/FILL, off by default) and true-pair head rows for accepted positions the draft chain never wrote
YanissAmzbe0dbc6d0f74
diff --git a/tools/hy4/engine.py b/tools/hy4/engine.pyindex 55c9535..96c19bf 100644--- a/tools/hy4/engine.py+++ b/tools/hy4/engine.py@@ -34,6 +34,9 @@ class Hy4Engine:self.tree, self.gguf, self.k, self.chunk = tree, gguf, k, chunkself.ctx = ctxself.prime_rows = prime_rows # 0 = fill the draft cache for the whole prompt; N = the last N rows only (cheaper, lower acceptance)+ self.ngram_ext = int(os.environ.get("HY4_NGRAM_EXT", "0")) # extra verify rows from a prompt-lookup n-gram match (0 = off)+ self.ngram_min = int(os.environ.get("HY4_NGRAM_MIN", "4"))+ self.ngram_fill = os.environ.get("HY4_NGRAM_FILL", "0") == "1"self.ready = Falseself.meta = Noneself.last_stats: dict = {}@@ -193,12 +196,22 @@ class Hy4Engine:k = self.kpc = time.perf_countertm = [0.0, 0.0, 0.0]+ ng = None+ if self.ngram_ext > 0:+ from ngram import NGram+ ng = NGram(self.ngram_min, self.ngram_ext)+ ng.reset(list(ids) + [y])+ n_ext = n_hit = n_extacc = 0while out_n < max_new and not stop and not cancel():j = GR.fw.n - 1ta = pc()d = MG.run(y, hcur, j).tolist()tb = pc()- toks = [y] + d+ ext = ng.propose(d, min(self.ngram_ext, self.SG.TM - 1 - k))[0] if ng is not None else []+ toks = [y] + d + ext+ if ext:+ n_hit += 1+ n_ext += len(ext)_lg, amv, _x, hv = GR.step_rows(toks)tc = pc()amh = amv.tolist()@@ -207,10 +220,18 @@ class Hy4Engine:tm[1] += tc - tbtm[2] += td - tcacc = 0- while acc < k and d[acc] == amh[acc]:+ nd = len(toks) - 1+ while acc < nd and toks[acc + 1] == amh[acc]:acc += 1+ n_extacc += max(0, acc - k)new = toks[1:acc + 1] + [amh[acc]] # toks[0] = y was already emitted; the verified drafts + the trunk's own next tokenGR.rewind(j + 2 + acc)+ if self.ngram_fill and acc >= k: # head rows j + k .. j + acc were never written by the draft chain: true pairs (token p + 1, trunk state h_p)+ MT = self.MT+ N = ((j + acc + 1 + 3) // 4) * 4+ MT.step(torch.tensor(toks[k:acc + 1], dtype=torch.long, device=self.dev), hv[k - 1:acc], MT.ar[j + k:j + acc + 1], N)+ if ng is not None:+ ng.add(new)y, hcur = amh[acc], hv[acc:acc + 1]hist[acc] += 1rounds += 1@@ -220,6 +241,7 @@ class Hy4Engine:out_n += len(new)stop = on_tokens(new)st["round_ms"] = [x / max(rounds, 1) * 1000 for x in tm]+ st["ngram"] = {"ext": self.ngram_ext, "min": self.ngram_min, "fill": self.ngram_fill, "hit_rounds": n_hit, "ext_rows": n_ext, "ext_accepted": n_extacc}else:while out_n < max_new and not stop and not cancel():lg = GR.step(y)diff --git a/tools/hy4/ngram.py b/tools/hy4/ngram.pynew file mode 100644index 0000000..dc3083c--- /dev/null+++ b/tools/hy4/ngram.py@@ -0,0 +1,40 @@+"""Prompt-lookup n-gram proposer for the Hy4 spec loop. Index of every n-gram (n = nmin) of the sequence seen so far (prompt + emitted tokens); a draft of+k MTP tokens is extended with the tokens that followed the latest earlier occurrence of the sequence's current suffix. Pure host code (dict of tuples)."""+++class NGram:+ def __init__(self, nmin=4, ext=4):+ self.nmin, self.ext = nmin, ext+ self.seq: list[int] = []+ self.idx: dict[tuple, int] = {} # n-gram -> index of its LAST token (latest occurrence)++ def reset(self, ids):+ self.seq, self.idx = [], {}+ self.add(ids)++ def add(self, toks):+ n, s, ix = self.nmin, self.seq, self.idx+ for t in toks:+ s.append(int(t))+ if len(s) >= n:+ ix[tuple(s[-n:])] = len(s) - 1++ def propose(self, d, ext=None):+ """d: the MTP drafts (host ints) that follow self.seq. Returns (extra tokens, match length) with extra = [] when there is no usable match."""+ n, s = self.nmin, self.seq+ ext = self.ext if ext is None else ext+ if ext <= 0:+ return [], 0+ ss = s + list(d)+ if len(ss) < n:+ return [], 0+ end = len(ss) - 1+ # the draft region ends at `end`; the index holds only positions < len(s), so a hit is always an earlier occurrence+ i = self.idx.get(tuple(ss[-n:]))+ if i is None or i >= end:+ return [], 0+ m = n # extend the match backwards+ while i - m >= 0 and end - m >= 0 and ss[i - m] == ss[end - m] and m < 32:+ m += 1+ cont = ss[i + 1:i + 1 + ext]+ return cont, m