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.

13.0 KB
Raw Blame
"""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


@pytest.mark.parametrize("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)))


@pytest.mark.skipif(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)