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++
perf(verify): resident_plan as a parallel grouping and prefix scan (#783 PR-e)
Niko1221fe4de5de85c3
diff --git a/CMakeLists.txt b/CMakeLists.txtindex a902781ff..ce6989c1f 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -815,6 +815,11 @@ if(STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP)target_include_directories(kv_steps_parity PRIVATE ${_strata_gpu_include_directories})target_link_libraries(kv_steps_parity PRIVATE strata_kernels ${_strata_gpu_runtime_target})add_test(NAME kv_steps_parity COMMAND kv_steps_parity)+ # #783 PR-e: the device-built expert plan (parallel grouping + prefix scan) against a host replay of its contract+ add_executable(resident_plan_parity src/kernels/resident_plan_parity.cpp)+ target_include_directories(resident_plan_parity PRIVATE ${_strata_gpu_include_directories})+ target_link_libraries(resident_plan_parity PRIVATE strata_kernels ${_strata_gpu_runtime_target})+ add_test(NAME resident_plan_parity COMMAND resident_plan_parity)if(STRATA_HIP_GFX906)# the S6 router (STRATA_HIP_ROUTER_FAST=1 on gfx906) against the portable kernel, bitwise, on wave64add_executable(hip_router_fast tests/hip/router_fast.cpp)diff --git a/src/kernels/cuda/verify_kernels.cu b/src/kernels/cuda/verify_kernels.cuindex e54ab9b21..1451385ad 100644--- a/src/kernels/cuda/verify_kernels.cu+++ b/src/kernels/cuda/verify_kernels.cu@@ -958,30 +958,28 @@ namespace {// 7+ tokens lost its last entries)constexpr int kResidentPlanMax = 128;static_assert(kVerifyMaxT * 10 <= kResidentPlanMax, "resident_plan: one thread per entry");+// the parallel scan keeps one partial sum per warp in s_wsum[4] and packs (entries << 16 | groups) in an int+static_assert(kResidentPlanMax <= 128 && kResidentPlanMax % 32 == 0, "resident_plan: at most 4 warps");+static_assert(kResidentPlanMax < 32768, "resident_plan: the packed entry count must stay below 2^15");__global__ void __launch_bounds__(kResidentPlanMax) 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, volatile uint32_t* plan_err) {__shared__ int32_t s_ids[kResidentPlanMax];- __shared__ unsigned long long s_ptr[kResidentPlanMax];- __shared__ int32_t s_first[kResidentPlanMax];- __shared__ int32_t s_cnt[kResidentPlanMax];- __shared__ int32_t s_gstart[kResidentPlanMax];+ __shared__ int32_t s_excl[kResidentPlanMax];+ __shared__ int32_t s_wsum[4];__shared__ int s_bad;const int tid = threadIdx.x;if (tid == 0) s_bad = 0;__syncthreads();int32_t eid = -1;+ int32_t slot = -1;if (tid < n) {eid = ids[tid];s_ids[tid] = eid;- const int32_t slot = (eid >= 0 && eid < n_expert) ? res[eid] : -1;- if (slot < 0) {- atomicOr(&s_bad, 1);- } else {- s_ptr[tid] = (unsigned long long) (cache_base + (slot_off ? (size_t) slot_off[slot] : (size_t) slot * (size_t) blob));- }+ slot = (eid >= 0 && eid < n_expert) ? res[eid] : -1;+ if (slot < 0) atomicOr(&s_bad, 1);}__syncthreads();if (s_bad) {@@ -1004,9 +1002,21 @@ __global__ void __launch_bounds__(kResidentPlanMax) resident_plan_kernel(const i++count_same;}}- const bool is_first = (first_j == tid);- s_first[tid] = is_first ? 1 : 0;- s_cnt[tid] = is_first ? count_same : 0;+ }+ const bool is_first = (tid < n && first_j == tid && slot >= 0);+ const int my_cnt = is_first ? count_same : 0;+ const int my_pack = (my_cnt << 16) | (is_first ? 1 : 0);+ const int lane = tid & 31;+ const int warp = tid >> 5;+ int pref = my_pack;+#pragma unroll+ for (int d = 1; d < 32; d <<= 1) {+ const int up = __shfl_up_sync(0xffffffffu, pref, d);+ if (lane >= d) pref += up;+ }+ s_excl[tid] = pref - my_pack;+ if (lane == 31) {+ s_wsum[warp] = pref;}__syncthreads();@@ -1018,27 +1028,30 @@ __global__ void __launch_bounds__(kResidentPlanMax) resident_plan_kernel(const iunsigned long long* ptr = (unsigned long long*) (pl + ptr_off);int32_t* start2 = pl + ptr_off + 4 * capx;- if (tid < n && first_j == tid) {- int grp_idx = 0;- int ent_start = 0;- for (int j = 0; j < tid; ++j) {- grp_idx += s_first[j];- ent_start += s_cnt[j];+ if (is_first) {+ int tot = s_excl[tid];+#pragma unroll+ for (int w = 0; w < 4; ++w) {+ if (w < warp) tot += s_wsum[w];}- ptr[grp_idx] = s_ptr[tid];+ const int grp_idx = tot & 0xffff;+ const int ent_start = tot >> 16;+ ptr[grp_idx] = (unsigned long long) (cache_base + (slot_off ? (size_t) slot_off[slot] : (size_t) slot * (size_t) blob));start[grp_idx] = ent_start;- s_gstart[tid] = ent_start;}- __syncthreads();-if (tid < n) {- const int out_idx = s_gstart[first_j] + rank_in_group;+ const int fj_warp = first_j >> 5;+ int fj_tot = s_excl[first_j];+#pragma unroll+ for (int w = 0; w < 4; ++w) {+ if (w < fj_warp) fj_tot += s_wsum[w];+ }+ const int out_idx = (fj_tot >> 16) + rank_in_group;dst[out_idx] = tid;tok[out_idx] = tid / k;}if (tid == 0) {- int groups = 0;- for (int j = 0; j < n; ++j) groups += s_first[j];+ const int groups = (s_wsum[0] + s_wsum[1] + s_wsum[2] + s_wsum[3]) & 0xffff;start[groups] = n;start2[0] = n;counts[0] = groups;diff --git a/src/kernels/resident_plan_parity.cpp b/src/kernels/resident_plan_parity.cppnew file mode 100644index 000000000..881ab5ce0--- /dev/null+++ b/src/kernels/resident_plan_parity.cpp@@ -0,0 +1,151 @@+// src/kernels/resident_plan_parity.cpp - the verify window's device-built expert plan (#783 PR-e: the parallel+// grouping and the prefix scan that replaced thread 0's serial pass) against a host replay of its contract:+// distinct experts in routing order, each with its entries ascending, `ptr[g]` = the expert's slot, counts / start /+// dst / tok / start2 in the host pool's layout.+//+// n = 1..80 entries (a window of up to kVerifyMaxT tokens x 10 experts), ids drawn from pools of 3..512 experts so+// duplicates run from "every entry the same expert" to "all distinct", with and without a slot table (`slot_off`).+// Both modes are run: with a `skip` word (ring when the plan was built, 0 when an expert was not resident - and+// then the plan is untouched) and without one (the all-resident graph: a non-resident expert gives an empty plan and+// sets *plan_err).+// Every word of the plan the host pool would read is compared with memcmp. GPU, synthetic, no model.+#include "strata/kernels/verify_kernels.hpp"++#include <cuda_runtime.h>++#include <cstdint>+#include <cstdio>+#include <cstdlib>+#include <cstring>+#include <random>+#include <vector>++namespace k = strata::kernels;++namespace {+void ck(cudaError_t e, const char* w) {+ if (e != cudaSuccess) {+ std::fprintf(stderr, "%s: %s\n", w, cudaGetErrorString(e));+ std::exit(2);+ }+}+} // namespace++int main() {+ const int K = 10, NE = 512;+ const long long capx = (long long) k::kVerifyMaxT * K;+ const long long ptr_off = ((4 + (capx + 1) + 2 * capx) + 1) & ~1ll;+ const size_t plan_words = (size_t) (ptr_off + 4 * capx + capx + 16);+ std::mt19937 rng(783);+ const long long blob = 1337;+ uint8_t* const cache_base = reinterpret_cast<uint8_t*>(uintptr_t(0x40000000)); // never dereferenced++ int32_t *d_ids, *d_res, *d_plan;+ unsigned long long* d_off;+ uint32_t* d_skip;+ ck(cudaMalloc(&d_ids, 128 * 4), "ids");+ ck(cudaMalloc(&d_res, NE * 4), "res");+ ck(cudaMalloc(&d_off, 1024 * 8), "off");+ ck(cudaMalloc(&d_plan, plan_words * 4), "plan");+ ck(cudaMalloc(&d_skip, 4), "skip");+ uint32_t* d_err = nullptr;+ ck(cudaMalloc(&d_err, 4), "err");++ std::vector<int32_t> res(NE);+ for (int e = 0; e < NE; ++e) res[e] = (e * 37 + 11) % 1000; // distinct slots, not the identity+ std::vector<unsigned long long> off(1024);+ for (int s = 0; s < 1024; ++s) off[s] = (unsigned long long) s * 4096 + (s % 7) * 64;+ ck(cudaMemcpy(d_res, res.data(), NE * 4, cudaMemcpyHostToDevice), "res");+ ck(cudaMemcpy(d_off, off.data(), 1024 * 8, cudaMemcpyHostToDevice), "off");++ int cases = 0, bad = 0;+ for (int n = 1; n <= 80; ++n) {+ for (int variant = 0; variant < 4; ++variant) {+ const int pool = variant == 0 ? 3 : (variant == 1 ? 12 : (variant == 2 ? 40 : 512));+ std::vector<int32_t> ids(n);+ for (int i = 0; i < n; ++i) ids[i] = (int32_t) ((rng() % pool) * (512 / pool));+ const bool use_off = (n + variant) % 2 == 1;+ ck(cudaMemcpy(d_ids, ids.data(), n * 4, cudaMemcpyHostToDevice), "ids");++ // host replay+ std::vector<int32_t> counts = {0, n, 0}, start, dst(n), tok(n);+ std::vector<unsigned long long> ptr;+ std::vector<int> first_seen; // group -> expert+ for (int i = 0; i < n; ++i) {+ int g = -1;+ for (size_t q = 0; q < first_seen.size(); ++q)+ if (first_seen[q] == ids[i]) g = (int) q;+ if (g < 0) { first_seen.push_back(ids[i]); g = (int) first_seen.size() - 1; }+ }+ const int groups = (int) first_seen.size();+ counts[0] = groups;+ start.assign(groups + 1, 0);+ int pos = 0;+ for (int g = 0; g < groups; ++g) {+ start[g] = pos;+ ptr.push_back((unsigned long long) (uintptr_t) cache_base ++ (use_off ? off[res[first_seen[g]]] : (unsigned long long) res[first_seen[g]] * (unsigned long long) blob));+ for (int i = 0; i < n; ++i)+ if (ids[i] == first_seen[g]) { dst[pos] = i; tok[pos] = i / K; ++pos; }+ }+ start[groups] = n;++ for (int mode = 0; mode < 2; ++mode) { // 0: general kernel with a skip word, 1: all resident+ ck(cudaMemset(d_plan, 0xAB, plan_words * 4), "fill");+ ck(cudaMemset(d_skip, 0, 4), "skip0");+ ck(cudaMemset(d_err, 0, 4), "err0");+ k::resident_plan(d_ids, n, K, d_res, NE, cache_base, use_off ? d_off : nullptr, blob, d_plan, capx,+ mode == 0 ? d_skip : nullptr, 7u, nullptr, d_err);+ ck(cudaDeviceSynchronize(), "sync");+ std::vector<int32_t> plan(plan_words);+ ck(cudaMemcpy(plan.data(), d_plan, plan_words * 4, cudaMemcpyDeviceToHost), "plan");+ uint32_t skip = 0;+ ck(cudaMemcpy(&skip, d_skip, 4, cudaMemcpyDeviceToHost), "skipr");+ bool ok = mode == 1 || skip == 7u;+ ok = ok && std::memcmp(plan.data(), counts.data(), 3 * 4) == 0;+ ok = ok && std::memcmp(plan.data() + 4, start.data(), (size_t) (groups + 1) * 4) == 0;+ const int32_t* pdst = plan.data() + 4 + capx + 1;+ ok = ok && std::memcmp(pdst, dst.data(), (size_t) n * 4) == 0;+ ok = ok && std::memcmp(pdst + capx, tok.data(), (size_t) n * 4) == 0;+ ok = ok && std::memcmp(plan.data() + ptr_off, ptr.data(), (size_t) groups * 8) == 0;+ ok = ok && plan[(size_t) (ptr_off + 4 * capx)] == n;+ ++cases;+ if (!ok) {+ std::printf(" *** n=%d pool=%d %s %s: the plan differs from the host replay ***\n", n, pool,+ use_off ? "slot_off" : "blob", mode ? "all_resident" : "general");+ ++bad;+ }+ }+ }+ }++ // a routed expert that is not resident: the general kernel leaves the plan alone and zeroes *skip+ {+ std::vector<int32_t> res2 = res;+ res2[5] = -1;+ ck(cudaMemcpy(d_res, res2.data(), NE * 4, cudaMemcpyHostToDevice), "res2");+ const int32_t ids[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};+ ck(cudaMemcpy(d_ids, ids, sizeof ids, cudaMemcpyHostToDevice), "ids");+ ck(cudaMemset(d_plan, 0xAB, plan_words * 4), "fill");+ const uint32_t seven = 7;+ ck(cudaMemcpy(d_skip, &seven, 4, cudaMemcpyHostToDevice), "skip7");+ k::resident_plan(d_ids, 10, K, d_res, NE, cache_base, nullptr, blob, d_plan, capx, d_skip, 7u, nullptr, d_err);+ ck(cudaDeviceSynchronize(), "sync");+ std::vector<int32_t> plan(plan_words);+ ck(cudaMemcpy(plan.data(), d_plan, plan_words * 4, cudaMemcpyDeviceToHost), "plan");+ uint32_t skip = 99;+ ck(cudaMemcpy(&skip, d_skip, 4, cudaMemcpyDeviceToHost), "skipr");+ bool untouched = true;+ for (int32_t w : plan) untouched = untouched && (uint32_t) w == 0xABABABABu;+ ++cases;+ if (skip != 0 || !untouched) {+ std::printf(" *** a non-resident expert: skip=%u untouched=%d (want 0 / 1) ***\n", skip, (int) untouched);+ ++bad;+ }+ }++ std::printf("resident_plan: %d cases, %d failures\n", cases, bad);+ if (bad) return 1;+ std::printf("resident_plan_parity OK\n");+ return 0;+}