mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #21] [MERGED] kv: add Q4_0 KV cache mode with FWHT-256 Hadamard rotation #2419
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#2419
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Niko1221/Strata/pull/21
Author: @code-martin
Created: 9/27/2026
Status: ✅ Merged
Merged: 9/27/2026
Merged by: @Niko1221
Base:
main← Head:feature/kv-cache-q4_0📝 Commits (1)
a278979feat(kv): add Q4_0 KV cache mode with FWHT-256 Walsh-Hadamard rotation📊 Changes
13 files changed (+937 additions, -79 deletions)
View changed files
📝
.gitignore(+12 -0)📝
CMakeLists.txt(+8 -1)📝
include/strata/core/layer.hpp(+6 -0)➕
include/strata/kernels/kv_q4.hpp(+53 -0)📝
include/strata/kernels/qsa_decode_attn.hpp(+2 -0)📝
src/core/layer.cpp(+131 -45)📝
src/core/mtp.cpp(+15 -1)📝
src/core/verify.cpp(+19 -1)➕
src/kernels/cuda/kv_q4.cu(+268 -0)📝
src/kernels/cuda/qsa_decode_attn.cu(+52 -20)➕
src/kernels/kv_q4_parity.cpp(+346 -0)📝
src/prefill/prefill.cpp(+22 -9)📝
src/program/generate.cpp(+3 -2)📄 Description
What
Adds an optional 4-bit (
Q4_0) quantized KV cache mode with Fast Walsh-Hadamard Transform (FWHT-256) rotation to Strata:--kv q4_0flag in the engine or server configuration.Q8_0(down to ~4.5 bytes per token per layer per head), unlocking double context capacity on memory-constrained systems (e.g. 256k context).D=256).\langle Hq, Hk \rangle = \langle q, k \rangleby applying the same orthogonal Hadamard matrix to query tokens.verify.cpp), multi-token prediction (mtp.cpp), and QSA decode streaming (qsa_decode_attn.cu).Why
At 256k context, unquantized or 8-bit KV caches consume substantial memory. Standard 4-bit quantization on raw key-value vectors often suffers quality loss due to channel outliers. Applying an orthogonal Walsh-Hadamard transform distributes outlier energy uniformly across all 256 dimensions before block quantization, preserving perplexity with zero score drift.
How
src/kernels/cuda/kv_q4.cu,include/strata/kernels/kv_q4.hpp):1/\sqrt{256} = 1/16.src/kernels/cuda/qsa_decode_attn.cu):Q4_0block layout: 1 FP16 scale + 16 bytes for 32 int4 nibbles.src/kernels/kv_q4_parity.cpp):H \cdot H \cdot x == x.\langle Hq, Hk \rangle == \langle q, k \rangle.kv_q4_paritytarget inCMakeLists.txt.tools/_paths.py,tools/pack_layer.py,tools/strata_pack.pywhen repo is placed near filesystem roots.Verification
kv_q4_parity.exepasses 100% on CUDA (FWHT-256 bitwise match, attention score invariance, block bounds).🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.