From edc592b1ef321fd9e89d93a0e77f0ad17d686993 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Mon, 5 Oct 2026 10:12:20 +0200 Subject: [PATCH] F7: the final mixer in one fused read (fork 2ec7a80) The final mixer in one fused read. The hyper-connection read before the head ran gr_read's kernels once per token of the window (lm_head_mix), and once per draft pass in the draft layer. Both now go through fused_gr_read_multi, the layers' read (no injection weight: the final mixer), the window's tokens in one launch. Its sums are gr_read's (fused_gr_read_multi is bitwise fused_gr_read, which gr_parity checks against the plain read). CUDA only; HIP keeps gr_read per token (STRATA_HEAD_MIX_MULTI=0 does too). This tree's opt-in STRATA_FUSE_HEAD_GR path is unchanged. Ported from eddoursul's fork, commit 2ec7a80 (MIT). Co-authored-by: Eddoursul --- src/core/mtp.cpp | 19 +++++++++++++++++++ src/core/verify.cpp | 29 +++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/src/core/mtp.cpp b/src/core/mtp.cpp index 2784c0a3..9941e55c 100644 --- a/src/core/mtp.cpp +++ b/src/core/mtp.cpp @@ -796,6 +796,14 @@ bool MtpDrafter::record_rest(int step_row, cudaStream_t cs, std::string& err) { const char* v = std::getenv("STRATA_FUSE_HEAD_GR"); return v != nullptr && std::atoi(v) != 0; }(); + static const bool head_mix_multi_on = [] { +#if defined(STRATA_USE_HIP) + return false; +#else + const char* v = std::getenv("STRATA_HEAD_MIX_MULTI"); + return v == nullptr || std::atoi(v) != 0; +#endif + }(); // the shared expert only reads mixed_ / xq_ and writes shared_: a branch beside the router and the routed // experts, joined before the combine (CUDA; HIP keeps one stream: STRATA_MTP_SHARED_BRANCH=0 does too) static const bool branch_on = [] { @@ -863,6 +871,17 @@ bool MtpDrafter::record_rest(int step_row, cudaStream_t cs, std::string& err) { fa[t].inject_out = dummy_inj_; fa[t].mixed = sample_ + t * N; } fused_gr_read_multi(fa, T, xn_, cs); + } else if (head_mix_multi_on) { + // the window's rows in one read (CUDA; HIP keeps gr_read per row): bitwise the same sums + FusedGrArgs fa[kFusedGrMaxT]; + for (int t = 0; t < T; ++t) { + fa[t].R = R_ + (size_t) t * HC * N; fa[t].R_out = R_ + (size_t) t * HC * N; fa[t].apply = false; + fa[t].w_norm = f32("hyper_connection_mixer.hc_norm.weight"); + fa[t].w_down = bf16("hyper_connection_mixer.input_mix_weight_down.weight"); + fa[t].w_up = bf16("hyper_connection_mixer.input_mix_weight_up.weight"); + fa[t].eps = EPS; fa[t].lo = lo_ + t * g.hc_lr; fa[t].rs = rs_ + t * HC; fa[t].mixed = sample_ + t * N; + } + fused_gr_read_multi(fa, T, xn_, cs); } else { for (int t = 0; t < T; ++t) gr_read(R_ + (size_t) t * HC * N, f32("hyper_connection_mixer.hc_norm.weight"), diff --git a/src/core/verify.cpp b/src/core/verify.cpp index 9ca5781d..5439d2a7 100644 --- a/src/core/verify.cpp +++ b/src/core/verify.cpp @@ -107,6 +107,20 @@ const bool g_sh_stream = [] { // 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). +// The head's hyper-connection read for the window's tokens in one launch set (CUDA; HIP keeps gr_read per token; +// STRATA_HEAD_MIX_MULTI=0 does too): bitwise the same sums. +bool head_mix_multi_enabled() { +#if defined(STRATA_USE_HIP) + return false; +#else + static const bool on = [] { + const char* v = std::getenv("STRATA_HEAD_MIX_MULTI"); + return v == nullptr || std::atoi(v) != 0; + }(); + return on; +#endif +} + bool one_token_self_commit() { #if defined(STRATA_USE_HIP) return false; @@ -1300,6 +1314,21 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) { a.mixed = head_mixed_ + t * N; } fused_gr_read_multi(fa, T, xn_, cs); + } else if (head_mix_multi_enabled() && head_ != nullptr && head_->loaded()) { + // the final mixer, the window's tokens in one read (fused_gr_read_multi without the pending write: the + // last layer's write was done above; its sums are gr_read's) + if (hn->kind != WeightKind::F32 || hd->kind != WeightKind::Bf16InF32 || hu->kind != WeightKind::Bf16InF32) { + err = "verify: the output_hc_* weights have the wrong engine forms"; + return false; + } + FusedGrArgs fa[kFusedGrMaxT]; + for (int t = 0; t < T; ++t) { + fa[t].R = Rt(t); fa[t].R_out = Rt(t); fa[t].apply = false; + fa[t].w_norm = (const float*) hn->data; fa[t].w_down = (const uint16_t*) hd->data; + fa[t].w_up = (const uint16_t*) hu->data; fa[t].eps = EPS; + fa[t].lo = lo_ + t * g.hc_lr; fa[t].rs = rs_ + t * HC; fa[t].mixed = head_mixed_ + t * N; + } + fused_gr_read_multi(fa, T, xn_, cs); } else { for (int t = 0; t < T; ++t) { BlockBuffers bb = ss.block;