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.

GLM server: add `--cache-bits 8` (packed int8 MLA latent cache) (#118)

resynthbd07d7ffb3d0
diff --git a/tools/glm/SERVE.md b/tools/glm/SERVE.md
index bb13db2..d07a369 100644
--- a/tools/glm/SERVE.md
+++ b/tools/glm/SERVE.md
@@ -30,6 +30,7 @@ call, a streamed call, a tool call, and two 400-token benchmark calls.
| `--default-reasoning-effort` | `medium` | template `reasoning_effort` when a request omits it: `low`, `medium`, `high` or `max` |
| `--max-history` | `1` | recurrent history slots; must equal draft tokens (1 for MTP). Each extra slot costs ~2.15 GiB |
| `-c`, `--max-ctx` | `131072` | context window in tokens, shared by all sessions |
+| `--cache-bits` | `0` | `0` = fp16 MLA latent pages (default); `8` = packed int8 latent pages, target and MTP draft caches together. The DSA indexer and k-pool planes and the shared rope key stay fp16, so the saving is a smaller fraction than the latent-only 1.7×. Greedy output stays coherent but differs token-wise from fp16. Slot files do not cross cache modes (a cross-mode restore fails with `slot file cache layout does not match`); with `--sessions` > 1 the packed pages are not row-invariant, so batched decode changes the greedy text (see below). |
| `--image-cache-mb` | `1024` | host memory for the decoded image embeddings, in MiB, one pool shared by all sessions; `0` = no byte limit |
| `--image-cache-items` | `32` | cap on how many pictures are held at once; `0` = no limit |
| `--image-cache-dir` | `~/.cache/kyojin/image-embeddings` | directory of the second-level picture cache; used only when `--image-cache-dir-max-gb` is set. A file is named after the pack and its preprocessing settings as well as the picture, so another model or other pixel settings never reach rows computed with the old ones |
diff --git a/tools/glm/serve.py b/tools/glm/serve.py
index bc51bd6..8be9a2f 100644
--- a/tools/glm/serve.py
+++ b/tools/glm/serve.py
@@ -642,7 +642,7 @@ class ResidentEngine:
row_budget = 0
def __init__(self, model_path: str, max_history: int = 1, max_ctx: int = 131072, num_draft: int = 1,
- vision: bool = True, sessions: int = 1,
+ vision: bool = True, sessions: int = 1, cache_bits: int = 0,
image_cache_bytes: int | None = None, image_cache_items: int = 0,
image_cache_format: str = "fp32",
image_cache_dir: str | None = IMAGE_CACHE_DIR_DEFAULT,
@@ -685,6 +685,10 @@ class ResidentEngine:
max_position = int(self.config.config_dict.get("max_position_embeddings") or self.config.config_dict.get("text_config", {}).get("max_position_embeddings") or 32768)
ctx = min(max_position, max_ctx)
self.ctx = ctx
+ # cache_bits 0 (default) = the fp16 MLA latent pages; 8 (--cache-bits 8) = packed int8 latent pages. The DSA
+ # indexer and k-pool planes and the rope key stay fp16, so the saving is smaller than the latent-only ratio.
+ from exllamav3 import CacheLayer_quant
+ qkw = dict(layer_type=CacheLayer_quant, k_bits=cache_bits, v_bits=cache_bits) if cache_bits else {}
slots = max(sessions, int(os.environ.get("EXL3_GLM_SLOTS", "0") or 0))
# GLM_NGRAM = "gram,K,match[,cmin]": n-gram lookup of tools/qwen/spec_policy.install_lookup. When the
# last `match` tokens occurred before, the round verifies the K tokens that followed them instead of the drafter's
@@ -699,7 +703,7 @@ class ResidentEngine:
max_history = max(max_history, self.lookup[1])
self.lookup_stats: dict[str, int] = {}
self.cache = Cache(self.model, max_num_tokens=ctx + 4096,
- max_history=max_history, max_batch_size=slots)
+ max_history=max_history, max_batch_size=slots, **qkw)
from exllamav3.util import jit_log, kernel_seed, tune_cache
tune_cache.install_pack(model_path) # <pack>/tuning: GEMM autotune winners from qualify --stage autotune
kernel_seed.install() # shipped Triton kernels for this Triton version: nothing to compile on a first start
@@ -709,7 +713,7 @@ class ResidentEngine:
if num_draft > 0:
self.draft_model = Model.from_config(self.config, component="mtp")
self.draft_cache = Cache(self.draft_model, max_num_tokens=ctx + 4096,
- max_history=max_history, max_batch_size=slots)
+ max_history=max_history, max_batch_size=slots, **qkw)
startup_health.stage("drafter weights")
self.draft_model.load(device="cuda:0", progressbar=False, callback=startup_health.load_callback())
else: # --num-draft 0: plain decode, no drafter
@@ -2079,6 +2083,10 @@ def main() -> None:
help=f"requests decoded together through the one Generator (1..{MAX_SESSIONS}); default 1 = one request at a time. "
"The KV pool (-c) is shared by all sessions")
parser.add_argument("-c", "--max-ctx", type=int, default=131072, help="context window (capped by max_position_embeddings)")
+ parser.add_argument("--cache-bits", type=int, default=0, choices=(0, 8),
+ help="bits per element of the MLA latent cache pages (0 = fp16, the default; 8 = packed int8; the DSA "
+ "indexer and k-pool planes and the rope key stay fp16; output differs token-wise from fp16; slot "
+ "files do not cross cache modes, and a cross-mode restore is rejected)")
parser.add_argument("--image-cache-mb", type=int, default=IMAGE_CACHE_MB_DEFAULT,
help=f"host memory for the decoded image embeddings, in MiB; 0 = no byte limit. Like -c this "
f"is one pool shared by every --sessions request (default {IMAGE_CACHE_MB_DEFAULT}): "
@@ -2123,6 +2131,10 @@ def main() -> None:
f"(per-session window = -c / sessions when all are busy); greedy output is not guaranteed equal to the same "
f"request run alone; slot save/restore waits for every running request and acts on the last prompt noted",
file=sys.stderr, flush=True)
+ if args.cache_bits and args.sessions > 1:
+ print(f"serve: --cache-bits {args.cache_bits} with --sessions {args.sessions}: the packed int8 MLA pages are not "
+ f"row-invariant, so batched decode changes greedy output versus a single session (the rows still agree with "
+ f"each other). Slot files also do not cross cache modes.", file=sys.stderr, flush=True)
if os.environ.get("EXL3_SCHED_CHUNK_IL", "0") == "1":
# chunked-prefill interleave (exllamav3/generator/chunk_il.py): needs another request running beside the big prefill
print("serve: chunk_il ON " + ("" if args.sessions > 1 else "(no effect: needs --sessions >= 2)"), file=sys.stderr, flush=True)
@@ -2155,7 +2167,7 @@ def main() -> None:
image_cache_size = (f"{args.image_cache_mb} MiB of host memory (--image-cache-mb)" if image_cache_bytes
else "no byte limit (--image-cache-mb 0)")
engine = ResidentEngine(args.model, max_history=args.max_history or max(args.num_draft, 1), max_ctx=args.max_ctx, num_draft=args.num_draft,
- vision=not args.no_vision, sessions=args.sessions,
+ vision=not args.no_vision, sessions=args.sessions, cache_bits=args.cache_bits,
image_cache_bytes=image_cache_bytes, image_cache_items=args.image_cache_items,
image_cache_format=args.image_cache_format,
image_cache_dir=args.image_cache_dir, image_cache_dir_max_gb=args.image_cache_dir_max_gb,
@@ -2164,6 +2176,7 @@ def main() -> None:
template = Path(args.chat_template or DEFAULT_TEMPLATE).expanduser().read_text(encoding="utf-8")
print(mem_line("loaded"), flush=True)
print(f"serve: vision={engine.supports_vision}", flush=True)
+ print(f"serve: cache={'int' + str(args.cache_bits) + ' (slot files do not cross cache modes)' if args.cache_bits else 'fp16'}", flush=True)
print(f"serve: image cache {image_cache_size}, {'no count cap' if not engine.emb_cache.max_items else str(engine.emb_cache.max_items) + ' pictures max'}, "
f"{args.image_cache_format} rows, {engine.emb_cache.policy} policy, shared by {args.sessions} session(s)", flush=True)
if engine.emb_disk is not None: