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.

KVWIN: on by default (4:128) when the k or v cache bits are <= 5, off at 6 and above; EXL3_KVWIN=0 opts out, an explicit value forces it

YanissAmz208c7cde4783
diff --git a/doc/env_vars.md b/doc/env_vars.md
index ba4820d..7888e72 100644
--- a/doc/env_vars.md
+++ b/doc/env_vars.md
@@ -157,9 +157,9 @@ Pipeline stage count for the direct quantized-cache prefill kernel. Unset/`0`, t
skipping the measurement. Only relevant where the direct path still runs (`EXL3_QC_STAGING=0`,
or short chunks below the threshold above).
-### `EXL3_KVWIN` (default: unset = off)
+### `EXL3_KVWIN` (default: unset = `4:128` when k or v bits <= 5, off at 6 bits and above)
-`sink:window`, e.g. `4:128`. Keeps the first `sink` positions and the last `window` positions of every job in fp16
+`sink:window`, e.g. `4:128`; `0` / `off` disables it, an explicit value forces it at any bit width. Keeps the first `sink` positions and the last `window` positions of every job in fp16
next to a quantized K/V cache (`--k-bits/--v-bits`): attention reads the quantized pool for everything in between
and a small fp16 ring (`sink + window + 16` rows per layer and job) for the rest, merged exactly by the flash-decoding
combine. Costs about 10 MiB of VRAM per job slot at `4:128` on a 16-layer, 4-kv-head, head-dim-256 model. Applies to
@@ -169,7 +169,7 @@ bit-exact (a checkpoint written without the switch rebuilds the ring from the po
first `window` tokens). While it is on, the graph-captured decode attention (`EXL3_BC_ATTN`) and the whole-forward
graph (`EXL3_CUDA_GRAPH`) decline, and tensor-parallel loading is not supported. Prefill chunks attend the exact
sinks, the exact window before the chunk and all of the chunk's own rows (a superset of the per-row decode window).
-Unset or `0` leaves every code path bit-identical.
+With the window off (`0`, or unset at >= 6 bits) every code path is bit-identical to a build without it.
### `EXL3_MLA_PREFILL` (default: `mha`)
diff --git a/exllamav3/cache/kvwin.py b/exllamav3/cache/kvwin.py
index f4261f9..f44d08e 100644
--- a/exllamav3/cache/kvwin.py
+++ b/exllamav3/cache/kvwin.py
@@ -12,8 +12,9 @@ The ring is per-job state with the lifecycle of the job's recurrent state: it ri
prefix disk) and restored bitwise with them. A stash without a ring blob (written with the switch off) restores
as "cold": the ring is rebuilt from the dequantized pool the first time the job attends (fill).
-EXL3_KVWIN=S:W (e.g. 4:128) turns it on for CacheLayer_quant layers of models that have recurrent state slots;
-unset / 0 / empty = off, which leaves every code path bit-identical to a build without this file.
+"Optimized Q4": with EXL3_KVWIN unset the window (4:128) is ON for CacheLayer_quant layers whose k or v bits are <= 5
+(models that have recurrent state slots) and OFF at >= 6, where every code path is bit-identical to a build without this
+file. EXL3_KVWIN=S:W forces a window at any bit width; EXL3_KVWIN=0 / off turns it off.
"""
from __future__ import annotations
import os
@@ -49,8 +50,18 @@ def parse_spec(spec: str | None) -> KVWinCfg | None:
return KVWinCfg(S, W, W + MAX_DRAFT, S + W + MAX_DRAFT)
-def env_cfg() -> KVWinCfg | None:
- return parse_spec(os.environ.get("EXL3_KVWIN"))
+DEFAULT_SPEC = "4:128"
+DEFAULT_MAX_BITS = 5 # unset switch: the window is on when k_bits or v_bits <= this
+
+
+def env_cfg(k_bits: int | None = None, v_bits: int | None = None) -> KVWinCfg | None:
+ """The EXL3_KVWIN switch; unset means DEFAULT_SPEC for a cache with k or v bits <= DEFAULT_MAX_BITS."""
+ spec = os.environ.get("EXL3_KVWIN")
+ if spec is None:
+ if k_bits is None or v_bits is None or min(k_bits, v_bits) > DEFAULT_MAX_BITS:
+ return None
+ spec = DEFAULT_SPEC
+ return parse_spec(spec)
def ring_rows(pos: torch.Tensor, cfg: KVWinCfg) -> torch.Tensor:
@@ -107,10 +118,10 @@ class KVWinLayerState:
# -- Cache.__init__ hook ---------------------------------------------------------------------------------------
def bind(self, cache_layer):
- """Called once the cache layer of this attention exists. Inert unless EXL3_KVWIN is set and the layer is a
- plain quantized cache."""
+ """Called once the cache layer of this attention exists. Inert when the switch is off (see env_cfg) or the layer
+ is not a plain quantized cache."""
from .quant import CacheLayer_quant
- cfg = env_cfg()
+ cfg = env_cfg(getattr(cache_layer, "k_bits", None), getattr(cache_layer, "v_bits", None))
m = self.module
if (cfg is None or not isinstance(cache_layer, CacheLayer_quant) or cache_layer.compand_a != 0.0 or
m.head_dim % 32 != 0 or m.window_arg() not in (None, -1, (-1, -1)) or m.logit_softcapping):
diff --git a/tests/test_kvwin.py b/tests/test_kvwin.py
index 5cb00d0..4a049ed 100644
--- a/tests/test_kvwin.py
+++ b/tests/test_kvwin.py
@@ -480,7 +480,7 @@ def test_cache_binds_ring_to_recurrent_state_lifecycle(monkeypatch):
def test_ring_inert_when_off_or_unsupported(monkeypatch):
from exllamav3.cache import Cache
- monkeypatch.delenv("EXL3_KVWIN", raising = False)
+ monkeypatch.setenv("EXL3_KVWIN", "0")
model = _FakeModel()
cache = Cache(model, 8 * PAGE, layer_type = CacheLayer_quant, max_batch_size = 2, k_bits = 4, v_bits = 4)
st = model.attn.recurrent_layers[0]
@@ -497,6 +497,23 @@ def test_ring_inert_when_off_or_unsupported(monkeypatch):
assert model2.attn.recurrent_layers[0].cfg is None
[email protected]("env, bits, expect", [
+ (None, (4, 4), "4:128"), (None, (5, 5), "4:128"), (None, (6, 4), "4:128"), (None, (4, 8), "4:128"),
+ (None, (6, 6), None), (None, (8, 8), None),
+ ("0", (4, 4), None), ("off", (5, 5), None), ("", (4, 4), None),
+ ("2:64", (6, 6), "2:64"), ("2:64", (4, 4), "2:64"),
+])
+def test_default_on_for_q5_and_below(monkeypatch, env, bits, expect):
+ """Optimized Q4: unset switch = 4:128 when k or v bits <= 5, off (no ring, today's paths) at >= 6; explicit values win."""
+ from exllamav3.cache import Cache
+ if env is None: monkeypatch.delenv("EXL3_KVWIN", raising = False)
+ else: monkeypatch.setenv("EXL3_KVWIN", env)
+ model = _FakeModel()
+ Cache(model, 8 * PAGE, layer_type = CacheLayer_quant, max_batch_size = 2, k_bits = bits[0], v_bits = bits[1])
+ st = model.attn.recurrent_layers[0]
+ assert st.cfg == (parse_spec(expect) if expect else None)
+
+
def test_models_without_recurrent_state_drop_ring_layers():
from exllamav3.model.model import Model
a = _FakeAttn()