From 32bde8827e28a5b81ecd30ee65c19fc614942416 Mon Sep 17 00:00:00 2001 From: ssarules-tech <283043461+SSA1911@users.noreply.github.com> Date: Wed, 25 Nov 2026 20:21:18 +0400 Subject: [PATCH] dflash: fix draft-feature capture on hyper-connection models The draft-feature capture read one hyper-connection stream instead of the average of all of them, so standalone DFlash2 drafters were fed inputs they were never trained on (44% draft acceptance on an 8k-context bench; 100% after this fix). Average the streams before the capture, in both the per-token and prefill paths. --- src/gpu_model.rs | 19 ++++++++++++++++++- src/gpu_model/prefill.rs | 24 ++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/gpu_model.rs b/src/gpu_model.rs index 8efc204..71501d6 100644 --- a/src/gpu_model.rs +++ b/src/gpu_model.rs @@ -284,6 +284,10 @@ struct DFlashGpu { layers: Vec, /// `[TMAX, n_target_layers * n_embd]` concatenated post-layer target hidden states. feat: Buf, + /// hc stream gate for the mean collapse (all large => plain mean), `[hc_n * n_embd]`. + dfg: Buf, + /// hc-mean collapse output, `[TMAX, n_embd]`. + dfm: Buf, /// `[TMAX, n_embd]` projected & normalized `target_hidden = hidden_norm(fc(feat))` from the last forward. th: Buf, x: Buf, @@ -876,6 +880,10 @@ impl<'a> GpuModel<'a> { norm: up("dflash.norm.weight")?, layers: dl_layers, feat: gpu.zeros::(TMAX * dc.target_layer_ids.len() * c.n_embd)?, + // hc_mix indexes its gate buffer per position (g[t*hc*n + c*n + i]), so it needs + // TMAX rows; 12.0 => sigmoid ~ 1 => plain stream mean. + dfg: { let n = (TMAX * c.hc_n * c.n_embd).max(1); let b = gpu.zeros::(n)?; gpu.put(&vec![12.0f32; n], &b, 0)?; b }, + dfm: gpu.zeros::(TMAX * c.n_embd)?, th: gpu.zeros::(TMAX * dc.n_embd)?, x: gpu.zeros::(TMAX * dc.n_embd)?, h: gpu.zeros::(TMAX * dc.n_embd)?, @@ -2000,9 +2008,18 @@ impl<'a> GpuModel<'a> { if let Some(idx) = df.cfg.target_layer_ids.iter().position(|&tl| tl == il) { if res != 0 { g.add(x, res, nt * ne)?; res = 0; } let n_ids = df.cfg.target_layer_ids.len(); + // hc targets (qwen4exp): the drafter reads the plain mean of the hc streams + // (mirrors llama.cpp's hc_stream_mean); hc_mix with a constant large gate + // yields sum * sigmoid(g) / hc = mean. + let src = if self.m.cfg.hc_n > 0 { + g.hc_mix(self.x.addr(), df.dfg.addr(), df.dfm.addr(), ne, self.m.cfg.hc_n, nt)?; + &df.dfm + } else { + &self.x + }; g.batch(&mut || { for r in 0..nt { - g.dtod(&self.x, 4 * r * ne, &df.feat, 4 * (r * n_ids + idx) * ne, 4 * ne)?; + g.dtod(src, 4 * r * ne, &df.feat, 4 * (r * n_ids + idx) * ne, 4 * ne)?; } Ok(()) })?; diff --git a/src/gpu_model/prefill.rs b/src/gpu_model/prefill.rs index 9e1344a..f96d50a 100644 --- a/src/gpu_model/prefill.rs +++ b/src/gpu_model/prefill.rs @@ -3,6 +3,7 @@ //! copied into a small staging area, a group of them at a time, each group's copy beside the previous group's GEMMs. use super::{Ffn, GpuModel, HcAt, Mixer}; +use crate::cpu_worker::TMAX; use crate::backend::{BATCHED, Backend, Buf, GEMM_BM, SAME_X}; use crate::expert_cache::MATS; use crate::model::Model; @@ -24,6 +25,8 @@ pub(super) struct Scratch { feat: Option, /// `[chunk, n_ids * n_embd]` post-layer hidden states captured for DFlash `target_layer_ids`. dflash_feat: Option, + /// `[chunk, n_embd]` hc stream mean (hc targets), input to the DFlash capture. + dflash_mean: Option, /// ids | xrow | inv, `chunk * k` each. ints: Buf, tiles: Buf, @@ -85,6 +88,7 @@ impl Scratch { slab: gpu.zeros::(total)?, feat: if c.eagle3.is_some() { Some(gpu.zeros::(3 * ne * chunk)?) } else { None }, dflash_feat: c.dflash.as_ref().map(|dc| gpu.zeros::(dc.target_layer_ids.len() * ne * chunk)).transpose()?, + dflash_mean: if c.dflash.is_some() && c.hc_n > 0 { Some(gpu.zeros::(ne * chunk)?) } else { None }, ints: gpu.zeros::(3 * chunk * k)?, tiles: gpu.zeros::(2 * n_tiles)?, staging: gpu.zeros::(if c.is_moe() { 3 * STAGE * stage_bytes } else { 0 })?, }) @@ -775,8 +779,24 @@ impl GpuModel<'_> { if let (Some(dff), Some(df)) = (&pf.dflash_feat, self.dflash.as_ref()) { if let Some(idx) = df.cfg.target_layer_ids.iter().position(|&tl| tl == il) { let n_ids = df.cfg.target_layer_ids.len(); - for r in 0..t { - g.dtod(&pf.slab, 4 * r * ne, dff, 4 * (r * n_ids + idx) * ne, 4 * ne)?; + // hc targets (qwen4exp): collapse the hc streams to their plain mean first + // (mirrors llama.cpp's hc_stream_mean); see the step-side note. hc_mix's gate + // buffer holds TMAX rows, so tile over TMAX-position blocks. + if c.hc_n > 0 { + let dfm = pf.dflash_mean.as_ref().expect("dflash_mean with hc"); + let mut done = 0; + while done < t { + let rows = (t - done).min(TMAX); + g.hc_mix(pf.slab.addr() + 4 * (done * c.hc_n * ne) as u64, df.dfg.addr(), dfm.addr() + 4 * (done * ne) as u64, ne, c.hc_n, rows)?; + for r in 0..rows { + g.dtod(dfm, 4 * (done + r) * ne, dff, 4 * ((done + r) * n_ids + idx) * ne, 4 * ne)?; + } + done += rows; + } + } else { + for r in 0..t { + g.dtod(&pf.slab, 4 * r * ne, dff, 4 * (r * n_ids + idx) * ne, 4 * ne)?; + } } } }