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.
Python amdapple-siliconcudadeepseekexl3exllamav3glminferencellmllm-inferencelocal-llmnvidiaopenai-apiquantizationqwenrocmstrix-halo
Vision: opt-in GEMM for the spatial-merge conv, plus a tune primer
MangetsuC3cce11d25759
diff --git a/exllamav3/modules/conv.py b/exllamav3/modules/conv.pyindex 8937238..e4dce2f 100644--- a/exllamav3/modules/conv.py+++ b/exllamav3/modules/conv.py@@ -1,4 +1,5 @@from __future__ import annotations+import osfrom typing_extensions import overrideimport torchimport torch.nn.functional as F@@ -9,6 +10,31 @@ from . import Modulefrom ..ext import exllamav3_ext as extfrom ..model.model_tp_alloc import TPAllocation++def conv_gemm_enabled() -> bool:+ """+ EXL3_CONV_GEMM=1: run the reshape2d 2D convolutions (the spatial-merge downsample of the GLM-4V+ family vision tower) as one hgemm GEMM instead of F.conv2d.++ F.conv2d goes to MIOpen, which has to pick a solver for every new convolution shape the first+ time it sees it. On a platform that ships no MIOpen find-db (gfx1151: the ROCm SDK's+ share/miopen/db has no .fdb.txt at all), that search benchmarks the candidate solvers on the+ real tensors, and its cost is linear in the batch -- which here is the number of vision tokens,+ so it grows with the image:++ 400x400 (225 vision tokens) ~67 s+ 1024x1024 (1369 vision tokens) ~409 s+ 2888x4177 (7992 vision tokens) ~40 min (extrapolated)++ The shape is cached per (arch, MIOpen version, shape) in the user find-db, so a repeat of the+ same image is fast, but every new image size pays again. The GEMM below is the same+ contraction without MIOpen: it removes the search entirely and costs milliseconds.++ Default off ("0" or unset): the original F.conv2d path, byte for byte.+ """+ return os.environ.get("EXL3_CONV_GEMM", "0") != "0"++class Conv(Module):def __init__(@@ -36,6 +62,9 @@ class Conv(Module):self._numel = Noneself.flat = flatself.reshape2d = reshape2d+ # EXL3_CONV_GEMM: (dim, out_channels) weight in GEMM layout, built on first use so the+ # default F.conv2d path pays neither the memory nor the transpose+ self._gemm_b = Noneself.dims = len(kernel_size)assert self.dims in [2, 3], "Convolution must be 2D or 3D"@@ -53,6 +82,7 @@ class Conv(Module):self._numel = weight.numel() + (bias.numel() if bias is not None else 0)self.weight = weightself.bias = bias+ self._gemm_b = None # rebuilt from these weights on the next EXL3_CONV_GEMM forward@override@@ -60,6 +90,7 @@ class Conv(Module):self.device = Noneself.weight = Noneself.bias = None+ self._gemm_b = None@override@@ -76,6 +107,36 @@ class Conv(Module):return self._numel+ def forward_2d_gemm(self, x: torch.Tensor, out_dtype: torch.dtype | None) -> torch.Tensor:+ """+ The reshape2d 2D convolution as one GEMM (see conv_gemm_enabled).++ The input is a patch sequence (batch, tokens * kh * kw, in_channels) laid out merge-major:+ the kh * kw patches of one merged token are consecutive rows, and each row is+ (in_channels, ...) inside its patch. With stride == kernel the output is 1x1, so the+ convolution is a plain sum over (c, kh, kw) of x * weight -- i.e. x_flat @ w_flat.T with+ both operands flattened in (kh, kw, c) order. That is the same contraction the flat 3D+ patch embedding already runs through ext.hgemm, and `weight` is stored (out, c, kh, kw),+ so the (kh, kw, c) layout is one permute done once per load, never per forward.+ """+ assert x.numel() % self.dim == 0, \+ f"EXL3_CONV_GEMM: {x.shape} does not hold whole {self.kernel_size} patches of {self.in_channels} channels"+ # The activations reaching this layer are fp32 (the 3D patch embedding runs out_dtype=float+ # and the blocks keep it); the F.conv2d branch casts with x.to(self.weight.dtype) and so does+ # this one, so both compute on the same fp16 operand.+ x_flat = x.reshape(-1, self.dim).to(torch.half)+ if self._gemm_b is None:+ w = self.weight.permute(0, *range(2, 2 + self.dims), 1).reshape(self.out_channels, self.dim)+ self._gemm_b = w.T.contiguous()+ dt = out_dtype or self.out_dtype or torch.half+ y = torch.empty((x_flat.shape[0], self.out_channels), dtype = dt, device = x_flat.device)+ ext.hgemm(x_flat, self._gemm_b, y)+ if self.bias is not None:+ y += self.bias+ # Same shape the F.conv2d branch returns: 1x1 output spatial, so (tokens, 1, out_channels)+ return y.view(-1, 1, self.out_channels)++@overridedef forward(self,@@ -85,10 +146,13 @@ class Conv(Module):) -> torch.Tensor:if self.dims == 2:- if self.reshape2d:- x = x.view(-1, self.kernel_size[0], self.kernel_size[1], x.shape[-1]).permute(0, 3, 1, 2)- y = F.conv2d(x.to(self.weight.dtype), self.weight, self.bias, self.kernel_size)- y = y.flatten(2).permute(0, 2, 1).contiguous()+ if self.reshape2d and conv_gemm_enabled():+ y = self.forward_2d_gemm(x, out_dtype)+ else:+ if self.reshape2d:+ x = x.view(-1, self.kernel_size[0], self.kernel_size[1], x.shape[-1]).permute(0, 3, 1, 2)+ y = F.conv2d(x.to(self.weight.dtype), self.weight, self.bias, self.kernel_size)+ y = y.flatten(2).permute(0, 2, 1).contiguous()elif self.dims == 3:bsz, seqlen, dim = x.shapediff --git a/tools/strix_halo/prime_dense_tune.py b/tools/strix_halo/prime_dense_tune.pynew file mode 100644index 0000000..314d435--- /dev/null+++ b/tools/strix_halo/prime_dense_tune.py@@ -0,0 +1,331 @@+"""Create/populate the dense-GEMM tune cache *before* the first server start.++Why this exists+---------------+Every dense product exllamav3 runs goes through hgemm, whose C++ tuner (hgemm.cu dtune)+screens the rocBLAS solutions on the live operands the first time it sees a key++ (n, k, rows rounded up to 256, (ldc == n) | (padded A pitch) << 1, out fp32)++and appends the winner to EXL3_DENSE_GEMM_TUNE_FILE+(default ~/.cache/exllamav3/dense_gemm_tune.txt). Inside a request that screening costs+0.05-4 s per key. Measured stage times for a first start with no tune file: 27 s weights ++1 s drafter + 1 s vision tower + 139 s warm-up (its 4.6K-token prefill walks the row classes)++ 538 s startup priming = ~12 min to /health, against ~38 s with a populated file (warm-up+9 s, priming 0.3 s).++tools/glm/serve.py's own prime_dense_tune() cannot help on an empty file -- it only fills in+missing row classes for the (n, k, ...) shapes the file already holds. This script+enumerates the shapes from the pack itself, building the module lists straight from+config.json without loading a single weight (a 100 GB pack costs ~1 GiB of RSS, ~0.3 s), and+runs that same screening up front with small preallocated buffers.++What it primes+--------------+* every dense Linear of the target and MTP components (the filter of+ exllamav3/model/dense_warmup.py), rows 256 .. --lm-class+* every dense Linear of the vision tower (rows = patches, up to max_image_tokens * 4)+* the vision tower's GEMM convolutions: the flat 3D patch embed (rows = patches) and the+ reshape2d spatial-merge downsample (rows = vision tokens)++Rows below 256 never reach the tuner (MIN_M in hgemm.cu), so they are not generated.++Entries are only valid for the rocBLAS version + GPU arch that wrote them: the tuner tags+every line and ignores lines whose tag does not match. Re-run after a ROCm upgrade (or use+--rescreen).++ python tools/strix_halo/prime_dense_tune.py --model <pack dir> --dry-run+ python tools/strix_halo/prime_dense_tune.py --model <pack dir>+"""++from __future__ import annotations++import argparse+import os+import sys+import time+from dataclasses import dataclass+from pathlib import Path++import torch++MIN_M = 256 # hgemm.cu: rows below this never reach the tuner+PAD_ELEMS = 64 # exl3.PAD_ELEMS: width a row-padded activation adds+CLASS_STEP = 256+++@dataclass+class Shape:+ n: int+ k: int+ f32: set[int] # 1 = fp32 output, 0 = fp16 output+ key4: set[int] # bit0 ldc == n, bit1 padded A pitch+ classes: list[int]+ label: str = ""++ def keys(self):+ return [(self.n, self.k, mc, k4, f32)+ for mc in self.classes for k4 in sorted(self.key4) for f32 in sorted(self.f32)]+++def m_class(m: int) -> int:+ """hgemm.cu: rows are rounded up to a 256 row class."""+ return ((m + CLASS_STEP - 1) // CLASS_STEP) * CLASS_STEP+++def classes_up_to(top: int) -> list[int]:+ """Every row class the tuner can be asked for at or below `top` (top included, rounded up)."""+ return list(range(MIN_M, m_class(top) + 1, CLASS_STEP))+++def default_tune_path() -> Path:+ p = os.environ.get("EXL3_DENSE_GEMM_TUNE_FILE")+ return Path(p) if p else Path(os.environ.get("HOME", "/tmp")) / ".cache/exllamav3/dense_gemm_tune.txt"+++def read_keys(path: Path) -> list[tuple[str, int, int, int, int, int]]:+ if not path.exists():+ return []+ out = []+ for line in path.read_text().splitlines():+ f = line.split()+ if len(f) == 7:+ out.append((f[0], int(f[1]), int(f[2]), int(f[3]), int(f[4]), int(f[5])))+ return out+++def newest_tag(keys) -> str | None:+ """The tag the tuner would write now = the tag of the newest line in the file."""+ return keys[-1][0] if keys else None+++def _conv_flags(out_dtype, k: int) -> tuple[set[int], set[int]]:+ """(f32, key4) for a Conv GEMM. The conv's own out_dtype IS the GEMM output dtype+ (Conv.forward allocates y with it), unlike a Linear, whose inner module decides."""+ f32 = {1} if out_dtype == torch.float32 else {0}+ return f32, ({1, 3} if k % 128 == 0 else {1})+++def _linear_flags(k: int, union: bool) -> tuple[set[int], set[int]]:+ """(f32, key4) for a dense Linear GEMM.++ Measured on GLM-5.3-Flash (all 20 dense Linear shapes in a populated tune file): the GEMM+ output is fp16 -- f32 = 0 -- even where the Linear module declares out_dtype = torch.float32+ (that dtype is a later cast, not the GEMM's C tensor). f32 = 1 occurs only on the vision+ Conv layers. --union-flags also primes f32 = 1 here, at twice the screening cost.++ A padded activation pitch is only possible where the producer pads (EXL3 linears,+ k % 128 == 0); eligibility can depend on loaded weights, so both key4 variants are primed.+ """+ f32 = {0, 1} if union else {0}+ return f32, ({1, 3} if k % 128 == 0 else {1})+++def linear_shapes(models: dict, top: int, union: bool = False) -> list[Shape]:+ """Dense Linears, same filter as exllamav3/model/dense_warmup.py._dense_linears."""+ from exllamav3.modules.linear import Linear++ seen: dict[tuple, Shape] = {}+ for comp, model in models.items():+ for m in model:+ if not isinstance(m, Linear) or ".experts." in m.key or m.out_features == 0 \+ or m.out_features > 65536 or m.in_features == 0:+ continue+ n, k = m.out_features, m.in_features+ f32, key4 = _linear_flags(k, union)+ sig = (n, k, tuple(sorted(f32)), tuple(sorted(key4)))+ if sig not in seen:+ seen[sig] = Shape(n, k, f32, key4, classes_up_to(top), f"{comp or 'target'}: {m.key}")+ return list(seen.values())+++def vision_rows_for(cfg, width: int, height: int) -> tuple[int, int]:+ """(vision tokens, patches) the largest canvas of an image that fits `width x height` produces."""+ from exllamav3.architecture.mm_processing.glm5_next import glm5_vision_canvas++ v, pp = cfg.vision, cfg.vision_pp+ th, tw = glm5_vision_canvas(pp.temporal_patch_size, height, width, pp.temporal_patch_size,+ pp.patch_size * pp.merge_size, pp.min_image_tokens,+ pp.max_image_tokens)+ patches = (th // pp.patch_size) * (tw // pp.patch_size)+ return patches // (v.spatial_merge_size ** 2), patches+++def vision_rows(m, token_top: int, patch_top: int) -> int:+ """Row ceiling for one vision module.++ The tower feeds whole patches to the blocks and to the flat patch embed, but the merger+ sits behind the spatial-merge downsample and therefore sees one row per merged token.+ Priming the merger at the patch count would quadruple its key count for nothing.+ """+ return token_top if ".merger" in m.key else patch_top+++def vision_conv_shapes(cfg, model, token_top: int, patch_top: int) -> list[Shape]:+ """The vision tower's GEMM convolutions as (n, k, rows) key space."""+ from exllamav3.modules.conv import Conv++ out = []+ for m in model:+ if not isinstance(m, Conv) or not (m.flat or m.reshape2d):+ continue+ f32, key4 = _conv_flags(m.out_dtype, m.dim)+ # a flat 3D patch embed runs one row per patch; reshape2d runs one row per merged token+ # the flat patch embed is fed patches; the reshape2d downsample merges them+ rows = patch_top if m.flat else token_top+ out.append(Shape(m.out_channels, m.dim, f32, key4, classes_up_to(rows),+ f"vision: {m.key} "+ f"({'patch embed, rows = patches' if m.flat else 'spatial merge, rows = tokens'})"))+ return out+++def screen(shapes: list[Shape], have: list, budget_s: float, log=print) -> dict:+ """One hgemm per missing key: the tuner screens that key and appends the winner."""+ from exllamav3.ext import exllamav3_ext as ext++ tag = newest_tag(have)+ known = {(t, n, k, mc, k4, f32) for (t, n, k, mc, k4, f32) in have if t == tag}+ todo = []+ for s in shapes:+ for key in s.keys():+ if (tag, *key) not in known:+ todo.append((s, key))+ total = sum(len(s.keys()) for s in shapes)+ log(f"tag in use: {tag or '(none yet -- the tuner will stamp its own)'}")+ log(f"keys to screen: {len(todo)} of {total} for these shapes")+ if not todo:+ log("nothing to do: every key is already in the file for this tag")+ return {"screened": 0, "seconds": 0.0, "todo": 0}++ t0, done = time.perf_counter(), 0+ gen = torch.Generator(device="cuda").manual_seed(0)+ by_shape: dict[tuple, list] = {}+ for s, key in todo:+ by_shape.setdefault((s.n, s.k), []).append((s, key))+ for (n, k), items in sorted(by_shape.items()):+ max_mc = max(key[2] for _, key in items)+ padded = any(key[3] & 2 for _, key in items)+ # Two activation buffers: a contiguous one (lda == k) and a wider one whose [:, :k] view+ # carries the padded row pitch (lda != k) that the other key4 variant encodes.+ a_buf = torch.empty((max_mc, k), device="cuda", dtype=torch.half).normal_(0, 0.02, generator=gen)+ a_wide = (torch.empty((max_mc, k + PAD_ELEMS), device="cuda", dtype=torch.half)+ .normal_(0, 0.02, generator=gen)) if padded else None+ b_buf = torch.empty((k, n), device="cuda", dtype=torch.half).normal_(0, 0.02, generator=gen)+ c_bufs = {dt: torch.empty((max_mc, n), device="cuda", dtype=dt)+ for dt in {torch.float32 if key[4] else torch.half for _, key in items}}+ for s, key in items:+ _, _, mc, k4, f32 = key+ a = a_wide[:mc, :k] if (k4 & 2) else a_buf[:mc]+ ext.hgemm(a, b_buf, c_bufs[torch.float32 if f32 else torch.half][:mc])+ done += 1+ if budget_s and time.perf_counter() - t0 > budget_s:+ log(f" budget {budget_s:.0f} s reached at {done}/{len(todo)} keys")+ break+ if done % 128 == 0:+ torch.cuda.synchronize()+ el = time.perf_counter() - t0+ log(f" {done}/{len(todo)} keys, {el:.0f} s ({el / done * 1000:.0f} ms/key)")+ del a_buf, a_wide, b_buf+ c_bufs.clear()+ torch.cuda.empty_cache()+ torch.cuda.synchronize()+ return {"screened": done, "seconds": time.perf_counter() - t0, "todo": len(todo)}+++def main() -> int:+ ap = argparse.ArgumentParser(description=__doc__,+ formatter_class=argparse.RawDescriptionHelpFormatter)+ ap.add_argument("--model", required=True, help="EXL3 pack directory (only config.json is read)")+ ap.add_argument("--tune-file", default=None,+ help="dense GEMM tune file (default: $EXL3_DENSE_GEMM_TUNE_FILE)")+ ap.add_argument("--lm-class", type=int, default=4096,+ help="largest LM prefill row class to prime (default 4096, the serving chunk)")+ ap.add_argument("--max-image", default="",+ help="cover every image that fits WxH, e.g. 2048x2048: derives the vision row "+ "ceilings from the pack's canvas rules instead of a raw class number")+ ap.add_argument("--vision-class", type=int, default=0,+ help="manual override: largest row class to prime for the vision tower "+ "(default 0 = the whole patch range, max_image_tokens * 4 = 32000 on "+ "GLM-5.3, which is the expensive end)")+ ap.add_argument("--only", choices=("all", "vision", "lm"), default="all")+ ap.add_argument("--vision-parts", choices=("all", "convs", "linears"), default="all",+ help="which half of the vision tower to prime. The convolutions are 1-2 keys "+ "per row class and were the layer that made MIOpen slow; the block Linears "+ "are ~7 keys per class and only 0.2-3.3 s each, so leaving them lazy costs "+ "seconds per new image size instead of a long generation")+ ap.add_argument("--budget-s", type=float, default=0.0, help="stop after N seconds (0 = no limit)")+ ap.add_argument("--union-flags", action="store_true",+ help="also prime f32 = 1 for dense Linears (2x the keys; measured unnecessary)")+ ap.add_argument("--rescreen", action="store_true",+ help="ignore what the file already holds and screen every key again")+ ap.add_argument("--dry-run", action="store_true", help="list the plan, screen nothing")+ args = ap.parse_args()++ from exllamav3 import Config, Model++ path = Path(args.tune_file) if args.tune_file else default_tune_path()+ cfg = Config.from_directory(args.model)+ t0 = time.perf_counter()+ models: dict = {}+ for comp in (None, "mtp", "vision"):+ try:+ models[comp] = Model.from_config(cfg, **({} if comp is None else {"component": comp}))+ except Exception as exc: # noqa: BLE001+ print(f" ({comp or 'target'} component not built: {type(exc).__name__}: {exc})")+ rss = int(Path("/proc/self/statm").read_text().split()[1]) * 4096 / 2**30+ n_mod = sum(1 for m in models.values() for _ in m)+ print(f"built {n_mod} module objects in "+ f"{time.perf_counter() - t0:.2f} s, rss {rss:.2f} GiB (no weights loaded)")++ shapes: list[Shape] = []+ if args.only in ("all", "lm"):+ shapes += linear_shapes({k: v for k, v in models.items() if k in (None, "mtp")},+ args.lm_class, args.union_flags)+ if args.only in ("all", "vision") and models.get("vision") is not None:+ pp = getattr(cfg, "vision_pp", None)+ token_top = int(getattr(pp, "max_image_tokens", 8192) or 8192)+ patch_top = token_top * 4 # the flat patch embed runs one row per patch+ if args.max_image:+ w, h = (int(x) for x in args.max_image.lower().replace("*", "x").split("x"))+ token_top, patch_top = vision_rows_for(cfg, w, h)+ print(f"max-image {w}x{h} -> vision rows: tokens {token_top} "+ f"(class {m_class(token_top)}), patches {patch_top} (class {m_class(patch_top)})")+ if args.vision_class:+ token_top = patch_top = args.vision_class+ if args.vision_parts in ("all", "convs"):+ shapes += vision_conv_shapes(cfg, models["vision"], token_top, patch_top)+ if args.vision_parts in ("all", "linears"):+ for m in models["vision"]:+ pass+ for sh in linear_shapes({"vision": models["vision"]}, patch_top, args.union_flags):+ top = token_top if ".merger" in sh.label else patch_top+ sh.classes = classes_up_to(top)+ shapes.append(sh)++ total = sum(len(s.keys()) for s in shapes)+ lm_keys = sum(len(s.keys()) for s in shapes if s.label.startswith(("target", "mtp")))+ print(f"shapes: {len(shapes)}, keys if nothing were cached: {total} "+ f"(language model {lm_keys}, vision {total - lm_keys})")+ print("cost: ~0.05-4 s per key, rising with the row class (measured here: the language model "+ "+ conv set, 893 keys, 1106 s; the vision rows 4352..8192, 189 keys, 668 s)")+ print("bound the job with --only lm / --only vision, --vision-class and --budget-s")+ print(f"tune file: {path}")+ have = [] if args.rescreen else read_keys(path)+ if args.dry_run:+ for s in sorted(shapes, key=lambda s: -len(s.keys())):+ print(f" n={s.n:<6} k={s.k:<6} f32={sorted(s.f32)} key4={sorted(s.key4)} "+ f"rows={s.classes[0]}..{s.classes[-1]} ({len(s.keys())} keys) <- {s.label}")+ print(f"\n{total} keys would be screened; nothing written (--dry-run)")+ return 0++ st = screen(shapes, have, args.budget_s)+ if st["screened"]:+ after = read_keys(path)+ print(f"\nscreened {st['screened']} keys in {st['seconds']:.0f} s "+ f"({st['seconds'] / max(st['screened'], 1) * 1000:.0f} ms/key)")+ print(f"tune file now holds {len(after)} lines; the next server start skips this screening")+ return 0+++if __name__ == "__main__":+ sys.exit(main())diff --git a/tools/strix_halo/prime_dense_tune.sh b/tools/strix_halo/prime_dense_tune.shnew file mode 100755index 0000000..ea0134b--- /dev/null+++ b/tools/strix_halo/prime_dense_tune.sh@@ -0,0 +1,23 @@+#!/usr/bin/env bash+# Prime the dense-GEMM tune cache before the first server start.+#+# Same environment as run_kyojin.sh (venv + ROCm SDK + strix_halo env), then run+# prime_dense_tune.py with every argument passed through.+#+# bash tools/strix_halo/prime_dense_tune.sh --model <pack dir> --dry-run+# bash tools/strix_halo/prime_dense_tune.sh --model <pack dir>+#+# EXL3_DENSE_GEMM_TUNE_FILE decides which file is written (default+# ~/.cache/exllamav3/dense_gemm_tune.txt, the same one the server reads).+set -eo pipefail++PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"+cd "$PROJECT_ROOT"++# shellcheck disable=SC1091+source "$PROJECT_ROOT/.venv/bin/activate"+export EXL3_ROCM_SDK="$(rocm-sdk path --root)"+# shellcheck disable=SC1091+source tools/strix_halo/env.sh++exec python tools/strix_halo/prime_dense_tune.py "$@"