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++

#293 follow-up: the drafter stores rotated INT8 K/V as INT8; the rotation is part of the snapshot format

Niko1221c81244d4e9ac
diff --git a/src/core/conversation_snapshot.cpp b/src/core/conversation_snapshot.cpp
index f7bc09883..759daa65a 100644
--- a/src/core/conversation_snapshot.cpp
+++ b/src/core/conversation_snapshot.cpp
@@ -43,8 +43,12 @@ bool layout(const QsaState& st, const ModelGeometry& g, int64_t upto, bool index
// Include the moving spare row, not only completed blocks. The checkpoint
// restore reconstructs that row when rewinding to an earlier prefix.
const int64_t pooled = index && upto > 0 ? upto / s.idx_block + 1 : 0;
- // Format 3 identifies snapshot K8V4 only; never pass it to the block movers.
- l = {st.kv_hybrid ? 3 : qsa_kv_format(st), cells, pooled, s.page_size, 0, 0, 0, 0, 0};
+ // Format 3 identifies snapshot K8V4 only; never pass it to the block movers. Rotated INT8 K/V (#293,
+ // STRATA_KV_ROT=1) is another format too (+16): its bytes mean nothing to a state that does not rotate, so a
+ // snapshot or prompt checkpoint taken with the rotation never restores into one without it, nor the reverse.
+ // (Q4_0 is always rotated: nothing to tell apart; without the rotation the format is the one it always was.)
+ const int rotated = st.kv_rot && !st.kv_q4 && !st.kv_hybrid ? 16 : 0;
+ l = {(st.kv_hybrid ? 3 : qsa_kv_format(st)) + rotated, cells, pooled, s.page_size, 0, 0, 0, 0, 0};
using conversation_detail::product;
if (!product(l.data, {(uint64_t) cells, (uint64_t) g.n_head_kv, per}) ||
!product(l.scales, {(uint64_t) cells, (uint64_t) g.n_head_kv,
diff --git a/src/core/conversation_snapshot_test.cpp b/src/core/conversation_snapshot_test.cpp
index bf732f3fa..da95113c6 100644
--- a/src/core/conversation_snapshot_test.cpp
+++ b/src/core/conversation_snapshot_test.cpp
@@ -259,6 +259,17 @@ int main() {
}
auto bad=a; bad.head_dim++;
check(!conversation_kv_restore(bad,f.state,f.g,upto,index,err),"reject incompatible geometry");
+ if (fmt==kKvInt8) {
+ // #293: INT8 K/V saved through the Hadamard rotation (STRATA_KV_ROT=1) never restores into a state
+ // without it, nor the reverse
+ f.state.kv_rot=true;
+ ConversationKv rotated;
+ check(conversation_kv_save(rotated,f.state,f.g,upto,index,err),"save rotated INT8");
+ check(!conversation_kv_restore(a,f.state,f.g,upto,index,err),"unrotated image into a rotated state rejected");
+ f.state.kv_rot=false;
+ check(!conversation_kv_restore(rotated,f.state,f.g,upto,index,err),"rotated image into an unrotated state rejected");
+ check(conversation_kv_restore(a,f.state,f.g,upto,index,err),"unrotated image still restores");
+ }
if (!a.k.empty()) {
bad=a; bad.k.pop_back();
check(!conversation_kv_restore(bad,f.state,f.g,upto,index,err),"reject malformed payload");
diff --git a/src/core/mtp.cpp b/src/core/mtp.cpp
index 90be8162a..17e36556a 100644
--- a/src/core/mtp.cpp
+++ b/src/core/mtp.cpp
@@ -471,12 +471,15 @@ bool MtpDrafter::record_forward(int T, int step_row0, cudaStream_t cs, std::stri
native_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_rot) { // rotated K and V (kv_q4.hpp)
+ if (st_.kv_rot) { // rotated K and V (kv_q4.hpp): Q4_0, and INT8 with STRATA_KV_ROT=1
fwht256_inplace_cuda(kcur_ + t * NKV * HD, NKV, cs);
fwht256_inplace_cuda(vcur_ + t * NKV * HD, NKV, cs);
+ }
+ // stored in the state's own format (#293 appended rotated INT8 K/V as Q4_0, into pools INT8 never has)
+ if (st_.kv_q4)
kv_append_q4_step(st_.k_q4, st_.v_q4, st_.page_table, step + t * 4, kcur_ + t * NKV * HD,
vcur_ + t * NKV * HD, s, cs, &st_.host);
- } else if (st_.kv_int8)
+ else if (st_.kv_int8)
kv_append_q8_step(st_.k_q, st_.v_q, st_.k_scale, st_.v_scale, st_.page_table, step + t * 4,
kcur_ + t * NKV * HD, vcur_ + t * NKV * HD, s, cs, &st_.host);
else
diff --git a/src/program/generate.cpp b/src/program/generate.cpp
index e3cf26a5d..f91a9177a 100644
--- a/src/program/generate.cpp
+++ b/src/program/generate.cpp
@@ -1391,11 +1391,13 @@ 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");
+ strata::core::qsa_set_kv_q4(o.kv == "q4_0"); // PR #21: 4-bit codes after a Hadamard rotation (kv_q4.hpp)
// 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_int8_rotate(kv_rot != nullptr && kv_rot[0] == '1');
+ if (kv_rot != nullptr && kv_rot[0] == '1' && o.kv == "int8")
+ std::fprintf(stderr, "strata generate: STRATA_KV_ROT=1: INT8 K/V through the Hadamard rotation (opt-in)\n");
strata::core::qsa_set_kv_hybrid(o.kv == "k8v4"); // K8V4: INT8 K + rotated Q4_0 V, 816 B/cell
if (o.kv_resident < 0) {
std::fprintf(stderr, "strata generate: --kv-resident must be >= 0\n");