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 step tools: HB micro test and graph-step A/B

YanissAmz3246fab08b88
diff --git a/tools/hy4/step/ab_hb.py b/tools/hy4/step/ab_hb.py
new file mode 100644
index 0000000..8c5480d
--- /dev/null
+++ b/tools/hy4/step/ab_hb.py
@@ -0,0 +1,46 @@
+"""ab_hb.py: ONE load. Graph-replay step wall at T = 1 / 2 / 6, ctx 1000 / 4000, HY4_MLA_HB off (A) vs on (B), ABBA, graphs re-captured at every arm switch;
+logits of A vs B on the same tokens: KL(A||B), top-1 agreement. Run inside job.sh (box rules)."""
+import json, os, statistics, sys, time
+import numpy as np
+tree = os.getcwd()
+sys.path.insert(0, os.path.join(tree, "tools", "hy4"))
+import serve
+serve.apply_env(16384)
+import torch
+from engine import Hy4Engine
+from exllamav3.loader import ref_fused as rf
+GGUF = "/path/to/models/hy4-flash-gguf/Hy4-flash-Q3_K-00001-of-00002.gguf"
+e = Hy4Engine(tree, GGUF, ctx=16384, k=3)
+e.load(print)
+GR = e.GR
+wiki = json.load(open("/path/to/wiki_ids.json"))
+pc = time.perf_counter
+sync = torch.cuda.synchronize
+print("REF_FUSED", rf.ENABLED, "HB_BN", rf.HB_BN, "HB_NW", rf.HB_NW, flush=True)
+ctxs = [int(x) for x in os.environ.get("PCTXS", "1000,4000").split(",")]
+Ts = [int(x) for x in os.environ.get("PTS", "1,2,6").split(",")]
+res = {}
+logit = {}
+for ctxn in ctxs:
+ e._t0 = pc(); e.reset(); e.prefill(wiki[:ctxn]); e.phase("decode")
+ for arm in ("A", "B", "B", "A"):
+ rf.HB = arm == "B"
+ GR.graphs.clear()
+ for T in Ts:
+ toks = wiki[ctxn:ctxn + T]
+ for _ in range(3):
+ GR.step_rows(toks); sync(); GR.rewind(ctxn)
+ ts = []
+ for _ in range(20):
+ t = pc(); out = GR.step_rows(toks); sync(); ts.append((pc() - t) * 1000); GR.rewind(ctxn)
+ res.setdefault((ctxn, T, arm), []).append(statistics.median(ts))
+ if (ctxn, T, arm) not in logit:
+ logit[(ctxn, T, arm)] = out[0].float().reshape(T, -1).clone()
+for ctxn in ctxs:
+ for T in Ts:
+ a, b = res[(ctxn, T, "A")], res[(ctxn, T, "B")]
+ la, lb = logit[(ctxn, T, "A")].log_softmax(-1), logit[(ctxn, T, "B")].log_softmax(-1)
+ kl = (la.exp() * (la - lb)).sum(-1)
+ agree = (la.argmax(-1) == lb.argmax(-1)).float().mean().item()
+ print(f"AB ctx {ctxn} T {T}: A {a} B {b} ms delta {statistics.mean(b) - statistics.mean(a):+.2f} ms KL_mean {kl.mean().item():.2e} KL_max {kl.max().item():.2e} top1_agree {agree:.2f}", flush=True)
+print("ABHB_DONE", flush=True)
diff --git a/tools/hy4/step/mla_hb_test.py b/tools/hy4/step/mla_hb_test.py
new file mode 100644
index 0000000..0810ffd
--- /dev/null
+++ b/tools/hy4/step/mla_hb_test.py
@@ -0,0 +1,49 @@
+"""mla_hb_test.py: head-batched decode attention vs the shipped per-(row, head) kernel: error + time, real Hy4 shapes (R 512, P 64, H 64 / 32 window layers). Run in job.sh."""
+import os, sys, time, statistics
+import torch
+tree = os.getcwd()
+sys.path.insert(0, tree)
+from exllamav3.loader import ref_fused as rf
+dev = "cuda"
+R, P = 512, 64
+torch.manual_seed(0)
+sync = torch.cuda.synchronize
+
+
+def bench(fn, n=30):
+ for _ in range(3):
+ fn()
+ sync()
+ ts = []
+ for _ in range(n):
+ e0, e1 = torch.cuda.Event(enable_timing=True), torch.cuda.Event(enable_timing=True)
+ e0.record(); fn(); e1.record(); sync(); ts.append(e0.elapsed_time(e1) * 1000)
+ return statistics.median(ts)
+
+
+for H, window, ring in ((64, 0, 0), (32, 512, 0), (32, 512, 1024)):
+ for N in (1000, 4000):
+ cap = N + 16
+ K = (torch.randn(cap, R + P, device=dev) * 1.5)
+ sinks = torch.randn(H, device=dev)
+ for T in (1, 2, 6):
+ q = torch.randn(T, H, R, device=dev) * 0.5
+ qp = torch.randn(T, H, P, device=dev) * 0.5
+ qpos = torch.arange(N - T, N, device=dev)
+ kpos = torch.arange(cap, device=dev)
+ scale = (R + P) ** -0.5 * 1.7
+ if ring:
+ Kr = torch.zeros(ring, R + P, device=dev)
+ for j in range(max(0, N - ring), N):
+ Kr[j % ring] = K[j]
+ kc, Nk = Kr, N
+ else:
+ kc, Nk = K, N
+ def call(hb):
+ rf.HB = hb
+ return rf.mla_sink_attend(q, qp, kc[:, :R], kc[:, R:], qpos, kpos[:Nk], scale, sinks, window, None, kpos_is_arange=True, ring=ring)
+ a, b = call(False), call(True)
+ err = (a - b).abs().max().item(); rel = ((a - b).norm() / a.norm()).item()
+ ta, tb = bench(lambda: call(False)), bench(lambda: call(True))
+ print(f"H {H} win {window} ring {ring} N {N} T {T}: max_abs {err:.2e} rel {rel:.2e} old {ta:.1f} us new {tb:.1f} us", flush=True)
+print("MLA_HB_DONE", flush=True)