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.

Two-stage draft head for the MTP drafters (opt-in: EXL3_DRAFT2S=<pack>)

YanissAmz3f66a26bdb37
diff --git a/exllamav3/architecture/glm5_next_mtp.py b/exllamav3/architecture/glm5_next_mtp.py
index a9cca19..7880d4f 100644
--- a/exllamav3/architecture/glm5_next_mtp.py
+++ b/exllamav3/architecture/glm5_next_mtp.py
@@ -281,6 +281,17 @@ class Glm5NextMTPModel(Model):
ll = self.attached_model().logit_layer_idx
lm = self.attached_model().modules[ll]
logits = lm.prepare_for_device(state, params)
+ # EXL3_DRAFT2S: two-stage draft head (modules/draft2s.py) for 1-row draft steps (not for sampled jobs: they need the full law); conf = the exact argmax logit
+ lead = logits.shape[:-1]
+ if lead.numel() == 1 and os.environ.get("EXL3_DRAFT2S") and not params.get("export_draft_logits"):
+ from ..modules import draft2s
+ d2s = draft2s.head_for(lm, self.attached_model().config.vocab_size)
+ if d2s is not None:
+ ids, p, conf = d2s(logits)
+ params["draft_p"] = p.clone()
+ if params.get("export_draft_conf"):
+ params["draft_conf"] = conf.clone().view(lead)
+ return ids.clone().view(lead)
logits = lm.forward(logits, params)
if params.get("export_draft_logits"):
params["draft_logits"] = logits # spec_sample: the drafter's law for sampled jobs
diff --git a/exllamav3/architecture/qwen4_exp_mtp.py b/exllamav3/architecture/qwen4_exp_mtp.py
index ae8888f..273f82a 100644
--- a/exllamav3/architecture/qwen4_exp_mtp.py
+++ b/exllamav3/architecture/qwen4_exp_mtp.py
@@ -10,6 +10,7 @@ from ..modules import Embedding, Linear, GatedResidual
from ..modules.module import Module
from ..modules.arch_specific.qwen4_exp_mtp import Qwen4ExpMTPInputLayer
from ..modules.attn import prepare_for_attn
+import os
from typing import TYPE_CHECKING
if TYPE_CHECKING:
@@ -176,6 +177,17 @@ class Qwen4ExpMTPModel(Model):
ll = self.attached_model().logit_layer_idx
lm = self.attached_model().modules[ll]
logits = lm.prepare_for_device(state, params)
+ # EXL3_DRAFT2S: two-stage draft head (modules/draft2s.py); its max probability goes to the depth rule via params
+ d2s = None
+ if bsz * seq == 1 and os.environ.get("EXL3_DRAFT2S") and not params.get("export_draft_logits"):
+ from ..modules import draft2s
+ d2s = draft2s.head_for(lm, self.attached_model().config.vocab_size)
+ if d2s is not None:
+ ids, p, conf = d2s(logits)
+ params["draft_p"] = p.clone()
+ if params.get("export_draft_conf"):
+ params["draft_conf"] = conf.clone().view(bsz, seq)
+ return ids.clone().view(bsz, seq)
logits = lm.forward(logits, params)
if params.get("export_draft_logits"):
params["draft_logits"] = logits # spec_sample: the drafter's law for sampled jobs
diff --git a/exllamav3/modules/draft2s.py b/exllamav3/modules/draft2s.py
new file mode 100644
index 0000000..88623d1
--- /dev/null
+++ b/exllamav3/modules/draft2s.py
@@ -0,0 +1,250 @@
+"""Two-stage draft head (opt-in: EXL3_DRAFT2S=<pack.safetensors>, built by tools/qwen/draft2s_pack.py).
+
+A draft step needs one argmax and one max probability over the whole vocabulary. Today it reads the whole EXL3 head
+(Qwen 2560 x 248,320: 318 MB, 1.35 ms). This head reads ~70 MB instead:
+ tier 1 rotation xr = E @ h (512 axes) and a 4-bit preview score of every row; 4096-bin histogram of the score keys;
+ threshold = smallest bin whose suffix count >= M (pack meta "m", default 512; GLM 4096-wide head: 1024);
+ compaction of the rows above it (~1.2 M, capped at CAP)
+ tier 2 a 3-bit row copy (input Hadamard per 128 group) scores the candidates; top-16 by that score
+ tier 3 the EXL3 head itself on the 16 candidates' 128-row blocks (trellis tiles gathered, production exl3_dec_gemv):
+ exact logits, bit-identical to the full head's
+ argmax = best exact logit among the 16; p = 1 / sum over candidates exp(v - max), v = exact logit for the 16, the
+ 3-bit score for the others (rows outside the candidates are left out: they hold a median 0.0000 / 90th pct 0.0011 of
+ the normaliser on held-out Qwen draft steps, and dropping them changes the replayed served rule by <= 0.12 point).
+The target verifies every draft, so this head cannot change the text, only how many drafts are accepted (box1 NOTEBOOK
+Results 3-7: argmax = full head on 99.70 % of held-out steps).
+All index maths are bounds-checked on the CPU in box1 desk/draft2s_head_bounds.py (masked loads, clamped rows)."""
+import os
+import torch
+import triton
+import triton.language as tl
+from safetensors import safe_open
+from safetensors.torch import load_file
+
+NB, M, CAP, TOPN = 4096, 512, 2048, 16
+
+
+def _rot_kernel(E, x, xr, had, hq, hist, cnt, A, NA, H: tl.constexpr, G: tl.constexpr, RB: tl.constexpr, NB: tl.constexpr):
+ # programs < NA: xr = E @ x (A rows); programs >= NA: hq = per-group Hadamard of x (H rows); program 0 zeroes the histogram
+ pid = tl.program_id(0)
+ if pid < NA:
+ rows = pid * RB + tl.arange(0, RB)
+ rm = rows < A
+ acc = tl.zeros((RB,), dtype=tl.float32)
+ for k0 in tl.static_range(0, H, 512):
+ k = k0 + tl.arange(0, 512)
+ km = k < H
+ e = tl.load(E + rows[:, None] * H + k[None, :], mask=rm[:, None] & km[None, :], other=0.0).to(tl.float32)
+ xv = tl.load(x + k, mask=km, other=0.0).to(tl.float32)
+ acc += tl.sum(e * xv[None, :], axis=1)
+ tl.store(xr + rows, acc, mask=rm)
+ else:
+ kh = (pid - NA) * RB + tl.arange(0, RB)
+ khm = kh < H
+ jj = tl.arange(0, G)
+ hm = tl.load(had + (kh % G)[:, None] * G + jj[None, :], mask=khm[:, None], other=0.0).to(tl.float32)
+ xh = tl.load(x + (kh // G)[:, None] * G + jj[None, :], mask=khm[:, None], other=0.0).to(tl.float32)
+ tl.store(hq + kh, tl.sum(hm * xh, axis=1), mask=khm)
+ if pid == 0:
+ b = tl.arange(0, NB)
+ tl.store(hist + b, tl.zeros((NB,), dtype=tl.int32))
+ tl.store(cnt + tl.arange(0, 1), tl.zeros((1,), dtype=tl.int32))
+
+
+def _key_bin(s):
+ u = s.to(tl.uint32, bitcast=True)
+ k = tl.where((u >> 31) != 0, ~u, u | 0x80000000) # order-preserving float -> uint32
+ return (k >> 20).to(tl.int32) # 4096 bins
+
+
+def _preview_kernel(codes, scale, xr, out, hist, V, AW: tl.constexpr, AWP: tl.constexpr, BR: tl.constexpr):
+ pid = tl.program_id(0)
+ rows = pid * BR + tl.arange(0, BR)
+ rm = rows < V
+ wi = tl.arange(0, AWP)
+ wm = wi < AW
+ w = tl.load(codes + rows[:, None] * AW + wi[None, :], mask=rm[:, None] & wm[None, :], other=0)
+ acc = tl.zeros((BR,), dtype=tl.float32)
+ for i in tl.static_range(8):
+ xi = tl.load(xr + wi * 8 + i, mask=wm, other=0.0)
+ acc += tl.sum((((w >> (4 * i)) & 15).to(tl.float32) - 7.5) * xi[None, :], axis=1)
+ s = acc * tl.load(scale + rows, mask=rm, other=0.0)
+ tl.store(out + rows, s, mask=rm)
+ tl.atomic_add(hist + _key_bin(s), 1, mask=rm)
+
+
+def _thresh_kernel(hist, thr, M, NB: tl.constexpr):
+ b = tl.arange(0, NB)
+ h = tl.load(hist + b)
+ suf = tl.sum(h) - tl.cumsum(h, axis=0) + h # rows with bin >= b
+ best = tl.max(tl.where(suf >= M, b, -1))
+ tl.store(thr + tl.arange(0, 1), tl.full((1,), 0, tl.int32) + best)
+
+
+def _compact_kernel(scores, thr, cnt, idx, V, CAP, BR: tl.constexpr):
+ pid = tl.program_id(0)
+ rows = pid * BR + tl.arange(0, BR)
+ rm = rows < V
+ s = tl.load(scores + rows, mask=rm, other=float("-inf"))
+ t = tl.load(thr)
+ sel = rm & (_key_bin(s) >= t)
+ pos = tl.atomic_add(cnt + tl.zeros((BR,), dtype=tl.int32), 1, mask=sel)
+ tl.store(idx + pos, rows, mask=sel & (pos < CAP))
+
+
+def _row3_kernel(q3, q3s, hq, idx, cnt, out, CAP, V, D: tl.constexpr, QW: tl.constexpr, QWP: tl.constexpr,
+ G: tl.constexpr, NG: tl.constexpr):
+ # one program per candidate slot: score = sum_k (code - 4) * scale[row, k // G] * hq[k], k = 10 w + i
+ m = tl.program_id(0)
+ n = tl.minimum(tl.load(cnt), CAP)
+ live = m < n
+ r = tl.load(idx + m, mask=live, other=0)
+ r = tl.minimum(tl.maximum(r, 0), V - 1).to(tl.int64)
+ wi = tl.arange(0, QWP)
+ wm = (wi < QW) & live
+ w = tl.load(q3 + r * QW + wi, mask=wm, other=0)
+ acc = tl.zeros((QWP,), dtype=tl.float32)
+ for i in tl.static_range(10):
+ k = wi * 10 + i
+ km = wm & (k < D)
+ sc = tl.load(q3s + r * NG + k // G, mask=km, other=0.0).to(tl.float32)
+ xv = tl.load(hq + k, mask=km, other=0.0).to(tl.float32)
+ acc += tl.where(km, (((w >> (3 * i)) & 7).to(tl.float32) - 4.0) * sc * xv, 0.0)
+ tl.store(out + m, tl.where(live, tl.sum(acc), float("-inf")))
+
+
+def _select_kernel(s2, idx, cnt, rows16, CAP, V, TOPN: tl.constexpr, CAPP: tl.constexpr):
+ # top-TOPN candidate slots by tier-2 score (first index on ties); their rows (clamped) -> rows16; their s2 -> -inf so
+ # the finish kernel's normaliser counts them once, with their exact logits
+ m = tl.arange(0, CAPP)
+ n = tl.minimum(tl.load(cnt), CAP)
+ live = m < n
+ s = tl.load(s2 + m, mask=live, other=float("-inf"))
+ for t in tl.static_range(TOPN):
+ mv = tl.max(s)
+ pos = tl.min(tl.where(s == mv, m, CAPP - 1))
+ r = tl.load(idx + pos)
+ r = tl.minimum(tl.maximum(r, 0), V - 1).to(tl.int64)
+ tl.store(rows16 + t + tl.arange(0, 1), tl.full((1,), 0, tl.int64) + r)
+ s = tl.where(m == pos, float("-inf"), s)
+ tl.store(s2 + m, s, mask=live)
+
+
+def _gather_kernel(tre, svh, rows16, sub, svs, NT, TOPN: tl.constexpr, TW8: tl.constexpr, TW8P: tl.constexpr):
+ # program (kt, j): the 8 n-tiles of candidate j's 128-row block (contiguous in the trellis) -> sub[kt, 8 j : 8 j + 8]
+ kt = tl.program_id(0).to(tl.int64)
+ j = tl.program_id(1)
+ blk = tl.minimum(tl.load(rows16 + j) // 128, NT // 8 - 1)
+ e = tl.arange(0, TW8P)
+ em = e < TW8
+ v = tl.load(tre + kt * NT * TW8 // 8 + blk * TW8 + e, mask=em, other=0)
+ tl.store(sub + kt * TOPN * TW8 + j * TW8 + e, v, mask=em)
+ if kt == 0:
+ i = tl.arange(0, 128)
+ tl.store(svs + j * 128 + i, tl.load(svh + blk * 128 + i))
+
+
+def _finish_kernel(s2, cnt, rows16, y16, ids, pout, conf, CAP, TOPN: tl.constexpr, CAPP: tl.constexpr):
+ # exact logits of the TOPN short-listed rows (y16[j * 128 + row % 128]); argmax among them; normaliser over the candidates
+ j = tl.arange(0, TOPN)
+ rj = tl.load(rows16 + j)
+ lj = tl.load(y16 + j * 128 + rj % 128).to(tl.float32)
+ mx = tl.max(lj)
+ best = tl.min(tl.where(lj == mx, j, TOPN))
+ rid = tl.sum(tl.where(j == best, rj, 0))
+ n = tl.minimum(tl.load(cnt), CAP)
+ m = tl.arange(0, CAPP)
+ live = m < n
+ s = tl.load(s2 + m, mask=live, other=float("-inf"))
+ z = tl.sum(tl.where(live, tl.exp(s - mx), 0.0)) + tl.sum(tl.exp(lj - mx))
+ tl.store(ids + tl.arange(0, 1), tl.full((1,), 0, tl.int64) + rid.to(tl.int64))
+ tl.store(pout + tl.arange(0, 1), tl.full((1,), 0.0, tl.float32) + 1.0 / z)
+ tl.store(conf + tl.arange(0, 1), tl.full((1,), 0.0, tl.float32) + mx)
+
+
+class Draft2SHead:
+ """Bound to one EXL3 head (LinearEXL3 inner: trellis / suh / svh, mul1 codebook). __call__(h [1, D] fp16) -> (ids [1, 1]
+ int64, p [1] fp32, conf [1] fp32 = best exact logit). Fixed shapes, no host sync: graph-capturable."""
+
+ def __init__(self, pack_path: str, inner, vocab: int):
+ from exllamav3.ext import exllamav3_ext as ext
+ from exllamav3.modules.quant.exl3 import dec_workspace
+ self.ext = ext
+ dev = inner.trellis.device
+ P = load_file(pack_path, device=str(dev))
+ with safe_open(pack_path, "pt") as fm: self.M = int((fm.metadata() or {}).get("m", M))
+ self.E, self.pcodes, self.pscale = P["E"].contiguous(), P["pcodes"].contiguous(), P["pscale"].float().contiguous()
+ self.q3, self.q3s, self.had = P["q3"].contiguous(), P["q3s"].contiguous(), P["had"].contiguous()
+ self.A, self.D = self.E.shape
+ self.V = self.pcodes.shape[0]
+ self.G = self.had.shape[0]
+ assert self.V == vocab and self.D == inner.in_features and self.D % self.G == 0
+ assert self.q3.shape == (self.V, triton.cdiv(self.D, 10)) and self.q3s.shape == (self.V, self.D // self.G)
+ self.tre, self.suh, self.svh, self.K = inner.trellis, inner.suh, inner.svh, inner.K
+ self.KT, self.NT, self.TW = self.tre.shape
+ self.nblk = inner.out_features // 128
+ assert self.nblk * 128 >= self.V
+ f = lambda *s, dt=torch.float32: torch.empty(*s, dtype=dt, device=dev)
+ self.xr, self.scores = f(self.A), f(self.V)
+ self.hist, self.cnt, self.thr = f(NB, dt=torch.int32), f(1, dt=torch.int32), f(1, dt=torch.int32)
+ self.idx = torch.zeros(CAP, dtype=torch.int32, device=dev)
+ self.s2, self.hq, self.rows16 = f(CAP), f(self.D), torch.zeros(TOPN, dtype=torch.long, device=dev)
+ self.sub = torch.empty(self.KT, TOPN * 8, self.TW, dtype=self.tre.dtype, device=dev)
+ self.svs = torch.empty(TOPN * 128, dtype=self.svh.dtype, device=dev)
+ self.y16 = f(1, TOPN * 128, dt=torch.half)
+ self.ids, self.p, self.conf = f(1, 1, dt=torch.long), f(1), f(1)
+ self.scratch, self.counters = dec_workspace(dev)
+ self.AW = self.A // 8
+ self.AWP = 1 << (self.AW - 1).bit_length()
+ self.QW = triton.cdiv(self.D, 10) # the row kernel masks k >= D
+ self.QWP = 1 << (self.QW - 1).bit_length()
+ self.NA = triton.cdiv(self.A, 8)
+ self.TW8 = 8 * self.TW
+ self.TW8P = 1 << (self.TW8 - 1).bit_length()
+
+ def __call__(self, h: torch.Tensor):
+ x = h.reshape(self.D)
+ if x.dtype != torch.half: x = x.half()
+ x = x.contiguous()
+ _rot_kernel[(self.NA + triton.cdiv(self.D, 8),)](self.E, x, self.xr, self.had, self.hq, self.hist, self.cnt, self.A,
+ self.NA, H=self.D, G=self.G, RB=8, NB=NB)
+ _preview_kernel[(triton.cdiv(self.V, 32),)](self.pcodes, self.pscale, self.xr, self.scores, self.hist, self.V,
+ AW=self.AW, AWP=self.AWP, BR=32)
+ _thresh_kernel[(1,)](self.hist, self.thr, self.M, NB=NB)
+ _compact_kernel[(triton.cdiv(self.V, 1024),)](self.scores, self.thr, self.cnt, self.idx, self.V, CAP, BR=1024)
+ _row3_kernel[(CAP,)](self.q3, self.q3s, self.hq, self.idx, self.cnt, self.s2, CAP, self.V, D=self.D, QW=self.QW,
+ QWP=self.QWP, G=self.G, NG=self.D // self.G)
+ _select_kernel[(1,)](self.s2, self.idx, self.cnt, self.rows16, CAP, self.V, TOPN=TOPN, CAPP=CAP)
+ _gather_kernel[(self.KT, TOPN)](self.tre, self.svh, self.rows16, self.sub, self.svs, self.NT, TOPN=TOPN, TW8=self.TW8,
+ TW8P=self.TW8P)
+ self.ext.exl3_dec_gemv(x.view(1, self.D), self.sub, self.suh, self.svs, self.y16, self.scratch, self.counters, self.K)
+ _finish_kernel[(1,)](self.s2, self.cnt, self.rows16, self.y16, self.ids, self.p, self.conf, CAP, TOPN=TOPN, CAPP=CAP)
+ return self.ids, self.p, self.conf
+
+
+_heads = {}
+
+
+def head_for(lm, vocab):
+ """The Draft2SHead for this lm_head (a Linear wrapper) when EXL3_DRAFT2S names a pack and the head qualifies (EXL3 mul1
+ inner, no LoRA / bias / logit scaling, pack built for this vocabulary), else None. One per head, built on first use."""
+ path = os.environ.get("EXL3_DRAFT2S", "")
+ if not path: return None
+ key = id(lm)
+ if key not in _heads:
+ inner = getattr(lm, "inner", None)
+ ok = (type(inner).__name__ == "LinearEXL3" and getattr(inner, "mul1", False) and not getattr(inner, "mcg", False)
+ and inner.bias is None and not lm.lora_a_tensors and lm.pre_scale == 1.0 and lm.softcap == 0.0
+ and lm.post_scale == 1.0 and inner.out_features % 128 == 0)
+ _heads[key] = Draft2SHead(path, inner, vocab) if ok else None
+ print(f" -- EXL3_DRAFT2S: {'two-stage draft head ON' if ok else 'head not eligible, OFF'} ({path})")
+ return _heads[key]
diff --git a/tools/qwen/draft2s_pack.py b/tools/qwen/draft2s_pack.py
new file mode 100644
index 0000000..b1f11e1
--- /dev/null
+++ b/tools/qwen/draft2s_pack.py
@@ -0,0 +1,66 @@
+# Two-stage draft head (EXL3_DRAFT2S), offline pack builder (CPU).
+# Tier 1 preview: a data-aware rotation of the head input (512 axes, fit in the head's metric on captured draft-head inputs)
+# and 4-bit codes of the rotated head rows, one scale per row: score[v] = pscale[v] * sum_j (code[v, j] - 7.5) * (h @ E)[j].
+# Tier 2 row copy: the head rows after a 128-point input Hadamard per group, 3 bits per weight, one fp16 scale per (row,
+# group): row-addressable, unlike the EXL3 head (its output Hadamard mixes 128 rows).
+# Tier 3 is the EXL3 head itself (exact logits of the short-listed 128-row blocks): nothing to pack.
+# Maths = box1 desk/composed_check.py and night1 qw0/head_sim3.py (recall 0.9958 at m = 512 on held-out steps).
+# usage: draft2s_pack.py W.npy HDIR OUT.safetensors (W.npy = the dequantized head (in_features, vocab); HDIR = rec.json +
+# h_*.npy of a capture; only the "train" split is used for the fit)
+import os, sys, json
+for k in ("OMP_NUM_THREADS", "OPENBLAS_NUM_THREADS", "MKL_NUM_THREADS"): os.environ.setdefault(k, "8")
+import numpy as np
+import torch
+from safetensors.torch import save_file
+
+AXES, GROUP = int(os.environ.get("DRAFT2S_AXES", 512)), 128
+M = int(os.environ.get("DRAFT2S_M", 512)) # tier-1 target count; GLM 4096-wide head: 1024 (box1 NOTEBOOK Result 11)
+
+def hadamard(n):
+ h = np.array([[1.0]], dtype=np.float32)
+ while h.shape[0] < n: h = np.block([[h, h], [h, -h]])
+ return h / np.sqrt(n)
+
+def build(W, Htr):
+ d, V = W.shape
+ G = W @ W.T; ew, U = np.linalg.eigh(G.astype(np.float64)); U = U[:, ::-1].astype(np.float32); ew = ew[::-1]
+ C = (Htr.T @ Htr) / len(Htr)
+ S = (U * np.sqrt(np.maximum(ew, 1e-12)).astype(np.float32)) @ U.T
+ em, Q = np.linalg.eigh((S @ C @ S).astype(np.float64)); Q = Q[:, ::-1].astype(np.float32)
+ Sinv = (U / np.sqrt(np.maximum(ew, 1e-12)).astype(np.float32)) @ U.T
+ E = (S @ Q)[:, :AXES]; B = W.T @ (Sinv @ Q)[:, :AXES]; del G
+ step = 0.335 * (B.std(0) + 1e-12)
+ q = np.floor(B / step).clip(-8, 7)
+ Bq = (q + 0.5) * step
+ al = (B * Bq).sum(1) / np.maximum((Bq * Bq).sum(1), 1e-20)
+ code = (q + 8).astype(np.uint32) # [V, 512] in [0, 15]
+ pcodes = np.zeros((V, AXES // 8), np.uint32)
+ for i in range(8): pcodes |= code[:, i::8] << (4 * i) # nibble i of word w = axis 8 w + i
+ del B, Bq, q, code
+ HAD = hadamard(GROUP)
+ Wg = np.einsum("ij,bjv->biv", HAD, W.reshape(d // GROUP, GROUP, V)) # [groups, 128, V]
+ s3 = (np.abs(Wg).max(1) / 3).astype(np.float16) # [groups, V]
+ s3f = s3.astype(np.float32); s3f[s3f == 0] = 1
+ c3 = (np.clip(np.round(Wg / s3f[:, None, :]), -4, 3) + 4).astype(np.uint32).reshape(d, V).T # [V, d] in [0, 7]
+ del Wg
+ qw = -(-d // 10) # words per row; d % 10 != 0 (GLM 4096): pad code 4 (= 0)
+ if qw * 10 > d: c3 = np.concatenate([c3, np.full((V, qw * 10 - d), 4, np.uint32)], 1)
+ q3 = np.zeros((V, qw), np.uint32)
+ for i in range(10): q3 |= c3[:, i::10] << (3 * i) # bits 3i..3i+2 of word w = input 10 w + i
+ del c3
+ t = lambda a: torch.from_numpy(np.ascontiguousarray(a))
+ return {"E": t((E * step[None, :]).T.astype(np.float16)), # [512, d]: xr = E @ h (the code step folded in)
+ "pcodes": t(pcodes.view(np.int32)), "pscale": t(al.astype(np.float32)),
+ "q3": t(q3.view(np.int32)), "q3s": t(s3.T.copy()), "had": t(HAD.astype(np.float16))}
+
+if __name__ == "__main__":
+ WP, HD, OUT = sys.argv[1:4]
+ R = json.load(open(f"{HD}/rec.json"))
+ V = R["head"]["vocab"]
+ W = np.ascontiguousarray(np.load(WP, mmap_mode="r")[:, :V], dtype=np.float32)
+ Htr = np.concatenate([np.load(f"{HD}/h_{i}.npy").astype(np.float32) for i, r in enumerate(R["rec"]) if r["split"] == "train"])
+ P = build(W, Htr)
+ meta = {"format": "draft2s-v1", "axes": str(AXES), "group": str(GROUP), "m": str(M), "vocab": str(V), "in_features": str(W.shape[0]),
+ "fit_rows": str(len(Htr))}
+ save_file(P, OUT, metadata=meta)
+ print(OUT, {k: (tuple(v.shape), str(v.dtype)) for k, v in P.items()}, f"{sum(v.numel() * v.element_size() for v in P.values()) / 1e6:.1f} MB")
diff --git a/tools/qwen/spec_policy.py b/tools/qwen/spec_policy.py
index fdf69d1..05c9901 100644
--- a/tools/qwen/spec_policy.py
+++ b/tools/qwen/spec_policy.py
@@ -443,10 +443,12 @@ def install(g, dm, model, thf=0.6, thv=0.3, maxd=3, ngram=None, stats=None, rule
return orig_fwd(*a, **k)
def sfs(state, params):
ids = orig_sfs(state, params)
+ dp = params.pop("draft_p", None) # EXL3_DRAFT2S head: its max probability (lm.forward did not run: cap["lg"] is stale)
if st["mode"] == "mtp" and not getattr(rule, "needs_p", True):
return ids
if st["mode"] == "mtp":
- pt = torch.softmax(cap["lg"][..., :model.config.vocab_size].float().reshape(-1, model.config.vocab_size), -1).max()
+ if dp is not None: pt = dp.reshape(())
+ else: pt = torch.softmax(cap["lg"][..., :model.config.vocab_size].float().reshape(-1, model.config.vocab_size), -1).max()
i = st["i"]
if fixed is not None:
st["ps"].append(pt); st["i"] = i + 1