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 MTP draft head: HY4_MTP_HEAD=q5k reads Q5_K bytes of the bf16 head from HY4_MTP_HEAD_BIN (tools/hy4/make_head_bin.py), Q8_0 fallback when the file is missing

YanissAmz39ab04f3b883
diff --git a/exllamav3/loader/dense_req.py b/exllamav3/loader/dense_req.py
index ab30133..49338ae 100644
--- a/exllamav3/loader/dense_req.py
+++ b/exllamav3/loader/dense_req.py
@@ -231,3 +231,15 @@ def set_head(mt, mg, mode):
import gc
gc.collect()
torch.cuda.empty_cache()
+
+
+def head_from_file(mt, path, kind="q5_k"):
+ """HY4_MTP_HEAD=q5k: the draft head as a K-quant QLinear read from `path` (tools/hy4/make_head_bin.py); returns None when the file does not match the head"""
+ from .hy4_forward import QLinear
+ W = mt.fw.out_w
+ V, D = W.shape
+ n = V * (D // 256) * KQ[kind]["bb"]
+ if not os.path.exists(path) or os.path.getsize(path) != n:
+ return None
+ arr = np.fromfile(path, dtype=np.uint8).reshape(V, D // 256, KQ[kind]["bb"])
+ return QLinear(torch.from_numpy(arr).to(W.device), _KIND_TY[kind], (256, KQ[kind]["bb"], "fq_gemv_" + kind, _dq_fn(kind)), _gemv(kind, "gemv"), None)
diff --git a/exllamav3/loader/mtp_head.py b/exllamav3/loader/mtp_head.py
index fb1f025..a5f1243 100644
--- a/exllamav3/loader/mtp_head.py
+++ b/exllamav3/loader/mtp_head.py
@@ -77,7 +77,13 @@ class Hy4MTP:
# draft head: "" = the trunk's bf16 head (Triton mat-vec), "q8" = Q8_0 copy (half the bytes), "sub<N>" = the first N vocabulary rows only (draft picks among them)
hm = os.environ.get("HY4_MTP_HEAD", "")
self.head_q8 = self.head_sub = self.ids = None
- if hm == "q8":
+ if hm == "q5k": # Q5_K bytes of the bf16 head from a file (make_head_bin.py): 0.255 GB instead of 0.394 GB (Q8_0); falls back to Q8_0 when the file is missing
+ from . import dense_req as _DR
+ self.head_q8 = _DR.head_from_file(self, os.environ.get("HY4_MTP_HEAD_BIN", "/path/to/head_q5_k.bin"))
+ if self.head_q8 is None:
+ print("mtp_head: HY4_MTP_HEAD=q5k but no matching head file, using Q8_0", flush=True)
+ self.head_q8 = q8_linear(fw.out_w.float())
+ elif hm == "q8":
self.head_q8 = q8_linear(fw.out_w.float())
elif hm.startswith("sub"):
n = int(hm[3:])
diff --git a/tools/hy4/make_head_bin.py b/tools/hy4/make_head_bin.py
new file mode 100644
index 0000000..71a645d
--- /dev/null
+++ b/tools/hy4/make_head_bin.py
@@ -0,0 +1,21 @@
+"""make_head_bin.py GGUF OUT.bin [THREADS]: Q5_K bytes of the bf16 output head (ggml_quantize_chunk, no imatrix) for HY4_MTP_HEAD=q5k. CPU only."""
+import os, sys, types
+import numpy as np
+tree = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
+for n, s in (("exllamav3", ""), ("exllamav3.loader", "loader")):
+ m = types.ModuleType(n); m.__path__ = [os.path.join(tree, "exllamav3", s)]; sys.modules[n] = m
+from exllamav3.loader.gguf_collection import GGUFCollection
+from exllamav3.loader.dense_req import quantize_rows
+g = GGUFCollection(sys.argv[1])
+ty, dims, off = g.infos["output.weight"]
+assert ty == 30, ty
+K, N = dims
+raw = np.asarray(g._pread(off, N * K * 2)).view(np.uint16)
+out = []
+for a in range(0, N, 8192):
+ w = (raw[a * K:min(N, a + 8192) * K].astype(np.uint32) << 16).view(np.float32).reshape(-1, K)
+ out.append(quantize_rows(w, "q5_k", int(sys.argv[3]) if len(sys.argv) > 3 else 12))
+q = np.concatenate(out)
+assert q.shape == (N, K // 256 * 176), q.shape
+q.tofile(sys.argv[2])
+print("OK", q.shape, q.nbytes)