From d22177c6104440e03040352c752e67c0c4d8d48c Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Tue, 29 Sep 2026 07:18:20 +0200 Subject: [PATCH] Layer split: mid-prompt checkpoints (--prompt-cache-every) with the pipelined prompt path Each stage saves its own part of a checkpoint when it has read the chunk that reaches the position (Prefill::on_stage_chunk, its state synchronized at that chunk's end), by the same rule the last stage uses; the last stage puts the parts together in on_chunk. They were off across GPUs because the first stage is a chunk ahead when the last one reports. --- include/strata/prefill/prefill.hpp | 6 +++ src/prefill/prefill.cpp | 7 ++- src/program/generate.cpp | 70 ++++++++++++++++++++++++++---- 3 files changed, 72 insertions(+), 11 deletions(-) diff --git a/include/strata/prefill/prefill.hpp b/include/strata/prefill/prefill.hpp index 837ffd78..056bf4bd 100644 --- a/include/strata/prefill/prefill.hpp +++ b/include/strata/prefill/prefill.hpp @@ -72,6 +72,12 @@ public: /// K/V from them. The prefill stream is synchronized before the call. std::function on_chunk; + /// Layer split: called by every stage when it has read a chunk, with the position reached, while its own state + /// is still at that chunk's end (its stream synchronized; the last stage calls it just before `on_chunk`). An + /// earlier stage is a chunk or more ahead of the last one by the time `on_chunk` runs, so this is where a + /// mid-prompt checkpoint takes each stage's part. Runs on that stage's thread, with its device current. + std::function on_stage_chunk; + /// Checked before every chunk: true stops the prompt early (`run` returns false with err "cancelled"). std::function should_stop; diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp index 73c43ee8..153403ac 100644 --- a/src/prefill/prefill.cpp +++ b/src/prefill/prefill.cpp @@ -1381,6 +1381,8 @@ bool Prefill::run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& e err = std::string("prefill: the layer split's hand-off: ") + cudaGetErrorString(cudaGetLastError()); return false; } + // this stage's state is at the chunk's end now (synced) and moves on with the next chunk below + if (on_stage_chunk && !on_stage_chunk(p0 + T, err)) return false; if (next_run.valid() && !next_run.get()) { err = next_err; return false; } next_->hand_in_ = h; next_run = std::async(std::launch::async, [this, tokens, c0, T, p0, &next_err] { @@ -1402,12 +1404,13 @@ bool Prefill::run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& e std::fclose(f); } } - if (on_chunk) { + if (on_chunk || on_stage_chunk) { if (cudaStreamSynchronize(m.cs) != cudaSuccess) { err = std::string("prefill: ") + cudaGetErrorString(cudaGetLastError()); return false; } - if (!on_chunk(m.R, T, p0, err)) return false; + if (on_stage_chunk && !on_stage_chunk(p0 + T, err)) return false; + if (on_chunk && !on_chunk(m.R, T, p0, err)) return false; } } if (next_run.valid() && !next_run.get()) { err = next_err; return false; } diff --git a/src/program/generate.cpp b/src/program/generate.cpp index e186279d..4d4050ed 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -73,6 +73,7 @@ #include #include #include +#include #include #include #include @@ -3121,19 +3122,35 @@ int main(int argc, char** argv) { return v; }; // a checkpoint of the state after `cur[0, L)`; false only when the copy itself failed - auto checkpoint_at = [&](int64_t L) -> bool { + // A layer split's mid-prompt checkpoints: when the last stage reports a chunk, the earlier ones already read + // the next, so each stage saves its own part when IT reaches a checkpoint position (the same rule as below: + // every `prompt_cache_every` tokens from where the request resumed), and the last stage puts them together. + std::mutex part_mu; + std::map> part_at; // position -> one part per stage + std::vector part_next(stages.size() + 1, INT64_MAX); + // a checkpoint of the state after `cur[0, L)`; false only when the copy itself failed. `parts`: the stages' + // states saved at L (a split's mid-prompt checkpoint); without, they are read now (everything is at L) + auto checkpoint_at = [&](int64_t L, std::vector* parts = nullptr) -> bool { if (o.prompt_cache <= 0 || L < 1) return true; for (ConvCheckpoint& c : checks) if ((int64_t) c.ids.size() == L) { c.used = ++check_clock; return true; } ConvCheckpoint c; c.ids.assign(cur.begin(), cur.begin() + L); c.imgs = imgs_below(req_imgs, L); - if (cudaDeviceSynchronize() != cudaSuccess || !checkpoint_save(c, ss, g)) return false; - for (auto& st : stages) { // a layer split's later stages: their sessions' part - const strata::core::OnDevice on(st->dev); - ConvCheckpoint part; - if (cudaDeviceSynchronize() != cudaSuccess || !checkpoint_save(part, st->ss, g)) return false; - c.stage_parts.push_back(std::move(part)); + if (parts != nullptr) { + if (parts->size() != stages.size() + 1) return false; + c.gdn = std::move((*parts)[0].gdn); + c.ple = std::move((*parts)[0].ple); + c.tails = std::move((*parts)[0].tails); + for (size_t i = 1; i < parts->size(); ++i) c.stage_parts.push_back(std::move((*parts)[i])); + } else { + if (cudaDeviceSynchronize() != cudaSuccess || !checkpoint_save(c, ss, g)) return false; + for (auto& st : stages) { // a layer split's later stages: their sessions' part + const strata::core::OnDevice on(st->dev); + ConvCheckpoint part; + if (cudaDeviceSynchronize() != cudaSuccess || !checkpoint_save(part, st->ss, g)) return false; + c.stage_parts.push_back(std::move(part)); + } } c.used = ++check_clock; checks.push_back(std::move(c)); @@ -3159,8 +3176,23 @@ int main(int argc, char** argv) { strata::core::progress_at("reading the prompt (batched), done up to token", done); strata::core::progress_beat(); std::fflush(stdout); - if (o.prompt_cache_every > 0 && done >= pp_next_check && !multi_gpu) { // split: stage 0 is ahead - if (!checkpoint_at(done)) { e = "saving a conversation checkpoint failed"; return false; } + if (o.prompt_cache_every > 0 && done >= pp_next_check) { + bool saved = false; + if (multi_gpu) { // the stages' parts, saved when each of them read this chunk + std::vector parts; + { + std::lock_guard lk(part_mu); + auto it = part_at.find(done); + if (it != part_at.end()) parts = std::move(it->second); + part_at.erase(part_at.begin(), part_at.upper_bound(done)); + } + bool complete = parts.size() == stages.size() + 1; + for (const ConvCheckpoint& k : parts) complete = complete && !k.gdn.empty(); + saved = !complete || checkpoint_at(done, &parts); // an incomplete set: no checkpoint here + } else { + saved = checkpoint_at(done); + } + if (!saved) { e = "saving a conversation checkpoint failed"; return false; } pp_next_check = done + o.prompt_cache_every; } return true; @@ -3168,6 +3200,21 @@ int main(int argc, char** argv) { if (multi_gpu) { // the batched prompt is reported by its last stage (the drafter's rows are there) stages.back()->sp.on_chunk = std::move(sp.on_chunk); sp.on_chunk = nullptr; + for (size_t i = 0; i <= stages.size(); ++i) { + strata::prefill::Prefill& stage_sp = i == 0 ? sp : stages[i - 1]->sp; + strata::core::SessionState& stage_ss = i == 0 ? ss : stages[i - 1]->ss; + stage_sp.on_stage_chunk = [&, i](int64_t done, std::string& e) -> bool { + if (o.prompt_cache <= 0 || o.prompt_cache_every <= 0 || done < part_next[i]) return true; + part_next[i] = done + o.prompt_cache_every; + ConvCheckpoint part; // this stage's state at `done` (its stream is synchronized) + if (!checkpoint_save(part, stage_ss, g)) { e = "saving a checkpoint part failed"; return false; } + std::lock_guard lk(part_mu); + auto& v = part_at[done]; + v.resize(stages.size() + 1); + v[i] = std::move(part); + return true; + }; + } } drive.d.plan = ver.plan_sink(); drive.d.pcie_num = std::max(0, std::min(256, (int) (o.pcie_frac * 256.0 + 0.5))); @@ -3665,6 +3712,11 @@ int main(int argc, char** argv) { pp_from = read_from; pp_t0 = r0; pp_next_check = reread_to > 0 ? INT64_MAX : resume + o.prompt_cache_every; + { + std::lock_guard lk(part_mu); + part_at.clear(); + std::fill(part_next.begin(), part_next.end(), pp_next_check); + } std::printf("RESUME %lld\n", (long long) resume); // before reading: this many prompt tokens are reused strata::core::progress_at("reading the prompt, from token", read_from); std::fflush(stdout);