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++
E-6: a verify layer whose routed experts are all in VRAM needs nothing from the host
Niko12211c58de5ab276
diff --git a/include/strata/core/verify.hpp b/include/strata/core/verify.hppindex 349afcf75..a0a3a2acb 100644--- a/include/strata/core/verify.hpp+++ b/include/strata/core/verify.hpp@@ -44,6 +44,8 @@ using PoolMultiFn = void (*)(void* user, const float* x_f, const int32_t* ids, istruct VerifyHits {const int32_t* d_res = nullptr; ///< device [n_layers * n_expert] slot or -1const uint8_t* cache_base = nullptr; ///< slot 0 of the VRAM expert arena+ const uint64_t* slot_off = nullptr; ///< E-6: host per-slot offsets when slots differ in size (null: slot * blob)+ int64_t n_slots = 0; ///< E-6: how many (for the device copy)int64_t blob = 0;};@@ -138,6 +140,9 @@ private:int hist_len_ = 0;bool head_sampling_ = true; ///< set_head_samplingint 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 deviceint64_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;diff --git a/include/strata/kernels/verify_kernels.hpp b/include/strata/kernels/verify_kernels.hppindex d66093221..4bf28b687 100644--- a/include/strata/kernels/verify_kernels.hpp+++ b/include/strata/kernels/verify_kernels.hpp@@ -39,6 +39,22 @@ void gdn_step_norm_multi(float* state, const float* h, int conv_channels, const/// outstanding at once (the split verify window keeps two).void wait_flag_ge(const uint32_t* flag, uint32_t value, 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 --git a/src/core/verify.cpp b/src/core/verify.cppindex 226f09a19..068a75de9 100644--- a/src/core/verify.cpp+++ b/src/core/verify.cpp@@ -274,6 +274,20 @@ bool Verifier::init(const WeightTable& wt, const ModelGeometry& g, SessionState&err = "verify: stream create failed";return false;}+ // E-6: a layer whose routed experts are all resident is planned on the device (STRATA_VERIFY_DEVICE_PLAN=0: off)+ {+ const char* v = std::getenv("STRATA_VERIFY_DEVICE_PLAN");+ device_plan_ = v == nullptr || std::atoi(v) != 0;+ }+ if (device_plan_) {+ bool ok2 = cudaMalloc((void**) &skip_, 64) == cudaSuccess && cudaMemset(skip_, 0, 64) == cudaSuccess;+ if (ok2 && hits.slot_off != nullptr && hits.n_slots > 0) {+ ok2 = cudaMalloc((void**) &slot_off_d_, (size_t) hits.n_slots * sizeof(unsigned long long)) == cudaSuccess &&+ cudaMemcpy(slot_off_d_, hits.slot_off, (size_t) hits.n_slots * sizeof(unsigned long long),+ cudaMemcpyHostToDevice) == cudaSuccess;+ }+ if (!ok2) { cudaGetLastError(); device_plan_ = false; }+ }std::fprintf(stderr, "strata verify: window up to %d tokens, %.1f MiB of device buffers\n", max_t,(double) count.used / 1048576.0);return true;@@ -530,6 +544,11 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {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);{@@ -564,10 +583,15 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {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 - 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);- 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);+ }const int32_t* p_counts = pl;const int32_t* p_start = pl + 4;const int32_t* p_dst = p_start + capx + 1;@@ -592,7 +616,8 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {}};grouped(p_ptr, p_start, p_counts);- 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 mappedif (sink_.pcie_mode == 2) { // stage it with a copy kernel, then point at stagingconst int64_t per = G == 2 ? kStagingBlobs / 2 : kStagingBlobs;uint8_t* stage = staging_ + (size_t) (grp * per) * lay.max_blob;@@ -600,8 +625,14 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {rebase_ptrs((unsigned long long*) p_ptr2, p_counts + 2, stage, (int64_t) lay.blob_bytes(l), cs);}grouped(p_ptr2, p_start2, p_counts + 2);- 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);+ 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);+ }moe_hit_add(parts_ + (size_t) tb * K * N, hit_out, p_dst, p_counts + 1, cap, N, cs);for (int t = tb; t < te; ++t) {MoEBuffers mb = ss.moe;diff --git a/src/kernels/cuda/verify_kernels.cu b/src/kernels/cuda/verify_kernels.cuindex 6dde0cbf2..7f999ddc0 100644--- a/src/kernels/cuda/verify_kernels.cu+++ b/src/kernels/cuda/verify_kernels.cu@@ -428,6 +428,93 @@ __global__ void wait_flag_ge_kernel(const volatile uint32_t* flag, uint32_t valu}} // namespace+namespace {+__global__ void resident_plan_kernel(const int32_t* __restrict__ ids, int n, int k, const int32_t* __restrict__ res,+ int n_expert, const uint8_t* cache_base, const unsigned long long* slot_off,+ long long blob, int32_t* __restrict__ pl, long long capx, uint32_t* skip,+ uint32_t ring) {+ // one thread: at most kVerifyMaxT * 10 entries, the host's exact loop+ for (int i = 0; i < n; ++i) {+ const int32_t e = ids[i];+ if (e < 0 || e >= n_expert || res[e] < 0) { *skip = 0; return; }+ }+ int32_t* counts = pl;+ int32_t* start = pl + 4;+ int32_t* dst = start + capx + 1;+ int32_t* tok = dst + capx;+ const long long ptr_off = ((4 + (capx + 1) + 2 * capx) + 1) & ~1ll;+ unsigned long long* ptr = (unsigned long long*) (pl + ptr_off);+ int32_t* start2 = pl + ptr_off + 4 * capx;+ int groups = 0, entries = 0;+ for (int i0 = 0; i0 < n; ++i0) {+ bool first = true;+ for (int j = 0; j < i0; ++j) if (ids[j] == ids[i0]) { first = false; break; }+ if (!first) continue;+ const int32_t slot = res[ids[i0]];+ ptr[groups] = (unsigned long long) (cache_base + (slot_off ? (size_t) slot_off[slot] : (size_t) slot * (size_t) blob));+ start[groups] = entries;+ for (int i = i0; i < n; ++i)+ if (ids[i] == ids[i0]) {+ // an entry belongs to i0's group when its first occurrence is i0: the same expert id+ dst[entries] = i;+ tok[entries] = i / k;+ ++entries;+ }+ ++groups;+ }+ start[groups] = entries;+ start2[0] = entries;+ counts[0] = groups;+ counts[1] = entries;+ counts[2] = 0;+ __threadfence();+ *skip = ring;+}+__global__ void wait_flag_ge_or_kernel(const volatile uint32_t* flag, uint32_t value, const volatile uint32_t* skip) {+ if (*skip == value) return;+ while (*flag < value) __nanosleep(100);+ __threadfence_system();+}+__global__ void copy_i32_unless_kernel(int32_t* __restrict__ dst, const volatile int32_t* src, int n,+ const uint32_t* skip, uint32_t value) {+ if (*skip == value) return;+ for (int i = threadIdx.x; i < n; i += blockDim.x) dst[i] = src[i];+}+__global__ void copy_or_zero_kernel(float4* __restrict__ dst, const volatile float4* src, long long n4,+ const uint32_t* skip, uint32_t value) {+ const bool zero = *skip == value;+ for (long long i = (long long) blockIdx.x * blockDim.x + threadIdx.x; i < n4; i += (long long) gridDim.x * blockDim.x)+ dst[i] = zero ? make_float4(0.f, 0.f, 0.f, 0.f) : const_cast<const float4*>(src)[i];+}+} // namespace++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) {+ resident_plan_kernel<<<1, 1, 0, (cudaStream_t) stream>>>(ids, n_entries, k, res_layer, n_expert, cache_base, slot_off,+ blob, plan, capx, skip, ring);+ check("resident_plan");+}+void wait_flag_ge_or(const uint32_t* flag, uint32_t value, const uint32_t* skip, void* stream) {+ wait_flag_ge_or_kernel<<<1, 1, 0, (cudaStream_t) stream>>>(flag, value, skip);+ check("wait_flag_ge_or");+}+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) {+ if (n <= 0) return;+ copy_i32_unless_kernel<<<1, 128, 0, (cudaStream_t) stream>>>(dst, (const volatile int32_t*) src, (int) n, skip, value);+ check("copy_i32_from_mapped_unless");+}+void copy_or_zero_from_mapped(float* dst, const float* src, long long n, const uint32_t* skip, uint32_t value,+ void* stream) {+ if (n <= 0) return;+ const long long n4 = n / 4;+ const int blocks = (int) ((n4 + 255) / 256 < 64 ? (n4 + 255) / 256 : 64);+ copy_or_zero_kernel<<<blocks, 256, 0, (cudaStream_t) stream>>>((float4*) dst, (const volatile float4*) src, n4, skip,+ value);+ check("copy_or_zero_from_mapped");+}+void wait_flag_ge(const uint32_t* flag, uint32_t value, void* stream) {wait_flag_ge_kernel<<<1, 1, 0, (cudaStream_t) stream>>>(flag, value);check("wait_flag_ge");diff --git a/src/program/generate.cpp b/src/program/generate.cppindex 0364a73ed..48cfa53aa 100644--- a/src/program/generate.cpp+++ b/src/program/generate.cpp@@ -2975,6 +2975,8 @@ int main(int argc, char** argv) {vh.d_res = thits.d_res;vh.cache_base = thits.cache_base;vh.blob = thits.blob;+ vh.slot_off = xcache.slot_offsets(); // E-6: the device plan's pointers+ vh.n_slots = xcache.slots();// Layer split: `ver` runs layers [0, K1) and hands its residual to the next stage's verifier, and so on; the// last runs the head. The hand-offs are mapped pinned memory, portable: a stage on another GPU reads it.// (--split-device 0: the second stage on this GPU, sharing its weights, session and cache - the A/B.)@@ -3019,6 +3021,8 @@ int main(int argc, char** argv) {vs.d_res = gs.d_res;vs.cache_base = gs.cache.device_slot(0);vs.blob = thits.blob;+ vs.slot_off = gs.cache.slot_offsets();+ vs.n_slots = gs.cache.slots();ok_s = gs.ver.init(gs.wt, g, gs.ss, vs, gs.head.loaded() ? &gs.head : nullptr, o.spec, err);split_drive.cache_base[st] = gs.cache.device_slot(0);split_drive.cache_slot_off[st] = gs.cache.slot_offsets();@@ -4395,6 +4399,8 @@ int main(int argc, char** argv) {vh.d_res = thits.d_res;vh.cache_base = thits.cache_base;vh.blob = thits.blob;+ vh.slot_off = xcache.slot_offsets(); // E-6: the device plan's pointers+ vh.n_slots = xcache.slots();if (!ver.init(wt, g, ss, vh, native_head.loaded() ? &native_head : nullptr, o.spec, err)) {std::fprintf(stderr, "strata generate: %s\n", err.c_str());return 1;