okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge PR #21: Q4_0 KV with Hadamard rotation (--kv q4_0), integrated with KV streaming; accurate expert-cache note (#23)40542e8bb

main
3.1 KiB60 linesraw

2026-09-27: Q4_0 KV with Hadamard rotation (PR #21, --kv q4_0)

PR #21 (code-martin) adds a 4-bit K/V format: an orthonormal 256-point Walsh-Hadamard rotation of K, V and Q, then ggml q4_0 blocks (576 B per cell and layer, vs 1,056 B in int8).

Integration fixes made on merge

  • Q4 + KV streaming (--kv-resident). The PR had no host copy for Q4 and skipped the staging, so with streaming on, the prompt path wrote K/V through the residency map's -1 entries (out-of-bounds writes).
    • Q4 is now a third format in kv_stream: host copy, block residency, prompt-path staging, MTP ring and STATE_HASH.
    • kv_stream_parity checks it bitwise, streamed vs resident, with ring restore.
  • Duplicate attention. The per-token decode path ran attention twice, the first time before the selection's blocks were made resident; it now runs once.
  • Tie-break. The group maximum's tie-break is now deterministic across lanes: equal |x| with opposite signs could give one block two scales.
  • Unrelated changes dropped: duplicate path fixes (already in 0.1.7), personal .gitignore entries, and a re-encoded CMakeLists.

kv_q4_parity (the PR's test) and all 70 ctest tests pass.

Quality (teacher-forced, tools/kv_precision_compare.py)

Same text, same fixed expert set (Q2_0, 3,000 slots, --adapt-swaps 0); only --kv differs. fp16 is the reference.

TextKVSame top-1 as fp16Mean KLPerplexityvs fp16 (nats/token)
code agent (512)int898.4%0.0142.649-0.011 +- 0.006
q4_095.7%0.0302.598-0.031 +- 0.014
document (1,024)int895.6%0.00930.18+0.012 +- 0.005
q4_091.3%0.03932.26+0.078 +- 0.011
chat (1,024)int895.5%0.00918.24-0.003 +- 0.005
q4_090.7%0.02818.52+0.012 +- 0.008
document, 8K (every 8th position)int892.3%0.1147.63-0.013 +- 0.020
q4_088.0%0.2978.65+0.112 +- 0.037
  • int8 is indistinguishable from fp16 (within about 2 standard errors everywhere).
  • q4_0 costs measurable precision, and more as the context grows:
    • document perplexity +8% at 1K and +12% at 8K;
    • the KL from fp16 is 2.6-4.2x int8's;
    • code and chat are near-neutral.
  • Needles with --kv q4_0 --kv-resident 32768: 5/5 (1K, 32K, 128K at 10% and 90% depth, 262K; needles-q4.*). Retrieval holds.

Speed and memory (Q2_0, 128K, matrix bench, --kv-resident 32768)

KVExpert slotsDecode tok/sTokens/roundRounds/sK/V in RAM
int8 (kv-stream/matrix-stream)4,04462.42.5224.81.52 GiB
q4_0 (matrix-q4)4,19067.42.6225.7 (+3.7%)0.83 GiB

Without streaming (contexts up to 64K, or too little RAM for it) q4_0 halves the whole KV cache in VRAM instead.

Decision

--kv q4_0 is a user option:

  • START-HERE.bat --kv q4_0, or the question setup asks above 8K context;
  • or --kv q4_0 in a strata-*.json.

int8 stays the default: every published quality number was measured with it, and q4_0's precision cost is real, though it passes the needles.

1# 2026-09-27: Q4_0 KV with Hadamard rotation (PR #21, `--kv q4_0`)
2
3PR #21 (code-martin) adds a 4-bit K/V format: an orthonormal 256-point Walsh-Hadamard rotation of K, V and Q, then
4ggml `q4_0` blocks (576 B per cell and layer, vs 1,056 B in int8).
5
6## Integration fixes made on merge
7
8- **Q4 + KV streaming (`--kv-resident`).** The PR had no host copy for Q4 and skipped the staging, so with streaming
9  on, the prompt path wrote K/V through the residency map's -1 entries (out-of-bounds writes).
10  - Q4 is now a third format in `kv_stream`: host copy, block residency, prompt-path staging, MTP ring and STATE_HASH.
11  - `kv_stream_parity` checks it bitwise, streamed vs resident, with ring restore.
12- **Duplicate attention.** The per-token decode path ran attention twice, the first time before the selection's
13  blocks were made resident; it now runs once.
14- **Tie-break.** The group maximum's tie-break is now deterministic across lanes: equal |x| with opposite signs
15  could give one block two scales.
16- **Unrelated changes dropped:** duplicate path fixes (already in 0.1.7), personal `.gitignore` entries, and a
17  re-encoded CMakeLists.
18
19`kv_q4_parity` (the PR's test) and all 70 ctest tests pass.
20
21## Quality (teacher-forced, `tools/kv_precision_compare.py`)
22
23Same text, same fixed expert set (Q2_0, 3,000 slots, `--adapt-swaps 0`); only `--kv` differs. fp16 is the reference.
24
25| Text | KV | Same top-1 as fp16 | Mean KL | Perplexity | vs fp16 (nats/token) |
26|---|---|---:|---:|---:|---:|
27| code agent (512) | int8 | 98.4% | 0.014 | 2.649 | -0.011 +- 0.006 |
28| | q4_0 | 95.7% | 0.030 | 2.598 | -0.031 +- 0.014 |
29| document (1,024) | int8 | 95.6% | 0.009 | 30.18 | +0.012 +- 0.005 |
30| | q4_0 | 91.3% | 0.039 | 32.26 | **+0.078 +- 0.011** |
31| chat (1,024) | int8 | 95.5% | 0.009 | 18.24 | -0.003 +- 0.005 |
32| | q4_0 | 90.7% | 0.028 | 18.52 | +0.012 +- 0.008 |
33| document, 8K (every 8th position) | int8 | 92.3% | 0.114 | 7.63 | -0.013 +- 0.020 |
34| | q4_0 | 88.0% | 0.297 | 8.65 | **+0.112 +- 0.037** |
35
36- **int8 is indistinguishable from fp16** (within about 2 standard errors everywhere).
37- **q4_0 costs measurable precision, and more as the context grows:**
38  - document perplexity +8% at 1K and +12% at 8K;
39  - the KL from fp16 is 2.6-4.2x int8's;
40  - code and chat are near-neutral.
41- **Needles with `--kv q4_0 --kv-resident 32768`: 5/5** (1K, 32K, 128K at 10% and 90% depth, 262K;
42  `needles-q4.*`). Retrieval holds.
43
44## Speed and memory (Q2_0, 128K, matrix bench, `--kv-resident 32768`)
45
46| KV | Expert slots | Decode tok/s | Tokens/round | Rounds/s | K/V in RAM |
47|---|---:|---:|---:|---:|---:|
48| int8 (`kv-stream/matrix-stream`) | 4,044 | 62.4 | 2.52 | 24.8 | 1.52 GiB |
49| q4_0 (`matrix-q4`) | 4,190 | 67.4 | 2.62 | 25.7 (+3.7%) | 0.83 GiB |
50
51Without streaming (contexts up to 64K, or too little RAM for it) q4_0 halves the whole KV cache in VRAM instead.
52
53## Decision
54
55`--kv q4_0` is a user option:
56- `START-HERE.bat --kv q4_0`, or the question setup asks above 8K context;
57- or `--kv q4_0` in a `strata-*.json`.
58
59int8 stays the default: every published quality number was measured with it, and q4_0's precision cost is real,
60though it passes the needles.