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.

GGUF registry: Hy4 MLA with sinks, sliding window and output gate (CPU reference)

YanissAmz85a22135d3dc
diff --git a/exllamav3/loader/gguf_blocks/mla_attn.py b/exllamav3/loader/gguf_blocks/mla_attn.py
index ec9278b..99a9e2b 100644
--- a/exllamav3/loader/gguf_blocks/mla_attn.py
+++ b/exllamav3/loader/gguf_blocks/mla_attn.py
@@ -19,7 +19,8 @@ def _mla_layer(ctx, i):
return {"n_head": nh[i], "q_lora_rank": ctx.key("attention.q_lora_rank"), "kv_lora_rank": ctx.key("attention.kv_lora_rank"),
"qk_head_dim": ctx.key("attention.key_length_mla"), "v_head_dim": ctx.key("attention.value_length_mla"),
"rope_dim": ctx.key("rope.dimension_count"), "k_b_dims": kb, "v_b_dims": ctx.dims(f"blk.{i}.attn_v_b.weight"),
- "kv_a_dims": ctx.dims(f"blk.{i}.attn_kv_a_mqa.weight"), "kq_scale_dim": ctx.key("attention.key_length_mla")}
+ "kv_a_dims": ctx.dims(f"blk.{i}.attn_kv_a_mqa.weight"), "kq_scale_dim": ctx.key("attention.key_length_mla"),
+ "rope_theta": ctx.key("rope.freq_base_swa") if _swa(ctx, i) else ctx.key("rope.freq_base")}
register(Block("mla", "attn", select=is_mla, config=_mla_layer, claims=_MLA, build=None, step="step 3", priority=1))
@@ -30,19 +31,32 @@ def _swa(ctx, i):
return bool(pat[i]) if isinstance(pat, list) and i < len(pat) else False
+def _gguf(config):
+ return getattr(config, "gguf", config) # the GGUFCollection, from a model config or passed directly
+
+
+def _build_hy4_attn(config, plan, i, key=None):
+ """Hy4 attention layer (CPU fp32 maths, loader/mla_hy4.py): MLA + sinks + window + sigmoid gate before o_proj + post norm.
+ Selected through the tensors attn_gate / attn_sinks (+ the window keys), never a model name; other MLA GGUFs keep build=None."""
+ from ..mla_hy4 import Hy4MLA
+ return Hy4MLA(_gguf(config), i)
+
+
register(Block(
"mla_window_sinks", "attn_mod", step="step 3",
select=lambda ctx, i: is_mla(ctx, i) and (ctx.has(i, "attn_sinks.weight") or (_swa(ctx, i) and ctx.key("attention.sliding_window"))),
config=lambda ctx, i: {"swa": _swa(ctx, i), "window": ctx.key("attention.sliding_window") if _swa(ctx, i) else 0,
"rope_theta": ctx.key("rope.freq_base_swa") if _swa(ctx, i) else ctx.key("rope.freq_base"),
"sinks": ctx.has(i, "attn_sinks.weight")},
- claims=frozenset({"attn_sinks.weight"}), build=None))
+ claims=frozenset({"attn_sinks.weight"}),
+ build=lambda config, plan, i, key=None: _build_hy4_attn(config, plan, i, key) if plan is None or plan.ctx.has(i, "attn_gate.weight") else None))
register(Block(
"mla_output_gate", "attn_mod", step="step 3",
select=lambda ctx, i: is_mla(ctx, i) and ctx.has(i, "attn_gate.weight"),
- config=lambda ctx, i: {"gate": "sigmoid_full", "dims": ctx.dims(f"blk.{i}.attn_gate.weight")},
- claims=frozenset({"attn_gate.weight"}), build=None))
+ config=lambda ctx, i: {"gate": "sigmoid_full", "order": "before_o_proj", "dims": ctx.dims(f"blk.{i}.attn_gate.weight"),
+ "post_norm": ctx.has(i, "post_attention_norm.weight")},
+ claims=frozenset({"attn_gate.weight"}), build=_build_hy4_attn))
def _indexer_layer(ctx, i):
diff --git a/exllamav3/loader/mla_hy4.py b/exllamav3/loader/mla_hy4.py
new file mode 100644
index 0000000..cea356e
--- /dev/null
+++ b/exllamav3/loader/mla_hy4.py
@@ -0,0 +1,129 @@
+"""CPU fp32 reference of the Hy4-flash attention layer (registry blocks mla + mla_window_sinks + mla_output_gate; design
+HY4_STRIX_DESIGN.md section 2). Maths of llama.cpp hy-v4-flash.cpp build_attention (patch M:5787-5947), config from the GGUF
+header keys and the per-layer tensors only. Weights are dequantised once (Q8_0 / F32); the ForeignLinear path is a later step.
+Only relative import: loader/engram_rows.py (tests load both by path)."""
+from __future__ import annotations
+import importlib.util, os
+import torch
+from .engram_rows import load_dense, _dequant_q8_0, q8_0_roundtrip, rms
+
+
+def _sinks_mod():
+ p = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "modules", "attention_fn", "mla_sinks.py")
+ spec = importlib.util.spec_from_file_location("exl_mla_sinks", p)
+ m = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(m)
+ return m
+
+
+def _dense3(g, name, h):
+ """GGUF 3-D Q8_0 tensor (ne0, ne1, h) -> [h, ne1, ne0] fp32 (ggml_mul_mat(w, x): out[h, j] = sum_k w[h, j, k] x[k])"""
+ ty, dims, off = g.infos[name]
+ assert ty == 8 and dims[2] == h, (name, ty, dims)
+ return _dequant_q8_0(g._pread(off, dims[1] * dims[2] * (dims[0] // 32) * 34), dims[0], dims[1] * dims[2]).view(h, dims[1], dims[0])
+
+
+def rope_norm(x: torch.Tensor, pos: torch.Tensor, base: float) -> torch.Tensor:
+ """ggml_rope_ext mode NORM (consecutive pairs), n_dims = x.shape[-1], no scaling / yarn: x [T, ..., P], pos [T]"""
+ P = x.shape[-1]
+ th = pos.double()[:, None] * (base ** (-2.0 * torch.arange(P // 2, dtype=torch.float64) / P))[None, :] # [T, P/2]
+ c, s = th.cos().float(), th.sin().float()
+ shp = (x.shape[0],) + (1,) * (x.dim() - 2) + (P // 2,)
+ c, s = c.view(shp), s.view(shp)
+ x0, x1 = x[..., 0::2], x[..., 1::2]
+ out = torch.empty_like(x)
+ out[..., 0::2], out[..., 1::2] = x0 * c - x1 * s, x0 * s + x1 * c
+ return out
+
+
+class Hy4MLA:
+ """One attention layer: cur [T, n_embd] (normed collapsed stream) -> kqv / gated / out, with a K cache the caller keeps."""
+
+ def __init__(self, g, layer: int, q8_act: bool = False, f16_kv: bool = False):
+ m = g.meta
+ p = m["general.architecture"] + "."
+ b = f"blk.{layer}."
+ self.layer, self.q8_act, self.f16_kv = layer, q8_act, f16_kv
+ self.H = int(list(m[p + "attention.head_count"])[layer])
+ self.hd, self.vd = m[p + "attention.key_length_mla"], m[p + "attention.value_length_mla"]
+ self.rope_dim, self.R = m[p + "rope.dimension_count"], m[p + "attention.kv_lora_rank"]
+ self.nope = self.hd - self.rope_dim
+ pat = m.get(p + "attention.sliding_window_pattern")
+ self.swa = bool(list(pat)[layer]) if pat is not None else False
+ self.window = int(m[p + "attention.sliding_window"]) if self.swa else 0
+ self.theta = float(m[p + ("rope.freq_base_swa" if self.swa else "rope.freq_base")])
+ self.eps = float(m[p + "attention.layer_norm_rms_epsilon"])
+ self.scale = 1.0 / float(self.hd) ** 0.5
+ self.wq_a, self.q_a_norm = load_dense(g, b + "attn_q_a.weight"), load_dense(g, b + "attn_q_a_norm.weight")
+ self.wq_b = load_dense(g, b + "attn_q_b.weight")
+ self.wkv_a, self.kv_a_norm = load_dense(g, b + "attn_kv_a_mqa.weight"), load_dense(g, b + "attn_kv_a_norm.weight")
+ self.wk_b, self.wv_b = _dense3(g, b + "attn_k_b.weight", self.H), _dense3(g, b + "attn_v_b.weight", self.H)
+ self.wo, self.wgate = load_dense(g, b + "attn_output.weight"), load_dense(g, b + "attn_gate.weight")
+ pn = b + POST_NORM
+ self.post_norm = load_dense(g, pn) if pn in g.infos else None
+ sk = b + "attn_sinks.weight"
+ self.sinks = load_dense(g, sk) if sk in g.infos else None
+ self.S = _sinks_mod()
+
+ q8_skip: tuple = () # matmul tags (wq_a wq_b wkv_a kb vb wgate wo) whose activation is NOT quantised (diagnostics)
+ f16_parts: tuple = ("q", "k")
+
+ def _q(self, x, tag=None): # activation of a Q8_0 mul_mat (llama.cpp CPU vec_dot_type)
+ return q8_0_roundtrip(x) if self.q8_act and tag not in self.q8_skip else x
+
+ def _h(self, x, part="k"):
+ return x.half().float() if self.f16_kv and part in self.f16_parts else x
+
+ def kv(self, cur, pos):
+ """cur [T, n_embd], pos [T] -> K cache rows [T, R + rope] = [rms(kv_a)*w | rope(k_pe)] (V is the first R columns)"""
+ c = self._q(cur, "wkv_a") @ self.wkv_a.T
+ lat = rms(c[:, :self.R], self.eps) * self.kv_a_norm
+ pe = rope_norm(c[:, self.R:], pos, self.theta)
+ return self._h(torch.cat([lat, pe], -1))
+
+ def q(self, cur, pos):
+ """-> q_lat [T,H,R], q_pe [T,H,rope] (q_nope absorbed with k_b; rope on the pe part)"""
+ T, H = cur.shape[0], self.H
+ qr = rms(self._q(cur, "wq_a") @ self.wq_a.T, self.eps) * self.q_a_norm
+ q = (self._q(qr, "wq_b") @ self.wq_b.T).view(T, H, self.hd)
+ qn, qp = q[..., :self.nope], rope_norm(q[..., self.nope:].contiguous(), pos, self.theta)
+ qlat = torch.einsum("hrk,thk->thr", self.wk_b, self._q(qn, "kb"))
+ return self._h(qlat, "q"), self._h(qp, "q")
+
+ def attend(self, cur, pos, kcache, kpos):
+ """cur [T, n_embd] at positions pos; kcache [N, R+rope] at kpos (includes the T new rows). -> (kqv, gated, out)"""
+ T, H = cur.shape[0], self.H
+ qlat, qpe = self.q(cur, pos)
+ o = self.S.mla_sink_attend(qlat, qpe, kcache[:, :self.R], kcache[:, self.R:], pos, kpos, self.scale, self.sinks, self.window)
+ kqv = torch.einsum("hdr,thr->thd", self.wv_b, self._q(o, "vb")).reshape(T, H * self.vd)
+ gated, out = self.gate_out(cur, kqv)
+ return kqv, gated, out
+
+ def gate_out(self, cur, kqv):
+ """isolated tail (gate, then wo, then post norm): oracle attn_kqv + cur -> (gated, out)"""
+ gated = self.S.sigmoid_gate(kqv, self._q(cur, "wgate") @ self.wgate.T)
+ out = self._q(gated, "wo") @ self.wo.T
+ return gated, (rms(out, self.eps) * self.post_norm if self.post_norm is not None else out)
+
+
+POST_NORM = "post_attention_norm.weight"
+
+
+class Hy4HCPre:
+ """hc_pre (M:5383-5418) + reduce + attn_norm: stream [T, hc, n_embd] -> normed collapsed cur [T, n_embd] and pre weights [T, hc]"""
+
+ def __init__(self, g, layer: int):
+ m = g.meta
+ p = m["general.architecture"] + "."
+ b = f"blk.{layer}."
+ self.hc, self.eps = m[p + "hyper_connection.count"], float(m[p + "attention.layer_norm_rms_epsilon"])
+ self.hc_eps = float(m[p + "hyper_connection.epsilon"])
+ self.fn, self.base, self.scale = load_dense(g, b + "hc_attn_fn.weight"), load_dense(g, b + "hc_attn_base.weight"), load_dense(g, b + "hc_attn_scale.weight")
+ self.norm = load_dense(g, b + "attn_norm.weight")
+
+ def __call__(self, x):
+ T = x.shape[0]
+ mixes = rms(x.reshape(T, -1), self.eps) @ self.fn.T
+ pre = torch.sigmoid(mixes[:, :self.hc] * self.scale[0] + self.base[:self.hc]) + self.hc_eps
+ cur = (x * pre[:, :, None]).sum(1)
+ return rms(cur, self.eps) * self.norm, pre
diff --git a/exllamav3/modules/attention_fn/mla_sinks.py b/exllamav3/modules/attention_fn/mla_sinks.py
new file mode 100644
index 0000000..7733d9a
--- /dev/null
+++ b/exllamav3/modules/attention_fn/mla_sinks.py
@@ -0,0 +1,31 @@
+"""Shared MLA variant maths: per-head attention sinks + sliding window + sigmoid output gate (Hy4-flash attention,
+llama.cpp hy-v4-flash.cpp build_attention M:5787-5947). Pure torch, no package imports, fp32, absorbed form.
+
+ scores[h, i, j] = scale * (q_lat[h, i] . ckv[j] + q_pe[h, i] . k_pe[j]) scale = 1/sqrt(nope + rope) (unabsorbed head dim)
+ visible j: kpos[j] <= qpos[i] and (window == 0 or qpos[i] - kpos[j] < window) (llama.cpp LLAMA_SWA_TYPE_STANDARD)
+ sink s[h] joins the softmax denominator only (no value): p = exp(scores - m) / (sum exp(scores - m) + exp(s - m)), m = max(max scores, s)
+ o_lat[h, i] = p @ ckv ; the caller folds W_UV per head afterwards, multiplies by sigmoid(gate_proj(x)) and then applies o_proj.
+"""
+import torch
+
+
+def mla_sink_attend(q_lat, q_pe, ckv, k_pe, qpos, kpos, scale, sinks=None, window=0):
+ """q_lat [T,H,R], q_pe [T,H,P], ckv [N,R], k_pe [N,P], qpos [T], kpos [N], sinks [H] or None -> o_lat [T,H,R]"""
+ s = torch.einsum("thr,nr->htn", q_lat, ckv) + torch.einsum("thp,np->htn", q_pe, k_pe)
+ s = s * scale
+ vis = kpos[None, :] <= qpos[:, None]
+ if window:
+ vis = vis & ((qpos[:, None] - kpos[None, :]) < window)
+ s = s.masked_fill(~vis[None], float("-inf"))
+ if sinks is not None:
+ s = torch.cat([s, sinks.float()[:, None, None].expand(-1, s.shape[1], 1)], dim=-1)
+ p = torch.softmax(s, dim=-1)
+ if sinks is not None:
+ p = p[..., :-1]
+ return torch.einsum("htn,nr->thr", p, ckv)
+
+
+def sigmoid_gate(o, gate_logits):
+ """the Hy4 output gate: o * sigmoid(gate_logits), before o_proj"""
+ return o * torch.sigmoid(gate_logits)
+
diff --git a/tests/test_gguf_hy4_attn.py b/tests/test_gguf_hy4_attn.py
new file mode 100644
index 0000000..079e888
--- /dev/null
+++ b/tests/test_gguf_hy4_attn.py
@@ -0,0 +1,249 @@
+"""Hy4-flash attention block (MLA + per-head sinks + sliding window + sigmoid output gate), CPU only, no extension.
+Loaded by path under stub packages (as tests/test_hy4_engram_rows.py). Run:
+ HIP_VISIBLE_DEVICES= CUDA_VISIBLE_DEVICES= nice -n 19 python -m pytest tests/test_gguf_hy4_attn.py -q -s
+Oracle part (needs the Hy4 Q3_K parts + llama.cpp dump, else skipped; env HY4_GGUF / HY4_ORACLE / HY4_PROMPTS / HY4_DEC override):
+ layer 0 (full, indexer) all prompts: input = token embedding tiled to hc streams -> hc_pre -> rms * attn_norm (the attention input is
+ not dumped, only the hc_pre weights, which are checked too). Layers 1 (full) and 2 (sliding window): p1 only, input = c<N>_engram_out-1 /
+ c<N>_l_out-1 (every position of p1 is dumped; the long prompts dump a handful of positions, so the earlier-token KV cannot be rebuilt).
+ Targets: attn_kqv (after W_UV), attn_gated (after the sigmoid gate), attn_out (after wo + post_attention_norm). Bar: max abs / max|ref| < 1e-3
+ (the _rel of tests/test_glm_dec2.py:91). Variants: exact fp32; q8act (activation of every Q8_0 mul_mat quantised to Q8_0, as Hy4Engram q8_act);
+ q8act+f16kv (Q, K, V rounded to fp16 as the llama.cpp f16 KV cache / flash-attention does)."""
+import importlib.util, json, os, resource, sys, types
+import numpy as np
+import pytest
+import torch
+
+ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
+GGUF = os.environ.get("HY4_GGUF", "/path/to/models/hy4-flash-gguf/Hy4-flash-Q3_K-00001-of-00002.gguf")
+ORACLE = os.environ.get("HY4_ORACLE", "/path/to/oracle_out")
+BAR = 1e-3
+
+
+def _stub():
+ for name, sub in (("exllamav3", ""), ("exllamav3.loader", "loader")):
+ if name not in sys.modules:
+ m = types.ModuleType(name)
+ m.__path__ = [os.path.join(ROOT, "exllamav3", sub)]
+ sys.modules[name] = m
+
+
+def _load(modname, rel):
+ _stub()
+ if modname in sys.modules:
+ return sys.modules[modname]
+ spec = importlib.util.spec_from_file_location(modname, os.path.join(ROOT, rel))
+ mod = importlib.util.module_from_spec(spec)
+ mod.__package__ = modname.rsplit(".", 1)[0]
+ sys.modules[modname] = mod
+ spec.loader.exec_module(mod)
+ return mod
+
+
+def mla_mod():
+ _load("exllamav3.loader.engram_rows", "exllamav3/loader/engram_rows.py")
+ return _load("exllamav3.loader.mla_hy4", "exllamav3/loader/mla_hy4.py")
+
+
+def sinks_mod():
+ return mla_mod()._sinks_mod()
+
+
+def mem_avail_gib():
+ for ln in open("/proc/meminfo"):
+ if ln.startswith("MemAvailable"):
+ return int(ln.split()[1]) / 2 ** 20
+ return 0.0
+
+
+# ---------------------------------------------------------------- shared variant maths (synthetic)
+def _naive(q_lat, q_pe, ckv, k_pe, qpos, kpos, scale, sinks, window):
+ T, H, _ = q_lat.shape
+ out = torch.zeros(T, H, ckv.shape[1], dtype=torch.float64)
+ for t in range(T):
+ for h in range(H):
+ idx = [j for j in range(ckv.shape[0]) if kpos[j] <= qpos[t] and (not window or qpos[t] - kpos[j] < window)]
+ s = torch.stack([(q_lat[t, h].double() @ ckv[j].double() + q_pe[t, h].double() @ k_pe[j].double()) * scale for j in idx])
+ m = s.max() if sinks is None else torch.maximum(s.max(), sinks[h].double())
+ e = torch.exp(s - m)
+ den = e.sum() + (0 if sinks is None else torch.exp(sinks[h].double() - m))
+ out[t, h] = (e[:, None] * torch.stack([ckv[j].double() for j in idx])).sum(0) / den
+ return out
+
+
[email protected]("sinks,window", [(False, 0), (True, 0), (True, 5), (False, 4)])
+def test_sink_window_maths_vs_naive(sinks, window):
+ S = sinks_mod()
+ g = torch.Generator().manual_seed(1)
+ T, H, R, P, N = 6, 3, 8, 4, 12
+ ql, qp, ck, kp = (torch.randn(*s, generator=g) for s in ((T, H, R), (T, H, P), (N, R), (N, P)))
+ sk = torch.randn(H, generator=g) * 3 if sinks else None
+ qpos, kpos = torch.arange(6, 12), torch.arange(12)
+ ref = _naive(ql, qp, ck, kp, qpos, kpos, 0.37, sk, window)
+ got = S.mla_sink_attend(ql, qp, ck, kp, qpos, kpos, 0.37, sk, window)
+ assert float((got - ref).abs().max()) < 1e-5
+
+
+def test_sink_does_not_add_value_mass():
+ S = sinks_mod()
+ ql, qp = torch.zeros(1, 1, 2), torch.zeros(1, 1, 2)
+ ck, kp = torch.ones(1, 2), torch.zeros(1, 2)
+ o = S.mla_sink_attend(ql, qp, ck, kp, torch.tensor([0]), torch.tensor([0]), 1.0, torch.tensor([0.0]), 0)
+ assert abs(float(o[0, 0, 0]) - 0.5) < 1e-6 # one key at logit 0 and a sink at logit 0: p = 1/2, no value for the sink
+
+
+def test_rope_norm_pairs():
+ M = mla_mod()
+ x = torch.randn(3, 2, 8)
+ pos = torch.tensor([0, 1, 7])
+ y = M.rope_norm(x, pos, 1e4)
+ assert torch.allclose(y[0], x[0]) # position 0 is identity
+ th = 7 * 1e4 ** (-2.0 * 2 / 8) # pair 2 at position 7
+ assert abs(float(y[2, 0, 4]) - float(x[2, 0, 4] * np.cos(th) - x[2, 0, 5] * np.sin(th))) < 1e-5
+ assert torch.allclose(y.norm(dim=-1), x.norm(dim=-1), atol=1e-5)
+
+
+# ---------------------------------------------------------------- registry predicates
+def test_registry_predicates():
+ reg = _load("exllamav3.loader.gguf_registry", "exllamav3/loader/gguf_registry.py")
+ importlib.import_module("exllamav3.loader.gguf_blocks.mla_attn")
+ B = reg.REGISTRY
+
+ class G:
+ def __init__(self, sinks, gate, swa, post=True):
+ self.meta = {"general.architecture": "x", "x.block_count": 3, "x.attention.kv_lora_rank": 512, "x.attention.head_count": [4, 4, 8],
+ "x.attention.q_lora_rank": 16, "x.attention.key_length_mla": 256, "x.attention.value_length_mla": 256,
+ "x.rope.dimension_count": 64, "x.attention.sliding_window": 512, "x.attention.sliding_window_pattern": [False, False, swa],
+ "x.rope.freq_base": 1e7, "x.rope.freq_base_swa": 1e4}
+ names = ["attn_kv_a_mqa.weight", "attn_k_b.weight", "attn_v_b.weight", "attn_q_a.weight", "attn_q_b.weight", "attn_output.weight"]
+ names += (["attn_sinks.weight"] if sinks else []) + (["attn_gate.weight"] if gate else []) + (["post_attention_norm.weight"] if post else [])
+ self.infos = {f"blk.{i}.{n}": (8, (4, 4, 4), 0) for i in range(3) for n in names}
+
+ plain = reg.Ctx(G(False, False, False))
+ assert B["mla"].select(plain, 2) and not B["mla_window_sinks"].select(plain, 2) and not B["mla_output_gate"].select(plain, 2)
+ hy = reg.Ctx(G(True, True, True))
+ assert B["mla_window_sinks"].select(hy, 2) and B["mla_output_gate"].select(hy, 0)
+ cfg = B["mla_window_sinks"].config(hy, 2)
+ assert cfg["swa"] and cfg["window"] == 512 and cfg["rope_theta"] == 1e4 and cfg["sinks"]
+ cfg0 = B["mla_window_sinks"].config(hy, 0)
+ assert not cfg0["swa"] and cfg0["window"] == 0 and cfg0["rope_theta"] == 1e7 and cfg0["sinks"]
+ assert B["mla_window_sinks"].build is not None and B["mla_output_gate"].build is not None
+ assert B["mla_output_gate"].config(hy, 0)["order"] == "before_o_proj"
+
+
+# ---------------------------------------------------------------- oracle
+def _have_oracle():
+ return os.path.exists(GGUF) and os.path.exists(os.path.join(ORACLE, "p1_short_en", "index.json"))
+
+
+class Dump:
+ def __init__(self, prompt):
+ self.dir = os.path.join(ORACLE, prompt)
+ self.idx = json.load(open(os.path.join(self.dir, "index.json")))
+ self.ids = self.idx["prompt_ids"] + self.idx["gen_ids"]
+ self.by = {(t["name"], t["layer"], t["occ"], t["call"]): t for t in self.idx["tensors"]}
+ self.calls = sorted({t["call"] for t in self.idx["tensors"]})
+
+ def get(self, name, layer, call, occ=0):
+ t = self.by.get((name, layer, occ, call))
+ if t is None:
+ return None, None
+ a = np.fromfile(os.path.join(self.dir, t["file"]), dtype="<f4")
+ T = len(t["positions"])
+ a = a.reshape(T, 4, -1) if name in ("l_out", "engram_out") else a.reshape(T, -1) # one-token calls drop the token axis in shape_ggml
+ return torch.from_numpy(a.copy()), t["positions"]
+
+
+def _emb_rows(g, ids):
+ R = mla_mod()
+ ty, dims, off = g.infos["token_embd.weight"]
+ assert ty == 8 and dims[0] == 3072
+ rb = dims[0] // 32 * 34
+ return torch.cat([R._dequant_q8_0(g._pread(off + int(t) * rb, rb), dims[0], 1) for t in ids])
+
+
+def _rel(a, ref):
+ ma = float((a - ref).abs().max())
+ return ma, ma / float(ref.abs().max())
+
+
+VARIANTS = (("exact", False, False, False), ("q8act", True, False, False), ("q8act+f16kv", True, True, False))
+
+
+def run_layer(g, D, prompt, layer, att, hcp, max_pos, max_dec, rows):
+ """layer 0: stream = embedding tiled; layers 1 / 2: stream from the dumps of earlier / current calls (positions seen so far)."""
+ in_name = {0: None, 1: "engram_out", 2: "l_out"}[layer]
+ n_pf = D.idx["prefill_calls"]
+ calls = [c for c in D.calls if c < n_pf + max_dec]
+ stream = {}
+ if layer == 0:
+ n_all = min(len(D.ids), max_pos + 1)
+ x_all = _emb_rows(g, D.ids[:n_all])[:, None, :].expand(-1, 4, -1).contiguous()
+ cur_all, pre_all = hcp(x_all)
+ for tag, q8, f16, fa in VARIANTS:
+ att.q8_act, att.f16_kv = q8, f16
+ kc, kpos_known = {}, []
+ if layer == 0:
+ kcache = att.kv(cur_all, torch.arange(cur_all.shape[0]))
+ for c in calls:
+ kqv_ref, pos = D.get("attn_kqv", layer, c)
+ if kqv_ref is None or max(pos) > max_pos:
+ continue
+ gat_ref, _ = D.get("attn_gated", layer, c)
+ out_ref, _ = D.get("attn_out", layer, c)
+ pos_t = torch.tensor(pos)
+ if layer == 0:
+ cur, pre = cur_all[pos_t], pre_all[pos_t]
+ if tag == "exact":
+ hp, _ = D.get("hc_pre", 0, c)
+ if hp is not None:
+ rows.append((prompt, layer, c, "embedding+hc_pre.weights", "hc_pre(dump o0)", len(pos), *_rel(pre, hp)))
+ kcs, kp = kcache[:max(pos) + 1], torch.arange(max(pos) + 1)
+ else:
+ xs, _ = D.get(in_name, layer - 1 if in_name == "l_out" else layer, c)
+ for k, p in enumerate(pos):
+ stream[p] = xs[k]
+ cur, _p = hcp(xs)
+ known = sorted(stream)
+ allx = torch.stack([stream[p] for p in known])
+ cur_k, _ = hcp(allx)
+ kcs, kp = att.kv(cur_k, torch.tensor(known)), torch.tensor(known)
+ kqv, gated, out = att.attend(cur, pos_t, kcs, kp)
+ g2, o2 = att.gate_out(cur, kqv_ref)
+ kind = "prefill" if c < n_pf else "decode"
+ rows.append((prompt + "/" + tag, layer, c, f"{kind} cur+KV", "attn_kqv", len(pos), *_rel(kqv, kqv_ref)))
+ rows.append((prompt + "/" + tag, layer, c, f"{kind} cur+KV", "attn_gated", len(pos), *_rel(gated, gat_ref)))
+ rows.append((prompt + "/" + tag, layer, c, f"{kind} cur+KV", "attn_out", len(pos), *_rel(out, out_ref)))
+ rows.append((prompt + "/" + tag, layer, c, f"{kind} cur+attn_kqv(oracle)", "attn_gated", len(pos), *_rel(g2, gat_ref)))
+ rows.append((prompt + "/" + tag, layer, c, f"{kind} cur+attn_kqv(oracle)", "attn_out", len(pos), *_rel(o2, out_ref)))
+
+
[email protected](not _have_oracle(), reason="Hy4 GGUF / oracle dump not on this host")
+def test_attention_vs_oracle():
+ if mem_avail_gib() < 14:
+ pytest.skip(f"MemAvailable {mem_avail_gib():.1f} GiB < 14")
+ M = mla_mod()
+ gc = _load("exllamav3.loader.gguf_collection", "exllamav3/loader/gguf_collection.py")
+ g = gc.GGUFCollection(GGUF)
+ max_dec = int(os.environ.get("HY4_DEC", "3"))
+ plan = [(L, p) for L, ps in ((0, os.environ.get("HY4_PROMPTS", "p1_short_en,p4_long_n700,p5_long_n4000").split(",")),
+ (1, ["p1_short_en"]), (2, ["p1_short_en"])) for p in ps]
+ rows = []
+ cache = {}
+ for L, p in plan:
+ if L not in cache:
+ cache = {L: (M.Hy4MLA(g, L), M.Hy4HCPre(g, L))}
+ att, hcp = cache[L]
+ # layer 0 of the 4000-token prompt: the indexer (top_k 2048, pool 4, tail group) is not binding below ~2040; later positions need it
+ max_pos = 2039 if p.startswith("p5") else 10 ** 9
+ run_layer(g, Dump(p), p, L, att, hcp, max_pos, max_dec, rows)
+ print(f"[rss {resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 2 ** 20:.2f} GiB after layer {L} {p}]", flush=True)
+ print()
+ print("prompt/variant layer call input target ntok max_abs rel bar PASS/FAIL")
+ for r in rows:
+ print(f"{r[0]} L{r[1]} c{r[2]:03d} {r[3]} -> {r[4]} n={r[5]} {r[6]:.3e} {r[7]:.3e} {BAR:.0e} {'PASS' if r[7] < BAR else 'FAIL'}")
+ assert resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 2 ** 20 < 6
+ # llama.cpp CPU: Q8_0 activations + f16 KV cache. Emulated path: every row within 5e-3 (a sink / window / gate bug is O(0.1..1));
+ # the 1e-3 bar is reported per row (see the docstring: layer 0 inputs are rebuilt from the embedding, a Q8_0 rounding flip of cur shows up at ~1e-3)
+ emu = [r for r in rows if r[0].endswith("/q8act+f16kv")]
+ assert emu and all(r[7] < 5e-3 for r in emu)
+ assert all(r[7] < 1e-3 for r in rows if r[0].endswith("/q8act+f16kv") and r[3].endswith("attn_kqv(oracle)") and r[1] != 0)