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 latent MoE block (sigmoid router + score bias, latent routed experts, full-width shared expert)

YanissAmz81793080f0aa
diff --git a/exllamav3/loader/gguf_blocks/mlp.py b/exllamav3/loader/gguf_blocks/mlp.py
index deb9a6a..6b1a799 100644
--- a/exllamav3/loader/gguf_blocks/mlp.py
+++ b/exllamav3/loader/gguf_blocks/mlp.py
@@ -145,24 +145,78 @@ register(Block(
config=_moe_layer, claims=_MOE_CLAIMS, build=_moe_build, names=_moe_names))
-# ---- predicates only (Hy4 step 5): latent MoE (routed experts at expert_latent_length between ffn_routed_down/up),
-# sigmoid router (gating_func 2) + exp_probs_b, weights_scale, per-layer swiglu_clamp_exp; engine module: block_sparse_mlp latent_in/out
+# ---- latent MoE (Hy4-flash, llama.cpp build_latent_moe M:5949): router on the full-width input (sigmoid + exp_probs_b for the pick,
+# unbiased weights, renormalised, x weights_scale), routed experts at expert_latent_length between ffn_routed_down / _up,
+# pre-activation swiglu clamp on the routed experts only, one full-width shared expert added after ffn_routed_up.
+# The post_ffw_norm that follows is the stream block's (hyperconnections._stream_build), not this block's.
+# engine module: block_sparse_mlp latent_in / latent_out + router_type "ds3" with one group
def _latent_layer(ctx, i):
gd = ctx.dims(f"blk.{i}.ffn_gate_exps.weight")
sh = ctx.dims(f"blk.{i}.ffn_gate_shexp.weight")
+ rd = ctx.dims(f"blk.{i}.ffn_routed_down.weight")
clamp = ctx.per_layer("swiglu_clamp_exp", 0.0)
- return {"latent": ctx.key("expert_latent_length"), "latent_down": ctx.dims(f"blk.{i}.ffn_routed_down.weight"),
- "expert_dims": gd, "num_experts": gd[2] if gd else None, "top_k": ctx.key("expert_used_count"),
+ lat = ctx.key("expert_latent_length")
+ if ctx.key("expert_gating_func") != 2:
+ ctx.gaps.append("latent MoE: expert_gating_func is not sigmoid (2)")
+ if gd and rd and not (gd[0] == lat == rd[1]):
+ ctx.gaps.append(f"latent MoE layer {i}: expert_latent_length {lat} disagrees with ffn_gate_exps {gd} / ffn_routed_down {rd}")
+ if not ctx.has(i, "exp_probs_b.bias") or not ctx.has(i, "ffn_routed_up.weight"):
+ ctx.gaps.append(f"latent MoE layer {i}: exp_probs_b.bias / ffn_routed_up.weight missing")
+ return {"latent": lat, "latent_down": rd, "expert_dims": gd, "num_experts": gd[2] if gd else None, "top_k": ctx.key("expert_used_count"),
"gating_func": ctx.key("expert_gating_func"), "weights_scale": ctx.key("expert_weights_scale"),
"weights_norm": ctx.key("expert_weights_norm"), "e_score_bias": ctx.has(i, "exp_probs_b.bias"),
"clamp": clamp[i] if i < len(clamp) else None, "clamp_routed_only": bool(ctx.feats.get("clamp_routed_only")),
"shared_dims": sh}
+def _latent_names(cfg, i, L, G):
+ nm = {f"{L}.mlp.gate.weight": f"{G}.ffn_gate_inp.weight", f"{L}.mlp.gate.e_score_correction_bias": f"{G}.exp_probs_b.bias",
+ f"{L}.mlp.fc1_latent_proj.weight": f"{G}.ffn_routed_down.weight", f"{L}.mlp.fc2_latent_proj.weight": f"{G}.ffn_routed_up.weight"}
+ for proj in ("gate", "up", "down"):
+ nm[f"{L}.mlp.shared_experts.{proj}_proj.weight"] = f"{G}.ffn_{proj}_shexp.weight"
+ for e in range(cfg["num_experts"]):
+ for proj, gp in (("gate_proj", "ffn_gate_exps"), ("up_proj", "ffn_up_exps"), ("down_proj", "ffn_down_exps")):
+ nm[f"{L}.mlp.experts.{e}.{proj}.weight"] = f"{G}.{gp}.e{e}.weight"
+ return nm
+
+
+def _latent_build(config, plan, idx, L):
+ import torch
+ from ...modules import GatedMLP, BlockSparseMLP
+ from ...architecture.gguf_generic import _fl
+ c = plan.configs[("latent_moe", idx)]
+ H, LAT, I, E = config.hidden_size, c["latent"], c["expert_dims"][1], c["num_experts"]
+ SI, SK = c["shared_dims"][1], f"{L}.mlp.shared_experts"
+ gates = [_fl(config, f"{L}.mlp.experts.{e}.gate_proj", LAT, I) for e in range(E)]
+ ups = [_fl(config, f"{L}.mlp.experts.{e}.up_proj", LAT, I) for e in range(E)]
+ downs = [_fl(config, f"{L}.mlp.experts.{e}.down_proj", I, LAT, out_dtype=torch.float) for e in range(E)]
+ shared = GatedMLP(config=config, key=SK, hidden_size=H, intermediate_size=SI, qmap="block.mlp", interm_dtype=torch.half, out_dtype=torch.float,
+ gates=[_fl(config, f"{SK}.gate_proj", H, SI)], ups=[_fl(config, f"{SK}.up_proj", H, SI)],
+ downs=[_fl(config, f"{SK}.down_proj", SI, H, out_dtype=torch.float)])
+ for m in shared.gates + shared.ups + shared.downs:
+ shared.register_submodule(m)
+ kw = {}
+ if c["clamp"]:
+ # the tree's clamp is post-activation; Hy4's is pre-activation (min(gate, c) then silu): they differ only for gate > c (<= 4.6e-4 at c = 10)
+ kw["act_limit"] = float(c["clamp"])
+ mlp = BlockSparseMLP(config=config, key=f"{L}.mlp", hidden_size=H, intermediate_size=I, num_experts=E, shared_experts=shared,
+ num_experts_per_tok=c["top_k"], norm_topk=bool(c["weights_norm"]), key_up=None, key_gate=None, key_down=None,
+ key_routing_gate="gate", qmap="block.mlp", interm_dtype=torch.half, out_dtype=torch.float,
+ gates=gates, ups=ups, downs=downs, latent_size=LAT,
+ key_latent_in="fc1_latent_proj", key_latent_out="fc2_latent_proj",
+ latent_in=_fl(config, f"{L}.mlp.fc1_latent_proj", H, LAT),
+ latent_out=_fl(config, f"{L}.mlp.fc2_latent_proj", LAT, H, out_dtype=torch.float),
+ router_type="ds3", n_group=1, topk_group=1, routed_scaling_factor=float(c["weights_scale"]), **kw)
+ for m in gates + ups + downs:
+ mlp.register_submodule(m)
+ return mlp
+
+
register(Block(
"latent_moe", "ffn", step="step 5",
select=lambda ctx, i: ctx.has(i, "ffn_routed_down.weight") and ctx.has(i, "ffn_gate_exps.weight"),
- config=_latent_layer, claims=_MOE_CLAIMS | {"ffn_routed_down.weight", "ffn_routed_up.weight", "exp_probs_b.bias"}, build=None))
+ config=_latent_layer, claims=_MOE_CLAIMS | {"ffn_routed_down.weight", "ffn_routed_up.weight", "exp_probs_b.bias"},
+ build=_latent_build, names=_latent_names))
# sigmoid / sqrt-softplus router with a per-expert score bias on a plain MoE layer (glm5-next, deepseek41); predicate only
register(Block(
diff --git a/tests/test_gguf_hy4_moe.py b/tests/test_gguf_hy4_moe.py
new file mode 100644
index 0000000..4eac62e
--- /dev/null
+++ b/tests/test_gguf_hy4_moe.py
@@ -0,0 +1,196 @@
+"""Hy4-flash latent MoE block (registry block `latent_moe`, loader/gguf_blocks/mlp.py) vs the llama.cpp oracle (CPU only, no extension).
+Maths (llama.cpp build_latent_moe, M:5949; build_moe_ffn with gating func 2): x = ffn_norm(collapsed ffn input) (3072);
+ router: p = sigmoid(x @ ffn_gate_inp^T) (512, F32), pick top-16 of p + exp_probs_b, weights = p[pick] / sum * 3.998;
+ routed: z = ffn_routed_down x (3072 -> 1536, Q8_0); per expert h = silu(min(gate z, 10)) * clip(up z, +-10) (Q4_K), e = down h (1536);
+ ffn_moe_out (the oracle's 1536-wide dump) = sum_k w_k e_k; y = ffn_routed_up ffn_moe_out (1536 -> 3072, Q8_0)
+ shared: ffn_down_shexp(silu(ffn_gate_shexp x) * ffn_up_shexp x) at full width, NOT clamped; ffn = y + shared; then post_ffw_norm (the stream block).
+The module path (BlockSparseMLP + ForeignLinear) needs the extension, so this checks the registry (names, config, constructor kwargs through stubs)
+and the maths with the GGUF's own tensors: only the routed experts the router picks for the tokens under test are read (by offset, one expert at a time).
+The ffn input is rebuilt as the HC child did: s1 = attn site of the oracle's input stream + attn_out-L, collapsed by the ffn site, rms * ffn_norm.
+Layer 5 cannot be rebuilt (no l_out-4 / engram dump of its input stream).
+Float matmuls on CPU: llama.cpp quantises the activation to Q8_0 (Q8_0 weights) / Q8_K (Q4_K weights); both variants are reported (q8 = emulated).
+Bars: cosine (min over tokens) >= 0.9995 on the oracle's own input; rel = max abs / max|target| reported, not gated at 1e-3."""
+import os, sys, json
+import numpy as np
+import pytest
+import torch
+
+sys.path.insert(0, os.path.dirname(__file__))
+import test_gguf_hy4_hc as T # stubs + registry
+from test_gguf_hy4_hc import ORACLE, gg, Cfg, rel, site
+from exllamav3.loader.engram_rows import load_dense, q8_0_roundtrip
+
+COS_BAR = 0.9995
+pytestmark = pytest.mark.skipif(not (os.path.isdir(ORACLE) and os.path.exists(T.GGUF1)), reason="oracle dumps / Hy4 GGUF absent")
+NT_MAX = 8 # prefill calls: first tokens only (a few experts' rows, never the whole stack)
+EPS = float(np.float32(9.999999747378752e-06))
+_NDIM = {"attn_out": 2, "l_out": 3, "engram_out": 3, "ffn_moe_out": 2}
+
+
+def q8k_roundtrip(x):
+ """ggml quantize_row_q8_K_ref + dequant: per 256 values iscale = -127 / (signed value of max |x|), q = nearest_int (even), d = 1/iscale"""
+ b = x.reshape(*x.shape[:-1], -1, 256)
+ idx = b.abs().argmax(-1, keepdim=True)
+ mx = torch.gather(b, -1, idx)
+ iscale = torch.where(mx != 0, -127.0 / mx, torch.zeros_like(mx))
+ q = torch.round(b * iscale).clamp(-127, 127)
+ d = torch.where(iscale != 0, 1.0 / iscale, torch.zeros_like(iscale))
+ return (q * d).reshape(x.shape)
+
+
+def dq_q4k(raw, N, K):
+ """Q4_K (144 B / 256: fp16 d, fp16 dmin, 12 B packed 6-bit scales / mins, 128 B nibbles) -> fp32 [N, K] (ggml dequantize_row_q4_K)"""
+ nb = K // 256
+ r = torch.from_numpy(np.ascontiguousarray(raw)).reshape(N * nb, 144)
+ d = r[:, 0:2].contiguous().view(torch.float16).float().reshape(-1, 1)
+ dm = r[:, 2:4].contiguous().view(torch.float16).float().reshape(-1, 1)
+ s = r[:, 4:16].int()
+ sc = torch.cat([s[:, 0:4] & 63, (s[:, 8:12] & 15) | ((s[:, 0:4] >> 6) << 4)], 1).float()
+ mn = torch.cat([s[:, 4:8] & 63, (s[:, 8:12] >> 4) | ((s[:, 4:8] >> 6) << 4)], 1).float()
+ qs = r[:, 16:].int().reshape(-1, 4, 32)
+ out = torch.empty(N * nb, 8, 32)
+ for j in range(4):
+ out[:, 2 * j] = (qs[:, j] & 15).float() * (d * sc[:, 2 * j:2 * j + 1]) - dm * mn[:, 2 * j:2 * j + 1]
+ out[:, 2 * j + 1] = (qs[:, j] >> 4).float() * (d * sc[:, 2 * j + 1:2 * j + 2]) - dm * mn[:, 2 * j + 1:2 * j + 2]
+ return out.reshape(N, K)
+
+
+def rmsn(x, w, eps=EPS):
+ return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + eps) * w
+
+
+def ffn_ref(g, L, x, q8, clamp_pre=True, rounding=None):
+ """x (nt, 3072) normed ffn input -> dict(moe latent (nt,1536), ffn (nt,3072) pre post-norm, sel (nt,16), w)"""
+ qa8 = (lambda t: q8_0_roundtrip(t)) if q8 else (lambda t: t)
+ qak = (lambda t: q8k_roundtrip(t)) if q8 else (lambda t: t)
+ b = f"blk.{L}."
+ gate = g.get_tensor(b + "ffn_gate_inp.weight").float() # (512, 3072)
+ bias = g.get_tensor(b + "exp_probs_b.bias").float()
+ p = torch.sigmoid(x @ gate.T)
+ sel = torch.topk(p + bias, 16, dim=-1).indices
+ w = torch.gather(p, 1, sel)
+ w = w / w.sum(-1, keepdim=True).clamp(min=6.103515625e-5) * 3.997999906539917
+ z = qa8(x) @ load_dense(g, b + "ffn_routed_down.weight").T # (nt, 1536)
+ moe = torch.zeros(x.shape[0], 1536)
+ for e in sorted(set(sel.flatten().tolist())):
+ rows, slot = (sel == e).nonzero(as_tuple=True)
+ zi = qak(z[rows])
+ W = {n: dq_q4k(g.get_tensor(f"{b}ffn_{n}_exps.e{e}.weight"), 1536, 1536) for n in ("gate", "up", "down")}
+ ga, up = zi @ W["gate"].T, zi @ W["up"].T
+ ga = torch.minimum(ga, torch.tensor(10.0)) if clamp_pre else ga
+ h = torch.nn.functional.silu(ga) * up.clamp(-10, 10)
+ o = qak(h) @ W["down"].T
+ moe.index_add_(0, rows, o * w[rows, slot].unsqueeze(-1))
+ y = qa8(moe) @ load_dense(g, b + "ffn_routed_up.weight").T
+ sg, su = qa8(x) @ load_dense(g, b + "ffn_gate_shexp.weight").T, qa8(x) @ load_dense(g, b + "ffn_up_shexp.weight").T
+ sh = qa8(torch.nn.functional.silu(sg) * su) @ load_dense(g, b + "ffn_down_shexp.weight").T
+ return {"moe": moe, "ffn": y + sh, "sel": sel, "w": w}
+
+
+def cos(a, b):
+ a, b = a.double().reshape(a.shape[0], -1), b.double().reshape(b.shape[0], -1)
+ return float(((a * b).sum(-1) / (a.norm(dim=-1) * b.norm(dim=-1))).min())
+
+
+def call_data(prompt, call):
+ d = os.path.join(ORACLE, prompt)
+ idx = json.load(open(os.path.join(d, "index.json")))
+ ent = {(t["name"], t["layer"], t["occ"]): t for t in idx["tensors"] if t["call"] == call}
+ def get(name, layer, occ=0):
+ t = ent[(name, layer, occ)]
+ a = torch.from_numpy(np.fromfile(os.path.join(d, t["file"]), np.float32).reshape(tuple(reversed(t["shape_ggml"]))).copy())
+ return a.unsqueeze(0) if a.dim() < _NDIM.get(name, 99) else a
+ return get
+
+
+def check(prompt, call, layers=(1, 2)):
+ g, plan = gg()
+ get = call_data(prompt, call)
+ rows = []
+ for L in layers:
+ s0 = (get("engram_out", 1) if L == 1 else get("l_out", L - 1))[:NT_MAX].unsqueeze(0)
+ a, f = site(L, "attn"), site(L, "ffn")
+ post_g, comb, _ = a.mix(s0, {})
+ s1 = a.apply_(s0.clone(), get("attn_out", L)[:NT_MAX].unsqueeze(0), post_g, comb, {})
+ coll = f.mix(s1, {})[2][0]
+ x = rmsn(coll, g.get_tensor(f"blk.{L}.ffn_norm.weight").float())
+ post = f.gates(s1)[1][0] # (nt, 4)
+ pn = g.get_tensor(f"blk.{L}.post_ffw_norm.weight").float()
+ fo = ((get("l_out", L)[:NT_MAX] - s1[0]) / post.unsqueeze(-1))[:, 0] # oracle ffn output after post_ffw_norm, stream 0
+ tm = get("ffn_moe_out", L)[:NT_MAX]
+ for q8 in (True, False):
+ r = ffn_ref(g, L, x, q8)
+ ma, rm = rel(r["moe"], tm)
+ mb, rf = rel(rmsn(r["ffn"], pn), fo)
+ rows.append((f"L{L} {'q8' if q8 else 'f32'}", f"{prompt}#{call} nt={x.shape[0]}", rm, cos(r["moe"], tm), rf, cos(rmsn(r["ffn"], pn), fo),
+ "PASS" if cos(r["moe"], tm) >= COS_BAR and cos(rmsn(r["ffn"], pn), fo) >= COS_BAR else "FAIL"))
+ return rows
+
+
+CASES = [("p1_short_en", 0), ("p1_short_en", 1), ("p1_short_en", 2), ("p4_long_n700", 0), ("p4_long_n700", 2), ("p5_long_n4000", 0), ("p5_long_n4000", 8)]
+
+
+def test_registry_selects_latent_moe():
+ g, plan = gg()
+ assert plan.layers[0]["ffn"] == "dense_ffn" and all(plan.layers[i]["ffn"] == "latent_moe" for i in range(1, 42))
+ c = plan.configs[("latent_moe", 1)]
+ assert (c["latent"], c["num_experts"], c["top_k"], c["gating_func"], c["weights_norm"], c["e_score_bias"], c["clamp"]) == (1536, 512, 16, 2, True, True, 10.0)
+ assert abs(c["weights_scale"] - 3.998) < 1e-6 and c["expert_dims"] == (1536, 1536, 512) and c["shared_dims"] == (3072, 1536)
+ assert not [x for x in plan.gaps if "latent" in x.lower() or "ffn_" in x]
+
+
+def test_names_resolve():
+ from exllamav3.loader.gguf_blocks.mlp import _latent_names
+ g, plan = gg()
+ nm = _latent_names(plan.configs[("latent_moe", 1)], 1, "L", "blk.1")
+ assert len(nm) == 4 + 3 + 3 * 512
+ for k in ("L.mlp.gate.weight", "L.mlp.gate.e_score_correction_bias", "L.mlp.fc1_latent_proj.weight", "L.mlp.fc2_latent_proj.weight",
+ "L.mlp.shared_experts.up_proj.weight", "L.mlp.experts.37.down_proj.weight"):
+ assert g.has_tensor(nm[k]), k
+
+
+def test_build_kwargs():
+ """the registry build, with module stubs: latent projections, ds3 router with one group, scale 3.998, clamp 10 on the routed experts only"""
+ import types
+ from exllamav3.loader.gguf_blocks import mlp as M
+ seen = {}
+ class Cap:
+ def __init__(self, **k): self.k = k; seen.setdefault("n", []).append(k)
+ def register_submodule(self, m): pass
+ gates = ups = downs = []
+ ga = types.ModuleType("exllamav3.architecture.gguf_generic"); ga._fl = lambda cfg, key, i, o, **k: (key, i, o)
+ pk = sys.modules["exllamav3.modules"]; pk.GatedMLP = pk.BlockSparseMLP = Cap
+ sys.modules["exllamav3.architecture"] = types.ModuleType("exllamav3.architecture"); sys.modules["exllamav3.architecture"].__path__ = []
+ sys.modules["exllamav3.architecture.gguf_generic"] = ga
+ sys.modules["exllamav3.architecture"].gguf_generic = ga
+ g, plan = gg()
+ cfg = Cfg(); cfg.gguf_plan = plan
+ mod = M._latent_build(cfg, plan, 1, "L")
+ k = mod.k
+ assert k["latent_size"] == 1536 and k["router_type"] == "ds3" and k["n_group"] == k["topk_group"] == 1 and k["norm_topk"] is True
+ assert abs(k["routed_scaling_factor"] - 3.998) < 1e-6 and k["act_limit"] == 10.0 and k["num_experts_per_tok"] == 16
+ assert k["latent_in"] == ("L.mlp.fc1_latent_proj", 3072, 1536) and k["latent_out"] == ("L.mlp.fc2_latent_proj", 1536, 3072)
+ assert k["gates"][0] == ("L.mlp.experts.0.gate_proj", 1536, 1536)
+ sh = seen["n"][0]
+ assert sh["hidden_size"] == 3072 and sh["intermediate_size"] == 1536 and "act_limit" not in sh # shared expert: full width, unclamped
+
+
+def test_q4k_decoder_matches_numpy_reference():
+ from exllamav3.foreign_quant.k_quants import dequant_q4_K_bulk
+ g, _ = gg()
+ raw = g.get_tensor("blk.1.ffn_gate_exps.e3.weight")
+ a = dq_q4k(raw, 1536, 1536)[:4]
+ b = torch.from_numpy(dequant_q4_K_bulk(raw.numpy().reshape(1536, -1)[:4].reshape(-1, 144)).astype(np.float32)).reshape(4, 1536)
+ assert (a - b).abs().max() <= 2e-3 * a.abs().max() # the tree decoder returns fp16
+
+
[email protected]("prompt,call", CASES)
+def test_ffn_vs_oracle(prompt, call):
+ for row in check(prompt, call):
+ assert row[-1] == "PASS", row
+
+
+if __name__ == "__main__":
+ for p, c in CASES:
+ for r in check(p, c):
+ print(f"{r[0]} | {r[1]} | ffn_moe_out rel {r[2]:.2e} cos {r[3]:.6f} | ffn_out(l_out-derived) rel {r[4]:.2e} cos {r[5]:.6f} | {r[6]}", flush=True)