Repository navigation
inkling2: batch host expert matvecs (with natural-prompt benchmarks) #80
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 |
|---|---|---|
|
|
@@ -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. | ||
| pub fn matvec_many_cpu<'a>(ms: &[Mat<'a>], xs: &[&[f32]]) -> Option<Vec<f32>> { | ||
|
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 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 agentReply |
||
| 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 | ||
|
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 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 agentReply |
||
| || 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; | ||
|
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. 🟡 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 agentReply |
||
| } | ||
| }); | ||
| 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; } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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(); | ||
|
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 · 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 agentReply |
||
| 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) { | ||
|
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 · 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 agentReply |
||
| 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); | ||
|
|
||
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.
🟠 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
Reply
/dismiss(optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.