diff --git a/data/settings-register.json b/data/settings-register.json index 6be2918f63..3dc63dc8a0 100644 --- a/data/settings-register.json +++ b/data/settings-register.json @@ -34060,6 +34060,39 @@ "retire_after": "", "retired_in": "" }, + { + "name": "STRATA_SERVE_STOP_SKIP", + "backends": [ + "cuda-hip" + ], + "reads": [ + { + "file": "src/program/generate.cpp", + "line": 18427, + "backend": "cuda-hip", + "fn": "getenv" + } + ], + "parse": "flag", + "default_state": "on", + "code_default": null, + "engine_sets": [], + "engine_defaults": [], + "launch_files": [], + "launch_sets": [], + "launch_reads": [], + "doc_mentions": [], + "first_commit": "", + "first_date": "", + "since": "", + "last_commit": "", + "last_date": "", + "status": "research", + "owner": "program", + "note": "on unless set to 0: a decode window that finishes after the server's STOP is dropped whole (not committed, not printed), so the session ends at the rows the client was sent (lane AGENT-CAP; serial and pipelined loop; src/program/generate.cpp)", + "retire_after": "", + "retired_in": "" + }, { "name": "STRATA_SESSION_BUFFERED", "backends": [ diff --git a/include/strata/core/serve_stop_bound.hpp b/include/strata/core/serve_stop_bound.hpp index 43039fd1fd..e458785529 100644 --- a/include/strata/core/serve_stop_bound.hpp +++ b/include/strata/core/serve_stop_bound.hpp @@ -35,4 +35,19 @@ int64_t serve_hold_remaining(int accepted, int64_t produced, int64_t max_new, in inline bool serve_stop_bound_on(const char* env_value) { return env_value == nullptr || std::strcmp(env_value, "0") != 0; } + +// A window that finishes after the server's STOP has arrived (the client left, a stop string was found, the server ended its +// pass) is dropped whole: no commit, no tokens printed. The server throws away every token after its STOP (it drains the +// engine to DONE), so nothing a client receives changes; what changes is the state the engine keeps: it ends at the rows +// the client was sent, not a window or two past them, and the next request that continues the conversation resumes from it +// instead of falling back to the newest checkpoint and reading the reply again. Dropped only when its commit can be left +// undone: not a merged window (its rows are prompt rows), not the request's first window, and not a window whose state the +// one-token path advanced already (Verifier::commit_deferred()). A request that ends by its cap or EOS +// never reaches this (the stop flag is not set). STRATA_SERVE_STOP_SKIP=0 turns it off. +inline bool serve_skip_stopped_window(bool enabled, bool stop_seen, bool merged, bool first_window, bool commit_deferred) { + return enabled && stop_seen && !merged && !first_window && commit_deferred; +} +inline bool serve_stop_skip_on(const char* env_value) { + return env_value == nullptr || std::strcmp(env_value, "0") != 0; +} } diff --git a/include/strata/core/verify.hpp b/include/strata/core/verify.hpp index a0dbeda0ab..fb8cf10085 100644 --- a/include/strata/core/verify.hpp +++ b/include/strata/core/verify.hpp @@ -229,6 +229,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); + /// Is the last window's state change still undone (only `commit` / `pl_commit_async` advances it)? False for a one-token + /// window that advanced the state itself (see commit), and on every stage of a chain. serve_skip_stopped_window asks this + /// before it drops a window nobody will read. + bool commit_deferred() const; + // ================================ SEVERAL SEQUENCES IN ONE WINDOW ================================ // // A batch window holds S INDEPENDENT sequences, one token each: row s is slot s, at slot s's own position, diff --git a/src/core/verify.cpp b/src/core/verify.cpp index 61b32ded8b..679581f61a 100644 --- a/src/core/verify.cpp +++ b/src/core/verify.cpp @@ -3933,6 +3933,16 @@ bool Verifier::commit(int n_keep, std::string& err) { return next_ == nullptr || next_->commit(n_keep, err); } +bool Verifier::commit_deferred() const { +#if defined(STRATA_GPU_METAL) + return false; // the Metal window keeps its own bookkeeping: never dropped +#else + if (last_t_ < 1) return false; + const bool self = last_t_ == 1 && !always_publish_ && one_token_self_commit() && !g_qfuse(); // as commit() above + return !self && (next_ == nullptr || next_->commit_deferred()); +#endif +} + bool Verifier::wait_commit(std::string& err) { #if defined(STRATA_GPU_METAL) (void) err; diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 000b5f6880..a12e0ae3da 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -18422,6 +18422,9 @@ int main(int argc, char** argv) { // the older checkpoint against 4 of 16 without it, #4677; it changes no emitted token, only which accepted rows the // session keeps); STRATA_SERVE_STOP_BOUND=0 turns it off static const bool stop_bound = strata::core::serve_stop_bound_on(std::getenv("STRATA_SERVE_STOP_BOUND")); + // a window that finishes after the server's STOP is dropped whole (no commit, no tokens): the session ends at the rows the + // client was sent. Research switch, on by default; STRATA_SERVE_STOP_SKIP=0 turns it off (serve_stop_bound.hpp) + static const bool stop_skip = strata::core::serve_stop_skip_on(std::getenv("STRATA_SERVE_STOP_SKIP")); int64_t chain_windows = 0, chain_drafts = 0, chain_ok = 0; // --lookup-chain's own counts int64_t t2_rej[8] = {}, t2_hit[8] = {}; // STRATA_MTP_TOP2: rejections inside the MTP drafts by depth, runner-up hits int64_t dp_tested[8] = {}, dp_ok[8] = {}; // STRATA_SPEC_DEPTH=1: MTP drafts tested / accepted by depth @@ -18819,7 +18822,7 @@ int main(int argc, char** argv) { ss.ple_prev[0] = undo_ple[0]; ss.ple_prev[1] = undo_ple[1]; } - if (!VF(st, undo_w).pl_commit_async(undo_keep, err)) return die(err); + if (undo_keep > 0 && !VF(st, undo_w).pl_commit_async(undo_keep, err)) return die(err); // 0: the window was dropped (stop_skip) } doomed = false; ++pl_undo; @@ -18986,6 +18989,25 @@ int main(int argc, char** argv) { outp.data(), [&](int32_t y) { return std::find(o.eos_ids.begin(), o.eos_ids.end(), (int64_t) y) != o.eos_ids.end(); }) - 1; + // STOP arrived while A was in flight: A is dropped whole (serve_skip_stopped_window), not committed on any stage and not + // printed. A front stage that B ran on has A committed speculatively (A.com): it goes back to the snapshot taken before + // A, with no replay (undo_keep 0); a stage B never reached never committed A. + if (stop_skip && stop_req.load() && A.seq > 0) { + bool deferred = V1(A).commit_deferred(); + for (int st = 0; deferred && st < pl_nf; ++st) deferred = VF(st, A).commit_deferred(); + if (strata::core::serve_skip_stopped_window(true, true, false, false, deferred)) { + finish = "cancel"; + if (B.launched) { + doomed = true; + D = B; + undo_w = A; + undo_keep = 0; + } + B = PW{}; + ending = true; + continue; + } + } if (!V1(A).pl_commit_async(a + 1, err)) return die(err); for (int i = 0; i <= a; ++i) consumed.push_back(A.tok[i]); draft_offered += A.T - 1; @@ -19504,6 +19526,14 @@ int main(int argc, char** argv) { if (f) std::fclose(f); } } + // STOP arrived while this window ran: drop it whole (serve_skip_stopped_window). Not committed, not printed - the server throws + // away every token after its STOP - so the session ends at the rows the client was sent and the next request continues + // from it. One flag read per window; a request that ends by its cap or EOS never sets the flag. + if (stop_skip && stop_req.load() && + strata::core::serve_skip_stopped_window(true, true, merged, first_window, ver.commit_deferred())) { + finish = "cancel"; + break; + } if (fol != nullptr) { // STRATA_FOLLOW: the forced continuation stands in for the argmax (row i answers continuation index produced_n + i) for (int i = 0; i < T; ++i) { const int64_t fi = produced_n + i - (merged ? T - 1 : 0); diff --git a/tests/core/serve_stop_bound_test.cpp b/tests/core/serve_stop_bound_test.cpp index 1fca899c23..75c5f53d25 100644 --- a/tests/core/serve_stop_bound_test.cpp +++ b/tests/core/serve_stop_bound_test.cpp @@ -1,6 +1,7 @@ // (asserts stay on in a Release build: the test is its own check) #undef NDEBUG #include "strata/core/serve_stop_bound.hpp" +#include #include #include #include @@ -70,4 +71,66 @@ int main() { assert(strata::core::serve_stop_bound_on("")); assert(!strata::core::serve_stop_bound_on("0")); std::cout << "SUMMARY stop_bound cases=" << cases << " passed\n"; + + // ---- STRATA_SERVE_STOP_SKIP: a window that finishes after the server's STOP is dropped whole ----------------------- + // the decision: only when enabled, STOP seen, not merged, not the request's first window, and the commit still undone + int skip_cases = 0; + for (int m = 0; m < 32; ++m) { + const bool enabled = m & 1, stop = m & 2, merged = m & 4, first = m & 8, deferred = m & 16; + assert(strata::core::serve_skip_stopped_window(enabled, stop, merged, first, deferred) == + (enabled && stop && !merged && !first && deferred)); + ++skip_cases; + } + assert(strata::core::serve_stop_skip_on(nullptr)); + assert(strata::core::serve_stop_skip_on("1")); + assert(strata::core::serve_stop_skip_on("")); + assert(!strata::core::serve_stop_skip_on("0")); + // A ledger of the serial loop: windows of 1..6 rows; each window commits and prints its rows (bounded by the cap, as above) unless it is dropped; + // STOP is seen from the end of window `stop_at` on (-1: never) and the loop breaks after the first window that saw it. + // Properties: (1) no STOP, or the switch off: the ledger is the plain loop's; (2) the switch on and the stopped window eligible (not the + // first, commit undone): that window adds nothing - committed rows = the rows of the windows before it; (3) not eligible (the first window, + // or a one-row window the state advanced itself): as without the switch; (4) the cap ends a request before STOP is looked at: same rows. + int ledger_cases = 0; + for (int code = 0; code < 6 * 6 * 6 * 6; ++code) { + int rows[4]; + for (int w = 0; w < 4; ++w) rows[w] = 1 + (code / (w == 0 ? 1 : w == 1 ? 6 : w == 2 ? 36 : 216)) % 6; + for (int stop_at = -1; stop_at < 4; ++stop_at) + for (int self_mask = 0; self_mask < 16; ++self_mask) // window w is a self-committed one-row window (only when rows[w] == 1) + for (int cap = 1; cap <= 12; cap += 3) + for (int on = 0; on < 2; ++on) { + auto run = [&](bool enabled, int& committed, int& emitted) { + committed = emitted = 0; + for (int w = 0; w < 4 && emitted < cap; ++w) { + const bool deferred = !(rows[w] == 1 && ((self_mask >> w) & 1)); + const bool stop_seen = stop_at >= 0 && w >= stop_at; + if (strata::core::serve_skip_stopped_window(enabled, stop_seen, false, w == 0, deferred)) break; + const int b = std::min(rows[w], cap - emitted); + committed += b; + emitted += b; + if (stop_seen) break; + } + }; + int c_on = 0, e_on = 0, c_off = 0, e_off = 0; + run(on != 0, c_on, e_on); + run(false, c_off, e_off); + assert(c_on == e_on && c_off == e_off); // rows committed = tokens printed in this model: never a row the client was not sent + if (!on || stop_at < 0) { + assert(c_on == c_off); // (1) + } else { + // the rows of the windows before `stop_at`, up to the cap + int before = 0; + for (int w = 0; w < stop_at && before < cap; ++w) before += std::min(rows[w], cap - before); + const bool deferred = !(rows[stop_at] == 1 && ((self_mask >> stop_at) & 1)); + if (before >= cap) assert(c_on == before && c_off == before); // (4) + else if (stop_at > 0 && deferred) { + assert(c_on == before); // (2) + assert(c_off == before + std::min(rows[stop_at], cap - before)); + } else { + assert(c_on == c_off); // (3) + } + } + ++ledger_cases; + } + } + std::cout << "SUMMARY stop_skip decision cases=" << skip_cases << " ledger cases=" << ledger_cases << " passed\n"; }