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 dense: tokenizer.json from tokenizer.ggml metadata (byte-level BPE, qwen2 pre-tokenizer); Q8_0 torch dequant fallback for builds without fq_* kernels

YanissAmz2fb6cd1c6e11
diff --git a/exllamav3/foreign_quant/gap_formats.py b/exllamav3/foreign_quant/gap_formats.py
index b0dc400..25093a4 100644
--- a/exllamav3/foreign_quant/gap_formats.py
+++ b/exllamav3/foreign_quant/gap_formats.py
@@ -11,9 +11,9 @@ 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),
+BLOCKS = {"q8_0": (32, 34), "q4_0": (32, 18), "q5_0": (32, 22), "iq4_nl": (32, 18), "iq3_xxs": (256, 98), "iq3_s": (256, 110),
"mxfp4": (32, 17), "nvfp4_gguf": (64, 36)}
-GGML_TYPE_IDS = {"q4_0": 2, "q5_0": 6, "iq3_xxs": 18, "iq4_nl": 20, "iq3_s": 21, "mxfp4": 39, "nvfp4_gguf": 40}
+GGML_TYPE_IDS = {"q8_0": 8, "q4_0": 2, "q5_0": 6, "iq3_xxs": 18, "iq4_nl": 20, "iq3_s": 21, "mxfp4": 39, "nvfp4_gguf": 40}
# MXFP4 / NVFP4 nibble -> value in half units (the scale carries the other factor 0.5)
_FP4_HALF = (0, 1, 2, 3, 4, 6, 8, 12, 0, -1, -2, -3, -4, -6, -8, -12)
@@ -60,6 +60,9 @@ def dequant(fmt: str, raw: torch.Tensor) -> torch.Tensor:
n = raw.shape[0]
b = _blocks(raw, fmt)
nb = b.shape[0]
+ if fmt == "q8_0": # [fp16 d][32 x int8]; torch fallback for builds without the fq_* extension kernels (ROCm)
+ d = b[:, 0:2].contiguous().view(torch.float16).float()
+ return (d * b[:, 2:34].contiguous().view(torch.int8).float()).reshape(n, -1).half()
if fmt == "mxfp4": # [e8m0 scale][16 bytes: elements 0..15 low nibbles, 16..31 high nibbles]
e = b[:, 0:1].float()
d = torch.exp2(e - 128.0) # ggml_e8m0_to_fp32_half
diff --git a/exllamav3/loader/gguf_config.py b/exllamav3/loader/gguf_config.py
index 4555cdb..da13670 100644
--- a/exllamav3/loader/gguf_config.py
+++ b/exllamav3/loader/gguf_config.py
@@ -77,12 +77,14 @@ def name_map(cfg: dict) -> dict:
def write_config_dir(gguf_path: str, outdir: str) -> str:
"""Create outdir/{config.json, model.gguf -> gguf_path}; return outdir."""
+ from .gguf_tokenizer import write_tokenizer_files
g = GGUFCollection(gguf_path)
+ os.makedirs(outdir, exist_ok=True)
try:
cfg = hf_config(g)
+ write_tokenizer_files(g, outdir)
finally:
g.close()
- os.makedirs(outdir, exist_ok=True)
with open(os.path.join(outdir, "config.json"), "w", encoding="utf8") as f:
json.dump(cfg, f, indent=1)
link = os.path.join(outdir, "model.gguf")
diff --git a/exllamav3/loader/gguf_tokenizer.py b/exllamav3/loader/gguf_tokenizer.py
new file mode 100644
index 0000000..b905ee2
--- /dev/null
+++ b/exllamav3/loader/gguf_tokenizer.py
@@ -0,0 +1,48 @@
+"""tokenizer.json + tokenizer_config.json from the tokenizer.ggml.* metadata of a GGUF (byte-level BPE: gpt2 model with
+the qwen2 pre-tokenizer). Other tokenizer models / pre-tokenizers raise: nothing is guessed."""
+from __future__ import annotations
+import json
+import os
+
+from .gguf_collection import GGUFCollection
+
+# ggml token types: 1 normal, 2 unknown, 3 control, 4 user-defined, 5 unused, 6 byte
+_QWEN2_REGEX = (r"(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\r\n\p{L}\p{N}]?\p{L}+|\p{N}| ?[^\s\p{L}\p{N}]+[\r\n]*|\s*[\r\n]+|\s+(?!\S)|\s+")
+_PRE = {"qwen2": _QWEN2_REGEX}
+
+
+def write_tokenizer_files(g: GGUFCollection, outdir: str) -> None:
+ m = g.meta
+ model, pre = m.get("tokenizer.ggml.model"), m.get("tokenizer.ggml.pre")
+ if model != "gpt2" or pre not in _PRE:
+ raise ValueError(f"GGUF tokenizer model={model!r} pre={pre!r} is not mapped (have gpt2 + {sorted(_PRE)})")
+ toks, types = m["tokenizer.ggml.tokens"], m["tokenizer.ggml.token_type"]
+ merges = [tuple(x.split(" ", 1)) for x in m["tokenizer.ggml.merges"]]
+ added = [{"id": i, "content": t, "single_word": False, "lstrip": False, "rstrip": False, "normalized": False,
+ "special": ty == 3} for i, (t, ty) in enumerate(zip(toks, types)) if ty in (3, 4)]
+ tj = {
+ "version": "1.0", "truncation": None, "padding": None, "added_tokens": added, "normalizer": None,
+ "pre_tokenizer": {"type": "Sequence", "pretokenizers": [
+ {"type": "Split", "pattern": {"Regex": _PRE[pre]}, "behavior": "Isolated", "invert": False},
+ {"type": "ByteLevel", "add_prefix_space": False, "trim_offsets": False, "use_regex": False}]},
+ "post_processor": None,
+ "decoder": {"type": "ByteLevel", "add_prefix_space": False, "trim_offsets": False, "use_regex": False},
+ "model": {"type": "BPE", "dropout": None, "unk_token": None, "continuing_subword_prefix": "",
+ "end_of_word_suffix": "", "fuse_unk": False, "byte_fallback": False, "ignore_merges": False,
+ "vocab": {t: i for i, t in enumerate(toks)}, "merges": [list(x) for x in merges]},
+ }
+ with open(os.path.join(outdir, "tokenizer.json"), "w", encoding="utf8") as f:
+ json.dump(tj, f, ensure_ascii=False)
+
+ def tok(key):
+ i = m.get(key)
+ return toks[i] if i is not None else None
+
+ tc = {"tokenizer_class": "PreTrainedTokenizerFast", "model_max_length": 131072, "clean_up_tokenization_spaces": False,
+ "add_bos_token": bool(m.get("tokenizer.ggml.add_bos_token", False)),
+ "eos_token": tok("tokenizer.ggml.eos_token_id"), "pad_token": tok("tokenizer.ggml.padding_token_id"),
+ "bos_token": tok("tokenizer.ggml.bos_token_id"), "unk_token": None}
+ if "tokenizer.chat_template" in m:
+ tc["chat_template"] = m["tokenizer.chat_template"]
+ with open(os.path.join(outdir, "tokenizer_config.json"), "w", encoding="utf8") as f:
+ json.dump(tc, f, ensure_ascii=False, indent=1)
diff --git a/exllamav3/modules/foreign_linear.py b/exllamav3/modules/foreign_linear.py
index 4a4ca20..0f3842e 100644
--- a/exllamav3/modules/foreign_linear.py
+++ b/exllamav3/modules/foreign_linear.py
@@ -179,6 +179,8 @@ class ForeignLinear(Module):
getattr(ext, f"fq_dequant_{self.format}")(
self.packed[first:first + count].contiguous(), out)
return out
+ if self.format == "q8_0" and not hasattr(ext, "fq_dequant_q8_0"):
+ return gap_dequant("q8_0", self.packed[first:first + count])
if self.format == "q8_0":
Kb = self.in_features // 32
out = torch.empty((count, self.in_features), dtype=torch.float16, device=dev)