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 loader: resolve per-expert slice names after the name map

YanissAmz9dc8893c75a4
diff --git a/exllamav3/loader/gguf_collection.py b/exllamav3/loader/gguf_collection.py
index c95c313..e0163c3 100644
--- a/exllamav3/loader/gguf_collection.py
+++ b/exllamav3/loader/gguf_collection.py
@@ -151,7 +151,14 @@ class GGUFCollection:
return self.aux.has_tensor(key)
if self.new_tensors and key in self.new_tensors:
return True
- return self._resolve(key) in self.infos
+ r = self._resolve(key)
+ if r in self.infos:
+ return True
+ if "_exps.e" in r: # generic-loader expert slice name: "<gguf3d>.e<idx>[.weight]"
+ mb = r[:-7] if r.endswith(".weight") else r
+ bb_, es_ = mb.rsplit(".e", 1)
+ return es_.isdigit() and (bb_ + ".weight") in self.infos
+ return False
# Deferred-load arena API (SafetensorsCollection): ForeignLinear and the
# fp16 paths load eagerly, so these are no-ops. Present because
@@ -241,6 +248,12 @@ class GGUFCollection:
want_T = False
if isinstance(mapped, tuple):
mapped, want_T = mapped[0], (mapped[1] == "T")
+ if exp_idx is None and mapped not in self.infos and "_exps.e" in mapped:
+ # engine key mapped by name_map to "<gguf3d>.e<idx>[.weight]" (generic loader): same slice protocol, after the map
+ mb = mapped[:-7] if mapped.endswith(".weight") else mapped
+ bb_, es_ = mb.rsplit(".e", 1)
+ if es_.isdigit() and (bb_ + ".weight") in self.infos:
+ mapped, exp_idx = bb_ + ".weight", int(es_)
info = self.infos.get(mapped)
if info is None:
if not optional:
diff --git a/tests/test_gguf_expert_slice_names.py b/tests/test_gguf_expert_slice_names.py
new file mode 100644
index 0000000..dceb81a
--- /dev/null
+++ b/tests/test_gguf_expert_slice_names.py
@@ -0,0 +1,91 @@
+"""CPU test: GGUFCollection resolves per-expert slice names (blk.N.ffn_{gate,up,down}_exps.eK.weight),
+both when passed directly and when produced by a name map."""
+import os
+import struct
+import tempfile
+
+import numpy as np
+import pytest
+import torch
+
+import importlib.util
+
+# Load the module by path: importing the exllamav3 package would build/load the CUDA extension (not needed here)
+_p = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "exllamav3", "loader", "gguf_collection.py")
+_spec = importlib.util.spec_from_file_location("gguf_collection_under_test", _p)
+_mod = importlib.util.module_from_spec(_spec)
+_spec.loader.exec_module(_mod)
+GGUFCollection = _mod.GGUFCollection
+
+K, N, E = 64, 6, 4 # K = row length (2 Q8_0 blocks of 32), N rows, E experts
+NB, BB = K // 32, 34 # Q8_0: 32 elements in 34 bytes
+ROW = NB * BB
+NAMES = ["ffn_gate_exps", "ffn_up_exps", "ffn_down_exps"]
+
+
+def _write_gguf(path, payloads):
+ def s(x):
+ b = x.encode()
+ return struct.pack("<Q", len(b)) + b
+ head = b"GGUF" + struct.pack("<IQQ", 3, len(payloads), 0)
+ infos, off = b"", 0
+ for name, data in payloads.items():
+ infos += s(name) + struct.pack("<I", 3) + struct.pack("<QQQ", K, N, E) + struct.pack("<I", 8) + struct.pack("<Q", off)
+ off += (len(data) + 31) // 32 * 32
+ blob = head + infos
+ blob += b"\0" * (-len(blob) % 32)
+ for data in payloads.values():
+ blob += data + b"\0" * (-len(data) % 32)
+ with open(path, "wb") as f:
+ f.write(blob)
+
+
[email protected](scope="module")
+def gguf():
+ rng = np.random.default_rng(0)
+ raw = {f"blk.0.{n}.weight": rng.integers(0, 256, E * N * ROW, dtype=np.uint8) for n in NAMES}
+ with tempfile.TemporaryDirectory() as d:
+ p = os.path.join(d, "t.gguf")
+ _write_gguf(p, {k: v.tobytes() for k, v in raw.items()})
+ yield GGUFCollection(p), raw
+
+
+def _expect(raw, name, e):
+ return raw[f"blk.0.{name}.weight"].reshape(E, N, ROW)[e]
+
+
[email protected]("name", NAMES)
[email protected]("e", [0, 3])
+def test_direct_slice_name(gguf, name, e):
+ c, raw = gguf
+ key = f"blk.0.{name}.e{e}.weight"
+ assert c.has_tensor(key)
+ t = c.get_tensor(key)
+ assert t.shape == (N, ROW)
+ assert np.array_equal(t.numpy(), _expect(raw, name, e))
+
+
[email protected]("name", NAMES)
[email protected]("e", [0, 3])
+def test_slice_name_after_name_map(gguf, name, e):
+ c, raw = gguf
+ ek = f"model.layers.0.mlp.experts.{e}.{name}.weight"
+ c.name_map = {ek: f"blk.0.{name}.e{e}.weight",
+ ek + ".nosfx": f"blk.0.{name}.e{e}"}
+ try:
+ assert c.has_tensor(ek)
+ assert np.array_equal(c.get_tensor(ek).numpy(), _expect(raw, name, e))
+ assert c.has_tensor(ek + ".nosfx")
+ assert np.array_equal(c.get_tensor(ek + ".nosfx").numpy(), _expect(raw, name, e))
+ finally:
+ c.name_map = {}
+
+
+def test_missing_slice_not_found(gguf):
+ c, _ = gguf
+ c.name_map = {"x": "blk.0.ffn_gate_exps.eX.weight", "y": "blk.0.nope_exps.e1.weight"}
+ try:
+ assert not c.has_tensor("x")
+ assert not c.has_tensor("y")
+ finally:
+ c.name_map = {}