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++
layer split overlap: bound the GPU-side hand-off wait (STRATA_SPLIT_WAIT_MS)
bsvinay2443c9225c1a
diff --git a/docs/MULTI_GPU.md b/docs/MULTI_GPU.mdindex 640d60ed5..0ee03e2eb 100644--- a/docs/MULTI_GPU.md+++ b/docs/MULTI_GPU.md@@ -220,6 +220,13 @@ KV, layers 0-25 / 26-47; 512-token greedy answers, median of 8 runs:Greedy output is byte-identical with the flag at 0 and 1; the quiz set scored the same (47/51). Not measured onNVIDIA yet, where a sync and a graph launch cost less.+The reading stage's wait for the flag is bounded on the GPU: `STRATA_SPLIT_WAIT_MS` (default 30000, 0 = no bound).+If the flag does not arrive in time, the wait marks an error word and returns, the window's graph completes, and the+host fails that window with an error naming the setting (the server then restarts the engine) instead of using a+stale hand-off. Normally the host's own per-layer wait (20 s, #267) ends a stalled window first and releases every+GPU wait, the hand-off's included. Test hook: `STRATA_TEST_HANDOFF_DROP=N` withholds the flag in the first stage's+N-th window, as if the publish were lost.+## Limits (for now)- **Works across cards** (bench/results/2026-09-29-layer-split-limits):diff --git a/include/strata/core/verify.hpp b/include/strata/core/verify.hppindex 313ea4a7a..358aacd57 100644--- a/include/strata/core/verify.hpp+++ b/include/strata/core/verify.hpp@@ -446,6 +446,11 @@ private:uint32_t* hflag_out_h_ = nullptr; ///< ... and the outgoing one'suint32_t* hflag_out_d_ = nullptr;uint32_t* hcount_d_ = nullptr; ///< handoff_publish's block counter (device memory)+ uint32_t* herr_h_ = nullptr; ///< the bounded hand-off wait gave up (mapped; the reading stage's)+ uint32_t* herr_d_ = nullptr;+ uint32_t* hdrop_h_ = nullptr; ///< STRATA_TEST_HANDOFF_DROP: the writing stage withholds its flag (mapped)+ uint32_t* hdrop_d_ = nullptr;+ bool handoff_failed(std::string& err) const; ///< the bounded hand-off wait timed out in the last windowint prelaunched_ = 0; ///< prelaunch ran for a window of this size: run skips staging + launch/// The overlapped split: stage this window's inputs and launch its graph now (the graph waits on the hand-off/// flag); `run` then only serves the host side. Called on a helper thread while the previous stage runs.diff --git a/include/strata/kernels/verify_kernels.hpp b/include/strata/kernels/verify_kernels.hppindex 9669f53f2..dab7c9fe3 100644--- a/include/strata/kernels/verify_kernels.hpp+++ b/include/strata/kernels/verify_kernels.hpp@@ -79,8 +79,12 @@ void wait_copy_i32_from_mapped(const uint32_t* flag, uint32_t value, int32_t* ds/// stores, then the LAST block to finish raises *flag = 1 (mapped) after a system fence, so the next stage's GPU can/// start (wait_flag_ge(flag, 1)) without the host syncing this stage first. `counter` is one device word, 0 between/// calls (the last block resets it). Volatile because on RDNA a plain store to mapped memory can sit in the L2.+/// `drop` (mapped, may be null): a test hook - nonzero leaves the flag down, as if the publish were lost.void handoff_publish(float* dst, const float* a, int64_t na, const float* b, int64_t nb, const float* c, int64_t nc,- uint32_t* counter, uint32_t* flag, void* stream);+ uint32_t* counter, uint32_t* flag, void* stream, const uint32_t* drop = nullptr);+/// The reading stage's hand-off wait, bounded: spin until *flag >= value, or after timeout_ns (0: no bound) set+/// *err = 1 (mapped) and return, so the window's graph completes and the host fails the window cleanly.+void wait_handoff(const uint32_t* flag, uint32_t value, uint32_t* err, unsigned long long timeout_ns, void* stream);/// the GPU's %globaltimer (ns) into buf[i] (a one-thread kernel: the verify window's stage profiler). Inside a PDL/// stretch (pdl.hpp) the stamp passes the early launch on, so a profiled window keeps the chain it measures.void gpu_stamp(unsigned long long* buf, int i, void* stream);diff --git a/src/core/verify.cpp b/src/core/verify.cppindex ed2a37f9c..2aaa6dba7 100644--- a/src/core/verify.cpp+++ b/src/core/verify.cpp@@ -389,6 +389,19 @@ const int64_t g_test_stall = [] {const char* e = std::getenv("STRATA_TEST_VERIFY_STALL");return e != nullptr ? (int64_t) std::atoll(e) : (int64_t) 0;}();+// The overlapped split's hand-off wait on the GPU is bounded: STRATA_SPLIT_WAIT_MS (default 30000, 0 = no bound).+// The host's own per-layer wait (20 s, #267) normally ends a stalled window first; this bound is the backstop for+// a host that is gone (a killed process must not leave a kernel spinning on its card).+const unsigned long long g_split_wait_ns = [] {+ const char* e = std::getenv("STRATA_SPLIT_WAIT_MS");+ return (unsigned long long) (e != nullptr ? std::atoll(e) : 30000) * 1000000ull;+}();+// test hook: STRATA_TEST_HANDOFF_DROP=N - the writing stage's N-th window (1-based) leaves its hand-off flag down, as+// if the publish were lost; the reading stage's bounded wait (or the host's) must then end the window cleanly.+const int64_t g_test_drop = [] {+ const char* e = std::getenv("STRATA_TEST_HANDOFF_DROP");+ return e != nullptr ? (int64_t) std::atoll(e) : (int64_t) 0;+}();} // namespacebool Verifier::release_gpu_waits(int timeout_ms) {@@ -532,7 +545,7 @@ Verifier::~Verifier() {if (arena_) cudaFree(arena_);if (hcount_d_) cudaFree(hcount_d_);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_, h_plan_err_, h_nf_};+ h_flagA_, h_plan_, h_flagB_, h_plan_err_, h_nf_, herr_h_, hdrop_h_};for (void* h : hosts)if (h) cudaFreeHost(h);}@@ -718,7 +731,10 @@ bool Verifier::init(const WeightTable& wt, const ModelGeometry& g, SessionState&if (hflag_out_d_ != nullptr && hcount_d_ == nullptr) { // the overlapped split's hand-off counterif (cudaMalloc((void**) &hcount_d_, 64) != cudaSuccess) { err = "verify: hand-off counter allocation failed"; return false; }cudaMemset(hcount_d_, 0, 64);+ if (!mapped(64, (void**) &hdrop_h_, (void**) &hdrop_d_)) { err = "verify: hand-off test word allocation failed"; return false; }}+ if (hflag_in_d_ != nullptr && herr_h_ == nullptr &&+ !mapped(64, (void**) &herr_h_, (void**) &herr_d_)) { err = "verify: hand-off error word allocation failed"; return false; }if (g_trace && trace_h_ == nullptr) { // #649: the breadcrumbs, mapped so they read while the GPU hangstrace_n_ = (size_t) (g.n_layers + 1) * kProfPer * 2;if (!mapped(trace_n_ * 8, (void**) &trace_h_, (void**) &trace_m_)) {@@ -964,7 +980,7 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {if (lb_ > 0) {const float* hin = hand_in_ + (size_t) hrow0 * HB; // the group's rows: [R][bo][inj] contiguous// the overlapped split: this graph was launched before the previous stage finished - wait for its hand-off- if (hflag_in_d_ != nullptr && !batch_rec_) wait_flag_ge(hflag_in_d_, 1u, cs);+ if (hflag_in_d_ != nullptr && !batch_rec_) wait_handoff(hflag_in_d_, 1u, herr_d_, g_split_wait_ns, cs);copy_from_mapped(R_, hin, (int64_t) T * HC * N, cs);copy_from_mapped(bo_, hin + (size_t) T * HC * N, (int64_t) T * N, cs);copy_from_mapped(inj2_, hin + (size_t) T * (HC + 1) * N, (int64_t) T * HC, cs);@@ -1805,7 +1821,7 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {float* hout = hand_out_ + (size_t) hrow0 * HB;if (hflag_out_d_ != nullptr && !batch_rec_) { // the overlapped split: payload + the next stage's flaghandoff_publish(hout, R_, (int64_t) T * HC * N, bo_, (int64_t) T * N, inj2_, (int64_t) T * HC, hcount_d_,- hflag_out_d_, cs);+ hflag_out_d_, cs, hdrop_d_);return true;}copy_from_mapped(hout, R_, (int64_t) T * HC * N, cs);@@ -2240,6 +2256,8 @@ void Verifier::stage_inputs(int T, const int32_t* tokens, int64_t pos0) {*(volatile uint32_t*) h_flagB_ = 0;// the overlapped split: the next stage's graph is launched after this, and must see THIS window's raiseif (hflag_out_h_ != nullptr) *(volatile uint32_t*) hflag_out_h_ = 0;+ if (herr_h_ != nullptr) *(volatile uint32_t*) herr_h_ = 0;+ if (hdrop_h_ != nullptr) *(volatile uint32_t*) hdrop_h_ = (g_test_drop > 0 && windows + 1 == g_test_drop) ? 1u : 0u;std::atomic_thread_fence(std::memory_order_seq_cst);_mm_sfence();last_t_ = T;@@ -2314,6 +2332,15 @@ bool Verifier::adapt_gate_wait(int32_t layer, std::string& err) {return true;}+bool Verifier::handoff_failed(std::string& err) const {+ if (herr_h_ == nullptr || *(const volatile uint32_t*) herr_h_ == 0) return false;+ err = "verify: layer split: the hand-off into layer " + std::to_string(lb_) + " did not arrive within " ++ std::to_string(g_split_wait_ns / 1000000ull) + " ms (STRATA_SPLIT_WAIT_MS); the window was not used. "+ "STRATA_SPLIT_OVERLAP=0 runs the serial order";+ trace_ev("HANDOFF-TIMEOUT", -1, lb_, (int64_t) (g_split_wait_ns / 1000000ull));+ return true;+}+bool Verifier::chain_overlap() const {return next_ != nullptr && hflag_out_h_ != nullptr && hflag_out_d_ != nullptr && hcount_d_ != nullptr &&next_->hflag_in_h_ == hflag_out_h_ && next_->device_ != device_ && !next_->ple_stage();@@ -2569,6 +2596,12 @@ bool Verifier::run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool,}if (!ok) return false;if (se != cudaSuccess) { err = std::string("verify: ") + cudaGetErrorString(se); return false; }+ if (handoff_failed(err)) return false; // a middle stage of three or more: its own hand-in+ if (ar_on() && h_plan_err_ != nullptr && *(volatile uint32_t*) h_plan_err_ != 0) { // #871, as below+ *(volatile uint32_t*) h_plan_err_ = 0;+ err = "verify: the all-resident plan met an expert that is not in VRAM (the residency table changed during the window)";+ return false;+ }commit_pending_ = false;if (prof_on_ && G == 1) collect_profile();return true;@@ -2590,6 +2623,7 @@ bool Verifier::run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool,const cudaError_t se = cudaStreamSynchronize(cs_);trace_ev("SYNCED", -1, -1, (int64_t) se);if (se != cudaSuccess) { err = std::string("verify: ") + cudaGetErrorString(se); return false; }+ if (handoff_failed(err)) return false; // a stale hand-off: nothing of this window is usedif (ar_on() && h_plan_err_ != nullptr && *(volatile uint32_t*) h_plan_err_ != 0) { // #871: refresh_ar saw the table whole*(volatile uint32_t*) h_plan_err_ = 0;err = "verify: the all-resident plan met an expert that is not in VRAM (the residency table changed during the window)";diff --git a/src/kernels/cuda/verify_kernels.cu b/src/kernels/cuda/verify_kernels.cuindex 75c6f3d34..0928c731a 100644--- a/src/kernels/cuda/verify_kernels.cu+++ b/src/kernels/cuda/verify_kernels.cu@@ -1157,9 +1157,39 @@ __global__ void wait_flag_ge_kernel(const volatile uint32_t* flag, uint32_t valu__threadfence_system();}+// the GPU's clock in ns (the same sources as gpu_stamp_kernel)+__device__ __forceinline__ unsigned long long strata_now_ns() {+ unsigned long long t;+#if defined(STRATA_HIP_GFX906)+ t = wall_clock64() * 40ull;+#elif defined(__HIPCC__)+ t = wall_clock64() * 10ull;+#else+ asm volatile("mov.u64 %0, %%globaltimer;" : "=l"(t));+#endif+ return t;+}++// the overlapped split's hand-off wait, bounded: spin until *flag >= value, or give up after timeout_ns (0: never),+// marking *err = 1 (mapped) so the host fails the window instead of using a stale hand-off. The window's later+// kernels still run (on whatever the hand-off buffer holds) so the graph completes and no stream is left hanging.+__global__ void wait_handoff_kernel(const volatile uint32_t* flag, uint32_t value, uint32_t* err,+ unsigned long long timeout_ns) {+ const unsigned long long t0 = timeout_ns ? strata_now_ns() : 0ull;+ while (*flag < value) {+ strata_spin_pause();+ if (timeout_ns && strata_now_ns() - t0 > timeout_ns) {+ *(volatile uint32_t*) err = 1u;+ __threadfence_system();+ return;+ }+ }+ __threadfence_system();+}+__global__ void handoff_publish_kernel(float* dst, const float* __restrict__ a, int64_t na, const float* __restrict__ b,int64_t nb, const float* __restrict__ c, int64_t nc, uint32_t* counter,- uint32_t* flag) {+ uint32_t* flag, const volatile uint32_t* drop) {const int64_t n = na + nb + nc;volatile float* const out = dst;for (int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; i < n; i += (int64_t) gridDim.x * blockDim.x)@@ -1171,15 +1201,22 @@ __global__ void handoff_publish_kernel(float* dst, const float* __restrict__ a,if (prev == gridDim.x - 1) { // every block's payload is out: raise the next stage's flag*(volatile uint32_t*) counter = 0u;__threadfence_system();- *(volatile uint32_t*) flag = 1u;- __threadfence_system();+ if (drop == nullptr || *drop == 0u) { // test hook (STRATA_TEST_HANDOFF_DROP): a lost publish+ *(volatile uint32_t*) flag = 1u;+ __threadfence_system();+ }}}}} // namespace+void wait_handoff(const uint32_t* flag, uint32_t value, uint32_t* err, unsigned long long timeout_ns, void* stream) {+ wait_handoff_kernel<<<1, 1, 0, (cudaStream_t) stream>>>(flag, value, err, timeout_ns);+ check("wait_handoff");+}+void handoff_publish(float* dst, const float* a, int64_t na, const float* b, int64_t nb, const float* c, int64_t nc,- uint32_t* counter, uint32_t* flag, void* stream) {+ uint32_t* counter, uint32_t* flag, void* stream, const uint32_t* drop) {const int64_t n = na + nb + nc;if (n <= 0 || dst == nullptr || counter == nullptr || flag == nullptr) {std::fprintf(stderr, "handoff_publish: invalid arguments\n");@@ -1187,7 +1224,7 @@ void handoff_publish(float* dst, const float* a, int64_t na, const float* b, int}const int64_t nb256 = (n + 255) / 256;const int blocks = (int) (nb256 < 64 ? nb256 : 64);- handoff_publish_kernel<<<blocks, 256, 0, (cudaStream_t) stream>>>(dst, a, na, b, nb, c, nc, counter, flag);+ handoff_publish_kernel<<<blocks, 256, 0, (cudaStream_t) stream>>>(dst, a, na, b, nb, c, nc, counter, flag, drop);check("handoff_publish");}