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: the unset-switch default (4:128 at <= 5 bits) applies on ROCm only until the CUDA served A/B clears it; an explicit EXL3_KVWIN forces it on any backend

YanissAmzd5bda4855c78
diff --git a/doc/env_vars.md b/doc/env_vars.md
index 7888e72..f807b12 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 = `4:128` when k or v bits <= 5, off at 6 bits and above)
+### `EXL3_KVWIN` (default: unset = `4:128` when k or v bits <= 5 on ROCm, off on CUDA and at 6 bits and above)
-`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
+`sink:window`, e.g. `4:128`; `0` / `off` disables it, an explicit value forces it at any bit width and on any backend (the unset-switch default is ROCm-only until the CUDA served A/B clears it: `DEFAULT_ON_CUDA` in `cache/kvwin.py`). 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
diff --git a/exllamav3/cache/kvwin.py b/exllamav3/cache/kvwin.py
index 48b35ce..de3651f 100644
--- a/exllamav3/cache/kvwin.py
+++ b/exllamav3/cache/kvwin.py
@@ -14,7 +14,7 @@ as "cold": the ring is rebuilt from the dequantized pool the first time the job
"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.
+file. The default applies on ROCm only for now (DEFAULT_ON_CUDA). EXL3_KVWIN=S:W forces a window at any bit width; EXL3_KVWIN=0 / off turns it off.
"""
from __future__ import annotations
import os
@@ -52,6 +52,11 @@ def parse_spec(spec: str | None) -> KVWinCfg | None:
DEFAULT_SPEC = "4:128"
DEFAULT_MAX_BITS = 5 # unset switch: the window is on when k_bits or v_bits <= this
+DEFAULT_ON_CUDA = False # the unset-switch default applies on ROCm only until the CUDA served A/B clears it (EXL3_KVWIN=S:W still forces it)
+
+
+def default_platform() -> bool:
+ return DEFAULT_ON_CUDA or bool(torch.version.hip)
def env_capable() -> bool:
@@ -65,7 +70,7 @@ def env_cfg(k_bits: int | None = None, v_bits: int | None = None) -> KVWinCfg |
"""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:
+ if k_bits is None or v_bits is None or min(k_bits, v_bits) > DEFAULT_MAX_BITS or not default_platform():
return None
spec = DEFAULT_SPEC
return parse_spec(spec)
diff --git a/tests/test_kvwin.py b/tests/test_kvwin.py
index 2ae114a..7eb374b 100644
--- a/tests/test_kvwin.py
+++ b/tests/test_kvwin.py
@@ -505,7 +505,8 @@ def test_ring_inert_when_off_or_unsupported(monkeypatch):
])
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
+ from exllamav3.cache import Cache, kvwin
+ monkeypatch.setattr(kvwin, "default_platform", lambda: True)
if env is None: monkeypatch.delenv("EXL3_KVWIN", raising = False)
else: monkeypatch.setenv("EXL3_KVWIN", env)
model = _FakeModel()
@@ -514,6 +515,19 @@ 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, hip, expect", [(None, False, None), (None, True, "4:128"), ("4:128", False, "4:128"), ("0", True, None)])
+def test_default_is_rocm_only_until_cuda_is_cleared(monkeypatch, env, hip, expect):
+ """CUDA keeps the window off by default (DEFAULT_ON_CUDA = False); an explicit EXL3_KVWIN forces it on any backend."""
+ from exllamav3.cache import Cache, kvwin
+ monkeypatch.setattr(kvwin, "DEFAULT_ON_CUDA", False)
+ monkeypatch.setattr(kvwin.torch.version, "hip", "7.0" if hip else None)
+ 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 = 4, v_bits = 4)
+ assert model.attn.recurrent_layers[0].cfg == (parse_spec(expect) if expect else None)
+
+
@pytest.mark.parametrize("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."""