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 v1 (step 1-2): a verify window can run a range of layers and hand its residual to the next stage
Niko122145266e06fc3f
diff --git a/include/strata/core/verify.hpp b/include/strata/core/verify.hppindex bf48b5b80..001a9e4cc 100644--- a/include/strata/core/verify.hpp+++ b/include/strata/core/verify.hpp@@ -69,6 +69,7 @@ public:/// parameters would otherwise be baked forever - so this can change between requests freely.void set_sampling(const strata::kernels::SamplerParams& sp) {sampling_ = sp; // row t of a window at pos0 draws Philox(seed, pos0 + t): see run()+ if (next_) next_->set_sampling(sp);}/// The penalty histories for `sampling_.penalty_last_n`: ONE ROW PER WINDOW ROW, T rows of `history_len`@@ -80,18 +81,35 @@ public:void set_history(const int32_t* history, int history_len) {hist_d_ = history;hist_len_ = history_len;+ if (next_) next_->set_history(history, history_len);}/// Off: `run` skips the request's head sampling and `out` is the recorded greedy pick. For windows whose/// picks are discarded - a prompt read through windows commits every token - so they cost no sampler launch/// or sync and never read a history staged for another position.- void set_head_sampling(bool on) { head_sampling_ = on; }+ void set_head_sampling(bool on) { head_sampling_ = on; if (next_) next_->set_head_sampling(on); }++ /// LAYER SPLIT (multi-GPU): this verifier runs layers [layer_begin, layer_end) of every window. A stage that+ /// does not start at layer 0 takes its residual from `handoff_in` instead of embedding the tokens; a stage that+ /// does not end at the last layer writes its residual to `handoff_out` and has no head. The hand-off holds,+ /// per token, the residual R (hc x n_embd), the last layer's pending write bo (n_embd) and inject (hc): the next+ /// stage folds that write into its first read exactly as the unsplit window does, so the split is bit-exact.+ /// Both pointers must be device-visible (mapped pinned memory, portable when the stages are on two devices).+ /// Set before `init`. Default: the whole model, no hand-off.+ 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;+ }+ /// 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; }+ /// floats per token in a hand-off buffer+ static int64_t handoff_floats(const ModelGeometry& g) { return (int64_t) g.hc * g.n_embd + g.n_embd + g.hc; }/// 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);/// Token t's residual after the last layer, (hc, n_embd) on the device, valid until the next `run`.const float* final_R(int t) const;- const float* final_R_all() const { return R_; }+ const float* final_R_all() const { return next_ ? next_->final_R_all() : R_; }/// The GPU plan the pool writes each layer (VRAM hits + the PCIe share of the misses); give it to the/// dispatch (`ExpertDispatch::plan`) before the first `run`.@@ -119,6 +137,12 @@ private:const int32_t* hist_d_ = nullptr; ///< penalty-history row (set_history); null = no penalties applyint hist_len_ = 0;bool head_sampling_ = true; ///< set_head_sampling+ 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;+ Verifier* next_ = nullptr;+ void* next_user_ = nullptr;+ bool ple_stage() const { return lb_ <= 1 && 1 < le_; } ///< holds layer 1, where the PLE block runsbool capture_commit(std::string& err);bool record_window(int T, cudaStream_t cs, std::string& err);diff --git a/src/core/verify.cpp b/src/core/verify.cppindex 72fccd5e3..ba46cac90 100644--- a/src/core/verify.cpp+++ b/src/core/verify.cpp@@ -153,6 +153,12 @@ bool Verifier::init(const WeightTable& wt, const ModelGeometry& g, SessionState&err = "verify: geometry differs from the artifact's";return false;}+ if (le_ < 0) le_ = g.n_layers;+ if (lb_ < 0 || lb_ >= le_ || le_ > g.n_layers || (lb_ > 0 && hand_in_ == nullptr) ||+ (le_ < g.n_layers && hand_out_ == nullptr)) {+ err = "verify: the stage's layer range or its hand-off buffers are wrong";+ return false;+ }const WeightRef* wo = wt.find("output.weight");if (wo == nullptr) { err = "verify: output.weight is missing"; return false; }n_vocab_ = wo->ne1;@@ -297,7 +303,7 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {(uint64_t) g.ssm_conv_channels * (g.ssm_d_conv - 1);const int64_t HS = (int64_t) NG_HIST * NG_HC_DIM;const int64_t TS = (s.idx_block - 1) * ID;- const bool ple_on = ss.ple.ready();+ const bool ple_on = ss.ple.ready() && ple_stage();auto Rt = [&](int t) { return R_ + (size_t) t * HC * N; };const int G = (split_ && T >= 2) ? 2 : 1;const int tb_[2] = {0, (T + 1) / 2}, te_[2] = {G == 2 ? (T + 1) / 2 : T, T};@@ -309,8 +315,16 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {copy_i32_from_mapped(pos_, m_pos_, (int64_t) T * NH, cs);if (ple_on) copy_from_mapped(ple_, m_ple_, (int64_t) T * N, cs);- // ---- the embeddings, broadcast to the hc streams- if (const NativeEmbed* ne = native_embed()) { // plan v0.3 P6: the GGUF-form table+ // ---- the embeddings, broadcast to the hc streams - or, in a later stage of a layer split, the previous stage's+ // residual, pending write and inject (see set_stage)+ const int64_t HB = Verifier::handoff_floats(g);+ if (lb_ > 0) {+ for (int t = 0; t < T; ++t) {+ copy_from_mapped(Rt(t), hand_in_ + (size_t) t * HB, HC * N, cs);+ copy_from_mapped(bo_ + (size_t) t * N, hand_in_ + (size_t) t * HB + HC * N, N, cs);+ copy_from_mapped(inj2_ + (size_t) t * HC, hand_in_ + (size_t) t * HB + HC * N + N, HC, cs);+ }+ } else if (const NativeEmbed* ne = native_embed()) { // plan v0.3 P6: the GGUF-form tablene->gather_dev(tok_, T, emb_, cs);broadcast_streams(emb_, R_, N, (int) HC, T, cs);} else {@@ -547,7 +561,7 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {// ---------------------------------------------------------------- post(l, group): experts, combineauto post = [&](int64_t l, int grp) -> bool {const int tb = tb_[grp], te = te_[grp], n = te - tb;- const uint32_t ring = (uint32_t) (l * G + grp + 1);+ const uint32_t ring = (uint32_t) ((l - lb_) * G + grp + 1);wait_flag_ge(m_flagA_, ring, cs); // the pool published this group's GPU planconst int64_t cap = (int64_t) n * K, capx = (int64_t) max_t_ * K;int32_t* pl = plan_ + (size_t) grp * (size_t) (plan_i32_ + 16);@@ -602,12 +616,20 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {};for (int grp = 0; grp < G; ++grp)- if (!pre(0, grp)) return false;- for (int64_t l = 0; l < g.n_layers; ++l)+ if (!pre(lb_, grp)) return false;+ for (int64_t l = lb_; l < le_; ++l)for (int grp = 0; grp < G; ++grp) {if (!post(l, grp)) return false;- if (l + 1 < g.n_layers && !pre(l + 1, grp)) return false;+ if (l + 1 < le_ && !pre(l + 1, grp)) return false;+ }+ if (le_ < g.n_layers) { // a layer split's earlier stage: hand the residual on, no head+ for (int t = 0; t < T; ++t) {+ copy_from_mapped(hand_out_ + (size_t) t * HB, Rt(t), HC * N, cs);+ copy_from_mapped(hand_out_ + (size_t) t * HB + HC * N, bo_ + (size_t) t * N, N, cs);+ copy_from_mapped(hand_out_ + (size_t) t * HB + HC * N + N, inj2_ + (size_t) t * HC, HC, cs);}+ return true;+ }// ---- the head, T columns, and the argmax of each{@@ -695,7 +717,8 @@ bool Verifier::capture_commit(std::string& err) {try {copy_i32_from_mapped(commit_, m_commit_, 2 + MT, cs_);int64_t qsa_index = 0, gdn_index = 0;- for (int64_t l = 0; l < g.n_layers && ok; ++l) {+ for (int64_t l = 0; l < lb_; ++l) (is_qsa_layer(g, l) ? qsa_index : gdn_index) += 1;+ for (int64_t l = lb_; l < le_ && ok; ++l) {const LayerView v(*wt_, l);if (!is_qsa_layer(g, l)) {const WeightRef* wnm = need(v, "ssm_norm.weight", err);@@ -721,7 +744,7 @@ bool Verifier::capture_commit(std::string& err) {++qsa_index;}}- if (ok && ss.ple.ready()) copy_indexed(ss.ple.hist, hist_snap_, HS, commit_ + 1, HS, cs_);+ if (ok && ss.ple.ready() && ple_stage()) copy_indexed(ss.ple.hist, hist_snap_, HS, commit_ + 1, HS, cs_);} catch (const std::exception& e) {err = std::string("verify commit: ") + e.what();ok = false;@@ -757,7 +780,7 @@ bool Verifier::run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool,qsa_step_fill(h_step_ + t * kStepCount, pos0 + t, s);for (int64_t h = 0; h < g.n_head; ++h) h_pos_[t * g.n_head + h] = (int32_t) (pos0 + t);}- if (ss.ple.ready()) {+ if (ss.ple.ready() && ple_stage()) {uint32_t rows[kVerifyMaxT * PLE_N_HEADS];int32_t prev[2] = {ss.ple_prev[0], ss.ple_prev[1]};for (int t = 0; t < T; ++t) {@@ -785,8 +808,8 @@ bool Verifier::run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool,volatile uint32_t* const flag = h_flag_;const int G = groups_[T] > 0 ? groups_[T] : 1;const int gtb[2] = {0, (T + 1) / 2}, gte[2] = {G == 2 ? (T + 1) / 2 : T, T};- for (int64_t k = 0; k < g.n_layers * G; ++k) {- const int64_t l = k / G;+ for (int64_t k = 0; k < (le_ - lb_) * G; ++k) {+ const int64_t l = lb_ + k / G;const int grp = (int) (k % G);const uint32_t want = (uint32_t) (k + 1);const Clock::time_point a = Clock::now();@@ -844,6 +867,10 @@ bool Verifier::run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool,// own (a captured kernel would replay the same draws forever). Row t's draw is Philox(seed, pos0 + t): tied to// the POSITION it samples, not to how the text was cut into windows, so a seed replays the same text whatever// the drafts were. Exact: a rejected row's draw is discarded, and no kept decision depends on a reused draw.+ if (le_ < g.n_layers) { // a layer split's earlier stage: the hand-off is written (synced above)+ ++windows;+ return next_ == nullptr || next_->run(T, tokens, pos0, pool, next_user_, out, err);+ }const bool sampled = !sampling_.greedy && sampling_.temperature > 0.0f;if (head_sampling_ && (sampled || hist_d_ != nullptr)) {SamplerParams sp = sampling_;@@ -944,12 +971,13 @@ bool Verifier::commit(int n_keep, std::string& err) {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; }- for (int t = 0; t < n_keep; ++t) {- ss_->ple_prev[0] = ss_->ple_prev[1];- ss_->ple_prev[1] = last_tokens_[t];- }+ 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];+ ss_->ple_prev[1] = last_tokens_[t];+ }ms_commit += ms_since(t0);- return true;+ return next_ == nullptr || next_->commit(n_keep, err);}} // namespace strata::corediff --git a/src/kernels/cuda/fused_gr.cu b/src/kernels/cuda/fused_gr.cuindex c439ee48c..ba29acce5 100644--- a/src/kernels/cuda/fused_gr.cu+++ b/src/kernels/cuda/fused_gr.cu@@ -315,11 +315,15 @@ void fused_gr_read_multi(const FusedGrArgs* a, int n_tok, float* xn_scratch, voim.T = n_tok;cudaStream_t st = (cudaStream_t) stream;gr_norm_multi_kernel<<<n_tok, THREADS, 0, st>>>(m);- static bool attr = false;- if (!attr) {+ // the shared-memory opt-in is a per-DEVICE setting: once per device, not once per process (a layer split+ // runs this kernel on two cards)+ static bool attr[64] = {};+ int dev = 0;+ cudaGetDevice(&dev);+ if (dev < 0 || dev >= 64 || !attr[dev]) {cudaFuncSetAttribute(gr_down_multi_kernel, cudaFuncAttributeMaxDynamicSharedMemorySize,(int) (kFusedGrMaxT * TILE * sizeof(float)));- attr = true;+ if (dev >= 0 && dev < 64) attr[dev] = true;}gr_down_multi_kernel<<<DOWN_BLOCKS + 1, THREADS, (size_t) n_tok * TILE * sizeof(float), st>>>(m);gr_up_multi_kernel<<<UPM_BLOCKS, THREADS, 0, st>>>(m);diff --git a/src/kernels/cuda/qsa.cu b/src/kernels/cuda/qsa.cuindex d81b1d829..f783164ed 100644--- a/src/kernels/cuda/qsa.cu+++ b/src/kernels/cuda/qsa.cu@@ -656,7 +656,11 @@ void qsa_attend_step(const float* q, const uint16_t* k_scratch, const uint16_t*// then index past it at token 500. The kernel reads the real count from `step`, so a larger allocation is// exactly what it wants. The opt-in therefore happens once, for the capacity.const size_t smem = (size_t) (max_ids + 32) * sizeof(float);- static size_t s_configured = 0;+ // per DEVICE: the opt-in is a device setting (a layer split runs this on two cards)+ static size_t s_configured_dev[64] = {};+ int cur_dev = 0;+ cudaGetDevice(&cur_dev);+ size_t& s_configured = s_configured_dev[(cur_dev >= 0 && cur_dev < 64) ? cur_dev : 0];if (smem > s_configured) {int dev = 0, max_shared = 0;cudaGetDevice(&dev);diff --git a/src/program/generate.cpp b/src/program/generate.cppindex 3b3141d2b..2bc177183 100644--- a/src/program/generate.cpp+++ b/src/program/generate.cpp@@ -257,6 +257,8 @@ struct Options {bool stop_eos = false;std::vector<int64_t> eos_ids = {248044, 248046};bool spec_split = false; ///< opt-in split verify window (the overlap study: exact, ~7% slower)+ /// --serve: run layers [0, K) and [K, n_layers) as two verify stages (multi-GPU layer split; 0 = off)+ int64_t layer_split = 0;/// Plan v0.3 P8: stay resident and take requests on stdin (see the --serve block in main).bool serve = false;/// The vision path: keep a per-cell (t, h, w) rotary position table so --serve can take GENI requests.@@ -508,6 +510,20 @@ void drive_pool_multi(void* user, const float* x_f, const int32_t* ids, int64_t++t->calls;}+/// Layer split: both verify stages share one Drive (its counters, PCIe share, failure flags); the GPU plan the+/// pool publishes goes to the verifier that runs the layer.+struct SplitDrive {+ Drive* base = nullptr;+ strata::core::GpuPlanSink* plan[2] = {};+ int64_t split = 0;+};+void drive_pool_split(void* user, const float* x_f, const int32_t* ids, int64_t n_tok, int64_t k, float* out,+ int64_t layer) {+ SplitDrive* s = (SplitDrive*) user;+ s->base->d.plan = s->plan[layer < s->split ? 0 : 1];+ drive_pool_multi(s->base, x_f, ids, n_tok, k, out, layer);+}+// ---- issue #31: what the watchdog prints before it stops a stalled enginestruct MemSample {unsigned long long faults = 0, rss_mib = 0, avail_mib = 0, commit_mib = 0; // faults: hard (Linux) / all (Windows)@@ -931,6 +947,7 @@ int main(int argc, char** argv) {else if (a == "--spec-min-p") o.spec_min_p = std::atof(next("--spec-min-p"));else if (a == "--stop-eos") o.stop_eos = true;else if (a == "--spec-split") o.spec_split = true;+ else if (a == "--layer-split") o.layer_split = std::atoll(next("--layer-split"));else if (a == "--pcie-mode") o.pcie_mode = next("--pcie-mode");else if (a == "--serve") o.serve = true;else if (a == "--vision") o.vision = true;@@ -2593,11 +2610,52 @@ int main(int argc, char** argv) {vh.d_res = thits.d_res;vh.cache_base = thits.cache_base;vh.blob = thits.blob;+ // Layer split (multi-GPU, v1: both stages on this device, sharing its weights, session and expert cache):+ // `ver` runs layers [0, K) and hands its residual to `ver_b`, which runs the rest and the head. The+ // hand-off is mapped pinned memory, portable so a stage on another device can read it.+ strata::core::Verifier ver_b;+ SplitDrive split_drive;+ float* handoff_d = nullptr;+ if (o.layer_split != 0) {+ if (o.layer_split < 2 || o.layer_split >= g.n_layers) {+ std::fprintf(stderr, "strata serve: --layer-split must be 2..%lld\n", (long long) (g.n_layers - 1));+ return 1;+ }+ float* handoff_h = nullptr;+ const size_t hb = (size_t) strata::kernels::kVerifyMaxT *+ (size_t) strata::core::Verifier::handoff_floats(g) * sizeof(float);+ if (cudaHostAlloc((void**) &handoff_h, hb, cudaHostAllocMapped | cudaHostAllocPortable) != cudaSuccess ||+ cudaHostGetDevicePointer((void**) &handoff_d, handoff_h, 0) != cudaSuccess) {+ std::fprintf(stderr, "strata serve: the layer-split hand-off allocation failed\n");+ return 1;+ }+ std::memset(handoff_h, 0, hb);+ ver.set_stage(0, o.layer_split, nullptr, handoff_d);+ ver_b.set_stage(o.layer_split, -1, handoff_d, nullptr);+ if (!ver_b.init(wt, g, ss, vh, native_head.loaded() ? &native_head : nullptr, o.spec, err)) {+ std::fprintf(stderr, "strata serve: layer split, stage 2: %s\n", err.c_str());+ return 1;+ }+ split_drive.base = &drive;+ split_drive.split = o.layer_split;+ ver.set_next(&ver_b, &split_drive);+ std::fprintf(stderr, "strata serve: layer split: layers 0-%lld, then %lld-%lld (one hand-off per window)\n",+ (long long) (o.layer_split - 1), (long long) o.layer_split, (long long) (g.n_layers - 1));+ }if (!ver.init(wt, g, ss, vh, native_head.loaded() ? &native_head : nullptr, o.spec, err) ||!mtp.bind(wt, &native_head, ver.final_R_all(), err)) {std::fprintf(stderr, "strata serve: %s\n", err.c_str());return 1;}+ if (o.layer_split != 0) {+ split_drive.plan[0] = ver.plan_sink();+ split_drive.plan[1] = ver_b.plan_sink();+ ver_b.set_split(o.spec_split);+ ver_b.set_pcie_mode(o.pcie_mode == "dma" ? 0 : o.pcie_mode == "direct" ? 1 : 2);+ }+ // the pool the verify windows call: with a layer split, the wrapper that routes each layer's GPU plan+ const strata::core::PoolMultiFn win_pool_fn = o.layer_split != 0 ? &drive_pool_split : &drive_pool_multi;+ void* const win_pool_user = o.layer_split != 0 ? (void*) &split_drive : (void*) &drive;mem_mark("the verifier and the drafter's binding");ver.set_split(o.spec_split);// auto: the copy kernel for every pack. DMA (the native packs' default until 0.1.13) has the host call@@ -3144,7 +3202,7 @@ int main(int argc, char** argv) {drive.d.layers = 0;drive.d.experts = 0;drive.d.failed = false;- if (!ver.run(T, win.data(), q, &drive_pool_multi, &drive, outw.data(), e) || drive.d.failed) {+ if (!ver.run(T, win.data(), q, win_pool_fn, win_pool_user, outw.data(), e) || drive.d.failed) {if (drive.d.failed && drive.d.fail) e = drive.d.fail;return false;}@@ -3345,7 +3403,7 @@ int main(int argc, char** argv) {cudaMemcpyHostToDevice);}tr("window", p, T);- if (!ver.run(T, window.data(), p, &drive_pool_multi, &drive, outv.data(), err) || drive.d.failed) {+ if (!ver.run(T, window.data(), p, win_pool_fn, win_pool_user, outv.data(), err) || drive.d.failed) {std::printf("ERR %s\n", drive.d.failed && drive.d.fail ? drive.d.fail : err.c_str());return 1;}