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 prefill indexer: blocked score + linear-time exact top-k for the sparse path (no [T, P] fp32 matrix, sampled threshold + candidate buffer, exact fallback); HY4_PF_IXBLK=0 keeps the unblocked path; CPU test vs the reference select, GPU same selection at 8K-128K, 128K 331 -> 85 ms per layer per 8192 rows
YanissAmz159764da753f
diff --git a/exllamav3/loader/prefill_kernels.py b/exllamav3/loader/prefill_kernels.pyindex 39c924c..5efc63d 100644--- a/exllamav3/loader/prefill_kernels.py+++ b/exllamav3/loader/prefill_kernels.py@@ -397,6 +397,15 @@ def _pf_indexer_forward(self, x, qr, n_keys=None):iq = self._rope((qr @ self.w["q_b"].T).reshape(T, H, d), pos)wt = (x.float() @ self.w["proj"].T) / math.sqrt(d * H)P = self.P+ if PF_FUSE and PF_SPARSE and IX_BLOCKED: # blocked score + linear-time top-k: no [T, P] fp32 matrix, cost linear in N+ N = self.n if n_keys is None else n_keys+ last = start + T - 1+ if (last // k if self.tail else (last + 1) // k) <= self.n_top:+ return None+ if N >= SPARSE_MINN and N >= P * k:+ with t("f ix: scores + select (blocked)"):+ self.last_scores = None+ return ix_select_blocked(to_f16(iq), to_f16(self.pool[:P]), wt, start, k, self.tail, self.n_top, N)with t("f ix: scores"):if PF_FUSE:sc = ix_scores_fused(to_f16(iq), to_f16(self.pool[:P]), wt, start, k, self.tail)@@ -633,6 +642,160 @@ def ix_select(sc, start, N, n_top, kpool, tail, want_bias=True, sparse=False):return allow+# ------------------------------------------------------------------------------------------------ blocked score + linear-time exact top-k (HY4_PF_SPARSE=1)+def _fkey(x):+ """fp32 -> int32 key with the same order (-0.0 == +0.0)"""+ b = (x + 0.0).to(tl.int32, bitcast=True)+ return tl.where(b < 0, b ^ 0x7FFFFFFF, b)+++def _kth_key(key, valid, k):+ """largest L with count(valid & key >= L) >= k (int32 keys of one vector; -2^31 when fewer than k valid entries)"""+ zero = tl.sum(tl.zeros([1], dtype=tl.int32), axis=0).to(tl.int64)+ lo = zero - 2147483648+ hi = zero + 2147483647+ k64 = key.to(tl.int64)+ for _ in range(32):+ mid = lo + (hi - lo + 1) // 2+ cnt = tl.sum(((k64 >= mid) & valid).to(tl.int32), axis=0)+ ge = cnt >= k+ lo = tl.where(ge, mid, lo)+ hi = tl.where(ge, hi, mid - 1)+ return lo+++def _ix_topk_kernel(SC, CK, CI, GIDX, NSEL, P, start, n_top, KP: tl.constexpr, TAIL: tl.constexpr, BLK: tl.constexpr, SM: tl.constexpr, CAP: tl.constexpr,+ TARGET: tl.constexpr, NATT: tl.constexpr):+ """One row per program, cost linear in the visible groups nv (no power-of-two row vector, no 32 passes):+ 1. sample SM groups (stride ceil(nv / SM)), threshold = the key giving ~TARGET expected hits; one counting pass; accept when n_top <= count <= CAP (NATT tries, the+ expected count is rescaled after a miss);+ 2. accepted: one pass compacts the candidates (ascending group order) into the row scratch CK / CI; exact n_top-th key on that buffer (ties: lowest index first);+ 3. not accepted (heavy ties / odd score law): exact 32-step bisection with tiled passes, then one compacting pass.+ nv <= CAP skips the sampling (every visible group is a candidate). Output = GIDX [T, n_top] ascending group ids + NSEL, same selection as _ix_select_kernel."""+ r = tl.program_id(0)+ pos = start + r+ if TAIL:+ nv = pos // KP+ else:+ nv = (pos + 1) // KP+ nv = tl.minimum(nv, P)+ row = SC + r.to(tl.int64) * P+ zero = tl.sum(tl.zeros([1], dtype=tl.int32), axis=0)+ thr = zero.to(tl.int64) - 2147483648+ ok = zero + 1+ if nv > CAP:+ ok = zero+ s = (nv + SM - 1) // SM+ sidx = tl.arange(0, SM) * s+ sm = sidx < nv+ skey = _fkey(tl.load(row + sidx, mask=sm, other=float("-inf")))+ m = tl.sum(sm.to(tl.int32), axis=0)+ kk = tl.maximum((TARGET * m + nv - 1) // nv, 1)+ for _a in range(NATT):+ if ok == 0:+ ta = _kth_key(skey, sm, kk)+ cnt = zero+ for t0 in range(0, nv, BLK):+ g = t0 + tl.arange(0, BLK)+ gm = g < nv+ key = _fkey(tl.load(row + g, mask=gm, other=float("-inf")))+ cnt += tl.sum(((key.to(tl.int64) >= ta) & gm).to(tl.int32), axis=0)+ good = (cnt >= n_top) & (cnt <= CAP)+ thr = tl.where(good, ta, thr)+ ok = good.to(tl.int32)+ # rescale the sample rank so the next try expects CAP / 2 hits (too many) or 2 * n_top hits (too few)+ want = tl.where(cnt > CAP, CAP // 2, 2 * n_top)+ kk = tl.minimum(tl.maximum(kk * want // tl.maximum(cnt, 1), 1), m)+ if ok != 0:+ crow = r.to(tl.int64) * CAP+ off = zero+ for t0 in range(0, nv, BLK):+ g = t0 + tl.arange(0, BLK)+ gm = g < nv+ key = _fkey(tl.load(row + g, mask=gm, other=float("-inf")))+ sel = gm & (key.to(tl.int64) >= thr)+ si = sel.to(tl.int32)+ pb = off + tl.cumsum(si, axis=0) - 1+ tl.store(CK + crow + pb, key, mask=sel)+ tl.store(CI + crow + pb, g, mask=sel)+ off += tl.sum(si, axis=0)+ tl.debug_barrier()+ c = tl.arange(0, CAP)+ cm = c < off+ bk = tl.load(CK + crow + c, mask=cm, other=0)+ bi = tl.load(CI + crow + c, mask=cm, other=0)+ lo = _kth_key(bk, cm, n_top)+ k64 = bk.to(tl.int64)+ gt = cm & (k64 > lo)+ eq = cm & (k64 == lo)+ need = n_top - tl.sum(gt.to(tl.int32), axis=0)+ sel = gt | (eq & (tl.cumsum(eq.to(tl.int32), axis=0) <= need))+ si = sel.to(tl.int32)+ tl.store(GIDX + r.to(tl.int64) * n_top + tl.cumsum(si, axis=0) - 1, bi, mask=sel)+ tl.store(NSEL + r, tl.sum(si, axis=0))+ else:+ lo = zero.to(tl.int64) - 2147483648+ hi = zero.to(tl.int64) + 2147483647+ for _b in range(32):+ mid = lo + (hi - lo + 1) // 2+ cnt = zero+ for t0 in range(0, nv, BLK):+ g = t0 + tl.arange(0, BLK)+ gm = g < nv+ key = _fkey(tl.load(row + g, mask=gm, other=float("-inf")))+ cnt += tl.sum(((key.to(tl.int64) >= mid) & gm).to(tl.int32), axis=0)+ ge = cnt >= n_top+ lo = tl.where(ge, mid, lo)+ hi = tl.where(ge, hi, mid - 1)+ ngt = zero+ for t0 in range(0, nv, BLK):+ g = t0 + tl.arange(0, BLK)+ gm = g < nv+ key = _fkey(tl.load(row + g, mask=gm, other=float("-inf")))+ ngt += tl.sum(((key.to(tl.int64) > lo) & gm).to(tl.int32), axis=0)+ need = n_top - ngt+ ce = zero+ off = zero+ for t0 in range(0, nv, BLK):+ g = t0 + tl.arange(0, BLK)+ gm = g < nv+ key = _fkey(tl.load(row + g, mask=gm, other=float("-inf")))+ gt = gm & (key.to(tl.int64) > lo)+ eq = gm & (key.to(tl.int64) == lo)+ sel = gt | (eq & ((ce + tl.cumsum(eq.to(tl.int32), axis=0)) <= need))+ si = sel.to(tl.int32)+ tl.store(GIDX + r.to(tl.int64) * n_top + off + tl.cumsum(si, axis=0) - 1, g, mask=sel)+ ce += tl.sum(eq.to(tl.int32), axis=0)+ off += tl.sum(si, axis=0)+ tl.store(NSEL + r, off)+++IX_BLOCKED = os.environ.get("HY4_PF_IXBLK", "1") == "1" # HY4_PF_SPARSE=1 only: 0 = the unblocked score matrix + ix_select+IX_BLOCK_ELEMS = 1 << 27 # fp32 score elements of one query block (512 MB): the [T, P] matrix is never materialised, so the cost per layer stays linear in N+++def ix_select_blocked(iq16, pk16, wt, start, kpool, tail, n_top, N, rows=None, cap=4096, blk=2048, sm=4096, target=2048, natt=3):+ """score + exact top-k per query block -> PfSparse (same selection as ix_scores_fused + ix_select(sparse=True)); iq16 [T, H, d] fp16, pk16 [P, d] fp16, wt [T, H] fp32"""+ T = iq16.shape[0]+ P = pk16.shape[0]+ TB = rows or max(64, min(T, (IX_BLOCK_ELEMS // max(P, 1)) // 64 * 64))+ dev = iq16.device+ gidx = torch.empty(T, n_top, dtype=torch.int32, device=dev)+ nsel = torch.empty(T, dtype=torch.int32, device=dev)+ ck = torch.empty(min(TB, T), cap, dtype=torch.int32, device=dev)+ ci = torch.empty(min(TB, T), cap, dtype=torch.int32, device=dev)+ for t0 in range(0, T, TB):+ t1 = min(T, t0 + TB)+ sc = ix_scores_fused(iq16[t0:t1], pk16, wt[t0:t1], start + t0, kpool, tail)+ _ix_topk_kernel[(t1 - t0,)](sc, ck, ci, gidx[t0:t1], nsel[t0:t1], P, start + t0, n_top, KP=kpool, TAIL=int(bool(tail)), BLK=blk, SM=sm, CAP=cap, TARGET=target,+ NATT=natt, num_warps=8)+ del sc+ return PfSparse(gidx, nsel, kpool, tail, start, N)++MOE_NF = None # tests: force the grid order (None = env HY4_MOE_NFIRST)LAST_PATH = "" # path the last moe_prefill call ran (triton / hip_wmma / gm_pfmoe_shared); gmmq is not called from this moduleMOE_HIP = None # tests: force (None = env HY4_MOE_HIP=1); HIP / WMMA expert kernels (moe_hip.cu, 128-row blocks, Q4_K / Q3_K); off = Tritondiff --git a/tests/test_hy4_ix_topk_cpu.py b/tests/test_hy4_ix_topk_cpu.pynew file mode 100644index 0000000..9987bb2--- /dev/null+++ b/tests/test_hy4_ix_topk_cpu.py@@ -0,0 +1,70 @@+"""CPU test (TRITON_INTERPRET=1, no GPU) of the blocked fused score + linear-time top-k: python test_hy4_ix_topk_cpu.py TREE+_ix_topk_kernel must pick exactly the groups of the reference _ix_select_kernel (same ids in the same ascending order, same count) on the candidate path, the+sample-retry path and the exact fallback path (natt=0, heavy ties); ix_select_blocked must equal ix_scores_fused + ix_select(sparse=True)."""+import os, sys, types+os.environ["TRITON_INTERPRET"] = "1"+os.environ["HIP_VISIBLE_DEVICES"] = ""+os.environ["CUDA_VISIBLE_DEVICES"] = ""+import torch+tree = sys.argv[1]+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 import prefill_kernels as PK++torch.manual_seed(1)+bad = 0+++def run_topk(sc, start, n_top, KP, tail, **kw):+ T, P = sc.shape+ cap = kw.get("cap", 32)+ ck = torch.empty(T, cap, dtype=torch.int32); ci = torch.empty(T, cap, dtype=torch.int32)+ gidx = torch.full((T, n_top), -1, dtype=torch.int32); nsel = torch.empty(T, dtype=torch.int32)+ PK._ix_topk_kernel[(T,)](sc, ck, ci, gidx, nsel, P, start, n_top, KP=KP, TAIL=int(tail), BLK=kw.get("blk", 16), SM=kw.get("sm", 64), CAP=cap,+ TARGET=kw.get("target", 16), NATT=kw.get("natt", 3), num_warps=4)+ return gidx, nsel+++def check(name, sc, start, n_top, KP, tail, N, **kw):+ global bad+ ref = PK.ix_select(sc, start, N, n_top, KP, tail, sparse=True)+ gidx, nsel = run_topk(sc, start, n_top, KP, tail, **kw)+ same_n = bool((nsel == ref.nsel).all())+ same_g = all(bool((gidx[r, :int(nsel[r])] == ref.gidx[r, :int(ref.nsel[r])]).all()) for r in range(sc.shape[0])) if same_n else False+ ok = same_n and same_g+ bad += not ok+ print(("OK " if ok else "FAIL"), name, "nsel", int(nsel.min()), int(nsel.max()), flush=True)+++for tail in (1, 0):+ for (start, T, KP, n_top) in ((0, 5, 4, 6), (200, 6, 4, 6), (3, 40, 4, 9), (260, 7, 4, 12)):+ N = start + T+ P = N // KP+ pos = torch.arange(start, N)+ gend = torch.arange(P) * KP + KP - 1+ vis = (gend[None] < pos[:, None]) if tail else (gend[None] <= pos[:, None])+ for kind in ("rand", "ties", "allsame", "clustered"):+ if kind == "rand": sc = torch.randn(T, P)+ elif kind == "ties": sc = torch.randint(0, 4, (T, P)).float()+ elif kind == "allsame": sc = torch.zeros(T, P)+ else: sc = torch.randn(T, P) * 0.001 + (torch.arange(P) % 7 == 0).float() * 5+ sc = sc.masked_fill(~vis, float("-inf"))+ for kw in (dict(), dict(natt=0), dict(cap=16, target=8), dict(cap=64, sm=16, blk=8)):+ check(f"tail{tail} start{start} T{T} {kind} {kw}", sc, start, n_top, KP, tail, N, **kw)++# blocked scoring + select vs the unblocked reference+H, D, KP, n_top = 4, 16, 4, 7+for tail in (1, 0):+ start, T = 90, 23+ N = start + T+ P = N // KP+ iq = (torch.randn(T, H, D) * 0.5).half(); pk = (torch.randn(P, D) * 0.5).half(); wt = torch.rand(T, H)+ sc = PK.ix_scores_fused(iq, pk, wt, start, KP, tail)+ ref = PK.ix_select(sc, start, N, n_top, KP, tail, sparse=True)+ for rows in (7, 23, 64):+ got = PK.ix_select_blocked(iq, pk, wt, start, KP, tail, n_top, N, rows=rows, cap=32, blk=16, sm=64, target=16)+ same = bool((got.nsel == ref.nsel).all()) and bool((got.to_dense() == ref.to_dense()).all())+ bad += not same+ print(("OK " if same else "FAIL"), "blocked tail", tail, "rows", rows, flush=True)+print("ALL OK" if not bad else f"{bad} FAILED")+sys.exit(1 if bad else 0)