Repository navigation
dflash: fix draft-feature capture on hyper-connection models #113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -284,6 +284,10 @@ struct DFlashGpu { | |
| layers: Vec<DFlashLayer>, | ||
| /// `[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::<f32>(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::<f32>(n)?; gpu.put(&vec![12.0f32; n], &b, 0)?; b }, | ||
| dfm: gpu.zeros::<f32>(TMAX * c.n_embd)?, | ||
| th: gpu.zeros::<f32>(TMAX * dc.n_embd)?, | ||
| x: gpu.zeros::<f32>(TMAX * dc.n_embd)?, | ||
| h: gpu.zeros::<f32>(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. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔵 Low · Performance · 6 of 6 The step path issues one dtod per row inside the batch, with the hc mean now computed as a separate kernel launch each step, rather than producing the feature in a single pass. Each decode step launches hc_mix and nt separate copy operations for every captured layer. With many layers and drafts, launch overhead grows, even though a fused or strided copy could move the rows in one operation. Prompt for your AI agentReply |
||
| 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)?; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 High · Correctness · 1 of 6 The 12.0 gate gives sigmoid(12) ≈ 0.999994, so the drafter feature is only approximately the hc mean, not exactly the mean the comment claims. The feature is scaled by about 0.999994 on every hc target. The error is small but systematic, so drafter outputs can drift from the llama.cpp reference that the comment says it mirrors, and a numerical comparison test would fail on tight tolerances. Prompt for your AI agentReply There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔵 Low · Maintainability · 4 of 6 A constant gate buffer is used to fake a plain mean, instead of a mean kernel or a flag on the mixing path. Correctness now depends on hc_mix's undocumented gate semantics (sigmoid and division by hc) staying fixed. Any change to that kernel silently breaks the feature. A direct mean operation would be simpler and independent of the gate. Prompt for your AI agentReply |
||
| &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(()) | ||
| })?; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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<Buf>, | ||
| /// `[chunk, n_ids * n_embd]` post-layer hidden states captured for DFlash `target_layer_ids`. | ||
| dflash_feat: Option<Buf>, | ||
| /// `[chunk, n_embd]` hc stream mean (hc targets), input to the DFlash capture. | ||
| dflash_mean: Option<Buf>, | ||
| /// ids | xrow | inv, `chunk * k` each. | ||
| ints: Buf, | ||
| tiles: Buf, | ||
|
|
@@ -85,6 +88,7 @@ impl Scratch { | |
| slab: gpu.zeros::<f32>(total)?, | ||
| feat: if c.eagle3.is_some() { Some(gpu.zeros::<f32>(3 * ne * chunk)?) } else { None }, | ||
| dflash_feat: c.dflash.as_ref().map(|dc| gpu.zeros::<f32>(dc.target_layer_ids.len() * ne * chunk)).transpose()?, | ||
| dflash_mean: if c.dflash.is_some() && c.hc_n > 0 { Some(gpu.zeros::<f32>(ne * chunk)?) } else { None }, | ||
| ints: gpu.zeros::<i32>(3 * chunk * k)?, tiles: gpu.zeros::<u64>(2 * n_tiles)?, | ||
| staging: gpu.zeros::<u8>(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(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔵 Low · Maintainability · 5 of 6 The DFlash capture loop (hc_mix tiling and per-row dtod copies) is duplicated between prefill and the step path with near-identical logic. A future fix to the row-copy or tiling logic must be made in two places, and they can drift apart. A shared helper taking the source buffer and row count would remove the duplication. Prompt for your AI agentReply |
||
| 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. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 High · Correctness · 2 of 6 Prefill tests Prompt for your AI agentReply |
||
| 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)?; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔵 Low · Performance · 3 of 6
dfg is allocated and filled from the host with 12.0 for every DFlash model, and dfm is allocated, even when hc_n is 0 and neither is used. Non-hc DFlash models pay TMAX*n_embd extra device memory for dfm plus a host-built vector upload at construction for dfg, with no effect on output. Allocation should be gated on hc_n > 0, as Scratch::dflash_mean already is.
Prompt for your AI agent
Reply
/dismiss(optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.