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.

ForeignLinear and GGUF collection: serve Q4_0, Q5_0, IQ4_NL, IQ3_XXS, IQ3_S, MXFP4, NVFP4 blocks (dequant + matmul)

YanissAmza967d5209692
diff --git a/exllamav3/loader/gguf_collection.py b/exllamav3/loader/gguf_collection.py
index ffca8e6..e4ff6a2 100644
--- a/exllamav3/loader/gguf_collection.py
+++ b/exllamav3/loader/gguf_collection.py
@@ -26,7 +26,11 @@ GGML_BBS = {
14: 210, # Q6_K
23: 136, # IQ4_XS
}
-GGML_QUANT = set(GGML_BBS.keys())
+# (elements, bytes) per block of the formats served by gap_formats / fq_gemv_q8_0 (32- and 64-element blocks)
+GGML_BLK = {t: (256, bb) for t, bb in GGML_BBS.items()}
+GGML_BLK.update({2: (32, 18), 6: (32, 22), 8: (32, 34), 20: (32, 18), 18: (256, 98), 21: (256, 110),
+ 39: (32, 17), 40: (64, 36)})
+GGML_QUANT = set(GGML_BLK.keys())
_META_SZ = {0: 1, 1: 1, 2: 2, 3: 2, 4: 4, 5: 4, 6: 4, 7: 1, 10: 8, 11: 8, 12: 8}
@@ -203,10 +207,10 @@ class GGUFCollection:
ty, dims, off = info
dev = device if device is not None else torch.device("cpu")
if ty in GGML_QUANT:
- bb = GGML_BBS[ty]
+ qk, bb = GGML_BLK[ty]
K, N = dims[0], dims[1]
- assert K % 256 == 0
- nb = K // 256
+ assert K % qk == 0
+ nb = K // qk
if exp_idx is not None:
assert len(dims) == 3
E = dims[2]
diff --git a/exllamav3/modules/foreign_linear.py b/exllamav3/modules/foreign_linear.py
index c4ec2f2..62d97d0 100644
--- a/exllamav3/modules/foreign_linear.py
+++ b/exllamav3/modules/foreign_linear.py
@@ -23,6 +23,7 @@ import torch
from .module import Module
from ..ext import exllamav3_ext as ext
+from ..foreign_quant.gap_formats import dequant as gap_dequant
FUSED_ROWS_MAX = 16
_FUSED = os.environ.get("BOLT_FQ_FUSED", "1") != "0"
@@ -39,7 +40,10 @@ _UNFUSED_ROWS = int(os.environ.get("BOLT_FQ_UNFUSED_ROWS", "8192"))
# restores the direct dot for A/B.
_PAIR = os.environ.get("BOLT_FQ_PAIR", "1") != "0"
-_FORMATS = ("q8_0", "iq2_xxs", "nvfp4", "q4_k", "q5_k", "q6_k", "iq4_xs")
+_FORMATS = ("q8_0", "iq2_xxs", "nvfp4", "q4_k", "q5_k", "q6_k", "iq4_xs",
+ "q4_0", "q5_0", "iq4_nl", "iq3_xxs", "iq3_s", "mxfp4", "nvfp4_gguf")
+# GGUF blocks without a fused kernel yet: torch dequant (foreign_quant/gap_formats.py) + matmul at every width.
+_GAP_FORMATS = ("q4_0", "q5_0", "iq4_nl", "iq3_xxs", "iq3_s", "mxfp4", "nvfp4_gguf")
# GGUF K-quant formats: decode (rows < 16) via the q8_1-
# activation row kernels; prefill (rows >= 16) via the unfused fp16 dequant
# + cuBLAS GEMM path, one GEMM per call -- i.e. one GEMM per
@@ -132,7 +136,7 @@ class ForeignLinear(Module):
def load(self, device: torch.device, **kwargs):
self.device = device
stc = self.config.stc
- if self.format in ("q8_0", "iq2_xxs") or self.format in _KQ_FORMATS:
+ if self.format in ("q8_0", "iq2_xxs") or self.format in _KQ_FORMATS or self.format in _GAP_FORMATS:
# Single raw payload (KQ GGUF bytes / Q8_0 / IQ2_XXS blocks).
self.packed = stc.get_tensor(self.key + ".weight", device, no_defer=True)
assert self.packed.dtype == torch.uint8, f"{self.key}: expected U8 payload, got {self.packed.dtype}"
@@ -161,6 +165,8 @@ class ForeignLinear(Module):
N = self.out_features
assert 0 <= first and first + count <= N
dev = self.packed.device
+ if self.format in _GAP_FORMATS:
+ return gap_dequant(self.format, self.packed[first:first + count])
if self.format in _KQ_FORMATS:
Kb = self.in_features // 256
bb = _KQ_BLOCK_BYTES[self.format]
@@ -247,6 +253,7 @@ class ForeignLinear(Module):
ext_fn(qb, self.packed, out[_s:_e])
elif (
_FUSED and R >= 1 and R <= FUSED_ROWS_MAX and flat.is_contiguous()
+ and self.format not in _GAP_FORMATS
and flat.dtype in (torch.half, torch.float)
and hasattr(ext, "fq_gemv_q8_0")
and _GEMV
@@ -262,6 +269,7 @@ class ForeignLinear(Module):
self.scales.reshape(N, K // 16), self.global_scale, out, _PAIR)
elif (
_FUSED and R >= 1 and R <= FUSED_ROWS_MAX and flat.dtype == torch.half and flat.is_contiguous()
+ and self.format not in _GAP_FORMATS
and hasattr(ext, "fq_gemm_q8_0")
):
N = self.out_features
diff --git a/tests/foreign_gap_linear_gpu_check.py b/tests/foreign_gap_linear_gpu_check.py
new file mode 100644
index 0000000..132945d
--- /dev/null
+++ b/tests/foreign_gap_linear_gpu_check.py
@@ -0,0 +1,30 @@
+"""GPU: ForeignLinear over the GGUF gap formats (dequant + matmul) vs the gguf-py dequant, R = 1, 4, 40. Needs the built extension and the gguf package."""
+import sys, types, torch
+sys.path.insert(0, __import__("os").path.dirname(__import__("os").path.dirname(__import__("os").path.abspath(__file__))))
+import numpy as np, gguf
+from gguf import GGMLQuantizationType as T
+from exllamav3.modules.foreign_linear import ForeignLinear
+N, K = 192, 512
+names = {"q4_0": T.Q4_0, "q5_0": T.Q5_0, "iq4_nl": T.IQ4_NL, "iq3_xxs": T.IQ3_XXS, "iq3_s": T.IQ3_S, "mxfp4": T.MXFP4, "nvfp4_gguf": T.NVFP4}
+dev = torch.device("cuda:0")
+class STC:
+ def __init__(s, raw): s.raw = raw
+ def get_tensor(s, key, device, **kw): return s.raw.to(device)
+for fmt, ty in names.items():
+ rng = np.random.default_rng(1)
+ qk, bb = {2:(32,18),6:(32,22),20:(32,18),18:(256,98),21:(256,110),39:(32,17),40:(64,36)}[ty.value]
+ raw = rng.integers(0, 256, size=(N, K // qk, bb), dtype=np.uint8)
+ if fmt == "mxfp4": raw[:, :, 0] = rng.integers(120, 135, size=(N, K // qk))
+ elif fmt == "nvfp4_gguf": raw[:, :, :4] = rng.integers(40, 90, size=(N, K // qk, 4))
+ else: raw[:, :, :2] = rng.uniform(0.001, 0.05, size=(N, K // qk)).astype(np.float16).view(np.uint8).reshape(N, K // qk, 2)
+ raw = raw.reshape(N, -1)
+ ref_w = torch.from_numpy(gguf.quants.dequantize(raw, ty)).to(dev).half()
+ cfg = types.SimpleNamespace(stc=STC(torch.from_numpy(raw)))
+ lin = ForeignLinear(cfg, "blk.0.ffn", K, N, fmt); lin.load(dev)
+ for R in (1, 4, 40):
+ x = torch.randn(R, K, device=dev, dtype=torch.half)
+ y = lin.forward(x, {})
+ ref = x.float() @ ref_w.float().T
+ err = ((y.float() - ref).norm() / ref.norm()).item()
+ assert err < 2e-3, (fmt, R, err)
+ print(fmt, "ok", err)