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 PR #109 (q8atnight): batch the verify window's per-token kernels

Niko12211977f5952b9d
diff --cc include/strata/core/verify.hpp
index a0a3a2acb,3d32fc15d..bc0051db1
--- a/include/strata/core/verify.hpp
+++ b/include/strata/core/verify.hpp
@@@ -124,6 -105,8 +125,7 @@@ public
/// arena directly, 2 = a copy kernel stages it inside the graph (no API calls on the pool's thread; best when
/// the CPU is RAM-bound, Q2_0). Set before the first `run`.
void set_pcie_mode(int mode) { sink_.pcie_mode = mode; }
+ /// the pool never plans a PCIe share (--pcie-frac 0): the window skips that path. Before the first run.
- void set_pcie_off(bool off) { pcie_off_ = off; }
double ms_wait = 0, ms_pool = 0, ms_host = 0, ms_commit = 0;
int64_t windows = 0;
@@@ -139,18 -125,14 +144,24 @@@ private
const int32_t* hist_d_ = nullptr; ///< penalty-history row (set_history); null = no penalties apply
int hist_len_ = 0;
bool head_sampling_ = true; ///< set_head_sampling
+ int device_ = -1; ///< the device `init` ran on: run/commit switch to it (layer split)
+ bool device_plan_ = false; ///< E-6: resident-only layers planned on the device (STRATA_VERIFY_DEVICE_PLAN)
+ uint32_t* skip_ = nullptr; ///< E-6: per group, the ring whose plan the device built (0: the host's)
+ unsigned long long* slot_off_d_ = nullptr; ///< E-6: the slot offsets on the device
+ 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 runs
bool capture_commit(std::string& err);
bool record_window(int T, cudaStream_t cs, std::string& err);
+ static constexpr int kProfPer = 32; // stamps per layer
+ bool prof_on_ = false;
+ unsigned long long* prof_ = nullptr; // device: n_layers * kProfPer + 4 stamps
+ std::vector<unsigned long long> prof_h_;
+ double prof_sum_[2][kProfPer] = {}; // [GDN / QSA layers][stage]
+ int64_t prof_windows_ = 0;
const WeightTable* wt_ = nullptr;
const ModelGeometry* g_ = nullptr;
diff --cc include/strata/kernels/verify_kernels.hpp
index 4bf28b687,1ce71cd31..50d951a69
--- a/include/strata/kernels/verify_kernels.hpp
+++ b/include/strata/kernels/verify_kernels.hpp
@@@ -38,23 -38,9 +38,25 @@@ void gdn_step_norm_multi(float* state,
/// Spin until *flag >= value (a mapped host flag). The value is fixed at capture, so several rings can be
/// outstanding at once (the split verify window keeps two).
void wait_flag_ge(const uint32_t* flag, uint32_t value, void* stream);
+ /// the GPU's %globaltimer (ns) into buf[i] (a one-thread kernel: the verify window's stage profiler).
+ void gpu_stamp(unsigned long long* buf, int i, void* stream);
+// ---- perf-review E-6: a layer whose routed experts are all in VRAM needs nothing from the host
+/// One group's plan, built on the device when every routed expert of its n*k entries is resident: the host pool's
+/// layout (counts | start | dst | tok | pad | ptr | ptr2 | start2, `capx` entries) and order (distinct experts in
+/// routing order, their entries ascending), no PCIe groups. *skip = ring when it did, else 0.
+void resident_plan(const int32_t* ids, int n_entries, int k, const int32_t* res_layer, int n_expert,
+ const uint8_t* cache_base, const unsigned long long* slot_off, long long blob, int32_t* plan,
+ long long capx, uint32_t* skip, uint32_t ring, void* stream);
+/// wait_flag_ge that also returns when *skip == value (device memory).
+void wait_flag_ge_or(const uint32_t* flag, uint32_t value, const uint32_t* skip, void* stream);
+/// copy_i32_from_mapped unless *skip == value.
+void copy_i32_from_mapped_unless(int32_t* dst, const int32_t* src, long long n, const uint32_t* skip, uint32_t value,
+ void* stream);
+/// copy_from_mapped, or zeros when *skip == value (n a multiple of 4, 16-byte aligned).
+void copy_or_zero_from_mapped(float* dst, const float* src, long long n, const uint32_t* skip, uint32_t value,
+ void* stream);
+
/// Rows of the S2/S4/S8 embedding for T token ids read from DEVICE memory; out (T, n). Bitwise `embedding_gather`.
void embedding_gather_dev(const uint8_t* codes, const float* scales, const float* offsets, const int32_t* tokens,
int n_tok, int64_t n, int code_bits, int code_bias, int group_elems, uint64_t row_codes,
diff --cc src/core/verify.cpp
index 6262ea5b5,8896731da..29643d6de
--- a/src/core/verify.cpp
+++ b/src/core/verify.cpp
@@@ -320,28 -309,25 +332,33 @@@ bool Verifier::record_window(int T, cud
(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;
+ static const bool dec_batch = [] { const char* v = std::getenv("STRATA_DEC_BATCH"); return v == nullptr || std::atoi(v) != 0; }();
+ auto stamp = [&](int64_t l, int i, int grp) { if (prof_on_ && grp == 0) gpu_stamp(prof_, (int) (l * kProfPer + i), cs); };
const int tb_[2] = {0, (T + 1) / 2}, te_[2] = {G == 2 ? (T + 1) / 2 : T, T};
groups_[T] = G;
// ---- the window's inputs, from mapped staging
copy_i32_from_mapped(tok_, m_tok_, T, cs);
copy_i32_from_mapped(step_, m_step_, (int64_t) T * kStepCount, cs);
- copy_i32_from_mapped(pos_, m_pos_, (int64_t) T * NH, cs);
+ copy_i32_from_mapped(pos_, m_pos_, (int64_t) MT * (NH + NKV + IQ), cs);
+ // per-ROW positions of the K rows [t][NKV] and the indexer query rows [t][IQ] (for batched RoPE)
+ const int32_t* pos_k = pos_ + MT * NH;
+ const int32_t* pos_i = pos_ + MT * (NH + NKV);
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 table
ne->gather_dev(tok_, T, emb_, cs);
broadcast_streams(emb_, R_, N, (int) HC, T, cs);
} else {
@@@ -545,13 -578,9 +609,14 @@@
mb.logits = logits_ + t * NE; mb.ids = ids_ + t * K; mb.weights = w_ + t * K;
if (!moe_route(wt, g, l, K, mb, mixed_ + t * N, cs, err, nullptr)) return false;
}
+ if (device_plan_) // E-6: every routed expert resident: this group's plan without the host
+ resident_plan(ids_ + tb * K, n * (int) K, (int) K, hits_.d_res + l * g.n_expert, (int) g.n_expert,
+ hits_.cache_base, slot_off_d_, (long long) hits_.blob,
+ plan_ + (size_t) grp * (size_t) (plan_i32_ + 16), (long long) max_t_ * K, skip_ + grp,
+ (uint32_t) ((l - lb_) * G + grp + 1), cs);
doorbell_publish(xm, ids_ + tb * K, w_ + tb * K, (int64_t) n * N, (int64_t) n * K, m_x_ + tb * N,
m_ids_ + tb * K, m_w_ + tb * K, m_seq_, cs);
+ stamp(l, 17, grp);
{
const WeightRef *wgi = need(v, "ffn_gate_inp_shexp.weight", err), *wsg = need(v, "ffn_gate_shexp.weight", err),
*wsu = need(v, "ffn_up_shexp.weight", err), *wsd = need(v, "ffn_down_shexp.weight", err);
@@@ -583,16 -614,12 +650,17 @@@
// ---------------------------------------------------------------- post(l, group): experts, combine
auto 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);
- wait_flag_ge(m_flagA_, ring, cs); // the pool published this group's GPU plan
+ const uint32_t ring = (uint32_t) ((l - lb_) * G + grp + 1);
const 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);
- copy_i32_from_mapped(pl, m_plan_ + (size_t) grp * (size_t) plan_i32_, plan_i32_, cs);
+ if (device_plan_) { // E-6: skipped when the device planned this group (all its experts resident)
+ wait_flag_ge_or(m_flagA_, ring, skip_ + grp, cs);
+ copy_i32_from_mapped_unless(pl, m_plan_ + (size_t) grp * (size_t) plan_i32_, plan_i32_, skip_ + grp, ring, cs);
+ } else {
+ wait_flag_ge(m_flagA_, ring, cs); // the pool published this group's GPU plan
+ copy_i32_from_mapped(pl, m_plan_ + (size_t) grp * (size_t) plan_i32_, plan_i32_, cs);
+ }
+ stamp(l, 19, grp);
const int32_t* p_counts = pl;
const int32_t* p_start = pl + 4;
const int32_t* p_dst = p_start + capx + 1;
@@@ -617,24 -644,32 +685,37 @@@
}
};
grouped(p_ptr, p_start, p_counts);
+ stamp(l, 20, grp);
- if (!pcie_off_) { // with --pcie-frac 0 there is never a PCIe share: no wait, no empty launch
- wait_flag_ge(m_flagB_, ring, cs); // the PCIe share is in staging (DMA) or mapped
+ if (device_plan_) wait_flag_ge_or(m_flagB_, ring, skip_ + grp, cs);
+ else wait_flag_ge(m_flagB_, ring, cs); // the PCIe share is in staging (DMA) or mapped
if (sink_.pcie_mode == 2) { // stage it with a copy kernel, then point at staging
const int64_t per = G == 2 ? kStagingBlobs / 2 : kStagingBlobs;
uint8_t* stage = staging_ + (size_t) (grp * per) * lay.max_blob;
fetch_blobs(p_ptr2, p_counts + 2, stage, (int64_t) lay.blob_bytes(l), (int) per, cs);
rebase_ptrs((unsigned long long*) p_ptr2, p_counts + 2, stage, (int64_t) lay.blob_bytes(l), cs);
}
+ stamp(l, 21, grp);
grouped(p_ptr2, p_start2, p_counts + 2);
- }
+ stamp(l, 22, grp);
- wait_flag_ge(m_flag_, ring, cs); // the CPU's share is in the mapped rows
- stamp(l, 23, grp);
- if (dec_batch) // only the CPU rows cross PCIe (p_dst[0, counts[1]) = the GPU's own rows)
- copy_rows_from_mapped(parts_ + (size_t) tb * K * N, m_ymiss_ + (size_t) tb * K * N, (int64_t) n * K, N, p_dst,
- p_counts + 1, cs);
- else
- copy_from_mapped(parts_ + (size_t) tb * K * N, m_ymiss_ + (size_t) tb * K * N, (int64_t) n * K * N, cs);
+ if (device_plan_) { // no CPU share when the device planned the group: its rows are zeros
+ wait_flag_ge_or(m_flag_, ring, skip_ + grp, cs);
+ copy_or_zero_from_mapped(parts_ + (size_t) tb * K * N, m_ymiss_ + (size_t) tb * K * N, (long long) n * K * N,
+ skip_ + grp, ring, cs);
+ } else {
- wait_flag_ge(m_flag_, ring, cs); // the CPU's share is in the mapped rows
- copy_from_mapped(parts_ + (size_t) tb * K * N, m_ymiss_ + (size_t) tb * K * N, (int64_t) n * K * N, cs);
++ wait_flag_ge(m_flag_, ring, cs); // the CPU's share is in the mapped rows
++ stamp(l, 23, grp);
++ if (dec_batch) // only the CPU rows cross PCIe (p_dst[0, counts[1]) = the GPU's own rows)
++ copy_rows_from_mapped(parts_ + (size_t) tb * K * N, m_ymiss_ + (size_t) tb * K * N, (int64_t) n * K, N,
++ p_dst, p_counts + 1, cs);
++ else
++ copy_from_mapped(parts_ + (size_t) tb * K * N, m_ymiss_ + (size_t) tb * K * N, (int64_t) n * K * N, cs);
+ }
moe_hit_add(parts_ + (size_t) tb * K * N, hit_out, p_dst, p_counts + 1, cap, N, cs);
+ if (dec_batch && n > 1 && native_moe_combine_enabled()) { // one launch for the window's rows
+ try {
+ native_moe_combine_multi(parts_ + (size_t) tb * K * N, w_ + tb * K, shared_ + tb * N, bo_ + tb * N, N, K, n, cs);
+ } catch (const std::exception& e) { err = "verify combine: " + std::string(e.what()); return false; }
+ } else
for (int t = tb; t < te; ++t) {
MoEBuffers mb = ss.moe;
mb.weights = w_ + t * K; mb.shared = shared_ + t * N;
@@@ -650,22 -686,15 +732,23 @@@
};
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
+ stamp(g.n_layers, 0, 0);
{
const WeightRef *hn = wt.find("output_hc_norm.weight"), *hd = wt.find("output_hc_down.weight"),
*hu = wt.find("output_hc_up.weight");
@@@ -814,8 -895,12 +951,12 @@@ bool Verifier::run(int T, const int32_t
h_tok_[t] = tokens[t];
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);
+ int32_t* pk = h_pos_ + (size_t) max_t_ * g.n_head;
+ int32_t* pi = pk + (size_t) max_t_ * g.n_head_kv;
+ for (int64_t h = 0; h < g.n_head_kv; ++h) pk[t * g.n_head_kv + h] = (int32_t) (pos0 + t);
+ for (int64_t h = 0; h < g.idx_q_heads; ++h) pi[t * g.idx_q_heads + 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) {
diff --cc src/kernels/cuda/fused_gr.cu
index 4b461cc7c,b95e5cfdb..2fac3535a
--- a/src/kernels/cuda/fused_gr.cu
+++ b/src/kernels/cuda/fused_gr.cu
@@@ -315,22 -317,15 +317,24 @@@ void fused_gr_read_multi(const FusedGrA
m.T = n_tok;
cudaStream_t st = (cudaStream_t) stream;
gr_norm_multi_kernel<<<n_tok, THREADS, 0, st>>>(m);
+ if (stamp_buf) gpu_stamp(stamp_buf, stamp_i0, stream);
- static bool attr = false;
- if (!attr) {
- cudaFuncSetAttribute(gr_down_multi_kernel, cudaFuncAttributeMaxDynamicSharedMemorySize,
- (int) (kFusedGrMaxT * TILE * sizeof(float)));
- attr = true;
+ // 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]) {
+ // at most what the card allows (Turing: 64 KB - enough for windows of up to 6 tokens)
+ int optin = 0;
+ cudaDeviceGetAttribute(&optin, cudaDevAttrMaxSharedMemoryPerBlockOptin, dev);
+ int want = (int) (kFusedGrMaxT * TILE * sizeof(float));
+ if (optin > 0 && want > optin) want = optin;
+ cudaFuncSetAttribute(gr_down_multi_kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, want);
+ cudaGetLastError();
+ 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);
+ if (stamp_buf) gpu_stamp(stamp_buf, stamp_i0 + 1, stream);
gr_up_multi_kernel<<<UPM_BLOCKS, THREADS, 0, st>>>(m);
const cudaError_t e = cudaGetLastError();
if (e != cudaSuccess) {
diff --cc src/program/generate.cpp
index 32307185f,035e956ad..7046bed17
--- a/src/program/generate.cpp
+++ b/src/program/generate.cpp
@@@ -4009,7 -3223,8 +4023,8 @@@ int main(int argc, char** argv)
cudaMemcpyHostToDevice);
}
tr("window", p, T);
+ const Clock::time_point tw0 = Clock::now();
- 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;
}