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

Match upstream K8V4 flags when sizing conversation snapshots

Jeremiah Ritchey055bc16e2ace
diff --git a/src/core/conversation_snapshot.cpp b/src/core/conversation_snapshot.cpp
index b085da83e..796d5c8be 100644
--- a/src/core/conversation_snapshot.cpp
+++ b/src/core/conversation_snapshot.cpp
@@ -25,19 +25,20 @@ 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 identity-layout INT8 K";
+ 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";
return false;
}
const auto s = strata::kernels::qsa_real_shapes();
+ const bool int8_keys = st.kv_int8 || st.kv_hybrid;
if (g.n_head_kv <= 0 || g.head_dim <= 0 || g.head_dim > INT32_MAX || g.idx_key_dim <= 0 ||
- (st.kv_q4 && g.head_dim % 32) || (st.kv_int8 && !st.kv_q4 && g.head_dim % 64)) {
+ (st.kv_q4 && g.head_dim % 32) || (int8_keys && !st.kv_q4 && g.head_dim % 64)) {
error = "conversation snapshot: invalid K/V geometry";
return false;
}
const int64_t cells = ((upto + s.page_size - 1) / s.page_size) * s.page_size;
const size_t per = st.kv_q4 ? (size_t) strata::kernels::kv_q4_bytes_per_head((int) g.head_dim)
- : (size_t) g.head_dim * (st.kv_int8 ? 1 : 2);
+ : (size_t) g.head_dim * (int8_keys ? 1 : 2);
// 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;
@@ -46,7 +47,7 @@ bool layout(const QsaState& st, const ModelGeometry& g, int64_t upto, bool index
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,
- st.kv_int8 && !st.kv_q4 ? (uint64_t) (g.head_dim / 64) * 2 : 0}) ||
+ int8_keys && !st.kv_q4 ? (uint64_t) (g.head_dim / 64) * 2 : 0}) ||
!product(l.pooled, {(uint64_t) pooled, (uint64_t) g.idx_key_dim, sizeof(float)})) {
error = "conversation snapshot: K/V byte count overflow";
return false;
diff --git a/src/core/conversation_snapshot_test.cpp b/src/core/conversation_snapshot_test.cpp
index f6b8e6fbf..5b0343bd9 100644
--- a/src/core/conversation_snapshot_test.cpp
+++ b/src/core/conversation_snapshot_test.cpp
@@ -42,10 +42,10 @@ struct Fixture {
}
Fixture(int fmt, int mode) {
auto& st = state;
- st.kv_mode = mode; st.kv_int8 = fmt==kKvInt8 || fmt==3; st.kv_q4 = fmt==kKvQ4; st.kv_hybrid = fmt==3;
+ st.kv_mode = mode; st.kv_int8 = fmt==kKvInt8; st.kv_q4 = fmt==kKvQ4; st.kv_hybrid = fmt==3;
st.n_pages = 24; st.n_slots = mode ? 4 : st.n_pages;
st.max_cells = st.n_pages * 4; st.idx_pooled_rows = mode==2 ? 2 : st.max_cells/4+2;
- const size_t per = fmt==kKvQ4 ? kv_q4_bytes_per_head((int)g.head_dim) : g.head_dim*(st.kv_int8 ? 1:2);
+ const size_t per = fmt==kKvQ4 ? kv_q4_bytes_per_head((int)g.head_dim) : g.head_dim*((st.kv_int8 || st.kv_hybrid) ? 1:2);
const size_t rows = st.max_cells*g.n_head_kv, slot_rows = st.n_slots*4*g.n_head_kv;
sizes = {rows*per, rows*per, fmt==kKvInt8 ? rows*(g.head_dim/64)*2:0,
fmt==kKvInt8 ? rows*(g.head_dim/64)*2:0, (size_t)st.idx_pooled_rows*g.idx_key_dim*4};
diff --git a/tools/agent_cache_task.mjs b/tools/agent_cache_task.mjs
index 13f6676c4..e48c7a78d 100644
--- a/tools/agent_cache_task.mjs
+++ b/tools/agent_cache_task.mjs
@@ -41,9 +41,11 @@ async function makeSession() {
let left = 16000;
return {content: event.content.map(part => {
if (part.type !== 'text') return part;
- const text = part.text.slice(0, left);
+ const marker = '\n[benchmark output limit]';
+ const text = part.text.length <= left ? part.text
+ : left >= marker.length ? part.text.slice(0, left - marker.length) + marker : '';
left -= text.length;
- return {...part, text: text + (text.length < part.text.length ? '\n[benchmark output limit]' : '')};
+ return {...part, text};
})};
});
}],