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: the draft round graph runs the draft layer's front once (fork 22b46b6)

Niko1221ca83311616fc
diff --git a/include/strata/core/mtp.hpp b/include/strata/core/mtp.hpp
index 019268d8d..c1e014302 100644
--- a/include/strata/core/mtp.hpp
+++ b/include/strata/core/mtp.hpp
@@ -150,6 +150,13 @@ public:
private:
bool record_forward(int T, int step_row0, cudaStream_t cs, std::string& err);
+ /// The layer's front for T rows at step rows [row0, +T): the embedding, the fc projections, the attention
+ /// hyper-connection read (R_, inj_, mixed_) and the K/V appended.
+ bool record_front(int T, int row0, cudaStream_t cs, std::string& err);
+ /// The rest for row 0 at step row `step_row`: the attention from the q projection, the MLP, the final mixer,
+ /// the head, the draft and its probability. Needs row 0's R_, inj_ and mixed_ (its q8_1 rows in xq_).
+ bool record_rest(int step_row, cudaStream_t cs, std::string& err);
+ void norm_rope(float* data, const float* gamma, int rows, int cols, const int32_t* p, cudaStream_t cs);
bool capture_prefill(int T, std::string& err);
bool capture_prefill_dev(int T, std::string& err); ///< E-4: without the mapped staging (inputs copied on device)
bool capture_round(int T, bool coupled, std::string& err);
diff --git a/include/strata/kernels/verify_kernels.hpp b/include/strata/kernels/verify_kernels.hpp
index f7a6070bb..3a947388f 100644
--- a/include/strata/kernels/verify_kernels.hpp
+++ b/include/strata/kernels/verify_kernels.hpp
@@ -90,6 +90,10 @@ void mtp_select(const float* R_src, int64_t R_stride, const int32_t* ids, const
/// --pipeline-windows, the drafter's chain teacher forced: `*tok = force[j]` when force[j] >= 0 (`force` is mapped
/// host memory, read when the kernel runs), else `*tok` is left as it is.
void force_token(int32_t* tok, const int32_t* force, int j, void* stream);
+/// Row *row_dev of a, b and c (a_n, b_n, c_n floats a row) copied to their row 0 (the draft layer's rest runs on
+/// row 0). Graph-capturable: the row is read on the device.
+void copy_row_to_first(const int32_t* row_dev, float* a, int64_t a_n, float* b, int64_t b_n, float* c, int64_t c_n,
+ void* stream);
/// dst row i = src row ids[i] (row_bytes each, multiple of 16), for n rows.
void gather_rows(const uint8_t* src, int64_t row_bytes, const int32_t* ids, int64_t n, uint8_t* dst, void* stream);
/// ids[t] = table[ids[t]] for n entries (a subset index back to a token id).
diff --git a/src/core/mtp.cpp b/src/core/mtp.cpp
index 77a01cb9f..8f3111591 100644
--- a/src/core/mtp.cpp
+++ b/src/core/mtp.cpp
@@ -628,26 +628,26 @@ bool MtpDrafter::bind(const WeightTable& wt, const NativeHead* head, const float
// The layer for T rows. full = false stops after the K/V append (the prompt only needs the cache).
bool MtpDrafter::record_forward(int T, int step_row0, cudaStream_t cs, std::string& err) {
- using namespace strata::kernels;
- const ModelGeometry& g = *g_;
- SessionState& ss = *ss_;
// step_row0 >= 0: the full layer on step rows [step_row0, +T); step_row0 < 0: K/V only on rows [-1 - step_row0, +T)
const bool full = step_row0 >= 0;
- const int row0 = full ? step_row0 : -1 - step_row0;
if (full && T != 1) { err = "mtp: the full layer runs one row at a time (its attention scratch is sized for one)"; return false; }
- const int64_t N = g.n_embd, HC = g.hc, K = ss.k, NH = g.n_head, HD = g.head_dim, NKV = g.n_head_kv;
+ if (!record_front(T, full ? step_row0 : -1 - step_row0, cs, err)) return false;
+ return !full || record_rest(step_row0, cs, err);
+}
+
+bool MtpDrafter::record_front(int T, int row0, cudaStream_t cs, std::string& err) {
+ using namespace strata::kernels;
+ const ModelGeometry& g = *g_;
+ const int64_t N = g.n_embd, HC = g.hc, NH = g.n_head, HD = g.head_dim, NKV = g.n_head_kv;
const QsaShapes s = shapes_of(g);
- const GrShapes gs{g.n_embd, g.hc, g.hc_lr};
const int32_t* step = step_ + row0 * 4;
const int32_t* pos = pos_ + row0 * NH;
// --mtp-q4: each projection's format and weights for this pass (Q8_0 unless the Q4_0 copies exist)
- int wt_fc_embedding, wt_fc_hidden, wt_k_proj, wt_v_proj, wt_q_proj, wt_o_proj;
+ int wt_fc_embedding, wt_fc_hidden, wt_k_proj, wt_v_proj;
const void* wp_fc_embedding = wq("fc_embedding.weight", wt_fc_embedding);
const void* wp_fc_hidden = wq("fc_hidden.weight", wt_fc_hidden);
const void* wp_k_proj = wq("self_attn.k_proj.weight", wt_k_proj);
const void* wp_v_proj = wq("self_attn.v_proj.weight", wt_v_proj);
- const void* wp_q_proj = wq("self_attn.q_proj.weight", wt_q_proj);
- const void* wp_o_proj = wq("self_attn.o_proj.weight", wt_o_proj);
try {
// ---- the two input branches
const WeightRef* we = wt_->find("token_embd.weight");
@@ -688,17 +688,12 @@ bool MtpDrafter::record_forward(int T, int step_row0, cudaStream_t cs, std::stri
}
fused_gr_read_multi(fa, T, xn_, cs);
}
- // ---- attention: K/V into the layer's own cache, then (full) dense attention over every cell
- auto norm_rope = [&](float* data, const float* gamma, int rows, int cols, const int32_t* p) {
- native_qsa_rms_norm_weighted(data, gamma, data, cols, rows, EPS, cs);
- if (native_rope_enabled()) native_rope_apply(data, data, rows, cols, (int) s.n_rot, rope_scaling(), p, cs);
- else rope_neox_apply(data, data, rows, cols, (int) s.n_rot, st_.cos_tab, st_.sin_tab, p, cs);
- };
+ // ---- attention: K/V into the layer's own cache
native_quantize_q8_1(mixed_, xq_, (int) N, T, cs);
native_mmvq(wt_k_proj, wp_k_proj, xq_, kcur_, (int) N, (int) (NKV * HD), T, cs);
native_mmvq(wt_v_proj, wp_v_proj, xq_, vcur_, (int) N, (int) (NKV * HD), T, cs);
for (int t = 0; t < T; ++t) {
- norm_rope(kcur_ + t * NKV * HD, f32("self_attn.k_norm.weight"), (int) NKV, (int) HD, pos + t * NH);
+ norm_rope(kcur_ + t * NKV * HD, f32("self_attn.k_norm.weight"), (int) NKV, (int) HD, pos + t * NH, cs);
if (st_.kv_rot) { // rotated K and V (kv_q4.hpp): Q4_0, and INT8 with STRATA_KV_ROT=1
fwht256_inplace_cuda(kcur_ + t * NKV * HD, NKV, cs);
fwht256_inplace_cuda(vcur_ + t * NKV * HD, NKV, cs);
@@ -714,7 +709,37 @@ bool MtpDrafter::record_forward(int T, int step_row0, cudaStream_t cs, std::stri
kv_append_step(st_.k_pool, st_.v_pool, st_.page_table, step + t * 4, kcur_ + t * NKV * HD,
vcur_ + t * NKV * HD, s, cs, &st_.host);
}
- if (!full) return true;
+ } catch (const std::exception& e) {
+ err = std::string("mtp: ") + e.what();
+ return false;
+ }
+ return true;
+}
+
+void MtpDrafter::norm_rope(float* data, const float* gamma, int rows, int cols, const int32_t* p, cudaStream_t cs) {
+ using namespace strata::kernels;
+ const QsaShapes s = shapes_of(*g_);
+ native_qsa_rms_norm_weighted(data, gamma, data, cols, rows, EPS, cs);
+ if (native_rope_enabled()) native_rope_apply(data, data, rows, cols, (int) s.n_rot, rope_scaling(), p, cs);
+ else rope_neox_apply(data, data, rows, cols, (int) s.n_rot, st_.cos_tab, st_.sin_tab, p, cs);
+}
+
+bool MtpDrafter::record_rest(int step_row, cudaStream_t cs, std::string& err) {
+ using namespace strata::kernels;
+ const ModelGeometry& g = *g_;
+ SessionState& ss = *ss_;
+ const int T = 1;
+ const int64_t N = g.n_embd, HC = g.hc, K = ss.k, NH = g.n_head, HD = g.head_dim;
+ const QsaShapes s = shapes_of(g);
+ const GrShapes gs{g.n_embd, g.hc, g.hc_lr};
+ const int32_t* step = step_ + step_row * 4;
+ const int32_t* pos = pos_ + step_row * NH;
+ // --mtp-q4: the q and o projections' format and weights (Q8_0 unless the Q4_0 copies exist)
+ int wt_q_proj, wt_o_proj;
+ const void* wp_q_proj = wq("self_attn.q_proj.weight", wt_q_proj);
+ const void* wp_o_proj = wq("self_attn.o_proj.weight", wt_o_proj);
+ try {
+ // ---- dense attention over every cell
native_mmvq(wt_q_proj, wp_q_proj, xq_, qfull_, (int) N, (int) (NH * 2 * HD), T, cs);
for (int t = 0; t < T; ++t) {
float* qc = qcur_ + t * NH * HD;
@@ -723,7 +748,7 @@ bool MtpDrafter::record_forward(int T, int step_row0, cudaStream_t cs, std::stri
err = "mtp: q split failed";
return false;
}
- norm_rope(qc, f32("self_attn.q_norm.weight"), (int) NH, (int) HD, pos + t * NH);
+ norm_rope(qc, f32("self_attn.q_norm.weight"), (int) NH, (int) HD, pos + t * NH, cs);
if (st_.kv_rot) fwht256_inplace_cuda(qc, NH, cs);
}
const QsaAttnPools pools = qsa_attn_pools(st_);
@@ -887,18 +912,22 @@ bool MtpDrafter::capture_round(int T, bool coupled, std::string& err) {
copy_i32_from_mapped(pos_, m_pos_, (int64_t) 2 * T * g_->n_head, cs_);
copy_i32_from_mapped(row_, m_row_, 2, cs_);
copy_from_mapped(Rin_, window_R_, (int64_t) T * HCN, cs_);
- // the catch-up: K/V for the window's T cells, then the full layer for row a only (its cell's K/V is written
- // again, identically), staged by the host in step row 2*max_t - 1; the draft chain is one graph per step
- // (`capture_step`) so the host can stop it when a draft is unlikely
+ // the catch-up: the layer's front for the window's T cells (their K/V), then its rest for row a only, on row
+ // a's intermediates copied to row 0, at the cell the host staged in step row 2*max_t - 1; the draft chain is
+ // one graph per step (`capture_step`) so the host can stop it when a draft is unlikely
const int ra = 2 * max_t_ - 1;
- ok = record_forward(T, -1, cs_, err);
- if (ok) mtp_select(Rin_, HCN, tok_, row_, Rin_, tok_, nullptr, 0, cs_);
+ const int64_t N = g_->n_embd;
+ ok = record_front(T, 0, cs_, err);
if (ok) {
+ if (T > 1) {
+ copy_row_to_first(row_, R_, HCN, inj_, g_->hc, mixed_, N, cs_);
+ native_quantize_q8_1(mixed_, xq_, (int) N, 1, cs_);
+ }
copy_i32_from_mapped(step_ + ra * 4, m_step_ + ra * 4, 4, cs_);
copy_i32_from_mapped(pos_ + ra * g_->n_head, m_pos_ + ra * g_->n_head, g_->n_head, cs_);
coupled_rec_ = coupled;
coupled_j_ = 0;
- ok = record_forward(1, ra, cs_, err);
+ ok = record_rest(ra, cs_, err);
coupled_rec_ = false;
}
if (ok) mtp_select(R_, HCN, out_ids_, row_ + 1, Rin_, tok_, m_out_, 0, cs_, probs_, m_prob_);
diff --git a/src/kernels/cuda/verify_kernels.cu b/src/kernels/cuda/verify_kernels.cu
index dc4e1c691..8fae405be 100644
--- a/src/kernels/cuda/verify_kernels.cu
+++ b/src/kernels/cuda/verify_kernels.cu
@@ -590,6 +590,24 @@ void ident_hits(const int32_t* ids, int n, int32_t* slot, int32_t* dst, int32_t*
check("ident_hits");
}
+namespace {
+__global__ void copy_row_to_first_kernel(const int32_t* __restrict__ row_dev, float* a, int64_t a_n, float* b,
+ int64_t b_n, float* c, int64_t c_n) {
+ const int64_t row = *row_dev;
+ if (row == 0) return;
+ const int64_t i0 = (int64_t) blockIdx.x * blockDim.x + threadIdx.x, st = (int64_t) gridDim.x * blockDim.x;
+ for (int64_t i = i0; i < a_n; i += st) a[i] = a[row * a_n + i];
+ for (int64_t i = i0; i < b_n; i += st) b[i] = b[row * b_n + i];
+ for (int64_t i = i0; i < c_n; i += st) c[i] = c[row * c_n + i];
+}
+} // namespace
+
+void copy_row_to_first(const int32_t* row_dev, float* a, int64_t a_n, float* b, int64_t b_n, float* c, int64_t c_n,
+ void* stream) {
+ copy_row_to_first_kernel<<<16, 256, 0, (cudaStream_t) stream>>>(row_dev, a, a_n, b, b_n, c, c_n);
+ check("copy_row_to_first");
+}
+
void mtp_select(const float* R_src, int64_t R_stride, const int32_t* ids, const int32_t* row_dev, float* R_dst,
int32_t* tok_dst, int32_t* out, int j, void* stream, const float* probs, float* out_p) {
mtp_select_kernel<<<1, 256, 0, (cudaStream_t) stream>>>(R_src, R_stride, ids, row_dev, R_dst, tok_dst, out, j,