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 fast paths: one switch, EXL3_GGUF_FAST=1 (default off)

YanissAmzc8b220a5493b
diff --git a/exllamav3/foreign_quant/gguf_fast.py b/exllamav3/foreign_quant/gguf_fast.py
new file mode 100644
index 0000000..0da9aa0
--- /dev/null
+++ b/exllamav3/foreign_quant/gguf_fast.py
@@ -0,0 +1,29 @@
+"""EXL3_GGUF_FAST: one switch for the generic-GGUF fast paths (default off).
+
+EXL3_GGUF_FAST=1 turns on, for every flag below that is not set explicitly:
+ EXL3_FQ_MMVQ_LEGACY, EXL3_MXFP4_MOE_TUNED, EXL3_FQ_MMVQ_MXFP4, EXL3_MXFP4_PREFILL_GROUPED, EXL3_FQ_MMVQ_EPI, EXL3_LEGACY_PREFILL_DQ.
+EXL3_MOE_FOLD_FUSED is NOT part of it (3.6 % < 4 %, stays opt-in). A flag that is set in the environment keeps its own value
+(EXL3_FQ_MMVQ_MXFP4=0 wins over EXL3_GGUF_FAST=1). Every reader calls flag() at call time.
+
+All six paths act on ForeignLinear / foreign MXFP4 expert weights, i.e. on weights kept in their stored GGUF format. This tree has no generic
+GGUF loader (exllamav3/architecture/gguf_generic.py is not in it), so there is no per-model attribute to key on: the switch is the env alone."""
+from __future__ import annotations
+
+import os
+
+FAST_FLAGS = frozenset({
+ "EXL3_FQ_MMVQ_LEGACY",
+ "EXL3_MXFP4_MOE_TUNED",
+ "EXL3_FQ_MMVQ_MXFP4",
+ "EXL3_MXFP4_PREFILL_GROUPED",
+ "EXL3_FQ_MMVQ_EPI",
+ "EXL3_LEGACY_PREFILL_DQ",
+})
+
+
+def flag(name: str) -> bool:
+ """True if `name` is on: set explicitly -> its own value == "1"; unset or empty -> EXL3_GGUF_FAST=1 and name in FAST_FLAGS."""
+ v = os.environ.get(name)
+ if v:
+ return v == "1"
+ return name in FAST_FLAGS and os.environ.get("EXL3_GGUF_FAST", "0") == "1"
diff --git a/exllamav3/foreign_quant/moe_mxfp4_prefill.py b/exllamav3/foreign_quant/moe_mxfp4_prefill.py
index 1ea4749..66ebd42 100644
--- a/exllamav3/foreign_quant/moe_mxfp4_prefill.py
+++ b/exllamav3/foreign_quant/moe_mxfp4_prefill.py
@@ -22,6 +22,8 @@ import os
import numpy as np
import torch
+from .gguf_fast import flag
+
try:
import triton
import triton.language as tl
@@ -36,7 +38,7 @@ FIRES = 0 # python
def flag_on() -> bool:
"""EXL3_MXFP4_PREFILL_GROUPED=1 on CUDA (not HIP). Read at call time."""
- return os.environ.get("EXL3_MXFP4_PREFILL_GROUPED", "0") == "1" and bool(torch.version.cuda) and not torch.version.hip
+ return flag("EXL3_MXFP4_PREFILL_GROUPED") and bool(torch.version.cuda) and not torch.version.hip
def use_prefill(rows: int, decode_max_rows: int) -> bool:
diff --git a/exllamav3/foreign_quant/triton_moe_mxfp4.py b/exllamav3/foreign_quant/triton_moe_mxfp4.py
index c3ec62e..6a0b162 100644
--- a/exllamav3/foreign_quant/triton_moe_mxfp4.py
+++ b/exllamav3/foreign_quant/triton_moe_mxfp4.py
@@ -12,6 +12,7 @@ import torch
import triton
import triton.language as tl
+from .gguf_fast import flag
from .triton_gap import _fp4half
# Strix sweep 11/11: BN 16 101 -> BN 32 115 GB/s, served gpt-oss 24.4 -> 25.7 tok/s
@@ -51,7 +52,7 @@ MMVQ_FIRES = 0 # python-level count of EXL3_FQ_MMVQ_MXFP4 branch calls (eage
def mmvq_mxfp4() -> bool:
"""EXL3_FQ_MMVQ_MXFP4=1 on CUDA (not HIP): llama.cpp-style MXFP4 mmvq decode MoE (fq_mxfp4_moe_gu / fq_mxfp4_moe_down). Read at call time."""
- return os.environ.get("EXL3_FQ_MMVQ_MXFP4", "0") == "1" and bool(torch.version.cuda) and not torch.version.hip
+ return flag("EXL3_FQ_MMVQ_MXFP4") and bool(torch.version.cuda) and not torch.version.hip
def moe_gemv(x: torch.Tensor, wtab: torch.Tensor, btab: torch.Tensor | None, sel: torch.Tensor, N: int, K: int, xdiv: int) -> torch.Tensor:
@@ -66,7 +67,7 @@ def moe_gemv(x: torch.Tensor, wtab: torch.Tensor, btab: torch.Tensor | None, sel
def tuned() -> bool:
"""EXL3_MXFP4_MOE_TUNED=1 on CUDA (not HIP): fused gate+up launch + BN16/NW4 grids. Read at call time."""
- return os.environ.get("EXL3_MXFP4_MOE_TUNED", "0") == "1" and not torch.version.hip
+ return flag("EXL3_MXFP4_MOE_TUNED") and not torch.version.hip
TUNED_BN = 16
diff --git a/exllamav3/modules/foreign_linear.py b/exllamav3/modules/foreign_linear.py
index 24029d7..f92f865 100644
--- a/exllamav3/modules/foreign_linear.py
+++ b/exllamav3/modules/foreign_linear.py
@@ -25,6 +25,7 @@ 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_iq
+from ..foreign_quant.gguf_fast import flag
from ..foreign_quant import triton_gap
from ..foreign_quant import triton_rows
from ..util.row_inv import GGUF_ROW_INV, GGUF_ROW_INV_MAX_ROWS, gguf_ri
@@ -102,16 +103,21 @@ _MMVQ_QK = {"iq4_nl": 32}
# EXL3_FQ_MMVQ_LEGACY (default 0): Q4_0 / Q4_1 / Q5_0 / Q5_1 single decode row through the mmvq kernel (ggml vec_dot_q*_q8_1 port, q8_1 activations) instead
# of the Triton triton_gap.gemv (~37 GB/s on gpt-oss-20b UD-Q4_K_XL attention q/k/v). Off = unchanged. 2+ rows stay on the Triton path.
_LEGACY_FMTS = ("q4_0", "q4_1", "q5_0", "q5_1")
-_MMVQ_LEGACY = os.environ.get("EXL3_FQ_MMVQ_LEGACY", "0") == "1" and bool(torch.version.cuda) and not torch.version.hip
-if _MMVQ_LEGACY:
- _MMVQ_FN.update({f: "fq_mmvq_" + f for f in _LEGACY_FMTS})
- _MMVQ_QK.update({f: 32 for f in _LEGACY_FMTS})
+_MMVQ_FN.update({f: "fq_mmvq_" + f for f in _LEGACY_FMTS}) # the legacy entries are always present; _mmvq_legacy_on() gates their use (call time)
+_MMVQ_QK.update({f: 32 for f in _LEGACY_FMTS})
+
+
+def _mmvq_legacy_on() -> bool:
+ return flag("EXL3_FQ_MMVQ_LEGACY") and bool(torch.version.cuda) and not torch.version.hip
+
+
+_MMVQ_LEGACY = _mmvq_legacy_on() # import-time snapshot, kept for the tests; the dispatch reads _mmvq_legacy_on()
# EXL3_FQ_MMVQ_EPI (default 0, CUDA only, read at call time): (A1) a ForeignLinear bias (_ForeignInner) is added inside the mmvq store (half out: round the dot to
# half, then half(float(h) + float(b)), torch's `y + bias` order; float out: v + float(b)) instead of a separate torch add; (A2) q, k and v of one attention
# layer read the same normed x: it is quantized to q8_1 once (attn.project_qkv brackets the three calls with q8_share_begin / q8_share_end; the key is
# x.data_ptr + x._version + R + K + device, valid only inside that bracket). Off = exactly the previous code.
def _epi_on() -> bool:
- return os.environ.get("EXL3_FQ_MMVQ_EPI", "0") == "1" and bool(torch.version.cuda) and not torch.version.hip
+ return flag("EXL3_FQ_MMVQ_EPI") and bool(torch.version.cuda) and not torch.version.hip
_Q8S = {"on": False, "key": None}
@@ -130,7 +136,7 @@ def q8_share_end() -> None:
# EXL3_LEGACY_PREFILL_DQ (default 0, CUDA only, read at call time): Q4_0 / Q5_0 weights at rows > 1 dequantize with ONE Triton launch into a per-call fp16 tensor
# (freed after the matmul, nothing resident) instead of the ~15 torch ops of gap_formats.dequant. Output bit-identical. Q5_1 already has its own Triton dequant.
def _legacy_dq_on() -> bool:
- return os.environ.get("EXL3_LEGACY_PREFILL_DQ", "0") == "1" and bool(torch.version.cuda) and not torch.version.hip
+ return flag("EXL3_LEGACY_PREFILL_DQ") and bool(torch.version.cuda) and not torch.version.hip
_DEQ_IQ_ID = {"iq2_xs": 1, "iq2_s": 2, "iq3_xxs": 3, "iq3_s": 4, "iq4_nl": 5}
@@ -339,7 +345,7 @@ class ForeignLinear(Module):
out = None
if out is not None:
pass
- elif (_MMVQ_ON and self.format in _MMVQ_FN and flat.dtype == torch.half and (R == 1 or self.format not in _LEGACY_FMTS) and R <= (min(_MMVQ_MAXR, FUSED_ROWS_MAX - 1) if self.format in ("q5_k", "q4_k", "q6_k", "q3_k") else _MMVQ_MAXR) and K % _MMVQ_QK.get(self.format, 256) == 0
+ elif (_MMVQ_ON and self.format in _MMVQ_FN and flat.dtype == torch.half and (self.format not in _LEGACY_FMTS or (R == 1 and _mmvq_legacy_on())) and R <= (min(_MMVQ_MAXR, FUSED_ROWS_MAX - 1) if self.format in ("q5_k", "q4_k", "q6_k", "q3_k") else _MMVQ_MAXR) and K % _MMVQ_QK.get(self.format, 256) == 0
and hasattr(ext, _MMVQ_FN[self.format])):
N = self.out_features
out = torch.empty((R, N), dtype=dtype if dtype in (torch.half, torch.float) else torch.half, device=dev)
diff --git a/tests/test_fq_mmvq_epi_cpu.py b/tests/test_fq_mmvq_epi_cpu.py
index 3059527..1775c2a 100644
--- a/tests/test_fq_mmvq_epi_cpu.py
+++ b/tests/test_fq_mmvq_epi_cpu.py
@@ -90,7 +90,7 @@ def test_dispatch(flag, monkeypatch):
fq_mmvq_epi_abi=lambda: 1)
ext.fq_mmvq_q5_0 = lambda qb, p, out, bias=None: (out.zero_(), log.append(("mmvq", bias is not None)))[1]
monkeypatch.setattr(fl, "ext", ext)
- monkeypatch.setattr(fl, "_MMVQ_LEGACY", True)
+ monkeypatch.setenv("EXL3_FQ_MMVQ_LEGACY", "1") # read at call time
monkeypatch.setitem(fl._MMVQ_FN, "q5_0", "fq_mmvq_q5_0")
monkeypatch.setitem(fl._MMVQ_QK, "q5_0", 32)
FB = _FakeCudaBias(8)
diff --git a/tests/test_gguf_fast_cpu.py b/tests/test_gguf_fast_cpu.py
new file mode 100644
index 0000000..8275df8
--- /dev/null
+++ b/tests/test_gguf_fast_cpu.py
@@ -0,0 +1,78 @@
+"""CPU checks for EXL3_GGUF_FAST (exllamav3/foreign_quant/gguf_fast.py): flag resolution, explicit values win, EXL3_MOE_FOLD_FUSED excluded,
+and every reader of the six flags goes through flag() (no direct os.environ read left). No GPU, no extension: gguf_fast is loaded by path."""
+import importlib.util
+import os
+import re
+
+import pytest
+
+ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
+PKG = os.path.join(ROOT, "exllamav3")
+FAST = ("EXL3_FQ_MMVQ_LEGACY", "EXL3_MXFP4_MOE_TUNED", "EXL3_FQ_MMVQ_MXFP4", "EXL3_MXFP4_PREFILL_GROUPED", "EXL3_FQ_MMVQ_EPI", "EXL3_LEGACY_PREFILL_DQ")
+READERS = ("foreign_quant/moe_mxfp4_prefill.py", "foreign_quant/triton_moe_mxfp4.py", "modules/foreign_linear.py")
+
+
+def _gf():
+ spec = importlib.util.spec_from_file_location("gguf_fast_t", os.path.join(PKG, "foreign_quant", "gguf_fast.py"))
+ m = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(m)
+ return m
+
+
+def clean(monkeypatch):
+ for k in FAST + ("EXL3_GGUF_FAST", "EXL3_MOE_FOLD_FUSED", "SOME_OTHER_FLAG"):
+ monkeypatch.delenv(k, raising=False)
+ return monkeypatch
+
+
+def test_default_off(clean):
+ gf = _gf()
+ assert gf.FAST_FLAGS == set(FAST)
+ assert not any(gf.flag(k) for k in FAST)
+
+
+def test_switch_turns_on_exactly_the_six(clean):
+ gf = _gf()
+ clean.setenv("EXL3_GGUF_FAST", "1")
+ assert all(gf.flag(k) for k in FAST)
+ assert not gf.flag("EXL3_MOE_FOLD_FUSED"), "fold stays opt-in"
+ assert not gf.flag("SOME_OTHER_FLAG")
+
+
[email protected]("fast", ["0", "", "2", "true"])
+def test_switch_only_on_exactly_1(clean, fast):
+ gf = _gf()
+ clean.setenv("EXL3_GGUF_FAST", fast)
+ assert not any(gf.flag(k) for k in FAST)
+
+
[email protected]("name", FAST)
+def test_explicit_zero_wins(clean, name):
+ gf = _gf()
+ clean.setenv("EXL3_GGUF_FAST", "1")
+ clean.setenv(name, "0")
+ assert not gf.flag(name)
+ assert all(gf.flag(k) for k in FAST if k != name), "the other five stay on"
+
+
[email protected]("name", FAST)
+def test_explicit_one_without_switch(clean, name):
+ gf = _gf()
+ clean.setenv(name, "1")
+ assert gf.flag(name)
+ assert not any(gf.flag(k) for k in FAST if k != name)
+
+
+def test_readers_use_flag():
+ for rel in READERS:
+ src = open(os.path.join(PKG, rel)).read()
+ for k in FAST:
+ if k in src:
+ assert not re.search(r'environ\.get\(\s*"%s"' % k, src), (rel, k, "direct env read")
+ assert "from .gguf_fast import flag" in src or "from ..foreign_quant.gguf_fast import flag" in src, rel
+ n = sum(len(re.findall(r'flag\("(%s)"\)' % "|".join(FAST), open(os.path.join(PKG, r)).read())) for r in READERS)
+ assert n >= 6, n
+ fl = open(os.path.join(PKG, "modules/foreign_linear.py")).read()
+ for k in ("EXL3_FQ_MMVQ_LEGACY", "EXL3_FQ_MMVQ_EPI", "EXL3_LEGACY_PREFILL_DQ"):
+ assert 'flag("%s")' % k in fl, k