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
8 changes: 6 additions & 2 deletions src/cpu_moe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1482,11 +1482,15 @@ pub fn matvec(t: &Tensor, x: &[f32], y: &mut [f32]) {
});
}
let Some(dot) = int_dot(t.ty) else { return t.matvec(x, y) };
let xq = quantize_q8k(x);
matvec_q8k(t, &quantize_q8k(x), y, dot);
}

pub(crate) fn matvec_q8k(t: &Tensor, xq: &[Q8K], y: &mut [f32], dot: fn(&[u8], &[Q8K]) -> f32) {
let rb = t.ty.row_bytes(t.cols as u64);
y.par_chunks_mut(32).enumerate().for_each(|(c, ys)| {
for (k, v) in ys.iter_mut().enumerate() {
let r = c * 32 + k;
*v = dot(&t.data[r * rb..(r + 1) * rb], &xq) * t.scale;
*v = dot(&t.data[r * rb..(r + 1) * rb], xq) * t.scale;
}
});
}
Expand Down
38 changes: 34 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ struct Engine {
/// Run on the GPU (experts that do not fit in VRAM run on the CPU) instead of the CPU reference.
#[arg(long)]
gpu: bool,
/// Accelerate CPU generation using Q8_K activations. This can change logits and generated tokens.
#[arg(long)]
fast_cpu: bool,

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 4

New --fast-cpu flag defaults to off, so default CPU generation loses the quantized kernels the PR enables. Before this push, CPU generate/chat/serve/bench used with_fast_matvec(!engine.gpu), so the quantized kernels ran by default. Now load_inference_model only enables them when the bool --fast-cpu is passed. A user running the reported model with the same flags as before gets the floating-point path again, so the claimed 0.6 to 5.7 tps prefill and 0.4 to 5.5 tps decode gain does not occur.

Prompt for your AI agent
File: src/main.rs
Line: 34
Symbol: Engine
Issue: New --fast-cpu flag defaults to off, so default CPU generation loses the quantized kernels the PR enables. Before this push, CPU generate/chat/serve/bench used with_fast_matvec(!engine.gpu), so the quantized kernels ran by default. Now load_inference_model only enables them when the bool --fast-cpu is passed. A user running the reported model with the same flags as before gets the floating-point path again, so the claimed 0.6 to 5.7 tps prefill and 0.4 to 5.5 tps decode gain does not occur.
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.

/// GPU context length (KV cache size) in tokens. Inkling 2, Hy4, GLM, DeepSeek and Gemma 4: the prompt plus reply
/// limit, the model's trained context when not given (their caches grow with the text).
#[arg(long, default_value_t = 4096)]
Expand Down Expand Up @@ -786,6 +789,10 @@ fn chunk_env_updates(n: usize, present: impl Fn(&str) -> bool) -> Vec<(&'static
}

impl Engine {
fn load_inference_model<'a>(&self, g: &'a Gguf, aux: Option<&'a Gguf>) -> anyhow::Result<Model<'a>> {
Model::load_with_matvec(g, aux, &self.lora_specs(), self.fast_cpu && !self.gpu)
}

pub fn lora_specs(&self) -> Vec<(String, f32)> {
self.lora.iter().map(|s| {
if let Some(idx) = s.rfind(':') {
Expand Down Expand Up @@ -1365,7 +1372,7 @@ fn run(cmd: Cmd) -> anyhow::Result<()> {
return rusting_strata::inkling2::chat::generate_image_opts(&g, &prompt, Some((&input.grids, &input.embeddings)), n, opts);
}
let aux_g = open_mtp_gguf(engine.aux_model_path())?;
let m = Model::load_with_aux(&g, aux_g.as_ref(), &engine.lora_specs())?;
let m = engine.load_inference_model(&g, aux_g.as_ref())?;
let tok = Tokenizer::from_gguf(&g)?;

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

The same load-then-set-flag chain is copied into four call sites. Each of the four sites (three in run, one in serve_model) repeats load_with_aux followed by .with_fast_matvec(!engine.gpu). A future load site that omits the call silently gets the slow path, and the GPU-to-CPU mapping lives in four places. Passing the flag into Model::load_with_aux once would remove the duplication.

Prompt for your AI agent
File: src/main.rs
Line: 1370
Symbol: run
Issue: The same load-then-set-flag chain is copied into four call sites. Each of the four sites (three in run, one in serve_model) repeats load_with_aux followed by .with_fast_matvec(!engine.gpu). A future load site that omits the call silently gets the slow path, and the GPU-to-CPU mapping lives in four places. Passing the flag into Model::load_with_aux once 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.

let prompt = if prompt == "-" {
let mut s = String::new();
Expand Down Expand Up @@ -1470,7 +1477,7 @@ fn run(cmd: Cmd) -> anyhow::Result<()> {
}
}
let aux_g = open_mtp_gguf(engine.aux_model_path())?;
let m = Model::load_with_aux(&g, aux_g.as_ref(), &engine.lora_specs())?;
let m = engine.load_inference_model(&g, aux_g.as_ref())?;
let tok = Tokenizer::from_gguf(&g)?;
let mut r = engine.runner(&m, &model)?;
let mut results = Vec::with_capacity(pp.len());
Expand Down Expand Up @@ -1706,7 +1713,7 @@ fn run(cmd: Cmd) -> anyhow::Result<()> {
};
}
let aux_g = open_mtp_gguf(engine.aux_model_path())?;
let m = Model::load_with_aux(&g, aux_g.as_ref(), &engine.lora_specs())?;
let m = engine.load_inference_model(&g, aux_g.as_ref())?;
let tok = Tokenizer::from_gguf(&g)?;
let image_input = if image.is_empty() { None } else {
let mmproj = engine.mmproj.clone().or_else(|| vision::find_sibling(&model));
Expand Down Expand Up @@ -3635,7 +3642,7 @@ fn serve_model(model: &Path, mut engine: Engine, s: &Serving, w: &mut Option<Tcp
})
} else {
aux_g = open_mtp_gguf(engine.aux_model_path())?;
m = { let mut m = Model::load_with_aux(&g, aux_g.as_ref(), &engine.lora_specs())?; if !engine.lora.is_empty() { m.apply_lora(&engine.lora_specs())?; } m };
m = engine.load_inference_model(&g, aux_g.as_ref())?;
engine.runner(&m, model)?
};
// Serving decodes one reply after a (usually cached) prompt: follow the reply's routing, not the prompt's.
Expand Down Expand Up @@ -4320,6 +4327,29 @@ mod bench_tests {
use serde_json::Value;
use std::path::Path;

#[test]
fn cpu_inference_loader_selects_requested_numerics() {
use super::{Cmd, Gguf, Model, State, ops};
let Some(path) = std::env::var_os("STRATA_CPU_PARITY_MODEL") else { return };

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

CPU parity test returns early and passes silently when the env var is unset. Without STRATA_CPU_PARITY_MODEL set, the test returns immediately and reports success. CI never checks that CPU generation logits match the reference path, so a regression in the loader or kernels goes unnoticed.

Prompt for your AI agent
File: src/main.rs
Line: 4333
Symbol: meta
Issue: CPU parity test returns early and passes silently when the env var is unset. Without STRATA_CPU_PARITY_MODEL set, the test returns immediately and reports success. CI never checks that CPU generation logits match the reference path, so a regression in the loader or kernels goes unnoticed.
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 g = Gguf::open(Path::new(&path)).unwrap();
let reference = Model::load(&g).unwrap();
let Some(Cmd::Bench { mut engine, .. }) = Cli::parse_from(["rustingstrata", "bench", "--model", "fixture"]).cmd else { unreachable!() };
let model = engine.load_inference_model(&g, None).unwrap();
let mut a = State::new(&reference.cfg);
let mut b = State::new(&model.cfg);
let mut token = 42;
for _ in 0..4 {
let want = reference.step(&mut a, token);
let got = model.step(&mut b, token);
assert_eq!(got, want, "CPU generation logits changed at step {}", b.pos);
token = ops::argmax(&want) as u32;
}
engine.fast_cpu = true;
let fast = engine.load_inference_model(&g, None).unwrap();
let expected = Model::load_with_matvec(&g, None, &[], true).unwrap();
assert_eq!(fast.step(&mut State::new(&fast.cfg), token), expected.step(&mut State::new(&expected.cfg), token));

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

Final assertion compares two identically configured models, so it checks nothing. expected is built with load_with_matvec(..., true) and fast is built from engine.load_inference_model with fast_cpu=true, which is the same configuration. The assertion passes whether or not the engine wiring is correct, so the test does not verify that the flag selects the requested numerics.

Prompt for your AI agent
File: src/main.rs
Line: 4350
Symbol: parsed
Issue: Final assertion compares two identically configured models, so it checks nothing. expected is built with load_with_matvec(..., true) and fast is built from engine.load_inference_model with fast_cpu=true, which is the same configuration. The assertion passes whether or not the engine wiring is correct, so the test does not verify that the flag selects the requested numerics.
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.

}

fn meta() -> BenchMeta<'static> {
BenchMeta {
backend: "cpu",
Expand Down
Loading