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

Verify restored draft KV and fail incomplete state fingerprints

Jeremiah Ritchey13fd32a227d3
diff --git a/include/strata/core/conversation_snapshot.hpp b/include/strata/core/conversation_snapshot.hpp
index a135e3101..550e373a7 100644
--- a/include/strata/core/conversation_snapshot.hpp
+++ b/include/strata/core/conversation_snapshot.hpp
@@ -18,6 +18,11 @@ bool conversation_kv_validate(const ConversationKv& image, const QsaState& state
int64_t upto, bool include_index, std::string& error);
bool conversation_kv_restore(const ConversationKv& image, const QsaState& state, const ModelGeometry& g,
int64_t upto, bool include_index, std::string& error);
+// Diagnostic read-back after a synchronized restore. Uses 64 KiB of stack
+// workspace, compares authoritative bytes and resident draft-ring pages, and
+// fingerprints the authoritative payload only. Never changes model state.
+bool conversation_kv_verify(const ConversationKv& image, const QsaState& state, const ModelGeometry& g,
+ int64_t upto, bool include_index, uint64_t& fingerprint, std::string& error);
struct ConversationStateSizes {
size_t gdn = 0, ple = 0, tail = 0, dead = 0, block_pos = 0;
diff --git a/src/core/conversation_snapshot.cpp b/src/core/conversation_snapshot.cpp
index 796d5c8be..3e1254a58 100644
--- a/src/core/conversation_snapshot.cpp
+++ b/src/core/conversation_snapshot.cpp
@@ -4,6 +4,7 @@
#include <cuda_runtime.h>
#include <array>
+#include <cstring>
#include <limits>
namespace strata::core {
@@ -65,8 +66,8 @@ bool layout(const QsaState& st, const ModelGeometry& g, int64_t upto, bool index
return true;
}
-std::array<void*, 5> pools(const QsaState& st) {
- const bool host = st.kv_mode != 0;
+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_q4)
return {host ? st.host.k_q4 : st.k_q4, host ? st.host.v_q4 : st.v_q4, nullptr, nullptr, st.idx_pooled};
@@ -188,4 +189,42 @@ bool conversation_kv_restore(const ConversationKv& image, const QsaState& st, co
return false;
}
+bool conversation_kv_verify(const ConversationKv& image, const QsaState& st, const ModelGeometry& g,
+ int64_t upto, bool index, uint64_t& fingerprint, std::string& error) {
+ if (!conversation_kv_validate(image, st, g, upto, index, error)) return false;
+ std::array<uint8_t, 65536> buffer;
+ uint64_t hash = 1469598103934665603ull;
+ auto compare = [&](const void* device, const uint8_t* expected, size_t bytes, bool include_hash) {
+ for (size_t offset = 0; offset < bytes;) {
+ const size_t n = std::min(buffer.size(), bytes - offset);
+ if (!transfer(buffer.data(), static_cast<const uint8_t*>(device) + offset, n, error)) return false;
+ if (std::memcmp(buffer.data(), expected + offset, n) != 0) {
+ error = "conversation snapshot: restored K/V bytes differ";
+ return false;
+ }
+ if (include_hash) for (size_t i = 0; i < n; ++i) { hash ^= buffer[i]; hash *= 1099511628211ull; }
+ offset += n;
+ }
+ return true;
+ };
+ const std::array<const std::vector<uint8_t>*, 5> saved = {&image.k, &image.v, &image.k_scale, &image.v_scale, &image.pooled};
+ const auto authoritative = pools(st);
+ for (size_t i = 0; i < saved.size(); ++i)
+ if (!compare(authoritative[i], saved[i]->data(), saved[i]->size(), true)) return false;
+ if (st.kv_mode == 2 && image.cells > 0) {
+ const auto resident = pools(st, true);
+ const int64_t end = image.cells / image.page_size;
+ const int64_t begin = std::max<int64_t>(0, end - st.n_slots);
+ for (size_t i = 0; i < 4; ++i) {
+ if (saved[i]->empty()) continue;
+ const size_t page_bytes = saved[i]->size() / size_t(end);
+ for (int64_t page = begin; page < end; ++page)
+ if (!compare(static_cast<const uint8_t*>(resident[i]) + size_t(page % st.n_slots) * page_bytes,
+ saved[i]->data() + size_t(page) * page_bytes, page_bytes, false)) return false;
+ }
+ }
+ fingerprint = hash;
+ return true;
+}
+
} // namespace strata::core
diff --git a/src/core/conversation_snapshot_test.cpp b/src/core/conversation_snapshot_test.cpp
index 5b0343bd9..515a64088 100644
--- a/src/core/conversation_snapshot_test.cpp
+++ b/src/core/conversation_snapshot_test.cpp
@@ -197,6 +197,8 @@ int main() {
cuda_check(cudaDeviceSynchronize());
check(conversation_kv_save(restored,f.state,f.g,upto,index,err),"read back restored A");
check(equal(a,restored),"A/B/A byte-exact K/V and indexer state");
+ uint64_t fingerprint = 0;
+ check(conversation_kv_verify(a,f.state,f.g,upto,index,fingerprint,err),"verify restored authoritative and resident bytes without rewriting them");
if (mode==1) {
std::vector<int32_t> table((size_t)f.state.n_pages);
cuda_check(cudaMemcpy(table.data(),f.state.page_table,table.size()*4,cudaMemcpyDeviceToHost));
@@ -205,8 +207,6 @@ int main() {
if (mode==2 && upto>0) {
const auto s=qsa_real_shapes();
const int64_t b1=(upto+s.page_size-1)/s.page_size,b0=std::max<int64_t>(0,b1-f.state.n_slots);
- kv_ring_restore(qsa_attn_pools(f.state),f.state.host,fmt,b0,b1,f.state.n_slots,s,nullptr);
- cuda_check(cudaDeviceSynchronize());
const size_t block=kv_block_bytes(s,fmt)/2;
// Codes and scales are separate in INT8; check code bytes here.
const size_t code_block=fmt==kKvInt8 ? s.page_size*s.n_head_kv*s.head_dim : block;
diff --git a/src/core/conversation_validation_test.cpp b/src/core/conversation_validation_test.cpp
index 13d9b7c4f..3676b93de 100644
--- a/src/core/conversation_validation_test.cpp
+++ b/src/core/conversation_validation_test.cpp
@@ -171,6 +171,13 @@ void fixture(int format, int experts, bool zero_qsa, bool ple) {
"injected host transfer backend completes a valid restore");
check(gdn==image.live.gdn && history==image.live.ple && ss.ple_prev[0]==8 && ss.ple_prev[1]==9,
"successful restore publishes correct running state and PLE window");
+ uint64_t fingerprint = 0;
+ check(conversation_kv_verify(image.kv.back(),draft.st,g,9,false,fingerprint,error), "read-back verifies complete draft payload");
+ draft.data[0][0] ^= 1;
+ check(!conversation_kv_verify(image.kv.back(),draft.st,g,9,false,fingerprint,error), "read-back detects corrupted draft byte");
+ draft.data[0][0] ^= 1;
+ fail_copy = copy_calls + 1;
+ check(!conversation_kv_verify(image.kv.back(),draft.st,g,9,false,fingerprint,error), "read-back transfer failure is not a successful fingerprint");
#endif
}
}
diff --git a/src/program/generate.cpp b/src/program/generate.cpp
index 7ee627bee..d98d43f40 100644
--- a/src/program/generate.cpp
+++ b/src/program/generate.cpp
@@ -3360,6 +3360,9 @@ int main(int argc, char** argv) {
// E-9: batched through the prompt path when it can (one GPU: a layer split's drafter is on the last stage)
const bool batched = !multi_gpu && sp.draft_kv(mtp, R_rows, nxt.data(), T, p0, e);
if (!e.empty() || (!batched && !mtp.prefill(R_rows, nxt.data(), T, p0, e))) return false;
+ if (std::getenv("STRATA_SNAPSHOT_VERIFY") != nullptr)
+ std::fprintf(stderr, "strata serve: DRAFT_PREFILL path=%s mode=%d cells=%lld\n",
+ batched ? "batched" : "token", mtp.kv_state().kv_mode, (long long) T);
// progress for the server window: PP <position reached> <prompt tokens> <ms> <fresh tokens/s>
const int64_t done = p0 + T;
const double ms = std::chrono::duration<double, std::milli>(Clock::now() - pp_t0).count();
@@ -3864,6 +3867,17 @@ int main(int argc, char** argv) {
std::printf("ERR restoring parked conversation: %s\n", err.c_str());
return 1;
}
+ if (std::getenv("STRATA_SNAPSHOT_VERIFY") != nullptr) {
+ uint64_t draft_hash = 0;
+ if (!strata::core::conversation_kv_verify(incoming->kv.back(), mtp.kv_state(), g,
+ int64_t(incoming->live.ids.size()), false, draft_hash, err)) {
+ std::printf("ERR verifying restored draft KV: %s\n", err.c_str());
+ return 1;
+ }
+ std::fprintf(stderr, "strata serve: SNAPSHOT_VERIFY draft=%016llx cells=%lld mode=%d source=%s\n",
+ (unsigned long long) draft_hash, (long long) incoming->kv.back().cells,
+ mtp.kv_state().kv_mode, "ram");
+ }
live = std::move(incoming->live.ids);
live_imgs = std::move(incoming->live.imgs);
checks = std::move(incoming->checkpoints);
@@ -4309,17 +4323,30 @@ int main(int argc, char** argv) {
if (state_hash && live_ok) {
// DEBUG: a fingerprint of every part of the session over the positions it holds ([0, L)), and
// separately of what lies past them in the last KV page (stale cells, fine unless something reads them)
- cudaDeviceSynchronize();
+ if (cudaDeviceSynchronize() != cudaSuccess) {
+ std::printf("ERR synchronizing state fingerprint\n");
+ return 1;
+ }
const int64_t L = (int64_t) live.size();
const strata::kernels::QsaShapes qs = [&] {
strata::kernels::QsaShapes s = strata::kernels::qsa_real_shapes();
s.n_head_kv = g.n_head_kv; s.head_dim = g.head_dim; s.idx_dim = g.idx_key_dim;
return s;
}();
+ bool hash_ok = true;
+ std::array<uint8_t, 65536> hash_buffer;
auto hash_dev = [&](const void* p, size_t bytes, uint64_t h) {
- std::vector<uint8_t> b(bytes);
- if (bytes) cudaMemcpy(b.data(), p, bytes, cudaMemcpyDefault); // VRAM or a streamed host copy
- return fnv1a(b.data(), b.size(), h);
+ for (size_t offset = 0; hash_ok && offset < bytes;) {
+ const size_t n = std::min(hash_buffer.size(), bytes - offset);
+ // VRAM or a streamed host copy, with fixed diagnostic workspace.
+ if (cudaMemcpy(hash_buffer.data(), static_cast<const uint8_t*>(p) + offset, n, cudaMemcpyDefault) != cudaSuccess) {
+ hash_ok = false;
+ break;
+ }
+ h = fnv1a(hash_buffer.data(), n, h);
+ offset += n;
+ }
+ return h;
};
// the cells [c0, c1) of one int8 K or V pool ([page][kv_head][page_size][head_dim]), bytes per value `w`
auto hash_cells = [&](const void* pool, int64_t per_cell, int64_t c0, int64_t c1, uint64_t h) {
@@ -4386,6 +4413,10 @@ int main(int argc, char** argv) {
const int64_t mL = std::min<int64_t>(L, ms.max_cells);
for (const auto& [pool, w] : kv_arrays(ms))
if (pool != nullptr) h_mtp = hash_cells(pool, w, 0, mL, h_mtp);
+ if (!hash_ok) {
+ std::printf("ERR reading state fingerprint\n");
+ return 1;
+ }
std::fprintf(stderr, "strata serve: STATE_HASH L=%lld gdn=%016llx ple=%016llx tail=%016llx pooled=%016llx "
"kv=%016llx mtp=%016llx stale=%016llx dead=%016llx ple_prev=%d,%d\n", (long long) L,
(unsigned long long) h_gdn, (unsigned long long) h_ple, (unsigned long long) h_tail,