From a1e4fb4c80f8e2f220deb12962952d685bbff533 Mon Sep 17 00:00:00 2001 From: sergiywith Date: Wed, 30 Sep 2026 22:25:39 +0300 Subject: [PATCH] Verify: the commit graph no longer waits; it overlaps the MTP draft The commit's cudaStreamSynchronize held the host ~0.45 ms a round. The next window follows it on the same stream and the drafter (its own stream) reads only this window's final rows and its own K/V, so a single-GPU session (Verifier::set_commit_async) returns at once. Whatever touches the session from another stream or the host afterwards synchronizes the device: the start of each --serve request (checkpoint restore, zeroing, the prompt path) and the end of a generate run; checkpoint saves already did. A layer split keeps the wait, as does STRATA_COMMIT_SYNC=1. IQ2_XS, 400-token answers, 3 runs each: 128.6 -> 135.8 tok/s decode (+5.6%), the same tokens. NVFP4 pack +0.8%. --- include/strata/core/verify.hpp | 5 +++++ src/core/verify.cpp | 12 ++++++++++-- src/program/generate.cpp | 5 +++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/include/strata/core/verify.hpp b/include/strata/core/verify.hpp index f478191a..4ddc5269 100644 --- a/include/strata/core/verify.hpp +++ b/include/strata/core/verify.hpp @@ -119,6 +119,11 @@ public: /// Keep the first `n_keep` (1..T) tokens of the last window; advances `ss.ple_prev` by them. 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. + static void set_commit_async(bool on); /// 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 diff --git a/src/core/verify.cpp b/src/core/verify.cpp index 179a7752..06f8ceaf 100644 --- a/src/core/verify.cpp +++ b/src/core/verify.cpp @@ -1292,6 +1292,9 @@ bool Verifier::window_logprobs(const int32_t* targets, int T, int64_t pos0, int3 return true; } +namespace { bool g_commit_async = false; } +void Verifier::set_commit_async(bool on) { g_commit_async = on && std::getenv("STRATA_COMMIT_SYNC") == nullptr; } + bool Verifier::commit(int n_keep, std::string& err) { const OnDevice on_device(device_); if (n_keep < 1 || n_keep > last_t_) { err = "verify: commit count out of range"; return false; } @@ -1302,8 +1305,13 @@ bool Verifier::commit(int n_keep, std::string& err) { std::atomic_thread_fence(std::memory_order_seq_cst); const cudaError_t le = cudaGraphLaunch(commit_exec_, cs_); if (le != cudaSuccess) { err = std::string("verify: commit launch: ") + cudaGetErrorString(le); return false; } - const cudaError_t se = cudaStreamSynchronize(cs_); - if (se != cudaSuccess) { err = std::string("verify: commit: ") + cudaGetErrorString(se); 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) { + const cudaError_t se = cudaStreamSynchronize(cs_); + if (se != cudaSuccess) { err = std::string("verify: commit: ") + cudaGetErrorString(se); return false; } + } if (ple_stage()) // stages that share one session must advance it once for (int t = 0; t < n_keep; ++t) { ss_->ple_prev[0] = ss_->ple_prev[1]; diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 74c47759..1ec6690b 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -2054,6 +2054,7 @@ int main(int argc, char** argv) { } } } + strata::core::Verifier::set_commit_async(!multi_gpu); // see Verifier::set_commit_async // ---- layer split across GPUs: each later stage's own copy of the dense weights, its session and (the last) the // head, made on its device before the host arena is mapped (as the drafter below, for the same WDDM reason) std::vector> stages; @@ -4607,6 +4608,9 @@ int main(int argc, char** argv) { return imgs_below(req_imgs, L) == pre_imgs; }; 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(); int64_t resume = 0; bool from_live = false; if (o.prompt_cache > 0 && want_cvec == cvec_cached) { @@ -6016,6 +6020,7 @@ 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)