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++
F-1: the hyper-connection read without its FP32 copy of the normalized rows (gr_mix recomputes them, bit-identical; +1.1% at 32K, +1.7% at 128K)
Niko12212a6bbc7304c5
diff --git a/include/strata/prefill/kernels.hpp b/include/strata/prefill/kernels.hppindex 0f76b5b88..712940457 100644--- a/include/strata/prefill/kernels.hpp+++ b/include/strata/prefill/kernels.hpp@@ -14,6 +14,11 @@ namespace strata::prefill {// ---- hyper-connection (n_embd 2560, hc 4, hc_lr 320)/// xn[t, c*2560 + d] = R[t,c,d] * rsqrt(mean_d R[t,c,:]^2 + eps) * w_norm[c*2560 + d]; also its BF16 image.void gr_norm(const float* R, const float* w_norm, float eps, float* xn, uint16_t* xn16, int64_t T, void* stream);+/// F-1: gr_norm without its FP32 output: the row scales rs[t*4 + c] and the BF16 image; gr_mix_r then reads R.+void gr_norm_rs(const float* R, const float* w_norm, float eps, float* rs, uint16_t* xn16, int64_t T, void* stream);+/// gr_mix with xn recomputed from R, rs and w_norm exactly as gr_norm computes it (the same bits).+void gr_mix_r(const float* R, const float* rs, const float* w_norm, const float* gated, float* mixed, uint16_t* mixed16,+ int64_t T, void* stream, uint16_t* mixed_h = nullptr);/// lo16[t, k] = bf16(silu(lo[t, k] / hc))void gr_silu(const float* lo, uint16_t* lo16, int64_t T, void* stream);/// mixed[t, d] = mean_c xn[t, c, d] * sigmoid(gated[t, c, d]); FP32, BF16 and FP16 (either image may be null).diff --git a/src/prefill/kernels.cu b/src/prefill/kernels.cuindex 8fbaec075..d1a63d5b1 100644--- a/src/prefill/kernels.cu+++ b/src/prefill/kernels.cu@@ -70,6 +70,38 @@ __global__ void gr_norm_kernel(const float* __restrict__ R, const float* __restrxn16[row * N + d] = bf(v);}}+// F-1: the row scale only (and the BF16 image); gr_mix_r_kernel recomputes r * rs * w itself, in the same order,+// so the FP32 copy of the normalized rows (T x 10240 floats) is neither written nor read+__global__ void gr_norm_rs_kernel(const float* __restrict__ R, const float* __restrict__ w, float eps,+ float* __restrict__ rs_out, uint16_t* __restrict__ xn16) {+ __shared__ float sh[32];+ const int64_t row = blockIdx.x; // t * 4 + c+ const int c = (int) (row % HC);+ const float* r = R + row * N;+ float ss = 0.0f;+ for (int d = threadIdx.x; d < N; d += blockDim.x) ss += r[d] * r[d];+ const float rs = rsqrtf(block_sum(ss, sh) / (float) N + eps);+ if (threadIdx.x == 0) rs_out[row] = rs;+ for (int d = threadIdx.x; d < N; d += blockDim.x) xn16[row * N + d] = bf(r[d] * rs * w[c * N + d]);+}+__global__ void gr_mix_r_kernel(const float* __restrict__ R, const float* __restrict__ rs, const float* __restrict__ w,+ const float* __restrict__ g, float* __restrict__ mixed, uint16_t* __restrict__ mixed16,+ int64_t T, uint16_t* __restrict__ mixed_h) {+ const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x;+ if (i >= T * N) return;+ const int64_t t = i / N, d = i % N;+ float s = 0.0f;+#pragma unroll+ for (int c = 0; c < HC; ++c) {+ const int64_t j = t * D + c * N + d;+ const float x = R[j] * rs[t * HC + c] * w[c * N + d]; // gr_norm_kernel's value, bit for bit+ s = fmaf(x, sigm(g[j]), s);+ }+ s /= (float) HC;+ mixed[i] = s;+ if (mixed16) mixed16[i] = bf(s);+ if (mixed_h) mixed_h[i] = hf(s);+}__global__ void gr_silu_kernel(const float* __restrict__ lo, uint16_t* __restrict__ lo16, int64_t n) {const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x;if (i >= n) return;@@ -519,6 +551,16 @@ void gr_norm(const float* R, const float* w_norm, float eps, float* xn, uint16_tgr_norm_kernel<<<(unsigned) (T * HC), 256, 0, (cudaStream_t) stream>>>(R, w_norm, eps, xn, xn16);check("gr_norm");}+void gr_norm_rs(const float* R, const float* w_norm, float eps, float* rs, uint16_t* xn16, int64_t T, void* stream) {+ gr_norm_rs_kernel<<<(unsigned) (T * HC), 256, 0, (cudaStream_t) stream>>>(R, w_norm, eps, rs, xn16);+ check("gr_norm_rs");+}+void gr_mix_r(const float* R, const float* rs, const float* w_norm, const float* gated, float* mixed, uint16_t* mixed16,+ int64_t T, void* stream, uint16_t* mixed_h) {+ gr_mix_r_kernel<<<blocks_for(T * N), 256, 0, (cudaStream_t) stream>>>(R, rs, w_norm, gated, mixed, mixed16, T,+ mixed_h);+ check("gr_mix_r");+}void gr_silu(const float* lo, uint16_t* lo16, int64_t T, void* stream) {gr_silu_kernel<<<blocks_for(T * LR), 256, 0, (cudaStream_t) stream>>>(lo, lo16, T * LR);check("gr_silu");diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cppindex a91952d9b..540ce8342 100644--- a/src/prefill/prefill.cpp+++ b/src/prefill/prefill.cpp@@ -86,6 +86,12 @@ inline int ring_slots(size_t T) {}constexpr int DQ = 2; // dequantized-expert ring (FP16 gate/up + down)+// F-1: STRATA_GR_UNFUSED=1 keeps the FP32 copy of the normalized rows (gr_norm + gr_mix), the A/B arm+inline bool gr_unfused() {+ static const bool v = [] { const char* e = std::getenv("STRATA_GR_UNFUSED"); return e && e[0] == '1'; }();+ return v;+}+double ms_since(Clock::time_point t) { return std::chrono::duration<double, std::milli>(Clock::now() - t).count(); }// Either cudaMalloc (owned, freed with the object) or a bump allocation from a borrowed region; with no base and@@ -252,6 +258,7 @@ struct Prefill::Impl {std::vector<void*> owned;// chunk buffersfloat *emb = nullptr, *R = nullptr, *xn = nullptr, *lo = nullptr, *gated = nullptr, *inj = nullptr;+ float* grs = nullptr; // F-1: the hyper-connection read's row scales (T x 4)uint16_t *xn16 = nullptr, *lo16 = nullptr;float* mixed = nullptr;uint16_t *mixed_bf = nullptr, *mixed_h = nullptr;@@ -565,7 +572,9 @@ bool Prefill::carve(size_t T, void* alloc) {const core::ModelGeometry& g = *m.g;core::SessionState& ss = *m.ss;bool ok = true;- m.emb = o.take<float>(T * N, ok); m.R = o.take<float>(T * D, ok); m.xn = o.take<float>(T * D, ok);+ m.emb = o.take<float>(T * N, ok); m.R = o.take<float>(T * D, ok);+ m.xn = gr_unfused() ? o.take<float>(T * D, ok) : nullptr; // F-1: not needed (gr_mix_r reads R)+ m.grs = o.take<float>(T * HC, ok);m.xn16 = o.take<uint16_t>(T * D, ok); m.lo = o.take<float>(T * LR, ok); m.lo16 = o.take<uint16_t>(T * LR, ok);m.gated = o.take<float>(T * D, ok); m.inj = o.take<float>(T * HC, ok);m.mixed = o.take<float>(T * N, ok); m.mixed_bf = o.take<uint16_t>(T * N, ok);@@ -1094,12 +1103,14 @@ bool Prefill::run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& e*wu = need(v, su.c_str(), err), *wi = need(v, si.c_str(), err);if (!wn || !wd || !wu || !wi) return false;pt.mark(kPfHc, cs);- gr_norm(m.R, (const float*) wn->data, EPS, m.xn, m.xn16, T, m.cs);+ if (gr_unfused()) gr_norm(m.R, (const float*) wn->data, EPS, m.xn, m.xn16, T, m.cs);+ else gr_norm_rs(m.R, (const float*) wn->data, EPS, m.grs, m.xn16, T, m.cs);if (!bf16_proj(m.gemm, wd, m.xn16, m.lo, T, sd, err)) return false;gr_silu(m.lo, m.lo16, T, m.cs);if (!bf16_proj(m.gemm, wu, m.lo16, m.gated, T, su, err)) return false;if (!bf16_proj(m.gemm, wi, m.xn16, m.inj, T, si, err)) return false;- gr_mix(m.xn, m.gated, m.mixed, m.mixed_bf, T, m.cs, m.mixed_h);+ if (gr_unfused()) gr_mix(m.xn, m.gated, m.mixed, m.mixed_bf, T, m.cs, m.mixed_h);+ else gr_mix_r(m.R, m.grs, (const float*) wn->data, m.gated, m.mixed, m.mixed_bf, T, m.cs, m.mixed_h);if (half == 0 && !core::is_qsa_layer(g, l)) {// ======================= GDN =======================