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 head: verify rows on tl.dot (HY4_HEAD_DOT=1, opt-in): fp16 WMMA with hi + lo activations, bf16 weights read once

YanissAmzcccd9344e8bc
diff --git a/exllamav3/loader/head_bf16.py b/exllamav3/loader/head_bf16.py
index 587386a..34f676a 100644
--- a/exllamav3/loader/head_bf16.py
+++ b/exllamav3/loader/head_bf16.py
@@ -10,6 +10,10 @@ import triton.language as tl
ENABLED = os.environ.get("HY4_HEAD_BF16", "0") == "1"
BV = int(os.environ.get("HY4_HEAD_BV", "8"))
BD = int(os.environ.get("HY4_HEAD_BD", "512"))
+DOT = os.environ.get("HY4_HEAD_DOT", "0") == "1" # 1 = verify rows (T >= 2) on tl.dot: fp16 WMMA, h split hi + lo (rel ~1e-7), W bf16 -> fp16 exact above 6e-8
+DOT_BV = int(os.environ.get("HY4_HEAD_DOT_BV", "64"))
+DOT_BD = int(os.environ.get("HY4_HEAD_DOT_BD", "128"))
+DOT_NW = int(os.environ.get("HY4_HEAD_DOT_NW", "4"))
ROW_TILES = os.environ.get("HY4_ROW_TILES", "0") == "1" # 1 = verify-row tiles tuned on the Strix (T 2..4: BD 64 + 2 warps; T >= 5: BV 16, BD 32)
@@ -44,6 +48,26 @@ def _k_headt(w_ptr, h_ptr, o_ptr, V, D, T, BT: tl.constexpr, BV: tl.constexpr, B
tl.store(o_ptr + tt[:, None] * V + rows[None, :], acc, mask=tm[:, None] & rm[None, :])
+def _k_headd(w_ptr, h_ptr, o_ptr, V, D, T, BT: tl.constexpr, BV: tl.constexpr, BD: tl.constexpr):
+ """T rows on the matrix units: out[t, v] = sum_d h[t, d] * W[v, d]. Each W tile is read once; h (fp32) = hi + lo fp16 pieces, two fp16 dots with fp32 accumulation."""
+ rows = tl.program_id(0) * BV + tl.arange(0, BV)
+ rm = rows < V
+ tt = tl.arange(0, BT)
+ tm = tt < T
+ acc = tl.zeros([BT, BV], dtype=tl.float32)
+ for d0 in range(0, D, BD):
+ cols = d0 + tl.arange(0, BD)
+ cm = cols < D
+ w = tl.load(w_ptr + rows[:, None] * D + cols[None, :], mask=rm[:, None] & cm[None, :], other=0.0).to(tl.float16)
+ hv = tl.load(h_ptr + tt[:, None] * D + cols[None, :], mask=tm[:, None] & cm[None, :], other=0.0)
+ hh = hv.to(tl.float16)
+ hl = (hv - hh.to(tl.float32)).to(tl.float16)
+ wt = tl.trans(w)
+ acc += tl.dot(hh, wt) + tl.dot(hl, wt)
+ tl.store(o_ptr + tt[:, None] * V + rows[None, :], acc, mask=tm[:, None] & rm[None, :])
+
+
def usable(h, W):
return ENABLED and 1 <= h.shape[0] <= 16 and W.dim() == 2 and W.is_contiguous() and h.dtype == torch.float32 and W.dtype in (torch.bfloat16, torch.float16, torch.float32) \
and (W.is_cuda or os.environ.get("TRITON_INTERPRET", "0") == "1")
@@ -55,6 +79,9 @@ def gemv(h, W):
T = h.shape[0]
if T > 1:
out = torch.empty(T, V, dtype=torch.float32, device=W.device)
+ if DOT and D % DOT_BD == 0 and W.dtype in (torch.bfloat16, torch.float16):
+ _k_headd[(triton.cdiv(V, DOT_BV),)](W, h.contiguous(), out, V, D, T, BT=16, BV=DOT_BV, BD=DOT_BD, num_warps=DOT_NW)
+ return out
bv, bd, nw = BV, min(BD, 128), 4
if ROW_TILES:
bv, bd, nw = (BV, 64, 2) if T <= 4 else (16, 32, 4)
diff --git a/tools/hy4/step/head_dot_test.py b/tools/hy4/step/head_dot_test.py
new file mode 100644
index 0000000..6a26b0a
--- /dev/null
+++ b/tools/hy4/step/head_dot_test.py
@@ -0,0 +1,35 @@
+"""head_dot_test.py: HY4_HEAD_DOT kernel vs the shipped fp32 row-batched kernel + fp64 torch reference; error always, time when on GPU (DEV=cuda). Loads head_bf16.py by path."""
+import os, sys, statistics, importlib.util
+import torch
+tree = os.getcwd()
+def mod(name, rel):
+ sp = importlib.util.spec_from_file_location(name, os.path.join(tree, "exllamav3", "loader", rel)); m = importlib.util.module_from_spec(sp); sp.loader.exec_module(m); return m
+hb = mod("head_bf16", "head_bf16.py")
+dev = os.environ.get("DEV", "cuda"); CPU = dev == "cpu"
+V, D = (700, 256) if CPU else (129024, 4096)
+torch.manual_seed(0)
+W = (torch.randn(V, D, device=dev) * 0.05).to(torch.bfloat16)
+hb.ENABLED = True
+def bench(fn, n=30):
+ for _ in range(3): fn()
+ torch.cuda.synchronize(); ts = []
+ for _ in range(n):
+ a, b = torch.cuda.Event(enable_timing=True), torch.cuda.Event(enable_timing=True)
+ a.record(); fn(); b.record(); torch.cuda.synchronize(); ts.append(a.elapsed_time(b) * 1000)
+ return statistics.median(ts)
+for T in (2, 3, 6, 8):
+ h = torch.randn(T, D, device=dev)
+ ref = (h.double() @ W.double().T).float()
+ hb.DOT = False; a = hb.gemv(h, W)
+ for bv, bd, nw in (((16, 64, 4),) if CPU else ((64, 128, 4), (64, 64, 4), (128, 64, 8), (32, 128, 4), (128, 128, 8))):
+ hb.DOT, hb.DOT_BV, hb.DOT_BD, hb.DOT_NW = True, bv, bd, nw
+ try:
+ b = hb.gemv(h, W)
+ except Exception as e:
+ print(f"T {T} BV {bv} BD {bd} NW {nw}: FAIL {type(e).__name__} {str(e)[:80]}", flush=True); continue
+ ea = ((a - ref).norm() / ref.norm()).item(); eb = ((b - ref).norm() / ref.norm()).item()
+ if CPU: print(f"T {T}: rel err old {ea:.1e} dot {eb:.1e}", flush=True)
+ else:
+ hb.DOT = False; ta = bench(lambda: hb.gemv(h, W)); hb.DOT = True; tb = bench(lambda: hb.gemv(h, W))
+ print(f"T {T} BV {bv} BD {bd} NW {nw}: rel err old {ea:.1e} dot {eb:.1e}; old {ta:.0f} us dot {tb:.0f} us", flush=True)
+print("HEAD_DOT_DONE", flush=True)