Skip to content

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: STRATA_CVEC_FUSE=1 - a steered layer's write, control vector and next norm in one pass over R, bitwise

Niko12216448f0f7ad63
diff --git a/include/strata/kernels/cvec.hpp b/include/strata/kernels/cvec.hpp
index a447eb528..c93474834 100644
--- a/include/strata/kernels/cvec.hpp
+++ b/include/strata/kernels/cvec.hpp
@@ -52,6 +52,10 @@ bool cvec_replicate(std::string& err);
void cvec_set_enabled(bool on);
bool cvec_enabled();
+/// The CURRENT device's tables (dir n_layers x n_embd, s n_layers, the request flag), for a caller that applies the
+/// vector inside its own kernel (the prompt path's STRATA_CVEC_FUSE). False without a vector on this device.
+bool cvec_tables(const float** dir, const float** s, const int** on);
+
/// Layer `layer`'s vector on T tokens' residual stacks (`R + t * r_ld`, hc streams of n_embd). With `write`, the
/// pending FFN write `R += bo * 2 sigmoid(inj / hc)` (the fused read's arithmetic) is applied first, for callers
/// whose writes are folded into the next layer's read; without it, R must already hold the layer's output.
diff --git a/include/strata/prefill/kernels.hpp b/include/strata/prefill/kernels.hpp
index 5c5fe1b64..8ce3f8edc 100644
--- a/include/strata/prefill/kernels.hpp
+++ b/include/strata/prefill/kernels.hpp
@@ -31,6 +31,12 @@ bool gr_upmix(const uint16_t* lo16, const uint16_t* w_up, const float* R, const
float* mixed, uint16_t* mixed16, uint16_t* mixed_h, int64_t T, void* stream);
void gr_write_norm_rs(float* R, const float* bo, const float* inj, int64_t inj_ld, const float* w_norm_next, float eps,
float* rs, uint16_t* xn16, int64_t T, void* stream, uint16_t* xn16_lo = nullptr);
+/// S23 (STRATA_CVEC_FUSE=1): gr_write, then the control vector (v_l = layer l's direction row, *s_l its scale,
+/// *on the request flag, mode 0 project / 1 add; cvec_kernel's arithmetic), then gr_norm_rs with the next half's
+/// norm - one pass over R, the same bits as the three kernels.
+void gr_write_cvec_norm_rs(float* R, const float* bo, const float* inj, int64_t inj_ld, const float* v_l,
+ const float* s_l, const int* on, int mode, const float* w_norm_next, float eps, float* rs,
+ uint16_t* xn16, int64_t T, void* stream, uint16_t* xn16_lo = nullptr);
/// lo16[t, k] = bf16(silu(lo[t, k] / hc))
void gr_silu(const float* lo, uint16_t* lo16, int64_t T, void* stream, uint16_t* lo16_lo = nullptr);
/// 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/kernels/cuda/cvec.cu b/src/kernels/cuda/cvec.cu
index 668293246..3f5fe7647 100644
--- a/src/kernels/cuda/cvec.cu
+++ b/src/kernels/cuda/cvec.cu
@@ -151,6 +151,14 @@ void cvec_set_enabled(bool on) {
bool cvec_enabled() { return g_cvec.loaded() && g_on_host; }
+bool cvec_tables(const float** dir, const float** s, const int** on) {
+ if (!g_cvec.loaded()) return false;
+ const DevTables& t = g_dev[cur_device()];
+ if (t.dir == nullptr) return false;
+ *dir = t.dir; *s = t.s; *on = t.on;
+ return true;
+}
+
void cvec_apply(float* R, int64_t layer, int64_t T, int64_t r_ld, const float* bo, int64_t bo_ld, const float* inj,
int64_t inj_ld, bool write, void* stream) {
if (!g_cvec.loaded() || T < 1) return;
diff --git a/src/prefill/kernels.cu b/src/prefill/kernels.cu
index 450747d11..1a09cd6f9 100644
--- a/src/prefill/kernels.cu
+++ b/src/prefill/kernels.cu
@@ -159,6 +159,68 @@ __global__ void __launch_bounds__(256) gr_write_norm_rs_kernel(float* __restrict
if (xn16_lo) xn16_lo[row * N + d] = bf_lo(x, h);
}
}
+// S23 (opt-in STRATA_CVEC_FUSE=1): a steered layer's FFN write, its control vector and the next half's norm in one
+// pass over R (was gr_write + cvec_apply + gr_norm_rs: three). The write is gr_write_kernel's, the vector
+// cvec_kernel's (the same per-thread dot order over d = tid + 256 k, the same two-level xor reduction), the norm
+// gr_norm_rs_kernel's (the same ss order and block_sum): the same bits.
+__global__ void __launch_bounds__(256) gr_write_cvec_norm_rs_kernel(
+ float* __restrict__ R, const float* __restrict__ bo, const float* __restrict__ inj, int64_t inj_ld,
+ const float* __restrict__ v_l, const float* __restrict__ s_l, const int* __restrict__ on, int mode,
+ const float* __restrict__ w, float eps, float* __restrict__ rs_out, uint16_t* __restrict__ xn16,
+ uint16_t* __restrict__ xn16_lo) {
+ __shared__ float sh[32];
+ __shared__ float part[8];
+ const int64_t row = blockIdx.x; // t * 4 + c
+ const int64_t t = row / HC;
+ const int c = (int) (row % HC);
+ float* r = R + row * N;
+ const float sc = 2.0f * sigm(inj[t * inj_ld + c] / (float) HC);
+ const float s = *s_l;
+ const bool steer = *on != 0 && s != 0.0f;
+ float x[GRW_PER];
+ float dot = 0.0f;
+ int k = 0;
+#pragma unroll
+ for (int d = threadIdx.x; d < N; d += 256, ++k) {
+ const float xv = fmaf(bo[t * N + d], sc, r[d]);
+ x[k] = xv;
+ if (steer && mode == 0) dot = fmaf(xv, v_l[d], dot);
+ }
+ if (steer && mode == 0) {
+#pragma unroll
+ for (int o = 16; o > 0; o >>= 1) dot += __shfl_xor_sync(0xffffffffu, dot, o);
+ if ((threadIdx.x & 31) == 0) part[threadIdx.x >> 5] = dot;
+ __syncthreads();
+ if (threadIdx.x < 32) {
+ float p = threadIdx.x < 8 ? part[threadIdx.x] : 0.0f;
+#pragma unroll
+ for (int o = 16; o > 0; o >>= 1) p += __shfl_xor_sync(0xffffffffu, p, o);
+ if (threadIdx.x == 0) part[0] = p;
+ }
+ __syncthreads();
+ dot = part[0] * s;
+ }
+ float ss = 0.0f;
+ k = 0;
+#pragma unroll
+ for (int d = threadIdx.x; d < N; d += 256, ++k) {
+ float xv = x[k];
+ if (steer) xv = mode == 0 ? fmaf(-dot, v_l[d], xv) : xv + v_l[d];
+ r[d] = xv;
+ x[k] = xv;
+ ss += xv * xv;
+ }
+ const float rs = rsqrtf(block_sum(ss, sh) / (float) N + eps);
+ if (threadIdx.x == 0) rs_out[row] = rs;
+ k = 0;
+#pragma unroll
+ for (int d = threadIdx.x; d < N; d += 256, ++k) {
+ const float y = x[k] * rs * w[c * N + d];
+ const uint16_t h = bf(y);
+ xn16[row * N + d] = h;
+ if (xn16_lo) xn16_lo[row * N + d] = bf_lo(y, h);
+ }
+}
__global__ void gr_silu_kernel(const float* __restrict__ lo, uint16_t* __restrict__ lo16, uint16_t* __restrict__ lo16_lo,
int64_t n) {
const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x;
@@ -941,6 +1003,13 @@ void gr_write_norm_rs(float* R, const float* bo, const float* inj, int64_t inj_l
rs, xn16, xn16_lo);
check("gr_write_norm_rs");
}
+void gr_write_cvec_norm_rs(float* R, const float* bo, const float* inj, int64_t inj_ld, const float* v_l,
+ const float* s_l, const int* on, int mode, const float* w_norm_next, float eps, float* rs,
+ uint16_t* xn16, int64_t T, void* stream, uint16_t* xn16_lo) {
+ gr_write_cvec_norm_rs_kernel<<<(unsigned) (T * HC), 256, 0, (cudaStream_t) stream>>>(
+ R, bo, inj, inj_ld, v_l, s_l, on, mode, w_norm_next, eps, rs, xn16, xn16_lo);
+ check("gr_write_cvec_norm_rs");
+}
void gr_silu(const float* lo, uint16_t* lo16, int64_t T, void* stream, uint16_t* lo16_lo) {
gr_silu_kernel<<<blocks_for(T * LR), 256, 0, (cudaStream_t) stream>>>(lo, lo16, lo16_lo, T * LR);
check("gr_silu");
diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp
index 7c2cf5a83..67997d565 100644
--- a/src/prefill/prefill.cpp
+++ b/src/prefill/prefill.cpp
@@ -180,6 +180,12 @@ inline bool hc_upmix() {
return v;
}
inline bool bf16x2_hc() { return bf16x2_mode() == 1; }
+// S23 (opt-in STRATA_CVEC_FUSE=1): a steered layer's FFN write + control vector + the next half's norm in one pass
+// over R (gr_write_cvec_norm_rs; bitwise the gr_write + cvec_apply + gr_norm_rs it replaces)
+inline bool cvec_fuse() {
+ static const bool v = [] { const char* e = std::getenv("STRATA_CVEC_FUSE"); return e && e[0] == '1'; }();
+ return v;
+}
// S23 (opt-in STRATA_PF_PAD=1 with STRATA_PF_GEMM=1): the FP16 activations of the two K 6144 projections (ssm_out's
// y_h, attn_output's attn_h) with row stride 6144 + 64, so the GEMM's rows do not camp on the memory channels (the
// 4 KB-multiple stride; Gemm::native pads the weight the same way). Same bits.
@@ -2109,22 +2115,64 @@ bool Prefill::run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& e
// touches R in between (not the stage's last half, not before the PLE block of layer 1, not under a
// control vector)
const int64_t nl = half == 0 ? l : l + 1;
- const bool fuse = !gr_unfused() && nl < LE && !(half == 1 && nl == 1 && ple_on) &&
- !(half == 1 && strata::kernels::cvec().covers(l));
+ const bool steered = half == 1 && strata::kernels::cvec().covers(l);
+ const float *cv_dir = nullptr, *cv_s = nullptr;
+ const int* cv_on = nullptr;
+ const bool cv_fused = steered && cvec_fuse() && !gr_unfused() && nl < LE && !(nl == 1 && ple_on) &&
+ strata::kernels::cvec().n_embd == N && strata::kernels::cvec().hc == HC &&
+ strata::kernels::cvec_tables(&cv_dir, &cv_s, &cv_on);
+ const bool fuse = !gr_unfused() && nl < LE && !(half == 1 && nl == 1 && ple_on) && (!steered || cv_fused);
const core::WeightRef* wnn = nullptr;
if (fuse) {
const core::LayerView vn(*m.wt, nl);
wnn = need(vn, half == 0 ? "hc_ffn_norm.weight" : "hc_attn_norm.weight", err);
if (!wnn) return false;
}
- if (wnn) {
+ if (wnn && cv_fused) {
+ // STRATA_CVEC_FUSE_CHECK=N: on the first N fused writes, also run the three-kernel path on a copy
+ // of R and report whether R, the row scales and the BF16 image are bit-identical
+ static int checks = [] { const char* e = std::getenv("STRATA_CVEC_FUSE_CHECK"); return e ? std::atoi(e) : 0; }();
+ if (checks > 0) {
+ --checks;
+ float *Rc = nullptr, *rc = nullptr;
+ uint16_t* xc = nullptr;
+ cudaMalloc(&Rc, (size_t) T * D * 4); cudaMalloc(&rc, (size_t) T * HC * 4);
+ cudaMalloc(&xc, (size_t) T * D * 2);
+ cudaMemcpyAsync(Rc, m.R, (size_t) T * D * 4, cudaMemcpyDeviceToDevice, m.cs);
+ gr_write(Rc, m.bo, m.inj, HC, T, m.cs);
+ strata::kernels::cvec_apply(Rc, l, T, D, nullptr, 0, nullptr, 0, false, m.cs);
+ gr_norm_rs(Rc, (const float*) wnn->data, EPS, rc, xc, T, m.cs);
+ gr_write_cvec_norm_rs(m.R, m.bo, m.inj, HC, cv_dir + l * N, cv_s + l, cv_on,
+ strata::kernels::cvec().mode, (const float*) wnn->data, EPS, m.grs,
+ m.xn16, T, m.cs, m.xn16_lo);
+ std::vector<float> a((size_t) T * D), b((size_t) T * D), ra((size_t) T * HC), rb((size_t) T * HC);
+ std::vector<uint16_t> xa((size_t) T * D), xb((size_t) T * D);
+ cudaMemcpyAsync(a.data(), m.R, a.size() * 4, cudaMemcpyDeviceToHost, m.cs);
+ cudaMemcpyAsync(b.data(), Rc, b.size() * 4, cudaMemcpyDeviceToHost, m.cs);
+ cudaMemcpyAsync(ra.data(), m.grs, ra.size() * 4, cudaMemcpyDeviceToHost, m.cs);
+ cudaMemcpyAsync(rb.data(), rc, rb.size() * 4, cudaMemcpyDeviceToHost, m.cs);
+ cudaMemcpyAsync(xa.data(), m.xn16, xa.size() * 2, cudaMemcpyDeviceToHost, m.cs);
+ cudaMemcpyAsync(xb.data(), xc, xb.size() * 2, cudaMemcpyDeviceToHost, m.cs);
+ cudaStreamSynchronize(m.cs);
+ cudaFree(Rc); cudaFree(rc); cudaFree(xc);
+ std::fprintf(stderr, "strata: STRATA_CVEC_FUSE_CHECK layer %lld T %lld: R %s, rs %s, xn16 %s\n",
+ (long long) l, (long long) T,
+ std::memcmp(a.data(), b.data(), a.size() * 4) ? "DIFFERS" : "identical",
+ std::memcmp(ra.data(), rb.data(), ra.size() * 4) ? "DIFFERS" : "identical",
+ std::memcmp(xa.data(), xb.data(), xa.size() * 2) ? "DIFFERS" : "identical");
+ } else
+ gr_write_cvec_norm_rs(m.R, m.bo, m.inj, HC, cv_dir + l * N, cv_s + l, cv_on,
+ strata::kernels::cvec().mode, (const float*) wnn->data, EPS, m.grs, m.xn16,
+ T, m.cs, m.xn16_lo);
+ normed = true;
+ } else if (wnn) {
gr_write_norm_rs(m.R, m.bo, m.inj, HC, (const float*) wnn->data, EPS, m.grs, m.xn16, T, m.cs,
m.xn16_lo);
normed = true;
} else {
gr_write(m.R, m.bo, m.inj, HC, T, m.cs);
}
- if (half == 1 && strata::kernels::cvec().covers(l)) // --control-vector-scaled
+ if (steered && !cv_fused) // --control-vector-scaled
strata::kernels::cvec_apply(m.R, l, T, D, nullptr, 0, nullptr, 0, false, m.cs);
}
}