diff --git a/include/strata/core/verify.hpp b/include/strata/core/verify.hpp index 4ddc5269..5717eb4f 100644 --- a/include/strata/core/verify.hpp +++ b/include/strata/core/verify.hpp @@ -121,9 +121,12 @@ public: bool commit(int n_keep, std::string& err); /// commit() returns without waiting for its graph (a single-GPU session sets it): the next window follows it on /// the same stream and the drafter reads nothing it writes, so it overlaps the draft. Whoever reads or writes - /// the session from another stream or the host afterwards (a new request, a checkpoint) synchronizes the device - /// first. STRATA_COMMIT_SYNC=1 keeps the wait. + /// the session from another stream or the host afterwards (a new request, a checkpoint, a snapshot, the prompt + /// path, the end of a run) calls wait_commit() first. STRATA_COMMIT_SYNC=1 keeps the wait. static void set_commit_async(bool on); + /// Waits for the last commit graph when commit() did not (an event recorded after it, not the whole device); + /// false with `err` when it failed. Free when nothing is pending. + bool wait_commit(std::string& err); /// Measurement hook (STRATA_LOGPOS): after run(), write one line per row t of the last window's head - /// "pos target logprob top top_logprob hit extra_logprob target_logprob_without_extra" - where row t is the @@ -217,6 +220,8 @@ private: uint32_t* h_flag_ = nullptr; uint32_t* m_flag_ = nullptr; uint32_t* h_flagA_ = nullptr; uint32_t* m_flagA_ = nullptr; // the GPU plan is in place uint32_t* h_flagB_ = nullptr; uint32_t* m_flagB_ = nullptr; // the PCIe share's DMA copies have landed + cudaEvent_t commit_done_ = nullptr; // recorded after an async commit (set_commit_async); see wait_commit + bool commit_pending_ = false; cudaStream_t copy_ = nullptr; // the copy engine's stream (DMA of missed experts) struct FlagSet { uint32_t* flag; uint32_t value; }; FlagSet flag_sets_[2 * 64 * 2] = {}; // host-function arguments, one per (layer, group) diff --git a/src/core/verify.cpp b/src/core/verify.cpp index 06f8ceaf..839151d6 100644 --- a/src/core/verify.cpp +++ b/src/core/verify.cpp @@ -177,6 +177,7 @@ Verifier::~Verifier() { if (commit_exec_) cudaGraphExecDestroy(commit_exec_); if (cs_) cudaStreamDestroy(cs_); if (copy_) { cudaStreamSynchronize(copy_); cudaStreamDestroy(copy_); } + if (commit_done_) cudaEventDestroy(commit_done_); if (arena_) cudaFree(arena_); void* hosts[] = {h_tok_, h_step_, h_pos_, h_commit_, h_ple_, h_out_, h_x_, h_ids_, h_w_, h_seq_, h_flag_, h_ymiss_, h_flagA_, h_plan_, h_flagB_}; @@ -346,6 +347,10 @@ bool Verifier::init(const WeightTable& wt, const ModelGeometry& g, SessionState& err = "verify: stream create failed"; return false; } + if (cudaEventCreateWithFlags(&commit_done_, cudaEventDisableTiming) != cudaSuccess) { + err = "verify: event create failed"; + return false; + } // E-6: a layer whose routed experts are all resident is planned on the device (STRATA_VERIFY_DEVICE_PLAN=1: on; // exact, but neutral on RIBPC 1-2 GPUs: off by default) { @@ -1307,10 +1312,14 @@ bool Verifier::commit(int n_keep, std::string& err) { if (le != cudaSuccess) { err = std::string("verify: commit launch: ") + cudaGetErrorString(le); return false; } // set_commit_async: no wait here - the next window runs on the same stream after it, and the drafter (its own // stream) reads only this window's final rows and its own K/V. h_commit_ is next written after the next window's - // results are read, i.e. after this graph has run. - if (!g_commit_async) { + // results are read, i.e. after this graph has run. Everything else waits on commit_done_ (wait_commit). + if (!g_commit_async || next_ != nullptr) { const cudaError_t se = cudaStreamSynchronize(cs_); if (se != cudaSuccess) { err = std::string("verify: commit: ") + cudaGetErrorString(se); return false; } + } else { + const cudaError_t re = cudaEventRecord(commit_done_, cs_); + if (re != cudaSuccess) { err = std::string("verify: commit event: ") + cudaGetErrorString(re); return false; } + commit_pending_ = true; } if (ple_stage()) // stages that share one session must advance it once for (int t = 0; t < n_keep; ++t) { @@ -1321,6 +1330,16 @@ bool Verifier::commit(int n_keep, std::string& err) { return next_ == nullptr || next_->commit(n_keep, err); } +bool Verifier::wait_commit(std::string& err) { + if (commit_pending_) { + const OnDevice on_device(device_); + commit_pending_ = false; + const cudaError_t se = cudaEventSynchronize(commit_done_); + if (se != cudaSuccess) { err = std::string("verify: commit: ") + cudaGetErrorString(se); return false; } + } + return next_ == nullptr || next_->wait_commit(err); +} + bool Verifier::copy_logits(int t, float* host) const { if (next_ != nullptr) return next_->copy_logits(t, host); // a layer split: the head is on the last stage if (head_logits_ == nullptr || host == nullptr || t < 0 || n_vocab_ <= 0) return false; diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 1ec6690b..0eb5ee84 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -4609,8 +4609,12 @@ int main(int argc, char** argv) { }; const bool want_cvec = strata::kernels::cvec().loaded() ? req_cvec != 0 : true; // the last request's final commit may still be running on the verifier's stream (set_commit_async): - // everything below reads, restores or zeroes the session from other streams and the host - cudaDeviceSynchronize(); + // everything below reads, restores or zeroes the session from other streams and the host (the end of the + // last request waited already; this covers a request that ended on an error path) + if (!ver.wait_commit(err)) { + std::printf("ERR %s\n", err.c_str()); + return 1; + } int64_t resume = 0; bool from_live = false; if (o.prompt_cache > 0 && want_cvec == cvec_cached) { @@ -4802,6 +4806,8 @@ int main(int argc, char** argv) { if (!ver.commit(T, e) || !mtp.prefill(ver.final_R_all(), nxt.data(), T, q, e)) return false; q += T; } + // the batched prompt path (other streams), checkpoints and snapshots may follow: the last commit first + if (!ver.wait_commit(e)) return false; const double ms = std::chrono::duration(Clock::now() - pp_t0).count(); std::printf("PP %lld %lld %.0f %.1f\n", (long long) b, (long long) pp_total, ms, ms > 0.0 ? 1000.0 * (double) (b - pp_from) / ms : 0.0); @@ -5121,6 +5127,11 @@ int main(int argc, char** argv) { p += a + 1; } const double decode_ms = std::chrono::duration(Clock::now() - d0).count(); + // the last commit (set_commit_async): the session is complete before anything reads or copies it + if (!ver.wait_commit(err)) { + std::printf("ERR %s\n", err.c_str()); + return 1; + } if (dec_timing && dec_windows > 0) { const DecSnap d1 = dec_snap(); const double w = (double) dec_windows, L = (double) g.n_layers; @@ -5923,6 +5934,11 @@ int main(int argc, char** argv) { std::fprintf(stderr, "strata generate: position %lld, %lld tokens, %lld rounds\n", (long long) p, (long long) produced.size(), (long long) rounds); } + // the last commit (set_commit_async) before anything reads the session again + if (!ver.wait_commit(err)) { + std::fprintf(stderr, "strata generate: %s\n", err.c_str()); + return 1; + } std::printf("%-24s %lld rounds of %d, drafts accepted %lld of %lld (%.3f), %.2f tokens per round\n", "speculation", (long long) rounds, o.spec, (long long) drafts_ok, (long long) drafts_total, drafts_total > 0 ? (double) drafts_ok / (double) drafts_total : 0.0, @@ -6020,7 +6036,6 @@ int main(int argc, char** argv) { for (int64_t t : produced) std::printf(" %lld", (long long) t); std::printf("\n"); const double decode_ms = decoded > 0 ? total_ms / (double) decoded : 0.0; - cudaDeviceSynchronize(); // the last commit (set_commit_async) before anything reads the session std::printf("%-24s %lld tokens in %.1f ms -> %.2f tok/s\n", "decode", (long long) decoded, total_ms, decode_ms > 0.0 ? 1000.0 / decode_ms : 0.0); if (n_prompt > 1)