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: Triton router mat-vec (T <= 8 rows), spec_generate loop, one-launch indexer norms with the rmsw flag
YanissAmz9f0a5265aacf
diff --git a/exllamav3/loader/hy4_forward.py b/exllamav3/loader/hy4_forward.pyindex 52822e7..ce8f005 100644--- a/exllamav3/loader/hy4_forward.py+++ b/exllamav3/loader/hy4_forward.py@@ -32,6 +32,7 @@ from . import kv_fused as _KVFfrom . import mla_bgemv as _BGMfrom . import head_bf16 as _HBfrom . import attn_eager as _AE+from . import router_gemv as _RG_HCF = Noneif os.environ.get("HY4_HC_FUSED", "0") == "1": # fused hyper-connection sites (loader/hc_fused.py)@@ -472,10 +473,10 @@ class Hy4Forward:T, dev = x.shape[0], self.devwith tm.t("router"):if _rf.ENABLED: # HY4_REF_FUSED=1: one router kernel; the host sync (tolist) only where the pread loop needs it- sel, w = _rf.router_select(x @ L.router.T, L.rbias, self.topk, self.w_scale)+ sel, w = _rf.router_select(_RG.logits(x, L.router) if _RG.usable(x, L.router) else x @ L.router.T, L.rbias, self.topk, self.w_scale)sel_l = [] if (self.res.get(i) is not None and T <= MMVQ_MAXR and dev.type == "cuda") else sorted(set(sel.flatten().tolist()))else:- p = torch.sigmoid(x @ L.router.T)+ p = torch.sigmoid(_RG.logits(x, L.router) if _RG.usable(x, L.router) else x @ L.router.T)sel = torch.sort(p + L.rbias, dim=-1, descending=True, stable=True).indices[:, :self.topk] # stable: near-tied experts resolve to the lowest id every callw = torch.gather(p, 1, sel)w = w / w.sum(-1, keepdim=True).clamp(min=6.103515625e-5) * self.w_scalediff --git a/exllamav3/loader/mtp_head.py b/exllamav3/loader/mtp_head.pyindex abda83e..b40f89a 100644--- a/exllamav3/loader/mtp_head.py+++ b/exllamav3/loader/mtp_head.py@@ -11,6 +11,7 @@ so a rejected draft row is simply overwritten by the next round. Greedy chain onfrom __future__ import annotationsimport os+import timeimport torch@@ -103,7 +104,9 @@ class Hy4MTP:fw = self.fwif self.fast:from . import ref_fused as _RF- sel, w = _RF.router_select(x @ self.router.T, self.rbias, fw.topk, fw.w_scale) # one launch, lowest id on ties+ from . import router_gemv as _RG+ lg = _RG.logits(x, self.router) if _RG.usable(x, self.router) else x @ self.router.T+ sel, w = _RF.router_select(lg, self.rbias, fw.topk, fw.w_scale) # one launch, lowest id on tieselse:p = torch.sigmoid(x @ self.router.T)sel = torch.topk(p + self.rbias, fw.topk, dim=-1).indices@@ -303,3 +306,40 @@ class MTPGraphCU(MTPGraph):cg, out = self._graph(N, a)cg.replay()return out+++def spec_generate(gd, mg, y, hcur, n_new, cu=False, timers=None):+ """Greedy speculative decode on the whole-step graph. gd = GraphDecoder (the trunk, context already in it), mg = MTPGraph / MTPGraphCU (k drafts per round),+ y = the trunk's greedy token after the context (host int, NOT yet fed), hcur = the trunk hidden [1, D] of the last context position, n_new = tokens wanted.+ Each round: the draft chain proposes k tokens, ONE T = k + 1 row verify replay checks them, the longest agreeing prefix plus the trunk's own next token is kept+ (the output equals plain greedy decoding of the trunk), K / indexer rows of rejected positions are overwritten by the next round.+ Returns (tokens [>= n_new, up to k more], accepted-length histogram, rounds). timers (list of 3 floats, optional) accumulates seconds in: draft chain launch ++ wait, host stage + verify launch, verify wait."""+ fw, k = gd.fw, mg.k+ out, hist, rounds = [], [0] * (k + 1), 0+ cu_toks, cu_h, cu_p = [y], hcur, fw.n - 1+ pc = time.perf_counter+ while len(out) < n_new:+ j = fw.n - 1+ ta = pc()+ d = (mg.run(cu_toks, cu_h, cu_p) if cu else mg.run(y, hcur, j)).tolist()+ tb = pc()+ toks = [y] + d+ _lg, amv, _x, hv = gd.step_rows(toks)+ tc = pc()+ amh = amv.tolist()+ td = pc()+ if timers is not None:+ timers[0] += tb - ta+ timers[1] += tc - tb+ timers[2] += td - tc+ acc = 0+ while acc < k and d[acc] == amh[acc]:+ acc += 1+ out += toks[:acc + 1]+ gd.rewind(j + 2 + acc)+ y, hcur = amh[acc], hv[acc:acc + 1]+ cu_toks, cu_h, cu_p = d[:acc] + [y], hv[:acc + 1], j + 1+ hist[acc] += 1+ rounds += 1+ return out, hist, roundsdiff --git a/exllamav3/loader/router_gemv.py b/exllamav3/loader/router_gemv.pynew file mode 100644index 0000000..9fc6d8b--- /dev/null+++ b/exllamav3/loader/router_gemv.py@@ -0,0 +1,46 @@+"""HY4_ROUTER_GEMV=1: the router logits x [T, K] @ W[N, K].T for T <= MAXT rows as ONE Triton mat-vec (weights read once for all T rows, fp32 accumulate).++rocBLAS runs this shape (T = 1..8, N = 512, K = 3072, fp32 weights 6 MiB) as an SGEMM tile kernel at about 47 GB/s (134 us per layer, 5.5 ms per decode step over+41 MoE layers); the mat-vec below reads the same bytes at DRAM speed. Same products as the fp32 matmul, only the summation order differs. T > MAXT keeps the matmul."""+import os++import torch+import triton+import triton.language as tl++ENABLED = os.environ.get("HY4_ROUTER_GEMV", "0") == "1"+MAXT = 8+BN = int(os.environ.get("HY4_RG_BN", "4"))+BK = int(os.environ.get("HY4_RG_BK", "256"))+NW = int(os.environ.get("HY4_RG_NW", "4"))+++def _k_rg(x_ptr, w_ptr, o_ptr, N, K, T, sxt, BT: tl.constexpr, BN: tl.constexpr, BK: tl.constexpr):+ rows = tl.program_id(0) * BN + tl.arange(0, BN)+ rm = rows < N+ tt = tl.arange(0, BT)+ tm = tt < T+ acc = tl.zeros([BT, BN], dtype=tl.float32)+ for k0 in range(0, K, BK):+ cols = k0 + tl.arange(0, BK)+ cm = cols < K+ w = tl.load(w_ptr + rows[:, None] * K + cols[None, :], mask=rm[:, None] & cm[None, :], other=0.0)+ xv = tl.load(x_ptr + tt[:, None] * sxt + cols[None, :], mask=tm[:, None] & cm[None, :], other=0.0)+ acc += tl.sum(w[None, :, :] * xv[:, None, :], axis=2)+ tl.store(o_ptr + tt[:, None] * N + rows[None, :], acc, mask=tm[:, None] & rm[None, :])+++def usable(x, w):+ return (ENABLED and x.dtype == torch.float32 and w.dtype == torch.float32 and x.dim() == 2 and 1 <= x.shape[0] <= MAXT and w.is_contiguous()+ and x.stride(-1) == 1 and (x.is_cuda or os.environ.get("TRITON_INTERPRET", "0") == "1"))+++def logits(x, w, bn=None, bk=None, nw=None):+ """x [T, K] fp32, w [N, K] fp32 (row-major) -> [T, N] fp32 = x @ w.T"""+ T, K = x.shape+ N = w.shape[0]+ out = torch.empty(T, N, dtype=torch.float32, device=x.device)+ bn, bk = bn or BN, bk or BK+ _k_rg[(triton.cdiv(N, bn),)](x, w, out, N, K, T, x.stride(0), BT=triton.next_power_of_2(T), BN=bn, BK=bk, num_warps=nw or NW)+ return outdiff --git a/exllamav3/loader/step_graph.py b/exllamav3/loader/step_graph.pyindex b080c5c..9d6fea9 100644--- a/exllamav3/loader/step_graph.py+++ b/exllamav3/loader/step_graph.py@@ -21,6 +21,7 @@ import torchfrom . import step_nosync as _nsfrom .engram_rows import QB, E4M3, E8M0+from . import attn_eager as _aeBUCKET = int(os.environ.get("HY4_GRAPH_BUCKET", "512"))TM = int(os.environ.get("HY4_GRAPH_TM", "8")) # max rows of one graph step (verify rows)@@ -33,6 +34,8 @@ _POOL = ThreadPoolExecutor(max_workers=_IO_W)# ---------------------------------------------------------------------------------------------------- static indexerdef _rms(x, w, eps):x = x.float()+ if _ae.RMS and x.dim() == 2 and x.is_cuda: # G2: one launch instead of five+ return _ae.rmsw(x, w, eps)return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + eps) * w