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
25 changes: 25 additions & 0 deletions src/deepseek/mat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,31 @@ pub fn matvec_many(ms: &[Mat], x: &[f32], xs: usize) -> Vec<f32> {
}

/// The batched path of `matvec_many`, or None when the matrices cannot share one GPU batch (or batching is off).
/// Host integer dot of several matrices, one activation each, in a single Rayon pass.

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

New fn is inserted between matvec_many_opt's doc comment and its definition, so the doc now describes matvec_many_cpu and matvec_many_opt loses its doc. Rustdoc renders the batched-path description under matvec_many_cpu and leaves matvec_many_opt undocumented. Maintainers reading the docs or hovering in an IDE get the wrong description for both functions.

Prompt for your AI agent
File: src/deepseek/mat.rs
Line: 490
Symbol: matvec_many_opt
Issue: New fn is inserted between matvec_many_opt's doc comment and its definition, so the doc now describes matvec_many_cpu and matvec_many_opt loses its doc. Rustdoc renders the batched-path description under matvec_many_cpu and leaves matvec_many_opt undocumented. Maintainers reading the docs or hovering in an IDE get the wrong description for both functions.
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.

pub fn matvec_many_cpu<'a>(ms: &[Mat<'a>], xs: &[&[f32]]) -> Option<Vec<f32>> {

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

New integer-dot CPU path has no test comparing it against the float reference path. A wrong row-to-matrix mapping in the partition_point logic, or a scale error, would produce wrong expert outputs with no failing test. The PR title mentions benchmarks, but no correctness test appears in the diff.

Prompt for your AI agent
File: src/deepseek/mat.rs
Line: 491
Symbol: matvec_many_opt
Issue: New integer-dot CPU path has no test comparing it against the float reference path. A wrong row-to-matrix mapping in the partition_point logic, or a scale error, would produce wrong expert outputs with no failing test. The PR title mentions benchmarks, but no correctness test appears in the diff.
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 ty = ms.first()?.ty;
if ms.len() != xs.len() || ms.iter().any(|m| m.ty != ty || m.gpu.is_some() || m.cols != xs[0].len())
|| xs.iter().any(|x| x.len() != xs[0].len()) { return None; }
if std::env::var_os("STRATA_CPU_REF").is_some() || xs[0].len() % ty.block_elems() != 0

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

std::env::var_os(STRATA_CPU_REF) is read on every call to matvec_many_cpu in the per-token expert path. The environment is queried once per layer per token. Caching the flag in a OnceLock or passing it through the config would remove that repeated lookup.

Prompt for your AI agent
File: src/deepseek/mat.rs
Line: 495
Issue: std::env::var_os(STRATA_CPU_REF) is read on every call to matvec_many_cpu in the per-token expert path. The environment is queried once per layer per token. Caching the flag in a OnceLock or passing it through the config would remove that repeated lookup.
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.

|| ms.iter().any(|m| !m.cpu && m.rows < 512) { return None; }
let dot = crate::cpu_moe::int_dot(ty)?;
let xq: Vec<_> = xs.par_iter().map(|x| crate::cpu_moe::quantize_q8k(x)).collect();
let rb = ty.row_bytes(xs[0].len() as u64);
let mut starts = Vec::with_capacity(ms.len());
let mut off = 0;
for m in ms { starts.push((off, *m)); off += m.rows; }
let mut y = vec![0f32; off];
y.par_chunks_mut(32).enumerate().for_each(|(c, ys)| {
for (k, o) in ys.iter_mut().enumerate() {
let r = c * 32 + k;
let i = starts.partition_point(|(s, _)| *s <= r) - 1;
let (s, m) = starts[i];
*o = dot(&m.data[(r - s) * rb..(r - s + 1) * rb], &xq[i]) * m.scale;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Correctness · 4 of 6

Row slice m.data[(r-s)rb..] is indexed with no check that data holds rowsrb bytes. A Mat whose data buffer is shorter than rows*row_bytes (for example a truncated mmap or a mismatched cols/ty) makes the Rayon worker panic on an out-of-range slice, which aborts the whole forward pass instead of returning None and falling back.

Prompt for your AI agent
File: src/deepseek/mat.rs
Line: 509
Symbol: matvec_rows
Issue: Row slice m.data[(r-s)*rb..] is indexed with no check that data holds rows*rb bytes. A Mat whose data buffer is shorter than rows*row_bytes (for example a truncated mmap or a mismatched cols/ty) makes the Rayon worker panic on an out-of-range slice, which aborts the whole forward pass instead of returning None and falling back.
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.

}
});
Some(y)
}

