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

F7: a one-token window commits itself (fork 235784c)

Niko1221352cad8f0dc9
diff --git a/include/strata/core/verify.hpp b/include/strata/core/verify.hpp
index aa321550d..a85100cb2 100644
--- a/include/strata/core/verify.hpp
+++ b/include/strata/core/verify.hpp
@@ -437,6 +437,7 @@ private:
float *head_mixed_ = nullptr, *head_inj_ = nullptr, *head_logits_ = nullptr;
uint16_t* sh_bf16_ = nullptr;
uint8_t* arg_scratch_ = nullptr; ///< argmax_rows' partials and counters
+ int32_t* one_ = nullptr; ///< device {1}: the n_keep of a one-token window, which commits itself
float *sh_gate_ = nullptr, *sh_up_ = nullptr, *sh_g_ = nullptr;
float* hist_snap_ = nullptr; // T * NG_HIST * NG_HC_DIM
int64_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.hpp
index 3a947388f..2fe4fa2a1 100644
--- a/include/strata/kernels/verify_kernels.hpp
+++ b/include/strata/kernels/verify_kernels.hpp
@@ -21,9 +21,10 @@ namespace strata::kernels {
inline constexpr int kVerifyMaxT = 8;
/// For token t of T: conv over [history(3) | qkv_0 .. qkv_t] -> SiLU -> L2 norm of the q/k heads -> h[t].
-/// `history` is NOT written. Bitwise `fused_gdn_conv_l2` per token.
+/// `history` is NOT written, but with `commit` (one token): it then keeps the token, as gdn_conv_commit. Bitwise
+/// `fused_gdn_conv_l2` per token.
void gdn_conv_l2_multi(const float* history, const float* qkv, const float* conv_w, float* h, int channels,
- int qk_heads, float eps, int n_tok, void* stream, int t_begin = 0);
+ int qk_heads, float eps, int n_tok, void* stream, int t_begin = 0, bool commit = false);
/// history <- the last 3 entries of [history | qkv_0 .. qkv_{n-1}], n = *n_keep (0 leaves it as it was).
void gdn_conv_commit(float* history, const float* qkv, int channels, const int32_t* n_keep, void* stream);
/// alpha/beta for T columns of x (T, n_embd): gate (T, h_v), beta (T, h_v). Bitwise `fused_gdn_ab` per column.
diff --git a/src/core/verify.cpp b/src/core/verify.cpp
index e1fa915d7..9ca5781d6 100644
--- a/src/core/verify.cpp
+++ b/src/core/verify.cpp
@@ -104,6 +104,21 @@ const bool g_sh_stream = [] {
#endif
}();
+// A one-token window always keeps its token, so its graph advances the sequence state itself (the GDN conv history
+// and recurrence state) and Verifier::commit launches no commit graph after it (eddoursul's fork, F7). CUDA only:
+// HIP keeps the commit graph after every window (STRATA_ONE_TOKEN_COMMIT=0 does too).
+bool one_token_self_commit() {
+#if defined(STRATA_USE_HIP)
+ return false;
+#else
+ static const bool on = [] {
+ const char* v = std::getenv("STRATA_ONE_TOKEN_COMMIT");
+ return v == nullptr || std::atoi(v) != 0;
+ }();
+ return on;
+#endif
+}
+
bool mapped(size_t bytes, void** h, void** d) {
#if defined(STRATA_USE_HIP)
if (g_coherent) {
@@ -464,6 +479,7 @@ bool Verifier::init(const WeightTable& wt, const ModelGeometry& g, SessionState&
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));
+ one_ = b.take<int32_t>(4);
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);
};
@@ -500,6 +516,13 @@ bool Verifier::init(const WeightTable& wt, const ModelGeometry& g, SessionState&
Bump real;
real.base = (uint8_t*) arena_;
carve(real);
+ {
+ const int32_t one = 1;
+ if (cudaMemcpy(one_, &one, sizeof one, cudaMemcpyHostToDevice) != cudaSuccess) {
+ err = "verify: the arena could not be set";
+ return false;
+ }
+ }
sink_.staging = (unsigned long long) staging_;
sink_.staging_cap = kStagingBlobs;
(void) TS;
@@ -603,6 +626,7 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
const bool ple_on = ss.ple.ready() && ple_stage();
auto Rt = [&](int t) { return R_ + (size_t) t * HC * N; };
const int G = (split_ && T >= 2 && !batch_rec_) ? 2 : 1; // a batch window is one group
+ const bool self_commit = T == 1 && !batch_rec_ && !g_qfuse() && one_token_self_commit(); // see Verifier::commit
static const bool dec_batch = [] { const char* v = std::getenv("STRATA_DEC_BATCH"); return v == nullptr || std::atoi(v) != 0; }();
auto stamp = [&](int64_t l, int i, int grp) {
if (prof_on_ && grp == 0) gpu_stamp(prof_, (int) (l * kProfPer + i), cs);
@@ -809,7 +833,9 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
hb + (size_t) first * C, (int) C, (int) (2 * HK), EPS, t - first, cs, 0);
}
} else
- gdn_conv_l2_multi(conv, qkv, (const float*) wc->data, hb, (int) C, (int) (2 * HK), EPS, n, cs, tb);
+ // a one-token window keeps its token: it advances the conv history and the state itself (commit)
+ gdn_conv_l2_multi(conv, qkv, (const float*) wc->data, hb, (int) C, (int) (2 * HK), EPS, n, cs, tb,
+ self_commit);
stamp(l, 3, grp);
gdn_ab_multi(xm, (const uint16_t*) wa->data, (const uint16_t*) wb->data, (const float*) wdt->data,
(const float*) wsa->data, gate + (size_t) tb * HV, beta + (size_t) tb * HV, (int) N, (int) HV,
@@ -831,7 +857,7 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
}
} else
gdn_step_norm_multi(state, hb, (int) C, gate, beta, z_, (const float*) wnm->data, EPS, y_, (int) HK,
- (int) HV, te, nullptr, cs, tb, g_qfuse() ? (void*) xq_ : nullptr);
+ (int) HV, te, self_commit ? one_ : nullptr, cs, tb, g_qfuse() ? (void*) xq_ : nullptr);
stamp(l, 6, grp);
if (!g_qfuse()) native_quantize_q8_1(y_ + (size_t) tb * ZV, xq_, (int) ZV, n, cs); // STRATA_QFUSE: done above
native_mmvq(wout->native_type, wout->native_data, xq_, bo_ + tb * N, (int) ZV, (int) N, n, cs);
@@ -1874,20 +1900,24 @@ bool Verifier::commit(int n_keep, std::string& err) {
h_commit_[0] = n_keep;
h_commit_[1] = n_keep - 1;
for (int t = 0; t < max_t_; ++t) h_commit_[2 + t] = t < n_keep ? (int32_t) (last_pos0_ + t) : -1;
- std::atomic_thread_fence(std::memory_order_seq_cst);
- const cudaError_t le = cudaGraphLaunch(commit_exec_, cs_);
- if (le != cudaSuccess) { err = std::string("verify: commit launch: ") + cudaGetErrorString(le); return false; }
- // set_commit_async: no wait here - the next window runs on the same stream after it, and the drafter (its own
- // stream) reads only this window's final rows and its own K/V. h_commit_ is next written after the next window's
- // results are read, i.e. after this graph has run. Everything else waits on commit_done_ (wait_commit).
- if (!g_commit_async || next_ != nullptr) {
- const cudaError_t se = cudaStreamSynchronize(cs_);
- if (se != cudaSuccess) { err = std::string("verify: commit: ") + cudaGetErrorString(se); return false; }
+ if (last_t_ == 1 && one_token_self_commit()) {
+ // a one-token window has advanced the state itself (record_window): no commit graph
} else {
- const cudaError_t re = cudaEventRecord(commit_done_, cs_);
- if (re != cudaSuccess) { err = std::string("verify: commit event: ") + cudaGetErrorString(re); return false; }
- (void) cudaStreamQuery(cs_);
- commit_pending_ = true;
+ std::atomic_thread_fence(std::memory_order_seq_cst);
+ const cudaError_t le = cudaGraphLaunch(commit_exec_, cs_);
+ if (le != cudaSuccess) { err = std::string("verify: commit launch: ") + cudaGetErrorString(le); return false; }
+ // set_commit_async: no wait here - the next window runs on the same stream after it, and the drafter (its own
+ // stream) reads only this window's final rows and its own K/V. h_commit_ is next written after the next window's
+ // results are read, i.e. after this graph has run. Everything else waits on commit_done_ (wait_commit).
+ if (!g_commit_async || next_ != nullptr) {
+ const cudaError_t se = cudaStreamSynchronize(cs_);
+ if (se != cudaSuccess) { err = std::string("verify: commit: ") + cudaGetErrorString(se); return false; }
+ } else {
+ const cudaError_t re = cudaEventRecord(commit_done_, cs_);
+ if (re != cudaSuccess) { err = std::string("verify: commit event: ") + cudaGetErrorString(re); return false; }
+ (void) cudaStreamQuery(cs_);
+ commit_pending_ = true;
+ }
}
if (ple_stage()) // stages that share one session must advance it once
for (int t = 0; t < n_keep; ++t) {
diff --git a/src/kernels/cuda/verify_kernels.cu b/src/kernels/cuda/verify_kernels.cu
index 8fae405be..a953d5c91 100644
--- a/src/kernels/cuda/verify_kernels.cu
+++ b/src/kernels/cuda/verify_kernels.cu
@@ -24,10 +24,10 @@ void check(const char* what) {
if (e != cudaSuccess) { std::fprintf(stderr, "%s: %s\n", what, cudaGetErrorString(e)); std::exit(1); }
}
-__global__ void __launch_bounds__(S) gdn_conv_l2_multi_kernel(const float* __restrict__ hist,
- const float* __restrict__ qkv,
+// `commit` (one token): the history then keeps it - [hist1, hist2, x_0], each thread its channel's, after reading them
+__global__ void __launch_bounds__(S) gdn_conv_l2_multi_kernel(float* hist, const float* __restrict__ qkv,
const float* __restrict__ w, float* __restrict__ h,
- int C, int qk_heads, float eps, int t_begin) {
+ int C, int qk_heads, float eps, int t_begin, bool commit) {
__shared__ float part[S / 32];
const int t = t_begin + blockIdx.y;
const int c = blockIdx.x * S + threadIdx.x;
@@ -39,6 +39,11 @@ __global__ void __launch_bounds__(S) gdn_conv_l2_multi_kernel(const float* __res
win[j] = src < 3 ? hist[c * 3 + src] : qkv[(size_t) (src - 3) * C + c];
}
const float v0 = win[0], v1 = win[1], v2 = win[2], x = qkv[(size_t) t * C + c];
+ if (commit) {
+ hist[c * 3] = v1;
+ hist[c * 3 + 1] = v2;
+ hist[c * 3 + 2] = x;
+ }
float sum = v0 * w[c * 4] + v1 * w[c * 4 + 1] + v2 * w[c * 4 + 2] + x * w[c * 4 + 3];
float y = sum / (1.0f + __expf(-sum));
if ((int) blockIdx.x < qk_heads) {
@@ -842,13 +847,14 @@ void dense_steps(const int32_t* cells, int n, int32_t* steps, void* stream) {
}
void gdn_conv_l2_multi(const float* history, const float* qkv, const float* conv_w, float* h, int channels,
- int qk_heads, float eps, int n_tok, void* stream, int t_begin) {
- if (!history || !qkv || !conv_w || !h || channels % S != 0 || n_tok < 1 || n_tok > kVerifyMaxT) {
+ int qk_heads, float eps, int n_tok, void* stream, int t_begin, bool commit) {
+ if (!history || !qkv || !conv_w || !h || channels % S != 0 || n_tok < 1 || n_tok > kVerifyMaxT ||
+ (commit && (n_tok != 1 || t_begin != 0))) {
std::fprintf(stderr, "gdn_conv_l2_multi: invalid arguments\n");
std::exit(1);
}
gdn_conv_l2_multi_kernel<<<dim3((unsigned) (channels / S), (unsigned) n_tok), S, 0, (cudaStream_t) stream>>>(
- history, qkv, conv_w, h, channels, qk_heads, eps, t_begin);
+ const_cast<float*>(history), qkv, conv_w, h, channels, qk_heads, eps, t_begin, commit);
check("gdn_conv_l2_multi");
}