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++

C-3: the prompt path's GDN short conv tiled over tokens

Niko122107b107403a9f
diff --git a/src/prefill/kernels.cu b/src/prefill/kernels.cu
index d09b06b25..09a013fd5 100644
--- a/src/prefill/kernels.cu
+++ b/src/prefill/kernels.cu
@@ -132,6 +132,38 @@ __global__ void gdn_conv_kernel(float* __restrict__ hist, const float* __restric
}
hist[c * 3] = v0; hist[c * 3 + 1] = v1; hist[c * 3 + 2] = v2;
}
+// C-3: the same 4-tap causal conv, tiled over tokens: thread (c, tile) reads its tile's 3 predecessors from the
+// chunk (or the history before it) instead of carrying them - the conv reads inputs, not its own outputs, so the
+// tiles are independent. The same expression per element (so the same bits); the history is written afterwards.
+constexpr int CONV_TILE = 64;
+__global__ void gdn_conv_tiled_kernel(const float* __restrict__ hist, const float* __restrict__ qkv,
+ const float* __restrict__ w, float* __restrict__ h, int64_t T) {
+ const int c = blockIdx.x * blockDim.x + threadIdx.x;
+ if (c >= C) return;
+ const int64_t t0 = (int64_t) blockIdx.y * CONV_TILE;
+ if (t0 >= T) return;
+ const int64_t t1 = t0 + CONV_TILE < T ? t0 + CONV_TILE : T;
+ auto input = [&](int64_t t) -> float { return t >= 0 ? qkv[t * C + c] : hist[c * 3 + (int) (t + 3)]; };
+ float v0 = input(t0 - 3), v1 = input(t0 - 2), v2 = input(t0 - 1);
+ const float w0 = w[c * 4], w1 = w[c * 4 + 1], w2 = w[c * 4 + 2], w3 = w[c * 4 + 3];
+ for (int64_t t = t0; t < t1; ++t) {
+ const float x = qkv[t * C + c];
+ const float s = v0 * w0 + v1 * w1 + v2 * w2 + x * w3;
+ h[t * C + c] = s / (1.0f + __expf(-s));
+ v0 = v1; v1 = v2; v2 = x;
+ }
+}
+// the history after the chunk: its last three inputs (the older history where the chunk is shorter than 3)
+__global__ void gdn_conv_hist_kernel(float* __restrict__ hist, const float* __restrict__ qkv, int64_t T) {
+ const int c = blockIdx.x * blockDim.x + threadIdx.x;
+ if (c >= C) return;
+ float v[3];
+ for (int k = 0; k < 3; ++k) {
+ const int64_t t = T - 3 + k;
+ v[k] = t >= 0 ? qkv[t * C + c] : hist[c * 3 + (int) (t + 3)];
+ }
+ hist[c * 3] = v[0]; hist[c * 3 + 1] = v[1]; hist[c * 3 + 2] = v[2];
+}
__global__ void gdn_l2_kernel(float* __restrict__ h, float eps) {
// block (t, head) over the 32 q/k heads, 128 threads
const int64_t t = blockIdx.y;
@@ -448,7 +480,14 @@ void gdn_gates(const float* ab, const float* dt, const float* ssm_a, float* gate
check("gdn_gates");
}
void gdn_conv(float* history, const float* qkv, const float* conv_w, float* h, int64_t T, float eps, void* stream) {
- gdn_conv_kernel<<<C / 128, 128, 0, (cudaStream_t) stream>>>(history, qkv, conv_w, h, T);
+ static const bool serial = std::getenv("STRATA_GDN_CONV_SERIAL") != nullptr; // the old walk (A/B)
+ if (serial || T <= CONV_TILE) {
+ gdn_conv_kernel<<<C / 128, 128, 0, (cudaStream_t) stream>>>(history, qkv, conv_w, h, T);
+ } else {
+ gdn_conv_tiled_kernel<<<dim3(C / 128, (unsigned) ((T + CONV_TILE - 1) / CONV_TILE)), 128, 0,
+ (cudaStream_t) stream>>>(history, qkv, conv_w, h, T);
+ gdn_conv_hist_kernel<<<C / 128, 128, 0, (cudaStream_t) stream>>>(history, qkv, T);
+ }
gdn_l2_kernel<<<dim3(2 * HK, (unsigned) T), S, 0, (cudaStream_t) stream>>>(h, eps);
check("gdn_conv");
}