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.

autotune: ship the ROCm dense-GEMM tune lines with the pack cache and merge them at serve start

YanissAmz16dcb31c37e9
diff --git a/exllamav3/util/tune_cache.py b/exllamav3/util/tune_cache.py
index cc21460..4d39cf1 100644
--- a/exllamav3/util/tune_cache.py
+++ b/exllamav3/util/tune_cache.py
@@ -6,7 +6,8 @@ The cooperative GEMM autotuner (exllamav3_ext/quant/coop_autotune.cu) times its
The qualify autotune stage runs the tuner for every shape of a pack ahead of time and ships the winners next to the pack:
<pack>/tuning/<signature>.bin same record format as the live cache (32-byte records, header EX3ATUNE)
- <pack>/tuning/<signature>.json manifest: device, shapes, per-shape timings, engine version
+ <pack>/tuning/<signature>.json manifest: device, sweep, timings, engine version
+ <pack>/tuning/<signature>.dense.txt ROCm only: dense-GEMM rocBLAS tune lines (EXL3_DENSE_GEMM_TUNE_FILE format)
install_pack(model_dir) merges the file whose signature matches this device into the live cache before the first GEMM
(records already present win; nothing is overwritten). A record is validated against the live candidates by the C++ loader
@@ -109,16 +110,41 @@ def merge_into(dst, srcs) -> int:
return len(new)
+def dense_live_path() -> Path:
+ """ROCm dense-GEMM tune file (hgemm.cu dtune): EXL3_DENSE_GEMM_TUNE_FILE or ~/.cache/exllamav3/dense_gemm_tune.txt."""
+ o = os.environ.get("EXL3_DENSE_GEMM_TUNE_FILE")
+ return Path(o) if o else Path(os.environ.get("HOME", "/tmp")) / ".cache/exllamav3/dense_gemm_tune.txt"
+
+
+def merge_dense_text(dst, src) -> int:
+ """Append the lines of src (7 fields: key = first 6) whose key dst lacks. Same rule as dense_warmup.seed_dense_tune."""
+ key = lambda ln: tuple(ln.split()[:6])
+ dst, src = Path(dst), Path(src)
+ have = {key(l) for l in dst.read_text().splitlines() if len(l.split()) == 7} if dst.exists() else set()
+ new = [l for l in src.read_text().splitlines() if len(l.split()) == 7 and key(l) not in have]
+ if new:
+ dst.parent.mkdir(parents=True, exist_ok=True)
+ with open(dst, "a") as f:
+ f.write("\n".join(new) + "\n")
+ return len(new)
+
+
def install_pack(model_dir, log=print) -> int:
"""Call before the first GEMM of the process (the C++ cache is read once, lazily). Returns records added to the live cache."""
if os.environ.get("EXL3_PACK_TUNE", "1") == "0":
return 0
try:
- files = pack_files(model_dir)
- if not files:
- return 0
- n = merge_into(live_cache_path(), files)
- log(f"tune cache: {n} records from {files[0].name} merged into {live_cache_path()}")
+ sig = signature()
+ n = 0
+ files = pack_files(model_dir, sig)
+ if files:
+ n = merge_into(live_cache_path(), files)
+ log(f"tune cache: {n} records from {files[0].name} merged into {live_cache_path()}")
+ dense = Path(model_dir) / TUNING_DIR / f"{sig['file']}.dense.txt" # ROCm rocBLAS solution per dense shape class
+ if dense.is_file():
+ m = merge_dense_text(dense_live_path(), dense)
+ log(f"tune cache: {m} dense-GEMM lines from {dense.name} merged into {dense_live_path()}")
+ n += m
return n
except Exception as e: # noqa: BLE001 a bad tuning file must never stop a serve start
log(f"tune cache: pack cache ignored ({e})")
diff --git a/tools/qualify/stages.py b/tools/qualify/stages.py
index f648d0a..dae1815 100644
--- a/tools/qualify/stages.py
+++ b/tools/qualify/stages.py
@@ -108,7 +108,8 @@ def job_autotune(c: Ctx) -> dict:
shutil.rmtree(scratch, ignore_errors=True); scratch.mkdir(parents=True)
sessions = max(c.args["sessions"])
ctx = min(c.args.get("ctx_cap") or 16384, 16384, c.meta["max_pos"])
- prof = {**c.prof, "env": {**c.prof["env"], "EXLLAMAV3_TUNE_CACHE": str(scratch), "EXL3_PACK_TUNE": "0"}}
+ prof = {**c.prof, "env": {**c.prof["env"], "EXLLAMAV3_TUNE_CACHE": str(scratch), "EXL3_PACK_TUNE": "0",
+ "EXL3_DENSE_GEMM_TUNE_FILE": str(scratch / "dense_gemm_tune.txt")}}
k, v = chosen_kv(c)
argv = packmod.serve_flags(c.meta, port=c.args["port"], ctx=ctx, sessions=sessions, k_bits=k, v_bits=v)
log = c.out / "srv_autotune.log"
@@ -127,6 +128,9 @@ def job_autotune(c: Ctx) -> dict:
if not recs:
raise RuntimeError("autotune: the replay produced no tuning records (does this build have the GEMM autotuner?)")
tdir.mkdir(exist_ok=True)
+ dense = scratch / "dense_gemm_tune.txt"
+ if dense.is_file() and dense.stat().st_size:
+ shutil.copy(dense, tdir / f"{sig['file']}.dense.txt")
tc.write_records(tdir / f"{sig['file']}.bin", recs)
man = dict(schema=1, signature=sig, records=len(recs), records_at_load=at_load, load_s=round(load_s, 1), wall_s=round(time.time() - t0, 1),
sessions=sessions, ctx=ctx, sweep=sweep, cmd=argv, engine_git=_git(c.repo), pack_index_sha=c.meta.get("index_sha"))
diff --git a/tools/qualify/tests/test_autotune.py b/tools/qualify/tests/test_autotune.py
index 06d731f..5fad6dd 100644
--- a/tools/qualify/tests/test_autotune.py
+++ b/tools/qualify/tests/test_autotune.py
@@ -133,3 +133,14 @@ class TestReplay(unittest.TestCase):
self.assertTrue(all(r["ok"] for r in res), [r for r in res if not r["ok"]][:2])
finally:
p.terminate(); p.wait(10)
+
+
+class TestDense(unittest.TestCase):
+ def test_dense_text_merge_keeps_live_lines(self):
+ with tempfile.TemporaryDirectory() as d:
+ d = Path(d)
+ (d / "live.txt").write_text("1 2 3 4 5 6 7\n")
+ (d / "pack.txt").write_text("1 2 3 4 5 6 9\n7 7 7 7 7 7 7\nbad line\n")
+ self.assertEqual(tc.merge_dense_text(d / "live.txt", d / "pack.txt"), 1)
+ self.assertEqual((d / "live.txt").read_text().splitlines(), ["1 2 3 4 5 6 7", "7 7 7 7 7 7 7"])
+ self.assertEqual(tc.merge_dense_text(d / "new.txt", d / "pack.txt"), 2)