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 graphs: context buckets grow with the context (1/32 of the octave above 16K rows) instead of a fixed 512 rows: a 256K prompt needed 512 buckets, so the first request of a process captured its own graphs inside the decode (11 tok/s cold vs 16 warm at 256K), and a long decode rebuilt one every 512 tokens; precapture lists 129-225 buckets instead of 264-2049; HY4_GRAPH_BUCKET_REL=0 restores the fixed granule; CPU test
YanissAmz5f0d00d8ce02
diff --git a/exllamav3/loader/graph_bucket.py b/exllamav3/loader/graph_bucket.pynew file mode 100644index 0000000..dcafdef--- /dev/null+++ b/exllamav3/loader/graph_bucket.py@@ -0,0 +1,36 @@+"""Context buckets of the captured decode graphs (step_graph.GraphDecoder, mtp_head.MTPGraph, step_mu): a graph is built per bucket Nb and the kernels mask the rows past the+position, so a bucket only costs the masked rows it adds.++Fixed 512-row buckets mean one graph build (2 eager bodies + the capture, 0.5 - 1 s of host time) every 512 decoded tokens and a new build for every (bucket, rows) a fresh+prompt lands in: at 256K that is 512 buckets, the first request of a process captured its own bucket inside a 47-token decode (256K spec: 11 tok/s, the same checkpoint+decoded warm: 16). Above HY4_GRAPH_BUCKET_FROM rows the bucket grows with the context: 1 / HY4_GRAPH_BUCKET_REL of its power-of-two octave (default 32: at most 3 % masked+rows, 32 buckets per octave: 8192 rows at 256K, 16384 at 512K, 32768 at 1M). HY4_GRAPH_BUCKET_REL=0 keeps the fixed bucket."""+import os++BASE = int(os.environ.get("HY4_GRAPH_BUCKET", "512"))+REL = int(os.environ.get("HY4_GRAPH_BUCKET_REL", "32"))+FROM = int(os.environ.get("HY4_GRAPH_BUCKET_FROM", "16384"))+++def size(n: int, base: int = BASE, rel: int = REL, start: int = FROM) -> int:+ """bucket granule for a step that needs n rows"""+ if rel <= 0 or n <= start:+ return base+ return max(base, (1 << (n.bit_length() - 1)) // rel // base * base)+++def bucket(n: int, cap: int, base: int = BASE, rel: int = REL, start: int = FROM) -> int:+ """smallest bucket >= n rows (a multiple of base), at most cap"""+ s = size(n, base, rel, start)+ return min(cap, -(-n // s) * s)+++def buckets(cap: int, base: int = BASE, rel: int = REL, start: int = FROM) -> list[int]:+ """every bucket a step can use, ascending (what precapture builds)"""+ out, n = [], 1+ while True:+ b = bucket(n, cap, base, rel, start)+ out.append(b)+ if b >= cap:+ return out+ n = b + 1diff --git a/exllamav3/loader/mtp_head.py b/exllamav3/loader/mtp_head.pyindex 3e6041a..0bf2ea7 100644--- a/exllamav3/loader/mtp_head.py+++ b/exllamav3/loader/mtp_head.py@@ -17,6 +17,7 @@ import torchfrom . import moe_i8 as MIfrom . import step_graph as SG+from . import graph_bucket as _gbfrom . import step_nosync as _nsfrom .engram_rows import load_dense, rmsfrom .hy4_forward import _mv, head_gemm, QLinear, GG_DENSE, dense_fn@@ -227,7 +228,7 @@ class MTPGraph:"""the draft chain of k head steps as ONE HIP graph per context bucket: static inputs (token, trunk state, head position), greedy argmax feeds the nextstep on the device, output = the k drafted tokens [k] (device). No host work between the steps."""- BUCKET = 512+ BUCKET = 512 # base granule; the bucket of a long context is coarser (graph_bucket)def __init__(self, mt, k):self.mt, self.k = mt, k@@ -276,7 +277,7 @@ class MTPGraph:def precapture(self, ns=None):"""capture the draft graph of every context bucket now (default: every bucket up to the cap); -> number built"""mt = self.mt- ns = sorted(set(ns)) if ns is not None else list(range(self.BUCKET, mt.cap + 1, self.BUCKET))+ ns = sorted(set(ns)) if ns is not None else _gb.buckets(mt.cap, self.BUCKET)built = 0for N in ns:if N not in self.graphs:@@ -287,7 +288,7 @@ class MTPGraph:def run(self, tok, h, j):"""tok: host int (the token of the pair, x_{j+1}), h: device [1, D] trunk state h_j, j: head position -> device tensor [k] of drafts"""mt = self.mt- N = min(mt.cap, ((j + self.k) // self.BUCKET + 1) * self.BUCKET)+ N = _gb.bucket(j + self.k + 1, mt.cap, self.BUCKET)if self.ev_set:self.ev.synchronize()self.hin[0], self.hin[1] = int(tok), int(j)@@ -347,7 +348,7 @@ class MTPGraphCU(MTPGraph):"""toks: host ints [a + 1] (accepted drafts then the new token), h_rows [a + 1, D] device (trunk hidden of the positions pos0 .. pos0 + a),pos0: first head position -> device [k] drafts"""mt, a = self.mt, len(toks) - 1- N = min(mt.cap, ((pos0 + a + self.k) // self.BUCKET + 1) * self.BUCKET)+ N = _gb.bucket(pos0 + a + self.k + 1, mt.cap, self.BUCKET)if self.ev_set:self.ev.synchronize()self.hin[:a + 1] = torch.tensor([int(t) for t in toks])diff --git a/exllamav3/loader/step_graph.py b/exllamav3/loader/step_graph.pyindex bc07495..83160cd 100644--- a/exllamav3/loader/step_graph.py+++ b/exllamav3/loader/step_graph.py@@ -25,7 +25,8 @@ from .engram_rows import QB, E4M3, E8M0from . import attn_eager as _aefrom .engram_stage import EngramStager-BUCKET = int(os.environ.get("HY4_GRAPH_BUCKET", "512"))+from . import graph_bucket as _gb+BUCKET = _gb.BASETM = int(os.environ.get("HY4_GRAPH_TM", "8")) # max rows of one graph step (verify rows)MASK64 = (1 << 64) - 1PERT = None # [cap, D] fp32 noise table (measurement arms only)@@ -368,7 +369,7 @@ class GraphDecoder:def bucket(self, start, T=1):"""context bucket Nb of a step of T rows at positions start .. start + T - 1 (rows past the last position are masked inside the graph)"""- return min(self.cap, ((start + T - 1) // BUCKET + 1) * BUCKET)+ return _gb.bucket(start + T, self.cap)def graph(self, Nb, T=1):"""the captured (graph, outputs) of a T-row step at context bucket Nb (captured now if not yet). Row-generic: T = rows of ONE sequence (verify) or any@@ -380,7 +381,7 @@ class GraphDecoder:nbs: iterable of buckets (default every bucket up to the cap), ts: row counts (default 1 .. TM). Returns [(Nb, T, ms)] of the graphs built. The staticinputs keep their last values: call it between requests (the K / indexer rows it rewrites are the same ones the last step wrote) and reset() after."""import time- nbs = sorted(set(nbs)) if nbs is not None else list(range(BUCKET, self.cap + 1, BUCKET))+ nbs = sorted(set(nbs)) if nbs is not None else _gb.buckets(self.cap)ts = sorted(set(ts)) if ts is not None else list(range(1, TM + 1))if not self.prepared:self.prepare()diff --git a/exllamav3/loader/step_mu.py b/exllamav3/loader/step_mu.pyindex 9dc4279..cb4b604 100644--- a/exllamav3/loader/step_mu.py+++ b/exllamav3/loader/step_mu.py@@ -184,7 +184,7 @@ class MultiDecoder(SG.GraphDecoder):ps += list(range(s.n, s.n + len(toks)))bs += [s.slot * self.cap] * len(toks)top = max(top, s.n + len(toks))- Nb = min(self.cap, ((top - 1) // SG.BUCKET + 1) * SG.BUCKET)+ Nb = SG._gb.bucket(top, self.cap)gs.Nb = Nbself.hin[0, :T] = torch.tensor(tk)self.hin[1, :T] = torch.tensor(ps)diff --git a/tests/test_graph_bucket.py b/tests/test_graph_bucket.pynew file mode 100644index 0000000..8fbb7af--- /dev/null+++ b/tests/test_graph_bucket.py@@ -0,0 +1,25 @@+"""CPU test (no torch, no GPU): python test_graph_bucket.py TREE+Context buckets of the decode graphs: >= rows needed, multiple of the base granule, monotone, <= cap, <= 1/32 masked rows above 16K, `buckets(cap)` lists exactly the+buckets `bucket()` can return, and the fixed-granule behaviour is unchanged below 16K / with REL = 0."""+import importlib.util, os, sys+spec = importlib.util.spec_from_file_location("gb", os.path.join(sys.argv[1], "exllamav3", "loader", "graph_bucket.py"))+gb = importlib.util.module_from_spec(spec); spec.loader.exec_module(gb)+B = 512+for cap in (135168, 528384, 1048576 + 512):+ cap -= cap % B+ allb = gb.buckets(cap, B, 32, 16384)+ assert allb == sorted(set(allb)) and allb[-1] == cap and all(b % B == 0 for b in allb)+ seen, prev = set(), 0+ for n in list(range(1, 40000, 37)) + list(range(40000, cap + 1, 997)) + [cap]:+ b = gb.bucket(n, cap, B, 32, 16384)+ assert b >= n and b <= cap and b % B == 0 and b >= prev and b in allb, (n, b)+ if n > 16384 and b < cap:+ assert (b - n) <= max(B, n // 32 + B), (n, b)+ if n <= 16384:+ assert b == min(cap, -(-n // B) * B)+ prev = b+ seen.add(b)+ print(f"cap {cap}: {len(allb)} buckets (fixed 512 would be {cap // B}), sample 262144+1 -> {gb.bucket(262145, cap, B, 32, 16384)}")+assert gb.buckets(135168, B, 0, 16384) == list(range(B, 135168 + 1, B))+assert gb.bucket(262145, 528384, B, 0, 16384) == 262144 + 512+print("OK")