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++

Merge branch 'f-139b-par' into f-139-rel

Niko1221f5410ae2be24
diff --cc include/strata/core/verify.hpp
index 68723d0ee,9e0f35686..e68f7b3a4
--- a/include/strata/core/verify.hpp
+++ b/include/strata/core/verify.hpp
@@@ -167,8 -210,36 +213,37 @@@ public
std::string profile_report();
private:
+ RemoteExpertOpt* remote_opt_ = nullptr;
bool capture(int T, std::string& err);
+ // batch windows (see init_slots)
+ std::vector<SessionState*> slots_;
+ bool batch_rec_ = false; ///< record_window is capturing a batch window
+ int row_base_ = 0; ///< ... its hand-off rows start here (a pipeline group's own rows)
+ int brow_[8] = {}; ///< ... and row t is slot brow_[t]
+ bool last_batch_ = false; ///< the last run was a batch window (set_plan_slot: one group)
+ std::map<uint64_t, cudaGraphExec_t> exec_bm_, commit_bm_; ///< key: batch_key(rows, S, hand-off base)
+ int last_rows_[8] = {}; ///< the slots of the last batch window's rows
+ static uint64_t batch_key(const int* rows, int S, int hbase) {
+ uint64_t k = (uint64_t) hbase << 40 | (uint64_t) S << 32;
+ for (int t = 0; t < S; ++t) k |= (uint64_t) (rows[t] & 15) << (4 * t);
+ return k;
+ }
+ // batch_launch / batch_poll
+ bool b_running_ = false;
+ int64_t b_k_ = 0, b_steps_ = 0;
+ std::chrono::steady_clock::time_point b_last_;
+ int32_t b_out_[8] = {};
+ std::vector<strata::kernels::SamplerParams> slot_sp_;
+ bool sample_rows(int S, std::string& err); ///< the sampled slots' rows of the last batch window
+ int32_t* h_commitb_ = nullptr; int32_t* m_commitb_ = nullptr; // per slot [1, 0, pos, -1 ..], stride 2 + max_t
+ int32_t* commitb_ = nullptr;
+ float* tail_snap_b_ = nullptr; ///< per (slot, QSA layer) indexer tail snapshot
+ void* arena_b_ = nullptr;
+ int64_t last_pos_b_[8] = {};
+ bool capture_batch(const int* rows, int S, int hbase, std::string& err);
+ void collect_profile(); ///< STRATA_VERIFY_PROFILE: add the last window's stamps to prof_sum_
+ bool capture_commit_batch(const int* rows, int S, int hbase, std::string& err);
+ bool stage_batch(const int* rows, int S, int hbase, const int32_t* tokens, const int64_t* pos, std::string& err);
strata::kernels::SamplerParams sampling_ = [] {
strata::kernels::SamplerParams s;
s.greedy = true;
diff --cc src/core/verify.cpp
index bc953de52,5ba7c93bf..dd5362bac
--- a/src/core/verify.cpp
+++ b/src/core/verify.cpp
@@@ -279,12 -277,15 +279,18 @@@ Verifier::~Verifier()
for (auto& e : exec_)
if (e) cudaGraphExecDestroy(e);
if (commit_exec_) cudaGraphExecDestroy(commit_exec_);
+ for (auto& kv : exec_bm_)
+ if (kv.second) cudaGraphExecDestroy(kv.second);
+ for (auto& kv : commit_bm_)
+ if (kv.second) cudaGraphExecDestroy(kv.second);
+ if (arena_b_) cudaFree(arena_b_);
+ if (h_commitb_) cudaFreeHost(h_commitb_);
if (cs_) cudaStreamDestroy(cs_);
+ if (sh_cs_) cudaStreamDestroy(sh_cs_);
if (copy_) { cudaStreamSynchronize(copy_); cudaStreamDestroy(copy_); }
if (commit_done_) cudaEventDestroy(commit_done_);
+ if (ev_fork_) cudaEventDestroy(ev_fork_);
+ if (ev_join_) cudaEventDestroy(ev_join_);
if (arena_) cudaFree(arena_);
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_};
@@@ -573,9 -553,12 +583,10 @@@ bool Verifier::record_window(int T, cud
// residual, pending write and inject (see set_stage)
const int64_t HB = Verifier::handoff_floats(g);
if (lb_ > 0) {
- copy_from_mapped(R_, hand_in_, (int64_t) T * HC * N, cs);
- copy_from_mapped(bo_, hand_in_ + (size_t) T * HC * N, (int64_t) T * N, cs);
- copy_from_mapped(inj2_, hand_in_ + (size_t) T * (HC + 1) * N, (int64_t) T * HC, cs);
- for (int t = 0; t < T; ++t) {
- const size_t hr = (size_t) (hrow0 + t) * HB;
- copy_from_mapped(Rt(t), hand_in_ + hr, HC * N, cs);
- copy_from_mapped(bo_ + (size_t) t * N, hand_in_ + hr + HC * N, N, cs);
- copy_from_mapped(inj2_ + (size_t) t * HC, hand_in_ + hr + HC * N + N, HC, cs);
- }
++ const float* hin = hand_in_ + (size_t) hrow0 * HB; // the group's rows: [R][bo][inj] contiguous
++ 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);
} else if (const NativeEmbed* ne = native_embed()) { // plan v0.3 P6: the GGUF-form table
ne->gather_dev(tok_, T, emb_, cs);
broadcast_streams(emb_, R_, N, (int) HC, T, cs);
@@@ -651,14 -612,10 +662,15 @@@
PleOut po;
po.normalized = normalized;
po.result = Rt(t);
+ float* hist = batch_rec_ ? slot_ss(t).ple_hist : ss.ple.hist; // the row's own history
try {
- ple_block(ple_ + t * N, Rt(t), hist, ss.ple.w, po, ss.ple.scratch, cs);
+ if (ple_batch_kv) {
- ple_block_projected(xn_ + (size_t) t * HC * N, z_ + (size_t) t * N, Rt(t), ss.ple.hist,
++ ple_block_projected(xn_ + (size_t) t * HC * N, z_ + (size_t) t * N, Rt(t), hist,
+ ss.ple.w, po, ss.ple.scratch, cs);
+ } else {
- ple_block(ple_ + t * N, Rt(t), ss.ple.hist, ss.ple.w, po, ss.ple.scratch, cs);
++ ple_block(ple_ + t * N, Rt(t), hist, ss.ple.w, po, ss.ple.scratch, cs);
+ }
- ple_history_advance(ss.ple.hist, normalized, cs);
+ ple_history_advance(hist, normalized, cs);
} catch (const std::exception& e) {
err = std::string("verify PLE: ") + e.what();
return false;
@@@ -1057,9 -999,12 +1110,10 @@@
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
- copy_from_mapped(hand_out_, R_, (int64_t) T * HC * N, cs);
- copy_from_mapped(hand_out_ + (size_t) T * HC * N, bo_, (int64_t) T * N, cs);
- copy_from_mapped(hand_out_ + (size_t) T * (HC + 1) * N, inj2_, (int64_t) T * HC, cs);
- for (int t = 0; t < T; ++t) {
- const size_t hr = (size_t) (hrow0 + t) * HB;
- copy_from_mapped(hand_out_ + hr, Rt(t), HC * N, cs);
- copy_from_mapped(hand_out_ + hr + HC * N, bo_ + (size_t) t * N, N, cs);
- copy_from_mapped(hand_out_ + hr + HC * N + N, inj2_ + (size_t) t * HC, HC, cs);
- }
++ float* hout = hand_out_ + (size_t) hrow0 * HB;
++ 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);
return true;
}
@@@ -1274,31 -1200,40 +1328,65 @@@ bool Verifier::capture_commit(std::stri
return true;
}
+void Verifier::stage_inputs(int T, const int32_t* tokens, int64_t pos0) {
+ using namespace strata::kernels;
+ const ModelGeometry& g = *g_;
+ const QsaShapes s = shapes_of(g);
+ int32_t* const pk = h_pos_ + (size_t) max_t_ * g.n_head;
+ int32_t* const pi = pk + (size_t) max_t_ * g.n_head_kv;
+ for (int t = 0; t < T; ++t) {
+ h_tok_[t] = tokens[t];
+ qsa_step_fill(h_step_ + t * kStepCount, pos0 + t, s);
+ const int32_t pos_t = (int32_t) (pos0 + t);
+ for (int64_t h = 0; h < g.n_head; ++h) h_pos_[t * g.n_head + h] = pos_t;
+ for (int64_t h = 0; h < g.n_head_kv; ++h) pk[t * g.n_head_kv + h] = pos_t;
+ for (int64_t h = 0; h < g.idx_q_heads; ++h) pi[t * g.idx_q_heads + h] = pos_t;
+ }
+ *(volatile uint32_t*) h_seq_ = 0;
+ *(volatile uint32_t*) h_flag_ = 0;
+ *(volatile uint32_t*) h_flagA_ = 0;
+ *(volatile uint32_t*) h_flagB_ = 0;
+ std::atomic_thread_fence(std::memory_order_seq_cst);
+ last_t_ = T;
+ last_pos0_ = pos0;
+ for (int t = 0; t < T; ++t) last_tokens_[t] = tokens[t];
+ staged_ = true;
+}
+
+ void Verifier::collect_profile() {
+ const ModelGeometry& g = *g_;
+ cudaMemcpy(prof_h_.data(), prof_, prof_h_.size() * 8, cudaMemcpyDeviceToHost);
+ const int64_t L = g.n_layers;
+ auto at = [&](int64_t l, int i) { return prof_h_[(size_t) (l * kProfPer + i)]; };
+ // D8: the derived columns below read stamps the hc-read kernels write themselves or the next
+ // layer's first. A slot no kernel stamped is 0 and its unsigned difference wrapped to ~1e19 ns -
+ // which is why (gap)/head/hc0 printed ~1e14 ms per window. A missing or out-of-order stamp now
+ // contributes nothing.
+ const auto gap = [](unsigned long long to, unsigned long long from) {
+ return (from != 0 && to != 0 && to >= from) ? (double) (to - from) : 0.0;
+ };
+ for (int64_t l = 0; l < L; ++l) {
+ const int kind = is_qsa_layer(g, l) ? 1 : 0;
+ unsigned long long prev = at(l, 0);
+ for (int i = 1; i <= 24; ++i) {
+ const unsigned long long x = at(l, i);
+ if (x == 0 || x < prev) continue;
+ prof_sum_[kind][i] += (double) (x - prev);
+ prev = x;
+ }
+ if (l + 1 < L) prof_sum_[kind][25] += gap(at(l + 1, 0), at(l, 24));
+ const double dn = gap(at(l, 27), at(l, 0)), dd = gap(at(l, 28), at(l, 27)), du = gap(at(l, 1), at(l, 28));
+ if (dn > 0 && dd > 0 && du > 0) { // the split exists: show it split, not twice
+ prof_sum_[kind][27] += dn; // hc-read0: norm
+ prof_sum_[kind][28] += dd; // down
+ prof_sum_[kind][29] += du; // up (through both halves, as before)
+ prof_sum_[kind][1] -= gap(at(l, 1), at(l, 0)); // (hc-read0 shown split)
+ }
+ }
+ prof_sum_[0][26] += gap(at(L, 1), at(L, 0));
+ ++prof_windows_;
+ }
+
bool Verifier::run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool, void* user, int32_t* out,
std::string& err) {
using namespace strata::kernels;
@@@ -1443,44 -1367,9 +1532,13 @@@
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; }
+ commit_pending_ = false;
progress_at("verify window: waiting for the expert copies", (int64_t) T);
- cudaStreamSynchronize(copy_); // no host function of this window may raise flag B in the next one
+ if (copy_used_) {
+ cudaStreamSynchronize(copy_); // no host function of this window may raise flag B in the next one
+ copy_used_ = false;
+ }
- if (prof_on_ && G == 1) { // the window's GPU stage stamps
- cudaMemcpy(prof_h_.data(), prof_, prof_h_.size() * 8, cudaMemcpyDeviceToHost);
- const int64_t L = g.n_layers;
- auto at = [&](int64_t l, int i) { return prof_h_[(size_t) (l * kProfPer + i)]; };
- // D8: the derived columns below read stamps the hc-read kernels write themselves or the next
- // layer's first. A slot no kernel stamped is 0 and its unsigned difference wrapped to ~1e19 ns -
- // which is why (gap)/head/hc0 printed ~1e14 ms per window. A missing or out-of-order stamp now
- // contributes nothing.
- const auto gap = [](unsigned long long to, unsigned long long from) {
- return (from != 0 && to != 0 && to >= from) ? (double) (to - from) : 0.0;
- };
- for (int64_t l = 0; l < L; ++l) {
- const int kind = is_qsa_layer(g, l) ? 1 : 0;
- unsigned long long prev = at(l, 0);
- for (int i = 1; i <= 24; ++i) {
- const unsigned long long x = at(l, i);
- if (x == 0 || x < prev) continue;
- prof_sum_[kind][i] += (double) (x - prev);
- prev = x;
- }
- if (l + 1 < L) prof_sum_[kind][25] += gap(at(l + 1, 0), at(l, 24));
- const double dn = gap(at(l, 27), at(l, 0)), dd = gap(at(l, 28), at(l, 27)), du = gap(at(l, 1), at(l, 28));
- if (dn > 0 && dd > 0 && du > 0) { // the split exists: show it split, not twice
- prof_sum_[kind][27] += dn; // hc-read0: norm
- prof_sum_[kind][28] += dd; // down
- prof_sum_[kind][29] += du; // up (through both halves, as before)
- prof_sum_[kind][1] -= gap(at(l, 1), at(l, 0)); // (hc-read0 shown split)
- }
- }
- prof_sum_[0][26] += gap(at(L, 1), at(L, 0));
- ++prof_windows_;
- }
+ if (prof_on_ && G == 1) collect_profile(); // the window's GPU stage stamps
// ---- a sampled or penalized request: the head's sampling again, host-side so its parameters are this call's
// 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
diff --cc src/program/generate.cpp
index 73742c767,e32c0d71a..085c3d8ce
--- a/src/program/generate.cpp
+++ b/src/program/generate.cpp
@@@ -5015,8 -4896,7 +5112,8 @@@ int main(int argc, char** argv)
vs.blob = thits.blob;
vs.slot_off = gs.cache.slot_offsets();
vs.n_slots = gs.cache.slots();
+ gs.ver.set_remote_expert_opt(remote_opt.get());
- ok_s = gs.ver.init(gs.wt, g, gs.ss, vs, gs.head.loaded() ? &gs.head : nullptr, o.spec, err);
+ ok_s = gs.ver.init(gs.wt, g, gs.ss, vs, gs.head.loaded() ? &gs.head : nullptr, std::max(o.spec, o.batch), err);
split_drive.cache_base[st] = gs.cache.device_slot(0);
split_drive.cache_slot_off[st] = gs.cache.slot_offsets();
split_drive.pcie_num[st] = pcie_num_of(gs.pcie_frac);
@@@ -5033,8 -4913,7 +5130,8 @@@
" (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());
}
+ ver.set_remote_expert_opt(remote_opt.get());
- if (!ver.init(wt, g, ss, vh, native_head.loaded() ? &native_head : nullptr, o.spec, err) ||
+ if (!ver.init(wt, g, ss, vh, native_head.loaded() ? &native_head : nullptr, std::max(o.spec, o.batch), err) ||
!mtp.bind(last_st ? last_st->wt : wt, last_st ? &last_st->head : &native_head, ver.final_R_all(), err)) {
std::fprintf(stderr, "strata serve: %s\n", err.c_str());
return 1;