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.

Hy4 prefill: merge a short tail (<= 25 % of a chunk, HY4_PF_TAIL_MERGE) into the last chunk instead of running it as its own small chunk (8.6K prompt 12.0 -> 11.5 s, 8.5K 12.35 -> 11.25 s, 33K 66.8 -> 58.5 s; KL 0.123 +- 0.012 vs three noise nulls 0.099-0.126)

YanissAmzb4a4be681b21
diff --git a/exllamav3/loader/hy4_forward.py b/exllamav3/loader/hy4_forward.py
index fae9894..73b4914 100644
--- a/exllamav3/loader/hy4_forward.py
+++ b/exllamav3/loader/hy4_forward.py
@@ -366,6 +366,20 @@ def _mv(o, dev, dtype=None):
# ------------------------------------------------------------------------------------------------ the model
+def chunk_plan(start, total, chunk):
+ """prefill chunk boundaries [(s, e)] for rows start..total: full chunks of `chunk` rows; a tail of at most HY4_PF_TAIL_MERGE * chunk rows (default 0.25)
+ is merged into the last chunk instead of becoming its own small chunk (8K measured: a 408-row tail chunk costs 1.8 s, merging it costs 1.5 s less)"""
+ merge = float(os.environ.get("HY4_PF_TAIL_MERGE", "0.25"))
+ out, s = [], start
+ while s < total:
+ e = min(total, s + chunk)
+ if total - e <= merge * chunk:
+ e = total
+ out.append((s, e))
+ s = e
+ return out
+
+
class Hy4Forward:
_gs = None # step_graph._GS while a decode graph is warmed up / captured (static buffers), else None
@@ -743,8 +757,8 @@ class Hy4Forward:
toks = [int(t) for t in ids_all]
self._eng_pf = {}
ex = ThreadPoolExecutor(max_workers=2)
- for s in range(self.n, len(toks), chunk):
- n = min(chunk, len(toks) - s)
+ for s, e in chunk_plan(self.n, len(toks), chunk):
+ n = e - s
for i in layers:
self._eng_pf[(i, s, n)] = ex.submit(self.eng[i].host_rows, toks, s, n)
ex.shutdown(wait=False)
diff --git a/tools/hy4/engine.py b/tools/hy4/engine.py
index 71570da..a636d26 100644
--- a/tools/hy4/engine.py
+++ b/tools/hy4/engine.py
@@ -173,8 +173,7 @@ class Hy4Engine:
self.phase("prefill")
M.prefetch_engram(ids, self.chunk)
states, lg = [], None
- for s in range(0, n, self.chunk):
- e = min(n, s + self.chunk)
+ for s, e in self.hf.chunk_plan(0, n, self.chunk):
lg = M.step(ids[s:e])
states.append(M.head_state(M._x))
M._x = None