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: fused Triton decode GEMV for GGUF Q4_0, Q5_0, IQ4_NL, MXFP4 (CUDA and ROCm)

YanissAmz82da10f02a92
diff --git a/exllamav3/foreign_quant/triton_gap.py b/exllamav3/foreign_quant/triton_gap.py
new file mode 100644
index 0000000..c99edb2
--- /dev/null
+++ b/exllamav3/foreign_quant/triton_gap.py
@@ -0,0 +1,102 @@
+"""Fused decode GEMV (<= 16 rows) for the 32-element GGUF blocks Q4_0, Q5_0, IQ4_NL, MXFP4, read in their native layout.
+
+One Triton kernel for CUDA and ROCm (no extension build): per block the 16 qs bytes give elements 0..15 (low nibble)
+and 16..31 (high nibble), so the activation slices are contiguous and each half is one tl.dot. Output matches
+gap_formats.dequant + matmul up to fp32 accumulation order.
+"""
+from __future__ import annotations
+
+import torch
+import triton
+import triton.language as tl
+
+FUSED = {"q4_0": 0, "iq4_nl": 1, "mxfp4": 2, "q5_0": 3}
+_BB = {"q4_0": 18, "iq4_nl": 18, "mxfp4": 17, "q5_0": 22}
+MAX_ROWS = 16
+# (BN, num_warps) per format, from a 3090 sweep at N=12288, K=4096, 4 rows
+_CFG = {"q4_0": (32, 1), "q5_0": (32, 1), "mxfp4": (32, 1), "iq4_nl": (16, 1)}
+
+
+def _iq4v(q):
+ b0 = (q & 1) != 0
+ b1 = (q & 2) != 0
+ b2 = (q & 4) != 0
+ b3 = (q & 8) != 0
+ t0 = tl.where(b0, -104.0, -127.0); t1 = tl.where(b0, -65.0, -83.0); t2 = tl.where(b0, -35.0, -49.0); t3 = tl.where(b0, -10.0, -22.0)
+ t4 = tl.where(b0, 13.0, 1.0); t5 = tl.where(b0, 38.0, 25.0); t6 = tl.where(b0, 69.0, 53.0); t7 = tl.where(b0, 113.0, 89.0)
+ u0 = tl.where(b1, t1, t0); u1 = tl.where(b1, t3, t2); u2 = tl.where(b1, t5, t4); u3 = tl.where(b1, t7, t6)
+ return tl.where(b3, tl.where(b2, u3, u2), tl.where(b2, u1, u0))
+
+
+def _fp4half(q):
+ m = q & 7
+ v = tl.where(m < 5, m.to(tl.float32), tl.where(m == 5, 6.0, tl.where(m == 6, 8.0, 12.0)))
+ return tl.where(q >= 8, -v, v)
+
+
+def _gemv32(x_ptr, w_ptr, o_ptr, R, K, N, FMT: tl.constexpr, BB: tl.constexpr, BN: tl.constexpr, RP: tl.constexpr):
+ pid = tl.program_id(0)
+ Kb = K // 32
+ rn = pid * BN + tl.arange(0, BN)
+ rr = tl.arange(0, RP)
+ j = tl.arange(0, 16)
+ acc = tl.zeros((RP, BN), dtype=tl.float32)
+ row = rn.to(tl.int64) * (Kb * BB)
+ for kb in range(0, Kb):
+ base = row + kb * BB
+ if FMT == 2:
+ e = tl.load(w_ptr + base).to(tl.float32)
+ d = tl.exp2(e - 128.0)
+ qoff = 1
+ else:
+ b0 = tl.load(w_ptr + base).to(tl.int32)
+ b1 = tl.load(w_ptr + base + 1).to(tl.int32)
+ d = (b0 | (b1 << 8)).to(tl.int16).to(tl.float16, bitcast=True).to(tl.float32)
+ qoff = 2
+ if FMT == 3:
+ qoff = 6
+ qs = tl.load(w_ptr + base[:, None] + qoff + j[None, :]).to(tl.int32)
+ lo = qs & 15
+ hi = qs >> 4
+ if FMT == 0:
+ wl = (lo - 8).to(tl.float32)
+ wh = (hi - 8).to(tl.float32)
+ elif FMT == 1:
+ wl = _iq4v(lo)
+ wh = _iq4v(hi)
+ elif FMT == 2:
+ wl = _fp4half(lo)
+ wh = _fp4half(hi)
+ else:
+ qh = tl.load(w_ptr + base + 2).to(tl.int32) | (tl.load(w_ptr + base + 3).to(tl.int32) << 8) \
+ | (tl.load(w_ptr + base + 4).to(tl.int32) << 16) | (tl.load(w_ptr + base + 5).to(tl.int32) << 24)
+ wl = ((lo | (((qh[:, None] >> j[None, :]) & 1) << 4)) - 16).to(tl.float32)
+ wh = ((hi | (((qh[:, None] >> (j[None, :] + 16)) & 1) << 4)) - 16).to(tl.float32)
+ wl = (wl * d[:, None]).to(tl.float16)
+ wh = (wh * d[:, None]).to(tl.float16)
+ a_lo = tl.load(x_ptr + rr[:, None] * K + kb * 32 + j[None, :], mask=rr[:, None] < R, other=0.0)
+ a_hi = tl.load(x_ptr + rr[:, None] * K + kb * 32 + 16 + j[None, :], mask=rr[:, None] < R, other=0.0)
+ acc = tl.dot(a_lo, tl.trans(wl), acc)
+ acc = tl.dot(a_hi, tl.trans(wh), acc)
+ tl.store(o_ptr + rr[:, None] * N + rn[None, :], acc.to(tl.float16), mask=rr[:, None] < R)
+
+
+def supported(fmt: str, rows: int, k: int, n: int) -> bool:
+ return fmt in FUSED and 1 <= rows <= MAX_ROWS and k % 32 == 0 and n % _CFG[fmt][0] == 0
+
+
+def gemv(fmt: str, x: torch.Tensor, raw: torch.Tensor, out: torch.Tensor | None = None, BN: int | None = None, num_warps: int | None = None):
+ """x fp16 [R, K] contiguous, raw uint8 [N, K/32*bb] -> fp16 [R, N]."""
+ R, K = x.shape
+ N = raw.shape[0]
+ BN = BN or _CFG[fmt][0]
+ num_warps = num_warps or _CFG[fmt][1]
+ assert R <= MAX_ROWS and K % 32 == 0 and raw.shape[1] == K // 32 * _BB[fmt] and N % BN == 0
+ if out is None:
+ out = torch.empty((R, N), dtype=torch.float16, device=x.device)
+ RP = 16
+ _gemv32[(N // BN,)](x, raw, out, R, K, N, FUSED[fmt], _BB[fmt], BN, RP, num_warps=num_warps)
+ return out
diff --git a/exllamav3/modules/foreign_linear.py b/exllamav3/modules/foreign_linear.py
index 62d97d0..4a4ca20 100644
--- a/exllamav3/modules/foreign_linear.py
+++ b/exllamav3/modules/foreign_linear.py
@@ -24,6 +24,7 @@ import torch
from .module import Module
from ..ext import exllamav3_ext as ext
from ..foreign_quant.gap_formats import dequant as gap_dequant
+from ..foreign_quant import triton_gap
FUSED_ROWS_MAX = 16
_FUSED = os.environ.get("BOLT_FQ_FUSED", "1") != "0"
@@ -43,6 +44,8 @@ _PAIR = os.environ.get("BOLT_FQ_PAIR", "1") != "0"
_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.
+# BOLT_FQ_GAP_FUSED=0 turns the fused Triton decode kernel off (A/B).
+_GAP_FUSED = os.environ.get("BOLT_FQ_GAP_FUSED", "1") != "0"
_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
@@ -210,7 +213,14 @@ class ForeignLinear(Module):
if not flat.is_contiguous():
flat = flat.contiguous()
out: Optional[torch.Tensor] = None
- if self.format in _KQ_FORMATS:
+ if (
+ _GAP_FUSED and self.format in _GAP_FORMATS and flat.dtype == torch.half
+ and triton_gap.supported(self.format, R, K, self.out_features)
+ ):
+ out = triton_gap.gemv(self.format, flat, self.packed)
+ if out.dtype != dtype:
+ out = out.to(dtype)
+ elif self.format in _KQ_FORMATS:
# KQ path: quantize once to the cached q8 scratch, one
# row-per-block launch. Decode widths (R < 16) always run here;
# prefill widths (R >= 16) use the unfused fp16 GEMM below (one
diff --git a/tests/foreign_gap_fused_bench.py b/tests/foreign_gap_fused_bench.py
new file mode 100644
index 0000000..e20897b
--- /dev/null
+++ b/tests/foreign_gap_fused_bench.py
@@ -0,0 +1,27 @@
+import sys, types, time, importlib.util, os, torch, numpy as np
+sys.path.insert(0, "pylib")
+D = "/home/kamel/hermes-work/kyolane/src_v185/kyojin/exllamav3/foreign_quant"
+spec = importlib.util.spec_from_file_location("fq_gap", D + "/__init__.py", submodule_search_locations=[D])
+m = importlib.util.module_from_spec(spec); sys.modules["fq_gap"] = m; spec.loader.exec_module(m)
+import fq_gap.gap_formats as gf, fq_gap.triton_gap as tg
+N, K = 12288, 4096
+dev = "cuda:0"
+for fmt in ("q4_0", "iq4_nl", "mxfp4", "q5_0"):
+ qk, bb = gf.BLOCKS[fmt]
+ rng = np.random.default_rng(2)
+ 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))
+ 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 = torch.from_numpy(raw.reshape(N, -1)).to(dev)
+ w = gf.dequant(fmt, raw)
+ for R in (1, 4, 16):
+ x = torch.randn(R, K, device=dev, dtype=torch.half)
+ y = tg.gemv(fmt, x, raw); ref = x.float() @ w.float().T
+ err = ((y.float() - ref).norm() / ref.norm()).item(); assert err < 2e-3, (fmt, R, err)
+ def t(f, n=30):
+ for _ in range(5): f()
+ torch.cuda.synchronize(); a = time.perf_counter()
+ for _ in range(n): f()
+ torch.cuda.synchronize(); return (time.perf_counter() - a) / n * 1e3
+ tf = t(lambda: tg.gemv(fmt, x, raw)); tb = t(lambda: x @ gf.dequant(fmt, raw).T)
+ print(f"{fmt} R={R} err={err:.1e} fused {tf:.3f} ms ({raw.numel()/tf/1e6:.0f} GB/s) vs dequant+matmul {tb:.3f} ms")