Skip to content

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 the stages' hand-off (STRATA_SPLIT_OVERLAP=1, opt-in)

Niko122156146c4c4906
diff --git a/docs/MULTI_GPU.md b/docs/MULTI_GPU.md
index 62a8a177a..640d60ed5 100644
--- a/docs/MULTI_GPU.md
+++ b/docs/MULTI_GPU.md
@@ -199,6 +199,27 @@ that overlap instead of one, a 32K one four instead of two. Same tokens; 2x / 3x
32K +18% / +21% / +29%, 64K +4% / +12% / +17%, 8K unchanged. `STRATA_SPLIT_CHUNK16K=1` keeps 16384.) Conversation checkpoints save and restore every card's state; the adaptive expert swaps copy
into the card that owns the layer.
+## Overlapped hand-off (`STRATA_SPLIT_OVERLAP=1`, opt-in)
+
+By default each verify window runs the stages one after another: the host syncs stage 1, then stages and launches
+stage 2's graph, and so on. `STRATA_SPLIT_OVERLAP=1` (stages on different cards only) gives the hand-off a ready
+flag: the earlier stage's GPU writes the hand-off with volatile stores and raises the flag (`handoff_publish`), the
+next stage's graph is staged and launched on a helper thread while the earlier stage is still running and waits for
+the flag on its GPU (`wait_flag_ge`), and the host goes straight from serving one stage's layers to the next, syncing
+the earlier stage only once the chain is done. It takes one stream sync and one graph launch per window off the
+critical path. Batch windows (several conversations) keep the serial order.
+
+Measured on 2x RX 7900 XTX (gfx1100, ROCm, Linux), Ryzen 9 9900X, 52 GB RAM, Swift 1.5 IQ3_XXS, 128K context, 8-bit
+KV, layers 0-25 / 26-47; 512-token greedy answers, median of 8 runs:
+
+| | serial (default) | `STRATA_SPLIT_OVERLAP=1` | one card |
+|---|---|---|---|
+| decode, code prompt | 53 tok/s | 70.1 tok/s | ~70 tok/s |
+| decode, prose prompt | 45 tok/s | 55.9 tok/s | ~68 tok/s |
+
+Greedy output is byte-identical with the flag at 0 and 1; the quiz set scored the same (47/51). Not measured on
+NVIDIA yet, where a sync and a graph launch cost less.
+
## 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.hpp
index 2539a33a3..313ea4a7a 100644
--- a/include/strata/core/verify.hpp
+++ b/include/strata/core/verify.hpp
@@ -170,6 +170,14 @@ public:
void set_stage(int64_t layer_begin, int64_t layer_end, const float* handoff_in, float* handoff_out) {
lb_ = layer_begin; le_ = layer_end; hand_in_ = handoff_in; hand_out_ = handoff_out;
}
+ /// STRATA_SPLIT_OVERLAP=1 (opt-in; stages on DIFFERENT devices only): the hand-off carries a ready flag (one
+ /// mapped word per hand-off buffer: host and device addresses). The writing stage raises it from the GPU when
+ /// its hand-off is out (handoff_publish); the reading stage's window graph waits on it (wait_flag_ge) - so the
+ /// next stage's graph is launched while this stage is still running, and the host no longer syncs between the
+ /// stages. Null (the default): the old order - sync, then launch the next stage. Set before `init`.
+ void set_handoff_flags(uint32_t* in_host, uint32_t* in_dev, uint32_t* out_host, uint32_t* out_dev) {
+ hflag_in_h_ = in_host; hflag_in_d_ = in_dev; hflag_out_h_ = out_host; hflag_out_d_ = out_dev;
+ }
/// The next stage: `run` and `commit` continue into it (its pool calls get `next_user`); sampling settings
/// and `final_R` are the last stage's.
void set_next(Verifier* next, void* next_user) { next_ = next; next_user_ = next_user; }
@@ -433,6 +441,16 @@ private:
int64_t lb_ = 0, le_ = -1; ///< set_stage: the layers this verifier runs (-1: to the last)
const float* hand_in_ = nullptr;
float* hand_out_ = nullptr;
+ uint32_t* hflag_in_h_ = nullptr; ///< set_handoff_flags: the incoming hand-off's ready word (host / device)
+ uint32_t* hflag_in_d_ = nullptr;
+ uint32_t* hflag_out_h_ = nullptr; ///< ... and the outgoing one's
+ uint32_t* hflag_out_d_ = nullptr;
+ uint32_t* hcount_d_ = nullptr; ///< handoff_publish's block counter (device memory)
+ int 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.
+ bool prelaunch(int T, const int32_t* tokens, int64_t pos0, std::string& err);
+ bool chain_overlap() const; ///< this stage hands off through a flag to a next stage that waits on it
Verifier* next_ = nullptr;
void* next_user_ = nullptr;
bool ple_stage() const { return lb_ <= 1 && 1 < le_; } ///< holds layer 1, where the PLE block runs
diff --git a/include/strata/kernels/verify_kernels.hpp b/include/strata/kernels/verify_kernels.hpp
index 6f54e0bb0..9669f53f2 100644
--- a/include/strata/kernels/verify_kernels.hpp
+++ b/include/strata/kernels/verify_kernels.hpp
@@ -74,6 +74,13 @@ void wait_flag_ge(const uint32_t* flag, uint32_t value, void* stream);
/// kernel costs ~2.4 us on a gfx1201 graph whatever it does. n <= 128 * k for any k (a strided loop).
void wait_copy_i32_from_mapped(const uint32_t* flag, uint32_t value, int32_t* dst, const int32_t* src, int64_t n,
void* stream);
+
+/// Layer split, overlapped hand-off (STRATA_SPLIT_OVERLAP): dst (mapped) = [a (na) | b (nb) | c (nc)] with volatile
+/// 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.
+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);
/// 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.cpp
index 9fe22ecfd..ed2a37f9c 100644
--- a/src/core/verify.cpp
+++ b/src/core/verify.cpp
@@ -57,6 +57,7 @@
#include <map>
#include <mutex>
#include <string>
+#include <optional>
#include <thread>
#include <vector>
#include <chrono>
@@ -396,7 +397,7 @@ bool Verifier::release_gpu_waits(int timeout_ms) {
// the words the spin kernels read (wait_flag_ge, wait_flag_ge_or) are mapped host memory, so a store here
// reaches them with no API call; UINT32_MAX is past every ring. (E-6's skip words are device memory, but
// wait_flag_ge_or also returns on its flag.) A host function raising flag B later only raises.
- for (uint32_t* p : {h_flag_, h_flagA_, h_flagB_})
+ for (uint32_t* p : {h_flag_, h_flagA_, h_flagB_, hflag_in_h_}) // + an overlapped split's hand-off wait
if (p != nullptr) *(volatile uint32_t*) p = UINT32_MAX;
std::atomic_thread_fence(std::memory_order_seq_cst);
_mm_sfence();
@@ -529,6 +530,7 @@ Verifier::~Verifier() {
for (cudaEvent_t e : df_join_)
if (e) cudaEventDestroy(e);
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_};
for (void* h : hosts)
@@ -713,6 +715,10 @@ bool Verifier::init(const WeightTable& wt, const ModelGeometry& g, SessionState&
return false;
}
cudaMemset(arena_, 0, count.used);
+ if (hflag_out_d_ != nullptr && hcount_d_ == nullptr) { // the overlapped split's hand-off counter
+ if (cudaMalloc((void**) &hcount_d_, 64) != cudaSuccess) { err = "verify: hand-off counter allocation failed"; return false; }
+ cudaMemset(hcount_d_, 0, 64);
+ }
if (g_trace && trace_h_ == nullptr) { // #649: the breadcrumbs, mapped so they read while the GPU hangs
trace_n_ = (size_t) (g.n_layers + 1) * kProfPer * 2;
if (!mapped(trace_n_ * 8, (void**) &trace_h_, (void**) &trace_m_)) {
@@ -957,6 +963,8 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
const int64_t HB = Verifier::handoff_floats(g);
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);
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);
@@ -1795,6 +1803,11 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
}
if (le_ < g.n_layers) { // a layer split's earlier stage: hand the residual on, no head
float* hout = hand_out_ + (size_t) hrow0 * HB;
+ if (hflag_out_d_ != nullptr && !batch_rec_) { // the overlapped split: payload + the next stage's flag
+ handoff_publish(hout, R_, (int64_t) T * HC * N, bo_, (int64_t) T * N, inj2_, (int64_t) T * HC, hcount_d_,
+ hflag_out_d_, cs);
+ return true;
+ }
copy_from_mapped(hout, R_, (int64_t) T * HC * N, cs);
copy_from_mapped(hout + (size_t) T * HC * N, bo_, (int64_t) T * N, cs);
copy_from_mapped(hout + (size_t) T * (HC + 1) * N, inj2_, (int64_t) T * HC, cs);
@@ -2225,7 +2238,10 @@ void Verifier::stage_inputs(int T, const int32_t* tokens, int64_t pos0) {
*(volatile uint32_t*) h_flag_ = 0;
*(volatile uint32_t*) h_flagA_ = 0;
*(volatile uint32_t*) h_flagB_ = 0;
+ // the overlapped split: the next stage's graph is launched after this, and must see THIS window's raise
+ if (hflag_out_h_ != nullptr) *(volatile uint32_t*) hflag_out_h_ = 0;
std::atomic_thread_fence(std::memory_order_seq_cst);
+ _mm_sfence();
last_t_ = T;
last_pos0_ = pos0;
for (int t = 0; t < T; ++t) last_tokens_[t] = tokens[t];
@@ -2298,6 +2314,36 @@ bool Verifier::adapt_gate_wait(int32_t layer, std::string& err) {
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();
+}
+
+bool Verifier::prelaunch(int T, const int32_t* tokens, int64_t pos0, std::string& err) {
+ const OnDevice on_device(device_);
+ prelaunched_ = 0;
+ cudaGraphExec_t exec_t = ar_off_ ? exec_nr_[T] : exec_[T]; // #871: the variant the previous stage refreshed
+ if (T < 1 || T > max_t_ || exec_t == nullptr) { err = "verify: prelaunch before the window was captured"; return false; }
+ if (released_.load()) { err = "verify: an earlier window never finished on the GPU (#267); restart the engine"; return false; }
+ if (pos0 + T > ss_->qsa_states[ss_->qsa_primary()].max_cells) { err = "verify: the window runs past the context"; return false; }
+ const Clock::time_point t0 = Clock::now();
+ stage_inputs(T, tokens, pos0);
+ if (trace_h_ != nullptr) std::memset(trace_h_, 0, trace_n_ * 8);
+ std::atomic_thread_fence(std::memory_order_seq_cst);
+ trace_ev("WINDOW-PRE", -1, -1, pos0 * 16 + T);
+ ms_host += ms_since(t0);
+ if (ar_on() && h_plan_err_ != nullptr) *(volatile uint32_t*) h_plan_err_ = 0;
+ // 0.1.43: the same gate run() applies before it launches (the adaptive tier's copies land first)
+ if (adapt_gate_ != nullptr && adapt_gate_->active.load(std::memory_order_acquire) && ar_on() &&
+ !adapt_gate_wait((int32_t) g_->n_layers, err)) return false;
+ const cudaError_t le = cudaGraphLaunch(exec_t, cs_);
+ trace_ev("LAUNCHED", -1, -1, (int64_t) le);
+ if (le != cudaSuccess) { err = std::string("verify: launch: ") + cudaGetErrorString(le); return false; }
+ (void) cudaStreamQuery(cs_);
+ prelaunched_ = T;
+ return true;
+}
+
bool Verifier::run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool, void* user, int32_t* out,
std::string& err) {
#if defined(STRATA_GPU_METAL)
@@ -2313,11 +2359,21 @@ bool Verifier::run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool,
const ModelGeometry& g = *g_;
SessionState& ss = *ss_;
if (pos0 + T > ss.qsa_states[ss.qsa_primary()].max_cells) { err = "verify: the window runs past the context"; return false; }
- refresh_ar();
+ // the overlapped split launched this stage's window already: keep the graph variant (#871) chosen for it then,
+ // or this thread would serve the window as the other variant
+ const bool pre = prelaunched_ == T;
+ prelaunched_ = 0;
+ if (!pre) refresh_ar();
if (!capture(T, err) || !capture_commit(err)) return false;
+ const bool chain = chain_overlap();
+ if (chain) { // capture the next stage's window here, on this thread, so the helper only stages and launches
+ const OnDevice on_next(next_->device_);
+ next_->refresh_ar(); // #871: the variant its prelaunch and its run() will both use
+ if (!next_->capture(T, err) || !next_->capture_commit(err)) return false;
+ }
VDBG("captured; staging\n");
const Clock::time_point t0 = Clock::now();
- if (!staged_) stage_inputs(T, tokens, pos0);
+ if (!pre && !staged_) stage_inputs(T, tokens, pos0);
staged_ = false;
const bool do_ple = ss.ple.ready() && ple_stage();
uint32_t ple_rows[kVerifyMaxT * PLE_N_HEADS];
@@ -2330,35 +2386,64 @@ bool Verifier::run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool,
ss.ple.table->prefetch_rows(ple_rows + t * PLE_N_HEADS);
}
}
- if (trace_h_ != nullptr) std::memset(trace_h_, 0, trace_n_ * 8); // #649: this window's breadcrumbs only
- std::atomic_thread_fence(std::memory_order_seq_cst);
- trace_ev("WINDOW", -1, -1, pos0 * 16 + T);
- ms_host += ms_since(t0);
- VDBG("staged; launching\n");
- if (ar_on() && h_plan_err_ != nullptr) *(volatile uint32_t*) h_plan_err_ = 0;
- // 0.1.43: with the all-resident graph the host serves no layer, so nothing can hold a layer back: every copy first
- if (adapt_gate_ != nullptr && adapt_gate_->active.load(std::memory_order_acquire) && ar_on() &&
- !adapt_gate_wait((int32_t) g.n_layers, err)) return false;
#if !defined(STRATA_USE_HIP) && !defined(STRATA_HIP_GFX906)
- // A blocking instrumented launch cannot also serve the graph's CPU doorbells.
- static const bool threaded_launch = [] {
- const char* value = std::getenv("STRATA_VERIFY_ASYNC_LAUNCH");
- return value != nullptr && value[0] == '1';
- }();
- HostGraphLaunch launch(ar_off_ ? exec_nr_[T] : exec_[T], cs_, device_, threaded_launch,
- [this] { (void) release_gpu_waits(5000); });
- const cudaError_t le = launch.status();
+ std::optional<HostGraphLaunch> launch_o; // (not set when the previous stage launched this window already)
+#endif
+ if (!pre) {
+ if (trace_h_ != nullptr) std::memset(trace_h_, 0, trace_n_ * 8); // #649: this window's breadcrumbs only
+ std::atomic_thread_fence(std::memory_order_seq_cst);
+ trace_ev("WINDOW", -1, -1, pos0 * 16 + T);
+ ms_host += ms_since(t0);
+ VDBG("staged; launching\n");
+ if (ar_on() && h_plan_err_ != nullptr) *(volatile uint32_t*) h_plan_err_ = 0;
+ // 0.1.43: with the all-resident graph the host serves no layer, so nothing can hold a layer back: every copy first
+ if (adapt_gate_ != nullptr && adapt_gate_->active.load(std::memory_order_acquire) && ar_on() &&
+ !adapt_gate_wait((int32_t) g.n_layers, err)) return false;
+#if !defined(STRATA_USE_HIP) && !defined(STRATA_HIP_GFX906)
+ // A blocking instrumented launch cannot also serve the graph's CPU doorbells.
+ static const bool threaded_launch = [] {
+ const char* value = std::getenv("STRATA_VERIFY_ASYNC_LAUNCH");
+ return value != nullptr && value[0] == '1';
+ }();
+ launch_o.emplace(ar_off_ ? exec_nr_[T] : exec_[T], cs_, device_, threaded_launch,
+ [this] { (void) release_gpu_waits(5000); });
+ const cudaError_t le = launch_o->status();
#else
- const cudaError_t le = cudaGraphLaunch(ar_off_ ? exec_nr_[T] : exec_[T], cs_);
+ const cudaError_t le = cudaGraphLaunch(ar_off_ ? exec_nr_[T] : exec_[T], cs_);
#endif
- trace_ev("LAUNCHED", -1, -1, (int64_t) le);
- if (le != cudaSuccess) { err = std::string("verify: launch: ") + cudaGetErrorString(le); return false; }
+ trace_ev("LAUNCHED", -1, -1, (int64_t) le);
+ if (le != cudaSuccess) { err = std::string("verify: launch: ") + cudaGetErrorString(le); return false; }
#if !defined(STRATA_USE_HIP) && !defined(STRATA_HIP_GFX906)
- (void) launch.query();
+ (void) launch_o->query();
#else
- (void) cudaStreamQuery(cs_);
+ (void) cudaStreamQuery(cs_);
#endif
- VDBG("launched\n");
+ VDBG("launched\n");
+ }
+ // The overlapped split: the next stage's window is staged and launched on a helper thread while this thread
+ // serves this stage's layers; its graph waits on the GPU for this stage's hand-off flag. The guard joins the
+ // helper on every return, and on an early return (an error after the launch) releases the next stage's GPU
+ // waits so no spin kernel outlives the window (#267).
+ struct NextLaunch {
+ Verifier* next = nullptr;
+ std::thread th;
+ bool ok = true;
+ bool handed = false;
+ std::string err;
+ bool join(std::string& e) {
+ if (th.joinable()) th.join();
+ if (!ok) e = err;
+ return ok;
+ }
+ ~NextLaunch() {
+ if (th.joinable()) th.join();
+ if (next != nullptr && !handed) (void) next->release_gpu_waits(5000);
+ }
+ } nl;
+ if (chain) {
+ nl.next = next_;
+ nl.th = std::thread([&nl, T, tokens, pos0] { nl.ok = nl.next->prelaunch(T, tokens, pos0, nl.err); });
+ }
volatile uint32_t* const seq = h_seq_;
volatile uint32_t* const flag = h_flag_;
const int G = groups_[T] > 0 ? groups_[T] : 1;
@@ -2392,7 +2477,7 @@ bool Verifier::run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool,
if (now - last_flush > std::chrono::microseconds(2000)) {
last_flush = now;
#if !defined(STRATA_USE_HIP) && !defined(STRATA_HIP_GFX906)
- const cudaError_t q = launch.query();
+ const cudaError_t q = launch_o ? launch_o->query() : cudaStreamQuery(cs_);
#else
const cudaError_t q = cudaStreamQuery(cs_);
#endif
@@ -2407,7 +2492,7 @@ bool Verifier::run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool,
if (now - a > std::chrono::seconds(20)) {
// #267: the caller ends the engine; no spin kernel may outlive it
#if !defined(STRATA_USE_HIP) && !defined(STRATA_HIP_GFX906)
- trace_ev("TIMEOUT", k, l, (int64_t) launch.query());
+ trace_ev("TIMEOUT", k, l, (int64_t) (launch_o ? launch_o->query() : cudaStreamQuery(cs_)));
#else
trace_ev("TIMEOUT", k, l, (int64_t) cudaStreamQuery(cs_));
#endif
@@ -2469,6 +2554,25 @@ bool Verifier::run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool,
ms_pool += ms_since(b);
}
// (#646 staged the next stage's inputs here; 0.1.39b keeps the layer split's order: each stage stages its own)
+ if (chain) {
+ // The overlapped split: no host sync between the stages. The next stage's GPU starts on this stage's flag;
+ // this thread goes straight to serving the next stage's layers, and syncs this stage once the chain is done.
+ if (!nl.join(err)) return false;
+ nl.handed = true;
+ ++windows;
+ const bool ok = next_->run(T, tokens, pos0, pool, next_user_, out, err);
+ const OnDevice back(device_);
+ const cudaError_t se = cudaStreamSynchronize(cs_);
+ if (copy_used_) {
+ cudaStreamSynchronize(copy_);
+ copy_used_ = false;
+ }
+ if (!ok) return false;
+ if (se != cudaSuccess) { err = std::string("verify: ") + cudaGetErrorString(se); return false; }
+ commit_pending_ = false;
+ if (prof_on_ && G == 1) collect_profile();
+ return true;
+ }
progress_at("verify window: waiting for the GPU to finish the window (flags A/B/M raised)", (int64_t) T);
// #267: a window the GPU never finishes (a spin kernel that never sees its flag) holds the host here; the stall
// watchdog then releases every verifier's GPU waits (release_live_verifiers) before it ends the engine, so no
@@ -2476,7 +2580,7 @@ bool Verifier::run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool,
// itself stays a blocking sync: a cudaStreamQuery poll here cost IQ3_S ~3% decode (a core calling the driver
// beside the expert workers).
#if !defined(STRATA_USE_HIP) && !defined(STRATA_HIP_GFX906)
- const cudaError_t launch_result = launch.finish();
+ const cudaError_t launch_result = launch_o ? launch_o->finish() : cudaSuccess;
if (launch_result != cudaSuccess) {
err = std::string("verify: launch: ") + cudaGetErrorString(launch_result);
return false;
diff --git a/src/kernels/cuda/verify_kernels.cu b/src/kernels/cuda/verify_kernels.cu
index 420ddf5ce..75c6f3d34 100644
--- a/src/kernels/cuda/verify_kernels.cu
+++ b/src/kernels/cuda/verify_kernels.cu
@@ -1156,8 +1156,41 @@ __global__ void wait_flag_ge_kernel(const volatile uint32_t* flag, uint32_t valu
while (*flag < value) strata_spin_pause();
__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) {
+ 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)
+ out[i] = i < na ? a[i] : i < na + nb ? b[i - na] : c[i - na - nb];
+ __threadfence_system(); // this thread's stores reach host memory before the block counts itself done
+ __syncthreads();
+ if (threadIdx.x == 0) {
+ const uint32_t prev = atomicAdd(counter, 1u);
+ 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();
+ }
+ }
+}
} // namespace
+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) {
+ const int64_t n = na + nb + nc;
+ if (n <= 0 || dst == nullptr || counter == nullptr || flag == nullptr) {
+ std::fprintf(stderr, "handoff_publish: invalid arguments\n");
+ std::exit(1);
+ }
+ 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);
+ check("handoff_publish");
+}
+
namespace {
// one thread per window entry: up to kVerifyMaxT tokens x 10 routed experts (80), so 128 (#646 had 64: a window of
// 7+ tokens lost its last entries)
diff --git a/src/program/generate.cpp b/src/program/generate.cpp
index f8b6de291..b64155f4b 100644
--- a/src/program/generate.cpp
+++ b/src/program/generate.cpp
@@ -7833,11 +7833,33 @@ int main(int argc, char** argv) {
}
std::memset(hh, 0, hb);
}
+ // STRATA_SPLIT_OVERLAP=1 (opt-in; measured on 2x RX 7900 XTX only): a ready word per hand-off, raised by the
+ // writing stage's GPU, so the next stage's window is launched early and the host never syncs between
+ // stages (Verifier::set_handoff_flags). Not for --split-device 0 (both stages on one GPU).
+ const bool overlap = !split_same && [] {
+ const char* v = std::getenv("STRATA_SPLIT_OVERLAP");
+ return v != nullptr && std::atoi(v) != 0;
+ }();
+ std::vector<uint32_t*> hflag_h((size_t) n_stages - 1, nullptr), hflag_d((size_t) n_stages - 1, nullptr);
+ if (overlap)
+ for (size_t i = 0; i < hflag_h.size(); ++i) {
+ if (cudaHostAlloc((void**) &hflag_h[i], 64, cudaHostAllocMapped | cudaHostAllocPortable) != cudaSuccess ||
+ cudaHostGetDevicePointer((void**) &hflag_d[i], hflag_h[i], 0) != cudaSuccess) {
+ std::fprintf(stderr, "strata serve: the layer-split hand-off flag allocation failed\n");
+ return 1;
+ }
+ std::memset(hflag_h[i], 0, 64);
+ }
split_drive.base = &drive;
split_drive.n = n_stages;
for (int st = 0; st < n_stages; ++st) {
stage_ver(st).set_stage(st == 0 ? 0 : split_at[(size_t) st - 1], st + 1 < n_stages ? split_at[(size_t) st] : -1,
st == 0 ? nullptr : hand[(size_t) st - 1], st + 1 < n_stages ? hand[(size_t) st] : nullptr);
+ if (overlap)
+ stage_ver(st).set_handoff_flags(st == 0 ? nullptr : hflag_h[(size_t) st - 1],
+ st == 0 ? nullptr : hflag_d[(size_t) st - 1],
+ st + 1 < n_stages ? hflag_h[(size_t) st] : nullptr,
+ st + 1 < n_stages ? hflag_d[(size_t) st] : nullptr);
split_drive.end[st] = st + 1 < n_stages ? split_at[(size_t) st] : g.n_layers;
split_drive.cache_base[st] = drive.d.cache_base;
split_drive.cache_slot_off[st] = drive.d.cache_slot_off;
@@ -7877,7 +7899,8 @@ int main(int argc, char** argv) {
for (int st = 1; st < n_stages; ++st)
plan_s += ", " + std::to_string(split_at[(size_t) st - 1]) + "-" + std::to_string(split_drive.end[st] - 1) +
" (CUDA" + std::to_string(split_same ? 0 : stages[(size_t) st - 1]->dev) + ")";
- std::fprintf(stderr, "strata serve: layer split: layers %s, one hand-off per window\n", plan_s.c_str());
+ std::fprintf(stderr, "strata serve: layer split: layers %s, one hand-off per window%s\n", plan_s.c_str(),
+ overlap ? " (overlapped: STRATA_SPLIT_OVERLAP=1)" : "");
}
// --pipeline-windows: the odd windows' verifiers and their hand-off (initialized before `ver`, which stays the
// watchdog's verifier), and the drafter's own row buffer, which either parity's rows are copied into