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.

Foreign formats: torch dequant for GGUF Q4_0, Q5_0, IQ4_NL, IQ3_XXS, IQ3_S, checked against gguf-py

YanissAmz54677dc52dff
diff --git a/exllamav3/foreign_quant/gap_formats.py b/exllamav3/foreign_quant/gap_formats.py
new file mode 100644
index 0000000..c519d7f
--- /dev/null
+++ b/exllamav3/foreign_quant/gap_formats.py
@@ -0,0 +1,92 @@
+"""Torch dequant of the ggml block formats the CUDA/HIP fq_* kernels do not cover: Q4_0, Q5_0, IQ4_NL, IQ3_XXS, IQ3_S.
+
+Input is the raw GGUF payload as uint8 [N, nb * block_bytes] (row-major GGUF order, K along the row, same shape the
+GGUFCollection serves for the K-quants); output is fp16 [N, K]. Layouts follow ggml-quants.h and are checked
+against the gguf-py reference in tests/test_foreign_gap_formats_cpu.py. Runs on any torch device.
+"""
+from __future__ import annotations
+
+import torch
+
+from .gguf_tables import IQ3_S_GRID, IQ3_XXS_GRID, IQ4_NL_VALUES, KSIGNS
+
+# name -> (elements per block, bytes per block)
+BLOCKS = {"q4_0": (32, 18), "q5_0": (32, 22), "iq4_nl": (32, 18), "iq3_xxs": (256, 98), "iq3_s": (256, 110)}
+GGML_TYPE_IDS = {"q4_0": 2, "q5_0": 6, "iq3_xxs": 18, "iq4_nl": 20, "iq3_s": 21}
+
+_TABLES: dict = {}
+
+
+def _table(name: str, device) -> torch.Tensor:
+ key = (name, str(device))
+ if key not in _TABLES:
+ if name == "iq4_nl":
+ t = torch.tensor(IQ4_NL_VALUES, dtype=torch.float32)
+ elif name == "iq3_xxs":
+ t = torch.frombuffer(bytearray(IQ3_XXS_GRID), dtype=torch.uint8).reshape(256, 4).float()
+ elif name == "iq3_s":
+ t = torch.frombuffer(bytearray(IQ3_S_GRID), dtype=torch.uint8).reshape(512, 4).float()
+ else:
+ t = torch.frombuffer(bytearray(KSIGNS), dtype=torch.uint8).long()
+ _TABLES[key] = t.to(device)
+ return _TABLES[key]
+
+
+def _f16(b: torch.Tensor) -> torch.Tensor:
+ return b.contiguous().view(torch.float16).float() # [..., 2] bytes -> [..., 1]
+
+
+def _bits(x: torch.Tensor, n: int) -> torch.Tensor:
+ """[...] integer -> [..., n] bits, least significant first."""
+ sh = torch.arange(n, device=x.device, dtype=torch.int64)
+ return (x.long().unsqueeze(-1) >> sh) & 1
+
+
+def _blocks(raw: torch.Tensor, fmt: str) -> torch.Tensor:
+ qk, bb = BLOCKS[fmt]
+ n, w = raw.shape
+ assert raw.dtype == torch.uint8 and w % bb == 0, (fmt, tuple(raw.shape))
+ return raw.reshape(n * (w // bb), bb)
+
+
+def dequant(fmt: str, raw: torch.Tensor) -> torch.Tensor:
+ """raw uint8 [N, nb*bb] -> fp16 [N, nb*qk]."""
+ qk, bb = BLOCKS[fmt]
+ n = raw.shape[0]
+ b = _blocks(raw, fmt)
+ nb = b.shape[0]
+ d = _f16(b[:, 0:2]) # [nb, 1]
+ if fmt in ("q4_0", "iq4_nl", "q5_0"):
+ qs = b[:, 2:18] if fmt != "q5_0" else b[:, 6:22]
+ q = torch.cat([qs & 15, qs >> 4], dim=1).long() # elements 0..15 low nibbles, 16..31 high nibbles
+ if fmt == "q4_0":
+ v = (q - 8).float()
+ elif fmt == "iq4_nl":
+ v = _table("iq4_nl", raw.device)[q]
+ else:
+ qh = b[:, 2:6].contiguous().view(torch.int32).long() & 0xFFFFFFFF # [nb, 1]
+ hb = _bits(qh.reshape(nb), 32) # [nb, 32]
+ v = ((q | (hb << 4)) - 16).float()
+ out = d * v
+ elif fmt == "iq3_xxs":
+ qs = b[:, 2:66].long() # [nb, 64] grid indices
+ sc = b[:, 66:98].contiguous().view(torch.int32).long() & 0xFFFFFFFF # [nb, 8]
+ db = d.reshape(nb, 1) * (0.5 + (sc >> 28).float()) * 0.5 # [nb, 8]
+ sh = torch.tensor([0, 7, 14, 21], device=raw.device)
+ sidx = (sc.unsqueeze(-1) >> sh) & 0x7F # [nb, 8, 4]
+ sbits = _bits(_table("ksigns", raw.device)[sidx], 8) # [nb, 8, 4, 8]
+ sgn = 1.0 - 2.0 * sbits.float()
+ grid = _table("iq3_xxs", raw.device)[qs].reshape(nb, 8, 4, 8)
+ out = (db.reshape(nb, 8, 1, 1) * grid * sgn).reshape(nb, 256)
+ else: # iq3_s
+ qs = b[:, 2:66].long()
+ qh = b[:, 66:74].long() # [nb, 8]
+ sg = b[:, 74:106] # [nb, 32]
+ scb = b[:, 106:110] # [nb, 4]
+ sc = torch.stack([(scb & 15), (scb >> 4)], dim=-1).reshape(nb, 8).float()
+ db = d.reshape(nb, 1) * (1.0 + 2.0 * sc)
+ sgn = (1.0 - 2.0 * _bits(sg, 8).float()).reshape(nb, 8, 4, 8)
+ idx = qs | (_bits(qh, 8).reshape(nb, 64) << 8)
+ grid = _table("iq3_s", raw.device)[idx].reshape(nb, 8, 4, 8)
+ out = (db.reshape(nb, 8, 1, 1) * grid * sgn).reshape(nb, 256)
+ return out.reshape(n, -1).half()
diff --git a/exllamav3/foreign_quant/gguf_tables.py b/exllamav3/foreign_quant/gguf_tables.py
new file mode 100644
index 0000000..371b904
--- /dev/null
+++ b/exllamav3/foreign_quant/gguf_tables.py
@@ -0,0 +1,76 @@
+"""Lookup tables of the ggml IQ3_XXS / IQ3_S / IQ4_NL block formats, extracted from the gguf-py reference (ggml-org/llama.cpp, MIT).
+
+IQ3 grids: one row per index, 4 values per row (odd integers, exact in uint8). KSIGNS: 7-bit sign pattern -> 8 sign bits.
+"""
+IQ4_NL_VALUES = [-127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113]
+IQ3_XXS_GRID = bytes.fromhex(
+ "0404040414040404240404040c0c04041c0c04043e0c040404140404141404040c1c0404142404041c3e04042c3e0404"
+ "0c040c041c040c04040c0c04140c0c040c140c042c140c04041c0c04141c0c040c240c04242c0c04043e0c0404041404"
+ "14041404240414040c0c140404141404141414040c1c14041c1c14043e1c14040c2c14043e2c14042c3e14040c041c04"
+ "3e041c04040c1c04140c1c042c141c04043e1c041c0c24043e1c2404242424043e2c24041c3e24042c3e24040c042c04"
+ "3e042c04141c2c04142c2c042c1c340424343404040c3e04240c3e04340c3e041c243e040c343e040c04040c1c04040c"
+ "040c040c140c040c0c14040c1c14040c041c040c141c040c241c040c3e24040c042c040c04040c0c14040c0c0c0c0c0c"
+ "04140c0c14140c0c0c04140c1c04140c040c140c140c140c0c14140c041c140c143e140c04041c0c14041c0c04141c0c"
+ "0c1c1c0c34241c0c34341c0c0c04240c2c04240c042c240c04142c0c24142c0c34242c0c0c3e2c0c2c04340c14143e0c"
+ "04243e0c04040414140404140c0c04141c0c04140414041414140414341404140c1c0414142404140c040c141c040c14"
+ "2c040c14040c0c14140c0c140c140c14041c0c141c340c143e340c14043e0c1404041414140414140c0c14143e0c1414"
+ "04141414141414143e1c1414042414142c2c14140c041c14040c1c14240c1c14043e1c14243e1c142c1c24141c2c2414"
+ "1c042c143e142c140c242c14243e2c140c043e141c043e14340c3e142c243e140c04041c040c041c140c041c0c14041c"
+ "1c14041c042c041c2c34041c143e041c04040c1c14040c1c04140c1c0c1c0c1c24240c1c34240c1c0c04141c1c04141c"
+ "040c141c2c14141c142c141c143e141c0c0c1c1c1c1c1c1c041c241c3e24241c143e241c04042c1c34042c1c14142c1c"
+ "2c2c2c1c240c341c341c341c1c34341c1c1c3e1c04343e1c240404243e0c04242c1c04243e1c04241c2c04243e2c0424"
+ "243e0c24041414243e1c14240424142404341424343414243e041c242c241c24240424240c2c2424243424242c142c24"
+ "1c242c24043e2c242c043e24040c3e24140c3e24041c3e24140c042c0c24042c043e042c04040c2c34040c2c34140c2c"
+ "2c2c0c2c240c142c141c142c143e142c14041c2c1c2c1c2c040c242c1c14242c3e14242c143e242c14042c2c0c1c2c2c"
+ "042c342c24143e2c14243e2c241404342424043434240434243404340c140c340c340c343e0c143424341434041c1c34"
+ "341c1c34242424342c042c34142c2c341c1c34341c043e340c143e341c04043e2c04043e3e04043e040c043e141c043e"
+ "142c043e34140c3e04240c3e140c143e2c24143e142c143e04041c3e2c0c1c3e1c1c1c3e04341c3e0c14243e0c24243e"
+ "04042c3e14042c3e24142c3e041c343e")
+IQ3_S_GRID = bytes.fromhex(
+ "0101010103010101050101010b0101010f010101010301010303010105030101090301010d0301010105010103050101"
+ "0b0501010707010101090101050901010b0901010f090101030b0101070b0101010d0101050d0101030f0101090f0101"
+ "0f0f01010101030103010301050103010901030101030301030303010b03030101050301070503010f05030103070301"
+ "0b07030109090301030d03010b0d0301050f030101010501030105010b0105010f01050101030501070305010d030501"
+ "030505010b0505010107050109070501050905010b0905010f090501030b0501070b0501010f0501070f050107010701"
+ "030307010b030701010507010505070103070701070707010d07070109090701010b0701050b07010f0d0701030f0701"
+ "0b0f070101010901070309010f0309010305090109050901050709010109090107090901030b0901010f090105010b01"
+ "09010b0101050b0105050b010d050b0107070b0103090b010b090b010f090b010d0d0b01070f0b010d010d0103030d01"
+ "07030d0103070d01050b0d01030f0d0101010f0105010f0109010f0101050f0105050f010d050f0107070f01010b0f01"
+ "090b0f01010101030301010305010103090101030103010303030103070301030b0301030f0301030105010305050103"
+ "03070103090701030d070103090b01030d0b0103030d0103050f01030101030303010303070103030d01030301030303"
+ "0903030303050303010703030707030303090303010b0303050b0303010f03030d0f030301010503050305030b030503"
+ "0f03050301050503090505030507050301090503070905030b0b0503010d0503050f050303010703090107030f010703"
+ "0103070307030703030507030f050703010707030907070303090703050d0703010f0703070109030b01090305030903"
+ "090309030307090307070903050909030d090903010b0903090b090303010b0301030b0307030b0303050b0301070b03"
+ "05070b03030b0b0301050d0309050d030f050d0309090d030d090d0303010f0307010f0301030f0305030f0303050f03"
+ "0b070f0303090f03050d0f03010f0f030101010503010105070101050b0101050f010105010301050503010509030105"
+ "0d03010503050105070501050f050105010701050507010503090105070901050b090105010b0105050b01050f0d0105"
+ "010f0105070f01050b0f0105010103050501030501030305070303050f030305050503050b0503050307030509070305"
+ "05090305030b030503010505090105050f0105050305050507050505010705050f07050503090505070b05050f0b0505"
+ "030f0505090f050501010705050107050b010705030307050505070509050705030707050707070505090705010b0705"
+ "0d0d0705030109050f0109050105090507050905050709050b07090503090905050f09050b0f090509010b0503030b05"
+ "05050b050f070b0501090b05070b0b05010f0b0501010d0505010d050f010d0503050d050b0b0d05030d0d050b010f05"
+ "03030f050d050f0501070f0507090f05010b0f050501010703030107070301070b0301070f0301070505010703070107"
+ "070701070b07010705090107090901070f090107030b0107070d0107030f010703010307070103070b01030709030307"
+ "030503070705030701090307010d0307050f03070d0f03070101050705030507010505070507050709070507010b0507"
+ "03010707010307070903070703050707070507070f0507070107070703090707070907070f0907070b0b0707070f0707"
+ "07010907030309070d0309070505090703070907050b0907010d0907090d090703010b0701030b0705030b070b050b07"
+ "05070b0709090b070d0b0b07070f0b070d030d0703090d0703010f0707010f0701050f0705050f070b070f0701010109"
+ "090101090503010901050109090501090f0501090507010903090109010b0109010f0109050103090f01030903030309"
+ "0703030905050309010703090b07030907090309030b03090b0b03090301050907010509010305090b03050903050509"
+ "07070509010905090f0b0509050d0509010f05090901070903030709070307090105070905050709030707090b070709"
+ "0101090905010909090509090f07090901090909030f09090b010b090f010b0903050b09050d0b0907030d0909070d09"
+ "010d0d0901030f090b030f0901070f0907090f09030b0f090501010b0103010b0903010b0505010b0109010b0909010b"
+ "0f09010b050b010b0d0d010b090f010b0301030b0701030b0b01030b0503030b0305030b0507030b050f030b0101050b"
+ "0303050b0705050b0107050b0d07050b070b050b0501070b0f01070b0103070b0f05070b0909070b030b070b0b0d070b"
+ "070f070b0301090b0901090b0105090b0507090b0d09090b05030b0b0d050b0b030b0b0b070b0b0b05090d0b05010f0b"
+ "09010f0b05050f0b0303010d0703010d0b03010d0307010d0707010d010d010d0101030d0105030d0f05030d090d030d"
+ "0503050d0907050d0509050d0b0b050d050d050d010f050d0101070d0903070d0305070d0109070d0b05090d0709090d"
+ "050d090d01010b0d07010b0d09070b0d010d0b0d0b010d0d01090d0d03030f0d07030f0d0101010f0901010f0f01010f"
+ "0105010f0505010f0d07010f0109010f090b010f050d010f0501030f0303030f0905030f0709030f0b09030f0301050f"
+ "0901050f0103050f0d03050f0305050f0107050f030b050f0501070f0507070f0b07070f070b070f0301090f0b01090f"
+ "0703090f0105090f010b090f05050b0f05090b0f05010d0f03070d0f01010f0f")
+KSIGNS = bytes.fromhex(
+ "008182038405068788090a8b0c8d8e0f901112931495961718999a1b9c1d1e9fa02122a324a5a62728a9aa2bac2d2eaf"
+ "30b1b233b43536b7b8393abb3cbdbe3fc04142c344c5c64748c9ca4bcc4d4ecf50d1d253d45556d7d8595adb5cddde5f"
+ "60e1e263e46566e7e8696aeb6cedee6ff07172f374f5f67778f9fa7bfc7d7eff")
diff --git a/tests/test_foreign_gap_formats_cpu.py b/tests/test_foreign_gap_formats_cpu.py
new file mode 100644
index 0000000..230b68b
--- /dev/null
+++ b/tests/test_foreign_gap_formats_cpu.py
@@ -0,0 +1,39 @@
+import os, sys
+import numpy as np
+import pytest
+import torch
+
+sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
+gguf = pytest.importorskip("gguf")
+from gguf import GGMLQuantizationType as T
+
+import importlib.util
+# Load the two modules by path: importing the exllamav3 package would build/load the CUDA extension.
+_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "exllamav3", "foreign_quant")
+_pkg = importlib.util.spec_from_file_location("fq_gap", os.path.join(_dir, "__init__.py"), submodule_search_locations=[_dir])
+_mod = importlib.util.module_from_spec(_pkg)
+sys.modules["fq_gap"] = _mod
+_pkg.loader.exec_module(_mod)
+import fq_gap.gap_formats as gf
+
+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}
+
+
+def _random_rows(fmt, n_rows, nb, seed):
+ qk, bb = gf.BLOCKS[fmt]
+ rng = np.random.default_rng(seed)
+ raw = rng.integers(0, 256, size=(n_rows, nb, bb), dtype=np.uint8)
+ d = rng.uniform(0.001, 0.05, size=(n_rows, nb)).astype(np.float16) # sane fp16 scale in the first 2 bytes
+ raw[:, :, 0:2] = d.view(np.uint8).reshape(n_rows, nb, 2)
+ return raw.reshape(n_rows, nb * bb)
+
+
[email protected]("fmt", list(NAMES))
+def test_matches_gguf_py(fmt):
+ qk, bb = gf.BLOCKS[fmt]
+ nb = 3 if qk == 256 else 8
+ raw = _random_rows(fmt, 5, nb, seed=7)
+ ref = gguf.quants.dequantize(raw, NAMES[fmt]) # [5, nb*qk] float32
+ out = gf.dequant(fmt, torch.from_numpy(raw)).float().numpy()
+ assert out.shape == ref.shape
+ np.testing.assert_allclose(out, ref, rtol=2e-3, atol=2e-3 * np.abs(ref).max())