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

Parking with a layer split: the draft ring with the last stage, on its GPU; guards; trim log

benoit lange2871ff4b681b
diff --git a/src/core/conversation_snapshot_test.cpp b/src/core/conversation_snapshot_test.cpp
index 215ec560d..1f1de53c0 100644
--- a/src/core/conversation_snapshot_test.cpp
+++ b/src/core/conversation_snapshot_test.cpp
@@ -220,6 +220,10 @@ void full_session(int fmt, int mode, int experts) {
check(conversation_snapshot_save(incremental,view,ss,g,nullptr,err,std::move(reuse),&reused),"incremental stage capture");
check(reused>0 && incremental.bytes()<=peak && equal(incremental.kv[0],fresh.kv[0]) &&
incremental.live.gdn==fresh.live.gdn,"an incremental stage capture reuses pages and equals a full one");
+ SavedConversation split_image=stage;
+ split_image.stage_images.push_back(stage);
+ check(!conversation_snapshot_validate(split_image,ss,g,draft.state,err),
+ "a layer split's image is refused by the whole-session form");
ConversationKvReuse wrong{a.kv,65,65,{}};
check(!conversation_snapshot_capture_bytes(wrong,view,ss,g,nullptr,peak,err),"a draft image's K/V is not a stage's reuse");
}
diff --git a/src/core/conversation_state.cpp b/src/core/conversation_state.cpp
index 1a45e7f14..981beca16 100644
--- a/src/core/conversation_state.cpp
+++ b/src/core/conversation_state.cpp
@@ -275,7 +275,8 @@ bool conversation_snapshot_save(SavedConversation& image, const ConversationView
bool conversation_snapshot_validate(const SavedConversation& image, const SessionState& ss,
const ModelGeometry& g, const QsaState* draft, std::string& error) {
- if (!image.live.stage_parts.empty()) return fail(error, "layer-split parking is not supported");
+ if (!image.live.stage_parts.empty())
+ return fail(error, "a running checkpoint's stage parts belong in the stage images, not in live.stage_parts");
if (image.geometry != geometry_key(g)) return fail(error, "incompatible runtime geometry");
// an image holds exactly one carve's running state and K/V: same layer range or nothing
if (image.layer_lo != ss.layer_lo || image.layer_hi != ss.layer_hi)
@@ -320,6 +321,8 @@ bool conversation_snapshot_save(SavedConversation& image, const ConversationView
}
bool conversation_snapshot_validate(const SavedConversation& image, const SessionState& ss, const ModelGeometry& g,
const QsaState& draft, std::string& error) {
+ // the whole-session form: a layer split's image (stage images) is restored stage by stage, never through here
+ if (!image.stage_images.empty()) return fail(error, "a layer split's image restored as a single session");
return conversation_snapshot_validate(image, ss, g, &draft, error);
}
ConversationRestore conversation_snapshot_restore(const SavedConversation& image, SessionState& ss,
diff --git a/src/program/generate.cpp b/src/program/generate.cpp
index 6b0f33bcf..99e1e8a85 100644
--- a/src/program/generate.cpp
+++ b/src/program/generate.cpp
@@ -1398,6 +1398,11 @@ int main(int argc, char** argv) {
for (int d = 1; d < n_dev && (split_auto || split_devs.size() < split_at.size()); ++d) split_devs.push_back(d);
if (ok && !split_auto && split_devs.empty() && split_at.size() == 1) split_devs.push_back(0); // one GPU
split_same = ok && split_devs.size() == 1 && split_devs[0] == 0 && !split_auto;
+ if (split_same && o.conversation_cache_mib > 0 && o.conversation_cache_slots > 0 && o.prompt_cache > 0) {
+ std::fprintf(stderr, "strata serve: conversation parking does not support a layer split on one GPU "
+ "(--split-device 0); disable parking with --conversation-cache-mib 0\n");
+ return 2;
+ }
if (ok && split_auto && split_devs.empty()) {
std::fprintf(stderr, "strata generate: --layer-split auto: one GPU visible, so no split\n");
o.layer_split.clear();
@@ -2003,6 +2008,12 @@ int main(int argc, char** argv) {
std::ifstream idx(o.pack + "/index.txt");
for (std::string ln; std::getline(idx, ln);)
if (!ln.empty() && ln[0] != '#') pack_names.push_back(ln.substr(0, ln.find(' ')));
+ if (pack_names.empty()) {
+ std::fprintf(stderr, "strata generate: --trim-stage-weights: cannot read %s/index.txt\n", o.pack.c_str());
+ return 1;
+ }
+ std::fprintf(stderr, "strata generate: --trim-stage-weights: CUDA0 loads the dense weights of layers 0-%lld\n",
+ (long long) split_at[0] - 1);
}
auto range_skip = [&](int64_t lo, int64_t hi) { // skip + every blk.<l>. tensor outside [lo, hi)
std::set<std::string> out = skip;
@@ -2337,6 +2348,9 @@ int main(int argc, char** argv) {
// --trim-stage-weights: this stage's layers only
const int64_t s_lo = trim ? split_at[i] : 0, s_hi = trim ? (i + 1 < split_at.size() ? split_at[i + 1] : g.n_layers) : -1;
const std::set<std::string> skip_s = trim ? range_skip(s_lo, s_hi) : skip;
+ if (trim)
+ std::fprintf(stderr, "strata generate: --trim-stage-weights: CUDA%d loads the dense weights of layers %lld-%lld\n",
+ st.dev, (long long) s_lo, (long long) s_hi - 1);
uint64_t pool_s = pool_bytes;
if (trim && !strata::core::WeightTable::pool_bytes(o.pack, pool_s, err, &skip_s)) {
std::fprintf(stderr, "strata generate: layer split, CUDA%d: %s\n", st.dev, err.c_str());
@@ -4663,6 +4677,10 @@ int main(int argc, char** argv) {
// later stage (without it - the draft ring is saved once). The checkpoints are MOVED apart into their
// stage parts for the capture and put back together whichever way this ends (no running state copied).
const size_t n_st = stages.size();
+ // the draft layer's K/V lives on the last stage's GPU (the drafter is loaded there): with a split it is
+ // saved with that stage's image, under its device; stage 0's image holds none
+ const strata::core::QsaState* draft0 = n_st > 0 ? nullptr : &mtp.kv_state();
+ auto draft_of = [&](size_t k) -> const strata::core::QsaState* { return k + 1 == n_st ? &mtp.kv_state() : nullptr; };
strata::core::ConversationCheckpointSplit cs;
if (n_st > 0) cs = strata::core::conversation_checkpoints_split(std::move(checks), n_st);
struct MergeBack {
@@ -4681,7 +4699,7 @@ int main(int argc, char** argv) {
const strata::core::OnDevice on(st->dev);
const strata::core::ConversationView view_k{live, live_imgs, cs.parts[k], cvec_cached};
size_t b = 0;
- if (!strata::core::conversation_snapshot_bytes(view_k, st->ss, g, nullptr, b, err)) {
+ if (!strata::core::conversation_snapshot_bytes(view_k, st->ss, g, draft_of(k), b, err)) {
std::fprintf(stderr, "strata serve: conversation cache: skip parking (stage CUDA%d: %s)\n",
st->dev, err.c_str());
err.clear();
@@ -4689,7 +4707,7 @@ int main(int argc, char** argv) {
}
if (!stage_reuse[k].kv.empty()) {
size_t r = 0;
- if (strata::core::conversation_snapshot_capture_bytes(stage_reuse[k], view_k, st->ss, g, nullptr,
+ if (strata::core::conversation_snapshot_capture_bytes(stage_reuse[k], view_k, st->ss, g, draft_of(k),
r, err)) {
b = r;
stage_retained += stage_reuse[k].bytes();
@@ -4701,14 +4719,14 @@ int main(int argc, char** argv) {
stage_estimate += b;
}
size_t estimate = 0;
- if (!strata::core::conversation_snapshot_bytes(view, ss, g, mtp.kv_state(), estimate, err)) {
+ if (!strata::core::conversation_snapshot_bytes(view, ss, g, draft0, estimate, err)) {
std::fprintf(stderr, "strata serve: conversation cache: skip parking (%s)\n", err.c_str());
err.clear(); // A recoverable miss must not poison the batched draft prefill's error channel.
return true;
}
const size_t fresh_estimate = estimate;
if (!reuse.kv.empty() && !strata::core::conversation_snapshot_capture_bytes(
- reuse, view, ss, g, mtp.kv_state(), estimate, err)) {
+ reuse, view, ss, g, draft0, estimate, err)) {
reuse = {};
estimate = fresh_estimate;
err.clear();
@@ -4738,15 +4756,15 @@ int main(int argc, char** argv) {
}
strata::core::SavedConversation image;
size_t reused_bytes = 0;
- if (!strata::core::conversation_snapshot_save(image, view, ss, g, mtp.kv_state(), err,
+ if (!strata::core::conversation_snapshot_save(image, view, ss, g, draft0, err,
std::move(reuse), &reused_bytes)) return false;
- for (size_t k = 0; k < n_st; ++k) { // the later stages, without the draft layer's K/V
+ for (size_t k = 0; k < n_st; ++k) { // the later stages (the last one with the draft layer's K/V)
auto& st = stages[k];
const strata::core::OnDevice on(st->dev);
if (cudaDeviceSynchronize() != cudaSuccess) { err = "stage sync failed"; return false; }
const strata::core::ConversationView view_k{live, live_imgs, cs.parts[k], cvec_cached};
strata::core::SavedConversation part;
- if (!strata::core::conversation_snapshot_save(part, view_k, st->ss, g, nullptr, err,
+ if (!strata::core::conversation_snapshot_save(part, view_k, st->ss, g, draft_of(k), err,
std::move(stage_reuse[k]), &reused_bytes))
return false;
image.stage_images.push_back(std::move(part));
@@ -5696,7 +5714,7 @@ int main(int argc, char** argv) {
if (incoming) for (size_t i = 0; i < stages.size(); ++i) {
const strata::core::OnDevice on(stages[i]->dev);
if (!strata::core::conversation_snapshot_validate(incoming->stage_images[i], stages[i]->ss, g,
- nullptr, err)) { // a later stage's image holds no draft K/V
+ i + 1 == stages.size() ? &mtp.kv_state() : nullptr, err)) { // the draft: the last stage's
std::fprintf(stderr, "strata serve: conversation cache: discard invalid stage snapshot (%s)\n",
err.c_str());
incoming.reset();
@@ -5704,7 +5722,8 @@ int main(int argc, char** argv) {
break;
}
}
- if (incoming && !strata::core::conversation_snapshot_validate(*incoming, ss, g, mtp.kv_state(), err)) {
+ if (incoming && !strata::core::conversation_snapshot_validate(*incoming, ss, g,
+ stages.empty() ? &mtp.kv_state() : nullptr, err)) {
std::fprintf(stderr, "strata serve: conversation cache: discard invalid snapshot (%s)\n", err.c_str());
incoming.reset();
err.clear();
@@ -5717,7 +5736,8 @@ int main(int argc, char** argv) {
}
if (incoming) {
const auto t0 = Clock::now();
- if (strata::core::conversation_snapshot_restore(*incoming, ss, g, mtp.kv_state(), err) !=
+ if (strata::core::conversation_snapshot_restore(*incoming, ss, g,
+ stages.empty() ? &mtp.kv_state() : nullptr, err) !=
strata::core::ConversationRestore::restored) {
// Already prevalidated above: a failure here is fatal, never
// permission to decode from a partially restored session.
@@ -5727,7 +5747,8 @@ int main(int argc, char** argv) {
for (size_t i = 0; i < stages.size(); ++i) { // the later stages' parts
const strata::core::OnDevice on(stages[i]->dev);
if (strata::core::conversation_snapshot_restore(incoming->stage_images[i], stages[i]->ss, g,
- nullptr, err) != strata::core::ConversationRestore::restored) {
+ i + 1 == stages.size() ? &mtp.kv_state() : nullptr, err) !=
+ strata::core::ConversationRestore::restored) {
std::printf("ERR restoring parked conversation (stage CUDA%d): %s\n", stages[i]->dev,
err.c_str());
return 1;
@@ -5736,13 +5757,15 @@ int main(int argc, char** argv) {
}
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,
+ const auto& draft_kv = stages.empty() ? incoming->kv.back() : incoming->stage_images.back().kv.back();
+ const strata::core::OnDevice on_d(stages.empty() ? 0 : stages.back()->dev);
+ if (!strata::core::conversation_kv_verify(draft_kv, 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 resident=%lld\n",
- (unsigned long long) draft_hash, (long long) incoming->kv.back().cells,
+ (unsigned long long) draft_hash, (long long) draft_kv.cells,
mtp.kv_state().kv_mode, "ram",
(long long) (mtp.kv_state().n_slots * strata::kernels::qsa_real_shapes().page_size));
}
diff --git a/tools/parking_test.py b/tools/parking_test.py
index cb2129822..299958253 100644
--- a/tools/parking_test.py
+++ b/tools/parking_test.py
@@ -34,7 +34,8 @@ def gen(eng, out, ids, n):
def run(a, cfg, tok, with_b):
- eng = Engine(a.exe, cfg, 0, {"STRATA_IQ_MT_MIN": "1"}, a.extra.split())
+ # STRATA_SNAPSHOT_VERIFY: the engine reads the restored draft ring back (on the GPU that holds it) after a restore
+ eng = Engine(a.exe, cfg, 0, {"STRATA_IQ_MT_MIN": "1", "STRATA_SNAPSHOT_VERIFY": "1"}, a.extra.split())
out = eng.lines()
pa = tok.encode(chat(DOC * a.repeat + "\nSummarize this text in five sentences."), parse_special=True)
ans, _ = gen(eng, out, pa, a.max_new)
@@ -53,6 +54,8 @@ def run(a, cfg, tok, with_b):
eng.p.wait(timeout=180)
log = open("/tmp/batch_test_engine.log").read()
restored = re.findall(r"restored \d+ tokens[^\n]*", log)
+ if with_b and "SNAPSHOT_VERIFY draft=" not in log:
+ print("no SNAPSHOT_VERIFY line: the draft ring was not read back", flush=True)
reparked = [int(x) for x in re.findall(r"parked \d+ tokens .*reused_kv_bytes=(\d+)", log)]
return len(pa), len(follow), got, done, dt, restored, reparked