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: optional Hadamard rotation for int8 K/V (STRATA_KV_ROT=1); the drafter and the batched verify rotation-consistent
sergiywithf133c655a865
diff --git a/include/strata/core/layer.hpp b/include/strata/core/layer.hppindex 34254a613..a8965da13 100644--- a/include/strata/core/layer.hpp+++ b/include/strata/core/layer.hpp@@ -217,6 +217,9 @@ struct QsaState {/// Uses k_q/k_scale + v_q4. Mode 0 only (no KV streaming, no ring); under this setting the MTP drafter's/// ring state stays plain INT8, so the block movers never see the hybrid layout.bool kv_hybrid = false;+ /// K and V go through kv_q4.hpp's Walsh-Hadamard rotation before they are stored, the queries too, the output+ /// back: always for Q4_0, for INT8 by qsa_set_kv_int8_rotate (spreads outlier channels over the scale groups)+ bool kv_rot = false;int32_t* page_table = nullptr; ///< (n_pages,) logical page -> physical page (-1: not resident, streamed)int64_t n_pages = 0;int64_t max_cells = 0;@@ -279,6 +282,8 @@ uint64_t qsa_kv_host_bytes();/// Plan v0.3 P7: store K/V as INT8 with FP16 scales per 64 values (half the VRAM of FP16). Set before sizing and/// initializing the session; default off until gate G-C accepts it.void qsa_set_kv_int8(bool enabled);+/// INT8 K/V through the Hadamard rotation (off by default: STRATA_KV_ROT=1)+void qsa_set_kv_int8_rotate(bool enabled);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);diff --git a/src/core/layer.cpp b/src/core/layer.cppindex e816da1ca..ff7fc17b5 100644--- a/src/core/layer.cpp+++ b/src/core/layer.cpp@@ -173,8 +173,9 @@ if (!w.wants_q8k()) { s_gemv_q8_0_split(x80, p.codes, p.scales, p.offset,// namespacevoid layer_set_native_bf16(bool enabled) { native_bf16_projections = enabled; }void layer_set_native_flash_attn_short(bool enabled) { native_flash_attn_short = enabled; }-namespace { bool g_kv_int8 = false, g_kv_q4 = false, g_kv_hybrid = false; }+namespace { bool g_kv_int8 = false, g_kv_q4 = false, g_kv_hybrid = false, g_kv_int8_rot = false; }void qsa_set_kv_int8(bool enabled) { g_kv_int8 = enabled; }+void qsa_set_kv_int8_rotate(bool enabled) { g_kv_int8_rot = enabled; }bool qsa_kv_int8() { return g_kv_int8; }void qsa_set_kv_q4(bool enabled) { g_kv_q4 = enabled; }bool qsa_kv_q4() { return g_kv_q4; }@@ -564,6 +565,7 @@ uint64_t qsa_state_init(const ModelGeometry& g, int64_t max_cells, void* base, Qst.kv_int8 = false;st.kv_q4 = false;}+ st.kv_rot = st.kv_q4 || (st.kv_int8 && g_kv_int8_rot); // K8V4 rotates only V (below)st.kv_mode = p.mode;st.n_slots = p.slots;const uint64_t rows = (uint64_t) p.slots * s.n_head_kv * s.page_size; // VRAM rows: the slots@@ -903,11 +905,14 @@ if (st.kv_hybrid) {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 mirrorstrata::kernels::kv_append_q4_step(st.v_q4, st.v_q4, st.page_table, st.step, b.vcur, b.vcur, s, stream, nullptr);-} else if (st.kv_q4) {- strata::kernels::fwht256_inplace_cuda(b.kcur, g.n_head_kv, stream); // Q4_0: rotated K and V (kv_q4.hpp)+} 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);strata::kernels::fwht256_inplace_cuda(b.vcur, g.n_head_kv, stream);+}+if (st.kv_q4) {strata::kernels::kv_append_q4_step(st.k_q4, st.v_q4, st.page_table, st.step, b.kcur, b.vcur, s, stream, &st.host);-} else if (st.kv_int8) kv_append_q8_step(st.k_q, st.v_q, st.k_scale, st.v_scale, st.page_table, st.step, b.kcur, b.vcur, s, stream, &st.host); else kv_append_step(st.k_pool, st.v_pool, st.page_table, st.step, b.kcur, b.vcur, s, stream, &st.host); { const uint64_t nvk = (uint64_t) g.n_head_kv * g.head_dim; const uint64_t base = (uint64_t) 2 * g.n_embd + 2 * g.hc + (uint64_t) g.n_head * g.head_dim + 2 * nvk + 8; if (!st.kv_int8 && !st.kv_hybrid) dump_slot(dump, g, layer, (const float*) st.k_pool, base, nvk / 2, stream); if (!st.kv_int8 && !st.kv_hybrid) dump_slot(dump, g, layer, (const float*) st.v_pool, base + nvk / 2, nvk / 2, stream); dump_slot(dump, g, layer, b.vcur, base + nvk, nvk, stream); dump_slot(dump, g, layer, b.kcur, base + 2 * nvk, nvk, stream); } { const QsaIndexerBuffers ib{st.idx_tail, st.idx_dead, st.idx_pooled, st.idx_block_pos}; if (native_qsa_indexer_enabled()) {+} else if (st.kv_int8) kv_append_q8_step(st.k_q, st.v_q, st.k_scale, st.v_scale, st.page_table, st.step, b.kcur, b.vcur, s, stream, &st.host); else kv_append_step(st.k_pool, st.v_pool, st.page_table, st.step, b.kcur, b.vcur, s, stream, &st.host); } /* not K8V4 */ { const uint64_t nvk = (uint64_t) g.n_head_kv * g.head_dim; const uint64_t base = (uint64_t) 2 * g.n_embd + 2 * g.hc + (uint64_t) g.n_head * g.head_dim + 2 * nvk + 8; if (!st.kv_int8 && !st.kv_hybrid) dump_slot(dump, g, layer, (const float*) st.k_pool, base, nvk / 2, stream); if (!st.kv_int8 && !st.kv_hybrid) dump_slot(dump, g, layer, (const float*) st.v_pool, base + nvk / 2, nvk / 2, stream); dump_slot(dump, g, layer, b.vcur, base + nvk, nvk, stream); dump_slot(dump, g, layer, b.kcur, base + 2 * nvk, nvk, stream); } { const QsaIndexerBuffers ib{st.idx_tail, st.idx_dead, st.idx_pooled, st.idx_block_pos}; if (native_qsa_indexer_enabled()) {try {native_qsa_indexer_append(b.idx_raw, st.step + kStepPos, pos_base,(const float*) w_ikn->data, RMS_EPS, ib, s, st.max_cells, rope_scaling(), stream);@@ -941,7 +946,7 @@ int64_t max_blocks = (st.max_cells / s.idx_block) + 2;}// KV streaming: every block the selection names is made resident before anything reads itqsa_kv_resolve(st, g, b.ids, st.step, 1, cap, stream);- if (st.kv_q4) strata::kernels::fwht256_inplace_cuda(b.qcur, g.n_head, stream); // <Hq, Hk> = <q, k>+ if (st.kv_rot) strata::kernels::fwht256_inplace_cuda(b.qcur, g.n_head, stream); // <Hq, Hk> = <q, k>if (g_fast_attn && !native_flash_attn_short && dump == nullptr) {const strata::kernels::QsaAttnPools pools = qsa_attn_pools(st);strata::kernels::qsa_decode_attn_step(b.qcur, pools, b.ids, st.step, cap, s, b.attn_scratch, b.attn, stream);@@ -968,7 +973,7 @@ int64_t max_blocks = (st.max_cells / s.idx_block) + 2;}} else qsa_attend_step(b.qcur, b.k_scratch, b.v_scratch, st.step, cap, s, b.attn, nullptr, stream);}- if (st.kv_q4 || st.kv_hybrid) strata::kernels::fwht256_inplace_cuda(b.attn, g.n_head, stream); // the output back: H is self-inverse+ if (st.kv_rot || st.kv_hybrid) strata::kernels::fwht256_inplace_cuda(b.attn, g.n_head, stream); // the output back: H is self-inversedump_slot(dump, g, layer, b.attn, (uint64_t) 2 * g.n_embd + 2 * g.hc, (uint64_t) g.n_head * g.head_dim, stream); { const uint64_t vs = (uint64_t) 2 * g.n_embd + 2 * g.hc + (uint64_t) g.n_head * g.head_dim + (uint64_t) 2 * g.n_head_kv * g.head_dim; dump_slot(dump, g, layer, (const float*) b.v_scratch, vs, (uint64_t) g.n_head_kv * g.head_dim / 2, stream); dump_slot(dump, g, layer, (const float*) b.ids, vs + (uint64_t) g.n_head_kv * g.head_dim / 2, 4, stream); dump_slot(dump, g, layer, (const float*) st.step, vs + (uint64_t) g.n_head_kv * g.head_dim / 2 + 4, 4, stream); }// ---- 9. Gate the attention output before its projection. Native CUDA keeps F32// sigmoid/multiply arithmetic and uses Q8_1 for the native quantized projection.diff --git a/src/core/mtp.cpp b/src/core/mtp.cppindex 566783749..90be8162a 100644--- a/src/core/mtp.cpp+++ b/src/core/mtp.cpp@@ -471,7 +471,7 @@ bool MtpDrafter::record_forward(int T, int step_row0, cudaStream_t cs, std::strinative_mmvq(GGML_Q8_0, q8("self_attn.v_proj.weight"), xq_, vcur_, (int) N, (int) (NKV * HD), T, cs);for (int t = 0; t < T; ++t) {norm_rope(kcur_ + t * NKV * HD, f32("self_attn.k_norm.weight"), (int) NKV, (int) HD, pos + t * NH);- if (st_.kv_q4) { // Q4_0 KV (kv_q4.hpp): rotated K and V+ if (st_.kv_rot) { // rotated K and V (kv_q4.hpp)fwht256_inplace_cuda(kcur_ + t * NKV * HD, NKV, cs);fwht256_inplace_cuda(vcur_ + t * NKV * HD, NKV, cs);kv_append_q4_step(st_.k_q4, st_.v_q4, st_.page_table, step + t * 4, kcur_ + t * NKV * HD,@@ -493,12 +493,12 @@ bool MtpDrafter::record_forward(int T, int step_row0, cudaStream_t cs, std::strireturn false;}norm_rope(qc, f32("self_attn.q_norm.weight"), (int) NH, (int) HD, pos + t * NH);- if (st_.kv_q4) fwht256_inplace_cuda(qc, NH, cs);+ if (st_.kv_rot) fwht256_inplace_cuda(qc, NH, cs);}const QsaAttnPools pools = qsa_attn_pools(st_);if (window_ > 0) window_ids(const_cast<int32_t*>(step), T, (int) window_, ident_, cap_, cs);qsa_decode_attn_batch(qcur_, pools, ident_, step, cap_, s, attn_scratch_, attn_, T, cs);- if (st_.kv_q4) fwht256_inplace_cuda(attn_, (int64_t) T * NH, cs);+ if (st_.kv_rot) fwht256_inplace_cuda(attn_, (int64_t) T * NH, cs);for (int t = 0; t < T; ++t)native_qsa_gate_apply(attn_ + t * NH * HD, qfull_ + t * NH * 2 * HD, attn32_ + t * NH * HD, (int) NH, (int) HD, cs);native_quantize_q8_1(attn32_, xq_, (int) (NH * HD), T, cs);diff --git a/src/core/verify.cpp b/src/core/verify.cppindex ebe0c8975..76f4e4884 100644--- a/src/core/verify.cpp+++ b/src/core/verify.cpp@@ -573,7 +573,7 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {native_mmvq(wv->native_type, wv->native_data, xq_, vcur_ + tb * NKV * HD, (int) N, (int) (NKV * HD), n, cs);if (qb) norm_rope(kcur_ + tb * NKV * HD, wkn, (int) (n * NKV), (int) HD, pos_k + tb * NKV);else for (int t = tb; t < te; ++t) norm_rope(kcur_ + t * NKV * HD, wkn, (int) NKV, (int) HD, pos_ + t * NH);- if (st.kv_q4) { // Q4_0 KV (kv_q4.hpp): K and V rotated before they are stored+ if (st.kv_rot) { // K and V rotated before they are stored (kv_q4.hpp)fwht256_inplace_cuda(kcur_ + tb * NKV * HD, (int64_t) n * NKV, cs);fwht256_inplace_cuda(vcur_ + tb * NKV * HD, (int64_t) n * NKV, cs);} else if (st.kv_hybrid) { // K8V4: only V is rotated@@ -613,6 +613,7 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {return false;}norm_rope(qcur_ + tb * NH * HD, wqn, (int) (n * NH), (int) HD, pos_ + tb * NH);+ if (st.kv_rot) fwht256_inplace_cuda(qcur_ + tb * NH * HD, (int64_t) n * NH, cs); // <Hq, Hk> = <q, k>bf16_gemv_fp32_mmvf_multi(mixed_ + tb * N, N, (const uint16_t*) wiq->data, qidx_ + tb * IQ * ID, IQ * ID,N, IQ * ID, n, cs);norm_rope(qidx_ + tb * IQ * ID, wiqn, (int) (n * IQ), (int) ID, pos_i + tb * IQ);@@ -625,7 +626,7 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {return false;}norm_rope(qc, wqn, (int) NH, (int) HD, pos_ + t * NH);- if (st.kv_q4) fwht256_inplace_cuda(qc, NH, cs); // <Hq, Hk> = <q, k>+ if (st.kv_rot) fwht256_inplace_cuda(qc, NH, cs); // <Hq, Hk> = <q, k>}for (int t = tb; t < te; ++t) {float* qx = qidx_ + t * IQ * ID;@@ -646,7 +647,7 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {qsa_decode_attn_batch(qcur_ + tb * NH * HD, pools, sel_ + (size_t) tb * cap_, step_ + tb * kStepCount, cap_,s, attn_scratch_ + (size_t) tb * attn_scratch_floats_, attn_ + tb * NH * HD, n, cs);stamp(l, 13, grp);- if (st.kv_q4 || st.kv_hybrid) fwht256_inplace_cuda(attn_ + tb * NH * HD, (int64_t) n * NH, cs); // back: H^-1 = H+ if (st.kv_rot || st.kv_hybrid) fwht256_inplace_cuda(attn_ + tb * NH * HD, (int64_t) n * NH, cs); // back: H^-1 = Hif (qb) native_qsa_gate_apply(attn_ + tb * NH * HD, qfull_ + tb * NH * 2 * HD, attn32_ + tb * NH * HD,(int) (n * NH), (int) HD, cs);elsediff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cppindex bd8ae5e72..74bf26f82 100644--- a/src/prefill/prefill.cpp+++ b/src/prefill/prefill.cpp@@ -843,9 +843,11 @@ bool Prefill::draft_kv(core::MtpDrafter& mtp, const float* R_rows, const int32_tproj(mixed, mixed_h, w_v, Vc, nb, KV, Nn, 0);rms_rows(Kc, w_kn, nb * g.n_head_kv, g.head_dim, g.head_dim, EPS, m.cs);rope(Kc, nb, g.n_head_kv, g.head_dim, KV, c0, strata::kernels::rope_scaling(), m.cs);- if (st.kv_q4) {+ if (st.kv_rot) { // rotated as the drafter's own decode stores them (mtp.cpp)strata::kernels::fwht256_inplace_cuda(Kc, nb * g.n_head_kv, m.cs);strata::kernels::fwht256_inplace_cuda(Vc, nb * g.n_head_kv, m.cs);+ }+ if (st.kv_q4) {strata::kernels::kv_append_q4(st.k_q4, st.v_q4, st.page_table, c0, nb, Kc, Vc, s, m.cs, &st.host);} else {kv_append(Kc, Vc, nb, c0, st.page_table, s.page_size, st.kv_int8 ? nullptr : st.k_pool,@@ -1376,20 +1378,23 @@ bool Prefill::run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& estaged ? &m.stage : 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);- } else if (st.kv_q4) { // Q4_0 KV (kv_q4.hpp): rotated K and V, the queries below too, the output back- strata::kernels::fwht256_inplace_cuda(m.Kc, T * 2, m.cs);- strata::kernels::fwht256_inplace_cuda(m.Vc, T * 2, m.cs);- strata::kernels::kv_append_q4(st.k_q4, st.v_q4, st.page_table, p0, T, m.Kc, m.Vc, s, m.cs,- &st.host, staged ? &m.stage : nullptr);} else {- kv_append(m.Kc, m.Vc, T, p0, st.page_table, s.page_size, st.kv_int8 ? nullptr : st.k_pool,- st.kv_int8 ? nullptr : st.v_pool, st.k_q, st.v_q, st.k_scale, st.v_scale, m.cs,- &st.host, staged ? &m.stage : nullptr);+ 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);+ strata::kernels::fwht256_inplace_cuda(m.Vc, T * 2, m.cs);+ }+ if (st.kv_q4)+ strata::kernels::kv_append_q4(st.k_q4, st.v_q4, st.page_table, p0, T, m.Kc, m.Vc, s, m.cs,+ &st.host, staged ? &m.stage : nullptr);+ else+ kv_append(m.Kc, m.Vc, T, p0, st.page_table, s.page_size, st.kv_int8 ? nullptr : st.k_pool,+ st.kv_int8 ? nullptr : st.v_pool, st.k_q, st.v_q, st.k_scale, st.v_scale, m.cs,+ &st.host, staged ? &m.stage : nullptr);}split_q(m.Qf, m.q, T, m.cs);rms_rows(m.q, (const float*) wqn->data, T * 24, 256, 256, EPS, m.cs);rope(m.q, T, 24, 256, 6144, p0, strata::kernels::rope_scaling(), m.cs);- if (st.kv_q4) strata::kernels::fwht256_inplace_cuda(m.q, T * 24, m.cs);+ if (st.kv_rot) strata::kernels::fwht256_inplace_cuda(m.q, T * 24, m.cs);rms_rows(m.q_idx, (const float*) wiqn->data, T * 4, 128, 128, EPS, m.cs);rope(m.q_idx, T, 4, 128, 512, p0, strata::kernels::rope_scaling(), m.cs);// the indexer appends, token by token; then scores + selection for many queries at once:@@ -1530,7 +1535,7 @@ bool Prefill::run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& em.steps_dev + t0 * strata::kernels::kStepCount, m.cap,s, m.attn_scratch, m.attn + t0 * ZV, nb, m.cs);}- if (st.kv_q4 || st.kv_hybrid) strata::kernels::fwht256_inplace_cuda(m.attn, T * 24, m.cs);+ if (st.kv_rot || st.kv_hybrid) strata::kernels::fwht256_inplace_cuda(m.attn, T * 24, m.cs);pt.mark(kPfQsa, cs);gate_attn(m.attn, m.Qf, m.attn_h, T, m.cs);if (!native_proj(m.gemm, wo, m.attn_h, m.bo, T, v.name("attn_output.weight"), err)) return false;diff --git a/src/program/generate.cpp b/src/program/generate.cppindex 18668d669..e3cf26a5d 100644--- a/src/program/generate.cpp+++ b/src/program/generate.cpp@@ -1391,7 +1391,11 @@ int main(int argc, char** argv) {return 2;}strata::core::qsa_set_kv_int8(o.kv == "int8");- strata::core::qsa_set_kv_q4(o.kv == "q4_0"); // PR #21: 4-bit codes after a Hadamard rotation (kv_q4.hpp)+ strata::core::qsa_set_kv_q4(o.kv == "q4_0");+ // STRATA_KV_ROT=1: INT8 K/V through the Hadamard rotation --kv q4_0 already uses. Opt-in: first-token KL to+ // fp16 K/V improved on an NVFP4 pack (0.0066 -> 0.0051) but not on IQ2_XS (0.0022 -> 0.0054)+ const char* kv_rot = std::getenv("STRATA_KV_ROT");+ strata::core::qsa_set_kv_int8_rotate(kv_rot != nullptr && kv_rot[0] == '1'); // PR #21: 4-bit codes after a Hadamard rotation (kv_q4.hpp)strata::core::qsa_set_kv_hybrid(o.kv == "k8v4"); // K8V4: INT8 K + rotated Q4_0 V, 816 B/cellif (o.kv_resident < 0) {std::fprintf(stderr, "strata generate: --kv-resident must be >= 0\n");