pub fn matvec_many_opt(ms: &[Mat], x: &[f32], xs: usize) -> Option<Vec<f32>> {
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
if !*ON.get_or_init(|| std::env::var("STRATA_DS_BATCH").ok().as_deref() != Some("0")) { return None; }
Expand Down
16 changes: 15 additions & 1 deletion src/inkling2/forward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! The prompt runs layer by layer over a chunk of tokens: one GPU GEMM per dense matrix and per routed expert.
//! ponytail: the attention scores run on the CPU. Move them to the GPU when a long prompt waits on them.
use super::Inkling2Config;
use crate::deepseek::mat::{Mat, matvec_many, matvec_many_opt, matvec_rows, matvec_rows_many, matvec_rows_sets, situ_chunk, situ_experts};
use crate::deepseek::mat::{Mat, matvec_many, matvec_many_cpu, matvec_many_opt, matvec_rows, matvec_rows_many, matvec_rows_sets, situ_chunk, situ_experts};
use crate::deepseek::ops::{rms_norm, rms_norm_w};
use crate::gguf::Gguf;
use crate::quant::GgmlType;
Expand Down Expand Up @@ -428,6 +428,20 @@ fn run_experts(list: &[(Mat, Mat, f32, f32)], x: &[f32], cfg: &Inkling2Config) -
}
}
None => {
let gus: Vec<Mat> = list.iter().map(|e| e.0).collect();

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 · 3 of 6

list.iter().map(|e| e.0) moves Mat out of a shared reference, which only compiles if Mat is Copy. If Mat is not Copy, the build fails with E0507 (cannot move out of a shared reference). Cloning or borrowing into a Vec<&Mat> would be needed.

Prompt for your AI agent
File: src/inkling2/forward.rs
Line: 431
Symbol: run_experts
Issue: list.iter().map(|e| e.0) moves Mat out of a shared reference, which only compiles if Mat is Copy. If Mat is not Copy, the build fails with E0507 (cannot move out of a shared reference). Cloning or borrowing into a Vec<&Mat> would be needed.
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 downs: Vec<Mat> = list.iter().map(|e| e.1).collect();
let gs: Vec<f32> = list.iter().map(|e| e.3).collect();
let gx: Vec<&[f32]> = vec![x; list.len()];
if let Some(y) = matvec_many_cpu(&gus, &gx) {

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 · 2 of 6

Passing vec![x; n] makes matvec_many_cpu quantize the same activation n times, once per expert. With top-k experts per token, quantize_q8k runs on an identical input k times per layer per token, multiplying the Q8K quantization cost on the decode hot path. Quantizing x once and sharing the result would avoid it.

Prompt for your AI agent
File: src/inkling2/forward.rs
Line: 435
Symbol: run_experts
Issue: Passing vec![x; n] makes matvec_many_cpu quantize the same activation n times, once per expert. With top-k experts per token, quantize_q8k runs on an identical input k times per layer per token, multiplying the Q8K quantization cost on the decode hot path. Quantizing x once and sharing the result would avoid it.
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 a: Vec<f32> = y.chunks_exact(2 * nf).zip(&gs).flat_map(|(y, &s)| situ(y, s, cfg)).collect();
let acts: Vec<&[f32]> = a.chunks_exact(nf).collect();
if let Some(dy) = matvec_many_cpu(&downs, &acts) {
for (e, dy) in list.iter().zip(dy.chunks_exact(nl)) {
for (m, d) in mix.iter_mut().zip(dy) { *m += e.2 * d; }
}
return mix;
}
}
let (mut y, mut dy) = (vec![0f32; 2 * nf], vec![0f32; nl]);
for (gu, down, w, s) in list {
gu.matvec(x, &mut y);
Expand Down
Loading