diff --git a/src/deepseek/mat.rs b/src/deepseek/mat.rs index 05ac70cb..5fae6ec3 100644 --- a/src/deepseek/mat.rs +++ b/src/deepseek/mat.rs @@ -487,6 +487,31 @@ pub fn matvec_many(ms: &[Mat], x: &[f32], xs: usize) -> Vec { } /// 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> { + 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 + || 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; + } + }); + Some(y) +} + pub fn matvec_many_opt(ms: &[Mat], x: &[f32], xs: usize) -> Option> { static ON: std::sync::OnceLock = std::sync::OnceLock::new(); if !*ON.get_or_init(|| std::env::var("STRATA_DS_BATCH").ok().as_deref() != Some("0")) { return None; } diff --git a/src/inkling2/forward.rs b/src/inkling2/forward.rs index f6d42012..5ad7bc7c 100644 --- a/src/inkling2/forward.rs +++ b/src/inkling2/forward.rs @@ -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 = list.iter().map(|e| e.0).collect(); + let downs: Vec = list.iter().map(|e| e.1).collect(); + let gs: Vec = list.iter().map(|e| e.3).collect(); + let gx: Vec<&[f32]> = vec![x; list.len()]; + if let Some(y) = matvec_many_cpu(&gus, &gx) { + let a: Vec = 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);