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++
F7: the head's argmax and the draft's top probability over many blocks a row (fork fbb8bfa)
Niko12213e4aa1d6189a
diff --git a/CMakeLists.txt b/CMakeLists.txtindex 98a5df2a8..9728d8f9e 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -730,6 +730,13 @@ if(STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP)target_include_directories(shared_expert_parity PRIVATE ${_strata_gpu_include_directories})target_link_libraries(shared_expert_parity PRIVATE strata_kernels ${_strata_gpu_runtime_target})add_test(NAME shared_expert_parity COMMAND shared_expert_parity --selftest)+ # the split / batched verify-window kernels (from eddoursul's fork), bitwise vs the kernels they replace; CUDA only+ if(STRATA_ENABLE_CUDA)+ add_executable(verify_parity src/kernels/verify_parity.cpp)+ target_include_directories(verify_parity PRIVATE ${_strata_gpu_include_directories})+ target_link_libraries(verify_parity PRIVATE strata_kernels ${_strata_gpu_runtime_target})+ add_test(NAME verify_parity COMMAND verify_parity --selftest)+ endif()add_executable(fused_gr_bench src/kernels/fused_gr_bench.cpp)target_include_directories(fused_gr_bench PRIVATE ${_strata_gpu_include_directories})diff --git a/include/strata/core/mtp.hpp b/include/strata/core/mtp.hppindex e4b1d6223..019268d8d 100644--- a/include/strata/core/mtp.hpp+++ b/include/strata/core/mtp.hpp@@ -237,6 +237,8 @@ private:int64_t window_ = 0; // attention over the last window_ cells (0 = every cell)int64_t prompt_len_ = 0;float* probs_ = nullptr;+ uint8_t* arg_scratch_ = nullptr; ///< argmax_rows' and row_top_prob_split's partials and counters+ uint8_t* top_scratch_ = nullptr;// deviceint32_t *tok_ = nullptr, *step_ = nullptr, *pos_ = nullptr, *row_ = nullptr, *ident_ = nullptr;float *Rin_ = nullptr, *R_ = nullptr, *emb_ = nullptr, *en_ = nullptr, *e2_ = nullptr, *hn_ = nullptr, *h2_ = nullptr;diff --git a/include/strata/core/verify.hpp b/include/strata/core/verify.hppindex 119f6535e..aa321550d 100644--- a/include/strata/core/verify.hpp+++ b/include/strata/core/verify.hpp@@ -436,6 +436,7 @@ private:void* hit_scratch_ = nullptr;float *head_mixed_ = nullptr, *head_inj_ = nullptr, *head_logits_ = nullptr;uint16_t* sh_bf16_ = nullptr;+ uint8_t* arg_scratch_ = nullptr; ///< argmax_rows' partials and countersfloat *sh_gate_ = nullptr, *sh_up_ = nullptr, *sh_g_ = nullptr;float* hist_snap_ = nullptr; // T * NG_HIST * NG_HC_DIMint64_t cap_ = 0, max_blocks_ = 0, attn_scratch_floats_ = 0;diff --git a/include/strata/kernels/verify_kernels.hpp b/include/strata/kernels/verify_kernels.hppindex 0ae553413..f7a6070bb 100644--- a/include/strata/kernels/verify_kernels.hpp+++ b/include/strata/kernels/verify_kernels.hpp@@ -96,6 +96,23 @@ void gather_rows(const uint8_t* src, int64_t row_bytes, const int32_t* ids, int6void map_ids(int32_t* ids, const int32_t* table, int n, void* stream);/// probs[t] = softmax(logits[t])[ids[t]] for n_rows rows of n_vocab (the probability of each row's argmax).void row_top_prob(const float* logits, int n_rows, int n_vocab, const int32_t* ids, float* probs, void* stream);+/// out[t] = the argmax of row t (n floats a row; ties to the lowest index, NaN never picked, 0 when no value is above+/// -inf): bitwise sample_tokens' greedy pick. Up to 128 blocks a row scan slices of it, the row's last block merges+/// their picks. `scratch`: argmax_rows_scratch_bytes(n_rows), zero before the first launch (each launch leaves it+/// so). Graph-capturable.+uint64_t argmax_rows_scratch_bytes(int n_rows);+void argmax_rows(const float* logits, int n_rows, int n, void* scratch, int32_t* out, void* stream);+/// row_top_prob over 8 blocks a row, each computing 4 of its 32 warps' sums, the row's last block adding the 32 in+/// order: bitwise row_top_prob. `scratch`: row_top_prob_scratch_bytes(n_rows), zero before the first launch.+uint64_t row_top_prob_scratch_bytes(int n_rows);+void row_top_prob_split(const float* logits, int n_rows, int n_vocab, const int32_t* ids, float* probs, void* scratch,+ void* stream);+/// True where row_top_prob_split runs as its own kernel (CUDA; AMD keeps the one-block row_top_prob, so it calls+/// that). STRATA_MULTI_BLOCK_ARGMAX=0: off.+bool multi_block_head_ops();+/// True where the verify head's and the draft's greedy pick should be argmax_rows: sm_80 to sm_89 (the one-block+/// kernel is slow there; sm_90+ has sample_tokens' cluster kernel, other cards keep sample_tokens).+bool argmax_rows_wanted();/// Dense-attention step records for `n` cells: [cell, cell+1, (cell+1)/4, cell+1] from cells[i] (device memory).void dense_steps(const int32_t* cells, int n, int32_t* steps, void* stream);/// A sliding attention window: for `n` step records (kStepCount ints each, n_kv at [1]) the selection becomes thediff --git a/src/core/mtp.cpp b/src/core/mtp.cppindex d6a47fb11..77a01cb9f 100644--- a/src/core/mtp.cpp+++ b/src/core/mtp.cpp@@ -382,6 +382,8 @@ bool MtpDrafter::load(const std::string& rt_dir, const ModelGeometry& g, Sessionx_bf16_ = b.take<uint16_t>(N);out_ids_ = b.take<int32_t>(T + 4);probs_ = b.take<float>(T + 4);+ arg_scratch_ = b.take<uint8_t>(strata::kernels::argmax_rows_scratch_bytes((int) T));+ top_scratch_ = b.take<uint8_t>(strata::kernels::row_top_prob_scratch_bytes((int) T));dummy_inj_ = b.take<float>(HC);};Bump count;@@ -814,11 +816,16 @@ bool MtpDrafter::record_forward(int T, int step_row0, cudaStream_t cs, std::stricparams_, cring_, kCoupledHistCap, coupled_j_, step, cscratch_, out_ids_, probs_, cs);return true;}- SamplerParams sp;- sp.greedy = true;- sp.temperature = 0.0f;- sample_tokens(head_logits_, T, (int) nv, nullptr, 0, sp, out_ids_, cs);- row_top_prob(head_logits_, T, (int) nv, out_ids_, probs_, cs);+ if (argmax_rows_wanted()) {+ argmax_rows(head_logits_, T, (int) nv, arg_scratch_, out_ids_, cs);+ } else {+ SamplerParams sp;+ sp.greedy = true;+ sp.temperature = 0.0f;+ sample_tokens(head_logits_, T, (int) nv, nullptr, 0, sp, out_ids_, cs);+ }+ if (multi_block_head_ops()) row_top_prob_split(head_logits_, T, (int) nv, out_ids_, probs_, top_scratch_, cs);+ else row_top_prob(head_logits_, T, (int) nv, out_ids_, probs_, cs);if (sub) map_ids(out_ids_, dvocab_, T, cs);} catch (const std::exception& e) {err = std::string("mtp: ") + e.what();diff --git a/src/core/verify.cpp b/src/core/verify.cppindex 2b9dc7323..e1fa915d7 100644--- a/src/core/verify.cpp+++ b/src/core/verify.cpp@@ -463,6 +463,7 @@ bool Verifier::init(const WeightTable& wt, const ModelGeometry& g, SessionState&sh_bf16_ = b.take<uint16_t>(T * N); sh_gate_ = b.take<float>(T * (uint64_t) g.n_ff);sh_up_ = b.take<float>(T * (uint64_t) g.n_ff); sh_g_ = b.take<float>(T + 4);head_logits_ = b.take<float>(T * (uint64_t) n_vocab_);+ arg_scratch_ = b.take<uint8_t>(strata::kernels::argmax_rows_scratch_bytes((int) T));hist_snap_ = b.take<float>(T * HS);ple_key_ = b.take<float>(T * (uint64_t) strata::kernels::NG_HC_DIM); ple_val_ = b.take<float>(T * N);};@@ -1297,10 +1298,14 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {// Greedy, the default, is recorded here as before (no extra launch or sync per window). A request that// samples or penalizes is sampled again host-side after the replay (run()) with its own parameters and a// fresh draw counter: a captured sampler would bake them in and replay the same draws forever.- SamplerParams sp;- sp.greedy = true;- sp.temperature = 0.0f;- sample_tokens(head_logits_, T, (int) n_vocab_, nullptr, 0, sp, m_out_, cs);+ if (argmax_rows_wanted()) { // sm_80 to sm_89: the pick over many blocks a row (bitwise the one-block kernel's)+ argmax_rows(head_logits_, T, (int) n_vocab_, arg_scratch_, m_out_, cs);+ } else {+ SamplerParams sp;+ sp.greedy = true;+ sp.temperature = 0.0f;+ sample_tokens(head_logits_, T, (int) n_vocab_, nullptr, 0, sp, m_out_, cs);+ }}stamp(g.n_layers, 1, 0);return true;diff --git a/src/kernels/cuda/verify_kernels.cu b/src/kernels/cuda/verify_kernels.cuindex e4f0e31a5..dc4e1c691 100644--- a/src/kernels/cuda/verify_kernels.cu+++ b/src/kernels/cuda/verify_kernels.cu@@ -8,6 +8,7 @@#include <cuda_fp16.h>#include <cuda_runtime.h>+#include <algorithm>#include <cstdio>#include <cstdlib>@@ -622,6 +623,182 @@ void row_top_prob(const float* logits, int n_rows, int n_vocab, const int32_t* icheck("row_top_prob");}+#if !defined(__HIPCC__) // warp-32 kernels: an AMD card keeps the one-block row_top_prob and sampler_greedy_kernel+namespace {++constexpr int kArgMaxBlocks = 128, kArgThreads = 256, kTopBlocks = 8;++// the larger value, on equality the lower index: sampler_greedy_kernel's order+__device__ __forceinline__ void arg_take(float ov, int oi, float& bv, int& bi) {+ if (ov > bv || (ov == bv && oi < bi)) { bv = ov; bi = oi; }+}++// a block's (value, index) pick, in thread 0; `none` is the index of no candidate+__device__ __forceinline__ void arg_block(float& bv, int& bi, int none) {+ __shared__ float sv[32];+ __shared__ int si[32];+ for (int off = 16; off > 0; off >>= 1) {+ const float ov = __shfl_down_sync(0xffffffffu, bv, off);+ const int oi = __shfl_down_sync(0xffffffffu, bi, off);+ arg_take(ov, oi, bv, bi);+ }+ const int warp = (int) (threadIdx.x >> 5), lane = (int) (threadIdx.x & 31);+ if (lane == 0) { sv[warp] = bv; si[warp] = bi; }+ __syncthreads();+ if (warp == 0) {+ const int nw = (int) (blockDim.x >> 5);+ bv = lane < nw ? sv[lane] : __int_as_float(0xff800000);+ bi = lane < nw ? si[lane] : none;+ for (int off = 16; off > 0; off >>= 1) {+ const float ov = __shfl_down_sync(0xffffffffu, bv, off);+ const int oi = __shfl_down_sync(0xffffffffu, bi, off);+ arg_take(ov, oi, bv, bi);+ }+ }+}++// A row's scratch, whatever the launch's row count (one scratch serves windows of every size): its counter, then+// the blocks' values and indices.+struct ArgRow {+ unsigned counter;+ unsigned pad[3];+ float v[kArgMaxBlocks];+ int i[kArgMaxBlocks];+};++// grid (blocks, rows): block b scans [b * per_block, +per_block) of its row+__global__ void argmax_rows_kernel(const float* __restrict__ logits, int n, int per_block, ArgRow* __restrict__ rows,+ int32_t* out) {+ const int row = (int) blockIdx.y, b = (int) blockIdx.x, nb = (int) gridDim.x;+ unsigned* counter = &rows[row].counter;+ float* pv = rows[row].v;+ int* pi = rows[row].i;+ const float* l = logits + (size_t) row * n;+ const int lo = b * per_block, hi = min(n, lo + per_block);+ float bv = __int_as_float(0xff800000); // -inf+ int bi = n;+ for (int v = lo + (int) threadIdx.x; v < hi; v += (int) blockDim.x) {+ const float s = l[v];+ if (s > bv) { bv = s; bi = v; }+ }+ arg_block(bv, bi, n);+ __shared__ bool last;+ if (threadIdx.x == 0) {+ pv[b] = bv;+ pi[b] = bi;+ __threadfence();+ last = atomicAdd(counter, 1u) == (unsigned) (nb - 1);+ }+ __syncthreads();+ if (!last) return;+ __threadfence();+ bv = __int_as_float(0xff800000);+ bi = n;+ if ((int) threadIdx.x < nb) {+ bv = ((volatile const float*) pv)[threadIdx.x];+ bi = ((volatile const int*) pi)[threadIdx.x];+ }+ arg_block(bv, bi, n);+ if (threadIdx.x == 0) {+ out[row] = bi < n ? bi : 0; // no value above -inf: 0, as the one-block kernel+ *counter = 0;+ }+}++// a row's top-probability scratch: its counter, then its 32 warps' sums+struct TopRow {+ unsigned counter;+ unsigned pad[3];+ float parts[32];+};++// grid (kTopBlocks, rows), 1024 / kTopBlocks threads: thread j of block b is row_top_prob's thread b * blockDim + j+__global__ void row_top_prob_split_kernel(const float* __restrict__ logits, int n_vocab, const int32_t* __restrict__ ids,+ float* __restrict__ probs, TopRow* __restrict__ rows) {+ const int row = (int) blockIdx.y;+ float* parts = rows[row].parts;+ const float* l = logits + (size_t) row * n_vocab;+ const float m = l[ids[row]];+ const int vt = (int) (blockIdx.x * blockDim.x + threadIdx.x);+ float s = 0.0f;+ for (int i = vt; i < n_vocab; i += 1024) s += __expf(l[i] - m);+ for (int o = 16; o > 0; o >>= 1) s += __shfl_xor_sync(0xffffffffu, s, o);+ if ((threadIdx.x & 31) == 0) parts[vt >> 5] = s;+ __threadfence();+ __syncthreads();+ __shared__ bool last;+ if (threadIdx.x == 0) last = atomicAdd(&rows[row].counter, 1u) == (unsigned) (gridDim.x - 1);+ __syncthreads();+ if (!last || threadIdx.x != 0) return;+ __threadfence();+ float tot = 0.0f;+ for (int w = 0; w < 32; ++w) tot += ((volatile const float*) parts)[w];+ probs[row] = 1.0f / tot;+ rows[row].counter = 0;+}++} // namespace++uint64_t argmax_rows_scratch_bytes(int n_rows) { return (uint64_t) n_rows * sizeof(ArgRow); }++void argmax_rows(const float* logits, int n_rows, int n, void* scratch, int32_t* out, void* stream) {+ if (n_rows <= 0) return;+ const int nb = std::min(kArgMaxBlocks, std::max(1, (n + 4095) / 4096));+ const int per_block = (n + nb - 1) / nb;+ argmax_rows_kernel<<<dim3((unsigned) nb, (unsigned) n_rows), kArgThreads, 0, (cudaStream_t) stream>>>(+ logits, n, per_block, (ArgRow*) scratch, out);+ check("argmax_rows");+}++uint64_t row_top_prob_scratch_bytes(int n_rows) { return (uint64_t) n_rows * sizeof(TopRow); }++void row_top_prob_split(const float* logits, int n_rows, int n_vocab, const int32_t* ids, float* probs, void* scratch,+ void* stream) {+ if (n_rows <= 0) return;+ row_top_prob_split_kernel<<<dim3(kTopBlocks, (unsigned) n_rows), 1024 / kTopBlocks, 0, (cudaStream_t) stream>>>(+ logits, n_vocab, ids, probs, (TopRow*) scratch);+ check("row_top_prob_split");+}+#else+uint64_t argmax_rows_scratch_bytes(int) { return 0; }+void argmax_rows(const float*, int, int, void*, int32_t*, void*) {+ std::fprintf(stderr, "argmax_rows: not built for this backend\n");+ std::exit(1);+}+uint64_t row_top_prob_scratch_bytes(int) { return 0; }+void row_top_prob_split(const float* logits, int n_rows, int n_vocab, const int32_t* ids, float* probs, void*,+ void* stream) {+ row_top_prob(logits, n_rows, n_vocab, ids, probs, stream);+}+#endif++bool multi_block_head_ops() {+#if defined(__HIPCC__)+ return false;+#else+ static const bool on = [] {+ const char* v = std::getenv("STRATA_MULTI_BLOCK_ARGMAX");+ return v == nullptr || std::atoi(v) != 0;+ }();+ return on;+#endif+}++bool argmax_rows_wanted() {+#if defined(__HIPCC__)+ return false;+#else+ // sm_80 to sm_89: sample_tokens' greedy pick there is the one-block kernel (sm_90+ has the cluster kernel)+ static const bool want = [] {+ int dev = 0, major = 0;+ if (cudaGetDevice(&dev) != cudaSuccess || cudaDeviceGetAttribute(&major, cudaDevAttrComputeCapabilityMajor, dev) != cudaSuccess)+ return false;+ return major == 8;+ }();+ return want && multi_block_head_ops();+#endif+}+namespace {__global__ void window_ids_kernel(int32_t* steps, int window, int32_t* ids, long long stride) {const int q = blockIdx.y;diff --git a/src/kernels/verify_parity.cpp b/src/kernels/verify_parity.cppnew file mode 100644index 000000000..26ff7e02d--- /dev/null+++ b/src/kernels/verify_parity.cpp@@ -0,0 +1,127 @@+// src/kernels/verify_parity.cpp - batched / split verify-window kernels against the kernels they replace, BITWISE.+//+// A verify window's token t must come out bit for bit as it would in a window of any other size (the drafts are+// accepted exactly when greedy decode would have produced them), so every kernel here is checked against the+// one it stands in for, on random inputs that include -0.0, denormals, NaN, -inf and large values. The tests+// follow eddoursul's fork (MIT; verify_parity.cpp), ported to this tree's kernels.+#include "strata/kernels/sampler.hpp"+#include "strata/kernels/verify_kernels.hpp"++#include <cuda_runtime.h>++#include <algorithm>+#include <cmath>+#include <cstdio>+#include <cstdlib>+#include <cstring>+#include <random>+#include <string>+#include <vector>++#include <cstdlib>+#include <cstring>+#include <random>+#include <string>+#include <vector>++namespace {++void check(cudaError_t e, const char* what) {+ if (e != cudaSuccess) {+ std::fprintf(stderr, "%s: %s\n", what, cudaGetErrorString(e));+ std::exit(1);+ }+}++template <typename T> T* dev(size_t n) {+ T* p = nullptr;+ check(cudaMalloc(&p, n * sizeof(T)), "cudaMalloc");+ return p;+}+template <typename T> void up(T* d, const std::vector<T>& h) {+ check(cudaMemcpy(d, h.data(), h.size() * sizeof(T), cudaMemcpyHostToDevice), "upload");+}+template <typename T> std::vector<T> down(const T* d, size_t n) {+ std::vector<T> h(n);+ check(cudaMemcpy(h.data(), d, n * sizeof(T), cudaMemcpyDeviceToHost), "download");+ return h;+}++// argmax_rows against sample_tokens' greedy pick and row_top_prob_split against row_top_prob, bitwise: rows of both+// vocabularies with ties at the maximum (the lowest index wins), NaN, -inf, a row with no value above -inf, and+// launches repeated on one scratch (each must leave its counters at zero)+int test_argmax(std::mt19937& rng, cudaStream_t s) {+ using namespace strata::kernels;+ int bad = 0;+ std::normal_distribution<float> nd(0.0f, 4.0f);+ uint8_t* sa = dev<uint8_t>(argmax_rows_scratch_bytes(8)); // one scratch for every row count, as in the engine+ uint8_t* st = dev<uint8_t>(row_top_prob_scratch_bytes(8));+ check(cudaMemset(sa, 0, argmax_rows_scratch_bytes(8)), "memset");+ check(cudaMemset(st, 0, row_top_prob_scratch_bytes(8)), "memset");+ for (const int n : {248320, 40525, 4097, 33}) {+ for (const int rows : {4, 8, 1, 3, 8, 2}) {+ std::vector<float> h((size_t) rows * n);+ for (float& v : h) v = nd(rng);+ for (int r = 0; r < rows; ++r) {+ float* row = h.data() + (size_t) r * n;+ const int a = (int) (rng() % n), b = (int) (rng() % n);+ row[a] = row[b] = 40.0f; // a tie at the maximum+ row[(int) (rng() % n)] = std::nanf(""); // never picked+ row[(int) (rng() % n)] = -INFINITY;+ if (r == 2) // no value above -inf: 0+ for (int i = 0; i < n; ++i) row[i] = i % 7 ? -INFINITY : std::nanf("");+ }+ float* d_l = dev<float>(h.size());+ up(d_l, h);+ int* d_ref = dev<int>(rows);+ int32_t* d_out = dev<int32_t>(rows);+ float* d_p1 = dev<float>(rows);+ float* d_p2 = dev<float>(rows);+ SamplerParams sp;+ sp.greedy = true;+ sp.temperature = 0.0f;+ sample_tokens(d_l, rows, n, nullptr, 0, sp, d_ref, s);+ row_top_prob(d_l, rows, n, d_ref, d_p1, s);+ for (int rep = 0; rep < 3; ++rep) {+ argmax_rows(d_l, rows, n, sa, d_out, s);+ row_top_prob_split(d_l, rows, n, d_out, d_p2, st, s);+ check(cudaStreamSynchronize(s), "argmax");+ const std::vector<int> ref = down(d_ref, rows);+ const std::vector<int32_t> out = down(d_out, rows);+ const std::vector<float> p1 = down(d_p1, rows), p2 = down(d_p2, rows);+ for (int r = 0; r < rows; ++r)+ if (ref[r] != out[r] || std::memcmp(&p1[r], &p2[r], 4) != 0) {+ if (bad < 5)+ std::printf(" argmax n %d rows %d row %d: %d / %d, p %.9g / %.9g\n", n, rows, r, ref[r],+ out[r], p1[r], p2[r]);+ ++bad;+ }+ }+ cudaFree(d_l); cudaFree(d_ref); cudaFree(d_out); cudaFree(d_p1); cudaFree(d_p2);+ }+ }+ cudaFree(sa);+ cudaFree(st);+ std::printf("argmax_rows, row_top_prob_split: %s\n",+ bad ? "MISMATCH" : "bitwise equal to the one-block kernels (ties, NaN, -inf, 1-8 rows on one scratch)");+ return bad;+}++} // namespace++int main(int argc, char** argv) {+ for (int i = 1; i < argc; ++i) {+ if (std::string(argv[i]) != "--selftest") {+ std::fprintf(stderr, "usage: verify_parity [--selftest]\n");+ return 2;+ }+ }+ std::mt19937 rng(20260925);+ cudaStream_t s = nullptr;+ check(cudaStreamCreate(&s), "stream");+ int bad = 0;+ bad += test_argmax(rng, s);+ cudaStreamDestroy(s);+ std::printf("verify_parity: %s\n", bad ? "FAIL" : "PASS");+ return bad ? 1 : 0;+}