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: the MTP draft-cache fill writes only K / V + indexer key state (no queries, selection, attention, MoE)

YanissAmzbd302dcd8a81
diff --git a/exllamav3/loader/mtp_head.py b/exllamav3/loader/mtp_head.py
index ffa6362..99fd348 100644
--- a/exllamav3/loader/mtp_head.py
+++ b/exllamav3/loader/mtp_head.py
@@ -160,6 +160,24 @@ class Hy4MTP:
y = nm(s2, self.shn)
return y, self.logits(y[-1:].float())
+ @torch.no_grad()
+ def fill(self, tok, h, pos):
+ """draft-cache fill of prompt rows: writes ONLY what the draft chain reads at decode (K row of every position, indexer value / gate rows, pool groups). All of it depends on the
+ layer input alone (token embedding + trunk state), so it is the same state as step() leaves, without the queries, the selection, the attention and the 512-expert MoE whose outputs
+ a fill discards. tok / h / pos as in step()."""
+ nm = self._rmsw
+ e = _ns.embed(self.fw, tok)
+ x = torch.cat([nm(e, self.enorm), nm(h, self.hnorm)], -1) @ self.eh.T
+ cur = nm(x, self.attn_norm)
+ self.kbuf.index_copy_(0, pos, self.mla.kv(cur, pos))
+ SG.indexer_keys_static(self.ix, cur, pos, self.ar)
+
+ def prime(self, tok, h, pos, N):
+ """one batched draft-cache fill call (HY4_PRIME_KVONLY=0: the full step, whose outputs are dropped)"""
+ if os.environ.get("HY4_PRIME_KVONLY", "1") == "1":
+ return self.fill(tok, h, pos)
+ return self.step(tok, h, pos, N)
+
def _rmsw(self, x, w):
if self.fast:
from . import attn_eager as _AE
diff --git a/exllamav3/loader/step_graph.py b/exllamav3/loader/step_graph.py
index 1427707..bc07495 100644
--- a/exllamav3/loader/step_graph.py
+++ b/exllamav3/loader/step_graph.py
@@ -73,11 +73,9 @@ def indexer_static_init(ix, cap):
@torch.no_grad()
-def indexer_forward_static(ix, x, qr, pos, Nb, ar):
- """x [T, E], qr [T, R], pos [T] long (device, ascending), Nb rows -> allow [T, Nb] bool (T = 1 decode, T > 1 verify rows). Same maths and op order as FusedKPoolIndexer.forward for one
- token; no host value is read: group slot and rope position come from `pos`, the visible groups from a mask (gend < pos).
- Returns None (all keys causal) while the bucket is small enough that no query can see more than n_top groups: only the pool state is updated."""
- k, d, H = ix.k, ix.d, ix.H
+def indexer_keys_static(ix, x, pos, ar):
+ """key side of indexer_forward_static: the per-position (value | gate) rows and the pool groups they complete (x [T, E], pos [T] long device, ascending). Needs no query and no selection."""
+ k, d = ix.k, ix.d
ik = _rms(x @ ix.w["attn_k"].T, ix.w["k_norm"], ix.eps)
vg_new = torch.cat([ik @ ix.w["value"].T, ik @ ix.w["gate"].T], -1)
vr = getattr(ix, "vg_ring", 0)
@@ -93,6 +91,15 @@ def indexer_forward_static(ix, x, qr, pos, Nb, ar):
p = (torch.softmax(g + ix.w["ape"][None], dim=1) * v).sum(1)
pooled = ix._rope(_rms(p, ix.w["pool_norm"], ix.eps), g0)
ix.pool.index_copy_(0, pos // k, pooled) # a group still open holds partial data here; it stays masked until it is complete
+
+
+def indexer_forward_static(ix, x, qr, pos, Nb, ar):
+ """x [T, E], qr [T, R], pos [T] long (device, ascending), Nb rows -> allow [T, Nb] bool (T = 1 decode, T > 1 verify rows). Same maths and op order as FusedKPoolIndexer.forward for one
+ token; no host value is read: group slot and rope position come from `pos`, the visible groups from a mask (gend < pos).
+ Returns None (all keys causal) while the bucket is small enough that no query can see more than n_top groups: only the pool state is updated."""
+ k, d, H = ix.k, ix.d, ix.H
+ indexer_keys_static(ix, x, pos, ar)
if Nb <= (ix.n_top + 1) * k: # every position of this bucket sees <= n_top groups: the selection is all-causal (the eager path returns None)
return None
T = x.shape[0]
diff --git a/tools/hy4/engine.py b/tools/hy4/engine.py
index 0796c70..adfb4ca 100644
--- a/tools/hy4/engine.py
+++ b/tools/hy4/engine.py
@@ -18,7 +18,7 @@ import types
from typing import Callable
CHUNK_DEFAULT = 8192
-PRIME_ROWS = 2048 # rows per batched MTP draft-cache fill call
+PRIME_ROWS = int(os.environ.get("HY4_PRIME_ROWS", "2048")) # rows per batched MTP draft-cache fill call
def _stub_package(tree: str):
@@ -235,7 +235,7 @@ class Hy4Engine:
try:
while s < n - 1:
e = min(n - 1, s + PRIME_ROWS)
- MT.step(tok[s + 1:e + 1], H[s:e], ar[s:e], ((e + 3) // 4) * 4)
+ MT.prime(tok[s + 1:e + 1], H[s:e], ar[s:e], ((e + 3) // 4) * 4)
s = e
self.last_stats["prime_mode"] = "batched" if lo == 0 else f"batched tail {n - 1 - lo}"
except Exception as ex: # noqa: BLE001 - the draft only affects speed, never the output: fall back to the last 128 rows one by one
diff --git a/tools/hy4/mu_engine.py b/tools/hy4/mu_engine.py
index 427ce98..ae9a14d 100644
--- a/tools/hy4/mu_engine.py
+++ b/tools/hy4/mu_engine.py
@@ -58,7 +58,7 @@ class MuEngine(Hy4Engine):
f16 = os.environ.get("HY4_PF_F16")
os.environ["HY4_PF_F16"] = "0" # the fp16 attention chain is tuned for the trunk's matrices; the draft layer takes the generic prefill path
try:
- MT.step(s.tok_dev[a + 1:e_end + 1], hs[:e_end - a], MT.ar[a:e_end], ((e_end + 3) // 4) * 4)
+ MT.prime(s.tok_dev[a + 1:e_end + 1], hs[:e_end - a], MT.ar[a:e_end], ((e_end + 3) // 4) * 4)
finally:
if f16 is None:
os.environ.pop("HY4_PF_F16", None)