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.

qualify: a tq format must beat k6v6's KL by 10 % (one doc is noisy); kv_tq.md: CUDA decode, measured results

YanissAmzf37536cfb0f6
diff --git a/doc/kv_tq.md b/doc/kv_tq.md
index 70a3097..cba8c3d 100644
--- a/doc/kv_tq.md
+++ b/doc/kv_tq.md
@@ -24,8 +24,28 @@ The default (`--kv-mode quant` or nothing) is unchanged. A pack that went throug
head vector. `copy_page`, save / restore, the prefix disk cache and the KVWIN stash move the same four tensors.
* `R` is orthonormal, so `q . k = (R q) . (R k)` and `o = R^T (sum p (R v))`: `attn_dispatch` rotates q, k and v on the way in and the
output on the way out, and the pool, the ring and every kernel work in the rotated domain (one fused Triton launch for q, k, v).
-* Writer: `tq_triton._tq_write_kernel` (Triton, ROCm + CUDA). Decode: `qdec.hip` with `-DTQ=1` (codebook in LDS, one scale per head) on
- ROCm; the Triton split kernel (`_qc_load_kt/v`, `TQ`) elsewhere. Prefill: the existing staging, with `tq_dequant_window` filling the fp16
- scratch (rotated domain) before the fp16 attention kernel. The BC graph attention declines tq layers.
+* Writer: `tq_triton._tq_write_kernel` (Triton, ROCm + CUDA). Decode: `qdec.hip` / `qdec.cu` with `-DTQ=1` (one scale per head vector, KVWIN ring on
+ the same path); the Triton split kernel (`_qc_load_kt/v`, `TQ`) for more than 32 q rows or an odd kv-head count. On CUDA the codebook is a
+ shared-memory table of code *pairs* (32-bit entries, 16 KB per side at 6 bits, 4 KB at 5), so a dequantized register is one LDS, and the
+ per-vector scale is folded into the scores (K) and the probabilities (V) instead of multiplying every dequantized pair. Prefill: the
+ existing staging, with `tq_dequant_window` filling the fp16 scratch (rotated domain) before the fp16 attention kernel. The BC graph
+ attention declines tq layers.
* `tools/qualify` (kv stage) measures the decode-faithful 32K KL against k8v8 of k6v6, tq6 and tq5 once per model and keeps the cheapest
- format whose KL is <= k6v6's (else k6v6); `--no-kv-modes` skips it.
+ format whose KL is <= 0.9 x k6v6's (a 256-step doc scatters 10-30 %: a tie stays on k6v6), and a narrower format only if every wider tq
+ format qualifies too; `--no-kv-modes` skips it.
+
+## Measured (Qwen3.8-27B EXL3, RTX 3090, graphs on, drafts off, decode tok/s, median of 3)
+
+| context | k6v6 | tq6 | tq5 |
+|---|---|---|---|
+| 4K | 46.18 | 45.28 (-1.9 %) | 45.82 (-0.8 %) |
+| 32K | 44.12 | 43.05 (-2.4 %) | 42.50 (-3.7 %) |
+| 120K | 38.33 | 37.55 (-2.0 %) | 37.18 (-3.0 %) |
+
+Kernel only (one layer, 128K, qdec_split): k6v6 271 us, tq6 268 us (306.8 before the pair table and the scale folding), tq5 263 us. The
+rest of the gap is the q/k/v rotation and the writer (about 12 us per layer). TQ is opt-in on CUDA. 32K KL vs k8v8 (Qwen27, two prompts):
+k6v6 .00062 / .043, tq6 .00060 / .026, tq5 .00115 / .100.
+
+Nanbeige (Llama arch, no recurrent slots): the sink/window ring now binds there too (KVWinState). 32K KL vs k8v8, p1 / p2: k6v6 (production, no
+window at 6 bits) .0536 / .0156, tq6 .0319 / .0088, tq5 .0544 / .0182, **k6v6 + the same ring .0216 / .0084**. Most of tq's gain over the
+production k6v6 on that model is the ring, not the codebook; k6v6 + ring is as good in KL and a little faster to decode (0.0309 vs 0.0318 s/tok).
diff --git a/tools/qualify/rules.py b/tools/qualify/rules.py
index 8df7702..31397e0 100644
--- a/tools/qualify/rules.py
+++ b/tools/qualify/rules.py
@@ -16,10 +16,13 @@ def choose_kv(kl_vs_k8: dict[int, float], kl_max: float = KV_KL_MAX) -> tuple[in
return 8, 8
-def choose_kv_mode(kl: dict[str, float], bits_per_val: dict[str, float], base: str = "k6v6") -> str:
- """Q6 rule for the KV format: any candidate (e.g. tq6, tq5) whose decode-faithful KL vs k8v8 is <= the k6v6 baseline's qualifies;
+KV_MODE_MARGIN = 0.9 # one 256-step doc is noisy (two docs of one model scatter by 10-30 %): a candidate has to beat k6v6 by this factor
+
+
+def choose_kv_mode(kl: dict[str, float], bits_per_val: dict[str, float], base: str = "k6v6", margin: float = KV_MODE_MARGIN) -> str:
+ """Q6 rule for the KV format: any candidate (e.g. tq6, tq5) whose decode-faithful KL vs k8v8 is <= margin x the k6v6 baseline's qualifies;
the one with the fewest bytes per value wins (ties: lower KL), else k6v6. kl / bits_per_val: {format: value}, both holding `base`."""
- ok = [n for n in kl if n != base and n in bits_per_val and kl[n] <= kl[base]]
+ ok = [n for n in kl if n != base and n in bits_per_val and kl[n] <= margin * kl[base]]
# sanity: more bits can only help, so a format with fewer bits that beats the baseline while a wider tq format does not is measurement noise
ok = [n for n in ok if all(m in ok for m in kl if m != base and bits_per_val.get(m, 0) > bits_per_val[n])]
return min(ok, key=lambda n: (bits_per_val[n], kl[n])) if ok else base
diff --git a/tools/qualify/stages.py b/tools/qualify/stages.py
index 09317b9..732195e 100644
--- a/tools/qualify/stages.py
+++ b/tools/qualify/stages.py
@@ -224,7 +224,7 @@ def job_kv(c: Ctx) -> dict:
def job_kvmode(c: Ctx, docs) -> dict:
"""KV format auto-select, ONCE per model: decode-faithful 32K KL vs k8v8 of k6v6 (quant), tq6, tq5 (TurboQuant + sink/window ring).
- Q6 rule (rules.choose_kv_mode): any tq format with KL <= k6v6's, then the fewest bytes per value; else k6v6."""
+ Q6 rule (rules.choose_kv_mode): any tq format with KL <= 0.9 x k6v6's, then the fewest bytes per value; else k6v6."""
with c.locks():
c.py("gpu_eval.py", "kvmode", c.meta["path"], docs, c.out / "kvmode_raw.json", c.args.get("kvmode_steps", 256), timeout=14400)
raw = json.loads((c.out / "kvmode_raw.json").read_text())
diff --git a/tools/qualify/tests/test_plan.py b/tools/qualify/tests/test_plan.py
index 71f7f4e..8f64b93 100644
--- a/tools/qualify/tests/test_plan.py
+++ b/tools/qualify/tests/test_plan.py
@@ -239,7 +239,7 @@ class CliTests(unittest.TestCase):
class KvModeRule(unittest.TestCase):
def test_choose_kv_mode(self):
bpv = {"k6v6": 6.5, "tq6": 6.0625, "tq5": 5.0625}
- # tq5 and tq6 both at or below k6v6's KL: the cheaper one wins
+ # tq5 and tq6 both within the margin (<= 0.9 x k6v6 KL): the cheaper one wins
self.assertEqual(rules.choose_kv_mode({"k6v6": 0.010, "tq6": 0.004, "tq5": 0.009}, bpv), "tq5")
# only tq6 qualifies
self.assertEqual(rules.choose_kv_mode({"k6v6": 0.010, "tq6": 0.008, "tq5": 0.020}, bpv), "tq6")
@@ -249,6 +249,8 @@ class KvModeRule(unittest.TestCase):
self.assertEqual(rules.choose_kv_mode({"k6v6": 0.002, "tq6": 0.004, "tq5": 0.010}, bpv), "k6v6")
# a model without tq support (head_dim not a power of two): only the baseline was measured
self.assertEqual(rules.choose_kv_mode({"k6v6": 0.002}, {"k6v6": 6.5}), "k6v6")
+ # 3090 Nanbeige doc: tq5 .00456 vs k6v6 .00495 is inside the noise, only tq6 clearly beats the baseline
+ self.assertEqual(rules.choose_kv_mode({"k6v6": 0.00495, "tq6": 0.00251, "tq5": 0.00456}, bpv), "tq6")
def test_job_kvmode_glue(self):
"""kv stage -> job_kvmode -> kv_cache.json: the winner is written as the lane preset, serve flags follow it"""