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: attention declares the ring state at build time unless the switch is off (the cache bits are unknown then), so the default-on policy and an EXL3_KVWIN set after startup of the cache take effect; QSA cache layers decline the ring (their sparse decode does not read it)

YanissAmzaf41d71f7fd8
diff --git a/exllamav3/cache/kvwin.py b/exllamav3/cache/kvwin.py
index f44d08e..4798689 100644
--- a/exllamav3/cache/kvwin.py
+++ b/exllamav3/cache/kvwin.py
@@ -54,6 +54,13 @@ DEFAULT_SPEC = "4:128"
DEFAULT_MAX_BITS = 5 # unset switch: the window is on when k_bits or v_bits <= this
+def env_capable() -> bool:
+ """Attention modules are built before any cache exists, so they cannot know the cache bits yet: unless the switch
+ is off, they declare the ring state (inert until a cache layer binds it, see KVWinLayerState.bind)."""
+ spec = os.environ.get("EXL3_KVWIN")
+ return spec is None or parse_spec(spec) is not None
+
+
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")
@@ -121,9 +128,11 @@ class KVWinLayerState:
"""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
+ from .qsa import QSAPlanes
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
+ if (cfg is None or not isinstance(cache_layer, CacheLayer_quant) or isinstance(cache_layer, QSAPlanes) 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):
return
self.cfg = cfg
diff --git a/exllamav3/modules/attn.py b/exllamav3/modules/attn.py
index d182090..e330196 100644
--- a/exllamav3/modules/attn.py
+++ b/exllamav3/modules/attn.py
@@ -43,7 +43,7 @@ def _sim_kvq_inplace(t: torch.Tensor, bits: int | None, compand_a: float):
from ..util.tensor import g_tensor_cache
from .attention_fn import attn_dispatch
-from ..cache.kvwin import env_cfg as _kvwin_cfg, make_ctx as _kvwin_ctx, ensure_filled as _kvwin_fill, KVWinLayerState
+from ..cache.kvwin import env_capable as _kvwin_capable, make_ctx as _kvwin_ctx, ensure_filled as _kvwin_fill, KVWinLayerState
"""
@@ -449,7 +449,7 @@ class Attention(Module):
# KVWIN (EXL3_KVWIN=sink:window): the fp16 sink + window ring of a quantized cache is a per-job recurrent layer state
self.recurrent_layers = []
- if _kvwin_cfg() is not None and self.num_kv_heads > 0:
+ if _kvwin_capable() and self.num_kv_heads > 0:
self.caps["recurrent_cache"] = True
self.layer_state_cls = KVWinLayerState
diff --git a/tests/test_kvwin.py b/tests/test_kvwin.py
index 4a049ed..0b11c7c 100644
--- a/tests/test_kvwin.py
+++ b/tests/test_kvwin.py
@@ -514,6 +514,27 @@ def test_default_on_for_q5_and_below(monkeypatch, env, bits, expect):
assert st.cfg == (parse_spec(expect) if expect else None)
[email protected]("env, capable", [(None, True), ("4:128", True), ("2:64", True), ("0", False), ("off", False), ("", False)])
+def test_attention_declares_ring_state_unless_switched_off(monkeypatch, env, capable):
+ """Attention modules are built before the cache (and its bits) exist: unless the switch is off they declare the ring state."""
+ from exllamav3.cache.kvwin import env_capable
+ if env is None: monkeypatch.delenv("EXL3_KVWIN", raising = False)
+ else: monkeypatch.setenv("EXL3_KVWIN", env)
+ assert env_capable() is capable
+
+
+def test_ring_declines_qsa_caches(monkeypatch):
+ """The sparse (QSA) decode path does not read the ring: its cache layers must not bind one."""
+ from exllamav3.cache.qsa import CacheLayer_qsa_quant
+ monkeypatch.setenv("EXL3_KVWIN", "4:32")
+ attn = types.SimpleNamespace(num_kv_heads = 2, head_dim = 128, qsa_indexer = types.SimpleNamespace(head_dim = 128, compress_ratio = 4),
+ window_arg = lambda: -1, logit_softcapping = 0.0)
+ cl = CacheLayer_qsa_quant(None, attn, 0, 8 * PAGE, 4, 4)
+ st = KVWinLayerState(attn, 2, 0, 0)
+ st.bind(cl)
+ assert st.cfg is None and getattr(cl, "kvwin", None) is None
+
+
def test_models_without_recurrent_state_drop_ring_layers():
from exllamav3.model.model import Model
a = _FakeAttn()