[PR #4068] [Engine] KV cache: the exact-cell window for q4lm8v (opt-in, hardened) #4896

Open
opened 2026-11-07 16:31:10 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/4068
Author: @monscoro
Created: 10/25/2026
Status: 🔄 Open

Base: main ← Head: kv-window-0.2


📝 Commits (10+)

  • 38ea4a0 KV: 4-bit Lloyd KV with an exact-cell window (opt-in)
  • 7af3ab5 KV window: the free ring caps the auto chunk scan at 6,144 cells too
  • fd97ade KV window: --kv-resident and the exact-cell window refuse each other
  • fd3fc13 KV window: freeze the grid after the prompt chunk settles (A3-lite)
  • 1e4933e KV window: tag the empty ring rows with 0xFF (A5)
  • 0497be1 KV cache: --kv-check helper and a DETAILS.md section
  • d5a337a KV cache: document the quantization choices and the window method
  • 955ceb6 docs: add the KV-window hardening paper (two-column LaTeX)
  • f3eb28b docs: revise the KV-window paper (related work, survey table, design alternatives, discussion, provenance; 8 pp)
  • ca1bca0 docs: needle A/B evidence for the window (int8 against 256/64)

📊 Changes

39 files changed (+2652 additions, -157 deletions)

View changed files

➕ bench/results/2026-10-24-needle-window/README.md (+50 -0)
➕ bench/results/2026-10-24-needle-window/cfg-needle-int8.json (+37 -0)
➕ bench/results/2026-10-24-needle-window/cfg-needle-win.json (+40 -0)
➕ bench/results/2026-10-24-needle-window/needle-int8.engine.txt (+100 -0)
➕ bench/results/2026-10-24-needle-window/needle-win.engine.txt (+101 -0)
➕ bench/results/2026-10-24-needle-window/needle_ab.py (+83 -0)
➕ bench/results/2026-10-24-needle-window/needles-int8.json (+56 -0)
➕ bench/results/2026-10-24-needle-window/needles-win.json (+56 -0)
📝 docs/AMD_HIP.md (+4 -0)
📝 docs/DETAILS.md (+38 -0)
➕ docs/paper/KV-Window-Hardening.pdf (+0 -0)
➕ docs/paper/KV-Window-Hardening.tex (+493 -0)
📝 include/strata/core/layer.hpp (+62 -1)
📝 include/strata/kernels/kv_q4.hpp (+118 -5)
📝 include/strata/kernels/kv_stream.hpp (+25 -1)
📝 include/strata/kernels/qsa_decode_attn.hpp (+62 -0)
📝 setup.py (+241 -14)
📝 src/core/conversation_snapshot.cpp (+9 -0)
📝 src/core/layer.cpp (+163 -4)
📝 src/core/mtp.cpp (+28 -4)

...and 19 more files

📄 Description

[Engine] KV cache: the exact-cell window for q4lm8v (opt-in, hardened)

Status: opt-in, experimental-adjacent. --kv q4lm8v + STRATA_KV_WINDOW=1 (shape S=256, R=64,
--prefill auto capped at B=6,144) is the only way to reach it; the default int8 path is byte-identical
to v0.2.4.2 across every run in this PR's evidence set (dump-logits SHA A014F504...BA052, 8/8 runs).
Tested on AMD HIP (RX 9060 XT, Windows, ROCm 7.1). CUDA and SYCL builds were not run — no suitable
hardware here; a build check is requested in this thread (see Validation → Not run).

Base: 011c9e60 (the 0.1.47 line). As of 2026-10-25 the branch is 414 commits behind main; a trial
merge with today's main conflicts only in setup.py (everything else auto-merges). Not rebased before
the scope question: the byte-identity evidence is tied to this base, and a rebase would need the int8
dump-logits re-run.

What this is, and what it is not

A basis, not the finished answer. It makes one KV mode safe, measurable and opt-in — the exact-cell
window on 4-bit Lloyd K/V — and it fixes the four bugs that shape exposed (A1–A5). What it enables is the
next step: a bug-corrected, more aggressive KV quantization that is checked for correctness instead of
tuned by feel. The levers that remain are named, not built: per-channel K before the rotation, lower-bit V,
a parity test over the whole cell→row contract, and the format-agnostic overlay of #2318 (see Follow-up).

And the KV cache is only one building block of the footprint problem. On this model the expert cache, the
PLE table and the dense weights are where the rest of it sits; the numbers in this PR are one card's
(RX 9060 XT 16 GB) on one quantization (IQ3_XXS).

Problem and result

int8 KV costs 8.25 bit/cell: 1,584 MiB at 128K context. On a 16 GB card that is the difference between
fitting the expert cache the model wants and paging. The exact-cell window is the memory/accuracy answer:
quantize the KV bulk with 4-bit Lloyd (K and V), keep the most recent cells (S=256) and the first cells
(sinks, R=64) exact in a small resident side pool. Measured result: 5.29 bit/cell, 1,016 MiB at 128K
(RX 9060 XT, Windows HIP), with the 128K-tail teacher-forced KL better than int8's: window 0.00663
against int8's 0.00959 and plain q4lm8v's 0.01587 (KL vs the fp16 reference, last 4,096 of 131,062
positions, measurement of the feature commit).

This PR ships the feature together with the hardening it needed to be safe (A1–A5 below), a --kv-check
helper, and the documentation. Every window-affecting line is gated behind qsa_kv_win() /
st.k_win != nullptr; the default path is untouched (the byte-identity SHA is the regression gate, run
8 times old-vs-new).

  • Scope is the question of this draft. CONTRIBUTING asks for an issue before a substantial PR; this
    comes as a draft instead, so the shape can be discussed on the diff itself. If you prefer the issue
    first, I will open one and follow it with this branch.
  • #2318 — the FP16 sliding-window + q2k PR, closed in the
    0.2.0-beta tracker reset ("closed does not mean rejected"). The maintainer's bar there — "It would need
    our KL and needle checks before it can be offered as an experimental option"
    — is the bar this PR is
    measured against: KL checks below, needle check under Validation. Differences to #2318 are in
    Related work at the end.
  • #3919 (STRATA_KV_RING_ADAPT, open) makes --kv-resident
    a cap for adaptive ring sizing. This PR's A2 refuses --kv-resident with the window before anything
    loads, so the two do not interact: RING_ADAPT only ever grows a resident ring, and a resident ring beside
    the window is exactly what A2 refuses. If #3919 merges first, nothing here changes; if this merges first,
    #3919 needs nothing either.

Approach

Why this shape. The window is a memory plan, not a tunable: its exact cells live in an always-resident
side pool, so the whole design follows from that one property. It cannot combine with KV streaming (the
streamed bulk has no resident pool to read the exact cells from — A2), the prompt chunk that sizes the ring
must never exceed the ring's scan limit (A1), every state carved later must read the same grid (A3), and an
empty ring slot must be distinguishable from cell 0 (A5). The four fixes are that shape's consequences, not
bolt-ons.

The fixes, one commit each, each with the default-path byte-identity proof:

commit what it fixes
A1 7af3ab59 The free ring caps the auto chunk scan at 6,144 cells. Before: kv_append_win: 8191 cells ... 6400-row ... ring, std::exit(1) mid-prompt. After: prompt chunk auto: 6144, DONE 8192.
A2 fd97ade2 --kv-resident (streaming) and the window refuse each other: the engine refuses the pair loudly (exit 2, before anything loads); setup never writes it (a q4lm8v config's KV stays in VRAM, streaming explained off). --kv-check reports the pair as NO - refused (exit 2).
A3-lite fd3fc135 The window grid (S/R/B) is frozen once the prompt chunk settles (qsa_kv_win_freeze()), so main/MTP-drafter/MTP-twin states cannot drift.
A5 1e4933e1 Empty ring tags init to 0xFF (-1) instead of 0, so an unwritten row can never be read as exact cell 0.

Plus 0497be1b (--kv-check helper + a docs/DETAILS.md KV section), d5a337a2 (documented numbers +
method note), 955ceb65 + 6acc91de (a two-column paper of the design and its decisions, and the same paper
after an independent desk review; docs/paper/KV-Window-Hardening.tex), ca1bca07 (the needle run's raw
JSON, configs, engine logs and runner under bench/results/), and 6c3bd969 (setup installs an engine built
from this tree:
--engine-dir DIR / STRATA_ENGINE_DIR, or the newest build-hip*/strata.exe by itself when q4lm8v is
chosen — no ready-made engine carries the mode, so without this the feature cannot be tried before a
release; the default download path is untouched).

Known limits of the design (why A3-lite and not a full state-layout refactor): the full A3 refactor is
held back until the follow-up parity test (below) exists; A3-lite freezes and asserts instead of moving
fields. An explicit small ring (STRATA_KV_WINDOW_BATCH) with --prefill above it still refuses by design
— the message names what to change.

Trying it (reviewer path)

No ready-made engine carries q4lm8v yet, so setup has to install one built from this branch. 6c3bd969
wires that in; without it a reviewer would have to package a release zip first.

  1. Build the engine: tools\hip\build_windows.bat on Windows (it packages dist\), or any build of this
    tree that left a build-hip*/strata.exe; Linux as in docs/AMD_HIP.md.
  2. START-HERE.bat --setup --kv q4lm8v — setup picks the newest build-hip*/strata.exe on its own and
    says q4lm8v is only in an engine built from this tree: using <folder> instead of the ready-made one.
    An explicit folder works too (--engine-dir <folder> or STRATA_ENGINE_DIR); only the Windows HIP path
    was run (see Not run). If no build is found, setup stops with what to do instead of downloading an
    engine that cannot run the mode.
  3. With pictures on, setup also installs the CPU image reader from this tree (copied from build-vision\bin,
    or compiled once); a self-built engine has no GPU reader unless one was built.
  4. Setup writes --kv q4lm8v and STRATA_KV_WINDOW=1 / _S=256 / _R=64 into the model's config.
    python setup.py --kv-check then prints mode, env, effective bits/cell and whether the engine would
    start; the engine log names the window (KV window ring sized for the prompt chunk (6144 cells)).
  5. --kv int8 (the default) never takes this path: no build folder is looked for and the ready-made engine
    is downloaded exactly as before.

UPDATE.bat replaces the engine folder with a ready-made one, so it is not part of this test.

Validation

Hardware for every run below: AMD Ryzen 9 3900XT, 64 GB DDR4-3000, RX 9060 XT 16 GB, Windows 11, ROCm 7.1,
Swift-Qwen3.8-Flash-Next IQ3_XXS. One GPU job at a time, quiet machine; timing numbers from noisy sessions
are not quoted. Reproduction commands live in the PR's commits and in docs/DETAILS.md.

Passed:

  • Default path byte identity: int8 dump-logits SHA A014F504BA315831DA1D13C6A0B3D23D6393B919872396C2A1464182710BA052
    (4,330,448 bytes, 4,095 positions) — 8/8 runs (4K A/B, rounds 1–4, old/new interleaved), identical
    to the pre-change binary.
  • Window path baselines (reference for future changes): 0768398A...94CA (8,191 rows, 8,192 ids,
    --prefill auto:16384), 4161376D...707E (4,095 rows, cwe-4k), 71B0B586...6463 (4,095 rows, set-4096).
  • KL (teacher-forced, STRATA_LOGPOS, quant_quality.py compare): window vs int8 on the same segment set
    KL 0.04524, top-1 98.00%; the feature commit's 128K-tail KL vs the fp16 reference: window 0.00663,
    int8 0.00959, plain q4lm8v 0.01587.
  • Former crash config (probe B): DONE 8192, chunk 6144, no refusal. Probe C (explicit ring 512 +
    --prefill 2048): refused as the contract says.
  • Refusal probes A/B/C: the --kv-resident + window pair refused in ~1 s (exit 2, message names the
    fix); window-alone and int8+streaming configs get past it.
  • tools/test_setup_choices.py: 56/56 pass (incl. the new A2 setup assertions).
  • Setup bridge (6c3bd969, Windows HIP, engine_step run directly with kv="q4lm8v"): installs
    build-hip-port\strata.exe into engine\ with BUILD.json "source": "local" and the version read
    from the binary, copies the CPU image reader from build-vision\bin (hip_reader → mode: cpu), and
    names a running model ("… is in use: a model is running from it") instead of a PermissionError.
  • Needle check (tools/needle_bench.py through serve/server.py, 32k and 128k prompt tokens at depths
    10/50/90, identical deterministic prompts for both arms, STRATA_PREFILL_CPU_SHARE=0): 6/6 found with
    int8, 6/6 found with the window
    — the code word was recalled at every depth at both lengths in both
    arms (31,643–31,644 and 126,015–126,017 prompt tokens). Raw data, configs and logs:
    bench/results/2026-10-24-needle-window/. Single run per arm, one model, repo-file haystack; wall-clock
    times are not quoted as a speed comparison (see the limits).

Failed: none observed.

User test (setup journey)

A Strata user set the branch up the way the project documents it, not as a developer: START-HERE.bat --setup --family swift --model IQ2_XS --gguf-dir <their own abliterated IQ2_XS shards> --port 7601, KV menu choice 3
(q4lm8v), 131K context, pictures on (CPU reader). The first attempt is what produced 6c3bd969: setup went
for the ready-made engine (404 for this version on the release page, then the signed update feed), which
cannot run the chosen KV mode, instead of a build of the branch. After the fix the same run installs the
local build and the CPU reader, writes --kv q4lm8v with STRATA_KV_WINDOW=1 / _S=256 / _R=64, and the
model starts and answers in chat. The engine's own status reports kv: q4lm8v, kv_resident: 0,
prefill_cap: 6144 (A1) and images: true, with 5.94 GiB of expert cache and 2.3 GiB VRAM free at 131K.

Covered: setup, start, short chat (IQ2_XS, RX 9060 XT 16 GB, Windows HIP). Not covered by this user test:
long-context work, pictures through an app, switching the same install back to int8, third-party app
wiring. A speed suite ran on the same install afterwards; it is a single arm without an int8 comparison
and is therefore not used as window evidence.

Not run:

  • CUDA (nvcc) and SYCL (dpcpp) builds — no suitable GPUs on the author's machine. The changes are
    backend-agnostic (globals, argument checks, setup Python, one memset value); SYCL does not offer
    q4lm8v at all (WINDOW_KV is False there), so the window paths are dead code on SYCL but still
    compile-checked there would be the ask. Requesting a targeted CUDA + SYCL build in this thread.
  • Layer-split/multi-GPU, parallel slots (>1), batch-MTP, Linux, NVIDIA hardware with the window enabled:
    reasoned about (the gating is per-session), not executed.
  • The setup bridge was run on Windows HIP only: an int8 install through the new code path was not re-run
    (the branch is entered only with --engine-dir/STRATA_ENGINE_DIR or q4lm8v), nor was --engine-dir
    with a CUDA build or on Linux.
  • docs/paper/KV-Window-Hardening.tex compiled by CI: the author's TeX build is local (MiKTeX); the file
    is documentation, no GPU build needed per CONTRIBUTING.

Limits

  • The window must stay resident: no streaming beside it (A2), and the expert cache yields the VRAM instead
    (setup's --kv-check prints the effective bits/cell so the plan is inspectable).
  • --prefill auto is capped at 6,144 cells in windowed runs (A1) — a deliberate contract, visible in the
    prompt chunk auto: line.
  • Quality is measured on one model (Swift-Qwen3.8-Flash-Next IQ3_XXS), one document family (cwe sets) and
    the repo-file haystack (needles). The KL numbers are single-model, single-document measurements.
  • The needle run's wall-clock times are not quoted: a second identical bench client overlapped on the same
    server during both arms (runner bug on the author's side), which queues the requests. Found/missed is
    unaffected — both clients asked the same deterministic prompts and every answer was found — but the
    seconds per request are not a clean speed measurement.
  • The full cell→row parity test does not exist yet (follow-up, below); the KL + needle checks cover the
    teacher-forced read path and long-context recall, not every ring-wrap/stale-tag/sink shape.
  • T ∈ [6145; 6400] with an explicit ring and --prefill above it still refuses (by design; the message
    names the remedy). SYCL ignores STRATA_KV_WINDOW silently (setup never writes it there).

Both PRs keep recent cells exact over a quantized bulk; they differ in what the exactness is made of and
where the device sits:

#2318 (bsorensen110) this PR
exact cells FP16 overlay pool (--kv-window N, --kv-sink S) on top of any format built into the format: q4lm8v = 4-bit Lloyd K+V + exact side pool (S=256/R=64)
bulk formats q4_0, q2, q2k (2-bit K per-channel + 4-bit V) q4lm8v only (opt-in; int8 untouched)
headline memory q2k 448 B/cell (1.31 GiB @262K) + 3.56–6.56 MiB overlay 5.29 bit/cell, 1,016 MiB @128K (overlay included)
quality evidence KL vs fp16 on 9 texts, needle-free; window 1024 regression unexplained KL vs fp16 @128K tail (window beats int8), KL vs int8 on cwe sets, needles 32k/128k (below)
known interaction exact window cells bias q2k's shrunk scores; fixed by --kv-k-gain not observed: the window measures better than int8 at the 128K tail (0.00663 vs 0.00959 vs the fp16 reference) — no gain knob needed at S=256/R=64

#2318's overlay is format-agnostic and its q2k format is the cheaper bulk; this PR's window is one format
whose measured point (5.29 bit @128K) sits between q4_0 (576 B/cell) and q2k (448 B/cell) while its 128K
tail KL beats int8. If both land, they compose differently and do not conflict: #2318's device would sit on
top of formats, this one is a format; nothing in either diff touches the other's paths.

Follow-up (not in this PR)

A window parity test over the whole cell→row contract: dump every window row at write time and re-read it
through qsa_win_slot on each shape (writer win_row_of kv_q4.cu:391 ↔ reader qsa_decode_attn.hpp:73,
host :93; ring wrap; stale-tag→quantized fallback; sink rows; batch-boundary shapes; the empty-tag case
A5 guards). The KL/needle checks above cover the read path, not these shapes; this is why A3 was shipped
as A3-lite. Meanwhile the window stays opt-in and the int8 default stays byte-identical.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/4068 **Author:** [@monscoro](https://github.com/monscoro) **Created:** 10/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `kv-window-0.2` --- ### 📝 Commits (10+) - [`38ea4a0`](https://github.com/Niko1221/Strata/commit/38ea4a03c8720283cbaacd3f702ece62aca5984f) KV: 4-bit Lloyd KV with an exact-cell window (opt-in) - [`7af3ab5`](https://github.com/Niko1221/Strata/commit/7af3ab593e63985ccd89dc2d17196a3f2ec6432b) KV window: the free ring caps the auto chunk scan at 6,144 cells too - [`fd97ade`](https://github.com/Niko1221/Strata/commit/fd97ade2ed550a50e7e3e94184fdf810c7969bd2) KV window: --kv-resident and the exact-cell window refuse each other - [`fd3fc13`](https://github.com/Niko1221/Strata/commit/fd3fc1355c03b941edde3a897c9a73992471915b) KV window: freeze the grid after the prompt chunk settles (A3-lite) - [`1e4933e`](https://github.com/Niko1221/Strata/commit/1e4933e1b71617d4aa460c4e634aff2979ced060) KV window: tag the empty ring rows with 0xFF (A5) - [`0497be1`](https://github.com/Niko1221/Strata/commit/0497be1bb1b5439672f339c258b867b03c6019bc) KV cache: --kv-check helper and a DETAILS.md section - [`d5a337a`](https://github.com/Niko1221/Strata/commit/d5a337a214896949776dfcb23132fff1758915ec) KV cache: document the quantization choices and the window method - [`955ceb6`](https://github.com/Niko1221/Strata/commit/955ceb65112903366821bfba8d99764d740542a2) docs: add the KV-window hardening paper (two-column LaTeX) - [`f3eb28b`](https://github.com/Niko1221/Strata/commit/f3eb28be4fee3bb8b2a2603beb2534d0fd2d013d) docs: revise the KV-window paper (related work, survey table, design alternatives, discussion, provenance; 8 pp) - [`ca1bca0`](https://github.com/Niko1221/Strata/commit/ca1bca07506e38802ca08d2ebe0a89791d5f5ca6) docs: needle A/B evidence for the window (int8 against 256/64) ### 📊 Changes **39 files changed** (+2652 additions, -157 deletions) <details> <summary>View changed files</summary> ➕ `bench/results/2026-10-24-needle-window/README.md` (+50 -0) ➕ `bench/results/2026-10-24-needle-window/cfg-needle-int8.json` (+37 -0) ➕ `bench/results/2026-10-24-needle-window/cfg-needle-win.json` (+40 -0) ➕ `bench/results/2026-10-24-needle-window/needle-int8.engine.txt` (+100 -0) ➕ `bench/results/2026-10-24-needle-window/needle-win.engine.txt` (+101 -0) ➕ `bench/results/2026-10-24-needle-window/needle_ab.py` (+83 -0) ➕ `bench/results/2026-10-24-needle-window/needles-int8.json` (+56 -0) ➕ `bench/results/2026-10-24-needle-window/needles-win.json` (+56 -0) 📝 `docs/AMD_HIP.md` (+4 -0) 📝 `docs/DETAILS.md` (+38 -0) ➕ `docs/paper/KV-Window-Hardening.pdf` (+0 -0) ➕ `docs/paper/KV-Window-Hardening.tex` (+493 -0) 📝 `include/strata/core/layer.hpp` (+62 -1) 📝 `include/strata/kernels/kv_q4.hpp` (+118 -5) 📝 `include/strata/kernels/kv_stream.hpp` (+25 -1) 📝 `include/strata/kernels/qsa_decode_attn.hpp` (+62 -0) 📝 `setup.py` (+241 -14) 📝 `src/core/conversation_snapshot.cpp` (+9 -0) 📝 `src/core/layer.cpp` (+163 -4) 📝 `src/core/mtp.cpp` (+28 -4) _...and 19 more files_ </details> ### 📄 Description # [Engine] KV cache: the exact-cell window for q4lm8v (opt-in, hardened) > **Status: opt-in, experimental-adjacent.** `--kv q4lm8v` + `STRATA_KV_WINDOW=1` (shape S=256, R=64, > `--prefill auto` capped at B=6,144) is the only way to reach it; the default `int8` path is byte-identical > to v0.2.4.2 across every run in this PR's evidence set (dump-logits SHA `A014F504...BA052`, 8/8 runs). > Tested on AMD HIP (RX 9060 XT, Windows, ROCm 7.1). **CUDA and SYCL builds were not run** — no suitable > hardware here; a build check is requested in this thread (see Validation → Not run). > > **Base:** `011c9e60` (the 0.1.47 line). As of 2026-10-25 the branch is 414 commits behind `main`; a trial > merge with today's `main` conflicts only in `setup.py` (everything else auto-merges). Not rebased before > the scope question: the byte-identity evidence is tied to this base, and a rebase would need the `int8` > dump-logits re-run. ## What this is, and what it is not **A basis, not the finished answer.** It makes one KV mode safe, measurable and opt-in — the exact-cell window on 4-bit Lloyd K/V — and it fixes the four bugs that shape exposed (A1–A5). What it enables is the next step: a bug-corrected, more aggressive KV quantization that is checked for correctness instead of tuned by feel. The levers that remain are named, not built: per-channel K before the rotation, lower-bit V, a parity test over the whole cell→row contract, and the format-agnostic overlay of #2318 (see Follow-up). And the KV cache is only one building block of the footprint problem. On this model the expert cache, the PLE table and the dense weights are where the rest of it sits; the numbers in this PR are one card's (RX 9060 XT 16 GB) on one quantization (IQ3_XXS). ## Problem and result `int8` KV costs 8.25 bit/cell: 1,584 MiB at 128K context. On a 16 GB card that is the difference between fitting the expert cache the model wants and paging. The exact-cell window is the memory/accuracy answer: quantize the KV bulk with 4-bit Lloyd (K and V), keep the most recent cells (S=256) and the first cells (sinks, R=64) exact in a small resident side pool. Measured result: **5.29 bit/cell, 1,016 MiB at 128K** (RX 9060 XT, Windows HIP), with the 128K-tail teacher-forced KL *better than int8's*: window 0.00663 against int8's 0.00959 and plain q4lm8v's 0.01587 (KL vs the fp16 reference, last 4,096 of 131,062 positions, measurement of the feature commit). This PR ships the feature together with the hardening it needed to be safe (A1–A5 below), a `--kv-check` helper, and the documentation. Every window-affecting line is gated behind `qsa_kv_win()` / `st.k_win != nullptr`; the default path is untouched (the byte-identity SHA is the regression gate, run 8 times old-vs-new). ## Related discussion - **Scope is the question of this draft.** CONTRIBUTING asks for an issue before a substantial PR; this comes as a draft instead, so the shape can be discussed on the diff itself. If you prefer the issue first, I will open one and follow it with this branch. - [#2318](https://github.com/Niko1221/Strata/pull/2318) — the FP16 sliding-window + q2k PR, closed in the 0.2.0-beta tracker reset ("closed does not mean rejected"). The maintainer's bar there — *"It would need our KL and needle checks before it can be offered as an experimental option"* — is the bar this PR is measured against: KL checks below, needle check under Validation. Differences to #2318 are in *Related work* at the end. - [#3919](https://github.com/Niko1221/Strata/pull/3919) (`STRATA_KV_RING_ADAPT`, open) makes `--kv-resident` a cap for adaptive ring sizing. This PR's A2 refuses `--kv-resident` **with** the window before anything loads, so the two do not interact: RING_ADAPT only ever grows a resident ring, and a resident ring beside the window is exactly what A2 refuses. If #3919 merges first, nothing here changes; if this merges first, #3919 needs nothing either. ## Approach **Why this shape.** The window is a memory plan, not a tunable: its exact cells live in an always-resident side pool, so the whole design follows from that one property. It cannot combine with KV streaming (the streamed bulk has no resident pool to read the exact cells from — A2), the prompt chunk that sizes the ring must never exceed the ring's scan limit (A1), every state carved later must read the same grid (A3), and an empty ring slot must be distinguishable from cell 0 (A5). The four fixes are that shape's consequences, not bolt-ons. The fixes, one commit each, each with the default-path byte-identity proof: | | commit | what it fixes | |---|---|---| | A1 | `7af3ab59` | The free ring caps the auto chunk scan at 6,144 cells. Before: `kv_append_win: 8191 cells ... 6400-row ... ring`, `std::exit(1)` mid-prompt. After: `prompt chunk auto: 6144`, `DONE 8192`. | | A2 | `fd97ade2` | `--kv-resident` (streaming) and the window refuse each other: the engine refuses the pair loudly (exit 2, before anything loads); setup never writes it (a `q4lm8v` config's KV stays in VRAM, streaming explained off). `--kv-check` reports the pair as `NO - refused (exit 2)`. | | A3-lite | `fd3fc135` | The window grid (S/R/B) is frozen once the prompt chunk settles (`qsa_kv_win_freeze()`), so main/MTP-drafter/MTP-twin states cannot drift. | | A5 | `1e4933e1` | Empty ring tags init to `0xFF` (-1) instead of `0`, so an unwritten row can never be read as exact cell 0. | Plus `0497be1b` (`--kv-check` helper + a `docs/DETAILS.md` KV section), `d5a337a2` (documented numbers + method note), `955ceb65` + `6acc91de` (a two-column paper of the design and its decisions, and the same paper after an independent desk review; `docs/paper/KV-Window-Hardening.tex`), `ca1bca07` (the needle run's raw JSON, configs, engine logs and runner under `bench/results/`), and `6c3bd969` (setup installs an engine built from this tree: `--engine-dir DIR` / `STRATA_ENGINE_DIR`, or the newest `build-hip*/strata.exe` by itself when `q4lm8v` is chosen — no ready-made engine carries the mode, so without this the feature cannot be tried before a release; the default download path is untouched). **Known limits of the design** (why A3-lite and not a full state-layout refactor): the full A3 refactor is held back until the follow-up parity test (below) exists; A3-lite freezes and asserts instead of moving fields. An explicit small ring (`STRATA_KV_WINDOW_BATCH`) with `--prefill` above it still refuses by design — the message names what to change. ## Trying it (reviewer path) No ready-made engine carries `q4lm8v` yet, so setup has to install one built from this branch. `6c3bd969` wires that in; without it a reviewer would have to package a release zip first. 1. Build the engine: `tools\hip\build_windows.bat` on Windows (it packages `dist\`), or any build of this tree that left a `build-hip*/strata.exe`; Linux as in `docs/AMD_HIP.md`. 2. `START-HERE.bat --setup --kv q4lm8v` — setup picks the newest `build-hip*/strata.exe` on its own and says `q4lm8v is only in an engine built from this tree: using <folder> instead of the ready-made one`. An explicit folder works too (`--engine-dir <folder>` or `STRATA_ENGINE_DIR`); only the Windows HIP path was run (see Not run). If no build is found, setup stops with what to do instead of downloading an engine that cannot run the mode. 3. With pictures on, setup also installs the CPU image reader from this tree (copied from `build-vision\bin`, or compiled once); a self-built engine has no GPU reader unless one was built. 4. Setup writes `--kv q4lm8v` and `STRATA_KV_WINDOW=1` / `_S=256` / `_R=64` into the model's config. `python setup.py --kv-check` then prints mode, env, effective bits/cell and whether the engine would start; the engine log names the window (`KV window ring sized for the prompt chunk (6144 cells)`). 5. `--kv int8` (the default) never takes this path: no build folder is looked for and the ready-made engine is downloaded exactly as before. `UPDATE.bat` replaces the engine folder with a ready-made one, so it is not part of this test. ## Validation Hardware for every run below: AMD Ryzen 9 3900XT, 64 GB DDR4-3000, RX 9060 XT 16 GB, Windows 11, ROCm 7.1, Swift-Qwen3.8-Flash-Next IQ3_XXS. One GPU job at a time, quiet machine; timing numbers from noisy sessions are not quoted. Reproduction commands live in the PR's commits and in `docs/DETAILS.md`. **Passed:** - Default path byte identity: `int8` dump-logits SHA `A014F504BA315831DA1D13C6A0B3D23D6393B919872396C2A1464182710BA052` (4,330,448 bytes, 4,095 positions) — **8/8 runs** (4K A/B, rounds 1–4, old/new interleaved), identical to the pre-change binary. - Window path baselines (reference for future changes): `0768398A...94CA` (8,191 rows, 8,192 ids, `--prefill auto:16384`), `4161376D...707E` (4,095 rows, cwe-4k), `71B0B586...6463` (4,095 rows, set-4096). - KL (teacher-forced, `STRATA_LOGPOS`, `quant_quality.py compare`): window vs int8 on the same segment set **KL 0.04524, top-1 98.00%**; the feature commit's 128K-tail KL vs the fp16 reference: window 0.00663, int8 0.00959, plain q4lm8v 0.01587. - Former crash config (probe B): `DONE 8192`, chunk 6144, no refusal. Probe C (explicit ring 512 + `--prefill 2048`): refused as the contract says. - Refusal probes A/B/C: the `--kv-resident` + window pair refused in ~1 s (exit 2, message names the fix); window-alone and int8+streaming configs get past it. - `tools/test_setup_choices.py`: **56/56 pass** (incl. the new A2 setup assertions). - Setup bridge (`6c3bd969`, Windows HIP, `engine_step` run directly with `kv="q4lm8v"`): installs `build-hip-port\strata.exe` into `engine\` with `BUILD.json` `"source": "local"` and the version read from the binary, copies the CPU image reader from `build-vision\bin` (`hip_reader` → `mode: cpu`), and names a running model ("… is in use: a model is running from it") instead of a `PermissionError`. - Needle check (`tools/needle_bench.py` through `serve/server.py`, 32k and 128k prompt tokens at depths 10/50/90, identical deterministic prompts for both arms, `STRATA_PREFILL_CPU_SHARE=0`): **6/6 found with int8, 6/6 found with the window** — the code word was recalled at every depth at both lengths in both arms (31,643–31,644 and 126,015–126,017 prompt tokens). Raw data, configs and logs: `bench/results/2026-10-24-needle-window/`. Single run per arm, one model, repo-file haystack; wall-clock times are not quoted as a speed comparison (see the limits). **Failed:** none observed. ## User test (setup journey) A Strata user set the branch up the way the project documents it, not as a developer: `START-HERE.bat --setup --family swift --model IQ2_XS --gguf-dir <their own abliterated IQ2_XS shards> --port 7601`, KV menu choice 3 (`q4lm8v`), 131K context, pictures on (CPU reader). The first attempt is what produced `6c3bd969`: setup went for the ready-made engine (404 for this version on the release page, then the signed update feed), which cannot run the chosen KV mode, instead of a build of the branch. After the fix the same run installs the local build and the CPU reader, writes `--kv q4lm8v` with `STRATA_KV_WINDOW=1` / `_S=256` / `_R=64`, and the model starts and answers in chat. The engine's own status reports `kv: q4lm8v`, `kv_resident: 0`, `prefill_cap: 6144` (A1) and `images: true`, with 5.94 GiB of expert cache and 2.3 GiB VRAM free at 131K. Covered: setup, start, short chat (IQ2_XS, RX 9060 XT 16 GB, Windows HIP). Not covered by this user test: long-context work, pictures through an app, switching the same install back to `int8`, third-party app wiring. A speed suite ran on the same install afterwards; it is a single arm without an `int8` comparison and is therefore not used as window evidence. **Not run:** - **CUDA (nvcc) and SYCL (dpcpp) builds** — no suitable GPUs on the author's machine. The changes are backend-agnostic (globals, argument checks, setup Python, one memset value); SYCL does not offer `q4lm8v` at all (`WINDOW_KV` is False there), so the window paths are dead code on SYCL but still compile-checked there would be the ask. **Requesting a targeted CUDA + SYCL build in this thread.** - Layer-split/multi-GPU, parallel slots (>1), batch-MTP, Linux, NVIDIA hardware with the window enabled: reasoned about (the gating is per-session), not executed. - The setup bridge was run on Windows HIP only: an `int8` install through the new code path was not re-run (the branch is entered only with `--engine-dir`/`STRATA_ENGINE_DIR` or `q4lm8v`), nor was `--engine-dir` with a CUDA build or on Linux. - `docs/paper/KV-Window-Hardening.tex` compiled by CI: the author's TeX build is local (MiKTeX); the file is documentation, no GPU build needed per CONTRIBUTING. ## Limits - The window must stay resident: no streaming beside it (A2), and the expert cache yields the VRAM instead (setup's `--kv-check` prints the effective bits/cell so the plan is inspectable). - `--prefill auto` is capped at 6,144 cells in windowed runs (A1) — a deliberate contract, visible in the `prompt chunk auto:` line. - Quality is measured on one model (Swift-Qwen3.8-Flash-Next IQ3_XXS), one document family (cwe sets) and the repo-file haystack (needles). The KL numbers are single-model, single-document measurements. - The needle run's wall-clock times are not quoted: a second identical bench client overlapped on the same server during both arms (runner bug on the author's side), which queues the requests. Found/missed is unaffected — both clients asked the same deterministic prompts and every answer was found — but the seconds per request are not a clean speed measurement. - The full cell→row parity test does not exist yet (follow-up, below); the KL + needle checks cover the teacher-forced read path and long-context recall, not every ring-wrap/stale-tag/sink shape. - T ∈ [6145; 6400] with an *explicit* ring and `--prefill` above it still refuses (by design; the message names the remedy). SYCL ignores `STRATA_KV_WINDOW` silently (setup never writes it there). ## Related work (vs #2318) Both PRs keep recent cells exact over a quantized bulk; they differ in what the exactness is made of and where the device sits: | | #2318 (bsorensen110) | this PR | |---|---|---| | exact cells | FP16 overlay pool (`--kv-window N`, `--kv-sink S`) on top of *any* format | built into the format: `q4lm8v` = 4-bit Lloyd K+V + exact side pool (S=256/R=64) | | bulk formats | q4_0, q2, q2k (2-bit K per-channel + 4-bit V) | q4lm8v only (opt-in; int8 untouched) | | headline memory | q2k 448 B/cell (1.31 GiB @262K) + 3.56–6.56 MiB overlay | 5.29 bit/cell, 1,016 MiB @128K (overlay included) | | quality evidence | KL vs fp16 on 9 texts, needle-free; window 1024 regression unexplained | KL vs fp16 @128K tail (window beats int8), KL vs int8 on cwe sets, needles 32k/128k (below) | | known interaction | exact window cells bias q2k's shrunk scores; fixed by `--kv-k-gain` | not observed: the window measures *better* than int8 at the 128K tail (0.00663 vs 0.00959 vs the fp16 reference) — no gain knob needed at S=256/R=64 | #2318's overlay is format-agnostic and its q2k format is the cheaper bulk; this PR's window is one format whose measured point (5.29 bit @128K) sits between q4_0 (576 B/cell) and q2k (448 B/cell) while its 128K tail KL beats int8. If both land, they compose differently and do not conflict: #2318's device would sit on top of formats, this one *is* a format; nothing in either diff touches the other's paths. ## Follow-up (not in this PR) A window parity test over the whole cell→row contract: dump every window row at write time and re-read it through `qsa_win_slot` on each shape (writer `win_row_of` `kv_q4.cu:391` ↔ reader `qsa_decode_attn.hpp:73`, host `:93`; ring wrap; stale-tag→quantized fallback; sink rows; batch-boundary shapes; the empty-tag case A5 guards). The KL/needle checks above cover the read path, not these shapes; this is why A3 was shipped as A3-lite. Meanwhile the window stays opt-in and the int8 default stays byte-identical. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#4896
No description provided.