Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

kv: --kv k8v4 streams with --kv-resident

Trevinfea9ff5a4d16
diff --git a/include/strata/core/layer.hpp b/include/strata/core/layer.hpp
index aec6e9eb7..4e3a891dc 100644
--- a/include/strata/core/layer.hpp
+++ b/include/strata/core/layer.hpp
@@ -291,17 +291,12 @@ bool qsa_kv_int8();
/// PR #21: store K/V as Q4_0 after a Hadamard rotation (`--kv q4_0`): 576 B per cell, vs 1,056 in INT8.
void qsa_set_kv_q4(bool enabled);
bool qsa_kv_q4();
-/// Hybrid K8V4 (`--kv k8v4`): K in INT8, V in rotated Q4_0 - 816 B per cell. Not with --kv-resident.
+/// Hybrid K8V4 (`--kv k8v4`): K in INT8, V in rotated Q4_0 - 816 B per cell; streams with --kv-resident too.
void qsa_set_kv_hybrid(bool enabled);
bool qsa_kv_hybrid();
-/// The state's KV format for the block-moving functions of kv_stream.hpp (kKvF16 / kKvInt8 / kKvQ4).
+/// The state's KV format for the block-moving functions of kv_stream.hpp (kKvF16 / kKvInt8 / kKvQ4 / kKvHybrid).
inline int qsa_kv_format(const QsaState& st) {
- // A hybrid K8V4 state is mode 0 only and never reaches the block movers; refuse rather than let it
- // fall through to kKvF16 - a wrong layout silently applied is worse than a hard stop (PR review).
- if (st.kv_hybrid) {
- std::fprintf(stderr, "strata: qsa_kv_format: a hybrid K8V4 state must never reach the block movers\n");
- std::exit(1); // the kernels' own "unsupported geometry" convention (kv_q8.cu, qsa_decode_attn.cu)
- }
+ if (st.kv_hybrid) return strata::kernels::kKvHybrid; // K8V4: its own three runs (kv_stream.cu)
return st.kv_q4 ? strata::kernels::kKvQ4 : st.kv_int8 ? strata::kernels::kKvInt8 : strata::kernels::kKvF16;
}
uint64_t qsa_state_init(const ModelGeometry& g, int64_t max_cells, void* base, QsaState& st,
diff --git a/include/strata/kernels/kv_stream.hpp b/include/strata/kernels/kv_stream.hpp
index 514474b11..8108795b8 100644
--- a/include/strata/kernels/kv_stream.hpp
+++ b/include/strata/kernels/kv_stream.hpp
@@ -41,9 +41,26 @@ struct KvHostPools {
bool present() const { return k_pool != nullptr || k_q != nullptr || k_q4 != nullptr; }
};
-/// The KV storage format, for the functions below that move whole blocks (`fmt`): fp16, int8 (+ scales), q4_0.
-/// (A bool `int8` argument still reads as kKvF16 / kKvInt8.)
-enum KvFormat : int { kKvF16 = 0, kKvInt8 = 1, kKvQ4 = 2 };
+/// The KV storage format, for the functions below that move whole blocks (`fmt`): fp16, int8 (+ scales), q4_0, and
+/// K8V4 (`--kv k8v4`: K as int8 codes + scales, V as rotated q4_0 - three runs, `k_q`, `k_scale`, `v_q4`).
+/// (A bool `int8` argument still reads as kKvF16 / kKvInt8.) kKvHybrid is 3, the number conversation snapshots
+/// already give K8V4.
+enum KvFormat : int { kKvF16 = 0, kKvInt8 = 1, kKvQ4 = 2, kKvHybrid = 3 };
+
+/// K8V4's halves as the single-format append kernels see them, so a hybrid layer's host copy (and the prompt path's
+/// staging pool) is written by the same calls that write its VRAM pools: K is "int8 whose V is K", V is "q4_0 whose
+/// K is V" - the folded duplicate the hybrid appends already make (layer.cpp). Both fields of a half point at the
+/// same array on purpose: the kernels test the K-side pointer for presence and pick K or V per lane.
+inline KvHostPools kv_hybrid_k_half(const KvHostPools& h) {
+ KvHostPools r;
+ r.k_q = h.k_q; r.v_q = h.k_q; r.k_scale = h.k_scale; r.v_scale = h.k_scale;
+ return r;
+}
+inline KvHostPools kv_hybrid_v_half(const KvHostPools& h) {
+ KvHostPools r;
+ r.k_q4 = h.v_q4; r.v_q4 = h.v_q4;
+ return r;
+}
/// The residency map of a streamed layer, all device memory at fixed addresses (the graphs bake them in).
struct KvStreamMap {
diff --git a/src/core/conversation_snapshot.cpp b/src/core/conversation_snapshot.cpp
index 759daa65a..8b74fc6c6 100644
--- a/src/core/conversation_snapshot.cpp
+++ b/src/core/conversation_snapshot.cpp
@@ -26,8 +26,8 @@ bool valid_extent(const QsaState& st, int64_t upto, std::string& error) {
bool layout(const QsaState& st, const ModelGeometry& g, int64_t upto, bool index, Layout& l, std::string& error) {
if (!valid_extent(st, upto, error)) return false;
- if (st.kv_hybrid && (st.kv_mode != 0 || st.kv_q4 || st.kv_int8)) {
- error = "conversation snapshot: hybrid K8V4 requires an identity layout and distinct format flags";
+ if (st.kv_hybrid && (st.kv_mode == 2 || st.kv_q4 || st.kv_int8)) { // a streamed one reads its host copy
+ error = "conversation snapshot: hybrid K8V4 cannot be a ring and needs distinct format flags";
return false;
}
const auto s = strata::kernels::qsa_real_shapes();
@@ -72,7 +72,9 @@ bool layout(const QsaState& st, const ModelGeometry& g, int64_t upto, bool index
std::array<void*, 5> pools(const QsaState& st, bool resident = false) {
const bool host = st.kv_mode != 0 && !resident;
- if (st.kv_hybrid) return {st.k_q, st.v_q4, st.k_scale, nullptr, st.idx_pooled};
+ if (st.kv_hybrid)
+ return {host ? st.host.k_q : st.k_q, host ? st.host.v_q4 : st.v_q4, host ? st.host.k_scale : st.k_scale,
+ nullptr, st.idx_pooled};
if (st.kv_q4)
return {host ? st.host.k_q4 : st.k_q4, host ? st.host.v_q4 : st.v_q4, nullptr, nullptr, st.idx_pooled};
if (st.kv_int8)
diff --git a/src/core/layer.cpp b/src/core/layer.cpp
index 33450f7d2..1cebc46f8 100644
--- a/src/core/layer.cpp
+++ b/src/core/layer.cpp
@@ -583,12 +583,8 @@ uint64_t qsa_state_init(const ModelGeometry& g, int64_t max_cells, void* base, Q
st.kv_int8 = g_kv_int8 && !g_kv_q4;
st.kv_q4 = g_kv_q4;
// Hybrid K8V4, main layers only (the drafter's state is created with the globals toggled to INT8 -
- // mtp.cpp). Streamed mode is refused outright; generate.cpp validates it too, this is the backstop.
+ // mtp.cpp). A streamed one keeps its host copy in the same three runs (kv_stream.cu, kKvHybrid).
if (g_kv_hybrid && ring_cells <= 0) {
- if (p.mode == 1) {
- std::fprintf(stderr, "strata: hybrid K8V4 KV does not support --kv-resident streaming\n");
- return 0;
- }
st.kv_hybrid = true;
st.kv_int8 = false;
st.kv_q4 = false;
@@ -670,7 +666,11 @@ uint64_t qsa_state_init(const ModelGeometry& g, int64_t max_cells, void* base, Q
}
g_kv_host_bytes += bytes;
Cursor hc{d};
- if (st.kv_q4) {
+ if (st.kv_hybrid) {
+ st.host.k_q = hc.take<int8_t>(hrows * s.head_dim);
+ st.host.k_scale = hc.take<uint16_t>(hrows * (s.head_dim / strata::kernels::KV_Q8_GROUP));
+ st.host.v_q4 = hc.take<uint8_t>(hrows * q4_row);
+ } else if (st.kv_q4) {
st.host.k_q4 = hc.take<uint8_t>(hrows * q4_row);
st.host.v_q4 = hc.take<uint8_t>(hrows * q4_row);
} else if (st.kv_int8) {
@@ -930,9 +930,15 @@ if (st.kv_hybrid) {
// K8V4: only V is rotated (kv_q4.hpp's H); the scores pair unrotated q with unrotated INT8 K, and the
// output - a mix of rotated values - is rotated back after attention. Each append/gather call folds the
// unused half's lanes onto the used pool (a bit-identical duplicate write), so no kernel variants exist.
+ // Streamed (--kv-resident), each half also writes its part of the host copy (kv_hybrid_*_half).
strata::kernels::fwht256_inplace_cuda(b.vcur, g.n_head_kv, stream);
- kv_append_q8_step(st.k_q, st.k_q, st.k_scale, st.k_scale, st.page_table, st.step, b.kcur, b.kcur, s, stream, nullptr); // mode 0: no host mirror
- strata::kernels::kv_append_q4_step(st.v_q4, st.v_q4, st.page_table, st.step, b.vcur, b.vcur, s, stream, nullptr);
+ const strata::kernels::KvHostPools hk = strata::kernels::kv_hybrid_k_half(st.host),
+ hv = strata::kernels::kv_hybrid_v_half(st.host);
+ const bool mirror = st.host.present();
+ kv_append_q8_step(st.k_q, st.k_q, st.k_scale, st.k_scale, st.page_table, st.step, b.kcur, b.kcur, s, stream,
+ mirror ? &hk : nullptr);
+ strata::kernels::kv_append_q4_step(st.v_q4, st.v_q4, st.page_table, st.step, b.vcur, b.vcur, s, stream,
+ mirror ? &hv : nullptr);
} else {
if (st.kv_rot) { // rotated K and V (kv_q4.hpp): Q4_0, and INT8 with STRATA_KV_ROT=1
strata::kernels::fwht256_inplace_cuda(b.kcur, g.n_head_kv, stream);
diff --git a/src/core/verify.cpp b/src/core/verify.cpp
index 6a19b3d15..2b9dc7323 100644
--- a/src/core/verify.cpp
+++ b/src/core/verify.cpp
@@ -890,10 +890,12 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
const QsaState& st = slot_ss(t).qsa_states[qi]; // the row's own K/V (ss's outside a batch)
const int32_t* step_t = step_ + t * kStepCount;
if (st.kv_hybrid) { // K8V4: the unused half's lanes folded onto the used pool (layer.cpp)
+ const KvHostPools hk = kv_hybrid_k_half(st.host), hv = kv_hybrid_v_half(st.host);
+ const bool mirror = st.host.present(); // streamed: the host copy too
kv_append_q8_step(st.k_q, st.k_q, st.k_scale, st.k_scale, st.page_table, step_t,
- kcur_ + t * NKV * HD, kcur_ + t * NKV * HD, s, cs, nullptr);
+ kcur_ + t * NKV * HD, kcur_ + t * NKV * HD, s, cs, mirror ? &hk : nullptr);
kv_append_q4_step(st.v_q4, st.v_q4, st.page_table, step_t, vcur_ + t * NKV * HD,
- vcur_ + t * NKV * HD, s, cs, nullptr);
+ vcur_ + t * NKV * HD, s, cs, mirror ? &hv : nullptr);
} else if (st.kv_q4)
kv_append_q4_step(st.k_q4, st.v_q4, st.page_table, step_t, kcur_ + t * NKV * HD,
vcur_ + t * NKV * HD, s, cs, &st.host);
diff --git a/src/kernels/cuda/kv_stream.cu b/src/kernels/cuda/kv_stream.cu
index 914a836ef..9f8232e2e 100644
--- a/src/kernels/cuda/kv_stream.cu
+++ b/src/kernels/cuda/kv_stream.cu
@@ -33,7 +33,14 @@ struct Runs {
Runs runs_of(const QsaAttnPools& slots, const KvHostPools& host, int fmt, const QsaShapes& s) {
const int rows = (int) (s.n_head_kv * s.page_size);
Runs r{};
- if (fmt == kKvQ4) {
+ if (fmt == kKvHybrid) { // K8V4: int8 K codes, their scales, rotated q4_0 V
+ const int codes = rows * (int) s.head_dim, scales = rows * (int) (s.head_dim / KV_Q8_GROUP) * 2;
+ const int v = rows * (int) kv_q4_bytes_per_head((int) s.head_dim);
+ r.src[0] = (const uint8_t*) host.k_q; r.dst[0] = (uint8_t*) slots.k_q; r.len[0] = codes;
+ r.src[1] = (const uint8_t*) host.k_scale; r.dst[1] = (uint8_t*) slots.k_scale; r.len[1] = scales;
+ r.src[2] = (const uint8_t*) host.v_q4; r.dst[2] = (uint8_t*) slots.v_q4; r.len[2] = v;
+ r.n = 3;
+ } else if (fmt == kKvQ4) {
const int bytes = rows * (int) kv_q4_bytes_per_head((int) s.head_dim);
r.src[0] = (const uint8_t*) host.k_q4; r.dst[0] = (uint8_t*) slots.k_q4; r.len[0] = bytes;
r.src[1] = (const uint8_t*) host.v_q4; r.dst[1] = (uint8_t*) slots.v_q4; r.len[1] = bytes;
@@ -191,6 +198,9 @@ __global__ void ring_kernel(int32_t* table, long long n_blocks, long long n_slot
uint64_t kv_block_bytes(const QsaShapes& s, int fmt) {
const uint64_t rows = (uint64_t) (s.n_head_kv * s.page_size);
+ if (fmt == kKvHybrid)
+ return rows * (uint64_t) s.head_dim + rows * (uint64_t) (s.head_dim / KV_Q8_GROUP) * 2 +
+ rows * kv_q4_bytes_per_head((int) s.head_dim);
if (fmt == kKvQ4) return rows * kv_q4_bytes_per_head((int) s.head_dim) * 2;
return fmt == kKvInt8 ? rows * (uint64_t) s.head_dim * 2 + rows * (uint64_t) (s.head_dim / KV_Q8_GROUP) * 2 * 2
: rows * (uint64_t) s.head_dim * 2 * 2;
diff --git a/src/kernels/kv_stream_parity.cpp b/src/kernels/kv_stream_parity.cpp
index 07557347e..722b7bc1c 100644
--- a/src/kernels/kv_stream_parity.cpp
+++ b/src/kernels/kv_stream_parity.cpp
@@ -8,7 +8,8 @@
// 2. the residency map is consistent after every call (slot_block and page_table invert each other);
// 3. no call overflowed, and the hit/miss counters add up;
// 4. a ring (the MTP drafter's layout) restored from the host copy reads the same values as the resident pool.
-// INT8, FP16 and Q4_0 (PR #21) pools.
+// INT8, FP16, Q4_0 (PR #21) and hybrid K8V4 pools; K8V4's appends are the engine's folded calls (layer.cpp), its host
+// copy written through kv_hybrid_k_half / kv_hybrid_v_half.
#include "strata/kernels/kv_q4.hpp"
#include "strata/kernels/kv_q8.hpp"
#include "strata/kernels/kv_stream.hpp"
@@ -51,7 +52,12 @@ struct Pools { // one K/V pool set of `pages` pages
k::KvHostPools p; // reused as a plain pointer bundle
void alloc(int64_t pages, const k::QsaShapes& s, int fmt, bool host) {
const size_t rows = (size_t) pages * s.n_head_kv * s.page_size;
- if (fmt == k::kKvQ4) {
+ if (fmt == k::kKvHybrid) {
+ const size_t b = rows * k::kv_q4_bytes_per_head((int) s.head_dim);
+ p.k_q = host ? halloc<int8_t>(rows * s.head_dim) : dalloc<int8_t>(rows * s.head_dim);
+ p.k_scale = host ? halloc<uint16_t>(rows * 4) : dalloc<uint16_t>(rows * 4);
+ p.v_q4 = host ? halloc<uint8_t>(b) : dalloc<uint8_t>(b);
+ } else if (fmt == k::kKvQ4) {
const size_t b = rows * k::kv_q4_bytes_per_head((int) s.head_dim);
p.k_q4 = host ? halloc<uint8_t>(b) : dalloc<uint8_t>(b);
p.v_q4 = host ? halloc<uint8_t>(b) : dalloc<uint8_t>(b);
@@ -75,7 +81,13 @@ struct Pools { // one K/V pool set of `pages` pages
void append(const Pools& pl, const int32_t* table, const int32_t* step, const float* kc, const float* vc,
const k::QsaShapes& s, int fmt, const k::KvHostPools* host) {
- if (fmt == k::kKvQ4) k::kv_append_q4_step(pl.p.k_q4, pl.p.v_q4, table, step, kc, vc, s, nullptr, host);
+ if (fmt == k::kKvHybrid) { // exactly as qsa_layer: each half folded onto its own pool, host copy by halves
+ const k::KvHostPools hk = host ? k::kv_hybrid_k_half(*host) : k::KvHostPools{},
+ hv = host ? k::kv_hybrid_v_half(*host) : k::KvHostPools{};
+ k::kv_append_q8_step(pl.p.k_q, pl.p.k_q, pl.p.k_scale, pl.p.k_scale, table, step, kc, kc, s, nullptr,
+ host ? &hk : nullptr);
+ k::kv_append_q4_step(pl.p.v_q4, pl.p.v_q4, table, step, vc, vc, s, nullptr, host ? &hv : nullptr);
+ } else if (fmt == k::kKvQ4) k::kv_append_q4_step(pl.p.k_q4, pl.p.v_q4, table, step, kc, vc, s, nullptr, host);
else if (fmt == k::kKvInt8) k::kv_append_q8_step(pl.p.k_q, pl.p.v_q, pl.p.k_scale, pl.p.v_scale, table, step, kc, vc, s, nullptr, host);
else k::kv_append_step(pl.p.k_pool, pl.p.v_pool, table, step, kc, vc, s, nullptr, host);
}
@@ -103,7 +115,7 @@ std::vector<int32_t> selection(int64_t n_kv, int64_t width, std::mt19937& rng) {
}
bool run(int fmt) {
- const char* name = fmt == k::kKvQ4 ? "q4_0" : fmt == k::kKvInt8 ? "int8" : "fp16";
+ const char* name = fmt == k::kKvHybrid ? "k8v4" : fmt == k::kKvQ4 ? "q4_0" : fmt == k::kKvInt8 ? "int8" : "fp16";
k::QsaShapes s = k::qsa_real_shapes();
const int64_t N = 40000, n_blocks = (N + 3) / 4, n_slots = 8 * 516 + 700; // must evict: slots < blocks
const int64_t cap = k::qsa_selection_width(k::kTopkMaxCells, s), NQ = 8;
@@ -227,8 +239,8 @@ bool run(int fmt) {
int main() {
std::printf("kv_stream_parity: streamed vs resident KV, bitwise\n");
- const bool a = run(k::kKvInt8), b = run(k::kKvF16), c = run(k::kKvQ4);
- if (!a || !b || !c) ++g_fail;
+ const bool a = run(k::kKvInt8), b = run(k::kKvF16), c = run(k::kKvQ4), d = run(k::kKvHybrid);
+ if (!a || !b || !c || !d) ++g_fail;
std::printf(g_fail ? "FAIL\n" : "PASS\n");
return g_fail ? 1 : 0;
}
diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp
index 05228b536..9cc1e5d51 100644
--- a/src/prefill/prefill.cpp
+++ b/src/prefill/prefill.cpp
@@ -635,7 +635,11 @@ void take_stage(Alloc& o_borrowed, const core::SessionState& ss, const strata::k
own.owned = o_borrowed.owned;
Alloc& o = stage_own() ? own : o_borrowed;
const size_t rows = (size_t) q0.n_pages * s.n_head_kv * s.page_size;
- if (q0.kv_q4) {
+ if (q0.kv_hybrid) { // K8V4: the three runs of kKvHybrid; pools_of() then reads as the hybrid pools (mode 3)
+ st.k_q = o.take<int8_t>(rows * s.head_dim, ok);
+ st.k_scale = o.take<uint16_t>(rows * (s.head_dim / 64), ok);
+ st.v_q4 = o.take<uint8_t>(rows * strata::kernels::kv_q4_bytes_per_head((int) s.head_dim), ok);
+ } else if (q0.kv_q4) {
st.k_q4 = o.take<uint8_t>(rows * strata::kernels::kv_q4_bytes_per_head((int) s.head_dim), ok);
st.v_q4 = o.take<uint8_t>(rows * strata::kernels::kv_q4_bytes_per_head((int) s.head_dim), ok);
} else if (q0.kv_int8) {
@@ -2187,12 +2191,18 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
const bool host_by_dma = staged && kv_host_dma;
const strata::kernels::KvHostPools* host_w = host_by_dma ? nullptr : &st.host;
if (st.kv_hybrid) { // K8V4: K INT8 unrotated, V rotated Q4_0 (only V and the output rotate)
+ // streamed: each half writes its part of the host copy and of the staging pool
strata::kernels::fwht256_inplace_cuda(m.Vc, T * 2, m.cs);
+ const strata::kernels::KvHostPools hk = strata::kernels::kv_hybrid_k_half(st.host),
+ hv = strata::kernels::kv_hybrid_v_half(st.host),
+ sk = strata::kernels::kv_hybrid_k_half(m.stage),
+ sv = strata::kernels::kv_hybrid_v_half(m.stage);
+ const bool mirror = st.host.present();
kv_append(m.Kc, m.Kc, T, p0, st.page_table, s.page_size, nullptr, nullptr,
- st.k_q, st.k_q, st.k_scale, st.k_scale, m.cs, nullptr, // mode 0: no host mirror
- staged ? &m.stage : nullptr);
+ st.k_q, st.k_q, st.k_scale, st.k_scale, m.cs, mirror ? &hk : nullptr,
+ staged ? &sk : nullptr);
strata::kernels::kv_append_q4(st.v_q4, st.v_q4, st.page_table, p0, T, m.Vc, m.Vc, s, m.cs,
- nullptr, staged ? &m.stage : nullptr);
+ mirror ? &hv : nullptr, staged ? &sv : nullptr);
} else {
if (st.kv_rot) { // rotated K and V (kv_q4.hpp), the queries below too, the output back
strata::kernels::fwht256_inplace_cuda(m.Kc, T * 2, m.cs);
diff --git a/src/program/generate.cpp b/src/program/generate.cpp
index 84f53e752..75deb2ff8 100644
--- a/src/program/generate.cpp
+++ b/src/program/generate.cpp
@@ -645,7 +645,7 @@ void usage() {
" --kv q4_0 4-bit K/V after a Hadamard rotation (PR #21): half of int8's memory,\n"
" slightly lower precision (see bench/results/2026-09-27-kv-q4)\n"
" --kv k8v4 hybrid: INT8 K (exact attention scores) + rotated Q4_0 V, 816 B/cell\n"
- " (vs int8's 1,056); not with --kv-resident\n"
+ " (vs int8's 1,056); streams with --kv-resident too\n"
" --kv-resident N KV streaming: keep N cells of each QSA layer in VRAM (min 20480) and the\n"
" whole K/V in pinned RAM; the freed VRAM goes to expert slots. 0 (default):\n"
" all of it in VRAM. A context of N cells or fewer is not streamed\n"
@@ -2012,10 +2012,6 @@ int main(int argc, char** argv) {
std::fprintf(stderr, "strata generate: --kv-resident must be >= 0\n");
return 2;
}
- if (o.kv == "k8v4" && o.kv_resident > 0) {
- std::fprintf(stderr, "strata generate: --kv k8v4 does not support --kv-resident streaming (yet)\n");
- return 2;
- }
strata::core::qsa_set_kv_resident(o.kv_resident);
// Prompt lookup (the suffix drafter, on by default): the MTP keeps its --spec windows and a lookup window may be
// up to 2 tokens longer; the draft policy (strata/spec/draft_policy.hpp) takes one only where it pays. Code