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.
Python amdapple-siliconcudadeepseekexl3exllamav3glminferencellmllm-inferencelocal-llmnvidiaopenai-apiquantizationqwenrocmstrix-halo
Hy4 decode: fused q pe rotation, k_b matvec reads the nope slice in place (HY4_KV_FUSED / HY4_MLA_BG)
YanissAmz8f8e97633d6d
diff --git a/exllamav3/loader/attn_eager.py b/exllamav3/loader/attn_eager.pyindex a4b97af..bd08f11 100644--- a/exllamav3/loader/attn_eager.py+++ b/exllamav3/loader/attn_eager.py@@ -12,6 +12,7 @@ from __future__ import annotationsimport osimport torchfrom . import mla_bgemv as _BG+from . import kv_fused as _KVF_F = os.environ.get("HY4_ATTN_EAGER", "0")ENABLED = _F in ("1", "2")@@ -90,7 +91,11 @@ def q(m, cur, pos):from .engram_rows import rmsqr = rms(qa, m.eps) * m.q_a_normqq = (m._q(qr, "wq_b") @ m.wq_b.T).view(T, H, m.hd)- qn, qp = qq[..., :m.nope], rope(qq[..., m.nope:], pos, m.theta)+ qn = qq[..., :m.nope]+ if _KVF.ENABLED and T == 1 and qq.is_cuda: # HY4_KV_FUSED: the pe rotation in one launch (no slice copies)+ qp = _KVF.rope_q(qq, table(m.rope_dim, m.theta, qq.device, pos), pos, m.nope, m.rope_dim)+ else:+ qp = rope(qq[..., m.nope:], pos, m.theta)qlat = m.bg_kb.mv(m._q(qn, "kb")) if (_BG.ENABLED and T == 1 and getattr(m, "bg_kb", None) is not None) else torch.einsum("hrk,thk->thr", m.wk_b, m._q(qn, "kb"))return m._h(qlat, "q"), m._h(qp, "q")diff --git a/exllamav3/loader/attn_eager.py.pre_qf b/exllamav3/loader/attn_eager.py.pre_qfnew file mode 100644index 0000000..a4b97af--- /dev/null+++ b/exllamav3/loader/attn_eager.py.pre_qf@@ -0,0 +1,124 @@+"""HY4_ATTN_EAGER: fewer eager launches in the attention projections (flag default OFF; old path untouched when off).++ HY4_ATTN_EAGER=1 (bit-exact vs the old code)+ rope_norm : cos/sin built ONCE per (dim, base, device) in float64 with the SAME expression as the per-call path, cast to fp32,+ stored as [n, 2, P/2, 2] = [(c, s), (-s, c)]. A call = 1 index_select + 2 mul + 1 add (old: 18 launches, +1 contiguous+ in q). out = x0*(c,s) + x1*(-s,c) is the same fp32 ops as x0*c - x1*s / x0*s + x1*c (a - b == a + (-b), no FMA in eager).+ kv() : rms*w and rope write straight into one preallocated [T, R+rope] buffer (no torch.cat, no copy_ into out[..., 0::2]).+ HY4_ATTN_EAGER=2 = 1 + rmsw(): rms(x)*w as ONE Triton launch (kv_a, q_a, post-attn norm). The reduction order differs from torch's+ mean(), so NOT bit-exact (~1e-7 rel): gate it with teacher forcing before keeping.+Generic by shape: P, base, R, H come from the caller / tensors."""+from __future__ import annotations+import os+import torch+from . import mla_bgemv as _BG++_F = os.environ.get("HY4_ATTN_EAGER", "0")+ENABLED = _F in ("1", "2")+RMS = _F == "2"+_NEED = [0] # highest position + 1 the caller announced (host int, set by ensure())+_TAB = {} # (P, base, device) -> [n, 2, P/2, 2]++try:+ import triton+ import triton.language as tl+except Exception:+ triton = None+++def ensure(n: int):+ """host-side bound on positions (start + T); tables grow lazily on the next rope call. No device sync."""+ if n > _NEED[0]:+ _NEED[0] = n+++def _build(P, base, n, dev):+ h = P // 2+ pos = torch.arange(n, device=dev)+ th = pos.double()[:, None] * (base ** (-2.0 * torch.arange(h, dtype=torch.float64, device=dev) / P))[None, :] # == rope_norm+ c, s = th.cos().float(), th.sin().float()+ tab = torch.empty(n, 2, h, 2, dtype=torch.float32, device=dev)+ tab[:, 0, :, 0], tab[:, 0, :, 1], tab[:, 1, :, 0], tab[:, 1, :, 1] = c, s, -s, c+ return tab+++def table(P, base, dev, pos):+ key = (P, float(base), str(dev))+ tab = _TAB.get(key)+ need = _NEED[0]+ if need == 0: # nobody announced a bound: derive it (one sync; CPU: free)+ need = int(pos.max()) + 1+ if tab is None or tab.shape[0] < need:+ tab = _TAB[key] = _build(P, base, max(need, 2 * (tab.shape[0] if tab is not None else 2048)), dev)+ return tab+++def rope(x, pos, base, out=None):+ """== mla_hy4.rope_norm (bit-exact). x [T, ..., P] with unit last stride (views / slices allowed); out: optional [T, ..., P] view"""+ P = x.shape[-1]+ h = P // 2+ g = table(P, base, x.device, pos).index_select(0, pos.long()) # [T, 2, h, 2]+ shp = (x.shape[0],) + (1,) * (x.dim() - 2) + (h, 2)+ a, b = g[:, 0].reshape(shp), g[:, 1].reshape(shp)+ xv = x.unflatten(-1, (h, 2))+ ov = torch.empty(xv.shape, dtype=x.dtype, device=x.device) if out is None else out.unflatten(-1, (h, 2))+ torch.add(xv[..., 0:1] * a, xv[..., 1:2] * b, out=ov)+ return ov.flatten(-2) if out is None else out+++def kv(m, cur, pos):+ """Hy4MLA.kv: [rms(kv_a)*w | rope(k_pe)] written into one buffer"""+ c = m._q(cur, "wkv_a") @ m.wkv_a.T+ R = m.R+ buf = torch.empty(c.shape[0], R + m.rope_dim, dtype=c.dtype, device=c.device)+ if RMS:+ rmsw(c[:, :R], m.kv_a_norm, m.eps, out=buf[:, :R])+ else:+ from .engram_rows import rms+ torch.mul(rms(c[:, :R], m.eps), m.kv_a_norm, out=buf[:, :R])+ rope(c[:, R:], pos, m.theta, out=buf[:, R:])+ return m._h(buf)+++def q(m, cur, pos):+ """Hy4MLA.q: same maths, rope reads the strided slice (no .contiguous())"""+ T, H = cur.shape[0], m.H+ qa = m._q(cur, "wq_a") @ m.wq_a.T+ if RMS:+ qr = rmsw(qa, m.q_a_norm, m.eps)+ else:+ from .engram_rows import rms+ qr = rms(qa, m.eps) * m.q_a_norm+ qq = (m._q(qr, "wq_b") @ m.wq_b.T).view(T, H, m.hd)+ qn, qp = qq[..., :m.nope], rope(qq[..., m.nope:], pos, m.theta)+ qlat = m.bg_kb.mv(m._q(qn, "kb")) if (_BG.ENABLED and T == 1 and getattr(m, "bg_kb", None) is not None) else torch.einsum("hrk,thk->thr", m.wk_b, m._q(qn, "kb"))+ return m._h(qlat, "q"), m._h(qp, "q")+++if triton is not None:+ @triton.jit+ def _k_rmsw(x_ptr, w_ptr, o_ptr, D, sx, so, eps, BD: tl.constexpr):+ r = tl.program_id(0)+ d = tl.arange(0, BD)+ dm = d < D+ x = tl.load(x_ptr + r * sx + d, mask=dm, other=0.0)+ rc = tl.math.rsqrt(tl.sum(x * x, axis=0) / D + eps)+ tl.store(o_ptr + r * so + d, x * rc * tl.load(w_ptr + d, mask=dm, other=0.0), mask=dm)+++def rmsw(x, w, eps, out=None):+ """rms(x, eps) * w over the last dim of a [R, D] fp32 tensor (row stride free, unit column stride): one launch"""+ from .engram_rows import rms+ usable = (triton is not None and x.dtype == torch.float32 and x.dim() == 2 and x.stride(1) == 1 and w.is_contiguous()+ and (x.is_cuda or os.environ.get("TRITON_INTERPRET", "0") == "1") and x.shape[1] <= 8192+ and (out is None or (out.stride(1) == 1 and out.dtype == torch.float32)))+ if not usable:+ r = rms(x, eps) * w+ if out is None:+ return r+ out.copy_(r)+ return out+ o = torch.empty(x.shape, dtype=x.dtype, device=x.device) if out is None else out+ D = x.shape[1]+ _k_rmsw[(x.shape[0],)](x, w, o, D, x.stride(0), o.stride(0), eps, BD=triton.next_power_of_2(D), num_warps=8)+ return odiff --git a/exllamav3/loader/kv_fused.py b/exllamav3/loader/kv_fused.pyindex bcc0375..19a4c15 100644--- a/exllamav3/loader/kv_fused.py+++ b/exllamav3/loader/kv_fused.py@@ -35,3 +35,28 @@ def _k_kv(c_ptr, w_ptr, tab_ptr, pos_ptr, k_ptr, sc, sk, eps, R: tl.constexpr, Pdef write_row(c, w, tab, pos, kbuf, R, P, eps):"""c [1, R+P] fp32 (unit column stride), w [R], tab [n, 2, P/2, 2], pos [1] long device, kbuf [cap, R+P] fp32"""_k_kv[(1,)](c, w, tab, pos, kbuf, c.stride(0), kbuf.stride(0), eps, R=R, P=P, BR=triton.next_power_of_2(R), num_warps=4)+++def _k_qrope(q_ptr, tab_ptr, pos_ptr, o_ptr, sqh, soh, NOPE: tl.constexpr, P: tl.constexpr):+ h = tl.program_id(0)+ pos = tl.load(pos_ptr)+ H: tl.constexpr = P // 2+ offh = tl.arange(0, H)+ x0 = tl.load(q_ptr + h * sqh + NOPE + 2 * offh)+ x1 = tl.load(q_ptr + h * sqh + NOPE + 2 * offh + 1)+ tb = tab_ptr + pos * (2 * H * 2)+ a0 = tl.load(tb + offh * 2)+ a1 = tl.load(tb + offh * 2 + 1)+ b0 = tl.load(tb + H * 2 + offh * 2)+ b1 = tl.load(tb + H * 2 + offh * 2 + 1)+ tl.store(o_ptr + h * soh + 2 * offh, x0 * a0 + x1 * b0)+ tl.store(o_ptr + h * soh + 2 * offh + 1, x0 * a1 + x1 * b1)+++def rope_q(qq, tab, pos, nope, P):+ """qq [1, H, hd] fp32 (unit last stride; the pe part is qq[..., nope:nope+P]) -> rotated pe [1, H, P] (one launch)"""+ H = qq.shape[1]+ out = torch.empty(1, H, P, dtype=torch.float32, device=qq.device)+ _k_qrope[(H,)](qq, tab, pos, out, qq.stride(1), out.stride(1), NOPE=nope, P=P, num_warps=1)+ return outdiff --git a/exllamav3/loader/mla_bgemv.py b/exllamav3/loader/mla_bgemv.pyindex 0a9065c..6e819ec 100644--- a/exllamav3/loader/mla_bgemv.py+++ b/exllamav3/loader/mla_bgemv.py@@ -17,7 +17,7 @@ BK = int(os.environ.get("HY4_BG_BK", "128"))@triton.jit-def _k_bg(q_ptr, d_ptr, x_ptr, o_ptr, N, K, NB, BN: tl.constexpr, BK: tl.constexpr):+def _k_bg(q_ptr, d_ptr, x_ptr, o_ptr, N, K, NB, sxh, BN: tl.constexpr, BK: tl.constexpr):h = tl.program_id(0)rows = tl.program_id(1) * BN + tl.arange(0, BN)rm = rows < N@@ -30,7 +30,7 @@ def _k_bg(q_ptr, d_ptr, x_ptr, o_ptr, N, K, NB, BN: tl.constexpr, BK: tl.constexm2 = rm[:, None] & cm[None, :]q = tl.load(q_ptr + qrow + cols[None, :], mask=m2, other=0).to(tl.float32)d = tl.load(d_ptr + drow + (cols // 32)[None, :], mask=m2, other=0.0).to(tl.float32)- xv = tl.load(x_ptr + h * K + cols, mask=cm, other=0.0)+ xv = tl.load(x_ptr + h * sxh + cols, mask=cm, other=0.0)acc += tl.sum(q * d * xv[None, :], axis=1)tl.store(o_ptr + h * N + rows, acc, mask=rm)@@ -47,7 +47,11 @@ class Q8Batched:def mv(self, x):"""x [1, H, K] fp32 -> [1, H, N] fp32"""out = torch.empty(self.H, self.N, dtype=torch.float32, device=x.device)- _k_bg[(self.H, triton.cdiv(self.N, BN))](self.q, self.d, x.reshape(self.H, self.K).contiguous(), out, self.N, self.K, self.K // 32, BN=BN, BK=BK, num_warps=4)+ if x.dim() == 3 and x.shape[0] == 1 and x.stride(-1) == 1: # [1, H, K] view with a head stride (e.g. the nope part of q): no copy+ xv, sxh = x, x.stride(1)+ else:+ xv, sxh = x.reshape(self.H, self.K).contiguous(), self.K+ _k_bg[(self.H, triton.cdiv(self.N, BN))](self.q, self.d, xv, out, self.N, self.K, self.K // 32, sxh, BN=BN, BK=BK, num_warps=4)return out.reshape(1, self.H, self.N)