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 HY4_MOE_V2: Q3_K gate/up rows need 2-byte alignment, not 16 (22 of 42 MoE layers of the Q3_K GGUF were refused as stride_align); draft indexer rows above the mat-vec limit are counted as rows>maxt

YanissAmz2b0a0f0482da
diff --git a/exllamav3/loader/hy4_forward.py b/exllamav3/loader/hy4_forward.py
index 80e47f4..cd86276 100644
--- a/exllamav3/loader/hy4_forward.py
+++ b/exllamav3/loader/hy4_forward.py
@@ -207,6 +207,20 @@ _MD = []
MOE_V2_FIRES = _LVF.Fires("moe_v2", hit="calls") # calls = layer calls (graph capture / eager sites) that took the code object; fallback = {reason: n} of the calls that did not (only counted with the flag on)
+def _moe_v2_align(rs, fmt):
+ """None when the row strides and the base pointers meet the code object contract, else "stride_align" / "ptr_align". Q4_K blocks are 144 B and read as uint4: 16-byte rows and bases
+ for gate, up and down. Q3_K gate / up blocks are 110 B (2-byte aligned: q3_load aligns the dword read itself, moe_dec.cu), so 2-byte rows and bases are enough; down is Q4_K
+ (fd == 0, 16-byte). 22 of the 42 MoE layers of the Q3_K GGUF are Q3_K gate / up (row 660 B): the old rule (16 B for every stream) sent all of them to the stock kernels."""
+ ga = 16 if fmt == "q4_k" else 2
+ if rs.strides[0] % ga or rs.strides[1] % ga or rs.strides[2] % 16:
+ return "stride_align"
+ for n, a in (("gate", ga), ("up", ga), ("down", 16)):
+ b = rs.bufs.get(n)
+ if b is not None and b.data_ptr() % a:
+ return "ptr_align"
+ return None
+
+
def _moe_v2(rs, T, K, S=16):
"""the MoeDec scratch holder for this layer shape, or None (flag off / code object missing / shape or alignment outside the kernels' contract)"""
if not MOE_V2:
@@ -228,8 +242,7 @@ def _moe_v2(rs, T, K, S=16):
md, cache = _MD[0], _MD[1]
fmt = {0: "q4_k", 8: "q3_k"}.get(rs.fu)
why = ("no_code_object" if md is None else "gate_up_fmt" if fmt is None else "down_fmt" if rs.fd != 0 else "nout!=K" if rs.nout != K
- else "shape" if (K % 256 or rs.nff % 256 or K != 1536 or rs.nff != 1536) else "stride_align" if any(st % 16 for st in rs.strides)
- else "ptr_align" if (rs.bufs["gate"].data_ptr() % 16) else None)
+ else "shape" if (K % 256 or rs.nff % 256 or K != 1536 or rs.nff != 1536) else _moe_v2_align(rs, fmt))
if why is not None:
MOE_V2_FIRES.miss(why)
return None
diff --git a/exllamav3/loader/step_graph.py b/exllamav3/loader/step_graph.py
index 9326e7a..2c8f09a 100644
--- a/exllamav3/loader/step_graph.py
+++ b/exllamav3/loader/step_graph.py
@@ -96,7 +96,7 @@ def _mmw(ix, a, name):
if gv and _RG.usable(a, w):
IX_GV_FIRES.hit()
return _RG.logits(a, w)
- IX_GV_FIRES.miss("flag_off" if not gv else "unusable_shape")
+ IX_GV_FIRES.miss("flag_off" if not gv else "rows>maxt" if (a.dim() == 2 and a.shape[0] > _RG.MAXT) else "unusable_shape") # rows>maxt: draft-cache fill / prefill chunks (natural); unusable_shape: any other refusal
return a @ w.T
diff --git a/tests/test_hy4_seta_levers_cpu.py b/tests/test_hy4_seta_levers_cpu.py
index 5e089a1..3820343 100644
--- a/tests/test_hy4_seta_levers_cpu.py
+++ b/tests/test_hy4_seta_levers_cpu.py
@@ -191,7 +191,9 @@ def test_moe_v2_happy_path(moe):
((1, 1536, 16), {"nout": 1024}, "nout!=K"),
((1, 1024, 16), {"nout": 1024}, "shape"),
((1, 1536, 16), {"nff": 2048}, "shape"),
- ((1, 1536, 16), {"strides": (1536, 2050, 4096)}, "stride_align"),
+ ((1, 1536, 16), {"strides": (1536, 2051, 4096)}, "stride_align"), # Q3_K gate / up need 2-byte rows: an odd row is refused
+ ((1, 1536, 16), {"strides": (1536, 2048, 4098)}, "stride_align"), # down is Q4_K: 16-byte rows
+ ((1, 1536, 16), {"fu": 0, "strides": (1536, 2050, 4096)}, "stride_align"), # Q4_K gate / up: 16-byte rows
])
def test_moe_v2_fallback_reasons(moe, args, rs, reason):
T, K, S = args
@@ -202,7 +204,7 @@ def test_moe_v2_fallback_reasons(moe, args, rs, reason):
def test_moe_v2_pointer_alignment_and_missing_code_object(moe, monkeypatch):
t = torch.zeros(64)
odd = t[1:] # 4-byte offset: not 16-byte aligned
- assert moe._moe_v2(_rs(bufs={"gate": odd}), 1, 1536) is None
+ assert moe._moe_v2(_rs(fu=0, bufs={"gate": odd}), 1, 1536) is None # Q4_K gate: uint4 loads need 16 bytes
monkeypatch.setattr(moe, "_MD", [None, {}])
assert moe._moe_v2(_rs(), 1, 1536) is None
assert moe.MOE_V2_FIRES.snapshot()["fallback"] == {"ptr_align": 1, "no_code_object": 1}
@@ -266,11 +268,14 @@ def test_mmw_with_gv_takes_the_matvec(mods, monkeypatch):
SG = mods.step_graph
a, w = torch.randn(1, 16), torch.randn(8, 16)
ix = SimpleNamespace(w={"x": w}, gv=True)
- monkeypatch.setattr(SG, "_RG", SimpleNamespace(usable=lambda a, w: True, logits=lambda a, w: "mv"))
+ monkeypatch.setattr(SG, "_RG", SimpleNamespace(usable=lambda a, w: True, logits=lambda a, w: "mv", MAXT=8))
assert SG._mmw(ix, a, "x") == "mv"
- monkeypatch.setattr(SG, "_RG", SimpleNamespace(usable=lambda a, w: False, logits=lambda a, w: "mv"))
+ monkeypatch.setattr(SG, "_RG", SimpleNamespace(usable=lambda a, w: False, logits=lambda a, w: "mv", MAXT=8))
assert torch.equal(SG._mmw(ix, a, "x"), a @ w.T)
assert SG.IX_GV_FIRES.snapshot() == {"gv": 1, "gemm": {"unusable_shape": 1}}
+ a9 = torch.randn(9, 16) # more rows than the mat-vec takes (draft-cache fill chunks): counted as the natural reason
+ assert torch.equal(SG._mmw(ix, a9, "x"), a9 @ w.T)
+ assert SG.IX_GV_FIRES.snapshot() == {"gv": 1, "gemm": {"unusable_shape": 1, "rows>maxt": 1}}
def test_indexer_keys_static_keeps_no_grad(mods):
@@ -291,3 +296,18 @@ def test_fires_line(mods):
mods.moe_i8.FIRES.hit()
ln = LF.line()
assert "FIRES dense_v2 {'v2': 1, 'stock': {'rows<2': 1}}" in ln and "FIRES draft_moe_tiles {'calls': 1, 'fallback': {}}" in ln
+
+
+# ---- MOE_V2 alignment contract (22 of 42 MoE layers of the Q3_K GGUF have 660-byte Q3_K gate / up rows) ------------------------------------------------------------------
+
+def test_moe_v2_q3k_660_byte_rows_take_the_code_object(moe):
+ strides = (660 * 1, 660, 864) # real Q3_K gate / up (K 1536: 6 blocks x 110 B) with Q4_K down (6 x 144 B)
+ assert 660 % 16 != 0 and 864 % 16 == 0
+ assert moe._moe_v2(_rs(fu=8, strides=strides, bufs={"gate": torch.zeros(64), "up": torch.zeros(64), "down": torch.zeros(64)}), 1, 1536) is not None
+ assert moe.MOE_V2_FIRES.snapshot() == {"calls": 1, "fallback": {}}
+
+
+def test_moe_v2_q4k_layers_keep_the_16_byte_rule(moe):
+ assert moe._moe_v2(_rs(fu=0, strides=(864, 864, 864)), 1, 1536) is not None
+ assert moe._moe_v2(_rs(fu=0, strides=(660, 864, 864)), 1, 1536) is None
+ assert moe.MOE_V2_FIRES.snapshot() == {"calls": 1, "fallback": {"stride_align": 1}}