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++
prefill: the GDN recurrence per head, a lane pair per column, the output norm fused (same bits)
Niko12219a427232831f
diff --git a/CMakeLists.txt b/CMakeLists.txtindex 1ce54a601..c64cf4d15 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -455,6 +455,11 @@ if(STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP)add_test(NAME hip_prefill_native_batch COMMAND hip_prefill_native_batch)list(APPEND _strata_hip_tests hip_prefill_native_batch)endif()+ # Aurora: the prompt GDN recurrence's per-head kernel against the column-split + norm kernels, bitwise+ add_executable(hip_gdn_rec_head tests/hip/gdn_rec_head.cpp)+ target_link_libraries(hip_gdn_rec_head PRIVATE strata_prefill strata_hip_runtime)+ add_test(NAME hip_gdn_rec_head COMMAND hip_gdn_rec_head)+ list(APPEND _strata_hip_tests hip_gdn_rec_head)# S6: the AMD fast router against the portable kernel, bitwiseadd_executable(hip_router_fast tests/hip/router_fast.cpp)target_link_libraries(hip_router_fast PRIVATE strata_kernels strata_hip_runtime)diff --git a/include/strata/prefill/kernels.hpp b/include/strata/prefill/kernels.hppindex 8f6145bf5..16e24697a 100644--- a/include/strata/prefill/kernels.hpp+++ b/include/strata/prefill/kernels.hpp@@ -47,6 +47,12 @@ void gdn_conv(float* history, const float* qkv, const float* conv_w, float* h, i/// (FP32 and FP16 bits: the out projection is quantized).void gdn_recurrence(float* state, const float* h, const float* gate, const float* beta, const float* z,const float* gamma, float eps, float* y, uint16_t* y16, int64_t T, void* stream);+/// The kernels behind gdn_recurrence, for the parity test: 0 = the column-split kernels + the norm kernel (or the+/// one-block-per-head kernel under STRATA_GDN_REC_HEADS), 1 = the per-head kernel with the norm fused, 2 = the+/// one-block-per-head kernel (its fused norm rounds differently). 0 and 1 give the same bits.+void gdn_recurrence_variant(int variant, float* state, const float* h, const float* gate, const float* beta,+ const float* z, const float* gamma, float eps, float* y, uint16_t* y16, int64_t T,+ void* stream);// ---- MoE/// softmax over 512, top-10 (ties to the lower id), weights renormalised over the ten (the native router).diff --git a/src/prefill/kernels.cu b/src/prefill/kernels.cuindex de833442e..3f5d6ecdb 100644--- a/src/prefill/kernels.cu+++ b/src/prefill/kernels.cu@@ -433,6 +433,152 @@ __global__ void __launch_bounds__(CB * RG) gdn_rec_cols_pipe_kernel(float* __res#pragma unrollfor (int r = 0; r < RPG; ++r) base[r * rs] = s[r];}+// Aurora (S23): the recurrence with ONE block per value head, a lane pair per value column (the two halves of the+// column's 128 state rows in the two lanes' registers, lanes l and l + 16 of one wave), and the output norm fused in.+// The same arithmetic in the same order as gdn_rec_cols_pipe_kernel + gdn_out_norm_kernel - the kv / o partials of+// the four 32-row groups are kept apart and added as red[0] + red[1] + red[2] + red[3] (the two lanes swap theirs+// within the wave); the norm's warp sums run over the same 32-column warps - so the same bits+// (tests/hip/gdn_rec_head.cpp). What changes is the schedule: no __syncthreads per token, one pair every HT tokens+// (the chunk's q/k rows staged in LDS, the next chunk's loaded into registers while this one computes), instead of+// four per token. STRATA_GDN_HEAD=0: the old kernels.+constexpr int HT = 8; // tokens per staged chunk+constexpr int HTH = 2 * S; // threads per head block+constexpr int HRS = S + 8; // staged q/k row stride: [rows 0..63, 4 pad, rows 64..127, 4 pad]+constexpr int HPF = HT * 2 * S / 4 / HTH; // float4 of q/k each thread prefetches per chunk (4)+__device__ __forceinline__ int hrow(int r) { return r + (r >= S / 2 ? 4 : 0); }+__global__ void __launch_bounds__(HTH) gdn_rec_head_kernel(float* __restrict__ state, const float* __restrict__ h,+ const float* __restrict__ gate, const float* __restrict__ beta,+ const float* __restrict__ z, const float* __restrict__ gamma,+ float eps, float* __restrict__ y, uint16_t* __restrict__ y16,+ int64_t T) {+ __shared__ __align__(16) float sqk[2][HT][2][HRS]; // [buffer][token][q | k][row (padded)]+ __shared__ float sgb[2][2][HT]; // [buffer][gate | beta][token]+ __shared__ float sv[2][HT][S]; // [buffer][token][column] v+ __shared__ float soc[HT][S];+ __shared__ float swsum[HT][4];+ const int head = blockIdx.x, tid = threadIdx.x, lane = tid & 31, wave = tid >> 5;+ const int col = wave * 16 + (lane & 15), half = lane >> 4, r0 = half * (S / 2); // this lane's rows r0 .. r0 + 63+ const int qh = head % HK;+ float s[S / 2];+ {+ const float* base = state + (size_t) head * S + col;+#pragma unroll+ for (int r = 0; r < S / 2; ++r) s[r] = base[(size_t) (r0 + r) * HV * S];+ }+ float4 pf[HPF];+ float pv[HT], pgb = 0.0f;+ // chunk c's inputs into registers: q/k rows (float4 i of the chunk's [token][q|k][row] block), the column's v (the+ // lower lane of the pair), and gate (threads 0..HT-1) / beta (HT..2HT-1) of token `tid % HT`+ auto fetch = [&](int64_t c) {+ const int64_t t0 = c * HT;+#pragma unroll+ for (int u = 0; u < HPF; ++u) {+ const int i = tid + u * HTH;+ const int tt = i / (2 * S / 4), rem = i % (2 * S / 4), part = rem / (S / 4), r4 = rem % (S / 4);+ const int64_t t = t0 + tt;+ pf[u] = t < T ? *reinterpret_cast<const float4*>(h + t * C + part * HK * S + qh * S + r4 * 4)+ : make_float4(0.f, 0.f, 0.f, 0.f);+ }+ if (half == 0) {+#pragma unroll+ for (int tt = 0; tt < HT; ++tt) {+ const int64_t t = t0 + tt;+ pv[tt] = t < T ? h[t * C + 2 * HK * S + head * S + col] : 0.0f;+ }+ }+ if (tid < 2 * HT) {+ const int64_t t = t0 + (tid % HT);+ pgb = t < T ? (tid < HT ? gate : beta)[t * HV + head] : 0.0f;+ }+ };+ const float rs_s = rsqrtf((float) S);+ const int64_t nch = (T + HT - 1) / HT;+ if (nch > 0) fetch(0);+ for (int64_t c = 0; c < nch; ++c) {+ const int b = (int) (c & 1);+ if (half == 0) {+#pragma unroll+ for (int tt = 0; tt < HT; ++tt) sv[b][tt][col] = pv[tt];+ }+#pragma unroll+ for (int u = 0; u < HPF; ++u) {+ const int i = tid + u * HTH;+ const int tt = i / (2 * S / 4), rem = i % (2 * S / 4), part = rem / (S / 4), r4 = rem % (S / 4);+ *reinterpret_cast<float4*>(&sqk[b][tt][part][hrow(r4 * 4)]) = pf[u];+ }+ if (tid < 2 * HT) sgb[b][tid / HT][tid % HT] = pgb;+ __syncthreads(); // chunk c staged (buffer b was last read two chunks ago)+ if (c + 1 < nch) fetch(c + 1);+ const int64_t t0 = c * HT;+ const int nt = (int) (T - t0 < HT ? T - t0 : HT);+ for (int tt = 0; tt < nt; ++tt) {+ const float* sq = &sqk[b][tt][0][hrow(r0)];+ const float* sk = &sqk[b][tt][1][hrow(r0)];+ const float g = __expf(sgb[b][0][tt]);+ float kva, kvb; // this lane's two 32-row groups+ {+ float kv = 0.0f;+#pragma unroll+ for (int r = 0; r < 32; ++r) kv = fmaf(s[r], sk[r], kv);+ kva = kv;+ kv = 0.0f;+#pragma unroll+ for (int r = 32; r < 64; ++r) kv = fmaf(s[r], sk[r], kv);+ kvb = kv;+ }+ asm volatile("" ::: "memory"); // re-read the k row below rather than hold it in VGPRs+ // groups 0, 1 in the lower lane, 2, 3 in the upper: ((g0 + g1) + g2) + g3 in both+ const float oa = __shfl_xor_sync(0xffffffffu, kva, 16), ob = __shfl_xor_sync(0xffffffffu, kvb, 16);+ const float kv_col = half == 0 ? kva + kvb + oa + ob : oa + ob + kva + kvb;+ const float cvt = sv[b][tt][col], cbt = sgb[b][1][tt];+ const float delta = (cvt - g * kv_col) * cbt;+ float opa = 0.0f, opb = 0.0f;+#pragma unroll+ for (int r = 0; r < 32; ++r) {+ s[r] = fmaf(g, s[r], sk[r] * delta);+ opa = fmaf(s[r], sq[r], opa);+ }+#pragma unroll+ for (int r = 32; r < 64; ++r) {+ s[r] = fmaf(g, s[r], sk[r] * delta);+ opb = fmaf(s[r], sq[r], opb);+ }+ const float pa = __shfl_xor_sync(0xffffffffu, opa, 16), pb = __shfl_xor_sync(0xffffffffu, opb, 16);+ if (half == 0) soc[tt][col] = (opa + opb + pa + pb) * rs_s;+ }+ __syncthreads(); // the chunk's outputs+ if (tid < S) { // the norm: thread = column, warps of 32 columns as before+ const int nc = tid, nl = tid & 31, nw = tid >> 5;+ for (int tt = 0; tt < nt; ++tt) {+ const float sp = warp_sum(soc[tt][nc] * soc[tt][nc]);+ if (nl == 0) swsum[tt][nw] = sp;+ }+ }+ __syncthreads();+ if (tid < S) {+ const int nc = tid;+ const float g_col = gamma[nc];+ float cz[HT];+#pragma unroll+ for (int tt = 0; tt < HT; ++tt) cz[tt] = tt < nt ? z[(t0 + tt) * HV * S + head * S + nc] : 0.0f;+#pragma unroll+ for (int tt = 0; tt < HT; ++tt) {+ if (tt >= nt) break;+ const float oc = soc[tt][nc];+ const float ss = swsum[tt][0] + swsum[tt][1] + swsum[tt][2] + swsum[tt][3];+ const float v = oc * rsqrtf(ss / (float) S + eps) * g_col * sigm(cz[tt]);+ const size_t at = (size_t) (t0 + tt) * HV * S + (size_t) head * S + nc;+ y[at] = v;+ y16[at] = hf(v);+ }+ }+ }+ {+ float* base = state + (size_t) head * S + col;+#pragma unroll+ for (int r = 0; r < S / 2; ++r) base[(size_t) (r0 + r) * HV * S] = s[r];+ }+}__global__ void __launch_bounds__(S) gdn_out_norm_kernel(const float* __restrict__ z, const float* __restrict__ gamma,float eps, float* __restrict__ y, uint16_t* __restrict__ y16) {__shared__ float wsum[4];@@ -741,10 +887,17 @@ void gdn_conv(float* history, const float* qkv, const float* conv_w, float* h, igdn_l2_kernel<<<dim3(2 * HK, (unsigned) T), S, 0, (cudaStream_t) stream>>>(h, eps);check("gdn_conv");}-void gdn_recurrence(float* state, const float* h, const float* gate, const float* beta, const float* z,- const float* gamma, float eps, float* y, uint16_t* y16, int64_t T, void* stream) {+void gdn_recurrence_variant(int variant, float* state, const float* h, const float* gate, const float* beta,+ const float* z, const float* gamma, float eps, float* y, uint16_t* y16, int64_t T,+ void* stream) {+ if (variant == 1) {+ if (T > 0)+ gdn_rec_head_kernel<<<HV, HTH, 0, (cudaStream_t) stream>>>(state, h, gate, beta, z, gamma, eps, y, y16, T);+ check("gdn_recurrence (head)");+ return;+ }static const bool serial = std::getenv("STRATA_GDN_REC_HEADS") != nullptr; // the one-block-per-head kernel (A/B)- if (serial || T <= 0) {+ if (serial || T <= 0 || variant == 2) {gdn_rec_kernel<<<HV, dim3(S, RG), 0, (cudaStream_t) stream>>>(state, h, gate, beta, z, gamma, eps, y, y16, T);} else {static const bool pipe = [] { const char* v = std::getenv("STRATA_GDN_PIPELINE"); return v == nullptr || std::atoi(v) != 0; }();@@ -756,6 +909,14 @@ void gdn_recurrence(float* state, const float* h, const float* gate, const float}check("gdn_recurrence");}+void gdn_recurrence(float* state, const float* h, const float* gate, const float* beta, const float* z,+ const float* gamma, float eps, float* y, uint16_t* y16, int64_t T, void* stream) {+ // Aurora (S23): the per-head kernel with the norm fused, the same bits (tests/hip/gdn_rec_head.cpp);+ // STRATA_GDN_HEAD=0 (or STRATA_GDN_REC_HEADS) keeps the earlier kernels+ static const bool head = [] { const char* v = std::getenv("STRATA_GDN_HEAD"); return v == nullptr || std::atoi(v) != 0; }();+ static const bool serial = std::getenv("STRATA_GDN_REC_HEADS") != nullptr;+ gdn_recurrence_variant(head && !serial ? 1 : 0, state, h, gate, beta, z, gamma, eps, y, y16, T, stream);+}void route(const float* logits, int32_t* ids, float* weights, int64_t T, int64_t n_expert, void* stream) {if (n_expert == 512)route_kernel<16><<<(unsigned) ((T + 7) / 8), 256, 0, (cudaStream_t) stream>>>(logits, ids, weights, T);diff --git a/tests/hip/gdn_rec_head.cpp b/tests/hip/gdn_rec_head.cppnew file mode 100644index 000000000..7e19e6fd9--- /dev/null+++ b/tests/hip/gdn_rec_head.cpp@@ -0,0 +1,116 @@+// tests/hip/gdn_rec_head.cpp - the prompt GDN recurrence's per-head kernel (gdn_recurrence_variant 1, the norm fused)+// against the column-split kernels + the norm kernel (variant 0, the default before it):+// y, its FP16 bits and the final state must be BITWISE equal, over chunk lengths around the 8-token staging and a+// long one, from a nonzero state. Synthetic inputs in the model's ranges (unit q/k rows, log-decay gates < 0).+#include "strata/prefill/kernels.hpp"++#include <cuda_runtime.h>++#include <cmath>+#include <cstdint>+#include <cstdio>+#include <cstring>+#include <random>+#include <vector>++namespace {+constexpr int S = 128, HK = 16, HV = 48, C = 10240;+void ck(cudaError_t e, const char* w) {+ if (e != cudaSuccess) { std::fprintf(stderr, "%s: %s\n", w, cudaGetErrorString(e)); std::exit(2); }+}+template <typename T> T* dev(const std::vector<T>& h) {+ T* d = nullptr;+ ck(cudaMalloc(&d, h.size() * sizeof(T)), "malloc");+ ck(cudaMemcpy(d, h.data(), h.size() * sizeof(T), cudaMemcpyHostToDevice), "upload");+ return d;+}+template <typename T> std::vector<T> host(const T* d, size_t n) {+ std::vector<T> h(n);+ ck(cudaMemcpy(h.data(), d, n * sizeof(T), cudaMemcpyDeviceToHost), "download");+ return h;+}+} // namespace++int main() {+ std::mt19937 rng(11);+ std::normal_distribution<float> nd(0.f, 1.f);+ std::uniform_real_distribution<float> ud(0.f, 1.f);+ int failures = 0;+ const int64_t lens[] = {1, 7, 8, 9, 17, 100, 2048, 2051};+ for (const int64_t T : lens) {+ std::vector<float> h((size_t) T * C), gate((size_t) T * HV), beta((size_t) T * HV), z((size_t) T * HV * S),+ gamma(S), state((size_t) S * HV * S);+ for (int64_t t = 0; t < T; ++t) {+ float* ht = h.data() + t * C;+ for (int i = 0; i < C; ++i) ht[i] = nd(rng);+ for (int hh = 0; hh < 2 * HK; ++hh) { // q and k heads: unit rows, as after gdn_conv's L2 norm+ double ss = 0;+ for (int d = 0; d < S; ++d) ss += (double) ht[hh * S + d] * ht[hh * S + d];+ for (int d = 0; d < S; ++d) ht[hh * S + d] = (float) (ht[hh * S + d] / std::sqrt(ss + 1e-6));+ }+ }+ for (auto& x : gate) x = -2.0f * ud(rng);+ for (auto& x : beta) x = ud(rng);+ for (auto& x : z) x = nd(rng);+ for (auto& x : gamma) x = 0.5f + ud(rng);+ for (auto& x : state) x = 0.1f * nd(rng);+ const float* dh = dev(h);+ const float* dg = dev(gate);+ const float* db = dev(beta);+ const float* dz = dev(z);+ const float* dgm = dev(gamma);+ std::vector<float> y_ref, st_ref;+ std::vector<uint16_t> y16_ref;+ for (int variant : {0, 1}) {+ float* dst = dev(state);+ float* dy = nullptr;+ uint16_t* dy16 = nullptr;+ ck(cudaMalloc(&dy, (size_t) T * HV * S * 4), "malloc");+ ck(cudaMalloc(&dy16, (size_t) T * HV * S * 2), "malloc");+ strata::prefill::gdn_recurrence_variant(variant, dst, dh, dg, db, dz, dgm, 1e-6f, dy, dy16, T, nullptr);+ ck(cudaDeviceSynchronize(), "run");+ auto y = host(dy, (size_t) T * HV * S);+ auto y16 = host(dy16, (size_t) T * HV * S);+ auto st = host(dst, state.size());+ if (variant == 0) {+ y_ref = y; y16_ref = y16; st_ref = st;+ } else {+ size_t dy_n = 0, d16 = 0, dsn = 0;+ for (size_t i = 0; i < y.size(); ++i) dy_n += std::memcmp(&y[i], &y_ref[i], 4) != 0;+ for (size_t i = 0; i < y16.size(); ++i) d16 += y16[i] != y16_ref[i];+ for (size_t i = 0; i < st.size(); ++i) dsn += std::memcmp(&st[i], &st_ref[i], 4) != 0;+ const bool ok = dy_n == 0 && d16 == 0 && dsn == 0;+ failures += !ok;+ std::printf("%s T %lld variant %d vs 0: y %zu, y16 %zu, state %zu values differ\n", ok ? "PASS" : "FAIL",+ (long long) T, variant, dy_n, d16, dsn);+ }+ cudaFree(dst); cudaFree(dy); cudaFree(dy16);+ }+ // timing at the long lengths: variant 0 vs 1+ if (T >= 2048) {+ float* dst = dev(state);+ float* dy = nullptr;+ uint16_t* dy16 = nullptr;+ ck(cudaMalloc(&dy, (size_t) T * HV * S * 4), "malloc");+ ck(cudaMalloc(&dy16, (size_t) T * HV * S * 2), "malloc");+ for (int variant : {0, 1}) {+ cudaEvent_t a, b;+ cudaEventCreate(&a); cudaEventCreate(&b);+ strata::prefill::gdn_recurrence_variant(variant, dst, dh, dg, db, dz, dgm, 1e-6f, dy, dy16, T, nullptr);+ cudaEventRecord(a);+ for (int r = 0; r < 3; ++r)+ strata::prefill::gdn_recurrence_variant(variant, dst, dh, dg, db, dz, dgm, 1e-6f, dy, dy16, T, nullptr);+ cudaEventRecord(b);+ ck(cudaEventSynchronize(b), "time");+ float ms = 0;+ cudaEventElapsedTime(&ms, a, b);+ std::printf(" T %lld variant %d: %.3f ms per call (%.2f us per token)\n", (long long) T, variant, ms / 3,+ ms / 3 * 1000 / (double) T);+ }+ cudaFree(dst); cudaFree(dy); cudaFree(dy16);+ }+ cudaFree((void*) dh); cudaFree((void*) dg); cudaFree((void*) db); cudaFree((void*) dz); cudaFree((void*) dgm);+ }+ std::printf("FAILURES: %d\n", failures);+ return failures ? 1 : 0;+}