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

- mtp.cpp record_forward: the `if (st_.kv_rot)` branch rotated K and V and then always appended with
  kv_append_q4_step - for INT8 + STRATA_KV_ROT=1 into Q4_0 pools the state does not have. The rotation and the
  append are separate now: rotate when kv_rot, then append in the state's own format (Q4_0, INT8, FP16).
- conversation_snapshot.cpp: rotated INT8 K/V is its own snapshot format (+16), so a conversation snapshot or prompt
  checkpoint taken with the rotation is refused by a state without it and the reverse ("incompatible K/V
  geometry"); without the rotation the format is unchanged. conversation_snapshot_test checks both directions.
- generate: says when STRATA_KV_ROT=1 applies; the q4_0 line's comment back.
This commit is contained in:
Niko1221 committed 2026-10-01 08:45:09 +02:00
1 parent f133c655a8
commit c81244d4e9
4 files changed
+26 -6

No files matched your search

+6 -2
View File
@@ -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,
+11
View File
@@ -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");
+5 -2
View File
@@ -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
+4 -2
View File
@@ -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");