Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/gpu_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.

Copy link
Copy Markdown

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
File: src/gpu_model.rs
Line: 884
Symbol: load_part
Issue: 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.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.

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)?,
Expand Down Expand Up @@ -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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 agent
File: src/gpu_model.rs
Line: 2013
Symbol: forward
Issue: 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.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.

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)?;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 agent
File: src/gpu_model.rs
Line: 2015
Symbol: forward
Issue: 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.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 agent
File: src/gpu_model.rs
Line: 2015
Symbol: forward
Issue: 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.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.

&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(())
})?;
Expand Down
24 changes: 22 additions & 2 deletions src/gpu_model/prefill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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 })?,
})
Expand Down Expand Up @@ -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();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 agent
File: src/gpu_model/prefill.rs
Line: 781
Symbol: prefill_chunk
Issue: 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.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.

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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 High · Correctness · 2 of 6

Prefill tests c.hc_n but the surrounding code uses self.m.cfg; if c is not the model config, this does not compile or reads the wrong value. If c is a different struct (for example the DFlash config or a scratch descriptor) without an hc_n field, the build breaks. If it is a different config, hc targets take the wrong branch and the drafter reads unmixed streams.

Prompt for your AI agent
File: src/gpu_model/prefill.rs
Line: 784
Symbol: prefill_chunk
Issue: Prefill tests `c.hc_n` but the surrounding code uses `self.m.cfg`; if `c` is not the model config, this does not compile or reads the wrong value. If `c` is a different struct (for example the DFlash config or a scratch descriptor) without an hc_n field, the build breaks. If it is a different config, hc targets take the wrong branch and the drafter reads unmixed streams.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.

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)?;
}
}
}
}
Expand Down