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: banded MLA core configs from a random sweep (SWA 14.3 -> 16.6, full 19.6 -> 24.6 TFLOPS, bit-identical output, 8K chunk MLA core 3.0 -> 2.7 s), HY4_PF_WOT=1 opt-in transposed wv_b / wgate / wo GEMM chain (off by default), key-pool sub-timers

YanissAmze6bce16ba1ca
diff --git a/exllamav3/loader/mla_hy4.py b/exllamav3/loader/mla_hy4.py
index d373546..2d9a8bf 100644
--- a/exllamav3/loader/mla_hy4.py
+++ b/exllamav3/loader/mla_hy4.py
@@ -14,6 +14,9 @@ from . import dense_req as _DR
PF_ATTN = os.environ.get("HY4_PREFILL_ATTN", "0") in ("1", "flash")
PF_ATTN_FLASH = os.environ.get("HY4_PREFILL_ATTN", "0") == "flash" # 1 = prefill rows (T > PF_ATTN_MIN): flash-style MLA kernel (prefill_attn.py)
PF_ATTN_MIN = 16
+def pf_wot():
+ return os.environ.get("HY4_PF_WOT", "0") == "1"
+# # HY4_PF_WOT=1: gate output written transposed [H*vd, T] so the wo GEMM reads A column-major (faster vendor kernel at K=8192-16384)
def pf_f16():
return os.environ.get("HY4_PF_F16", "0") == "1"
diff --git a/exllamav3/loader/prefill_attn.py b/exllamav3/loader/prefill_attn.py
index 2ae1852..6e6fcc9 100644
--- a/exllamav3/loader/prefill_attn.py
+++ b/exllamav3/loader/prefill_attn.py
@@ -192,8 +192,8 @@ BCFG = {"BM": 128, "BN": 128, "BK": 32, "warps": 8, "PBN": 128, "PBK": 32, "pwar
# measured (test_pf2, 2048 queries): sliding window H=64 best 7.9 TFLOPS at S=8 MiB; full + allow mask H=32 best 17.7 at S=16 MiB with a 64-wide key tile
-BCFG_SWA = dict(BCFG, S_BYTES=8 << 20)
-BCFG_FULL = dict(BCFG, S_BYTES=16 << 20, BN=64, PBK=64)
+BCFG_SWA = dict(BCFG, S_BYTES=8 << 20, PBN=64, pwarps=4, qs=2) # random config sweep (gb/sweep_att.py, 8K-class shapes): 14.3 -> 16.6 TFLOPS, bit-identical output
+BCFG_FULL = dict(BCFG, S_BYTES=16 << 20, BM=64, BN=64, warps=4, PBN=64, PBK=128, pwarps=4, qs=2) # sweep: 19.6 -> 24.6 TFLOPS, bit-identical
def mla_banded(q_lat, q_pe, kc, qstart, scale, sinks=None, window=0, allow=None, cfg=None, qcat=None, o16=False, ht=False):
@@ -252,9 +252,9 @@ def mla_banded(q_lat, q_pe, kc, qstart, scale, sinks=None, window=0, allow=None,
row0 = t0 * H
_qk_kernel[(triton.cdiv(M, c["BM"]), triton.cdiv(NKP, c["BN"]))](qcat, kc16, sbuf, al, M, NK, NKP, row0, t0, qstart, lo, klo_all, N, H, float(scale) * 1.4426950408889634, T,
D=D, WIN=int(window), HAS_ALLOW=allow is not None, BM=c["BM"], BN=c["BN"], BK=c["BK"], HT=ht,
- num_warps=c["warps"], num_stages=1)
+ num_warps=c["warps"], num_stages=c.get("qs", 1))
_softmax_kernel[(M,)](sbuf, pbuf, sk, NKP, H, BLOCK=triton.next_power_of_2(NKP), HAS_SINK=sinks is not None, num_warps=4)
_pv_kernel[(triton.cdiv(M, c["BM"]), triton.cdiv(R, c["PBN"]))](pbuf, kc16, o, M, NK, NKP, row0, lo, klo_all, H, T, D=D, R=R, BM=c["BM"], BN=c["PBN"], BK=c["PBK"], O16=o16, HT=ht,
- num_warps=c["pwarps"], num_stages=1)
+ num_warps=c["pwarps"], num_stages=c.get("qs", 1))
t0 += Bq
return o
diff --git a/exllamav3/loader/prefill_kernels.py b/exllamav3/loader/prefill_kernels.py
index 2798f8b..8f988ca 100644
--- a/exllamav3/loader/prefill_kernels.py
+++ b/exllamav3/loader/prefill_kernels.py
@@ -375,8 +375,12 @@ def _pf_indexer_forward(self, x, qr, n_keys=None):
self._max_pos = start + T
pos = torch.arange(start, start + T, device=self.dev)
with t("f ix: key pool"):
- ik = _rms(x @ self.w["attn_k"].T, self.w["k_norm"], self.eps)
- vg_new = torch.cat([ik @ self.w["value"].T, ik @ self.w["gate"].T], -1)
+ with t("f ixk: attn_k mm"):
+ ikr = x @ self.w["attn_k"].T
+ with t("f ixk: norm"):
+ ik = _rms(ikr, self.w["k_norm"], self.eps)
+ with t("f ixk: value/gate mm"):
+ vg_new = torch.cat([ik @ self.w["value"].T, ik @ self.w["gate"].T], -1)
buf = vg_new if self.vg is None else torch.cat([self.vg, vg_new], 0)
m = buf.shape[0] // k
if m:
@@ -384,7 +388,8 @@ def _pf_indexer_forward(self, x, qr, n_keys=None):
big = torch.empty(max(2 * self.pool.shape[0], self.P + m), d, device=self.dev)
big[:self.P] = self.pool[:self.P]
self.pool = big
- self.pool[self.P:self.P + m] = self._pool_groups(buf[:m * k], self.P)
+ with t("f ixk: pool groups"):
+ self.pool[self.P:self.P + m] = self._pool_groups(buf[:m * k], self.P)
self.P += m
self.vg = buf[m * k:] if buf.shape[0] - m * k else None
self.n += T
@@ -798,6 +803,52 @@ def gate16(kv, g):
return out
+
+def _gate_ew_kernel(KV, G, OUT, n, BLK: tl.constexpr):
+ """OUT = KV * sigmoid(G) elementwise, the maths of _gate_kernel (gate logits nan -> 0 and clamped, saturating fp16 result); all three operands in the SAME layout"""
+ i = tl.program_id(0).to(tl.int64) * BLK + tl.arange(0, BLK)
+ m = i < n
+ g = tl.load(G + i, mask=m, other=0.0).to(tl.float32)
+ g = tl.where(g != g, 0.0, tl.minimum(tl.maximum(g, -65504.0), 65504.0))
+ kv = tl.load(KV + i, mask=m, other=0.0).to(tl.float32)
+ o = kv * (1.0 / (1.0 + tl.exp(-g)))
+ tl.store(OUT + i, tl.minimum(tl.maximum(o, -65504.0), 65504.0).to(tl.float16), mask=m)
+
+
+def gate16t(kvt, gt):
+ """transposed-layout gate: kvt [H * VD, T] (wv_b GEMM output with the head rows stacked), gt [H * VD, T] (raw gate GEMM output, transposed) -> gated [H * VD, T] fp16"""
+ out = torch.empty_like(kvt)
+ n = out.numel()
+ _gate_ew_kernel[(triton.cdiv(n, 4096),)](kvt, gt, out, n, BLK=4096, num_warps=4)
+ return out
+
+
+def qdense_raw16_t(x, packed, tm=None):
+ """qdense_raw16 with a TRANSPOSED result: x [M, K] -> [N, M] fp16 = (x @ W.T).T, from the GEMM W.T-view @ x.T-view (both operands read in their stored layout)"""
+ N, nb, bb = packed.shape
+ t = tm.t if tm is not None and tm.on else (lambda n: contextlib.nullcontext())
+ with t(f"f dense N={N} K={nb * 32}: to_f16"):
+ x2 = to_f16(x.reshape(-1, nb * 32)).contiguous()
+ with t(f"f dense N={N} K={nb * 32}: dequant"):
+ w16 = dequant_q8(packed)
+ with t(f"f dense N={N} K={nb * 32}: mm"):
+ return torch.mm(w16.t(), x2.t())
+
+
+def qdense_t(xt, packed, tm=None):
+ """qdense_tm for an activation stored TRANSPOSED: xt [K, M] fp16 contiguous -> [M, N] fp32 = xt.T @ W.T (the GEMM reads A column-major)"""
+ N, nb, bb = packed.shape
+ K = nb * 32
+ t = tm.t if tm is not None and tm.on else (lambda n: contextlib.nullcontext())
+ with t(f"f dense N={N} K={K}: dequant"):
+ w16 = dequant_q8(packed)
+ with t(f"f dense N={N} K={K}: mm"):
+ o = torch.mm(xt.t(), w16)
+ with t(f"f dense N={N} K={K}: float"):
+ return torch.nan_to_num(o.float(), nan=0.0, posinf=F16MAX, neginf=-F16MAX)
+
+
def qdense_raw16(x, packed, tm=None, tag=""):
"""qdense_tm without the fp32 upcast: x [M, K] (fp16 passes through, else saturating cast) -> raw fp16 GEMM output [M, N]"""
N, nb, bb = packed.shape